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

248 lines
7.9 KiB
C++
Raw Normal View History

2017-07-22 22:08:25 +02:00
/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 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
static TextPaint _legacyPaint;
2018-06-22 22:59:03 +02:00
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, const_utf8string text);
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, rct_string_id format, void* args);
2017-07-22 22:08:25 +02:00
StaticLayout::StaticLayout(utf8string source, TextPaint paint, int32_t width)
2017-07-22 22:08:25 +02:00
{
_buffer = source;
_paint = paint;
int32_t fontSpriteBase;
2017-07-22 22:08:25 +02:00
2017-09-03 23:00:20 +01:00
gCurrentFontSpriteBase = paint.SpriteBase;
2017-07-22 22:08:25 +02:00
_maxWidth = gfx_wrap_string(_buffer, width, &_lineCount, &fontSpriteBase);
_lineCount += 1;
_lineHeight = font_get_line_height(fontSpriteBase);
}
2018-06-22 22:59:03 +02:00
void StaticLayout::Draw(rct_drawpixelinfo* dpi, int32_t x, int32_t y)
2017-07-22 22:08:25 +02:00
{
gCurrentFontFlags = 0;
2017-09-03 23:00:20 +01:00
gCurrentFontSpriteBase = _paint.SpriteBase;
2017-07-22 22:08:25 +02:00
TextPaint tempPaint = _paint;
gCurrentFontFlags = 0;
int32_t lineY = y;
int32_t lineX = x;
2018-06-22 22:59:03 +02:00
switch (_paint.Alignment)
{
case TextAlignment::LEFT:
lineX = x;
break;
case TextAlignment::CENTRE:
lineX = x + _maxWidth / 2;
break;
case TextAlignment::RIGHT:
lineX = x + _maxWidth;
break;
2017-07-22 22:08:25 +02:00
}
2018-06-22 22:59:03 +02:00
utf8* buffer = _buffer;
for (int32_t line = 0; line < _lineCount; ++line)
2017-09-03 23:00:20 +01:00
{
DrawText(dpi, lineX, lineY, &tempPaint, buffer);
tempPaint.Colour = TEXT_COLOUR_254;
2017-07-22 22:08:25 +02:00
buffer = get_string_end(buffer) + 1;
lineY += _lineHeight;
}
}
int32_t StaticLayout::GetHeight()
2017-07-22 22:08:25 +02:00
{
return _lineHeight * _lineCount;
}
int32_t StaticLayout::GetWidth()
2017-07-22 22:08:25 +02:00
{
return _maxWidth;
}
int32_t StaticLayout::GetLineCount()
2017-07-22 22:08:25 +02:00
{
return _lineCount;
}
2018-06-22 22:59:03 +02:00
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, 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
2018-06-22 22:59:03 +02:00
switch (paint->Alignment)
{
case TextAlignment::LEFT:
break;
case TextAlignment::CENTRE:
x -= (width - 1) / 2;
break;
case TextAlignment::RIGHT:
x -= width;
break;
2017-07-22 22:08:25 +02:00
}
2017-09-03 23:00:20 +01:00
ttf_draw_string(dpi, text, paint->Colour, x, y);
2017-07-22 22:08:25 +02:00
2017-09-03 23:00:20 +01:00
if (paint->UnderlineText)
{
2017-07-22 22:08:25 +02:00
gfx_fill_rect(dpi, x, y + 11, x + width, y + 11, text_palette[1]);
if (text_palette[2] != 0)
2017-09-03 23:00:20 +01:00
{
2017-07-22 22:08:25 +02:00
gfx_fill_rect(dpi, x + 1, y + 12, x + width + 1, y + 12, text_palette[2]);
2017-09-03 23:00:20 +01:00
}
2017-07-22 22:08:25 +02:00
}
}
2018-06-22 22:59:03 +02:00
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, rct_string_id format, void* args)
2017-07-22 22:08:25 +02:00
{
utf8 buffer[256];
format_string(buffer, sizeof(buffer), format, args);
2017-09-03 23:00:20 +01:00
DrawText(dpi, x, y, paint, buffer);
2017-07-22 22:08:25 +02:00
}
2018-06-22 22:59:03 +02:00
static void DrawTextCompat(
2018-07-21 13:51:54 +02:00
rct_drawpixelinfo* dpi, int32_t x, int32_t y, rct_string_id format, void* args, uint8_t colour, TextAlignment alignment,
2018-06-22 22:59:03 +02:00
bool underline = false)
2017-07-22 22:08:25 +02:00
{
2017-09-03 23:00:20 +01:00
_legacyPaint.UnderlineText = underline;
_legacyPaint.Colour = colour;
_legacyPaint.Alignment = alignment;
_legacyPaint.SpriteBase = FONT_SPRITE_BASE_MEDIUM;
2017-07-22 22:08:25 +02:00
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
2017-09-03 23:00:20 +01:00
DrawText(dpi, x, y, &_legacyPaint, format, args);
2017-07-22 22:08:25 +02:00
}
2018-06-22 22:59:03 +02:00
static void DrawTextEllipsisedCompat(
2018-07-21 13:51:54 +02:00
rct_drawpixelinfo* dpi, int32_t x, int32_t y, int32_t width, rct_string_id format, void* args, uint8_t colour,
TextAlignment alignment, bool underline = false)
2017-07-22 22:08:25 +02:00
{
2017-09-03 23:00:20 +01:00
_legacyPaint.UnderlineText = underline;
_legacyPaint.Colour = colour;
_legacyPaint.Alignment = alignment;
_legacyPaint.SpriteBase = FONT_SPRITE_BASE_MEDIUM;
2017-07-22 22:08:25 +02:00
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
utf8 buffer[256];
format_string(buffer, sizeof(buffer), format, args);
gfx_clip_string(buffer, width);
2017-09-03 23:00:20 +01:00
DrawText(dpi, x, y, &_legacyPaint, buffer);
2017-07-22 22:08:25 +02:00
}
2018-06-22 22:59:03 +02:00
void gfx_draw_string(rct_drawpixelinfo* dpi, const_utf8string buffer, uint8_t colour, int32_t x, int32_t y)
2017-07-22 22:08:25 +02:00
{
2018-02-01 18:49:14 +01:00
_legacyPaint.UnderlineText = false;
_legacyPaint.Colour = colour;
_legacyPaint.Alignment = TextAlignment::LEFT;
_legacyPaint.SpriteBase = gCurrentFontSpriteBase;
DrawText(dpi, x, y, &_legacyPaint, buffer);
2017-07-22 22:08:25 +02:00
}
2018-02-01 18:49:14 +01:00
// Basic
2018-06-22 22:59:03 +02:00
void gfx_draw_string_left(rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::LEFT);
}
2018-06-22 22:59:03 +02:00
void gfx_draw_string_centred(rct_drawpixelinfo* dpi, rct_string_id format, int32_t x, int32_t y, uint8_t colour, void* args)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::CENTRE);
}
2018-06-22 22:59:03 +02:00
void gfx_draw_string_right(rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::RIGHT);
}
// Underline
2018-06-22 22:59:03 +02:00
void draw_string_left_underline(rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::LEFT, true);
}
2018-06-22 22:59:03 +02:00
void draw_string_centred_underline(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::CENTRE, true);
}
2018-06-22 22:59:03 +02:00
void draw_string_right_underline(rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y)
2018-02-01 18:49:14 +01:00
{
DrawTextCompat(dpi, x, y, format, args, colour, TextAlignment::RIGHT, true);
}
// Ellipsised
2018-06-22 22:59:03 +02:00
void gfx_draw_string_left_clipped(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y, int32_t width)
2018-02-01 18:49:14 +01:00
{
DrawTextEllipsisedCompat(dpi, x, y, width, format, args, colour, TextAlignment::LEFT);
}
2018-06-22 22:59:03 +02:00
void gfx_draw_string_centred_clipped(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y, int32_t width)
2018-02-01 18:49:14 +01:00
{
DrawTextEllipsisedCompat(dpi, x, y, width, format, args, colour, TextAlignment::CENTRE);
}
2018-06-22 22:59:03 +02:00
void gfx_draw_string_right_clipped(
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, int32_t x, int32_t y, int32_t width)
2018-02-01 18:49:14 +01:00
{
DrawTextEllipsisedCompat(dpi, x, y, width, format, args, colour, TextAlignment::RIGHT);
}
// Wrapping
2018-06-22 22:59:03 +02:00
int32_t gfx_draw_string_left_wrapped(
rct_drawpixelinfo* dpi, void* args, int32_t x, int32_t y, int32_t width, rct_string_id format, uint8_t colour)
2018-02-01 18:49:14 +01:00
{
utf8 buffer[256];
format_string(buffer, sizeof(buffer), format, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
_legacyPaint.UnderlineText = false;
_legacyPaint.Colour = colour;
_legacyPaint.Alignment = TextAlignment::LEFT;
_legacyPaint.SpriteBase = gCurrentFontSpriteBase;
StaticLayout layout(buffer, _legacyPaint, width);
layout.Draw(dpi, x, y);
return layout.GetHeight();
}
2018-06-22 22:59:03 +02:00
int32_t gfx_draw_string_centred_wrapped(
rct_drawpixelinfo* dpi, void* args, int32_t x, int32_t y, int32_t width, rct_string_id format, uint8_t colour)
2018-02-01 18:49:14 +01:00
{
utf8 buffer[256];
format_string(buffer, sizeof(buffer), format, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
_legacyPaint.UnderlineText = false;
_legacyPaint.Colour = colour;
_legacyPaint.Alignment = TextAlignment::CENTRE;
_legacyPaint.SpriteBase = gCurrentFontSpriteBase;
StaticLayout layout(buffer, _legacyPaint, width);
// 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
layout.Draw(dpi, x - layout.GetWidth() / 2, y - yOffset);
return layout.GetHeight();
}