From f2488fdd8dcc20cc1cfdc1ec7a36e01c127fba26 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 12 Oct 2021 23:24:52 -0400 Subject: [PATCH 1/7] Added in support for font groupings. --- data/core/statusview.lua | 3 +- src/api/renderer.c | 69 ++++++++++++++++++++++++++++++---------- src/rencache.c | 16 +++++----- src/rencache.h | 2 +- src/renderer.c | 66 +++++++++++++++++++++++++------------- src/renderer.h | 13 ++++---- 6 files changed, 115 insertions(+), 54 deletions(-) diff --git a/data/core/statusview.lua b/data/core/statusview.lua index 58421c31..3342bdb9 100644 --- a/data/core/statusview.lua +++ b/data/core/statusview.lua @@ -6,6 +6,7 @@ local style = require "core.style" local DocView = require "core.docview" local LogView = require "core.logview" local View = require "core.view" +local Object = require "core.object" local StatusView = View:extend() @@ -70,7 +71,7 @@ local function draw_items(self, items, x, y, draw_fn) local color = style.text for _, item in ipairs(items) do - if type(item) == "userdata" then + if Object.is(item, renderer.font) then font = item elseif type(item) == "table" then color = item diff --git a/src/api/renderer.c b/src/api/renderer.c index a8475cbf..e1f4f9bb 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -55,21 +55,55 @@ static int f_font_load(lua_State *L) { return 1; } +static bool font_retrieve(lua_State* L, RenFont** fonts, int idx) { + memset(fonts, 0, sizeof(fonts)*FONT_FALLBACK_MAX); + if (lua_type(L, 1) == LUA_TTABLE) { + for (int i = 0; i < FONT_FALLBACK_MAX; ++i) { + lua_rawgeti(L, 1, i+1); + bool nil = lua_isnil(L, -1); + if (!nil) + fonts[i] = *(RenFont**)luaL_checkudata(L, -1, API_TYPE_FONT); + lua_pop(L, 1); + if (nil) + break; + } + return true; + } else { + fonts[0] = *(RenFont**)luaL_checkudata(L, 1, API_TYPE_FONT); + return false; + } +} + static int f_font_copy(lua_State *L) { - RenFont** self = luaL_checkudata(L, 1, API_TYPE_FONT); - float size = lua_gettop(L) >= 2 ? luaL_checknumber(L, 2) : ren_font_get_height(*self); - RenFont** font = lua_newuserdata(L, sizeof(RenFont*)); - *font = ren_font_copy(*self, size); - if (!*font) - return luaL_error(L, "failed to copy font"); + RenFont* fonts[FONT_FALLBACK_MAX]; + float size = lua_gettop(L) >= 2 ? luaL_checknumber(L, 2) : ren_font_group_get_height(fonts); + bool table = font_retrieve(L, fonts, 1); + if (table) { + lua_newtable(L); + luaL_setmetatable(L, API_TYPE_FONT); + } + for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { + RenFont** font = lua_newuserdata(L, sizeof(RenFont*)); + *font = ren_font_copy(fonts[i], size); + if (!*font) + return luaL_error(L, "failed to copy font"); + luaL_setmetatable(L, API_TYPE_FONT); + if (table) + lua_rawseti(L, -2, i+1); + } + return 1; +} + +static int f_font_group(lua_State* L) { + luaL_checktype(L, 1, LUA_TTABLE); luaL_setmetatable(L, API_TYPE_FONT); return 1; } static int f_font_set_tab_size(lua_State *L) { - RenFont** self = luaL_checkudata(L, 1, API_TYPE_FONT); + RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); int n = luaL_checknumber(L, 2); - ren_font_set_tab_size(*self, n); + ren_font_group_set_tab_size(fonts, n); return 0; } @@ -79,21 +113,22 @@ static int f_font_gc(lua_State *L) { return 0; } + static int f_font_get_width(lua_State *L) { - RenFont** self = luaL_checkudata(L, 1, API_TYPE_FONT); - lua_pushnumber(L, ren_font_get_width(*self, luaL_checkstring(L, 2))); + RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); + lua_pushnumber(L, ren_font_group_get_width(fonts, luaL_checkstring(L, 2))); return 1; } static int f_font_get_height(lua_State *L) { - RenFont** self = luaL_checkudata(L, 1, API_TYPE_FONT); - lua_pushnumber(L, ren_font_get_height(*self)); + RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); + lua_pushnumber(L, ren_font_group_get_height(fonts)); return 1; } static int f_font_get_size(lua_State *L) { - RenFont** self = luaL_checkudata(L, 1, API_TYPE_FONT); - lua_pushnumber(L, ren_font_get_size(*self)); + RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); + lua_pushnumber(L, ren_font_group_get_size(fonts)); return 1; } @@ -166,12 +201,13 @@ static int f_draw_rect(lua_State *L) { } static int f_draw_text(lua_State *L) { - RenFont** font = luaL_checkudata(L, 1, API_TYPE_FONT); + RenFont* fonts[FONT_FALLBACK_MAX]; + font_retrieve(L, fonts, 1); const char *text = luaL_checkstring(L, 2); float x = luaL_checknumber(L, 3); int y = luaL_checknumber(L, 4); RenColor color = checkcolor(L, 5, 255); - x = rencache_draw_text(L, *font, text, x, y, color); + x = rencache_draw_text(L, fonts, text, x, y, color); lua_pushnumber(L, x); return 1; } @@ -191,6 +227,7 @@ static const luaL_Reg fontLib[] = { { "__gc", f_font_gc }, { "load", f_font_load }, { "copy", f_font_copy }, + { "group", f_font_group }, { "set_tab_size", f_font_set_tab_size }, { "get_width", f_font_get_width }, { "get_height", f_font_get_height }, diff --git a/src/rencache.c b/src/rencache.c index ebb17da1..4997687c 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -24,7 +24,7 @@ typedef struct { int32_t size; RenRect rect; RenColor color; - RenFont *font; + RenFont *fonts[FONT_FALLBACK_MAX]; float text_x; char text[0]; } Command; @@ -128,20 +128,20 @@ void rencache_draw_rect(RenRect rect, RenColor color) { } } -float rencache_draw_text(lua_State *L, RenFont *font, const char *text, float x, int y, RenColor color) +float rencache_draw_text(lua_State *L, RenFont **fonts, const char *text, float x, int y, RenColor color) { - float width = ren_font_get_width(font, text); - RenRect rect = { x, y, (int)width, ren_font_get_height(font) }; + float width = ren_font_group_get_width(fonts, text); + RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) }; if (rects_overlap(screen_rect, rect)) { int sz = strlen(text) + 1; Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz); if (cmd) { memcpy(cmd->text, text, sz); cmd->color = color; - cmd->font = font; + memcpy(cmd->fonts, fonts, sizeof(RenFont*)*FONT_FALLBACK_MAX); cmd->rect = rect; cmd->text_x = x; - cmd->tab_size = ren_font_get_tab_size(font); + cmd->tab_size = ren_font_group_get_tab_size(fonts); } } return x + width; @@ -248,8 +248,8 @@ void rencache_end_frame(lua_State *L) { ren_draw_rect(cmd->rect, cmd->color); break; case DRAW_TEXT: - ren_font_set_tab_size(cmd->font, cmd->tab_size); - ren_draw_text(cmd->font, cmd->text, cmd->text_x, cmd->rect.y, cmd->color); + ren_font_group_set_tab_size(cmd->fonts, cmd->tab_size); + ren_draw_text(cmd->fonts, cmd->text, cmd->text_x, cmd->rect.y, cmd->color); break; } } diff --git a/src/rencache.h b/src/rencache.h index 034d2001..75bb5051 100644 --- a/src/rencache.h +++ b/src/rencache.h @@ -8,7 +8,7 @@ void rencache_show_debug(bool enable); void rencache_set_clip_rect(RenRect rect); void rencache_draw_rect(RenRect rect, RenColor color); -float rencache_draw_text(lua_State *L, RenFont *font, +float rencache_draw_text(lua_State *L, RenFont **font, const char *text, float x, int y, RenColor color); void rencache_invalidate(void); void rencache_begin_frame(lua_State *L); diff --git a/src/renderer.c b/src/renderer.c index 40fde76a..c406510d 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -29,7 +29,7 @@ static void* check_alloc(void *ptr) { /************************* Fonts *************************/ typedef struct { - unsigned short x0, x1, y0, y1; + unsigned short x0, x1, y0, y1, loaded; short bitmap_left, bitmap_top; float xadvance; } GlyphMetric; @@ -113,12 +113,12 @@ void font_load_glyphset(RenFont* font, int idx) { GlyphSet* set = check_alloc(calloc(1, sizeof(GlyphSet))); font->sets[j][idx] = set; for (int i = 0; i < 256; ++i) { - int glyph_index = FT_Get_Char_Index( font->face, i + idx * MAX_GLYPHSET); + int glyph_index = FT_Get_Char_Index(font->face, i + idx * MAX_GLYPHSET); if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option | FT_LOAD_BITMAP_METRICS_ONLY) || font_set_style(&font->face->glyph->outline, j * (64 / SUBPIXEL_BITMAPS_CACHED), font->style) || FT_Render_Glyph(font->face->glyph, render_option)) continue; FT_GlyphSlot slot = font->face->glyph; int glyph_width = slot->bitmap.width / byte_width; - set->metrics[i] = (GlyphMetric){ pen_x, pen_x + glyph_width, 0, slot->bitmap.rows, slot->bitmap_left, slot->bitmap_top, (slot->advance.x + slot->lsb_delta - slot->rsb_delta) / 64.0f}; + set->metrics[i] = (GlyphMetric){ pen_x, pen_x + glyph_width, 0, slot->bitmap.rows, true, slot->bitmap_left, slot->bitmap_top, (slot->advance.x + slot->lsb_delta - slot->rsb_delta) / 64.0f}; pen_x += glyph_width; font->max_height = slot->bitmap.rows > font->max_height ? slot->bitmap.rows : font->max_height; } @@ -192,39 +192,49 @@ void ren_font_free(RenFont* font) { free(font); } -void ren_font_set_tab_size(RenFont *font, int n) { - for (int i = 0; i < (font->subpixel ? SUBPIXEL_BITMAPS_CACHED : 1); ++i) - font_get_glyphset(font, '\t', i)->metrics['\t'].xadvance = font->space_advance * n; +void ren_font_group_set_tab_size(RenFont **fonts, int n) { + for (int j = 0; j < FONT_FALLBACK_MAX && fonts[j]; ++j) { + for (int i = 0; i < (fonts[j]->subpixel ? SUBPIXEL_BITMAPS_CACHED : 1); ++i) + font_get_glyphset(fonts[j], '\t', i)->metrics['\t'].xadvance = fonts[j]->space_advance * n; + } } -int ren_font_get_tab_size(RenFont *font) { - return font_get_glyphset(font, '\t', 0)->metrics['\t'].xadvance / font->space_advance; +int ren_font_group_get_tab_size(RenFont **fonts) { + return font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance / fonts[0]->space_advance; } -float ren_font_get_width(RenFont *font, const char *text) { +float ren_font_group_get_width(RenFont **fonts, const char *text) { float width = 0; const char* end = text + strlen(text); + GlyphMetric* metric; while (text < end) { unsigned int codepoint; text = utf8_to_codepoint(text, &codepoint); - GlyphMetric* metric = &font_get_glyphset(font, codepoint, 0)->metrics[codepoint % 256]; - width += metric->xadvance ? metric->xadvance : font->space_advance; + for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { + metric = &font_get_glyphset(fonts[i], codepoint, 0)->metrics[codepoint % 256]; + if (metric->loaded || codepoint < 0xFF) + break; + } + if (!metric->loaded && codepoint > 0xFF) + metric = &font_get_glyphset(fonts[0], 0x25A1, 0)->metrics[0x25A1 % 256]; + width += metric->xadvance ? metric->xadvance : fonts[0]->space_advance; } const int surface_scale = renwin_surface_scale(&window_renderer); return width / surface_scale; } -float ren_font_get_size(RenFont *font) { - return font->size; +float ren_font_group_get_size(RenFont **fonts) { + return fonts[0]->size; } -int ren_font_get_height(RenFont *font) { - return font->size + 3; +int ren_font_group_get_height(RenFont **fonts) { + return fonts[0]->size + 3; } -float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color) { +float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor color) { SDL_Surface *surface = renwin_get_surface(&window_renderer); const RenRect clip = window_renderer.clip; + RenFont* font; const int surface_scale = renwin_surface_scale(&window_renderer); float pen_x = x * surface_scale; y *= surface_scale; @@ -235,9 +245,21 @@ float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor co while (text < end) { unsigned int codepoint, r, g, b; text = utf8_to_codepoint(text, &codepoint); - int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; - GlyphSet* set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0)); - GlyphMetric* metric = &set->metrics[codepoint % 256]; + GlyphSet* set; GlyphMetric* metric; + for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { + font = fonts[i]; + int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; + set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0)); + metric = &set->metrics[codepoint % 256]; + if (metric->loaded || codepoint < 0xFF) + break; + } + if (!metric->loaded && codepoint > 0xFF) { + codepoint = 0x25A1; font = fonts[0]; + int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; + set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0)); + metric = &set->metrics[codepoint % 256]; + } int start_x = floor(pen_x) + metric->bitmap_left; int end_x = (metric->x1 - metric->x0) + start_x; int glyph_end = metric->x1, glyph_start = metric->x0; @@ -282,10 +304,10 @@ float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor co } } } - pen_x += metric->xadvance ? metric->xadvance : font->space_advance; + pen_x += metric->xadvance ? metric->xadvance : font->space_advance; } - if (font->style & FONT_STYLE_UNDERLINE) - ren_draw_rect((RenRect){ x, y / surface_scale + ren_font_get_height(font) - 1, (pen_x - x) / surface_scale, 1 }, color); + if (fonts[0]->style & FONT_STYLE_UNDERLINE) + ren_draw_rect((RenRect){ x, y / surface_scale + ren_font_group_get_height(fonts) - 1, (pen_x - x) / surface_scale, 1 }, color); return pen_x / surface_scale; } diff --git a/src/renderer.h b/src/renderer.h index 4eff61a5..6058583e 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -5,6 +5,7 @@ #include #include +#define FONT_FALLBACK_MAX 4 typedef struct RenFont RenFont; typedef enum { FONT_HINTING_NONE, FONT_HINTING_SLIGHT, FONT_HINTING_FULL } ERenFontHinting; typedef enum { FONT_STYLE_BOLD = 1, FONT_STYLE_ITALIC = 2, FONT_STYLE_UNDERLINE = 4 } ERenFontStyle; @@ -14,12 +15,12 @@ typedef struct { int x, y, width, height; } RenRect; RenFont* ren_font_load(const char *filename, float size, bool subpixel, unsigned char hinting, unsigned char style); RenFont* ren_font_copy(RenFont* font, float size); void ren_font_free(RenFont *font); -void ren_font_set_tab_size(RenFont *font, int n); -int ren_font_get_tab_size(RenFont *font); -float ren_font_get_width(RenFont *font, const char *text); -int ren_font_get_height(RenFont *font); -float ren_font_get_size(RenFont *font); -float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color); +int ren_font_group_get_tab_size(RenFont **font); +int ren_font_group_get_height(RenFont **font); +float ren_font_group_get_size(RenFont **font); +void ren_font_group_set_tab_size(RenFont **font, int n); +float ren_font_group_get_width(RenFont **font, const char *text); +float ren_draw_text(RenFont **font, const char *text, float x, int y, RenColor color); void ren_draw_rect(RenRect rect, RenColor color); From 7575d2eee68a43301e84ca158c30c48b5cb9d21b Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 16 Oct 2021 23:32:17 -0400 Subject: [PATCH 2/7] Fixed minor issue. --- src/api/renderer.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/api/renderer.c b/src/api/renderer.c index e1f4f9bb..60256118 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -56,28 +56,24 @@ static int f_font_load(lua_State *L) { } static bool font_retrieve(lua_State* L, RenFont** fonts, int idx) { - memset(fonts, 0, sizeof(fonts)*FONT_FALLBACK_MAX); - if (lua_type(L, 1) == LUA_TTABLE) { - for (int i = 0; i < FONT_FALLBACK_MAX; ++i) { - lua_rawgeti(L, 1, i+1); - bool nil = lua_isnil(L, -1); - if (!nil) - fonts[i] = *(RenFont**)luaL_checkudata(L, -1, API_TYPE_FONT); - lua_pop(L, 1); - if (nil) - break; - } - return true; - } else { - fonts[0] = *(RenFont**)luaL_checkudata(L, 1, API_TYPE_FONT); + memset(fonts, 0, sizeof(RenFont*)*FONT_FALLBACK_MAX); + if (lua_type(L, 1) != LUA_TTABLE) { + fonts[0] = *(RenFont**)luaL_checkudata(L, idx, API_TYPE_FONT); return false; } + int i = 0; + do { + lua_rawgeti(L, idx, i+1); + fonts[i] = !lua_isnil(L, -1) ? *(RenFont**)luaL_checkudata(L, -1, API_TYPE_FONT) : NULL; + lua_pop(L, 1); + } while (fonts[i] && i++ < FONT_FALLBACK_MAX); + return true; } static int f_font_copy(lua_State *L) { RenFont* fonts[FONT_FALLBACK_MAX]; - float size = lua_gettop(L) >= 2 ? luaL_checknumber(L, 2) : ren_font_group_get_height(fonts); bool table = font_retrieve(L, fonts, 1); + float size = lua_gettop(L) >= 2 ? luaL_checknumber(L, 2) : ren_font_group_get_height(fonts); if (table) { lua_newtable(L); luaL_setmetatable(L, API_TYPE_FONT); From d1fcdacacd137f461795e1c318e22efe55f5ecfd Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 16 Oct 2021 23:49:42 -0400 Subject: [PATCH 3/7] Broke out font groupings. --- src/renderer.c | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index c406510d..fb955b3e 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -145,9 +145,9 @@ void font_load_glyphset(RenFont* font, int idx) { static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int subpixel_idx) { int idx = (codepoint >> 8) % MAX_GLYPHSET; - if (!font->sets[subpixel_idx][idx]) + if (!font->sets[font->subpixel ? subpixel_idx : 0][idx]) font_load_glyphset(font, idx); - return font->sets[subpixel_idx][idx]; + return font->sets[font->subpixel ? subpixel_idx : 0][idx]; } RenFont* ren_font_load(const char* path, float size, bool subpixel, unsigned char hinting, unsigned char style) { @@ -203,26 +203,6 @@ int ren_font_group_get_tab_size(RenFont **fonts) { return font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance / fonts[0]->space_advance; } -float ren_font_group_get_width(RenFont **fonts, const char *text) { - float width = 0; - const char* end = text + strlen(text); - GlyphMetric* metric; - while (text < end) { - unsigned int codepoint; - text = utf8_to_codepoint(text, &codepoint); - for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { - metric = &font_get_glyphset(fonts[i], codepoint, 0)->metrics[codepoint % 256]; - if (metric->loaded || codepoint < 0xFF) - break; - } - if (!metric->loaded && codepoint > 0xFF) - metric = &font_get_glyphset(fonts[0], 0x25A1, 0)->metrics[0x25A1 % 256]; - width += metric->xadvance ? metric->xadvance : fonts[0]->space_advance; - } - const int surface_scale = renwin_surface_scale(&window_renderer); - return width / surface_scale; -} - float ren_font_group_get_size(RenFont **fonts) { return fonts[0]->size; } @@ -230,6 +210,35 @@ int ren_font_group_get_height(RenFont **fonts) { return fonts[0]->size + 3; } +static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) { + for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { + *set = font_get_glyphset(fonts[i], codepoint, bitmap_index); + *metric = &(*set)->metrics[codepoint % 256]; + if ((*metric)->loaded || codepoint < 0xFF) + return fonts[i]; + } + if (!(*metric)->loaded && codepoint > 0xFF) { + codepoint = 0x25A1; + *set = font_get_glyphset(fonts[0], codepoint, bitmap_index); + *metric = &(*set)->metrics[codepoint % 256]; + } + return fonts[0]; +} + +float ren_font_group_get_width(RenFont **fonts, const char *text) { + float width = 0; + const char* end = text + strlen(text); + GlyphMetric* metric; GlyphSet* set; + while (text < end) { + unsigned int codepoint; + text = utf8_to_codepoint(text, &codepoint); + font_group_get_glyph(&set, &metric, fonts, codepoint, 0); + width += metric->xadvance ? metric->xadvance : fonts[0]->space_advance; + } + const int surface_scale = renwin_surface_scale(&window_renderer); + return width / surface_scale; +} + float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor color) { SDL_Surface *surface = renwin_get_surface(&window_renderer); const RenRect clip = window_renderer.clip; @@ -246,20 +255,9 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor unsigned int codepoint, r, g, b; text = utf8_to_codepoint(text, &codepoint); GlyphSet* set; GlyphMetric* metric; - for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { - font = fonts[i]; - int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; - set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0)); - metric = &set->metrics[codepoint % 256]; - if (metric->loaded || codepoint < 0xFF) - break; - } - if (!metric->loaded && codepoint > 0xFF) { - codepoint = 0x25A1; font = fonts[0]; - int bitmap_index = font->subpixel ? (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED) : 0; - set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0)); - metric = &set->metrics[codepoint % 256]; - } + int bitmap_index = (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED); + bitmap_index += (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0); + font = font_group_get_glyph(&set, &metric, fonts, codepoint, bitmap_index); int start_x = floor(pen_x) + metric->bitmap_left; int end_x = (metric->x1 - metric->x0) + start_x; int glyph_end = metric->x1, glyph_start = metric->x0; From c7c4a3c5286826785de5ea483c1f16802ea79875 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 17 Oct 2021 00:10:40 -0400 Subject: [PATCH 4/7] Clarified. --- src/renderer.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index fb955b3e..a70111fa 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -211,6 +211,8 @@ int ren_font_group_get_height(RenFont **fonts) { } static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) { + if (bitmap_index < 0) + bitmap_index += SUBPIXEL_BITMAPS_CACHED; for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { *set = font_get_glyphset(fonts[i], codepoint, bitmap_index); *metric = &(*set)->metrics[codepoint % 256]; @@ -218,9 +220,8 @@ static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFo return fonts[i]; } if (!(*metric)->loaded && codepoint > 0xFF) { - codepoint = 0x25A1; - *set = font_get_glyphset(fonts[0], codepoint, bitmap_index); - *metric = &(*set)->metrics[codepoint % 256]; + *set = font_get_glyphset(fonts[0], 0x25A1, bitmap_index); + *metric = &(*set)->metrics[0x25A1 % 256]; } return fonts[0]; } @@ -228,7 +229,7 @@ static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFo float ren_font_group_get_width(RenFont **fonts, const char *text) { float width = 0; const char* end = text + strlen(text); - GlyphMetric* metric; GlyphSet* set; + GlyphMetric* metric = NULL; GlyphSet* set = NULL; while (text < end) { unsigned int codepoint; text = utf8_to_codepoint(text, &codepoint); @@ -243,7 +244,6 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor SDL_Surface *surface = renwin_get_surface(&window_renderer); const RenRect clip = window_renderer.clip; - RenFont* font; const int surface_scale = renwin_surface_scale(&window_renderer); float pen_x = x * surface_scale; y *= surface_scale; @@ -254,10 +254,8 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor while (text < end) { unsigned int codepoint, r, g, b; text = utf8_to_codepoint(text, &codepoint); - GlyphSet* set; GlyphMetric* metric; - int bitmap_index = (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED); - bitmap_index += (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0); - font = font_group_get_glyph(&set, &metric, fonts, codepoint, bitmap_index); + GlyphSet* set = NULL; GlyphMetric* metric = NULL; + RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED)); int start_x = floor(pen_x) + metric->bitmap_left; int end_x = (metric->x1 - metric->x0) + start_x; int glyph_end = metric->x1, glyph_start = metric->x0; From cab315bed1521f1c2dd1318be75e261d8ac65ad0 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 17 Oct 2021 00:22:27 -0400 Subject: [PATCH 5/7] Added in a rectdraw when a fallback glyph isn't present. --- src/renderer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/renderer.c b/src/renderer.c index a70111fa..ade84066 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -259,6 +259,8 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor int start_x = floor(pen_x) + metric->bitmap_left; int end_x = (metric->x1 - metric->x0) + start_x; int glyph_end = metric->x1, glyph_start = metric->x0; + if (!metric->loaded && codepoint > 0xFF) + ren_draw_rect((RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color); if (set->surface && color.a > 0 && end_x >= clip.x && start_x < clip_end_x) { unsigned char* source_pixels = set->surface->pixels; for (int line = metric->y0; line < metric->y1; ++line) { From 16fc15daee9b61366eced6d6dc4208ce2e8a0475 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 17 Oct 2021 00:26:20 -0400 Subject: [PATCH 6/7] Allowed for a white square as part of the other groups. --- src/renderer.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index ade84066..7035914d 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -219,10 +219,8 @@ static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFo if ((*metric)->loaded || codepoint < 0xFF) return fonts[i]; } - if (!(*metric)->loaded && codepoint > 0xFF) { - *set = font_get_glyphset(fonts[0], 0x25A1, bitmap_index); - *metric = &(*set)->metrics[0x25A1 % 256]; - } + if (!(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1) + return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index); return fonts[0]; } From b816a04d2725e7835c838664af084073353b2a49 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 17 Oct 2021 00:39:08 -0400 Subject: [PATCH 7/7] Added in a missing static. --- src/renderer.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/renderer.c b/src/renderer.c index 7035914d..385f8303 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -105,7 +105,7 @@ static int font_set_style(FT_Outline* outline, int x_translation, unsigned char return 0; } -void font_load_glyphset(RenFont* font, int idx) { +static void font_load_glyphset(RenFont* font, int idx) { unsigned int render_option = font_set_render_options(font), load_option = font_set_load_options(font); int bitmaps_cached = font->subpixel ? SUBPIXEL_BITMAPS_CACHED : 1; unsigned int byte_width = font->subpixel ? 3 : 1; @@ -150,6 +150,20 @@ static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int su return font->sets[font->subpixel ? subpixel_idx : 0][idx]; } +static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) { + if (bitmap_index < 0) + bitmap_index += SUBPIXEL_BITMAPS_CACHED; + for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { + *set = font_get_glyphset(fonts[i], codepoint, bitmap_index); + *metric = &(*set)->metrics[codepoint % 256]; + if ((*metric)->loaded || codepoint < 0xFF) + return fonts[i]; + } + if (!(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1) + return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index); + return fonts[0]; +} + RenFont* ren_font_load(const char* path, float size, bool subpixel, unsigned char hinting, unsigned char style) { FT_Face face; if (FT_New_Face( library, path, 0, &face)) @@ -210,20 +224,6 @@ int ren_font_group_get_height(RenFont **fonts) { return fonts[0]->size + 3; } -static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) { - if (bitmap_index < 0) - bitmap_index += SUBPIXEL_BITMAPS_CACHED; - for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { - *set = font_get_glyphset(fonts[i], codepoint, bitmap_index); - *metric = &(*set)->metrics[codepoint % 256]; - if ((*metric)->loaded || codepoint < 0xFF) - return fonts[i]; - } - if (!(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1) - return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index); - return fonts[0]; -} - float ren_font_group_get_width(RenFont **fonts, const char *text) { float width = 0; const char* end = text + strlen(text);