diff --git a/lib/sdf.c b/lib/sdf.c index 3fae68b7..0a0ae9e1 100644 --- a/lib/sdf.c +++ b/lib/sdf.c @@ -7,6 +7,7 @@ #include #include +#include #include #define BASE_WIDTH 50 @@ -16,6 +17,8 @@ static sprite_t _font_data_thin; static sprite_t _font_data_bold; +static hashmap_t * _font_cache; + struct { char code; size_t width_bold; @@ -123,6 +126,7 @@ static int loaded = 0; __attribute__((constructor)) static void _init_sdf(void) { /* Load the font. */ + _font_cache = hashmap_create_int(10); load_sprite(&_font_data_thin, "/usr/share/sdf_thin.bmp"); load_sprite(&_font_data_bold, "/usr/share/sdf_bold.bmp"); loaded = 1; @@ -198,9 +202,18 @@ int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, if (!loaded) return 0; double scale = (double)size / 50.0; - sprite_t * tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE); - gfx_context_t * t = init_graphics_sprite(tmp); - draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height); + int scale_height = scale * _font_data->height; + + sprite_t * tmp; + if (!hashmap_has(_font_cache, (void *)(scale_height | (font << 16)))) { + tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE); + gfx_context_t * t = init_graphics_sprite(tmp); + draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height); + free(t); + hashmap_set(_font_cache, (void *)(scale_height | (font << 16)), tmp); + } else { + tmp = hashmap_get(_font_cache, (void *)(scale_height | (font << 16))); + } int32_t out_width = 0; while (*str) { @@ -210,9 +223,6 @@ int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str, str++; } - sprite_free(tmp); - free(t); - return out_width; } diff --git a/util/auto-dep.py b/util/auto-dep.py index 242a4bad..737bacff 100755 --- a/util/auto-dep.py +++ b/util/auto-dep.py @@ -34,7 +34,7 @@ class Classifier(object): '': (None, '-ltoaru_yutani', ['', '', '', '', '']), '': (None, '-ltoaru_decorations', ['', '','']), '': (None, '-ltoaru_termemu', ['']), - '': (None, '-ltoaru_sdf', ['']), + '': (None, '-ltoaru_sdf', ['', '']), } def __init__(self, filename):