diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-22 21:13:47 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-22 21:13:47 +0300 |
commit | 704864566e61a7472c4b10d75601546e143e6a2e (patch) | |
tree | 23267f4a4fea187d4baba6e53d5bf4fe7e2331d7 /src/ui/text.c | |
parent | bf88085763c15255c68bfd1ef300e22b8db48e4d (diff) |
Text: Cache glyph advances; added small monospace font
Diffstat (limited to 'src/ui/text.c')
-rw-r--r-- | src/ui/text.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/ui/text.c b/src/ui/text.c index a24d4030..6ed95dc0 100644 --- a/src/ui/text.c +++ b/src/ui/text.c | |||
@@ -117,6 +117,7 @@ void init_Text(SDL_Renderer *render) { | |||
117 | const struct { const iBlock *ttf; int size; } fontData[max_FontId] = { | 117 | const struct { const iBlock *ttf; int size; } fontData[max_FontId] = { |
118 | { &fontFiraSansRegular_Embedded, fontSize_UI }, | 118 | { &fontFiraSansRegular_Embedded, fontSize_UI }, |
119 | { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.85f }, | 119 | { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.85f }, |
120 | { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.65f }, | ||
120 | { &fontFiraSansRegular_Embedded, fontSize_UI * 1.35f }, | 121 | { &fontFiraSansRegular_Embedded, fontSize_UI * 1.35f }, |
121 | { &fontFiraSansLightItalic_Embedded, fontSize_UI }, | 122 | { &fontFiraSansLightItalic_Embedded, fontSize_UI }, |
122 | { &fontFiraSansBold_Embedded, fontSize_UI }, | 123 | { &fontFiraSansBold_Embedded, fontSize_UI }, |
@@ -213,6 +214,8 @@ static void cache_Font_(iFont *d, iGlyph *glyph) { | |||
213 | if (!isSpecialChar_(ch)) { | 214 | if (!isSpecialChar_(ch)) { |
214 | /* Rasterize the glyph using stbtt. */ | 215 | /* Rasterize the glyph using stbtt. */ |
215 | surface = rasterizeGlyph_Font_(d, ch); | 216 | surface = rasterizeGlyph_Font_(d, ch); |
217 | int lsb; | ||
218 | stbtt_GetCodepointHMetrics(&d->font, ch, &glyph->advance, &lsb); | ||
216 | stbtt_GetCodepointBitmapBox( | 219 | stbtt_GetCodepointBitmapBox( |
217 | &d->font, ch, d->scale, d->scale, &glyph->dx, &glyph->dy, NULL, NULL); | 220 | &d->font, ch, d->scale, d->scale, &glyph->dx, &glyph->dy, NULL, NULL); |
218 | fromStb = iTrue; | 221 | fromStb = iTrue; |
@@ -226,6 +229,7 @@ static void cache_Font_(iFont *d, iGlyph *glyph) { | |||
226 | stbtt_GetCodepointHMetrics(&d->font, 'M', &em, &lsb); | 229 | stbtt_GetCodepointHMetrics(&d->font, 'M', &em, &lsb); |
227 | glyph->dx = d->baseline / 10; | 230 | glyph->dx = d->baseline / 10; |
228 | glyph->dy = -d->baseline; | 231 | glyph->dy = -d->baseline; |
232 | glyph->advance = em * symbolAdvance_(symbol); | ||
229 | glyph->rect.size = init_I2(symbolEmWidth_(symbol) * em * d->scale, d->height); | 233 | glyph->rect.size = init_I2(symbolEmWidth_(symbol) * em * d->scale, d->height); |
230 | #if 0 | 234 | #if 0 |
231 | if (isRasterizedSymbol_(ch)) { | 235 | if (isRasterizedSymbol_(ch)) { |
@@ -427,13 +431,7 @@ static iInt2 run_Font_(iFont *d, enum iRunMode mode, iRangecc text, size_t maxLe | |||
427 | glyph->rect.size.y }; | 431 | glyph->rect.size.y }; |
428 | SDL_RenderCopyF(text_.render, text_.cache, (const SDL_Rect *) &glyph->rect, &dst); | 432 | SDL_RenderCopyF(text_.render, text_.cache, (const SDL_Rect *) &glyph->rect, &dst); |
429 | } | 433 | } |
430 | int advance, lsb; | 434 | xpos += d->scale * glyph->advance; |
431 | const iBool spec = isSpecialChar_(ch); | ||
432 | stbtt_GetCodepointHMetrics(info, spec ? 'M' : ch, &advance, &lsb); | ||
433 | if (spec) { | ||
434 | advance *= symbolAdvance_(specialChar_(ch)); | ||
435 | } | ||
436 | xpos += d->scale * advance; | ||
437 | xposMax = iMax(xposMax, xpos); | 435 | xposMax = iMax(xposMax, xpos); |
438 | if (!isSpace_Char(prevCh) && isSpace_Char(ch)) { | 436 | if (!isSpace_Char(prevCh) && isSpace_Char(ch)) { |
439 | lastWordEnd = chPos; | 437 | lastWordEnd = chPos; |
@@ -461,13 +459,13 @@ int lineHeight_Text(int fontId) { | |||
461 | return text_.fonts[fontId].height; | 459 | return text_.fonts[fontId].height; |
462 | } | 460 | } |
463 | 461 | ||
464 | iInt2 measure_Text(int fontId, const char *text) { | 462 | iInt2 measureRange_Text(int fontId, iRangecc text) { |
465 | if (!*text) { | 463 | if (isEmpty_Range(&text)) { |
466 | return init_I2(0, lineHeight_Text(fontId)); | 464 | return init_I2(0, lineHeight_Text(fontId)); |
467 | } | 465 | } |
468 | return run_Font_(&text_.fonts[fontId], | 466 | return run_Font_(&text_.fonts[fontId], |
469 | measure_RunMode, | 467 | measure_RunMode, |
470 | range_CStr(text), | 468 | text, |
471 | iInvalidSize, | 469 | iInvalidSize, |
472 | zero_I2(), | 470 | zero_I2(), |
473 | 0, | 471 | 0, |
@@ -475,6 +473,10 @@ iInt2 measure_Text(int fontId, const char *text) { | |||
475 | NULL); | 473 | NULL); |
476 | } | 474 | } |
477 | 475 | ||
476 | iInt2 measure_Text(int fontId, const char *text) { | ||
477 | return measureRange_Text(fontId, range_CStr(text)); | ||
478 | } | ||
479 | |||
478 | iInt2 advanceRange_Text(int fontId, iRangecc text) { | 480 | iInt2 advanceRange_Text(int fontId, iRangecc text) { |
479 | int advance; | 481 | int advance; |
480 | const int height = run_Font_(&text_.fonts[fontId], | 482 | const int height = run_Font_(&text_.fonts[fontId], |