diff options
Diffstat (limited to 'src/ui/text.c')
-rw-r--r-- | src/ui/text.c | 77 |
1 files changed, 31 insertions, 46 deletions
diff --git a/src/ui/text.c b/src/ui/text.c index 8adfa019..14b4e305 100644 --- a/src/ui/text.c +++ b/src/ui/text.c | |||
@@ -171,7 +171,8 @@ iLocalDef iBool isMonospaced_Font(const iFont *d) { | |||
171 | static iFont *font_Text_(enum iFontId id); | 171 | static iFont *font_Text_(enum iFontId id); |
172 | 172 | ||
173 | static void init_Font(iFont *d, const iFontSpec *fontSpec, const iFontFile *fontFile, | 173 | static void init_Font(iFont *d, const iFontSpec *fontSpec, const iFontFile *fontFile, |
174 | enum iFontSize sizeId, int height) { | 174 | enum iFontSize sizeId, float height) { |
175 | const int scaleType = scaleType_FontSpec(sizeId); | ||
175 | d->fontSpec = fontSpec; | 176 | d->fontSpec = fontSpec; |
176 | d->fontFile = fontFile; | 177 | d->fontFile = fontFile; |
177 | /* TODO: Nunito kerning fixes need to be a font parameter of its own. */ | 178 | /* TODO: Nunito kerning fixes need to be a font parameter of its own. */ |
@@ -197,15 +198,9 @@ static void init_Font(iFont *d, const iFontSpec *fontSpec, const iFontFile *font | |||
197 | d->family = emojiAndSymbols_TextFont; | 198 | d->family = emojiAndSymbols_TextFont; |
198 | } | 199 | } |
199 | #endif | 200 | #endif |
200 | // d->isMonospaced = (fontSpec->flags & monospace_FontSpecFlag) != 0; | 201 | d->height = (int) (height * fontSpec->heightScale[scaleType]); |
201 | d->height = height; | 202 | const float glyphScale = fontSpec->glyphScale[scaleType]; |
202 | //iZap(d->font); | 203 | d->xScale = d->yScale = scaleForPixelHeight_FontFile(fontFile, d->height) * glyphScale; |
203 | // stbtt_InitFont(&d->font, constData_Block(data), 0); | ||
204 | // int ascent, descent, emAdv; | ||
205 | // stbtt_GetFontVMetrics(&d->font, &ascent, &descent, NULL); | ||
206 | // stbtt_GetCodepointHMetrics(&d->font, 'M', &emAdv, NULL); | ||
207 | const float scale = fontSpec->scaling; | ||
208 | d->xScale = d->yScale = scaleForPixelHeight_FontFile(fontFile, height) * scale; | ||
209 | if (isMonospaced_Font(d)) { | 204 | if (isMonospaced_Font(d)) { |
210 | /* It is important that monospaced fonts align 1:1 with the pixel grid so that | 205 | /* It is important that monospaced fonts align 1:1 with the pixel grid so that |
211 | box-drawing characters don't have partially occupied edge pixels, leading to seams | 206 | box-drawing characters don't have partially occupied edge pixels, leading to seams |
@@ -217,8 +212,9 @@ static void init_Font(iFont *d, const iFontSpec *fontSpec, const iFontFile *font | |||
217 | } | 212 | } |
218 | d->emAdvance = fontFile->emAdvance * d->xScale; | 213 | d->emAdvance = fontFile->emAdvance * d->xScale; |
219 | d->baseline = fontFile->ascent * d->yScale; | 214 | d->baseline = fontFile->ascent * d->yScale; |
220 | d->vertOffset = height * (1.0f - scale) / 2 * fontSpec->vertOffset; | 215 | d->vertOffset = d->height * (1.0f - glyphScale) / 2 * fontSpec->vertOffsetScale[scaleType]; |
221 | d->table = NULL; | 216 | d->table = NULL; |
217 | // printf("{%s} height:%d baseline:%d\n", cstr_String(&d->fontSpec->id), d->height, d->baseline); | ||
222 | } | 218 | } |
223 | 219 | ||
224 | static void deinit_Font(iFont *d) { | 220 | static void deinit_Font(iFont *d) { |
@@ -252,8 +248,8 @@ struct Impl_CacheRow { | |||
252 | }; | 248 | }; |
253 | 249 | ||
254 | struct Impl_Text { | 250 | struct Impl_Text { |
255 | enum iTextFont contentFont; | 251 | // enum iTextFont contentFont; |
256 | enum iTextFont headingFont; | 252 | // enum iTextFont headingFont; |
257 | float contentFontSize; | 253 | float contentFontSize; |
258 | iArray fonts; /* fonts currently selected for use (incl. all styles/sizes) */ | 254 | iArray fonts; /* fonts currently selected for use (incl. all styles/sizes) */ |
259 | int overrideFontId; /* always checked for glyphs first, regardless of which font is used */ | 255 | int overrideFontId; /* always checked for glyphs first, regardless of which font is used */ |
@@ -290,7 +286,8 @@ static void setupFontVariants_Text_(iText *d, const iFontSpec *spec, int baseId) | |||
290 | spec, | 286 | spec, |
291 | spec->styles[style], | 287 | spec->styles[style], |
292 | sizeId, | 288 | sizeId, |
293 | (sizeId < contentRegular_FontSize ? uiSize : textSize) * scale_FontSize(sizeId)); | 289 | (sizeId < contentRegular_FontSize ? uiSize : textSize) * |
290 | scale_FontSize(sizeId)); | ||
294 | } | 291 | } |
295 | } | 292 | } |
296 | } | 293 | } |
@@ -311,6 +308,11 @@ iLocalDef enum iFontStyle styleId_Text_(const iFont *d) { | |||
311 | return (fontId_Text_(d) / max_FontSize) % max_FontStyle; | 308 | return (fontId_Text_(d) / max_FontSize) % max_FontStyle; |
312 | } | 309 | } |
313 | 310 | ||
311 | static const iFontSpec *tryFindSpec_(enum iPrefsString ps, const char *fallback) { | ||
312 | const iFontSpec *spec = findSpec_Fonts(cstr_String(&prefs_App()->strings[ps])); | ||
313 | return spec ? spec : findSpec_Fonts(fallback); | ||
314 | } | ||
315 | |||
314 | static void initFonts_Text_(iText *d) { | 316 | static void initFonts_Text_(iText *d) { |
315 | #if 0 | 317 | #if 0 |
316 | const iBlock *regularFont = &fontNunitoRegular_Embedded; | 318 | const iBlock *regularFont = &fontNunitoRegular_Embedded; |
@@ -445,11 +447,11 @@ static void initFonts_Text_(iText *d) { | |||
445 | /* First the mandatory fonts. */ | 447 | /* First the mandatory fonts. */ |
446 | d->overrideFontId = -1; | 448 | d->overrideFontId = -1; |
447 | resize_Array(&d->fonts, auxiliary_FontId); /* room for the built-ins */ | 449 | resize_Array(&d->fonts, auxiliary_FontId); /* room for the built-ins */ |
448 | iAssert(auxiliary_FontId == documentHeading_FontId + maxVariants_Fonts); | 450 | setupFontVariants_Text_(d, tryFindSpec_(uiFont_PrefsString, "default"), default_FontId); |
449 | setupFontVariants_Text_(d, findSpec_Fonts("default"), default_FontId); | 451 | setupFontVariants_Text_(d, tryFindSpec_(monospaceFont_PrefsString, "iosevka"), monospace_FontId); |
450 | setupFontVariants_Text_(d, findSpec_Fonts("iosevka"), monospace_FontId); | 452 | setupFontVariants_Text_(d, tryFindSpec_(headingFont_PrefsString, "default"), documentHeading_FontId); |
451 | setupFontVariants_Text_(d, findSpec_Fonts("default"), documentBody_FontId); | 453 | setupFontVariants_Text_(d, tryFindSpec_(bodyFont_PrefsString, "default"), documentBody_FontId); |
452 | setupFontVariants_Text_(d, findSpec_Fonts("default"), documentHeading_FontId); | 454 | setupFontVariants_Text_(d, tryFindSpec_(monospaceDocumentFont_PrefsString, "iosevka-body"), documentMonospace_FontId); |
453 | /* Check if there are auxiliary fonts available and set those up, too. */ | 455 | /* Check if there are auxiliary fonts available and set those up, too. */ |
454 | iConstForEach(PtrArray, s, listSpecsByPriority_Fonts()) { | 456 | iConstForEach(PtrArray, s, listSpecsByPriority_Fonts()) { |
455 | const iFontSpec *spec = s.ptr; | 457 | const iFontSpec *spec = s.ptr; |
@@ -459,17 +461,6 @@ static void initFonts_Text_(iText *d) { | |||
459 | setupFontVariants_Text_(d, spec, fontId); | 461 | setupFontVariants_Text_(d, spec, fontId); |
460 | } | 462 | } |
461 | } | 463 | } |
462 | #if 0 | ||
463 | iForIndices(i, fontData) { | ||
464 | iFont *font = font_Text_(i); | ||
465 | init_Font(font, | ||
466 | fontData[i].ttf, | ||
467 | fontData[i].size, | ||
468 | fontData[i].scaling, | ||
469 | fontData[i].sizeId, | ||
470 | fontData[i].ttf == &fontIosevkaTermExtended_Embedded); | ||
471 | } | ||
472 | #endif | ||
473 | gap_Text = iRound(gap_UI * d->contentFontSize); | 464 | gap_Text = iRound(gap_UI * d->contentFontSize); |
474 | } | 465 | } |
475 | 466 | ||
@@ -544,8 +535,8 @@ void init_Text(iText *d, SDL_Renderer *render) { | |||
544 | iText *oldActive = activeText_; | 535 | iText *oldActive = activeText_; |
545 | activeText_ = d; | 536 | activeText_ = d; |
546 | init_Array(&d->fonts, sizeof(iFont)); | 537 | init_Array(&d->fonts, sizeof(iFont)); |
547 | d->contentFont = nunito_TextFont; | 538 | // d->contentFont = nunito_TextFont; |
548 | d->headingFont = nunito_TextFont; | 539 | // d->headingFont = nunito_TextFont; |
549 | d->contentFontSize = contentScale_Text_; | 540 | d->contentFontSize = contentScale_Text_; |
550 | d->ansiEscape = new_RegExp("[[()]([0-9;AB]*)m", 0); | 541 | d->ansiEscape = new_RegExp("[[()]([0-9;AB]*)m", 0); |
551 | d->render = render; | 542 | d->render = render; |
@@ -579,21 +570,15 @@ void setOpacity_Text(float opacity) { | |||
579 | SDL_SetTextureAlphaMod(activeText_->cache, iClamp(opacity, 0.0f, 1.0f) * 255 + 0.5f); | 570 | SDL_SetTextureAlphaMod(activeText_->cache, iClamp(opacity, 0.0f, 1.0f) * 255 + 0.5f); |
580 | } | 571 | } |
581 | 572 | ||
582 | void setContentFont_Text(iText *d, enum iTextFont font) { | 573 | //void setFont_Text(iText *d, int fontId, const char *fontSpecId) { |
583 | if (d->contentFont != font) { | 574 | // setupFontVariants_Text_(d, findSpec_Fonts(fontSpecId), fontId); |
584 | d->contentFont = font; | 575 | // if (d->contentFont != font) { |
585 | resetFonts_Text(d); | 576 | // d->contentFont = font; |
586 | } | 577 | // resetFonts_Text(d); |
587 | } | 578 | // } |
588 | 579 | //} | |
589 | void setHeadingFont_Text(iText *d, enum iTextFont font) { | ||
590 | if (d->headingFont != font) { | ||
591 | d->headingFont = font; | ||
592 | resetFonts_Text(d); | ||
593 | } | ||
594 | } | ||
595 | 580 | ||
596 | void setContentFontSize_Text(iText *d, float fontSizeFactor) { | 581 | void setDocumentFontSize_Text(iText *d, float fontSizeFactor) { |
597 | fontSizeFactor *= contentScale_Text_; | 582 | fontSizeFactor *= contentScale_Text_; |
598 | iAssert(fontSizeFactor > 0); | 583 | iAssert(fontSizeFactor > 0); |
599 | if (iAbs(d->contentFontSize - fontSizeFactor) > 0.001f) { | 584 | if (iAbs(d->contentFontSize - fontSizeFactor) > 0.001f) { |