Files
OpenRCT2-Web/src/openrct2/drawing/Text.cpp
T

202 lines
6.2 KiB
C++
Raw Normal View History

2017-07-22 22:08:25 +02:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2017-07-22 22:08:25 +02:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-07-22 22:08:25 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-07-22 22:08:25 +02:00
*****************************************************************************/
#include "Text.h"
2021-12-11 18:06:06 -05:00
#include "../localisation/Formatter.h"
#include "../localisation/Formatting.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Localisation.h"
2018-06-22 22:59:03 +02:00
#include "Drawing.h"
2017-07-22 22:08:25 +02:00
2020-11-22 00:36:40 +00:00
static void DrawText(
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, const_utf8string text,
bool noFormatting = false);
2020-07-08 09:06:22 -03:00
static void DrawText(
2022-07-31 14:22:58 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, StringId format, const void* args);
2017-07-22 22:08:25 +02:00
2021-09-26 19:55:16 +02:00
class StaticLayout
2017-07-22 22:08:25 +02:00
{
2021-09-26 19:55:16 +02:00
private:
utf8string Buffer;
TextPaint Paint;
int32_t LineCount = 0;
int32_t LineHeight;
int32_t MaxWidth;
2017-07-22 22:08:25 +02:00
2021-09-26 19:55:16 +02:00
public:
StaticLayout(utf8string source, const TextPaint& paint, int32_t width)
2018-06-22 22:59:03 +02:00
{
2021-09-26 19:55:16 +02:00
Buffer = source;
Paint = paint;
MaxWidth = GfxWrapString(Buffer, width, paint.FontStyle, &LineCount);
2021-09-26 19:55:16 +02:00
LineCount += 1;
LineHeight = FontGetLineHeight(paint.FontStyle);
2017-07-22 22:08:25 +02:00
}
2021-09-26 19:55:16 +02:00
void Draw(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords)
2017-09-03 23:00:20 +01:00
{
2021-09-26 19:55:16 +02:00
TextPaint tempPaint = Paint;
auto lineCoords = coords;
switch (Paint.Alignment)
{
case TextAlignment::LEFT:
break;
case TextAlignment::CENTRE:
lineCoords.x += MaxWidth / 2;
break;
case TextAlignment::RIGHT:
lineCoords.x += MaxWidth;
break;
}
utf8* buffer = Buffer;
for (int32_t line = 0; line < LineCount; ++line)
{
DrawText(dpi, lineCoords, tempPaint, buffer);
tempPaint.Colour = TEXT_COLOUR_254;
buffer = GetStringEnd(buffer) + 1;
2021-09-26 19:55:16 +02:00
lineCoords.y += LineHeight;
}
2017-07-22 22:08:25 +02:00
}
2021-09-26 19:55:16 +02:00
int32_t GetHeight() const
{
return LineHeight * LineCount;
}
2017-07-22 22:08:25 +02:00
2021-09-26 19:55:16 +02:00
int32_t GetWidth() const
{
return MaxWidth;
}
2017-07-22 22:08:25 +02:00
2021-09-26 19:55:16 +02:00
int32_t GetLineCount() const
{
return LineCount;
}
};
2017-07-22 22:08:25 +02:00
2020-11-22 00:36:40 +00:00
static void DrawText(
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, const_utf8string text, bool noFormatting)
2017-07-22 22:08:25 +02:00
{
int32_t width = noFormatting ? GfxGetStringWidthNoFormatting(text, paint.FontStyle)
: GfxGetStringWidth(text, paint.FontStyle);
2017-07-22 22:08:25 +02:00
2020-07-08 09:06:22 -03:00
auto alignedCoords = coords;
2020-07-31 11:13:28 +02:00
switch (paint.Alignment)
2018-06-22 22:59:03 +02:00
{
case TextAlignment::LEFT:
break;
case TextAlignment::CENTRE:
2020-07-08 09:06:22 -03:00
alignedCoords.x -= (width - 1) / 2;
2018-06-22 22:59:03 +02:00
break;
case TextAlignment::RIGHT:
2020-07-08 09:06:22 -03:00
alignedCoords.x -= width;
2018-06-22 22:59:03 +02:00
break;
2017-07-22 22:08:25 +02:00
}
TTFDrawString(dpi, text, paint.Colour, alignedCoords, noFormatting, paint.FontStyle, paint.Darkness);
2017-07-22 22:08:25 +02:00
2021-02-26 21:11:26 +01:00
if (paint.UnderlineText == TextUnderline::On)
2017-09-03 23:00:20 +01:00
{
GfxFillRect(
dpi, { { alignedCoords + ScreenCoordsXY{ 0, 11 } }, { alignedCoords + ScreenCoordsXY{ width, 11 } } },
gTextPalette[1]);
if (gTextPalette[2] != 0)
2017-09-03 23:00:20 +01:00
{
GfxFillRect(
dpi, { { alignedCoords + ScreenCoordsXY{ 1, 12 } }, { alignedCoords + ScreenCoordsXY{ width + 1, 12 } } },
gTextPalette[2]);
2017-09-03 23:00:20 +01:00
}
2017-07-22 22:08:25 +02:00
}
}
2020-07-08 09:06:22 -03:00
static void DrawText(
2022-07-31 14:22:58 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, StringId format, const void* args)
2017-07-22 22:08:25 +02:00
{
utf8 buffer[512];
OpenRCT2::FormatStringLegacy(buffer, sizeof(buffer), format, args);
2020-07-08 09:06:22 -03:00
DrawText(dpi, coords, paint, buffer);
2017-07-22 22:08:25 +02:00
}
2022-07-31 14:22:58 +02:00
void DrawTextBasic(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, StringId format)
2021-12-11 18:06:06 -05:00
{
Formatter ft{};
TextPaint textPaint{};
2021-12-11 18:06:06 -05:00
DrawTextBasic(dpi, coords, format, ft, textPaint);
}
2021-02-27 13:10:00 +01:00
void DrawTextBasic(
2022-07-31 14:22:58 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, StringId format, const Formatter& ft, TextPaint textPaint)
2021-02-27 13:10:00 +01:00
{
DrawText(dpi, coords, textPaint, format, ft.Data());
2021-02-27 13:10:00 +01:00
}
2022-07-31 14:22:58 +02:00
void DrawTextEllipsised(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format)
2021-12-11 18:06:06 -05:00
{
Formatter ft{};
TextPaint textPaint{};
2021-12-11 18:06:06 -05:00
DrawTextEllipsised(dpi, coords, width, format, ft, textPaint);
}
void DrawTextEllipsised(
2022-07-31 14:22:58 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format, const Formatter& ft,
TextPaint textPaint)
2017-07-22 22:08:25 +02:00
{
2020-08-27 14:35:37 +02:00
utf8 buffer[512];
OpenRCT2::FormatStringLegacy(buffer, sizeof(buffer), format, ft.Data());
GfxClipString(buffer, width, textPaint.FontStyle);
2020-08-27 14:35:37 +02:00
DrawText(dpi, coords, textPaint, buffer);
2017-07-22 22:08:25 +02:00
}
void GfxDrawString(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const_utf8string buffer, TextPaint textPaint)
2017-07-22 22:08:25 +02:00
{
2020-07-31 11:13:28 +02:00
DrawText(dpi, coords, textPaint, buffer);
2017-07-22 22:08:25 +02:00
}
2018-02-01 18:49:14 +01:00
void GfxDrawStringNoFormatting(
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const_utf8string buffer, TextPaint textPaint)
2020-11-22 00:36:40 +00:00
{
DrawText(dpi, coords, textPaint, buffer, true);
}
2022-07-31 14:22:58 +02:00
int32_t DrawTextWrapped(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format)
2021-12-11 18:06:06 -05:00
{
Formatter ft{};
TextPaint textPaint{};
2021-12-11 18:06:06 -05:00
return DrawTextWrapped(dpi, coords, width, format, ft, textPaint);
}
int32_t DrawTextWrapped(
2022-07-31 14:22:58 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format, const Formatter& ft,
TextPaint textPaint)
2018-02-01 18:49:14 +01:00
{
const void* args = ft.Data();
// TODO: Refactor StaticLayout to take a std::string_view instead. It shouldn't have to write to the buffer.
const std::string buffer = FormatStringID(format, args);
StaticLayout layout(const_cast<char*>(buffer.c_str()), textPaint, width);
if (textPaint.Alignment == TextAlignment::CENTRE)
{
// The original tried to vertically centre the text, but used line count - 1
int32_t lineCount = layout.GetLineCount();
int32_t lineHeight = layout.GetHeight() / lineCount;
int32_t yOffset = (lineCount - 1) * lineHeight / 2;
layout.Draw(dpi, coords - ScreenCoordsXY{ layout.GetWidth() / 2, yOffset });
}
else
{
layout.Draw(dpi, coords);
}
2018-02-01 18:49:14 +01:00
return layout.GetHeight();
}