2021-04-26 15:56:18 +02:00
|
|
|
#include <stddef.h>
|
2021-04-27 11:56:02 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-26 15:56:18 +02:00
|
|
|
|
2021-04-24 10:21:34 +02:00
|
|
|
#include "fontdesc.h"
|
|
|
|
#include "renderer.h"
|
|
|
|
|
2021-04-26 15:56:18 +02:00
|
|
|
|
|
|
|
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. */
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:49:37 +02:00
|
|
|
void font_desc_clear(FontDesc *font_desc) {
|
2021-04-26 15:56:18 +02:00
|
|
|
for (int i = 0; i < font_desc->cache_length; i++) {
|
|
|
|
ren_free_font(font_desc->cache[i].font);
|
|
|
|
}
|
|
|
|
font_desc->cache_length = 0;
|
2021-06-03 22:49:37 +02:00
|
|
|
font_desc->cache_last_index = 0;
|
2021-04-26 15:56:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:21:34 +02:00
|
|
|
void font_desc_set_tab_size(FontDesc *font_desc, int tab_size) {
|
|
|
|
font_desc->tab_size = tab_size;
|
2021-04-26 15:56:18 +02:00
|
|
|
for (int i = 0; i < font_desc->cache_length; i++) {
|
|
|
|
ren_set_font_tab_size(font_desc->cache[i].font, tab_size);
|
2021-04-24 10:21:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int font_desc_get_tab_size(FontDesc *font_desc) {
|
|
|
|
return font_desc->tab_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-27 11:56:02 +02:00
|
|
|
if (!font) {
|
|
|
|
/* The font was able to load when initially loaded using renderer.load.font.
|
|
|
|
If now is no longer available we just abort the application. */
|
|
|
|
fprintf(stderr, "Fatal error: unable to load font %s. Application will abort.\n",
|
|
|
|
font_desc->filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-04-26 15:56:18 +02:00
|
|
|
font_desc->cache[index].font = font;
|
|
|
|
font_desc->cache[index].scale = scale;
|
2021-04-24 10:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RenFont *font_desc_get_font_at_scale(FontDesc *font_desc, int scale) {
|
|
|
|
int index = -1;
|
2021-04-26 15:56:18 +02:00
|
|
|
for (int i = 0; i < font_desc->cache_length; i++) {
|
|
|
|
if (font_desc->cache[i].scale == scale) {
|
2021-04-24 10:21:34 +02:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index < 0) {
|
2021-04-26 15:56:18 +02:00
|
|
|
index = font_desc->cache_length;
|
|
|
|
if (index < FONT_CACHE_ARRAY_MAX) {
|
2021-04-24 10:21:34 +02:00
|
|
|
load_scaled_font(font_desc, index, scale);
|
2021-04-26 15:56:18 +02:00
|
|
|
font_desc->cache_length = index + 1;
|
2021-04-24 10:21:34 +02:00
|
|
|
} else {
|
|
|
|
// FIXME: should not print into the stderr or stdout.
|
|
|
|
fprintf(stderr, "Warning: max array of font scale reached.\n");
|
2021-04-26 15:56:18 +02:00
|
|
|
index = (font_desc->cache_last_index == 0 ? 1 : 0);
|
|
|
|
ren_free_font(font_desc->cache[index].font);
|
2021-04-24 10:21:34 +02:00
|
|
|
load_scaled_font(font_desc, index, scale);
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 15:56:18 +02:00
|
|
|
font_desc->cache_last_index = index;
|
|
|
|
return font_desc->cache[index].font;
|
2021-04-24 10:21:34 +02:00
|
|
|
}
|
|
|
|
|