mirror of https://github.com/bjornbytes/lovr.git
Some font cleanup;
This commit is contained in:
parent
02daeb1a2b
commit
756b184306
|
@ -46,6 +46,13 @@ static int l_lovrRasterizerHasGlyphs(lua_State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrRasterizerGetHeight(lua_State* L) {
|
||||
Rasterizer* rasterizer = luax_checktype(L, 1, Rasterizer);
|
||||
float height = lovrRasterizerGetHeight(rasterizer);
|
||||
lua_pushnumber(L, height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrRasterizerGetAscent(lua_State* L) {
|
||||
Rasterizer* rasterizer = luax_checktype(L, 1, Rasterizer);
|
||||
float ascent = lovrRasterizerGetAscent(rasterizer);
|
||||
|
@ -222,6 +229,7 @@ const luaL_Reg lovrRasterizer[] = {
|
|||
{ "getFontSize", l_lovrRasterizerGetFontSize },
|
||||
{ "getGlyphCount", l_lovrRasterizerGetGlyphCount },
|
||||
{ "hasGlyphs", l_lovrRasterizerHasGlyphs },
|
||||
{ "getHeight", l_lovrRasterizerGetHeight },
|
||||
{ "getAscent", l_lovrRasterizerGetAscent },
|
||||
{ "getDescent", l_lovrRasterizerGetDescent },
|
||||
{ "getLineGap", l_lovrRasterizerGetLineGap },
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "api.h"
|
||||
#include "graphics/graphics.h"
|
||||
#include "data/rasterizer.h"
|
||||
#include "util.h"
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
@ -25,9 +26,75 @@ static int l_lovrFontSetPixelDensity(lua_State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetLineSpacing(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
float spacing = lovrFontGetLineSpacing(font);
|
||||
lua_pushnumber(L, spacing);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrFontSetLineSpacing(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
float spacing = luax_optfloat(L, 2, 1.f);
|
||||
lovrFontSetLineSpacing(font, spacing);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetHeight(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const FontInfo* info = lovrFontGetInfo(font);
|
||||
float density = lovrFontGetPixelDensity(font);
|
||||
float height = lovrRasterizerGetHeight(info->rasterizer);
|
||||
lua_pushnumber(L, height / density);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetAscent(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const FontInfo* info = lovrFontGetInfo(font);
|
||||
float density = lovrFontGetPixelDensity(font);
|
||||
float ascent = lovrRasterizerGetAscent(info->rasterizer);
|
||||
lua_pushnumber(L, ascent / density);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetDescent(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const FontInfo* info = lovrFontGetInfo(font);
|
||||
float density = lovrFontGetPixelDensity(font);
|
||||
float descent = lovrRasterizerGetDescent(info->rasterizer);
|
||||
lua_pushnumber(L, descent / density);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetLineGap(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const FontInfo* info = lovrFontGetInfo(font);
|
||||
float density = lovrFontGetPixelDensity(font);
|
||||
float lineGap = lovrRasterizerGetLineGap(info->rasterizer);
|
||||
lua_pushnumber(L, lineGap / density);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_lovrFontGetLeading(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const FontInfo* info = lovrFontGetInfo(font);
|
||||
float density = lovrFontGetPixelDensity(font);
|
||||
float leading = lovrRasterizerGetLeading(info->rasterizer);
|
||||
lua_pushnumber(L, leading / density);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrFont[] = {
|
||||
{ "getRasterizer", l_lovrFontGetRasterizer },
|
||||
{ "getPixelDensity", l_lovrFontGetPixelDensity },
|
||||
{ "setPixelDensity", l_lovrFontSetPixelDensity },
|
||||
{ "getLineSpacing", l_lovrFontGetLineSpacing },
|
||||
{ "setLineSpacing", l_lovrFontSetLineSpacing },
|
||||
{ "getHeight", l_lovrFontGetHeight },
|
||||
{ "getAscent", l_lovrFontGetAscent },
|
||||
{ "getDescent", l_lovrFontGetDescent },
|
||||
{ "getLineGap", l_lovrFontGetLineGap },
|
||||
{ "getLeading", l_lovrFontGetLeading },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -85,6 +85,10 @@ bool lovrRasterizerHasGlyphs(Rasterizer* rasterizer, const char* str, size_t len
|
|||
return true;
|
||||
}
|
||||
|
||||
float lovrRasterizerGetHeight(Rasterizer* rasterizer) {
|
||||
return rasterizer->ascent - rasterizer->descent;
|
||||
}
|
||||
|
||||
float lovrRasterizerGetAscent(Rasterizer* rasterizer) {
|
||||
return rasterizer->ascent;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ void lovrRasterizerGetBoundingBox(Rasterizer* rasterizer, float box[4]);
|
|||
uint32_t lovrRasterizerGetGlyphCount(Rasterizer* rasterizer);
|
||||
bool lovrRasterizerHasGlyph(Rasterizer* rasterizer, uint32_t codepoint);
|
||||
bool lovrRasterizerHasGlyphs(Rasterizer* rasterizer, const char* str, size_t length);
|
||||
float lovrRasterizerGetHeight(Rasterizer* rasterizer);
|
||||
float lovrRasterizerGetAscent(Rasterizer* rasterizer);
|
||||
float lovrRasterizerGetDescent(Rasterizer* rasterizer);
|
||||
float lovrRasterizerGetLineGap(Rasterizer* rasterizer);
|
||||
|
|
|
@ -139,6 +139,7 @@ struct Font {
|
|||
map_t glyphLookup;
|
||||
map_t kerning;
|
||||
float pixelDensity;
|
||||
float lineSpacing;
|
||||
Texture* atlas;
|
||||
uint32_t atlasWidth;
|
||||
uint32_t atlasHeight;
|
||||
|
@ -1784,6 +1785,9 @@ Font* lovrFontCreate(FontInfo* info) {
|
|||
font->atlasHeight <<= 1;
|
||||
}
|
||||
|
||||
font->pixelDensity = lovrRasterizerGetHeight(info->rasterizer);
|
||||
font->lineSpacing = 1.f;
|
||||
|
||||
return font;
|
||||
}
|
||||
|
||||
|
@ -1810,6 +1814,14 @@ void lovrFontSetPixelDensity(Font* font, float pixelDensity) {
|
|||
font->pixelDensity = pixelDensity;
|
||||
}
|
||||
|
||||
float lovrFontGetLineSpacing(Font* font) {
|
||||
return font->lineSpacing;
|
||||
}
|
||||
|
||||
void lovrFontSetLineSpacing(Font* font, float spacing) {
|
||||
font->lineSpacing = spacing;
|
||||
}
|
||||
|
||||
static Glyph* lovrFontGetGlyph(Font* font, uint32_t codepoint) {
|
||||
uint64_t hash = hash64(&codepoint, 4);
|
||||
uint64_t index = map_get(&font->glyphLookup, hash);
|
||||
|
@ -1834,11 +1846,6 @@ static Glyph* lovrFontGetGlyph(Font* font, uint32_t codepoint) {
|
|||
|
||||
lovrRasterizerGetGlyphBoundingBox(font->info.rasterizer, codepoint, glyph->box);
|
||||
|
||||
glyph->box[0] -= font->info.padding;
|
||||
glyph->box[1] -= font->info.padding;
|
||||
glyph->box[2] += font->info.padding;
|
||||
glyph->box[3] += font->info.padding;
|
||||
|
||||
float width = glyph->box[2] - glyph->box[0];
|
||||
float height = glyph->box[3] - glyph->box[1];
|
||||
|
||||
|
@ -3356,9 +3363,10 @@ void lovrPassText(Pass* pass, Font* font, const char* text, uint32_t length, flo
|
|||
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
float leading = lovrRasterizerGetLeading(font->info.rasterizer) * .8f;
|
||||
float ascent = lovrRasterizerGetAscent(font->info.rasterizer) * .8f;
|
||||
float scale = 1.f / ascent;
|
||||
float leading = lovrRasterizerGetLeading(font->info.rasterizer) * font->lineSpacing;
|
||||
float height = lovrRasterizerGetHeight(font->info.rasterizer);
|
||||
float ascent = lovrRasterizerGetAscent(font->info.rasterizer);
|
||||
float scale = 1.f / font->pixelDensity;
|
||||
wrap /= scale;
|
||||
|
||||
size_t bytes;
|
||||
|
@ -3441,7 +3449,7 @@ void lovrPassText(Pass* pass, Font* font, const char* text, uint32_t length, flo
|
|||
lovrFontUploadNewGlyphs(font, originalGlyphCount);
|
||||
|
||||
mat4_scale(transform, scale, scale, scale);
|
||||
float totalHeight = ascent + leading * (lineCount - 1);
|
||||
float totalHeight = height + leading * (lineCount - 1);
|
||||
mat4_translate(transform, 0.f, -ascent + valign / 2.f * totalHeight, 0.f);
|
||||
|
||||
uint16_t* indices;
|
||||
|
|
|
@ -334,6 +334,8 @@ void lovrFontDestroy(void* ref);
|
|||
const FontInfo* lovrFontGetInfo(Font* font);
|
||||
float lovrFontGetPixelDensity(Font* font);
|
||||
void lovrFontSetPixelDensity(Font* font, float pixelDensity);
|
||||
float lovrFontGetLineSpacing(Font* font);
|
||||
void lovrFontSetLineSpacing(Font* font, float spacing);
|
||||
|
||||
// Pass
|
||||
|
||||
|
|
Loading…
Reference in New Issue