diff options
author | Alex Auvolat <alex@adnab.me> | 2017-05-04 11:48:08 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2017-05-04 11:48:08 +0200 |
commit | 9bb1c5371affb2ff0b83256470dec7461b404264 (patch) | |
tree | 9db7cd56713864a07f047572bfa2d55a5a9933dd /src/lib/libkogata | |
parent | 1161e1d8be014945266017cb0ce735537a287677 (diff) | |
download | kogata-9bb1c5371affb2ff0b83256470dec7461b404264.tar.gz kogata-9bb1c5371affb2ff0b83256470dec7461b404264.zip |
Beginning of tk.lua
Diffstat (limited to 'src/lib/libkogata')
-rw-r--r-- | src/lib/libkogata/draw.c | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/lib/libkogata/draw.c b/src/lib/libkogata/draw.c index 662b0d5..14f1dcf 100644 --- a/src/lib/libkogata/draw.c +++ b/src/lib/libkogata/draw.c @@ -433,16 +433,43 @@ void g_decref_font(font_t *f) { } } -int g_text_width(font_t *font, const char* text) { +int g_text_width(font_t *font, const char* text, int size) { if (font->type == FONT_ASCII_BITMAP) { return font->ascii_bitmap.cw * strlen(text); + } else if (font->type == FONT_STBTT) { + float scale = stbtt_ScaleForPixelHeight(&font->stbtt.info, size); + float xpos = 1; + while (*text != 0) { + int codepoint = *text; // TODO utf8 + text++; + + int advance, lsb; + stbtt_GetCodepointHMetrics(&font->stbtt.info, codepoint, &advance, &lsb); + xpos += scale * advance; + if (*(text+1)) + xpos += scale * stbtt_GetCodepointKernAdvance(&font->stbtt.info, *text, *(text+1)); + } + return ceil(xpos); + } return 0; } -int g_text_height(font_t *font, const char* text) { +int g_text_height(font_t *font, const char* text, int size) { if (font->type == FONT_ASCII_BITMAP) { return font->ascii_bitmap.ch; + } else if (font->type == FONT_STBTT) { + float scale = stbtt_ScaleForPixelHeight(&font->stbtt.info, size); + int max_h = 0; + while (*text != 0) { + int codepoint = *text; // TODO utf8 + text++; + + int x0, y0, x1, y1; + stbtt_GetCodepointBitmapBox(&font->stbtt.info, codepoint, scale, scale, &x0, &y0, &x1, &y1); + if (y1 - y0 > max_h) max_h = y1 - y0; + } + return max_h; } return 0; } @@ -501,7 +528,7 @@ void g_write(fb_t *fb, int x, int y, const char* text, font_t *font, int size, c int sx = (fb->geom.memory_model == FB_MM_RGB32 ? 4 : 3); for (int i = 0; i < h; i++) { - int yy = y + y0 + i; + int yy = y + size + y0 + i; if (yy < 0) continue; if (yy >= fb->geom.height) continue; uint8_t *line = fb->data + yy * fb->geom.pitch; |