mirror of https://github.com/bjornbytes/lovr.git
Font metrics;
This commit is contained in:
parent
0b57b48702
commit
7077d89a22
|
@ -1,6 +1,38 @@
|
|||
#include "api/lovr.h"
|
||||
#include "graphics/font.h"
|
||||
|
||||
int l_lovrFontGetWidth(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
const char* string = luaL_checkstring(L, 2);
|
||||
float wrap = luaL_optnumber(L, 3, 0);
|
||||
lua_pushnumber(L, lovrFontGetWidth(font, string, wrap));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFontGetHeight(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
lua_pushnumber(L, lovrFontGetHeight(font));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFontGetAscent(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
lua_pushnumber(L, lovrFontGetAscent(font));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFontGetDescent(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
lua_pushnumber(L, lovrFontGetDescent(font));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFontGetBaseline(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
lua_pushnumber(L, lovrFontGetBaseline(font));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrFontGetLineHeight(lua_State* L) {
|
||||
Font* font = luax_checktype(L, 1, Font);
|
||||
lua_pushinteger(L, lovrFontGetLineHeight(font));
|
||||
|
@ -28,6 +60,11 @@ int l_lovrFontSetPixelDensity(lua_State* L) {
|
|||
}
|
||||
|
||||
const luaL_Reg lovrFont[] = {
|
||||
{ "getWidth", l_lovrFontGetWidth },
|
||||
{ "getHeight", l_lovrFontGetHeight },
|
||||
{ "getAscent", l_lovrFontGetAscent },
|
||||
{ "getDescent", l_lovrFontGetDescent },
|
||||
{ "getBaseline", l_lovrFontGetBaseline },
|
||||
{ "getLineHeight", l_lovrFontGetLineHeight },
|
||||
{ "setLineHeight", l_lovrFontSetLineHeight },
|
||||
{ "getPixelDensity", l_lovrFontGetPixelDensity },
|
||||
|
|
|
@ -170,6 +170,49 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori
|
|||
lovrGraphicsPop();
|
||||
}
|
||||
|
||||
float lovrFontGetWidth(Font* font, const char* str, float wrap) {
|
||||
float width = 0;
|
||||
float x = 0;
|
||||
const char* end = str + strlen(str);
|
||||
size_t bytes;
|
||||
unsigned int previous = '\0';
|
||||
unsigned int codepoint;
|
||||
float scale = 1 / font->pixelDensity;
|
||||
|
||||
while ((bytes = utf8_decode(str, end, &codepoint)) > 0) {
|
||||
if (codepoint == '\n' || (wrap && x * scale > wrap && codepoint == ' ')) {
|
||||
width = MAX(width, x) / font->pixelDensity;
|
||||
x = 0;
|
||||
previous = '\0';
|
||||
str += bytes;
|
||||
continue;
|
||||
}
|
||||
|
||||
Glyph* glyph = lovrFontGetGlyph(font, codepoint);
|
||||
x += glyph->advance + lovrFontGetKerning(font, previous, codepoint);
|
||||
previous = codepoint;
|
||||
str += bytes;
|
||||
}
|
||||
|
||||
return MAX(x, width) * scale;
|
||||
}
|
||||
|
||||
float lovrFontGetHeight(Font* font) {
|
||||
return font->fontData->height / font->pixelDensity;
|
||||
}
|
||||
|
||||
float lovrFontGetAscent(Font* font) {
|
||||
return font->fontData->ascent / font->pixelDensity;
|
||||
}
|
||||
|
||||
float lovrFontGetDescent(Font* font) {
|
||||
return font->fontData->descent / font->pixelDensity;
|
||||
}
|
||||
|
||||
float lovrFontGetBaseline(Font* font) {
|
||||
return font->fontData->height * .8 / font->pixelDensity;
|
||||
}
|
||||
|
||||
float lovrFontGetLineHeight(Font* font) {
|
||||
return font->lineHeight;
|
||||
}
|
||||
|
|
|
@ -44,6 +44,11 @@ typedef struct {
|
|||
Font* lovrFontCreate(FontData* fontData);
|
||||
void lovrFontDestroy(const Ref* ref);
|
||||
void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign);
|
||||
float lovrFontGetWidth(Font* font, const char* string, float wrap);
|
||||
float lovrFontGetHeight(Font* font);
|
||||
float lovrFontGetAscent(Font* font);
|
||||
float lovrFontGetDescent(Font* font);
|
||||
float lovrFontGetBaseline(Font* font);
|
||||
float lovrFontGetLineHeight(Font* font);
|
||||
void lovrFontSetLineHeight(Font* font, float lineHeight);
|
||||
int lovrFontGetKerning(Font* font, unsigned int a, unsigned int b);
|
||||
|
|
|
@ -32,6 +32,8 @@ FontData* lovrFontDataCreate(void* data, int size, int height) {
|
|||
|
||||
FT_Size_Metrics metrics = face->size->metrics;
|
||||
fontData->height = metrics.height >> 6;
|
||||
fontData->ascent = metrics.ascender >> 6;
|
||||
fontData->descent = metrics.descender >> 6;
|
||||
|
||||
return fontData;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ typedef struct {
|
|||
void* rasterizer;
|
||||
int size;
|
||||
int height;
|
||||
int ascent;
|
||||
int descent;
|
||||
} FontData;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#define MAX(a, b) a > b ? a : b
|
||||
#define MIN(a, b) a < b ? a : b
|
||||
#define MAX(a, b) (a > b ? a : b)
|
||||
#define MIN(a, b) (a < b ? a : b)
|
||||
|
||||
typedef float* vec3;
|
||||
typedef float* quat;
|
||||
|
|
Loading…
Reference in New Issue