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

441 lines
20 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 22:59:03 +02:00
#include "Font.h"
2018-03-13 13:14:02 +01:00
#include "../localisation/FormatCodes.h"
#include "../localisation/Language.h"
2018-04-26 23:03:02 +01:00
#include "../localisation/LocalisationService.h"
2015-07-29 14:36:21 +01:00
#include "../sprites.h"
2018-01-05 21:17:33 +00:00
#include "Drawing.h"
2018-01-05 21:14:20 +00:00
#include "TTF.h"
2015-07-29 14:36:21 +01:00
2018-11-21 23:16:04 +01:00
#include <iterator>
#include <unordered_map>
2018-09-22 20:08:06 +02:00
2018-07-20 15:13:09 +02:00
static constexpr const int32_t SpriteFontLineHeight[FONT_SIZE_COUNT] = { 6, 10, 10 };
2018-07-20 15:51:31 +02:00
static uint8_t _spriteFontCharacterWidths[FONT_SIZE_COUNT][FONT_SPRITE_GLYPH_COUNT];
2018-07-20 17:33:20 +02:00
static uint8_t _additionalSpriteFontCharacterWidth[FONT_SIZE_COUNT][SPR_G2_GLYPH_COUNT] = {};
2015-07-29 14:36:21 +01:00
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2018-06-22 22:59:03 +02:00
TTFFontSetDescriptor* gCurrentTTFFontSet;
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
static const std::unordered_map<char32_t, int32_t> codepointOffsetMap = {
2018-09-22 20:08:06 +02:00
{ UnicodeChar::ae_uc, SPR_G2_AE_UPPER - SPR_CHAR_START },
{ UnicodeChar::o_stroke_uc, SPR_G2_O_STROKE_UPPER - SPR_CHAR_START },
{ UnicodeChar::y_acute_uc, SPR_G2_Y_ACUTE_UPPER - SPR_CHAR_START },
{ UnicodeChar::ae, SPR_G2_AE_LOWER - SPR_CHAR_START },
{ UnicodeChar::o_stroke, SPR_G2_O_STROKE_LOWER - SPR_CHAR_START },
{ UnicodeChar::y_acute, SPR_G2_Y_ACUTE_LOWER - SPR_CHAR_START },
{ UnicodeChar::a_breve_uc, SPR_G2_A_BREVE_UPPER - SPR_CHAR_START },
{ UnicodeChar::a_breve, 226 - CS_SPRITE_FONT_OFFSET }, // Render as â, no visual difference in the RCT font
{ UnicodeChar::a_ogonek_uc, CSChar::a_ogonek_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::a_ogonek, CSChar::a_ogonek - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::c_acute_uc, CSChar::c_acute_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::c_acute, CSChar::c_acute - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::c_caron_uc, SPR_G2_C_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::c_caron, SPR_G2_C_CARON_LOWER - SPR_CHAR_START },
2018-09-23 22:23:37 +02:00
{ UnicodeChar::d_caron_uc, SPR_G2_D_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::d_caron, SPR_G2_D_CARON_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::e_ogonek_uc, CSChar::e_ogonek_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::e_ogonek, CSChar::e_ogonek - CS_SPRITE_FONT_OFFSET },
2018-09-23 22:23:37 +02:00
{ UnicodeChar::e_caron_uc, SPR_G2_E_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::e_caron, SPR_G2_E_CARON_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::g_breve_uc, SPR_G2_G_BREVE_UPPER - SPR_CHAR_START },
{ UnicodeChar::g_breve, SPR_G2_G_BREVE_LOWER - SPR_CHAR_START },
{ UnicodeChar::i_with_dot_uc, SPR_G2_I_WITH_DOT_UPPER - SPR_CHAR_START },
{ UnicodeChar::i_without_dot, SPR_G2_I_WITHOUT_DOT_LOWER - SPR_CHAR_START },
{ UnicodeChar::j, SPR_G2_J - SPR_CHAR_START },
{ UnicodeChar::l, SPR_G2_L - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::l_stroke_uc, CSChar::l_stroke_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::l_stroke, CSChar::l_stroke - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::n_acute_uc, CSChar::n_acute_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::n_acute, CSChar::n_acute - CS_SPRITE_FONT_OFFSET },
2019-02-24 22:31:02 +01:00
{ UnicodeChar::n_caron_uc, SPR_G2_N_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::n_caron, SPR_G2_N_CARON_LOWER - SPR_CHAR_START },
2020-04-01 14:31:53 +02:00
{ UnicodeChar::o_macron, CSChar::o_circumflex - CS_SPRITE_FONT_OFFSET }, // No visual difference
2018-09-22 20:08:06 +02:00
{ UnicodeChar::o_double_acute_uc, SPR_G2_O_DOUBLE_ACUTE_UPPER - SPR_CHAR_START },
{ UnicodeChar::o_double_acute, SPR_G2_O_DOUBLE_ACUTE_LOWER - SPR_CHAR_START },
2021-07-20 14:28:43 +02:00
{ UnicodeChar::oe_uc, SPR_G2_OE_UPPER - SPR_CHAR_START },
{ UnicodeChar::oe, SPR_G2_OE_LOWER - SPR_CHAR_START },
2019-03-24 21:57:56 +01:00
{ UnicodeChar::r_caron_uc, SPR_G2_R_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::r_caron, SPR_G2_R_CARON_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::s_acute_uc, CSChar::s_acute_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::s_acute, CSChar::s_acute - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::s_cedilla_uc, SPR_G2_S_CEDILLA_UPPER - SPR_CHAR_START },
{ UnicodeChar::s_cedilla, SPR_G2_S_CEDILLA_LOWER - SPR_CHAR_START },
2019-03-25 16:15:33 +01:00
{ UnicodeChar::s_caron_uc, SPR_G2_S_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::s_caron, SPR_G2_S_CARON_LOWER - SPR_CHAR_START },
2019-03-25 16:51:27 +01:00
{ UnicodeChar::t_caron_uc, SPR_G2_T_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::t_caron, SPR_G2_T_CARON_LOWER - SPR_CHAR_START },
{ UnicodeChar::u_ring_uc, SPR_G2_U_RING_UPPER - SPR_CHAR_START },
{ UnicodeChar::u_ring, SPR_G2_U_RING_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::u_double_acute_uc, SPR_G2_U_DOUBLE_ACUTE_UPPER - SPR_CHAR_START },
{ UnicodeChar::u_double_acute, SPR_G2_U_DOUBLE_ACUTE_LOWER - SPR_CHAR_START },
{ UnicodeChar::z_acute_uc, CSChar::z_acute_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::z_acute, CSChar::z_acute - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::z_dot_uc, CSChar::z_dot_uc - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::z_dot, CSChar::z_dot - CS_SPRITE_FONT_OFFSET },
2019-03-25 17:02:35 +01:00
{ UnicodeChar::z_caron_uc, SPR_G2_Z_CARON_UPPER - SPR_CHAR_START },
{ UnicodeChar::z_caron, SPR_G2_Z_CARON_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::f_with_hook_uc, 'F' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::s_comma_uc, SPR_G2_S_CEDILLA_UPPER - SPR_CHAR_START }, // No visual difference
{ UnicodeChar::s_comma, SPR_G2_S_CEDILLA_LOWER - SPR_CHAR_START }, // Ditto
{ UnicodeChar::t_comma_uc, SPR_G2_T_COMMA_UPPER - SPR_CHAR_START },
{ UnicodeChar::t_comma, SPR_G2_T_COMMA_LOWER - SPR_CHAR_START },
{ UnicodeChar::sharp_s_uc, 223 - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::c_circumflex_uc, SPR_G2_C_CIRCUMFLEX_UPPER - SPR_CHAR_START },
{ UnicodeChar::c_circumflex, SPR_G2_C_CIRCUMFLEX_LOWER - SPR_CHAR_START },
{ UnicodeChar::g_circumflex_uc, SPR_G2_G_CIRCUMFLEX_UPPER - SPR_CHAR_START },
{ UnicodeChar::g_circumflex, SPR_G2_G_CIRCUMFLEX_LOWER - SPR_CHAR_START },
{ UnicodeChar::h_circumflex_uc, SPR_G2_H_CIRCUMFLEX_UPPER - SPR_CHAR_START },
{ UnicodeChar::h_circumflex, SPR_G2_H_CIRCUMFLEX_LOWER - SPR_CHAR_START },
{ UnicodeChar::j_circumflex_uc, SPR_G2_J_CIRCUMFLEX_UPPER - SPR_CHAR_START },
{ UnicodeChar::j_circumflex, SPR_G2_J_CIRCUMFLEX_LOWER - SPR_CHAR_START },
{ UnicodeChar::s_circumflex_uc, SPR_G2_S_CIRCUMFLEX_UPPER - SPR_CHAR_START },
{ UnicodeChar::s_circumflex, SPR_G2_S_CIRCUMFLEX_LOWER - SPR_CHAR_START },
{ UnicodeChar::u_breve_uc, SPR_G2_U_BREVE_UPPER - SPR_CHAR_START },
{ UnicodeChar::u_breve, SPR_G2_U_BREVE_LOWER - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
// Cyrillic alphabet
{ UnicodeChar::cyrillic_io_uc, 203 - CS_SPRITE_FONT_OFFSET }, // Looks just like Ë
{ UnicodeChar::cyrillic_a_uc, 'A' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_be_uc, SPR_G2_CYRILLIC_BE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ve_uc, 'B' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_ghe_uc, SPR_G2_CYRILLIC_GHE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_de_uc, SPR_G2_CYRILLIC_DE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ie_uc, 'E' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_zhe_uc, SPR_G2_CYRILLIC_ZHE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ze_uc, SPR_G2_CYRILLIC_ZE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_i_uc, SPR_G2_CYRILLIC_I_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_short_i_uc, SPR_G2_CYRILLIC_SHORT_I_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ka_uc, 'K' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_el_uc, SPR_G2_CYRILLIC_EL_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_em_uc, 'M' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_en_uc, 'H' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_o_uc, 'O' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_pe_uc, SPR_G2_CYRILLIC_PE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_er_uc, 'P' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_es_uc, 'C' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_te_uc, 'T' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_u_uc, SPR_G2_CYRILLIC_U_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ef_uc, SPR_G2_CYRILLIC_EF_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ha_uc, 'X' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_tse_uc, SPR_G2_CYRILLIC_TSE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_che_uc, SPR_G2_CYRILLIC_CHE_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_sha_uc, SPR_G2_CYRILLIC_SHA_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_shcha_uc, SPR_G2_CYRILLIC_SHCHA_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_hard_sign_uc, SPR_G2_CYRILLIC_HARD_SIGN_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_yeru_uc, SPR_G2_CYRILLIC_YERU_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_soft_sign_uc, SPR_G2_CYRILLIC_SOFT_SIGN_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_e_uc, SPR_G2_CYRILLIC_E_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_yu_uc, SPR_G2_CYRILLIC_YU_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ya_uc, SPR_G2_CYRILLIC_YA_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_a, 'a' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_be, SPR_G2_CYRILLIC_BE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ve, SPR_G2_CYRILLIC_VE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ghe, SPR_G2_CYRILLIC_GHE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_de, SPR_G2_CYRILLIC_DE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ie, 'e' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_zhe, SPR_G2_CYRILLIC_ZHE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ze, SPR_G2_CYRILLIC_ZE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_i, SPR_G2_CYRILLIC_I_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_short_i, SPR_G2_CYRILLIC_SHORT_I_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ka, SPR_G2_CYRILLIC_KA_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_el, SPR_G2_CYRILLIC_EL_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_em, SPR_G2_CYRILLIC_EM_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_en, SPR_G2_CYRILLIC_EN_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_o, 'o' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_pe, SPR_G2_CYRILLIC_PE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_er, 'p' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_es, 'c' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_te, SPR_G2_CYRILLIC_TE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_u, 'y' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_ef, SPR_G2_CYRILLIC_EF_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ha, 'x' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::cyrillic_tse, SPR_G2_CYRILLIC_TSE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_che, SPR_G2_CYRILLIC_CHE_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_sha, SPR_G2_CYRILLIC_SHA_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_shcha, SPR_G2_CYRILLIC_SHCHA_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_hard_sign, SPR_G2_CYRILLIC_HARD_SIGN_UPPER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_yeru, SPR_G2_CYRILLIC_YERU_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_soft_sign, SPR_G2_CYRILLIC_SOFT_SIGN_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_e, SPR_G2_CYRILLIC_E_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_yu, SPR_G2_CYRILLIC_YU_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_ya, SPR_G2_CYRILLIC_YA_LOWER - SPR_CHAR_START },
{ UnicodeChar::cyrillic_io, 235 - CS_SPRITE_FONT_OFFSET }, // Looks just like ë
// Punctuation
2018-11-09 23:33:34 +01:00
{ UnicodeChar::non_breaking_space, ' ' - CS_SPRITE_FONT_OFFSET },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::interpunct, SPR_G2_INTERPUNCT - SPR_CHAR_START },
{ UnicodeChar::multiplication_sign, CSChar::cross - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::en_dash, '-' - CS_SPRITE_FONT_OFFSET },
2021-07-20 13:34:22 +02:00
{ UnicodeChar::em_dash, '-' - CS_SPRITE_FONT_OFFSET },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::single_quote_open, '`' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::single_quote_end, '\'' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::single_german_quote_open, ',' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::german_quote_open, SPR_G2_GERMAN_OPENQUOTES - SPR_CHAR_START },
{ UnicodeChar::bullet, CSChar::bullet - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::ellipsis, SPR_G2_ELLIPSIS - SPR_CHAR_START },
2018-11-09 23:33:34 +01:00
{ UnicodeChar::narrow_non_breaking_space, ' ' - CS_SPRITE_FONT_OFFSET },
2018-09-22 20:08:06 +02:00
{ UnicodeChar::quote_open, CSChar::quote_open - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::quote_close, CSChar::quote_close - CS_SPRITE_FONT_OFFSET },
// Currency
{ UnicodeChar::guilder, SPR_G2_GUILDER_SIGN - SPR_CHAR_START },
{ UnicodeChar::euro, CSChar::euro - CS_SPRITE_FONT_OFFSET },
2018-11-09 23:17:39 +01:00
{ UnicodeChar::rouble, SPR_G2_ROUBLE_SIGN - SPR_CHAR_START },
2018-09-22 20:08:06 +02:00
// Dingbats
{ UnicodeChar::up, CSChar::up - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::small_up, CSChar::small_up - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::right, CSChar::right - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::down, CSChar::down - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::small_down, CSChar::small_down - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::left, CSChar::left - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::air, CSChar::air - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::tick, CSChar::tick - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::plus, '+' - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::minus, '-' - CS_SPRITE_FONT_OFFSET },
// Emoji
{ UnicodeChar::cross, CSChar::cross - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::water, CSChar::water - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::road, CSChar::road - CS_SPRITE_FONT_OFFSET },
{ UnicodeChar::railway, CSChar::railway - CS_SPRITE_FONT_OFFSET },
// Misc
{ UnicodeChar::superscript_minus_one, CSChar::superscript_minus_one - CS_SPRITE_FONT_OFFSET },
};
static char32_t _smallestCodepointValue = 0;
static char32_t _biggestCodepointValue = 0;
2015-07-29 14:36:21 +01:00
/**
*
* rct2: 0x006C19AC
*/
void font_sprite_initialise_characters()
{
// Compute min and max that helps avoiding lookups for no reason.
_smallestCodepointValue = std::numeric_limits<char32_t>::max();
for (const auto& entry : codepointOffsetMap)
{
_smallestCodepointValue = std::min(_smallestCodepointValue, entry.first);
_biggestCodepointValue = std::max(_biggestCodepointValue, entry.first);
}
2018-06-22 22:59:03 +02:00
for (int32_t fontSize = 0; fontSize < FONT_SIZE_COUNT; fontSize++)
{
int32_t glyphOffset = fontSize * FONT_SPRITE_GLYPH_COUNT;
2018-06-22 22:59:03 +02:00
for (uint8_t glyphIndex = 0; glyphIndex < FONT_SPRITE_GLYPH_COUNT; glyphIndex++)
{
const rct_g1_element* g1 = gfx_get_g1_element(glyphIndex + SPR_CHAR_START + glyphOffset);
2018-07-20 15:51:31 +02:00
int32_t width = 0;
2018-01-07 21:43:07 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
2020-11-27 18:54:51 +00:00
width = g1->width + (2 * g1->x_offset) - 1;
2017-06-06 23:24:18 +02:00
}
2018-07-20 15:51:31 +02:00
2020-04-22 17:09:29 +02:00
_spriteFontCharacterWidths[fontSize][glyphIndex] = static_cast<uint8_t>(width);
2017-06-06 23:24:18 +02:00
}
}
2015-10-20 20:16:30 +02:00
2018-07-20 17:33:20 +02:00
for (uint8_t fontSize : { FONT_SIZE_SMALL, FONT_SIZE_MEDIUM, FONT_SIZE_TINY })
{
int32_t glyphOffset = fontSize * SPR_G2_GLYPH_COUNT;
for (int32_t glyphIndex = 0; glyphIndex < SPR_G2_GLYPH_COUNT; glyphIndex++)
{
2018-06-22 22:59:03 +02:00
const rct_g1_element* g1 = gfx_get_g1_element(glyphIndex + SPR_G2_CHAR_BEGIN + glyphOffset);
2018-07-20 17:33:20 +02:00
int32_t width = 0;
if (g1 != nullptr)
{
width = g1->width + (2 * g1->x_offset) - 1;
}
2020-04-22 17:09:29 +02:00
_additionalSpriteFontCharacterWidth[fontSize][glyphIndex] = static_cast<uint8_t>(width);
2018-07-20 17:33:20 +02:00
}
}
2017-06-06 23:24:18 +02:00
scrolling_text_initialise_bitmaps();
2015-07-29 14:36:21 +01:00
}
int32_t font_sprite_get_codepoint_offset(int32_t codepoint)
2015-07-29 14:36:21 +01:00
{
// Only search the table when its in range of the map.
if (static_cast<char32_t>(codepoint) >= _smallestCodepointValue
&& static_cast<char32_t>(codepoint) <= _biggestCodepointValue)
{
auto result = codepointOffsetMap.find(codepoint);
if (result != codepointOffsetMap.end())
return result->second;
}
2015-07-29 14:36:21 +01:00
2018-09-22 20:08:06 +02:00
if (codepoint < 32 || codepoint >= 256)
codepoint = '?';
2015-07-29 14:36:21 +01:00
2018-09-22 20:08:06 +02:00
return codepoint - 32;
2015-07-29 14:36:21 +01:00
}
2021-02-27 12:40:20 +01:00
int32_t font_sprite_get_codepoint_width(FontSpriteBase fontSpriteBase, int32_t codepoint)
2015-07-29 14:36:21 +01:00
{
2021-02-27 12:40:20 +01:00
if (fontSpriteBase == FontSpriteBase::MEDIUM_DARK || fontSpriteBase == FontSpriteBase::MEDIUM_EXTRA_DARK)
{
2021-02-27 12:40:20 +01:00
fontSpriteBase = FontSpriteBase::MEDIUM;
}
2017-12-02 19:46:02 +01:00
2018-07-20 15:51:31 +02:00
int32_t glyphIndex = font_sprite_get_codepoint_offset(codepoint);
int32_t baseFontIndex = font_get_font_index_from_sprite_base(fontSpriteBase);
2018-07-20 17:33:20 +02:00
if (glyphIndex >= FONT_SPRITE_GLYPH_COUNT)
{
glyphIndex = (SPR_CHAR_START + glyphIndex) - SPR_G2_CHAR_BEGIN;
2020-04-22 17:09:29 +02:00
if (glyphIndex >= static_cast<int32_t>(std::size(_additionalSpriteFontCharacterWidth[baseFontIndex])))
2018-07-20 17:33:20 +02:00
{
log_warning("Invalid glyph index %u", glyphIndex);
glyphIndex = 0;
}
return _additionalSpriteFontCharacterWidth[baseFontIndex][glyphIndex];
}
2020-04-22 17:09:29 +02:00
else if (glyphIndex < 0 || glyphIndex >= static_cast<int32_t>(FONT_SPRITE_GLYPH_COUNT))
{
2018-07-20 15:51:31 +02:00
log_warning("Invalid glyph index %u", glyphIndex);
glyphIndex = 0;
}
2018-07-20 15:51:31 +02:00
return _spriteFontCharacterWidths[baseFontIndex][glyphIndex];
2015-07-29 14:36:21 +01:00
}
2021-02-27 12:40:20 +01:00
int32_t font_sprite_get_codepoint_sprite(FontSpriteBase fontSpriteBase, int32_t codepoint)
2015-07-29 14:36:21 +01:00
{
2021-02-27 12:40:20 +01:00
int32_t offset = static_cast<int32_t>(fontSpriteBase);
2018-07-20 17:33:20 +02:00
auto codePointOffset = font_sprite_get_codepoint_offset(codepoint);
if (codePointOffset > FONT_SPRITE_GLYPH_COUNT)
{
2021-02-27 12:40:20 +01:00
offset = font_get_font_index_from_sprite_base(fontSpriteBase) * SPR_G2_GLYPH_COUNT;
2018-07-20 17:33:20 +02:00
}
2021-02-27 12:40:20 +01:00
return SPR_CHAR_START + (IMAGE_TYPE_REMAP | (offset + codePointOffset));
2015-07-29 14:36:21 +01:00
}
2021-02-27 12:40:20 +01:00
int32_t font_get_font_index_from_sprite_base(FontSpriteBase spriteBase)
2018-07-20 15:51:31 +02:00
{
2018-06-22 22:59:03 +02:00
switch (spriteBase)
{
2021-02-27 12:40:20 +01:00
case FontSpriteBase::TINY:
2018-07-20 15:51:31 +02:00
return FONT_SIZE_TINY;
2021-02-27 12:40:20 +01:00
case FontSpriteBase::SMALL:
2018-07-20 15:51:31 +02:00
return FONT_SIZE_SMALL;
default:
2021-02-27 12:40:20 +01:00
case FontSpriteBase::MEDIUM:
2018-07-20 15:51:31 +02:00
return FONT_SIZE_MEDIUM;
}
}
2021-02-27 12:40:20 +01:00
int32_t font_get_size_from_sprite_base(FontSpriteBase spriteBase)
{
2018-06-22 22:59:03 +02:00
switch (spriteBase)
{
2021-02-27 12:40:20 +01:00
case FontSpriteBase::TINY:
2018-06-22 22:59:03 +02:00
return 0;
2021-02-27 12:40:20 +01:00
case FontSpriteBase::SMALL:
2018-06-22 22:59:03 +02:00
return 1;
default:
2021-02-27 12:40:20 +01:00
case FontSpriteBase::MEDIUM:
2018-06-22 22:59:03 +02:00
return 2;
2017-06-06 23:24:18 +02:00
}
}
2021-02-27 12:40:20 +01:00
int32_t font_get_line_height(FontSpriteBase fontSpriteBase)
{
int32_t fontSize = font_get_size_from_sprite_base(fontSpriteBase);
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2018-06-22 22:59:03 +02:00
if (LocalisationService_UseTrueTypeFont())
{
2017-06-06 23:24:18 +02:00
return gCurrentTTFFontSet->size[fontSize].line_height;
2018-06-22 22:59:03 +02:00
}
else
{
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
2017-06-06 23:24:18 +02:00
return SpriteFontLineHeight[fontSize];
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2017-06-06 23:24:18 +02:00
}
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
}
2021-02-27 12:40:20 +01:00
int32_t font_get_line_height_small(FontSpriteBase fontSpriteBase)
{
2017-06-06 23:24:18 +02:00
return font_get_line_height(fontSpriteBase) / 2;
}
2018-06-22 22:59:03 +02:00
bool font_supports_string_sprite(const utf8* text)
{
2018-06-22 22:59:03 +02:00
const utf8* src = text;
uint32_t codepoint;
2018-06-22 22:59:03 +02:00
while ((codepoint = utf8_get_next(src, &src)) != 0)
{
2017-06-06 23:24:18 +02:00
bool supported = false;
2018-09-22 20:08:06 +02:00
if ((codepoint >= 32 && codepoint < 256)
|| (codepoint >= UnicodeChar::cyrillic_a_uc && codepoint <= UnicodeChar::cyrillic_ya))
2018-06-22 22:59:03 +02:00
{
2018-09-22 20:08:06 +02:00
supported = true;
2017-06-06 23:24:18 +02:00
}
2017-10-09 23:25:35 +02:00
2018-09-22 20:08:06 +02:00
auto result = codepointOffsetMap.find(codepoint);
if (result != codepointOffsetMap.end())
supported = true;
2018-06-22 22:59:03 +02:00
if (!supported)
{
2017-06-06 23:24:18 +02:00
return false;
}
}
return true;
}
2018-06-22 22:59:03 +02:00
bool font_supports_string_ttf(const utf8* text, int32_t fontSize)
{
2017-01-04 13:51:12 +01:00
#ifndef NO_TTF
2018-06-22 22:59:03 +02:00
const utf8* src = text;
const TTF_Font* font = gCurrentTTFFontSet->size[fontSize].font;
if (font == nullptr)
{
2017-06-06 23:24:18 +02:00
return false;
}
uint32_t codepoint;
2018-06-22 22:59:03 +02:00
while ((codepoint = utf8_get_next(src, &src)) != 0)
{
bool supported = ttf_provides_glyph(font, codepoint);
2018-06-22 22:59:03 +02:00
if (!supported)
{
2017-06-06 23:24:18 +02:00
return false;
}
}
return true;
2017-01-04 13:51:12 +01:00
#else
2017-06-06 23:24:18 +02:00
return false;
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
2017-01-04 13:51:12 +01:00
}
2018-06-22 22:59:03 +02:00
bool font_supports_string(const utf8* text, int32_t fontSize)
{
2018-06-22 22:59:03 +02:00
if (LocalisationService_UseTrueTypeFont())
{
2017-06-06 23:24:18 +02:00
return font_supports_string_ttf(text, fontSize);
2018-06-22 22:59:03 +02:00
}
else
{
2017-06-06 23:24:18 +02:00
return font_supports_string_sprite(text);
}
}