summaryrefslogtreecommitdiff
path: root/src/ui/text.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-07-24 21:05:02 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-07-24 21:05:02 +0300
commite32fd4e7796d6e7c5a4803e45bc230378ec4dd0b (patch)
tree5232331654a977cf4ad1bf06eedc2f8752db6c32 /src/ui/text.c
parent0f0b4250ca460d58edb61cc0dd509ba1980c3272 (diff)
Font update; ANSI color escapes; fixed URL update
Newer version of the Fira fonts, and added a separate UI font (Source Sans Pro). The text renderer checks for the 4-bit ANSI color escapes for the setting the foreground color. InputWidget supports paste from clipboard. The navbar updates the current URL when the page has been loaded.
Diffstat (limited to 'src/ui/text.c')
-rw-r--r--src/ui/text.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/ui/text.c b/src/ui/text.c
index 27c41396..f1c2c947 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -11,6 +11,7 @@
11#include <the_Foundation/file.h> 11#include <the_Foundation/file.h>
12#include <the_Foundation/hash.h> 12#include <the_Foundation/hash.h>
13#include <the_Foundation/math.h> 13#include <the_Foundation/math.h>
14#include <the_Foundation/regexp.h>
14#include <the_Foundation/path.h> 15#include <the_Foundation/path.h>
15#include <the_Foundation/vec2.h> 16#include <the_Foundation/vec2.h>
16 17
@@ -89,12 +90,14 @@ struct Impl_Text {
89 iInt2 cachePos; 90 iInt2 cachePos;
90 int cacheRowHeight; 91 int cacheRowHeight;
91 SDL_Palette * grayscale; 92 SDL_Palette * grayscale;
93 iRegExp * ansiEscape;
92}; 94};
93 95
94static iText text_; 96static iText text_;
95 97
96void init_Text(SDL_Renderer *render) { 98void init_Text(SDL_Renderer *render) {
97 iText *d = &text_; 99 iText *d = &text_;
100 d->ansiEscape = new_RegExp("\\[([0-9;]+)m", 0);
98 d->render = render; 101 d->render = render;
99 /* A grayscale palette for rasterized glyphs. */ { 102 /* A grayscale palette for rasterized glyphs. */ {
100 SDL_Color colors[256]; 103 SDL_Color colors[256];
@@ -117,6 +120,7 @@ void init_Text(SDL_Renderer *render) {
117 } 120 }
118 /* Load the fonts. */ { 121 /* Load the fonts. */ {
119 const struct { const iBlock *ttf; int size; } fontData[max_FontId] = { 122 const struct { const iBlock *ttf; int size; } fontData[max_FontId] = {
123 { &fontSourceSansProRegular_Embedded, fontSize_UI },
120 { &fontFiraSansRegular_Embedded, fontSize_UI }, 124 { &fontFiraSansRegular_Embedded, fontSize_UI },
121 { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.866f }, 125 { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.866f },
122 { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.666f }, 126 { &fontFiraMonoRegular_Embedded, fontSize_UI * 0.666f },
@@ -141,6 +145,7 @@ void deinit_Text(void) {
141 } 145 }
142 SDL_DestroyTexture(d->cache); 146 SDL_DestroyTexture(d->cache);
143 d->render = NULL; 147 d->render = NULL;
148 iRelease(d->ansiEscape);
144} 149}
145 150
146static SDL_Surface *rasterizeGlyph_Font_(const iFont *d, iChar ch, float xShift) { 151static SDL_Surface *rasterizeGlyph_Font_(const iFont *d, iChar ch, float xShift) {
@@ -414,6 +419,20 @@ static iInt2 run_Font_(iFont *d, enum iRunMode mode, iRangecc text, size_t maxLe
414 iChar prevCh = 0; 419 iChar prevCh = 0;
415 for (const char *chPos = text.start; chPos != text.end; ) { 420 for (const char *chPos = text.start; chPos != text.end; ) {
416 iAssert(chPos < text.end); 421 iAssert(chPos < text.end);
422 if (*chPos == 0x1b) {
423 /* ANSI escape. */
424 chPos++;
425 iRegExpMatch m;
426 if (match_RegExp(text_.ansiEscape, chPos, text.end - chPos, &m)) {
427 if (mode == draw_RunMode) {
428 /* Change the color. */
429 const iColor clr = ansi_Color(capturedRange_RegExpMatch(&m, 1), gray75_ColorId);
430 SDL_SetTextureColorMod(text_.cache, clr.r, clr.g, clr.b);
431 }
432 chPos = end_RegExpMatch(&m);
433 continue;
434 }
435 }
417 iChar ch = nextChar_(&chPos, text.end); 436 iChar ch = nextChar_(&chPos, text.end);
418 /* Special instructions. */ { 437 /* Special instructions. */ {
419 if (ch == '\n') { 438 if (ch == '\n') {
@@ -424,8 +443,8 @@ static iInt2 run_Font_(iFont *d, enum iRunMode mode, iRangecc text, size_t maxLe
424 } 443 }
425 if (ch == '\r') { 444 if (ch == '\r') {
426 const iChar esc = nextChar_(&chPos, text.end); 445 const iChar esc = nextChar_(&chPos, text.end);
427 const iColor clr = get_Color(esc - '0');
428 if (mode == draw_RunMode) { 446 if (mode == draw_RunMode) {
447 const iColor clr = get_Color(esc - '0');
429 SDL_SetTextureColorMod(text_.cache, clr.r, clr.g, clr.b); 448 SDL_SetTextureColorMod(text_.cache, clr.r, clr.g, clr.b);
430 } 449 }
431 prevCh = 0; 450 prevCh = 0;