Commit: d538eba43dab4578427d7ada3db0542b50e54e22
Parent: edc98a4478a6aa44b1916586cf1f30d5f30f081a
Author: Randy Palamar
Date: Tue, 12 Nov 2024 06:23:51 -0700
font: try to account for negative x0
really this font code needs a cleanup pass
Diffstat:
M | font.c | | | 29 | +++++++++++++++++------------ |
M | util.h | | | 8 | ++++++-- |
2 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/font.c b/font.c
@@ -1,4 +1,7 @@
/* See LICENSE for copyright details */
+#define FONT_H_PADDING 1
+#define FONT_BASELINE_EXTRA 1
+
static b32
init_font(Font *f, FontDesc *fdesc)
{
@@ -213,14 +216,14 @@ render_glyph(Arena *a, FontAtlas *fa, u32 cp, u32 font_id, enum face_style style
stbtt_GetGlyphBitmapBoxSubpixel(&f->font_info, glyph_idx, scale, scale, 0, 0,
&x0, &y0, &x1, &y1);
- i32 height = y1 - y0;
- i32 width = x1 - x0;
+ i32 height = y1 - y0;
+ i32 width = x1 - x0;
stbtt_GetGlyphHMetrics(&f->font_info, glyph_idx, &advance, &left_bearing);
cg->advance = scale * advance;
cg->height = height;
cg->width = width;
- cg->x0 = x0;
+ cg->x0 = x0 + FONT_H_PADDING;
cg->y0 = y0;
u8 *render_buf = alloc(a, u8, width * height);
@@ -275,10 +278,12 @@ render_glyph(Arena *a, FontAtlas *fa, u32 cp, u32 font_id, enum face_style style
for (u32 i = 0; i < cg->tile_count; i++)
gc->occupied_tiles[tile_index + i] = 1;
+ ASSERT(x0 + FONT_H_PADDING >= 0);
+
rgba_bitmap = alloc(a, u32, out_size);
u32 out_y = (fa->info.h + y0 - fa->info.baseline) * out_width;
for (i32 i = 0; i < height; i++) {
- u32 out_x = x0;
+ u32 out_x = x0 + FONT_H_PADDING;
for (i32 j = 0; j < width; j++) {
/* TODO: handled coloured glyphs */
u32 pixel = 0;
@@ -316,8 +321,10 @@ font_atlas_update(FontAtlas *fa, iv2 glyph_bitmap_dim)
stbtt_GetFontBoundingBox(&font->font_info, &x0, &y0, &x1, &y1);
f32 scale = font->stbtt_scale;
fa->info.h = (u32)(scale * (y1 - y0)) + 1;
- fa->info.w = (u32)(scale * (x1 - x0)) + 1;
- fa->info.baseline = (u32)(-scale * y0) + 1;
+ fa->info.w = (u32)(scale * (x1 - x0)) + 1 + FONT_H_PADDING;
+ fa->info.baseline = (u32)(-scale * y0) + 1 + FONT_BASELINE_EXTRA;
+
+ ASSERT(FONT_H_PADDING >= (i32)ABS(x0 * scale));
/* NOTE: sometimes the reported width is wrong; try '_' which is often the widest char */
i32 glyph_idx = stbtt_FindGlyphIndex(&font->font_info, '_');
@@ -354,11 +361,9 @@ shift_font_sizes(FontAtlas *fa, i32 size_delta)
static void
init_fonts(FontAtlas *fa, Arena *a, iv2 glyph_bitmap_dim)
{
- u32 n_fonts = ARRAY_COUNT(g_fonts);
-
fa->nfonts = 0;
- fa->fonts = alloc(a, typeof(*fa->fonts), n_fonts);
- for (u32 j = 0; j < n_fonts; j++) {
+ fa->fonts = alloc(a, typeof(*fa->fonts), ARRAY_COUNT(g_fonts));
+ for (u32 j = 0; j < ARRAY_COUNT(g_fonts); j++) {
b32 result = 0;
for (u32 i = 0; i < FS_LAST; i++) {
if (!g_fonts[j][i].path)
@@ -381,8 +386,8 @@ init_fonts(FontAtlas *fa, Arena *a, iv2 glyph_bitmap_dim)
i32 x0, x1, y0, y1;
f32 scale = stbtt_ScaleForPixelHeight(&f->font_info, MIN_FONT_SIZE);
stbtt_GetFontBoundingBox(&f->font_info, &x0, &y0, &x1, &y1);
- i32 h = scale * (y1 - y0) + 0.5;
- i32 w = scale * (x1 - x0) + 0.5;
+ f32 h = (u32)(scale * (y1 - y0)) + 1;
+ f32 w = (u32)(scale * (x1 - x0)) + 1 + FONT_H_PADDING;
i32 max_tiles_x = glyph_bitmap_dim.x / w;
i32 max_tiles_y = glyph_bitmap_dim.y / h;
diff --git a/util.h b/util.h
@@ -11,6 +11,10 @@
#define asm __asm__
#endif
+#ifndef typeof
+#define typeof __typeof__
+#endif
+
#ifndef static_assert
#define static_assert _Static_assert
#endif
@@ -356,8 +360,8 @@ typedef struct {
typedef Font FontFamily[FS_LAST];
typedef struct {
- u32 h, w;
- i32 baseline;
+ u32 h, w;
+ i32 baseline;
} FontInfo;
typedef struct {