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 | |
parent | bf88085763c15255c68bfd1ef300e22b8db48e4d (diff) |
Text: Cache glyph advances; added small monospace font
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/text.c | 22 | ||||
-rw-r--r-- | src/ui/text.h | 19 |
2 files changed, 23 insertions, 18 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], |
diff --git a/src/ui/text.h b/src/ui/text.h index ddf9da86..29fc34a4 100644 --- a/src/ui/text.h +++ b/src/ui/text.h | |||
@@ -8,6 +8,7 @@ | |||
8 | enum iFontId { | 8 | enum iFontId { |
9 | default_FontId, | 9 | default_FontId, |
10 | monospace_FontId, | 10 | monospace_FontId, |
11 | monospaceSmall_FontId, | ||
11 | medium_FontId, | 12 | medium_FontId, |
12 | italic_FontId, | 13 | italic_FontId, |
13 | bold_FontId, | 14 | bold_FontId, |
@@ -18,14 +19,15 @@ enum iFontId { | |||
18 | /* UI fonts: */ | 19 | /* UI fonts: */ |
19 | uiInput_FontId = monospace_FontId, | 20 | uiInput_FontId = monospace_FontId, |
20 | /* Document fonts: */ | 21 | /* Document fonts: */ |
21 | paragraph_FontId = default_FontId, | 22 | paragraph_FontId = default_FontId, |
22 | firstParagraph_FontId = medium_FontId, | 23 | firstParagraph_FontId = medium_FontId, |
23 | preformatted_FontId = monospace_FontId, | 24 | preformatted_FontId = monospace_FontId, |
24 | quote_FontId = italic_FontId, | 25 | preformattedSmall_FontId = monospaceSmall_FontId, |
25 | header1_FontId = hugeBold_FontId, | 26 | quote_FontId = italic_FontId, |
26 | header2_FontId = largeBold_FontId, | 27 | header1_FontId = hugeBold_FontId, |
27 | header3_FontId = mediumBold_FontId, | 28 | header2_FontId = largeBold_FontId, |
28 | uiShortcuts_FontId = default_FontId, | 29 | header3_FontId = mediumBold_FontId, |
30 | uiShortcuts_FontId = default_FontId, | ||
29 | }; | 31 | }; |
30 | 32 | ||
31 | #define specialSymbol_Text 0x10 | 33 | #define specialSymbol_Text 0x10 |
@@ -39,6 +41,7 @@ void deinit_Text (void); | |||
39 | 41 | ||
40 | int lineHeight_Text (int fontId); | 42 | int lineHeight_Text (int fontId); |
41 | iInt2 measure_Text (int fontId, const char *text); | 43 | iInt2 measure_Text (int fontId, const char *text); |
44 | iInt2 measureRange_Text (int fontId, iRangecc text); | ||
42 | iInt2 advance_Text (int fontId, const char *text); | 45 | iInt2 advance_Text (int fontId, const char *text); |
43 | iInt2 advanceN_Text (int fontId, const char *text, size_t n); /* `n` in characters */ | 46 | iInt2 advanceN_Text (int fontId, const char *text, size_t n); /* `n` in characters */ |
44 | iInt2 advanceRange_Text (int fontId, iRangecc text); | 47 | iInt2 advanceRange_Text (int fontId, iRangecc text); |