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"
|
2023-01-17 21:56:30 +01:00
|
|
|
#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(
|
2023-01-19 08:16:44 +00:00
|
|
|
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(
|
2023-01-19 08:16:44 +00:00
|
|
|
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;
|
|
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
MaxWidth = GfxWrapString(Buffer, width, paint.FontStyle, &LineCount);
|
2021-09-26 19:55:16 +02:00
|
|
|
LineCount += 1;
|
2023-01-16 22:31:34 +00:00
|
|
|
LineHeight = FontGetLineHeight(paint.FontStyle);
|
2017-07-22 22:08:25 +02:00
|
|
|
}
|
2021-09-26 19:55:16 +02:00
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void Draw(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;
|
2023-01-17 00:12:35 +01:00
|
|
|
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(
|
2023-01-19 08:16:44 +00:00
|
|
|
DrawPixelInfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, const_utf8string text, bool noFormatting)
|
2017-07-22 22:08:25 +02:00
|
|
|
{
|
2023-01-16 18:52:17 +00: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
|
|
|
}
|
|
|
|
|
|
2023-01-16 22:31:34 +00: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
|
|
|
{
|
2023-01-16 13:50:43 +00:00
|
|
|
GfxFillRect(
|
2020-07-08 13:07:26 -04:00
|
|
|
dpi, { { alignedCoords + ScreenCoordsXY{ 0, 11 } }, { alignedCoords + ScreenCoordsXY{ width, 11 } } },
|
2022-07-30 14:21:34 -07:00
|
|
|
gTextPalette[1]);
|
|
|
|
|
if (gTextPalette[2] != 0)
|
2017-09-03 23:00:20 +01:00
|
|
|
{
|
2023-01-16 13:50:43 +00:00
|
|
|
GfxFillRect(
|
2020-07-08 13:07:26 -04:00
|
|
|
dpi, { { alignedCoords + ScreenCoordsXY{ 1, 12 } }, { alignedCoords + ScreenCoordsXY{ width + 1, 12 } } },
|
2022-07-30 14:21:34 -07:00
|
|
|
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(
|
2023-01-19 08:16:44 +00:00
|
|
|
DrawPixelInfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, StringId format, const void* args)
|
2017-07-22 22:08:25 +02:00
|
|
|
{
|
2018-08-13 20:34:06 +02:00
|
|
|
utf8 buffer[512];
|
2023-01-17 21:56:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void DrawTextBasic(DrawPixelInfo* dpi, const ScreenCoordsXY& coords, StringId format)
|
2021-12-11 18:06:06 -05:00
|
|
|
{
|
2021-12-12 12:33:09 -05:00
|
|
|
Formatter ft{};
|
|
|
|
|
TextPaint textPaint{};
|
2021-12-11 18:06:06 -05:00
|
|
|
DrawTextBasic(dpi, coords, format, ft, textPaint);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void DrawTextBasic(DrawPixelInfo* dpi, const ScreenCoordsXY& coords, StringId format, const Formatter& ft, TextPaint textPaint)
|
2021-02-27 13:10:00 +01:00
|
|
|
{
|
2021-07-27 20:32:27 +02:00
|
|
|
DrawText(dpi, coords, textPaint, format, ft.Data());
|
2021-02-27 13:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void DrawTextEllipsised(DrawPixelInfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format)
|
2021-12-11 18:06:06 -05:00
|
|
|
{
|
2021-12-12 12:33:09 -05:00
|
|
|
Formatter ft{};
|
|
|
|
|
TextPaint textPaint{};
|
2021-12-11 18:06:06 -05:00
|
|
|
DrawTextEllipsised(dpi, coords, width, format, ft, textPaint);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 14:44:30 +02:00
|
|
|
void DrawTextEllipsised(
|
2023-01-19 08:16:44 +00:00
|
|
|
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];
|
2023-01-17 21:56:30 +01:00
|
|
|
OpenRCT2::FormatStringLegacy(buffer, sizeof(buffer), format, ft.Data());
|
2023-01-16 18:52:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void GfxDrawString(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
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
void GfxDrawStringNoFormatting(DrawPixelInfo* dpi, const ScreenCoordsXY& coords, const_utf8string buffer, TextPaint textPaint)
|
2020-11-22 00:36:40 +00:00
|
|
|
{
|
|
|
|
|
DrawText(dpi, coords, textPaint, buffer, true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
int32_t DrawTextWrapped(DrawPixelInfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format)
|
2021-12-11 18:06:06 -05:00
|
|
|
{
|
2021-12-12 12:33:09 -05:00
|
|
|
Formatter ft{};
|
|
|
|
|
TextPaint textPaint{};
|
2021-12-11 18:06:06 -05:00
|
|
|
return DrawTextWrapped(dpi, coords, width, format, ft, textPaint);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-27 17:17:17 +01:00
|
|
|
int32_t DrawTextWrapped(
|
2023-01-19 08:16:44 +00:00
|
|
|
DrawPixelInfo* dpi, const ScreenCoordsXY& coords, int32_t width, StringId format, const Formatter& ft, TextPaint textPaint)
|
2018-02-01 18:49:14 +01:00
|
|
|
{
|
2021-08-21 09:42:05 +02:00
|
|
|
const void* args = ft.Data();
|
|
|
|
|
|
2021-09-27 15:03:25 +02:00
|
|
|
// TODO: Refactor StaticLayout to take a std::string_view instead. It shouldn't have to write to the buffer.
|
2023-01-17 09:59:07 +01:00
|
|
|
const std::string buffer = FormatStringID(format, args);
|
2021-09-27 15:03:25 +02:00
|
|
|
StaticLayout layout(const_cast<char*>(buffer.c_str()), textPaint, width);
|
2021-02-27 17:17:17 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|