mirror of https://github.com/bjornbytes/lovr.git
Font:getKerning;
This commit is contained in:
parent
078b54a7a5
commit
1f8d37a160
|
@ -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
|
||||
|
|
|
@ -6,6 +6,20 @@
|
|||
#include <lauxlib.h>
|
||||
#include <math.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -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 }
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue