Merge pull request #616 from adamharrison/font-groupings

Added in support for fallback font groupings.
This commit is contained in:
Adam 2021-10-20 11:00:40 -04:00 committed by GitHub
commit 3f1378ab2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 60 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,51 @@ 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(RenFont*)*FONT_FALLBACK_MAX);
if (lua_type(L, 1) != LUA_TTABLE) {
fonts[0] = *(RenFont**)luaL_checkudata(L, idx, API_TYPE_FONT);
return false;
}
int i = 0;
do {
lua_rawgeti(L, idx, i+1);
fonts[i] = !lua_isnil(L, -1) ? *(RenFont**)luaL_checkudata(L, -1, API_TYPE_FONT) : NULL;
lua_pop(L, 1);
} while (fonts[i] && i++ < FONT_FALLBACK_MAX);
return true;
}
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** font = lua_newuserdata(L, sizeof(RenFont*));
*font = ren_font_copy(*self, size);
if (!*font)
return luaL_error(L, "failed to copy font");
RenFont* fonts[FONT_FALLBACK_MAX];
bool table = font_retrieve(L, fonts, 1);
float size = lua_gettop(L) >= 2 ? luaL_checknumber(L, 2) : ren_font_group_get_height(fonts);
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(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 +109,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 +197,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 +223,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

@ -27,7 +27,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;
@ -103,7 +103,7 @@ static int font_set_style(FT_Outline* outline, int x_translation, unsigned char
return 0;
}
void font_load_glyphset(RenFont* font, int idx) {
static void font_load_glyphset(RenFont* font, int idx) {
unsigned int render_option = font_set_render_options(font), load_option = font_set_load_options(font);
int bitmaps_cached = font->subpixel ? SUBPIXEL_BITMAPS_CACHED : 1;
unsigned int byte_width = font->subpixel ? 3 : 1;
@ -111,12 +111,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;
}
@ -143,9 +143,23 @@ void font_load_glyphset(RenFont* font, int idx) {
static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int subpixel_idx) {
int idx = (codepoint >> 8) % MAX_GLYPHSET;
if (!font->sets[subpixel_idx][idx])
if (!font->sets[font->subpixel ? subpixel_idx : 0][idx])
font_load_glyphset(font, idx);
return font->sets[subpixel_idx][idx];
return font->sets[font->subpixel ? subpixel_idx : 0][idx];
}
static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) {
if (bitmap_index < 0)
bitmap_index += SUBPIXEL_BITMAPS_CACHED;
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
*set = font_get_glyphset(fonts[i], codepoint, bitmap_index);
*metric = &(*set)->metrics[codepoint % 256];
if ((*metric)->loaded || codepoint < 0xFF)
return fonts[i];
}
if (!(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1)
return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index);
return fonts[0];
}
RenFont* ren_font_load(const char* path, float size, bool subpixel, unsigned char hinting, unsigned char style) {
@ -189,36 +203,39 @@ 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_size(RenFont **fonts) {
return fonts[0]->size;
}
int ren_font_group_get_height(RenFont **fonts) {
return fonts[0]->size + 3;
}
float ren_font_group_get_width(RenFont **fonts, const char *text) {
float width = 0;
const char* end = text + strlen(text);
GlyphMetric* metric = NULL; GlyphSet* set = NULL;
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;
font_group_get_glyph(&set, &metric, fonts, codepoint, 0);
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;
}
int ren_font_get_height(RenFont *font) {
return font->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;
@ -232,12 +249,13 @@ 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);
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];
GlyphSet* set = NULL; GlyphMetric* metric = NULL;
RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED));
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;
if (!metric->loaded && codepoint > 0xFF)
ren_draw_rect((RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color);
if (set->surface && color.a > 0 && end_x >= clip.x && start_x < clip_end_x) {
unsigned char* source_pixels = set->surface->pixels;
for (int line = metric->y0; line < metric->y1; ++line) {
@ -266,10 +284,10 @@ float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor co
}
}
}
pen_x += metric->xadvance ? metric->xadvance : font->space_advance;
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);