diff options
Diffstat (limited to 'src/ui/text.c')
-rw-r--r-- | src/ui/text.c | 80 |
1 files changed, 61 insertions, 19 deletions
diff --git a/src/ui/text.c b/src/ui/text.c index b2110bb6..23524808 100644 --- a/src/ui/text.c +++ b/src/ui/text.c | |||
@@ -142,16 +142,17 @@ struct Impl_CacheRow { | |||
142 | }; | 142 | }; |
143 | 143 | ||
144 | struct Impl_Text { | 144 | struct Impl_Text { |
145 | float contentFontSize; | 145 | enum iTextFont contentFont; |
146 | iFont fonts[max_FontId]; | 146 | float contentFontSize; |
147 | SDL_Renderer *render; | 147 | iFont fonts[max_FontId]; |
148 | SDL_Texture * cache; | 148 | SDL_Renderer * render; |
149 | iInt2 cacheSize; | 149 | SDL_Texture * cache; |
150 | int cacheRowAllocStep; | 150 | iInt2 cacheSize; |
151 | int cacheBottom; | 151 | int cacheRowAllocStep; |
152 | iArray cacheRows; | 152 | int cacheBottom; |
153 | SDL_Palette * grayscale; | 153 | iArray cacheRows; |
154 | iRegExp * ansiEscape; | 154 | SDL_Palette * grayscale; |
155 | iRegExp * ansiEscape; | ||
155 | }; | 156 | }; |
156 | 157 | ||
157 | static iText text_; | 158 | static iText text_; |
@@ -159,6 +160,16 @@ static iText text_; | |||
159 | static void initFonts_Text_(iText *d) { | 160 | static void initFonts_Text_(iText *d) { |
160 | const float textSize = fontSize_UI * d->contentFontSize; | 161 | const float textSize = fontSize_UI * d->contentFontSize; |
161 | const float monoSize = fontSize_UI * d->contentFontSize / contentScale_Text_ * 0.866f; | 162 | const float monoSize = fontSize_UI * d->contentFontSize / contentScale_Text_ * 0.866f; |
163 | const iBlock *regularFont = &fontNunitoRegular_Embedded; | ||
164 | const iBlock *italicFont = &fontNunitoLightItalic_Embedded; | ||
165 | const iBlock *boldFont = &fontNunitoExtraBold_Embedded; | ||
166 | const iBlock *lightFont = &fontNunitoExtraLight_Embedded; | ||
167 | if (d->contentFont == firaSans_TextFont) { | ||
168 | regularFont = &fontFiraSansRegular_Embedded; | ||
169 | italicFont = &fontFiraSansItalic_Embedded; | ||
170 | boldFont = &fontFiraSansBold_Embedded; | ||
171 | lightFont = &fontFiraSansLight_Embedded; | ||
172 | } | ||
162 | const struct { | 173 | const struct { |
163 | const iBlock *ttf; | 174 | const iBlock *ttf; |
164 | int size; | 175 | int size; |
@@ -168,17 +179,17 @@ static void initFonts_Text_(iText *d) { | |||
168 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.125f, defaultMediumSymbols_FontId }, | 179 | { &fontSourceSansProRegular_Embedded, fontSize_UI * 1.125f, defaultMediumSymbols_FontId }, |
169 | { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.866f, defaultSymbols_FontId }, | 180 | { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.866f, defaultSymbols_FontId }, |
170 | /* content fonts */ | 181 | /* content fonts */ |
171 | { &fontNunitoRegular_Embedded, textSize, symbols_FontId }, | 182 | { regularFont, textSize, symbols_FontId }, |
172 | { &fontFiraMonoRegular_Embedded, monoSize, monospaceSymbols_FontId }, | 183 | { &fontFiraMonoRegular_Embedded, monoSize, monospaceSymbols_FontId }, |
173 | { &fontFiraMonoRegular_Embedded, monoSize * 0.750f, monospaceSmallSymbols_FontId }, | 184 | { &fontFiraMonoRegular_Embedded, monoSize * 0.750f, monospaceSmallSymbols_FontId }, |
174 | { &fontNunitoRegular_Embedded, textSize * 1.200f, mediumSymbols_FontId }, | 185 | { regularFont, textSize * 1.200f, mediumSymbols_FontId }, |
175 | { &fontNunitoRegular_Embedded, textSize * 1.333f, bigSymbols_FontId }, | 186 | { regularFont, textSize * 1.333f, bigSymbols_FontId }, |
176 | { &fontNunitoLightItalic_Embedded, textSize, symbols_FontId }, | 187 | { italicFont, textSize, symbols_FontId }, |
177 | { &fontNunitoExtraBold_Embedded, textSize, symbols_FontId }, | 188 | { boldFont, textSize, symbols_FontId }, |
178 | { &fontNunitoExtraBold_Embedded, textSize * 1.333f, mediumSymbols_FontId }, | 189 | { boldFont, textSize * 1.333f, mediumSymbols_FontId }, |
179 | { &fontNunitoExtraBold_Embedded, textSize * 1.666f, largeSymbols_FontId }, | 190 | { boldFont, textSize * 1.666f, largeSymbols_FontId }, |
180 | { &fontNunitoExtraBold_Embedded, textSize * 2.000f, hugeSymbols_FontId }, | 191 | { boldFont, textSize * 2.000f, hugeSymbols_FontId }, |
181 | { &fontNunitoExtraLight_Embedded, textSize * 1.666f, largeSymbols_FontId }, | 192 | { lightFont, textSize * 1.666f, largeSymbols_FontId }, |
182 | /* symbol fonts */ | 193 | /* symbol fonts */ |
183 | { &fontSymbola_Embedded, fontSize_UI, defaultSymbols_FontId }, | 194 | { &fontSymbola_Embedded, fontSize_UI, defaultSymbols_FontId }, |
184 | { &fontSymbola_Embedded, fontSize_UI * 1.125f, defaultMediumSymbols_FontId }, | 195 | { &fontSymbola_Embedded, fontSize_UI * 1.125f, defaultMediumSymbols_FontId }, |
@@ -278,6 +289,7 @@ static void deinitCache_Text_(iText *d) { | |||
278 | 289 | ||
279 | void init_Text(SDL_Renderer *render) { | 290 | void init_Text(SDL_Renderer *render) { |
280 | iText *d = &text_; | 291 | iText *d = &text_; |
292 | d->contentFont = nunito_TextFont; | ||
281 | d->contentFontSize = contentScale_Text_; | 293 | d->contentFontSize = contentScale_Text_; |
282 | d->ansiEscape = new_RegExp("\\[([0-9;]+)m", 0); | 294 | d->ansiEscape = new_RegExp("\\[([0-9;]+)m", 0); |
283 | d->render = render; | 295 | d->render = render; |
@@ -306,6 +318,13 @@ void setOpacity_Text(float opacity) { | |||
306 | SDL_SetTextureAlphaMod(text_.cache, iClamp(opacity, 0.0f, 1.0f) * 255 + 0.5f); | 318 | SDL_SetTextureAlphaMod(text_.cache, iClamp(opacity, 0.0f, 1.0f) * 255 + 0.5f); |
307 | } | 319 | } |
308 | 320 | ||
321 | void setContentFont_Text(enum iTextFont font) { | ||
322 | if (text_.contentFont != font) { | ||
323 | text_.contentFont = font; | ||
324 | resetFonts_Text(); | ||
325 | } | ||
326 | } | ||
327 | |||
309 | void setContentFontSize_Text(float fontSizeFactor) { | 328 | void setContentFontSize_Text(float fontSizeFactor) { |
310 | fontSizeFactor *= contentScale_Text_; | 329 | fontSizeFactor *= contentScale_Text_; |
311 | iAssert(fontSizeFactor > 0); | 330 | iAssert(fontSizeFactor > 0); |
@@ -766,6 +785,29 @@ void drawRange_Text(int fontId, iInt2 pos, int color, iRangecc text) { | |||
766 | draw_Text_(fontId, pos, color, text); | 785 | draw_Text_(fontId, pos, color, text); |
767 | } | 786 | } |
768 | 787 | ||
788 | iInt2 advanceWrapRange_Text(int fontId, int maxWidth, iRangecc text) { | ||
789 | iInt2 size = zero_I2(); | ||
790 | const char *endp; | ||
791 | while (!isEmpty_Range(&text)) { | ||
792 | iInt2 line = tryAdvance_Text(fontId, text, maxWidth, &endp); | ||
793 | text.start = endp; | ||
794 | size.x = iMax(size.x, line.x); | ||
795 | size.y += line.y; | ||
796 | } | ||
797 | return size; | ||
798 | } | ||
799 | |||
800 | int drawWrapRange_Text(int fontId, iInt2 pos, int maxWidth, int color, iRangecc text) { | ||
801 | const char *endp; | ||
802 | while (!isEmpty_Range(&text)) { | ||
803 | tryAdvance_Text(fontId, text, maxWidth, &endp); | ||
804 | drawRange_Text(fontId, pos, color, (iRangecc){ text.start, endp }); | ||
805 | text.start = endp; | ||
806 | pos.y += lineHeight_Text(fontId); | ||
807 | } | ||
808 | return pos.y; | ||
809 | } | ||
810 | |||
769 | void drawCentered_Text(int fontId, iRect rect, iBool alignVisual, int color, const char *format, ...) { | 811 | void drawCentered_Text(int fontId, iRect rect, iBool alignVisual, int color, const char *format, ...) { |
770 | iBlock chars; | 812 | iBlock chars; |
771 | init_Block(&chars, 0); { | 813 | init_Block(&chars, 0); { |