Cleanup FontDesc struct and implementation

This commit is contained in:
Francesco Abbate 2021-04-26 15:56:18 +02:00
parent 46c3bdea67
commit f2a33a567b
3 changed files with 47 additions and 41 deletions

View File

@ -45,17 +45,8 @@ static int f_load(lua_State *L) {
luaL_error(L, "failed to load font");
}
FontDesc *font_desc = lua_newuserdata(L, sizeof(FontDesc));
// FIXME: implement font_desc initialization in fontdesc.c
int filename_sz = strlen(filename) + 1;
font_desc->filename = malloc(filename_sz);
memcpy(font_desc->filename, filename, filename_sz);
font_desc->size = size;
font_desc->options = font_options;
font_desc->tab_size = 4;
font_desc->fonts_scale_length = 0;
font_desc->recent_font_scale_index = 0; /* No need to initialize. */
FontDesc *font_desc = lua_newuserdata(L, font_desc_alloc_size(filename));
font_desc_init(font_desc, filename, size, font_options);
luaL_setmetatable(L, API_TYPE_FONT);
return 1;
}

View File

@ -1,10 +1,33 @@
#include <stddef.h>
#include "fontdesc.h"
#include "renderer.h"
int font_desc_alloc_size(const char *filename) {
return offsetof(FontDesc, filename) + strlen(filename) + 1;
}
void font_desc_init(FontDesc *font_desc, const char *filename, float size, unsigned int font_options) {
memcpy(font_desc->filename, filename, strlen(filename) + 1);
font_desc->size = size;
font_desc->options = font_options;
font_desc->tab_size = 4;
font_desc->cache_length = 0;
font_desc->cache_last_index = 0; /* Normally no need to initialize. */
}
void font_desc_free(FontDesc *font_desc) {
for (int i = 0; i < font_desc->cache_length; i++) {
ren_free_font(font_desc->cache[i].font);
}
font_desc->cache_length = 0;
}
void font_desc_set_tab_size(FontDesc *font_desc, int tab_size) {
font_desc->tab_size = tab_size;
for (int i = 0; i < font_desc->fonts_scale_length; i++) {
ren_set_font_tab_size(font_desc->fonts_scale[i].font, tab_size);
for (int i = 0; i < font_desc->cache_length; i++) {
ren_set_font_tab_size(font_desc->cache[i].font, tab_size);
}
}
@ -12,42 +35,34 @@ int font_desc_get_tab_size(FontDesc *font_desc) {
return font_desc->tab_size;
}
void font_desc_free(FontDesc *font_desc) {
for (int i = 0; i < font_desc->fonts_scale_length; i++) {
ren_free_font(font_desc->fonts_scale[i].font);
}
font_desc->fonts_scale_length = 0;
free(font_desc->filename);
}
static void load_scaled_font(FontDesc *font_desc, int index, int scale) {
RenFont *font = ren_load_font(font_desc->filename, scale * font_desc->size, font_desc->options);
font_desc->fonts_scale[index].font = font;
font_desc->fonts_scale[index].scale = scale;
font_desc->cache[index].font = font;
font_desc->cache[index].scale = scale;
}
RenFont *font_desc_get_font_at_scale(FontDesc *font_desc, int scale) {
int index = -1;
for (int i = 0; i < font_desc->fonts_scale_length; i++) {
if (font_desc->fonts_scale[i].scale == scale) {
for (int i = 0; i < font_desc->cache_length; i++) {
if (font_desc->cache[i].scale == scale) {
index = i;
break;
}
}
if (index < 0) {
index = font_desc->fonts_scale_length;
if (index < FONT_SCALE_ARRAY_MAX) {
index = font_desc->cache_length;
if (index < FONT_CACHE_ARRAY_MAX) {
load_scaled_font(font_desc, index, scale);
font_desc->fonts_scale_length = index + 1;
font_desc->cache_length = index + 1;
} else {
// FIXME: should not print into the stderr or stdout.
fprintf(stderr, "Warning: max array of font scale reached.\n");
index = (font_desc->recent_font_scale_index == 0 ? 1 : 0);
ren_free_font(font_desc->fonts_scale[index].font);
index = (font_desc->cache_last_index == 0 ? 1 : 0);
ren_free_font(font_desc->cache[index].font);
load_scaled_font(font_desc, index, scale);
}
}
font_desc->recent_font_scale_index = index;
return font_desc->fonts_scale[index].font;
font_desc->cache_last_index = index;
return font_desc->cache[index].font;
}

View File

@ -3,27 +3,27 @@
typedef struct RenFont RenFont;
// FIXME: find a better name for the struct below
struct FontScaled {
struct FontInstance {
RenFont *font;
short int scale;
};
typedef struct FontScaled FontScaled;
typedef struct FontInstance FontInstance;
#define FONT_SCALE_ARRAY_MAX 2
#define FONT_CACHE_ARRAY_MAX 2
struct FontDesc {
char *filename;
float size;
unsigned int options;
short int tab_size;
// FIXME: find a better name for the array below
FontScaled fonts_scale[FONT_SCALE_ARRAY_MAX];
short int fonts_scale_length;
short int recent_font_scale_index; /* More recently scale used. */
FontInstance cache[FONT_CACHE_ARRAY_MAX];
short int cache_length;
short int cache_last_index; /* More recently used instance. */
char filename[0];
};
typedef struct FontDesc FontDesc;
void font_desc_init(FontDesc *font_desc, const char *filename, float size, unsigned int font_options);
int font_desc_alloc_size(const char *filename);
int font_desc_get_tab_size(FontDesc *font_desc);
void font_desc_set_tab_size(FontDesc *font_desc, int tab_size);
void font_desc_free(FontDesc *font_desc);