Allow setting custom glyphset size (#1542)
* Properly set glyphset size * Rename `MAX_GLYPHSET` to `GLYPHSET_SIZE` * Use more appropriate types for font metrics
This commit is contained in:
parent
ca3acd4ee9
commit
3775032c78
|
@ -16,8 +16,9 @@
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "renwindow.h"
|
#include "renwindow.h"
|
||||||
|
|
||||||
#define MAX_GLYPHSET 256
|
#define MAX_UNICODE 0x100000
|
||||||
#define MAX_LOADABLE_GLYPHSETS 4096
|
#define GLYPHSET_SIZE 256
|
||||||
|
#define MAX_LOADABLE_GLYPHSETS (MAX_UNICODE / GLYPHSET_SIZE)
|
||||||
#define SUBPIXEL_BITMAPS_CACHED 3
|
#define SUBPIXEL_BITMAPS_CACHED 3
|
||||||
|
|
||||||
RenWindow window_renderer = {0};
|
RenWindow window_renderer = {0};
|
||||||
|
@ -37,14 +38,14 @@ static void* check_alloc(void *ptr) {
|
||||||
/************************* Fonts *************************/
|
/************************* Fonts *************************/
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned short x0, x1, y0, y1, loaded;
|
unsigned int x0, x1, y0, y1, loaded;
|
||||||
short bitmap_left, bitmap_top;
|
int bitmap_left, bitmap_top;
|
||||||
float xadvance;
|
float xadvance;
|
||||||
} GlyphMetric;
|
} GlyphMetric;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Surface* surface;
|
SDL_Surface* surface;
|
||||||
GlyphMetric metrics[MAX_GLYPHSET];
|
GlyphMetric metrics[GLYPHSET_SIZE];
|
||||||
} GlyphSet;
|
} GlyphSet;
|
||||||
|
|
||||||
typedef struct RenFont {
|
typedef struct RenFont {
|
||||||
|
@ -128,14 +129,14 @@ static void font_load_glyphset(RenFont* font, int idx) {
|
||||||
for (int j = 0, pen_x = 0; j < bitmaps_cached; ++j) {
|
for (int j = 0, pen_x = 0; j < bitmaps_cached; ++j) {
|
||||||
GlyphSet* set = check_alloc(calloc(1, sizeof(GlyphSet)));
|
GlyphSet* set = check_alloc(calloc(1, sizeof(GlyphSet)));
|
||||||
font->sets[j][idx] = set;
|
font->sets[j][idx] = set;
|
||||||
for (int i = 0; i < MAX_GLYPHSET; ++i) {
|
for (int i = 0; i < GLYPHSET_SIZE; ++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 * GLYPHSET_SIZE);
|
||||||
if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option | FT_LOAD_BITMAP_METRICS_ONLY)
|
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)) {
|
|| font_set_style(&font->face->glyph->outline, j * (64 / SUBPIXEL_BITMAPS_CACHED), font->style) || FT_Render_Glyph(font->face->glyph, render_option)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
FT_GlyphSlot slot = font->face->glyph;
|
FT_GlyphSlot slot = font->face->glyph;
|
||||||
int glyph_width = slot->bitmap.width / byte_width;
|
unsigned int glyph_width = slot->bitmap.width / byte_width;
|
||||||
if (font->antialiasing == FONT_ANTIALIASING_NONE)
|
if (font->antialiasing == FONT_ANTIALIASING_NONE)
|
||||||
glyph_width *= 8;
|
glyph_width *= 8;
|
||||||
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};
|
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};
|
||||||
|
@ -153,8 +154,8 @@ static void font_load_glyphset(RenFont* font, int idx) {
|
||||||
continue;
|
continue;
|
||||||
set->surface = check_alloc(SDL_CreateRGBSurface(0, pen_x, font->max_height, font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 24 : 8, 0, 0, 0, 0));
|
set->surface = check_alloc(SDL_CreateRGBSurface(0, pen_x, font->max_height, font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 24 : 8, 0, 0, 0, 0));
|
||||||
uint8_t* pixels = set->surface->pixels;
|
uint8_t* pixels = set->surface->pixels;
|
||||||
for (int i = 0; i < MAX_GLYPHSET; ++i) {
|
for (int i = 0; i < GLYPHSET_SIZE; ++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 * GLYPHSET_SIZE);
|
||||||
if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option))
|
if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option))
|
||||||
continue;
|
continue;
|
||||||
FT_GlyphSlot slot = font->face->glyph;
|
FT_GlyphSlot slot = font->face->glyph;
|
||||||
|
@ -178,7 +179,7 @@ static void font_load_glyphset(RenFont* font, int idx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int subpixel_idx) {
|
static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int subpixel_idx) {
|
||||||
int idx = (codepoint >> 8) % MAX_LOADABLE_GLYPHSETS;
|
int idx = (codepoint / GLYPHSET_SIZE) % MAX_LOADABLE_GLYPHSETS;
|
||||||
if (!font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx])
|
if (!font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx])
|
||||||
font_load_glyphset(font, idx);
|
font_load_glyphset(font, idx);
|
||||||
return font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx];
|
return font->sets[font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? subpixel_idx : 0][idx];
|
||||||
|
@ -192,7 +193,7 @@ static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFo
|
||||||
bitmap_index += SUBPIXEL_BITMAPS_CACHED;
|
bitmap_index += SUBPIXEL_BITMAPS_CACHED;
|
||||||
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
|
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
|
||||||
*set = font_get_glyphset(fonts[i], codepoint, bitmap_index);
|
*set = font_get_glyphset(fonts[i], codepoint, bitmap_index);
|
||||||
*metric = &(*set)->metrics[codepoint % 256];
|
*metric = &(*set)->metrics[codepoint % GLYPHSET_SIZE];
|
||||||
if ((*metric)->loaded || codepoint < 0xFF)
|
if ((*metric)->loaded || codepoint < 0xFF)
|
||||||
return fonts[i];
|
return fonts[i];
|
||||||
}
|
}
|
||||||
|
@ -323,14 +324,16 @@ void ren_font_free(RenFont* font) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ren_font_group_set_tab_size(RenFont **fonts, int n) {
|
void ren_font_group_set_tab_size(RenFont **fonts, int n) {
|
||||||
|
unsigned int tab_index = '\t' % GLYPHSET_SIZE;
|
||||||
for (int j = 0; j < FONT_FALLBACK_MAX && fonts[j]; ++j) {
|
for (int j = 0; j < FONT_FALLBACK_MAX && fonts[j]; ++j) {
|
||||||
for (int i = 0; i < (fonts[j]->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? SUBPIXEL_BITMAPS_CACHED : 1); ++i)
|
for (int i = 0; i < (fonts[j]->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? SUBPIXEL_BITMAPS_CACHED : 1); ++i)
|
||||||
font_get_glyphset(fonts[j], '\t', i)->metrics['\t'].xadvance = fonts[j]->space_advance * n;
|
font_get_glyphset(fonts[j], '\t', i)->metrics[tab_index].xadvance = fonts[j]->space_advance * n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ren_font_group_get_tab_size(RenFont **fonts) {
|
int ren_font_group_get_tab_size(RenFont **fonts) {
|
||||||
float advance = font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance;
|
unsigned int tab_index = '\t' % GLYPHSET_SIZE;
|
||||||
|
float advance = font_get_glyphset(fonts[0], '\t', 0)->metrics[tab_index].xadvance;
|
||||||
if (fonts[0]->space_advance) {
|
if (fonts[0]->space_advance) {
|
||||||
advance /= fonts[0]->space_advance;
|
advance /= fonts[0]->space_advance;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue