summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-05-24 17:01:01 -0700
committer3gg <3gg@shellblade.net>2025-05-24 17:01:01 -0700
commit0ffd63fed9dcfb60be7378b6f53f0f1d011fd26c (patch)
tree4a3b0f541b91349955b5b58f24b47c8b38f287f0
parent68a3532728b55b73d8bcadb8ccfc1d9396346cd2 (diff)
Tweaksmain
-rw-r--r--font/font.h8
-rw-r--r--fontbaker/fontbaker.c31
-rw-r--r--src/input.c2
-rw-r--r--src/layout.c5
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 @@
5 5
6static const unsigned char FontGlyphStart = 32; // Space. 6static const unsigned char FontGlyphStart = 32; // Space.
7static const unsigned char FontGlyphEnd = 127; // One past tilde. 7static const unsigned char FontGlyphEnd = 127; // One past tilde.
8static const int FontAtlasNumGlyphs = FontGlyphEnd - FontGlyphStart; 8static const unsigned int FontAtlasNumGlyphs = FontGlyphEnd - FontGlyphStart;
9 9
10/// Font atlas header. 10/// Font atlas header.
11typedef struct FontHeader { 11typedef struct FontHeader {
@@ -32,8 +32,10 @@ FontAtlas* LoadFontAtlas(const char* path);
32static inline const unsigned char* FontGetGlyph( 32static inline const unsigned char* FontGetGlyph(
33 const FontAtlas* atlas, unsigned char c) { 33 const FontAtlas* atlas, unsigned char c) {
34 assert(atlas); 34 assert(atlas);
35 const int index = c - FontGlyphStart; 35 unsigned index = c - FontGlyphStart;
36 assert(index >= 0); 36 if (index >= FontAtlasNumGlyphs) {
37 index = '?' - FontGlyphStart;
38 }
37 assert(index < FontAtlasNumGlyphs); 39 assert(index < FontAtlasNumGlyphs);
38 return atlas->pixels + 40 return atlas->pixels +
39 index * (atlas->header.glyph_width * atlas->header.glyph_height); 41 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(
37 printf("\n"); 37 printf("\n");
38} 38}
39 39
40static char GetSymbol(unsigned char pixel) {
41 if (pixel >= 128) {
42 return '#';
43 } else if (pixel >= 32) {
44 return '*';
45 } else if (pixel > 0) {
46 return '.';
47 } else {
48 return ' ';
49 }
50}
51
40static void RenderText(const FontAtlas* atlas, const char* text) { 52static void RenderText(const FontAtlas* atlas, const char* text) {
53 assert(atlas);
41 assert(text); 54 assert(text);
42 55
43 const int glyph_width = atlas->header.glyph_width; 56 const int glyph_width = atlas->header.glyph_width;
44 const int glyph_height = atlas->header.glyph_height; 57 const int glyph_height = atlas->header.glyph_height;
45 const int glyph_size = glyph_width * glyph_height; 58 const int glyph_size = glyph_width * glyph_height;
59 const size_t text_length = strlen(text);
46 60
47 for (int y = 0; y < glyph_height; ++y) { 61 for (int y = 0; y < glyph_height; ++y) {
48 printf("%02d ", y); 62 printf("%02d ", y);
49 63
50 for (size_t i = 0; i < strlen(text); ++i) { 64 for (size_t i = 0; i < text_length; ++i) {
51 const char c = text[i]; 65 const char c = text[i];
52 const int glyph_offset = (c - FontGlyphStart) * glyph_size; 66 const int glyph_offset = (c - FontGlyphStart) * glyph_size;
53 const int row_offset = (y * glyph_width); 67 const int row_offset = (y * glyph_width);
@@ -55,14 +69,7 @@ static void RenderText(const FontAtlas* atlas, const char* text) {
55 const unsigned char* pixel = atlas->pixels + glyph_offset + row_offset; 69 const unsigned char* pixel = atlas->pixels + glyph_offset + row_offset;
56 70
57 for (int x = 0; x < glyph_width; ++x, ++pixel) { 71 for (int x = 0; x < glyph_width; ++x, ++pixel) {
58 unsigned char p = ' '; 72 unsigned char p = GetSymbol(*pixel);
59 if (*pixel >= 128) {
60 p = '#';
61 } else if (*pixel >= 32) {
62 p = '*';
63 } else if (*pixel > 0) {
64 p = '.';
65 }
66 printf("%c", p); 73 printf("%c", p);
67 } 74 }
68 } 75 }
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(
52 for (col = 0; (col < table->cols) && (x > table->widths[col]); ++col) { 52 for (col = 0; (col < table->cols) && (x > table->widths[col]); ++col) {
53 x -= table->widths[col]; 53 x -= table->widths[col];
54 } 54 }
55 // 0 is the header and we want to map the first row to 0, so -1. 55 // 0 is the header, and we want to map the first row to 0, so -1.
56 row = table->offset + 56 row = table->offset +
57 ((p.y - widget->rect.y) / g_ui.font->header.glyph_height) - 1; 57 ((p.y - widget->rect.y) / g_ui.font->header.glyph_height) - 1;
58 // Out-of-bounds check. 58 // 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 @@
6 6
7#include <cassert.h> 7#include <cassert.h>
8 8
9#define Min(a, b) ((a) < (b) ? (a) : (b))
10
9static void ResizeTable(uiTable* table, int width, int height) { 11static void ResizeTable(uiTable* table, int width, int height) {
10 assert(table); 12 assert(table);
11 13
@@ -16,7 +18,8 @@ static void ResizeTable(uiTable* table, int width, int height) {
16 table->height = height; 18 table->height = height;
17 19
18 // Compute the number of rows that are visible at once. 20 // Compute the number of rows that are visible at once.
19 table->num_visible_rows = height / g_ui.font->header.glyph_height; 21 table->num_visible_rows =
22 Min(table->rows, height / g_ui.font->header.glyph_height);
20 assert(table->num_visible_rows <= table->rows); 23 assert(table->num_visible_rows <= table->rows);
21 24
22 // Determine if there is vertical overflow. This determines whether we need to 25 // Determine if there is vertical overflow. This determines whether we need to