Added in support for font groupings.

This commit is contained in:
Adam Harrison 2021-10-12 23:24:52 -04:00
parent 3e2b0f28c8
commit f2488fdd8d
6 changed files with 115 additions and 54 deletions

View File

@ -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

View File

@ -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* 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(*self, size);
*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 },

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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);
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;
GlyphSet* set = font_get_glyphset(font, codepoint, bitmap_index + (bitmap_index < 0 ? SUBPIXEL_BITMAPS_CACHED : 0));
GlyphMetric* metric = &set->metrics[codepoint % 256];
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;
@ -284,8 +306,8 @@ float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor co
}
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;
}

View File

@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdbool.h>
#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);