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

201 lines
6.5 KiB
C++
Raw Normal View History

2017-07-22 22:08:25 +02:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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"
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-07-31 11:13:28 +02:00
static void DrawText(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, const_utf8string text);
2020-07-08 09:06:22 -03:00
static void DrawText(
2020-07-31 11:13:28 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, rct_string_id format, const void* args);
2017-07-22 22:08:25 +02:00
2020-07-31 11:13:28 +02:00
StaticLayout::StaticLayout(utf8string source, const TextPaint& paint, int32_t width)
2017-07-22 22:08:25 +02:00
{
2020-07-08 09:43:23 -03:00
Buffer = source;
Paint = paint;
2017-07-22 22:08:25 +02:00
int32_t fontSpriteBase;
2017-07-22 22:08:25 +02:00
2017-09-03 23:00:20 +01:00
gCurrentFontSpriteBase = paint.SpriteBase;
2020-07-08 09:43:23 -03:00
MaxWidth = gfx_wrap_string(Buffer, width, &LineCount, &fontSpriteBase);
LineCount += 1;
LineHeight = font_get_line_height(fontSpriteBase);
2017-07-22 22:08:25 +02:00
}
2020-07-08 09:14:43 -03:00
void StaticLayout::Draw(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords)
2017-07-22 22:08:25 +02:00
{
gCurrentFontFlags = 0;
2020-07-08 09:43:23 -03:00
gCurrentFontSpriteBase = Paint.SpriteBase;
2017-07-22 22:08:25 +02:00
2020-07-08 09:43:23 -03:00
TextPaint tempPaint = Paint;
2017-07-22 22:08:25 +02:00
gCurrentFontFlags = 0;
2020-07-08 09:14:43 -03:00
auto lineCoords = coords;
2020-07-08 09:43:23 -03:00
switch (Paint.Alignment)
2018-06-22 22:59:03 +02:00
{
case TextAlignment::LEFT:
break;
case TextAlignment::CENTRE:
2020-07-08 09:14:43 -03:00
lineCoords.x += MaxWidth / 2;
2018-06-22 22:59:03 +02:00
break;
case TextAlignment::RIGHT:
2020-07-08 09:14:43 -03:00
lineCoords.x += MaxWidth;
2018-06-22 22:59:03 +02:00
break;
2017-07-22 22:08:25 +02:00
}
2020-07-08 09:43:23 -03:00
utf8* buffer = Buffer;
for (int32_t line = 0; line < LineCount; ++line)
2017-09-03 23:00:20 +01:00
{
2020-07-31 11:13:28 +02:00
DrawText(dpi, lineCoords, tempPaint, buffer);
2017-09-03 23:00:20 +01:00
tempPaint.Colour = TEXT_COLOUR_254;
2017-07-22 22:08:25 +02:00
buffer = get_string_end(buffer) + 1;
2020-07-08 09:14:43 -03:00
lineCoords.y += LineHeight;
2017-07-22 22:08:25 +02:00
}
}
int32_t StaticLayout::GetHeight()
2017-07-22 22:08:25 +02:00
{
2020-07-08 09:43:23 -03:00
return LineHeight * LineCount;
2017-07-22 22:08:25 +02:00
}
int32_t StaticLayout::GetWidth()
2017-07-22 22:08:25 +02:00
{
2020-07-08 09:43:23 -03:00
return MaxWidth;
2017-07-22 22:08:25 +02:00
}
int32_t StaticLayout::GetLineCount()
2017-07-22 22:08:25 +02:00
{
2020-07-08 09:43:23 -03:00
return LineCount;
2017-07-22 22:08:25 +02:00
}
2020-07-31 11:13:28 +02:00
static void DrawText(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, const_utf8string text)
2017-07-22 22:08:25 +02:00
{
int32_t width = gfx_get_string_width(text);
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
}
2020-07-31 11:13:28 +02:00
ttf_draw_string(dpi, text, paint.Colour, alignedCoords);
2017-07-22 22:08:25 +02:00
2020-07-31 11:13:28 +02:00
if (paint.UnderlineText)
2017-09-03 23:00:20 +01:00
{
2020-07-08 09:06:22 -03:00
gfx_fill_rect(
dpi, { { alignedCoords + ScreenCoordsXY{ 0, 11 } }, { alignedCoords + ScreenCoordsXY{ width, 11 } } },
text_palette[1]);
2017-07-22 22:08:25 +02:00
if (text_palette[2] != 0)
2017-09-03 23:00:20 +01:00
{
2020-07-08 09:06:22 -03:00
gfx_fill_rect(
dpi, { { alignedCoords + ScreenCoordsXY{ 1, 12 } }, { alignedCoords + ScreenCoordsXY{ width + 1, 12 } } },
2020-07-08 09:06:22 -03:00
text_palette[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(
2020-07-31 11:13:28 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, const TextPaint& paint, rct_string_id format, const void* args)
2017-07-22 22:08:25 +02:00
{
utf8 buffer[512];
2017-07-22 22:08:25 +02:00
format_string(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
}
void DrawTextBasic(
2020-08-26 15:38:27 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, rct_string_id format, const void* args, colour_t colour,
TextAlignment alignment, bool underline)
2017-07-22 22:08:25 +02:00
{
2020-07-31 11:13:28 +02:00
TextPaint textPaint = { colour, FONT_SPRITE_BASE_MEDIUM, underline, alignment };
2017-07-22 22:08:25 +02:00
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
2020-07-31 11:13:28 +02:00
DrawText(dpi, coords, textPaint, format, args);
2017-07-22 22:08:25 +02:00
}
void DrawTextBasic(
2020-08-26 15:38:27 +02:00
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, rct_string_id format, const Formatter& ft, colour_t colour,
TextAlignment alignment, bool underline)
{
return DrawTextBasic(dpi, coords, format, ft.Data(), colour, alignment, underline);
}
void DrawTextEllipsised(
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, rct_string_id format, const Formatter& ft,
2020-08-26 15:38:27 +02:00
colour_t colour, TextAlignment alignment, bool underline)
2017-07-22 22:08:25 +02:00
{
return DrawTextEllipsised(dpi, coords, width, format, ft.Data(), colour, alignment, underline);
2017-07-22 22:08:25 +02:00
}
void gfx_draw_string(rct_drawpixelinfo* dpi, const_utf8string buffer, uint8_t colour, const ScreenCoordsXY& coords)
2017-07-22 22:08:25 +02:00
{
2020-07-31 11:13:28 +02:00
TextPaint textPaint = { colour, gCurrentFontSpriteBase, false, TextAlignment::LEFT };
DrawText(dpi, coords, textPaint, buffer);
2017-07-22 22:08:25 +02:00
}
2018-02-01 18:49:14 +01:00
// Basic
void gfx_draw_string_left(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
{
DrawTextBasic(dpi, coords, format, args, colour, TextAlignment::LEFT);
2018-02-01 18:49:14 +01:00
}
void gfx_draw_string_centred(
rct_drawpixelinfo* dpi, rct_string_id format, const ScreenCoordsXY& coords, uint8_t colour, const void* args)
2018-02-01 18:49:14 +01:00
{
DrawTextBasic(dpi, coords, format, args, colour, TextAlignment::CENTRE);
2018-02-01 18:49:14 +01:00
}
void gfx_draw_string_right(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
{
DrawTextBasic(dpi, coords, format, args, colour, TextAlignment::RIGHT);
}
2018-02-01 18:49:14 +01:00
// Wrapping
int32_t gfx_draw_string_left_wrapped(
rct_drawpixelinfo* dpi, void* args, const ScreenCoordsXY& coords, int32_t width, rct_string_id format, uint8_t colour)
2018-02-01 18:49:14 +01:00
{
utf8 buffer[512];
2018-02-01 18:49:14 +01:00
format_string(buffer, sizeof(buffer), format, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
2020-07-31 11:13:28 +02:00
TextPaint textPaint = { colour, gCurrentFontSpriteBase, false, TextAlignment::LEFT };
StaticLayout layout(buffer, textPaint, width);
2020-07-08 09:14:43 -03:00
layout.Draw(dpi, coords);
2018-02-01 18:49:14 +01:00
return layout.GetHeight();
}
2018-06-22 22:59:03 +02:00
int32_t gfx_draw_string_centred_wrapped(
rct_drawpixelinfo* dpi, void* args, const ScreenCoordsXY& coords, int32_t width, rct_string_id format, uint8_t colour)
2018-02-01 18:49:14 +01:00
{
utf8 buffer[512];
2018-02-01 18:49:14 +01:00
format_string(buffer, sizeof(buffer), format, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
2020-07-31 11:13:28 +02:00
TextPaint textPaint = { colour, gCurrentFontSpriteBase, false, TextAlignment::CENTRE };
StaticLayout layout(buffer, textPaint, width);
2018-02-01 18:49:14 +01:00
// 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;
2018-02-01 18:49:14 +01:00
2020-07-08 09:14:43 -03:00
layout.Draw(dpi, coords - ScreenCoordsXY{ layout.GetWidth() / 2, yOffset });
2018-02-01 18:49:14 +01:00
return layout.GetHeight();
}