From 6a4533dd02c053fc24738fdd285b398e3d7767c1 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Fri, 13 Aug 2021 14:31:10 -0700 Subject: BLF Cleanup: Size Defines, Comments, etc This patch makes some non-functional changes to BLF code. Some size defines added, comments changed, simplification of macro BLF_KERNING_VARS. See D12200 for more details. Differential Revision: https://developer.blender.org/D12200 Reviewed by Campbell Barton --- source/blender/blenfont/intern/blf_font.c | 19 +++++++++---------- source/blender/blenfont/intern/blf_glyph.c | 7 ++----- .../blender/blenfont/intern/blf_internal_types.h | 22 +++++++++++++--------- 3 files changed, 24 insertions(+), 24 deletions(-) (limited to 'source/blender/blenfont') diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 8e306730e3c..0dfb2e843c9 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -318,7 +318,7 @@ static GlyphBLF **blf_font_ensure_ascii_table(FontBLF *font, GlyphCacheBLF *gc) if (glyph_ascii_table['0'] == NULL) { GlyphBLF *g; /* Skip control characters and just cache rendered glyphs for visible ASCII range. */ - for (uint i = 32; i < 128; i++) { + for (uint i = GLYPH_ASCII_CACHE_MIN; i <= GLYPH_ASCII_CACHE_MAX; i++) { g = blf_glyph_search(gc, i); if (!g) { FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); @@ -355,7 +355,7 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, /* NOTE: `blf_font_ensure_ascii_table(font, gc);` must be called before this macro. */ #define BLF_UTF8_NEXT_FAST(_font, _gc, _g, _str, _i, _c, _glyph_ascii_table) \ - if (((_c) = (_str)[_i]) < 0x80) { \ + if (((_c) = (_str)[_i]) < GLYPH_ASCII_TABLE_SIZE) { \ _g = (_glyph_ascii_table)[_c]; \ _i++; \ } \ @@ -370,11 +370,10 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, (void)0 #define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode) \ - const bool _has_kerning = FT_HAS_KERNING((_font)->face) != 0; \ - const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 : \ - (((_font)->flags & BLF_KERNING_DEFAULT) ? \ - ft_kerning_default : \ - (FT_UInt)FT_KERNING_UNFITTED) + const bool _has_kerning = FT_HAS_KERNING((_font)->face); \ + const FT_UInt _kern_mode = (_has_kerning && !((_font)->flags & BLF_KERNING_DEFAULT)) ? \ + FT_KERNING_UNFITTED : \ + FT_KERNING_DEFAULT; /* NOTE: `blf_font_ensure_ascii_kerning(font, gc, kern_mode);` must be called before this macro. */ @@ -382,7 +381,7 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, { \ if (_g_prev) { \ FT_Vector _delta; \ - if (_c_prev < 0x80 && _c < 0x80) { \ + if (_c_prev < KERNING_CACHE_TABLE_SIZE && _c < GLYPH_ASCII_TABLE_SIZE) { \ _pen_x += (_font)->kerning_cache->table[_c][_c_prev]; \ } \ else if (FT_Get_Kerning((_font)->face, (_g_prev)->idx, (_g)->idx, _kern_mode, &(_delta)) == \ @@ -482,7 +481,7 @@ static void blf_font_draw_ascii_ex( blf_batch_draw_begin(font); while ((c = *(str++)) && len--) { - BLI_assert(c < 128); + BLI_assert(c < GLYPH_ASCII_TABLE_SIZE); if ((g = glyph_ascii_table[c]) == NULL) { continue; } @@ -1262,7 +1261,7 @@ int blf_font_count_missing_chars(FontBLF *font, while (i < len) { unsigned int c; - if ((c = str[i]) < 0x80) { + if ((c = str[i]) < GLYPH_ASCII_TABLE_SIZE) { i++; } else if ((c = BLI_str_utf8_as_unicode_step(str, &i)) != BLI_UTF8_ERR) { diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index 3f01501fda4..35938a7d5c3 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -80,8 +80,8 @@ KerningCacheBLF *blf_kerning_cache_new(FontBLF *font, GlyphCacheBLF *gc) kc->mode = font->kerning_mode; unsigned int i, j; - for (i = 0; i < 0x80; i++) { - for (j = 0; j < 0x80; j++) { + for (i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) { + for (j = 0; j < KERNING_CACHE_TABLE_SIZE; j++) { GlyphBLF *g = blf_glyph_search(gc, i); if (!g) { FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); @@ -144,8 +144,6 @@ GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table)); memset(gc->bucket, 0, sizeof(gc->bucket)); - gc->glyphs_len_max = (int)font->face->num_glyphs; - gc->glyphs_len_free = (int)font->face->num_glyphs; gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f; gc->descender = ((float)font->face->size->metrics.descender) / 64.0f; @@ -514,7 +512,6 @@ void blf_glyph_render(FontBLF *font, GlyphCacheBLF *gc, GlyphBLF *g, float x, fl memcpy(&gc->bitmap_result[gc->bitmap_len], g->bitmap, (size_t)buff_size); gc->bitmap_len = bitmap_len; - gc->glyphs_len_free--; g->glyph_cache = gc; } diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 6816c97321d..1cd004ce77f 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -28,6 +28,16 @@ #define BLF_BATCH_DRAW_LEN_MAX 2048 /* in glyph */ +/* Number of characters in GlyphCacheBLF.glyph_ascii_table. */ +#define GLYPH_ASCII_TABLE_SIZE 128 + +/* First and last characters (inclusive) of range to cache within GLYPH_ASCII_TABLE_SIZE. */ +#define GLYPH_ASCII_CACHE_MIN 32 /* Space */ +#define GLYPH_ASCII_CACHE_MAX 126 /* Tilde */ + +/* Number of characters in KerningCacheBLF.table. */ +#define KERNING_CACHE_TABLE_SIZE 128 + typedef struct BatchBLF { struct FontBLF *font; /* can only batch glyph from the same font */ struct GPUBatch *batch; @@ -51,7 +61,7 @@ typedef struct KerningCacheBLF { /* only cache a ascii glyph pairs. Only store the x * offset we are interested in, instead of the full FT_Vector. */ - int table[0x80][0x80]; + int table[KERNING_CACHE_TABLE_SIZE][KERNING_CACHE_TABLE_SIZE]; } KerningCacheBLF; typedef struct GlyphCacheBLF { @@ -71,7 +81,7 @@ typedef struct GlyphCacheBLF { ListBase bucket[257]; /* fast ascii lookup */ - struct GlyphBLF *glyph_ascii_table[128]; + struct GlyphBLF *glyph_ascii_table[GLYPH_ASCII_TABLE_SIZE]; /* texture array, to draw the glyphs. */ GPUTexture *texture; @@ -84,12 +94,6 @@ typedef struct GlyphCacheBLF { int glyph_width_max; int glyph_height_max; - /* number of glyphs in the font. */ - int glyphs_len_max; - - /* number of glyphs not yet loaded (decreases every glyph loaded). */ - int glyphs_len_free; - /* ascender and descender value. */ float ascender; float descender; @@ -99,7 +103,7 @@ typedef struct GlyphBLF { struct GlyphBLF *next; struct GlyphBLF *prev; - /* and the character, as UTF8 */ + /* and the character, as UTF-32 */ unsigned int c; /* freetype2 index, to speed-up the search. */ -- cgit v1.2.3