From b2873d2f86cff6ff905ce3f4c498b2d86efc2d1d Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Fri, 20 Jul 2018 15:08:25 +0200 Subject: [PATCH 1/3] Use more constants --- src/openrct2/drawing/Font.cpp | 2 +- src/openrct2/drawing/Font.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/openrct2/drawing/Font.cpp b/src/openrct2/drawing/Font.cpp index e8e7a2a6c8..09c9383d16 100644 --- a/src/openrct2/drawing/Font.cpp +++ b/src/openrct2/drawing/Font.cpp @@ -18,7 +18,7 @@ static constexpr const int32_t SpriteFontLineHeight[] = { 6, 10, 10, 18 }; -static uint8_t _spriteFontCharacterWidths[896]; +static uint8_t _spriteFontCharacterWidths[FONT_SIZE_COUNT * FONT_SPRITE_GLYPH_COUNT]; #ifndef NO_TTF TTFFontSetDescriptor *gCurrentTTFFontSet; diff --git a/src/openrct2/drawing/Font.h b/src/openrct2/drawing/Font.h index 90b0f0f32b..78a9ce2629 100644 --- a/src/openrct2/drawing/Font.h +++ b/src/openrct2/drawing/Font.h @@ -25,10 +25,10 @@ enum { FONT_SPRITE_BASE_MEDIUM_EXTRA_DARK = -2, FONT_SPRITE_BASE_MEDIUM_DARK = -1, - FONT_SPRITE_BASE_TINY = 448, - FONT_SPRITE_BASE_SMALL = 0, - FONT_SPRITE_BASE_MEDIUM = 224, - FONT_SPRITE_BASE_BIG = 672 + FONT_SPRITE_BASE_TINY = FONT_SIZE_TINY * FONT_SPRITE_GLYPH_COUNT, + FONT_SPRITE_BASE_SMALL = FONT_SIZE_SMALL * FONT_SPRITE_GLYPH_COUNT, + FONT_SPRITE_BASE_MEDIUM = FONT_SIZE_MEDIUM * FONT_SPRITE_GLYPH_COUNT, + FONT_SPRITE_BASE_BIG = FONT_SIZE_BIG * FONT_SPRITE_GLYPH_COUNT }; #ifndef NO_TTF From 41dcae581a952fb6a6d71f8705f29ce6c128fd92 Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Fri, 20 Jul 2018 15:13:09 +0200 Subject: [PATCH 2/3] Remove support for the unused big font --- src/openrct2/drawing/Drawing.String.cpp | 6 ------ src/openrct2/drawing/Font.cpp | 6 ++---- src/openrct2/drawing/Font.h | 4 +--- src/openrct2/interface/Fonts.cpp | 12 ------------ 4 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/openrct2/drawing/Drawing.String.cpp b/src/openrct2/drawing/Drawing.String.cpp index f30323d0f4..8f2c2075e7 100644 --- a/src/openrct2/drawing/Drawing.String.cpp +++ b/src/openrct2/drawing/Drawing.String.cpp @@ -332,9 +332,6 @@ int32_t string_get_height_raw(char *buffer) case FORMAT_TINYFONT: fontBase = FONT_SPRITE_BASE_TINY; break; - case FORMAT_BIGFONT: - fontBase = FONT_SPRITE_BASE_BIG; - break; case FORMAT_MEDIUMFONT: fontBase = FONT_SPRITE_BASE_MEDIUM; break; @@ -640,9 +637,6 @@ static const utf8 *ttf_process_format_code(rct_drawpixelinfo *dpi, const utf8 *t case FORMAT_MEDIUMFONT: info->font_sprite_base = FONT_SPRITE_BASE_MEDIUM; break; - case FORMAT_BIGFONT: - info->font_sprite_base = 672; - break; case FORMAT_OUTLINE: info->flags |= TEXT_DRAW_FLAG_OUTLINE; break; diff --git a/src/openrct2/drawing/Font.cpp b/src/openrct2/drawing/Font.cpp index 09c9383d16..8d70e037af 100644 --- a/src/openrct2/drawing/Font.cpp +++ b/src/openrct2/drawing/Font.cpp @@ -16,7 +16,7 @@ #include "Font.h" #include "TTF.h" -static constexpr const int32_t SpriteFontLineHeight[] = { 6, 10, 10, 18 }; +static constexpr const int32_t SpriteFontLineHeight[FONT_SIZE_COUNT] = { 6, 10, 10 }; static uint8_t _spriteFontCharacterWidths[FONT_SIZE_COUNT * FONT_SPRITE_GLYPH_COUNT]; @@ -38,7 +38,7 @@ void font_sprite_initialise_characters() if (g1 != nullptr) { int32_t width = g1->width + 2 * g1->x_offset; - width += fontSize == FONT_SIZE_BIG ? 1 : -1; + width -= 1; if (glyphIndex >= (FORMAT_ARGUMENT_CODE_START - 32) && glyphIndex < (FORMAT_COLOUR_CODE_END - 32)) { width = 0; } @@ -149,8 +149,6 @@ int32_t font_get_size_from_sprite_base(uint16_t spriteBase) default: case FONT_SPRITE_BASE_MEDIUM: return 2; - case FONT_SPRITE_BASE_BIG: - return 3; } } diff --git a/src/openrct2/drawing/Font.h b/src/openrct2/drawing/Font.h index 78a9ce2629..90b1f10de8 100644 --- a/src/openrct2/drawing/Font.h +++ b/src/openrct2/drawing/Font.h @@ -16,8 +16,7 @@ enum { FONT_SIZE_TINY = 2, FONT_SIZE_SMALL = 0, FONT_SIZE_MEDIUM = 1, - FONT_SIZE_BIG = 3, - FONT_SIZE_COUNT = 4 + FONT_SIZE_COUNT = 3 }; enum { @@ -28,7 +27,6 @@ enum { FONT_SPRITE_BASE_TINY = FONT_SIZE_TINY * FONT_SPRITE_GLYPH_COUNT, FONT_SPRITE_BASE_SMALL = FONT_SIZE_SMALL * FONT_SPRITE_GLYPH_COUNT, FONT_SPRITE_BASE_MEDIUM = FONT_SIZE_MEDIUM * FONT_SPRITE_GLYPH_COUNT, - FONT_SPRITE_BASE_BIG = FONT_SIZE_BIG * FONT_SPRITE_GLYPH_COUNT }; #ifndef NO_TTF diff --git a/src/openrct2/interface/Fonts.cpp b/src/openrct2/interface/Fonts.cpp index 8453b1fa8b..397d7db913 100644 --- a/src/openrct2/interface/Fonts.cpp +++ b/src/openrct2/interface/Fonts.cpp @@ -28,70 +28,60 @@ TTFFontSetDescriptor TTFFontMSGothic = { { { "msgothic.ttc", "MS PGothic", 9, 1, 1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, { "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, - { "msgothic.ttc", "MS PGothic", 13, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontHiragano = { { { u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 9, 1, 1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 11, 1, 0, 13, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 11, 1, 0, 13, HINTING_THRESHOLD_MEDIUM, nullptr }, - { u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontMingLiu = { { { "msjh.ttc", "JhengHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { "mingliu.ttc", "MingLiU", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, { "mingliu.ttc", "MingLiU", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, - { "mingliu.ttc", "MingLiU", 13, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontHeiti = { { { u8"华文黑体.ttf", "STHeiti", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"华文黑体.ttf", "STHeiti", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"华文黑体.ttf", "STHeiti", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, - { u8"华文黑体.ttf", "STHeiti", 13, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontSimSun = { { { "msyh.ttc", "YaHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { "simsun.ttc", "SimSun", 11, 1, -1, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, { "simsun.ttc", "SimSun", 12, 1, -2, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, - { "simsun.ttc", "SimSun", 13, 1, 0, 16, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontLiHeiPro = { { { u8"儷黑 Pro.ttf", "LiHei Pro", 9, 1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"儷黑 Pro.ttf", "LiHei Pro", 11, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr }, { u8"儷黑 Pro.ttf", "LiHei Pro", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, - { u8"儷黑 Pro.ttf", "LiHei Pro", 13, 1, 0, 16, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontGulim = { { { "gulim.ttc", "Gulim", 10, 1, 0, 10, HINTING_DISABLED, nullptr }, { "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, { "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr }, - { "gulim.ttc", "Gulim", 13, 1, 0, 16, HINTING_THRESHOLD_MEDIUM, nullptr }, } }; TTFFontSetDescriptor TTFFontNanum = { { { "NanumGothic.ttc", "Nanum Gothic", 10, 1, 0, 10, HINTING_DISABLED, nullptr }, { "NanumGothic.ttc", "Nanum Gothic", 12, 1, 0, 15, HINTING_THRESHOLD_LOW, nullptr }, { "NanumGothic.ttc", "Nanum Gothic", 12, 1, 0, 15, HINTING_THRESHOLD_LOW, nullptr }, - { "NanumGothic.ttc", "Nanum Gothic", 13, 1, 0, 16, HINTING_THRESHOLD_LOW, nullptr }, } }; TTFFontSetDescriptor TTFFontArial = { { { "arial.ttf", "Arial", 9, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr }, { "arial.ttf", "Arial", 10, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr }, { "arial.ttf", "Arial", 11, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr }, - { "arial.ttf", "Arial", 12, 0, -1, 14, HINTING_THRESHOLD_LOW, nullptr }, } }; TTFFontSetDescriptor TTFFontArialUnicode = { { { "arialuni.ttf", "Arial Unicode MS", 9, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr }, { "arialuni.ttf", "Arial Unicode MS", 10, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr }, { "arialuni.ttf", "Arial Unicode MS", 11, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr }, - { "arialuni.ttf", "Arial Unicode MS", 12, 0, -1, 14, HINTING_THRESHOLD_LOW, nullptr }, } }; // clang-format on #endif // NO_TTF @@ -125,8 +115,6 @@ static bool LoadCustomConfigFont(LocalisationService& localisationService) gConfigFonts.height_small, gConfigFonts.hinting_threshold, nullptr }, { gConfigFonts.file_name, gConfigFonts.font_name, gConfigFonts.size_medium, gConfigFonts.x_offset, gConfigFonts.y_offset, gConfigFonts.height_medium, gConfigFonts.hinting_threshold, nullptr }, - { gConfigFonts.file_name, gConfigFonts.font_name, gConfigFonts.size_big, gConfigFonts.x_offset, gConfigFonts.y_offset, - gConfigFonts.height_big, gConfigFonts.hinting_threshold, nullptr }, } }; ttf_dispose(); From 93bc11a93a359948f4e7e982151e758846f40c2a Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Fri, 20 Jul 2018 15:51:31 +0200 Subject: [PATCH 3/3] Refactor _spriteFontCharacterWidths --- src/openrct2/drawing/Font.cpp | 37 +++++++++++++++++++++++------------ src/openrct2/drawing/Font.h | 1 + 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/openrct2/drawing/Font.cpp b/src/openrct2/drawing/Font.cpp index 8d70e037af..708aab58fe 100644 --- a/src/openrct2/drawing/Font.cpp +++ b/src/openrct2/drawing/Font.cpp @@ -18,7 +18,7 @@ static constexpr const int32_t SpriteFontLineHeight[FONT_SIZE_COUNT] = { 6, 10, 10 }; -static uint8_t _spriteFontCharacterWidths[FONT_SIZE_COUNT * FONT_SPRITE_GLYPH_COUNT]; +static uint8_t _spriteFontCharacterWidths[FONT_SIZE_COUNT][FONT_SPRITE_GLYPH_COUNT]; #ifndef NO_TTF TTFFontSetDescriptor *gCurrentTTFFontSet; @@ -30,20 +30,19 @@ TTFFontSetDescriptor *gCurrentTTFFontSet; */ void font_sprite_initialise_characters() { - uint8_t* pCharacterWidth = _spriteFontCharacterWidths; for (int32_t fontSize = 0; fontSize < FONT_SIZE_COUNT; fontSize++) { int32_t glyphOffset = fontSize * FONT_SPRITE_GLYPH_COUNT; 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); + int32_t width = 0; if (g1 != nullptr) { - int32_t width = g1->width + 2 * g1->x_offset; - width -= 1; - if (glyphIndex >= (FORMAT_ARGUMENT_CODE_START - 32) && glyphIndex < (FORMAT_COLOUR_CODE_END - 32)) { - width = 0; + if (glyphIndex < (FORMAT_ARGUMENT_CODE_START - 32) || glyphIndex >= (FORMAT_COLOUR_CODE_END - 32)) { + width = (g1->width + 2 * g1->x_offset) - 1; } - *pCharacterWidth++ = (uint8_t)width; } + + _spriteFontCharacterWidths[fontSize][glyphIndex] = (uint8_t)width; } } @@ -125,13 +124,14 @@ int32_t font_sprite_get_codepoint_width(uint16_t fontSpriteBase, int32_t codepoi fontSpriteBase = (uint16_t)FONT_SPRITE_BASE_MEDIUM; } - int32_t spriteFontIdx = fontSpriteBase + font_sprite_get_codepoint_offset(codepoint); - if (spriteFontIdx < 0 || spriteFontIdx >= (int32_t)Util::CountOf(_spriteFontCharacterWidths)) + int32_t glyphIndex = font_sprite_get_codepoint_offset(codepoint); + int32_t baseFontIndex = font_get_font_index_from_sprite_base(fontSpriteBase); + if (glyphIndex < 0 || glyphIndex >= (int32_t)FONT_SPRITE_GLYPH_COUNT) { - log_warning("Invalid font index %u", spriteFontIdx); - spriteFontIdx = 0; + log_warning("Invalid glyph index %u", glyphIndex); + glyphIndex = 0; } - return _spriteFontCharacterWidths[spriteFontIdx]; + return _spriteFontCharacterWidths[baseFontIndex][glyphIndex]; } int32_t font_sprite_get_codepoint_sprite(int32_t fontSpriteBase, int32_t codepoint) @@ -139,6 +139,19 @@ int32_t font_sprite_get_codepoint_sprite(int32_t fontSpriteBase, int32_t codepoi return SPR_CHAR_START + (IMAGE_TYPE_REMAP | (fontSpriteBase + font_sprite_get_codepoint_offset(codepoint))); } +int32_t font_get_font_index_from_sprite_base(uint16_t spriteBase) +{ + switch (spriteBase) { + case FONT_SPRITE_BASE_TINY: + return FONT_SIZE_TINY; + case FONT_SPRITE_BASE_SMALL: + return FONT_SIZE_SMALL; + default: + case FONT_SPRITE_BASE_MEDIUM: + return FONT_SIZE_MEDIUM; + } +} + int32_t font_get_size_from_sprite_base(uint16_t spriteBase) { switch (spriteBase) { diff --git a/src/openrct2/drawing/Font.h b/src/openrct2/drawing/Font.h index 90b1f10de8..6ec21b1689 100644 --- a/src/openrct2/drawing/Font.h +++ b/src/openrct2/drawing/Font.h @@ -56,6 +56,7 @@ void font_sprite_initialise_characters(); int32_t font_sprite_get_codepoint_offset(int32_t codepoint); int32_t font_sprite_get_codepoint_width(uint16_t fontSpriteBase, int32_t codepoint); int32_t font_sprite_get_codepoint_sprite(int32_t fontSpriteBase, int32_t codepoint); +int32_t font_get_font_index_from_sprite_base(uint16_t spriteBase); int32_t font_get_size_from_sprite_base(uint16_t spriteBase); int32_t font_get_line_height(int32_t fontSpriteBase); int32_t font_get_line_height_small(int32_t fontSpriteBase);