From 0ffd63fed9dcfb60be7378b6f53f0f1d011fd26c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 24 May 2025 17:01:01 -0700 Subject: Tweaks --- font/font.h | 8 +++++--- fontbaker/fontbaker.c | 31 +++++++++++++++++++------------ src/input.c | 2 +- src/layout.c | 5 ++++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/font/font.h b/font/font.h index 13ff1c3..9d37e64 100644 --- a/font/font.h +++ b/font/font.h @@ -5,7 +5,7 @@ static const unsigned char FontGlyphStart = 32; // Space. static const unsigned char FontGlyphEnd = 127; // One past tilde. -static const int FontAtlasNumGlyphs = FontGlyphEnd - FontGlyphStart; +static const unsigned int FontAtlasNumGlyphs = FontGlyphEnd - FontGlyphStart; /// Font atlas header. typedef struct FontHeader { @@ -32,8 +32,10 @@ FontAtlas* LoadFontAtlas(const char* path); static inline const unsigned char* FontGetGlyph( const FontAtlas* atlas, unsigned char c) { assert(atlas); - const int index = c - FontGlyphStart; - assert(index >= 0); + unsigned index = c - FontGlyphStart; + if (index >= FontAtlasNumGlyphs) { + index = '?' - FontGlyphStart; + } assert(index < FontAtlasNumGlyphs); return atlas->pixels + index * (atlas->header.glyph_width * atlas->header.glyph_height); diff --git a/fontbaker/fontbaker.c b/fontbaker/fontbaker.c index a11aa88..25d629d 100644 --- a/fontbaker/fontbaker.c +++ b/fontbaker/fontbaker.c @@ -37,17 +37,31 @@ static void RenderGlyph( printf("\n"); } +static char GetSymbol(unsigned char pixel) { + if (pixel >= 128) { + return '#'; + } else if (pixel >= 32) { + return '*'; + } else if (pixel > 0) { + return '.'; + } else { + return ' '; + } +} + static void RenderText(const FontAtlas* atlas, const char* text) { + assert(atlas); assert(text); - const int glyph_width = atlas->header.glyph_width; - const int glyph_height = atlas->header.glyph_height; - const int glyph_size = glyph_width * glyph_height; + const int glyph_width = atlas->header.glyph_width; + const int glyph_height = atlas->header.glyph_height; + const int glyph_size = glyph_width * glyph_height; + const size_t text_length = strlen(text); for (int y = 0; y < glyph_height; ++y) { printf("%02d ", y); - for (size_t i = 0; i < strlen(text); ++i) { + for (size_t i = 0; i < text_length; ++i) { const char c = text[i]; const int glyph_offset = (c - FontGlyphStart) * glyph_size; const int row_offset = (y * glyph_width); @@ -55,14 +69,7 @@ static void RenderText(const FontAtlas* atlas, const char* text) { const unsigned char* pixel = atlas->pixels + glyph_offset + row_offset; for (int x = 0; x < glyph_width; ++x, ++pixel) { - unsigned char p = ' '; - if (*pixel >= 128) { - p = '#'; - } else if (*pixel >= 32) { - p = '*'; - } else if (*pixel > 0) { - p = '.'; - } + unsigned char p = GetSymbol(*pixel); printf("%c", p); } } diff --git a/src/input.c b/src/input.c index 20551a6..4461c8b 100644 --- a/src/input.c +++ b/src/input.c @@ -52,7 +52,7 @@ static void GetTableRowColAtXy( for (col = 0; (col < table->cols) && (x > table->widths[col]); ++col) { x -= table->widths[col]; } - // 0 is the header and we want to map the first row to 0, so -1. + // 0 is the header, and we want to map the first row to 0, so -1. row = table->offset + ((p.y - widget->rect.y) / g_ui.font->header.glyph_height) - 1; // Out-of-bounds check. diff --git a/src/layout.c b/src/layout.c index f83976f..62e1876 100644 --- a/src/layout.c +++ b/src/layout.c @@ -6,6 +6,8 @@ #include +#define Min(a, b) ((a) < (b) ? (a) : (b)) + static void ResizeTable(uiTable* table, int width, int height) { assert(table); @@ -16,7 +18,8 @@ static void ResizeTable(uiTable* table, int width, int height) { table->height = height; // Compute the number of rows that are visible at once. - table->num_visible_rows = height / g_ui.font->header.glyph_height; + table->num_visible_rows = + Min(table->rows, height / g_ui.font->header.glyph_height); assert(table->num_visible_rows <= table->rows); // Determine if there is vertical overflow. This determines whether we need to -- cgit v1.2.3