Merge pull request #7801 from Gymnasiast/refactor/sprite-font

Refactor sprite font code
This commit is contained in:
Michael Steenbeek 2018-07-20 16:21:11 +02:00 committed by GitHub
commit a9a9f3b517
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 39 deletions

View File

@ -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;

View File

@ -16,9 +16,9 @@
#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[896];
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 += fontSize == FONT_SIZE_BIG ? 1 : -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) {
@ -149,8 +162,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;
}
}

View File

@ -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 {
@ -25,10 +24,9 @@ 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,
};
#ifndef NO_TTF
@ -58,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);

View File

@ -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();