From 1f8d37a1602a26df5dff2fcde1c0db41618fb72b Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 29 Jun 2022 21:49:55 -0700 Subject: [PATCH] Font:getKerning; --- src/api/api.h | 1 + src/api/l_data_rasterizer.c | 28 ++++++++++++++-------------- src/api/l_graphics_font.c | 11 +++++++++++ src/modules/graphics/graphics.c | 6 +++--- src/modules/graphics/graphics.h | 1 + 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/api/api.h b/src/api/api.h index cd20ad54..26cd782f 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -142,6 +142,7 @@ struct Blob; struct Image; struct Blob* luax_readblob(struct lua_State* L, int index, const char* debug); struct Image* luax_checkimage(struct lua_State* L, int index); +uint32_t luax_checkcodepoint(struct lua_State* L, int index); #endif #ifndef LOVR_DISABLE_EVENT diff --git a/src/api/l_data_rasterizer.c b/src/api/l_data_rasterizer.c index 0fb84699..950acc8b 100644 --- a/src/api/l_data_rasterizer.c +++ b/src/api/l_data_rasterizer.c @@ -6,6 +6,20 @@ #include #include +uint32_t luax_checkcodepoint(lua_State* L, int index) { + size_t length; + const char* str; + uint32_t codepoint; + switch (lua_type(L, index)) { + case LUA_TSTRING: + str = lua_tolstring(L, index, &length); + return utf8_decode(str, str + length, &codepoint) ? codepoint : 0; + case LUA_TNUMBER: + return luax_checku32(L, index); + default: return luax_typeerror(L, index, "string or number"), 0; + } +} + static int l_lovrRasterizerGetFontSize(lua_State* L) { Rasterizer* rasterizer = luax_checktype(L, 1, Rasterizer); float size = lovrRasterizerGetFontSize(rasterizer); @@ -67,20 +81,6 @@ static int l_lovrRasterizerGetLeading(lua_State* L) { return 1; } -static uint32_t luax_checkcodepoint(lua_State* L, int index) { - size_t length; - const char* str; - uint32_t codepoint; - switch (lua_type(L, index)) { - case LUA_TSTRING: - str = lua_tolstring(L, index, &length); - return utf8_decode(str, str + length, &codepoint) ? codepoint : 0; - case LUA_TNUMBER: - return luax_checku32(L, index); - default: return luax_typeerror(L, index, "string or number"), 0; - } -} - static int l_lovrRasterizerGetAdvance(lua_State* L) { Rasterizer* rasterizer = luax_checktype(L, 1, Rasterizer); uint32_t codepoint = luax_checkcodepoint(L, 2); diff --git a/src/api/l_graphics_font.c b/src/api/l_graphics_font.c index daaa3a4a..89189cfe 100644 --- a/src/api/l_graphics_font.c +++ b/src/api/l_graphics_font.c @@ -89,6 +89,16 @@ static int l_lovrFontGetHeight(lua_State* L) { return 1; } +static int l_lovrFontGetKerning(lua_State* L) { + Font* font = luax_checktype(L, 1, Font); + uint32_t left = luax_checkcodepoint(L, 2); + uint32_t right = luax_checkcodepoint(L, 3); + float kerning = lovrFontGetKerning(font, left, right); + float density = lovrFontGetPixelDensity(font); + lua_pushnumber(L, kerning / density); + return 1; +} + static void online(void* context, const char* string, size_t length) { lua_State* L = context; int index = luax_len(L, -1) + 1; @@ -117,6 +127,7 @@ const luaL_Reg lovrFont[] = { { "getAscent", l_lovrFontGetAscent }, { "getDescent", l_lovrFontGetDescent }, { "getHeight", l_lovrFontGetHeight }, + { "getKerning", l_lovrFontGetKerning }, { "getWrap", l_lovrFontGetWrap }, { NULL, NULL } }; diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 17dcd8d7..1bb90a93 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -1970,13 +1970,13 @@ static Glyph* lovrFontGetGlyph(Font* font, uint32_t codepoint, bool* resized) { return glyph; } -static float lovrFontGetKerning(Font* font, uint32_t previous, uint32_t codepoint) { - uint32_t codepoints[] = { previous, codepoint }; +float lovrFontGetKerning(Font* font, uint32_t left, uint32_t right) { + uint32_t codepoints[] = { left, right }; uint64_t hash = hash64(codepoints, sizeof(codepoints)); union { float f32; uint64_t u64; } kerning = { .u64 = map_get(&font->kerning, hash) }; if (kerning.u64 == MAP_NIL) { - kerning.f32 = lovrRasterizerGetKerning(font->info.rasterizer, previous, codepoint); + kerning.f32 = lovrRasterizerGetKerning(font->info.rasterizer, left, right); map_set(&font->kerning, hash, kerning.u64); } diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index 78cdc4f1..77c00f74 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -353,6 +353,7 @@ float lovrFontGetPixelDensity(Font* font); void lovrFontSetPixelDensity(Font* font, float pixelDensity); float lovrFontGetLineSpacing(Font* font); void lovrFontSetLineSpacing(Font* font, float spacing); +float lovrFontGetKerning(Font* font, uint32_t left, uint32_t right); void lovrFontGetWrap(Font* font, ColoredString* strings, uint32_t count, float wrap, void (*callback)(void* context, const char* string, size_t length), void* context); // Pass