2014-10-06 17:36:58 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2014-10-06 17:36:58 +01: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
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-10-06 17:36:58 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2019-08-03 12:47:29 +01:00
|
|
|
#include "../Context.h"
|
2025-02-13 10:51:46 -05:00
|
|
|
#include "../SpriteIds.h"
|
2017-09-22 18:03:21 +02:00
|
|
|
#include "../config/Config.h"
|
2024-09-10 23:36:59 +02:00
|
|
|
#include "../core/CodepointView.hpp"
|
2020-10-14 19:40:24 +01:00
|
|
|
#include "../core/String.hpp"
|
2024-07-12 18:21:15 +02:00
|
|
|
#include "../core/UTF8.h"
|
2024-07-12 23:02:31 +02:00
|
|
|
#include "../core/UnicodeChar.h"
|
2019-08-03 12:47:29 +01:00
|
|
|
#include "../drawing/IDrawingContext.h"
|
|
|
|
|
#include "../drawing/IDrawingEngine.h"
|
2024-05-10 10:56:03 +01:00
|
|
|
#include "../drawing/Text.h"
|
2018-01-05 23:45:53 +00:00
|
|
|
#include "../interface/Viewport.h"
|
2020-10-13 01:14:39 +01:00
|
|
|
#include "../localisation/Formatting.h"
|
2018-04-26 23:03:02 +01:00
|
|
|
#include "../localisation/LocalisationService.h"
|
2022-02-18 21:57:00 +01:00
|
|
|
#include "../platform/Platform.h"
|
2018-01-05 21:14:20 +00:00
|
|
|
#include "TTF.h"
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2020-10-13 01:14:39 +01:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
static int32_t TTFGetStringWidth(std::string_view text, FontStyle fontStyle, bool noFormatting);
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2015-07-27 18:58:12 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* rct2: 0x006C23B1
|
|
|
|
|
*/
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t GfxGetStringWidthNewLined(std::string_view text, FontStyle fontStyle)
|
2015-07-27 18:58:12 +01:00
|
|
|
{
|
2023-02-13 22:02:40 +01:00
|
|
|
u8string buffer;
|
2014-11-01 13:10:14 +00:00
|
|
|
|
2020-10-15 22:12:41 +01:00
|
|
|
std::optional<int32_t> maxWidth;
|
|
|
|
|
FmtString fmt(text);
|
|
|
|
|
for (const auto& token : fmt)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2025-10-07 20:27:03 +02:00
|
|
|
if (token.kind == FormatToken::newline || token.kind == FormatToken::newlineSmall)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2023-01-16 18:52:17 +00:00
|
|
|
auto width = GfxGetStringWidth(buffer, fontStyle);
|
2021-09-13 19:47:13 +03:00
|
|
|
if (!maxWidth.has_value() || maxWidth.value() > width)
|
2020-10-15 22:12:41 +01:00
|
|
|
{
|
|
|
|
|
maxWidth = width;
|
|
|
|
|
}
|
|
|
|
|
buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
buffer.append(token.text);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-13 19:47:13 +03:00
|
|
|
if (!maxWidth.has_value())
|
2020-10-15 22:12:41 +01:00
|
|
|
{
|
2023-01-16 18:52:17 +00:00
|
|
|
maxWidth = GfxGetStringWidth(buffer, fontStyle);
|
2020-10-15 22:12:41 +01:00
|
|
|
}
|
2021-09-13 19:47:13 +03:00
|
|
|
return maxWidth.value();
|
2014-11-01 13:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-06 17:36:58 +01:00
|
|
|
/**
|
2015-12-11 09:38:37 -06:00
|
|
|
* Return the width of the string in buffer
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
|
|
|
|
* rct2: 0x006C2321
|
|
|
|
|
* buffer (esi)
|
|
|
|
|
*/
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t GfxGetStringWidth(std::string_view text, FontStyle fontStyle)
|
2014-10-06 17:36:58 +01:00
|
|
|
{
|
2023-01-16 22:31:34 +00:00
|
|
|
return TTFGetStringWidth(text, fontStyle, false);
|
2020-11-22 00:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t GfxGetStringWidthNoFormatting(std::string_view text, FontStyle fontStyle)
|
2020-11-22 00:36:40 +00:00
|
|
|
{
|
2023-01-16 22:31:34 +00:00
|
|
|
return TTFGetStringWidth(text, fontStyle, true);
|
2014-10-06 17:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-11 09:38:37 -06:00
|
|
|
* Clip the text in buffer to width, add ellipsis and return the new width of the clipped string
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
|
|
|
|
* rct2: 0x006C2460
|
|
|
|
|
* buffer (esi)
|
|
|
|
|
* width (edi)
|
|
|
|
|
*/
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t GfxClipString(utf8* text, int32_t width, FontStyle fontStyle)
|
2014-10-06 17:36:58 +01:00
|
|
|
{
|
2018-06-22 22:59:03 +02:00
|
|
|
if (width < 6)
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
*text = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-07-26 17:50:01 +01:00
|
|
|
|
2020-10-15 22:12:41 +01:00
|
|
|
// If width of the full string is less than allowed width then we don't need to clip
|
2023-01-16 18:52:17 +00:00
|
|
|
auto clippedWidth = GfxGetStringWidth(text, fontStyle);
|
2018-06-22 22:59:03 +02:00
|
|
|
if (clippedWidth <= width)
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return clippedWidth;
|
|
|
|
|
}
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2020-10-15 22:12:41 +01:00
|
|
|
// Append each character 1 by 1 with an ellipsis on the end until width is exceeded
|
|
|
|
|
thread_local std::string buffer;
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
|
|
size_t bestLength = 0;
|
|
|
|
|
int32_t bestWidth = 0;
|
|
|
|
|
|
|
|
|
|
FmtString fmt(text);
|
|
|
|
|
for (const auto& token : fmt)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-15 22:12:41 +01:00
|
|
|
CodepointView codepoints(token.text);
|
|
|
|
|
for (auto codepoint : codepoints)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-15 22:12:41 +01:00
|
|
|
// Add the ellipsis before checking the width
|
|
|
|
|
buffer.append("...");
|
2015-07-30 01:09:23 +01:00
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
auto currentWidth = GfxGetStringWidth(buffer, fontStyle);
|
2020-10-15 22:12:41 +01:00
|
|
|
if (currentWidth < width)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-15 22:12:41 +01:00
|
|
|
bestLength = buffer.size();
|
|
|
|
|
bestWidth = currentWidth;
|
2015-07-27 17:21:21 +01:00
|
|
|
|
2020-10-15 22:12:41 +01:00
|
|
|
// Trim the ellipsis
|
|
|
|
|
buffer.resize(bestLength - 3);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Width exceeded, rollback to best length and put ellipsis back
|
|
|
|
|
buffer.resize(bestLength);
|
2020-10-19 20:37:07 +01:00
|
|
|
for (auto i = static_cast<int32_t>(bestLength) - 1; i >= 0 && i >= static_cast<int32_t>(bestLength) - 3; i--)
|
2020-10-15 22:12:41 +01:00
|
|
|
{
|
2020-10-22 21:01:58 +01:00
|
|
|
buffer[i] = '.';
|
2020-10-15 22:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy buffer back to input text buffer
|
|
|
|
|
std::strcpy(text, buffer.c_str());
|
|
|
|
|
return bestWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char cb[8]{};
|
2023-01-17 00:12:35 +01:00
|
|
|
UTF8WriteCodepoint(cb, codepoint);
|
2020-10-15 22:12:41 +01:00
|
|
|
buffer.append(cb);
|
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2023-01-16 18:52:17 +00:00
|
|
|
return GfxGetStringWidth(text, fontStyle);
|
2015-07-27 17:21:21 +01:00
|
|
|
}
|
2014-10-06 17:36:58 +01:00
|
|
|
|
|
|
|
|
/**
|
2015-12-11 09:38:37 -06:00
|
|
|
* Wrap the text in buffer to width, returns width of longest line.
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
2015-12-11 09:38:37 -06:00
|
|
|
* Inserts NULL where line should break (as \n is used for something else),
|
|
|
|
|
* so the number of lines is returned in num_lines. font_height seems to be
|
|
|
|
|
* a control character for line height.
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
|
|
|
|
* rct2: 0x006C21E2
|
|
|
|
|
* buffer (esi)
|
|
|
|
|
* width (edi) - in
|
|
|
|
|
* num_lines (edi) - out
|
|
|
|
|
* font_height (ebx) - out
|
|
|
|
|
*/
|
2023-02-13 22:02:40 +01:00
|
|
|
int32_t GfxWrapString(u8string_view text, int32_t width, FontStyle fontStyle, u8string* outWrappedText, int32_t* outNumLines)
|
2014-10-06 17:36:58 +01:00
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
constexpr size_t kNullIndex = std::numeric_limits<size_t>::max();
|
2023-02-13 22:02:40 +01:00
|
|
|
u8string buffer;
|
2020-10-13 01:14:39 +01:00
|
|
|
|
|
|
|
|
size_t currentLineIndex = 0;
|
2025-01-19 16:59:45 +01:00
|
|
|
size_t splitIndex = kNullIndex;
|
|
|
|
|
size_t bestSplitIndex = kNullIndex;
|
2020-10-13 01:14:39 +01:00
|
|
|
size_t numLines = 0;
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t maxWidth = 0;
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2023-02-13 22:02:40 +01:00
|
|
|
FmtString fmt(text);
|
2020-10-13 01:14:39 +01:00
|
|
|
for (const auto& token : fmt)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
if (token.IsLiteral())
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
CodepointView codepoints(token.text);
|
|
|
|
|
for (auto codepoint : codepoints)
|
|
|
|
|
{
|
2020-10-15 22:12:41 +01:00
|
|
|
char cb[8]{};
|
2023-01-17 00:12:35 +01:00
|
|
|
UTF8WriteCodepoint(cb, codepoint);
|
2020-10-15 22:12:41 +01:00
|
|
|
buffer.append(cb);
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
auto lineWidth = GfxGetStringWidth(&buffer[currentLineIndex], fontStyle);
|
2025-01-19 16:59:45 +01:00
|
|
|
if (lineWidth <= width || (splitIndex == kNullIndex && bestSplitIndex == kNullIndex))
|
2020-10-13 01:14:39 +01:00
|
|
|
{
|
|
|
|
|
if (codepoint == ' ')
|
|
|
|
|
{
|
|
|
|
|
// Mark line split here
|
|
|
|
|
splitIndex = buffer.size() - 1;
|
|
|
|
|
}
|
2025-01-19 16:59:45 +01:00
|
|
|
else if (splitIndex == kNullIndex)
|
2020-10-13 01:14:39 +01:00
|
|
|
{
|
|
|
|
|
// Mark line split here (this is after first character of line)
|
2020-10-22 21:01:58 +01:00
|
|
|
bestSplitIndex = buffer.size();
|
2020-10-13 01:14:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Insert new line before current word
|
2025-01-19 16:59:45 +01:00
|
|
|
if (splitIndex == kNullIndex)
|
2020-10-22 21:01:58 +01:00
|
|
|
{
|
|
|
|
|
splitIndex = bestSplitIndex;
|
|
|
|
|
}
|
2020-10-13 01:14:39 +01:00
|
|
|
buffer.insert(buffer.begin() + splitIndex, '\0');
|
2015-07-27 17:21:21 +01:00
|
|
|
|
2020-10-13 01:14:39 +01:00
|
|
|
// Recalculate the line length after splitting
|
2023-01-16 18:52:17 +00:00
|
|
|
lineWidth = GfxGetStringWidth(&buffer[currentLineIndex], fontStyle);
|
2020-10-13 01:14:39 +01:00
|
|
|
maxWidth = std::max(maxWidth, lineWidth);
|
|
|
|
|
numLines++;
|
|
|
|
|
|
|
|
|
|
currentLineIndex = splitIndex + 1;
|
2025-01-19 16:59:45 +01:00
|
|
|
splitIndex = kNullIndex;
|
|
|
|
|
bestSplitIndex = kNullIndex;
|
2020-10-13 01:14:39 +01:00
|
|
|
|
|
|
|
|
// Trim the beginning of the new line
|
|
|
|
|
while (buffer[currentLineIndex] == ' ')
|
|
|
|
|
{
|
|
|
|
|
buffer.erase(buffer.begin() + currentLineIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
2025-10-07 20:27:03 +02:00
|
|
|
else if (token.kind == FormatToken::newline)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
buffer.push_back('\0');
|
|
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
auto lineWidth = GfxGetStringWidth(&buffer[currentLineIndex], fontStyle);
|
2018-01-05 18:21:57 +00:00
|
|
|
maxWidth = std::max(maxWidth, lineWidth);
|
2020-10-13 01:14:39 +01:00
|
|
|
numLines++;
|
2020-10-22 21:01:58 +01:00
|
|
|
|
|
|
|
|
currentLineIndex = buffer.size();
|
2025-01-19 16:59:45 +01:00
|
|
|
splitIndex = kNullIndex;
|
|
|
|
|
bestSplitIndex = kNullIndex;
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
buffer.append(token.text);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-13 01:14:39 +01:00
|
|
|
{
|
|
|
|
|
// Final line width calculation
|
2023-01-16 18:52:17 +00:00
|
|
|
auto lineWidth = GfxGetStringWidth(&buffer[currentLineIndex], fontStyle);
|
2020-10-13 01:14:39 +01:00
|
|
|
maxWidth = std::max(maxWidth, lineWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 22:02:40 +01:00
|
|
|
if (outWrappedText != nullptr)
|
|
|
|
|
{
|
|
|
|
|
*outWrappedText = std::move(buffer);
|
|
|
|
|
}
|
|
|
|
|
if (outNumLines != nullptr)
|
|
|
|
|
{
|
|
|
|
|
*outNumLines = static_cast<int32_t>(numLines);
|
|
|
|
|
}
|
2020-10-13 01:14:39 +01:00
|
|
|
return maxWidth;
|
2014-10-06 17:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-17 16:21:50 +01:00
|
|
|
/**
|
|
|
|
|
* Draws text that is left aligned and vertically centred.
|
|
|
|
|
*/
|
2024-05-09 02:45:07 +02:00
|
|
|
void GfxDrawStringLeftCentred(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, StringId format, void* args, ColourWithFlags colour, const ScreenCoordsXY& coords)
|
2015-06-17 16:21:50 +01:00
|
|
|
{
|
2024-07-13 13:25:55 +02:00
|
|
|
char buffer[512];
|
2022-11-15 00:48:34 +01:00
|
|
|
auto bufferPtr = buffer;
|
2023-01-17 21:56:30 +01:00
|
|
|
FormatStringLegacy(bufferPtr, sizeof(buffer), format, args);
|
2025-10-16 20:20:46 +02:00
|
|
|
int32_t height = StringGetHeightRaw(bufferPtr, FontStyle::medium);
|
2025-05-05 01:06:46 +03:00
|
|
|
DrawText(rt, coords - ScreenCoordsXY{ 0, (height / 2) }, { colour }, bufferPtr);
|
2015-06-17 16:21:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-06 17:36:58 +01:00
|
|
|
/**
|
|
|
|
|
* Changes the palette so that the next character changes colour
|
|
|
|
|
*/
|
2025-05-11 22:37:21 +02:00
|
|
|
static void ColourCharacter(TextColour colour, const uint16_t* current_font_flags, uint8_t* palette_pointer)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t colour32 = 0;
|
2023-01-19 08:16:44 +00:00
|
|
|
const G1Element* g1 = GfxGetG1Element(SPR_TEXT_PALETTE);
|
2018-01-07 21:43:07 +01:00
|
|
|
if (g1 != nullptr)
|
2017-10-26 13:14:37 +01:00
|
|
|
{
|
2025-05-11 22:37:21 +02:00
|
|
|
uint32_t idx = EnumValue(colour) * 4;
|
2019-02-04 23:15:14 +01:00
|
|
|
std::memcpy(&colour32, &g1->offset[idx], sizeof(colour32));
|
2017-10-26 13:14:37 +01:00
|
|
|
}
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2018-09-16 15:07:32 +02:00
|
|
|
if (!(*current_font_flags & TEXT_DRAW_FLAG_OUTLINE))
|
2017-10-26 13:14:37 +01:00
|
|
|
{
|
|
|
|
|
colour32 = colour32 & 0x0FF0000FF;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
// Adjust text palette. Store current colour?
|
2017-10-26 13:14:37 +01:00
|
|
|
palette_pointer[1] = colour32 & 0xFF;
|
|
|
|
|
palette_pointer[2] = (colour32 >> 8) & 0xFF;
|
|
|
|
|
palette_pointer[3] = (colour32 >> 16) & 0xFF;
|
|
|
|
|
palette_pointer[4] = (colour32 >> 24) & 0xFF;
|
2014-10-06 17:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the palette so that the next character changes colour
|
2015-12-11 09:38:37 -06:00
|
|
|
* This is specific to changing to a predefined window related colour
|
2014-10-06 17:36:58 +01:00
|
|
|
*/
|
2024-05-09 02:45:07 +02:00
|
|
|
static void ColourCharacterWindow(colour_t colour, const uint16_t* current_font_flags, uint8_t* palette_pointer)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t eax;
|
2014-10-06 17:36:58 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
eax = ColourMapA[colour].colour_11;
|
2018-06-22 22:59:03 +02:00
|
|
|
if (*current_font_flags & TEXT_DRAW_FLAG_OUTLINE)
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
eax |= 0x0A0A00;
|
|
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
// Adjust text palette. Store current colour?
|
2017-06-06 23:24:18 +02:00
|
|
|
palette_pointer[1] = eax & 0xFF;
|
|
|
|
|
palette_pointer[2] = (eax >> 8) & 0xFF;
|
|
|
|
|
palette_pointer[3] = (eax >> 16) & 0xFF;
|
|
|
|
|
palette_pointer[4] = (eax >> 24) & 0xFF;
|
2014-10-06 17:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2015-12-11 09:38:37 -06:00
|
|
|
* rct2: 0x006C1DB7
|
2014-10-06 17:36:58 +01:00
|
|
|
*
|
|
|
|
|
* left : cx
|
|
|
|
|
* top : dx
|
|
|
|
|
* numLines : bp
|
|
|
|
|
* text : esi
|
|
|
|
|
* dpi : edi
|
|
|
|
|
*/
|
2023-02-13 22:02:40 +01:00
|
|
|
void DrawStringCentredRaw(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, const ScreenCoordsXY& coords, int32_t numLines, const utf8* text, FontStyle fontStyle)
|
2014-10-06 17:36:58 +01:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
ScreenCoordsXY screenCoords(rt.x, rt.y);
|
|
|
|
|
DrawText(rt, screenCoords, { COLOUR_BLACK, fontStyle }, "");
|
2020-06-21 08:42:38 -03:00
|
|
|
screenCoords = coords;
|
2015-06-09 15:42:25 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
for (int32_t i = 0; i <= numLines; i++)
|
|
|
|
|
{
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t width = GfxGetStringWidth(text, fontStyle);
|
2025-05-05 01:06:46 +03:00
|
|
|
DrawText(rt, screenCoords - ScreenCoordsXY{ width / 2, 0 }, { kTextColour254, fontStyle }, text);
|
2015-06-09 15:42:25 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
const utf8* ch = text;
|
|
|
|
|
const utf8* nextCh = nullptr;
|
2016-03-01 20:23:48 +09:00
|
|
|
|
2023-01-17 00:12:35 +01:00
|
|
|
while ((UTF8GetNext(ch, &nextCh)) != 0)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
ch = nextCh;
|
|
|
|
|
}
|
2020-04-22 17:09:29 +02:00
|
|
|
text = const_cast<char*>(ch + 1);
|
2015-06-09 15:42:25 +01:00
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
screenCoords.y += FontGetLineHeight(fontStyle);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-06-16 22:56:08 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:52:17 +00:00
|
|
|
int32_t StringGetHeightRaw(std::string_view text, FontStyle fontStyle)
|
2015-06-16 22:56:08 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t height = 0;
|
2025-10-16 20:20:46 +02:00
|
|
|
if (fontStyle <= FontStyle::medium)
|
2017-06-06 23:24:18 +02:00
|
|
|
height += 10;
|
2025-10-16 20:20:46 +02:00
|
|
|
else if (fontStyle == FontStyle::tiny)
|
2017-06-06 23:24:18 +02:00
|
|
|
height += 6;
|
2015-06-16 22:56:08 +01:00
|
|
|
|
2021-02-02 23:48:46 +00:00
|
|
|
FmtString fmt(text);
|
2020-10-16 00:13:52 +01:00
|
|
|
for (const auto& token : fmt)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-16 00:13:52 +01:00
|
|
|
switch (token.kind)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::newline:
|
2025-10-16 20:20:46 +02:00
|
|
|
if (fontStyle == FontStyle::small || fontStyle == FontStyle::medium)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
height += 10;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2025-10-16 20:20:46 +02:00
|
|
|
if (fontStyle == FontStyle::tiny)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
height += 6;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
height += 18;
|
2017-06-06 23:24:18 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::newlineSmall:
|
2025-10-16 20:20:46 +02:00
|
|
|
if (fontStyle == FontStyle::small || fontStyle == FontStyle::medium)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
height += 5;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2025-10-16 20:20:46 +02:00
|
|
|
if (fontStyle == FontStyle::tiny)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
height += 3;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
height += 9;
|
2017-06-06 23:24:18 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontTiny:
|
2025-10-16 20:20:46 +02:00
|
|
|
fontStyle = FontStyle::tiny;
|
2017-06-06 23:24:18 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontMedium:
|
2025-10-16 20:20:46 +02:00
|
|
|
fontStyle = FontStyle::medium;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontSmall:
|
2025-10-16 20:20:46 +02:00
|
|
|
fontStyle = FontStyle::small;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2020-10-19 20:37:07 +01:00
|
|
|
default:
|
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return height;
|
2015-06-28 01:57:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2015-12-11 09:38:37 -06:00
|
|
|
* rct2: 0x006C1F57
|
2015-06-28 01:57:50 +01:00
|
|
|
*
|
|
|
|
|
* colour : al
|
|
|
|
|
* format : bx
|
|
|
|
|
* x : cx
|
|
|
|
|
* y : dx
|
|
|
|
|
* text : esi
|
|
|
|
|
* dpi : edi
|
|
|
|
|
* width : bp
|
|
|
|
|
* ticks : ebp >> 16
|
|
|
|
|
*/
|
2021-03-02 22:48:33 +01:00
|
|
|
void DrawNewsTicker(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, const ScreenCoordsXY& coords, int32_t width, colour_t colour, StringId format, u8string_view args,
|
2018-06-22 22:59:03 +02:00
|
|
|
int32_t ticks)
|
2015-06-28 01:57:50 +01:00
|
|
|
{
|
2021-02-27 12:40:20 +01:00
|
|
|
int32_t numLines, lineHeight, lineY;
|
2025-05-05 01:06:46 +03:00
|
|
|
ScreenCoordsXY screenCoords(rt.x, rt.y);
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
DrawText(rt, screenCoords, { colour }, "");
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2023-02-13 22:02:40 +01:00
|
|
|
u8string wrappedString;
|
2025-10-16 20:20:46 +02:00
|
|
|
GfxWrapString(FormatStringID(format, args), width, FontStyle::small, &wrappedString, &numLines);
|
|
|
|
|
lineHeight = FontGetLineHeight(FontStyle::small);
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t numCharactersDrawn = 0;
|
|
|
|
|
int32_t numCharactersToDraw = ticks;
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2023-02-13 22:02:40 +01:00
|
|
|
const utf8* buffer = wrappedString.data();
|
2020-06-21 08:46:51 -03:00
|
|
|
lineY = coords.y - ((numLines * lineHeight) / 2);
|
2018-06-22 22:59:03 +02:00
|
|
|
for (int32_t line = 0; line <= numLines; line++)
|
|
|
|
|
{
|
2025-10-16 20:20:46 +02:00
|
|
|
int32_t halfWidth = GfxGetStringWidth(buffer, FontStyle::small) / 2;
|
2015-08-04 22:39:44 +02:00
|
|
|
|
2020-10-16 00:13:52 +01:00
|
|
|
FmtString fmt(buffer);
|
|
|
|
|
for (const auto& token : fmt)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-16 00:13:52 +01:00
|
|
|
bool doubleBreak = false;
|
|
|
|
|
if (token.IsLiteral())
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-16 00:13:52 +01:00
|
|
|
CodepointView codepoints(token.text);
|
|
|
|
|
for (auto it = codepoints.begin(); it != codepoints.end(); it++)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2020-10-16 00:13:52 +01:00
|
|
|
numCharactersDrawn++;
|
|
|
|
|
if (numCharactersDrawn > numCharactersToDraw)
|
|
|
|
|
{
|
|
|
|
|
auto ch = const_cast<char*>(&token.text[it.GetIndex()]);
|
|
|
|
|
*ch = '\0';
|
|
|
|
|
doubleBreak = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-16 00:13:52 +01:00
|
|
|
if (doubleBreak)
|
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2020-06-21 08:46:51 -03:00
|
|
|
screenCoords = { coords.x - halfWidth, lineY };
|
2025-10-16 20:20:46 +02:00
|
|
|
DrawText(rt, screenCoords, { kTextColour254, FontStyle::small }, buffer);
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
if (numCharactersDrawn > numCharactersToDraw)
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2015-07-29 16:34:13 +01:00
|
|
|
|
2023-01-17 00:12:35 +01:00
|
|
|
buffer = GetStringEnd(buffer) + 1;
|
2017-06-06 23:24:18 +02:00
|
|
|
lineY += lineHeight;
|
|
|
|
|
}
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFDrawCharacterSprite(RenderTarget& rt, int32_t codepoint, TextDrawInfo* info)
|
2015-07-27 01:09:24 +01:00
|
|
|
{
|
2025-10-16 19:56:59 +02:00
|
|
|
int32_t characterWidth = FontSpriteGetCodepointWidth(info->fontStyle, codepoint);
|
|
|
|
|
auto sprite = FontSpriteGetCodepointSprite(info->fontStyle, codepoint);
|
2015-07-27 01:09:24 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
if (!(info->flags & TEXT_DRAW_FLAG_NO_DRAW))
|
|
|
|
|
{
|
2020-06-21 09:05:20 -03:00
|
|
|
auto screenCoords = ScreenCoordsXY{ info->x, info->y };
|
2018-06-22 22:59:03 +02:00
|
|
|
if (info->flags & TEXT_DRAW_FLAG_Y_OFFSET_EFFECT)
|
|
|
|
|
{
|
2025-10-16 19:56:59 +02:00
|
|
|
screenCoords.y += *info->yOffset++;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2020-05-28 19:52:20 +01:00
|
|
|
|
|
|
|
|
PaletteMap paletteMap(info->palette);
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawGlyph(rt, sprite, screenCoords, paletteMap);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-27 01:09:24 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
info->x += characterWidth;
|
2015-07-27 01:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFDrawStringRawSprite(RenderTarget& rt, std::string_view text, TextDrawInfo* info)
|
2015-07-26 13:58:53 +01:00
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
CodepointView codepoints(text);
|
|
|
|
|
for (auto codepoint : codepoints)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawCharacterSprite(rt, codepoint, info);
|
2020-10-13 01:14:39 +01:00
|
|
|
}
|
2015-07-26 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:09:52 +01:00
|
|
|
#ifndef DISABLE_TTF
|
2017-06-25 00:18:08 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFDrawStringRawTTF(RenderTarget& rt, std::string_view text, TextDrawInfo* info)
|
2015-07-26 00:55:17 +01:00
|
|
|
{
|
2023-01-16 22:31:34 +00:00
|
|
|
if (!TTFInitialise())
|
2017-06-06 23:24:18 +02:00
|
|
|
return;
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2025-10-16 19:56:59 +02:00
|
|
|
TTFFontDescriptor* fontDesc = TTFGetFontFromSpriteBase(info->fontStyle);
|
2018-06-22 22:59:03 +02:00
|
|
|
if (fontDesc->font == nullptr)
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawStringRawSprite(rt, text, info);
|
2017-06-06 23:24:18 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-31 19:05:00 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
if (info->flags & TEXT_DRAW_FLAG_NO_DRAW)
|
|
|
|
|
{
|
2023-01-16 22:31:34 +00:00
|
|
|
info->x += TTFGetWidthCacheGetOrAdd(fontDesc->font, text);
|
2017-06-06 23:24:18 +02:00
|
|
|
return;
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
TTFSurface* surface = TTFSurfaceCacheGetOrAdd(fontDesc->font, text);
|
2021-09-15 22:22:15 +02:00
|
|
|
if (surface == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
auto drawingEngine = rt.DrawingEngine;
|
2024-03-28 23:28:54 +01:00
|
|
|
if (drawingEngine != nullptr)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2024-03-28 23:28:54 +01:00
|
|
|
int32_t drawX = info->x + fontDesc->offset_x;
|
|
|
|
|
int32_t drawY = info->y + fontDesc->offset_y;
|
2025-10-07 20:14:37 +02:00
|
|
|
uint8_t hintThresh = Config::Get().fonts.enableHinting ? fontDesc->hinting_threshold : 0;
|
2024-03-28 23:28:54 +01:00
|
|
|
OpenRCT2::Drawing::IDrawingContext* dc = drawingEngine->GetDrawingContext();
|
2025-05-05 01:06:46 +03:00
|
|
|
dc->DrawTTFBitmap(rt, info, surface, drawX, drawY, hintThresh);
|
2021-09-15 22:22:15 +02:00
|
|
|
}
|
2024-03-28 23:28:54 +01:00
|
|
|
info->x += surface->w;
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
2017-06-13 20:50:14 +01:00
|
|
|
|
2025-04-11 16:09:52 +01:00
|
|
|
#endif // DISABLE_TTF
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFProcessFormatCode(RenderTarget& rt, const FmtString::Token& token, TextDrawInfo* info)
|
2015-07-26 00:55:17 +01:00
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
switch (token.kind)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::move:
|
2020-10-14 23:46:09 +01:00
|
|
|
info->x = info->startX + token.parameter;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::newline:
|
2018-06-22 22:59:03 +02:00
|
|
|
info->x = info->startX;
|
2025-10-16 19:56:59 +02:00
|
|
|
info->y += FontGetLineHeight(info->fontStyle);
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::newlineSmall:
|
2018-06-22 22:59:03 +02:00
|
|
|
info->x = info->startX;
|
2025-10-16 19:56:59 +02:00
|
|
|
info->y += FontGetLineHeightSmall(info->fontStyle);
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontTiny:
|
2025-10-16 20:20:46 +02:00
|
|
|
info->fontStyle = FontStyle::tiny;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontSmall:
|
2025-10-16 20:20:46 +02:00
|
|
|
info->fontStyle = FontStyle::small;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::fontMedium:
|
2025-10-16 20:20:46 +02:00
|
|
|
info->fontStyle = FontStyle::medium;
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::outlineEnable:
|
2018-06-22 22:59:03 +02:00
|
|
|
info->flags |= TEXT_DRAW_FLAG_OUTLINE;
|
|
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::outlineDisable:
|
2018-06-22 22:59:03 +02:00
|
|
|
info->flags &= ~TEXT_DRAW_FLAG_OUTLINE;
|
|
|
|
|
break;
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::colourWindow1:
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t flags = info->flags;
|
2023-01-16 22:31:34 +00:00
|
|
|
ColourCharacterWindow(gCurrentWindowColours[0], &flags, info->palette);
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::colourWindow2:
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
uint16_t flags = info->flags;
|
2023-01-16 22:31:34 +00:00
|
|
|
ColourCharacterWindow(gCurrentWindowColours[1], &flags, info->palette);
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::colourWindow3:
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
uint16_t flags = info->flags;
|
2023-01-16 22:31:34 +00:00
|
|
|
ColourCharacterWindow(gCurrentWindowColours[2], &flags, info->palette);
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2025-10-07 20:27:03 +02:00
|
|
|
case FormatToken::inlineSprite:
|
2020-10-14 23:46:09 +01:00
|
|
|
{
|
2024-12-17 21:26:11 +01:00
|
|
|
auto imageId = ImageId(token.parameter);
|
|
|
|
|
auto g1 = GfxGetG1Element(imageId);
|
2022-04-23 14:36:49 +01:00
|
|
|
if (g1 != nullptr && g1->width <= 32 && g1->height <= 32)
|
2020-10-14 23:46:09 +01:00
|
|
|
{
|
|
|
|
|
if (!(info->flags & TEXT_DRAW_FLAG_NO_DRAW))
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
GfxDrawSprite(rt, imageId, { info->x, info->y });
|
2020-10-14 23:46:09 +01:00
|
|
|
}
|
|
|
|
|
info->x += g1->width;
|
|
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
break;
|
2020-10-14 23:46:09 +01:00
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
default:
|
2020-10-16 00:13:52 +01:00
|
|
|
if (FormatTokenIsColour(token.kind))
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
uint16_t flags = info->flags;
|
2025-05-11 22:37:21 +02:00
|
|
|
auto colourIndex = FormatTokenToTextColour(token.kind);
|
|
|
|
|
ColourCharacter(colourIndex, &flags, info->palette);
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:09:52 +01:00
|
|
|
#ifndef DISABLE_TTF
|
2020-10-19 20:37:07 +01:00
|
|
|
static bool ShouldUseSpriteForCodepoint(char32_t codepoint)
|
|
|
|
|
{
|
|
|
|
|
switch (codepoint)
|
|
|
|
|
{
|
|
|
|
|
case UnicodeChar::up:
|
|
|
|
|
case UnicodeChar::down:
|
|
|
|
|
case UnicodeChar::leftguillemet:
|
|
|
|
|
case UnicodeChar::tick:
|
|
|
|
|
case UnicodeChar::cross:
|
|
|
|
|
case UnicodeChar::right:
|
|
|
|
|
case UnicodeChar::rightguillemet:
|
|
|
|
|
case UnicodeChar::small_up:
|
|
|
|
|
case UnicodeChar::small_down:
|
|
|
|
|
case UnicodeChar::left:
|
|
|
|
|
case UnicodeChar::quote_open:
|
|
|
|
|
case UnicodeChar::quote_close:
|
|
|
|
|
case UnicodeChar::german_quote_open:
|
|
|
|
|
case UnicodeChar::plus:
|
|
|
|
|
case UnicodeChar::minus:
|
2022-04-22 19:09:35 +02:00
|
|
|
case UnicodeChar::variation_selector:
|
|
|
|
|
case UnicodeChar::eye:
|
|
|
|
|
case UnicodeChar::road:
|
|
|
|
|
case UnicodeChar::railway:
|
2020-10-19 20:37:07 +01:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-11 16:09:52 +01:00
|
|
|
#endif // DISABLE_TTF
|
2020-10-19 20:37:07 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFProcessStringLiteral(RenderTarget& rt, std::string_view text, TextDrawInfo* info)
|
2015-07-26 00:55:17 +01:00
|
|
|
{
|
2025-04-11 16:09:52 +01:00
|
|
|
#ifndef DISABLE_TTF
|
2017-06-06 23:24:18 +02:00
|
|
|
bool isTTF = info->flags & TEXT_DRAW_FLAG_TTF;
|
2017-01-04 13:51:12 +01:00
|
|
|
#else
|
2017-06-06 23:24:18 +02:00
|
|
|
bool isTTF = false;
|
2025-04-11 16:09:52 +01:00
|
|
|
#endif // DISABLE_TTF
|
2020-10-13 01:14:39 +01:00
|
|
|
|
|
|
|
|
if (!isTTF)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawStringRawSprite(rt, text, info);
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
2025-04-11 16:09:52 +01:00
|
|
|
#ifndef DISABLE_TTF
|
2018-06-22 22:59:03 +02:00
|
|
|
else
|
|
|
|
|
{
|
2020-10-13 01:14:39 +01:00
|
|
|
CodepointView codepoints(text);
|
2022-10-10 20:42:40 +02:00
|
|
|
std::optional<size_t> ttfRunIndex{};
|
2020-10-13 01:14:39 +01:00
|
|
|
for (auto it = codepoints.begin(); it != codepoints.end(); it++)
|
|
|
|
|
{
|
|
|
|
|
auto codepoint = *it;
|
2020-10-19 20:37:07 +01:00
|
|
|
if (ShouldUseSpriteForCodepoint(codepoint))
|
2020-10-13 01:14:39 +01:00
|
|
|
{
|
2021-09-13 19:47:13 +03:00
|
|
|
if (ttfRunIndex.has_value())
|
2020-10-13 01:14:39 +01:00
|
|
|
{
|
|
|
|
|
// Draw the TTF run
|
2022-10-10 20:42:40 +02:00
|
|
|
// This error suppression abomination is here to suppress https://github.com/OpenRCT2/OpenRCT2/issues/17371.
|
|
|
|
|
// Additionally, we have to suppress the error for the error suppression... :'-(
|
2023-04-03 16:09:41 +02:00
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105937 is fixed in GCC13
|
2024-11-29 12:04:56 +09:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
|
#endif
|
2021-09-13 19:47:13 +03:00
|
|
|
auto len = it.GetIndex() - ttfRunIndex.value();
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawStringRawTTF(rt, text.substr(ttfRunIndex.value(), len), info);
|
2024-11-29 12:04:56 +09:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
#endif
|
2020-10-22 21:01:58 +01:00
|
|
|
ttfRunIndex = std::nullopt;
|
2020-10-13 01:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw the sprite font glyph
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawCharacterSprite(rt, codepoint, info);
|
2020-10-13 01:14:39 +01:00
|
|
|
}
|
2020-10-22 21:01:58 +01:00
|
|
|
else
|
|
|
|
|
{
|
2021-09-13 19:47:13 +03:00
|
|
|
if (!ttfRunIndex.has_value())
|
2020-10-22 21:01:58 +01:00
|
|
|
{
|
|
|
|
|
ttfRunIndex = it.GetIndex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 19:47:13 +03:00
|
|
|
if (ttfRunIndex.has_value())
|
2020-10-22 21:01:58 +01:00
|
|
|
{
|
|
|
|
|
// Final TTF run
|
|
|
|
|
auto len = text.size() - *ttfRunIndex;
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFDrawStringRawTTF(rt, text.substr(ttfRunIndex.value(), len), info);
|
2020-10-13 01:14:39 +01:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2025-04-11 16:09:52 +01:00
|
|
|
#endif // DISABLE_TTF
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFProcessStringCodepoint(RenderTarget& rt, codepoint_t codepoint, TextDrawInfo* info)
|
2020-11-21 18:16:56 +00:00
|
|
|
{
|
|
|
|
|
char buffer[8]{};
|
2023-01-17 00:12:35 +01:00
|
|
|
UTF8WriteCodepoint(buffer, codepoint);
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessStringLiteral(rt, buffer, info);
|
2020-11-21 18:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
static void TTFProcessString(RenderTarget& rt, std::string_view text, TextDrawInfo* info)
|
2015-07-26 00:55:17 +01:00
|
|
|
{
|
2020-11-22 00:36:40 +00:00
|
|
|
if (info->flags & TEXT_DRAW_FLAG_NO_FORMATTING)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessStringLiteral(rt, text, info);
|
2018-01-05 18:21:57 +00:00
|
|
|
info->maxX = std::max(info->maxX, info->x);
|
|
|
|
|
info->maxY = std::max(info->maxY, info->y);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2020-11-22 00:36:40 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FmtString fmt(text);
|
|
|
|
|
for (const auto& token : fmt)
|
|
|
|
|
{
|
|
|
|
|
if (token.IsLiteral())
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessStringLiteral(rt, token.text, info);
|
2020-11-22 00:36:40 +00:00
|
|
|
}
|
|
|
|
|
else if (token.IsCodepoint())
|
|
|
|
|
{
|
|
|
|
|
auto codepoint = token.GetCodepoint();
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessStringCodepoint(rt, codepoint, info);
|
2020-11-22 00:36:40 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessFormatCode(rt, token, info);
|
2020-11-22 00:36:40 +00:00
|
|
|
}
|
|
|
|
|
info->maxX = std::max(info->maxX, info->x);
|
|
|
|
|
info->maxY = std::max(info->maxY, info->y);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-26 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-09 02:45:07 +02:00
|
|
|
static void TTFProcessInitialColour(ColourWithFlags colour, TextDrawInfo* info)
|
2015-07-26 13:58:53 +01:00
|
|
|
{
|
2024-11-02 09:54:54 -03:00
|
|
|
if (colour.colour != kTextColour254 && colour.colour != kTextColour255)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2022-10-12 18:27:05 +02:00
|
|
|
info->flags &= ~(TEXT_DRAW_FLAG_INSET | TEXT_DRAW_FLAG_OUTLINE);
|
2024-05-09 02:45:07 +02:00
|
|
|
if (colour.hasFlag(ColourFlag::withOutline))
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
info->flags |= TEXT_DRAW_FLAG_OUTLINE;
|
|
|
|
|
}
|
2024-05-09 02:45:07 +02:00
|
|
|
if (!colour.hasFlag(ColourFlag::inset))
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
|
|
|
|
if (!(info->flags & TEXT_DRAW_FLAG_INSET))
|
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t flags = info->flags;
|
2024-05-09 02:45:07 +02:00
|
|
|
ColourCharacterWindow(colour.colour, &flags, reinterpret_cast<uint8_t*>(&info->palette));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
info->flags |= TEXT_DRAW_FLAG_INSET;
|
2015-07-26 13:58:53 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t eax;
|
2018-06-22 22:59:03 +02:00
|
|
|
if (info->flags & TEXT_DRAW_FLAG_DARK)
|
|
|
|
|
{
|
|
|
|
|
if (info->flags & TEXT_DRAW_FLAG_EXTRA_DARK)
|
|
|
|
|
{
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = ColourMapA[colour.colour].mid_light;
|
2017-06-06 23:24:18 +02:00
|
|
|
eax = eax << 16;
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = eax | ColourMapA[colour.colour].dark;
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = ColourMapA[colour.colour].light;
|
2017-06-06 23:24:18 +02:00
|
|
|
eax = eax << 16;
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = eax | ColourMapA[colour.colour].mid_dark;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 22:59:03 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = ColourMapA[colour.colour].lighter;
|
2017-06-06 23:24:18 +02:00
|
|
|
eax = eax << 16;
|
2024-05-09 02:45:07 +02:00
|
|
|
eax = eax | ColourMapA[colour.colour].mid_light;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-26 13:58:53 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
// Adjust text palette. Store current colour? ;
|
|
|
|
|
info->palette[1] = eax & 0xFF;
|
|
|
|
|
info->palette[2] = (eax >> 8) & 0xFF;
|
|
|
|
|
info->palette[3] = (eax >> 16) & 0xFF;
|
|
|
|
|
info->palette[4] = (eax >> 24) & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-26 13:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
void TTFDrawString(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, const_utf8string text, ColourWithFlags colour, const ScreenCoordsXY& coords, bool noFormatting,
|
2022-10-12 22:40:59 +02:00
|
|
|
FontStyle fontStyle, TextDarkness darkness)
|
2015-07-26 13:58:53 +01:00
|
|
|
{
|
2018-06-22 22:59:03 +02:00
|
|
|
if (text == nullptr)
|
|
|
|
|
return;
|
2015-08-16 16:36:57 +01:00
|
|
|
|
2023-01-19 08:16:44 +00:00
|
|
|
TextDrawInfo info;
|
2025-10-16 19:56:59 +02:00
|
|
|
info.fontStyle = fontStyle;
|
2021-01-30 17:46:22 +02:00
|
|
|
info.flags = 0;
|
2020-06-21 08:58:29 -03:00
|
|
|
info.startX = coords.x;
|
|
|
|
|
info.startY = coords.y;
|
|
|
|
|
info.x = coords.x;
|
|
|
|
|
info.y = coords.y;
|
2015-07-26 13:58:53 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
if (LocalisationService_UseTrueTypeFont())
|
|
|
|
|
{
|
2017-06-13 20:50:14 +01:00
|
|
|
info.flags |= TEXT_DRAW_FLAG_TTF;
|
|
|
|
|
}
|
2015-07-26 13:58:53 +01:00
|
|
|
|
2020-11-22 00:36:40 +00:00
|
|
|
if (noFormatting)
|
|
|
|
|
{
|
|
|
|
|
info.flags |= TEXT_DRAW_FLAG_NO_FORMATTING;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 20:04:57 +02:00
|
|
|
if (darkness == TextDarkness::dark)
|
2022-10-12 18:27:05 +02:00
|
|
|
{
|
|
|
|
|
info.flags |= TEXT_DRAW_FLAG_DARK;
|
|
|
|
|
}
|
2025-10-16 20:04:57 +02:00
|
|
|
else if (darkness == TextDarkness::extraDark)
|
2022-10-12 18:27:05 +02:00
|
|
|
{
|
|
|
|
|
info.flags |= (TEXT_DRAW_FLAG_DARK | TEXT_DRAW_FLAG_EXTRA_DARK);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 14:21:34 -07:00
|
|
|
std::memcpy(info.palette, gTextPalette, sizeof(info.palette));
|
2023-01-16 22:31:34 +00:00
|
|
|
TTFProcessInitialColour(colour, &info);
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessString(rt, text, &info);
|
2022-07-30 14:21:34 -07:00
|
|
|
std::memcpy(gTextPalette, info.palette, sizeof(info.palette));
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
rt.lastStringPos = { info.x, info.y };
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 22:31:34 +00:00
|
|
|
static int32_t TTFGetStringWidth(std::string_view text, FontStyle fontStyle, bool noFormatting)
|
2015-07-26 00:55:17 +01:00
|
|
|
{
|
2023-01-19 08:16:44 +00:00
|
|
|
TextDrawInfo info;
|
2025-10-16 19:56:59 +02:00
|
|
|
info.fontStyle = fontStyle;
|
2021-01-30 17:46:22 +02:00
|
|
|
info.flags = 0;
|
2017-06-06 23:24:18 +02:00
|
|
|
info.startX = 0;
|
|
|
|
|
info.startY = 0;
|
|
|
|
|
info.x = 0;
|
|
|
|
|
info.y = 0;
|
|
|
|
|
info.maxX = 0;
|
|
|
|
|
info.maxY = 0;
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
info.flags |= TEXT_DRAW_FLAG_NO_DRAW;
|
2018-06-22 22:59:03 +02:00
|
|
|
if (LocalisationService_UseTrueTypeFont())
|
|
|
|
|
{
|
2017-06-13 20:50:14 +01:00
|
|
|
info.flags |= TEXT_DRAW_FLAG_TTF;
|
|
|
|
|
}
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2020-11-22 00:36:40 +00:00
|
|
|
if (noFormatting)
|
|
|
|
|
{
|
|
|
|
|
info.flags |= TEXT_DRAW_FLAG_NO_FORMATTING;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget dummy{};
|
2023-02-24 22:05:07 +01:00
|
|
|
TTFProcessString(dummy, text, &info);
|
2015-07-26 00:55:17 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
return info.maxX;
|
2015-07-26 00:55:17 +01:00
|
|
|
}
|
2015-08-03 18:06:54 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* rct2: 0x00682F28
|
|
|
|
|
*/
|
2023-01-16 18:52:17 +00:00
|
|
|
void GfxDrawStringWithYOffsets(
|
2025-05-05 01:06:46 +03:00
|
|
|
RenderTarget& rt, const utf8* text, ColourWithFlags colour, const ScreenCoordsXY& coords, const int8_t* yOffsets,
|
2022-10-12 22:40:59 +02:00
|
|
|
bool forceSpriteFont, FontStyle fontStyle)
|
2015-08-03 18:06:54 +01:00
|
|
|
{
|
2023-01-19 08:16:44 +00:00
|
|
|
TextDrawInfo info;
|
2025-10-16 19:56:59 +02:00
|
|
|
info.fontStyle = fontStyle;
|
2021-01-30 17:46:22 +02:00
|
|
|
info.flags = 0;
|
2020-06-21 08:55:06 -03:00
|
|
|
info.startX = coords.x;
|
|
|
|
|
info.startY = coords.y;
|
|
|
|
|
info.x = coords.x;
|
|
|
|
|
info.y = coords.y;
|
2025-10-16 19:56:59 +02:00
|
|
|
info.yOffset = yOffsets;
|
2015-08-03 18:06:54 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
info.flags |= TEXT_DRAW_FLAG_Y_OFFSET_EFFECT;
|
2015-08-03 18:06:54 +01:00
|
|
|
|
2018-06-22 22:59:03 +02:00
|
|
|
if (!forceSpriteFont && LocalisationService_UseTrueTypeFont())
|
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
info.flags |= TEXT_DRAW_FLAG_TTF;
|
|
|
|
|
}
|
2015-08-03 18:06:54 +01:00
|
|
|
|
2022-07-30 14:21:34 -07:00
|
|
|
std::memcpy(info.palette, gTextPalette, sizeof(info.palette));
|
2023-01-16 22:31:34 +00:00
|
|
|
TTFProcessInitialColour(colour, &info);
|
2025-05-05 01:06:46 +03:00
|
|
|
TTFProcessString(rt, text, &info);
|
2022-07-30 14:21:34 -07:00
|
|
|
std::memcpy(gTextPalette, info.palette, sizeof(info.palette));
|
2015-08-03 18:06:54 +01:00
|
|
|
|
2025-05-05 01:06:46 +03:00
|
|
|
rt.lastStringPos = { info.x, info.y };
|
2015-08-03 18:06:54 +01:00
|
|
|
}
|
2016-01-02 22:41:35 +00:00
|
|
|
|
2023-08-10 14:39:38 +03:00
|
|
|
u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle)
|
2016-01-02 22:41:35 +00:00
|
|
|
{
|
2023-08-10 14:39:38 +03:00
|
|
|
if (GfxGetStringWidth(path, fontStyle) <= availableWidth)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2023-08-10 14:39:38 +03:00
|
|
|
return path;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-01-02 22:41:35 +00:00
|
|
|
|
2023-08-10 14:39:38 +03:00
|
|
|
u8string shortenedPath = u8"...";
|
|
|
|
|
|
|
|
|
|
size_t begin = 0;
|
|
|
|
|
while (begin < path.size())
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2023-08-10 14:39:38 +03:00
|
|
|
begin = path.find_first_of(*PATH_SEPARATOR, begin + 1);
|
|
|
|
|
if (begin == path.npos)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
shortenedPath = u8"..." + path.substr(begin);
|
|
|
|
|
if (GfxGetStringWidth(shortenedPath, fontStyle) <= availableWidth)
|
2018-06-22 22:59:03 +02:00
|
|
|
{
|
2023-08-10 14:39:38 +03:00
|
|
|
return shortenedPath;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-02 22:41:35 +00:00
|
|
|
|
2023-08-10 14:39:38 +03:00
|
|
|
return shortenedPath;
|
2016-01-02 22:41:35 +00:00
|
|
|
}
|