diff options
Diffstat (limited to 'src/ui/text.c')
-rw-r--r-- | src/ui/text.c | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/ui/text.c b/src/ui/text.c index 2ab40387..2b3e187d 100644 --- a/src/ui/text.c +++ b/src/ui/text.c | |||
@@ -130,10 +130,15 @@ static void init_Font(iFont *d, const iBlock *data, int height, float scale, | |||
130 | memset(d->indexTable, 0xff, sizeof(d->indexTable)); | 130 | memset(d->indexTable, 0xff, sizeof(d->indexTable)); |
131 | } | 131 | } |
132 | 132 | ||
133 | static void deinit_Font(iFont *d) { | 133 | static void clearGlyphs_Font_(iFont *d) { |
134 | iForEach(Hash, i, &d->glyphs) { | 134 | iForEach(Hash, i, &d->glyphs) { |
135 | delete_Glyph((iGlyph *) i.value); | 135 | delete_Glyph((iGlyph *) i.value); |
136 | } | 136 | } |
137 | clear_Hash(&d->glyphs); | ||
138 | } | ||
139 | |||
140 | static void deinit_Font(iFont *d) { | ||
141 | clearGlyphs_Font_(d); | ||
137 | deinit_Hash(&d->glyphs); | 142 | deinit_Hash(&d->glyphs); |
138 | delete_Block(d->data); | 143 | delete_Block(d->data); |
139 | } | 144 | } |
@@ -149,6 +154,8 @@ static uint32_t glyphIndex_Font_(iFont *d, iChar ch) { | |||
149 | return stbtt_FindGlyphIndex(&d->font, ch); | 154 | return stbtt_FindGlyphIndex(&d->font, ch); |
150 | } | 155 | } |
151 | 156 | ||
157 | /*----------------------------------------------------------------------------------------------*/ | ||
158 | |||
152 | iDeclareType(Text) | 159 | iDeclareType(Text) |
153 | iDeclareType(CacheRow) | 160 | iDeclareType(CacheRow) |
154 | 161 | ||
@@ -224,7 +231,9 @@ static void initFonts_Text_(iText *d) { | |||
224 | int symbolsFont; | 231 | int symbolsFont; |
225 | } fontData[max_FontId] = { | 232 | } fontData[max_FontId] = { |
226 | { &fontSourceSansProRegular_Embedded, fontSize_UI, 1.0f, defaultSymbols_FontId }, | 233 | { &fontSourceSansProRegular_Embedded, fontSize_UI, 1.0f, defaultSymbols_FontId }, |
234 | { &fontSourceSansProBold_Embedded, fontSize_UI, 1.0f, defaultSymbols_FontId }, | ||
227 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.125f, 1.0f, defaultMediumSymbols_FontId }, | 235 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.125f, 1.0f, defaultMediumSymbols_FontId }, |
236 | { &fontSourceSansProBold_Embedded, fontSize_UI * 1.125f, 1.0f, defaultMediumSymbols_FontId }, | ||
228 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.666f, 1.0f, defaultLargeSymbols_FontId }, | 237 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.666f, 1.0f, defaultLargeSymbols_FontId }, |
229 | { &fontIosevkaTermExtended_Embedded, fontSize_UI * 0.866f, 1.0f, defaultSymbols_FontId }, | 238 | { &fontIosevkaTermExtended_Embedded, fontSize_UI * 0.866f, 1.0f, defaultSymbols_FontId }, |
230 | { &fontSourceSansProRegular_Embedded, textSize, scaling, symbols_FontId }, | 239 | { &fontSourceSansProRegular_Embedded, textSize, scaling, symbols_FontId }, |
@@ -330,11 +339,15 @@ static void deinitFonts_Text_(iText *d) { | |||
330 | } | 339 | } |
331 | } | 340 | } |
332 | 341 | ||
342 | static int maxGlyphHeight_Text_(const iText *d) { | ||
343 | return 2 * d->contentFontSize * fontSize_UI; | ||
344 | } | ||
345 | |||
333 | static void initCache_Text_(iText *d) { | 346 | static void initCache_Text_(iText *d) { |
334 | init_Array(&d->cacheRows, sizeof(iCacheRow)); | 347 | init_Array(&d->cacheRows, sizeof(iCacheRow)); |
335 | const int textSize = d->contentFontSize * fontSize_UI; | 348 | const int textSize = d->contentFontSize * fontSize_UI; |
336 | iAssert(textSize > 0); | 349 | iAssert(textSize > 0); |
337 | const iInt2 cacheDims = init_I2(16, 80); | 350 | const iInt2 cacheDims = init_I2(16, 40); |
338 | d->cacheSize = mul_I2(cacheDims, init1_I2(iMax(textSize, fontSize_UI))); | 351 | d->cacheSize = mul_I2(cacheDims, init1_I2(iMax(textSize, fontSize_UI))); |
339 | SDL_RendererInfo renderInfo; | 352 | SDL_RendererInfo renderInfo; |
340 | SDL_GetRendererInfo(d->render, &renderInfo); | 353 | SDL_GetRendererInfo(d->render, &renderInfo); |
@@ -417,6 +430,14 @@ void setContentFontSize_Text(float fontSizeFactor) { | |||
417 | } | 430 | } |
418 | } | 431 | } |
419 | 432 | ||
433 | static void resetCache_Text_(iText *d) { | ||
434 | deinitCache_Text_(d); | ||
435 | for (int i = 0; i < max_FontId; i++) { | ||
436 | clearGlyphs_Font_(&d->fonts[i]); | ||
437 | } | ||
438 | initCache_Text_(d); | ||
439 | } | ||
440 | |||
420 | void resetFonts_Text(void) { | 441 | void resetFonts_Text(void) { |
421 | iText *d = &text_; | 442 | iText *d = &text_; |
422 | deinitFonts_Text_(d); | 443 | deinitFonts_Text_(d); |
@@ -572,6 +593,13 @@ static const iGlyph *glyph_Font_(iFont *d, iChar ch) { | |||
572 | iGlyph *glyph = new_Glyph(ch); | 593 | iGlyph *glyph = new_Glyph(ch); |
573 | glyph->glyphIndex = glyphIndex; | 594 | glyph->glyphIndex = glyphIndex; |
574 | glyph->font = font; | 595 | glyph->font = font; |
596 | /* If the cache is running out of space, clear it and we'll recache what's needed currently. */ | ||
597 | if (text_.cacheBottom > text_.cacheSize.y - maxGlyphHeight_Text_(&text_)) { | ||
598 | #if !defined (NDEBUG) | ||
599 | printf("[Text] glyph cache is full, clearing!\n"); fflush(stdout); | ||
600 | #endif | ||
601 | resetCache_Text_(&text_); | ||
602 | } | ||
575 | SDL_Texture *oldTarget = SDL_GetRenderTarget(text_.render); | 603 | SDL_Texture *oldTarget = SDL_GetRenderTarget(text_.render); |
576 | SDL_SetRenderTarget(text_.render, text_.cache); | 604 | SDL_SetRenderTarget(text_.render, text_.cache); |
577 | cache_Font_(font, glyph, 0); | 605 | cache_Font_(font, glyph, 0); |