Skip to content

Commit

Permalink
do less math while rendering, divide space size by 2 to look more nat…
Browse files Browse the repository at this point in the history
…ural
  • Loading branch information
ColleagueRiley committed Jul 4, 2024
1 parent 1c41a63 commit 74df521
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions RFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ struct RFont_font {
b8 free_font_memory;
float fheight; /* font height from stb */
float descent; /* font descent */
float numOfLongHorMetrics;
float space_adv;

RFont_glyph glyphs[RFONT_MAX_GLYPHS]; /* glyphs */
size_t glyph_len;
Expand Down Expand Up @@ -478,6 +480,10 @@ RFont_font* RFont_font_init_data(u8* font_data, b8 auto_free) {
font->fheight = ttSHORT(font->info.data + font->info.hhea + 4) - ttSHORT(font->info.data + font->info.hhea + 6);
font->descent = ttSHORT(font->info.data + font->info.hhea + 6);

font->numOfLongHorMetrics = ttUSHORT(font->info.data + font->info.hhea + 34);
font->space_adv = ttSHORT(font->info.data + font->info.hmtx + 4 * (u32)(font->numOfLongHorMetrics - 1));


#ifndef RFONT_NO_GRAPHICS
font->atlas = RFont_create_atlas(RFONT_ATLAS_WIDTH, RFONT_ATLAS_HEIGHT);
#endif
Expand Down Expand Up @@ -616,12 +622,11 @@ RFont_glyph RFont_font_add_char(RFont_font* font, char ch, size_t size) {
free(bitmap);

i32 advanceX;
u16 numOfLongHorMetrics = ttUSHORT(font->info.data + font->info.hhea + 34);

if (glyph->src < numOfLongHorMetrics)

if (glyph->src < font->numOfLongHorMetrics)
advanceX = ttSHORT(font->info.data + font->info.hmtx + 4 * glyph->src);
else
advanceX = ttSHORT(font->info.data + font->info.hmtx + 4 * (numOfLongHorMetrics - 1));
advanceX = ttSHORT(font->info.data + font->info.hmtx + 4 * (u32)(font->numOfLongHorMetrics - 1));

glyph->advance = advanceX * scale;

Expand All @@ -643,8 +648,8 @@ RFont_area RFont_text_area_len(RFont_font* font, const char* text, size_t len, u
char* str;

float scale = (((float)size) / font->fheight);
u16 numOfLongHorMetrics = ttUSHORT(font->info.data + font->info.hhea + 34);
float space_adv = scale * ttSHORT(font->info.data + font->info.hmtx + 4 * (numOfLongHorMetrics - 1));

float space_adv = (scale * font->space_adv) / 2;

for (str = (char*)text; (len == 0 || (size_t)(str - text) < len) && *str; str++) {
if (*str == '\n') {
Expand Down Expand Up @@ -695,8 +700,7 @@ RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, f
char* str;

float scale = (((float)size) / font->fheight);
u16 numOfLongHorMetrics = ttUSHORT(font->info.data + font->info.hhea + 34);
float space_adv = scale * ttSHORT(font->info.data + font->info.hmtx + 4 * (numOfLongHorMetrics - 1));
float space_adv = (scale * font->space_adv) / 2;

y -= (-font->descent * scale);

Expand All @@ -720,15 +724,6 @@ RFont_area RFont_draw_text_len(RFont_font* font, const char* text, size_t len, f
float realX = x + glyph.x1;
float realY = y + glyph.y1;

switch (*str) {
case '(':
case ')':
case ';':
realY -= (size / 16);
break;
default: break;
}

verts[i] = RFONT_GET_WORLD_X((i32)realX, RFont_width);
verts[i + 1] = RFONT_GET_WORLD_Y(realY, RFont_height);
verts[i + 2] = 0;
Expand Down

0 comments on commit 74df521

Please sign in to comment.