diff options
author | 3gg <3gg@shellblade.net> | 2024-05-04 16:44:28 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-05-04 16:52:53 -0700 |
commit | af641426fad35cd857c1f14bda523db3d85a70cd (patch) | |
tree | 8a219b03aef0c80cac56cd6b88571a7a6988b35b /font/font.h |
Initial commit.
Diffstat (limited to 'font/font.h')
-rw-r--r-- | font/font.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/font/font.h b/font/font.h new file mode 100644 index 0000000..13ff1c3 --- /dev/null +++ b/font/font.h | |||
@@ -0,0 +1,40 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <assert.h> | ||
4 | #include <stdint.h> | ||
5 | |||
6 | static const unsigned char FontGlyphStart = 32; // Space. | ||
7 | static const unsigned char FontGlyphEnd = 127; // One past tilde. | ||
8 | static const int FontAtlasNumGlyphs = FontGlyphEnd - FontGlyphStart; | ||
9 | |||
10 | /// Font atlas header. | ||
11 | typedef struct FontHeader { | ||
12 | uint16_t glyph_width; | ||
13 | uint16_t glyph_height; | ||
14 | uint16_t num_glyphs; | ||
15 | } FontHeader; | ||
16 | |||
17 | /// Font atlas. | ||
18 | /// | ||
19 | /// Pixels are stored in "glyph-major" order. First the rows of the first glyph, | ||
20 | /// followed by the rows of the second glyph, etc. Thus, pixels should be viewed | ||
21 | /// as a linear array, not a 2d array. This arrangement allows an application to | ||
22 | /// render glyphs by scanning atlas memory linearly. | ||
23 | typedef struct FontAtlas { | ||
24 | FontHeader header; | ||
25 | unsigned char pixels[1]; | ||
26 | } FontAtlas; | ||
27 | |||
28 | /// Load a font atlas from a file. | ||
29 | FontAtlas* LoadFontAtlas(const char* path); | ||
30 | |||
31 | /// Get the glyph into the atlas. | ||
32 | static inline const unsigned char* FontGetGlyph( | ||
33 | const FontAtlas* atlas, unsigned char c) { | ||
34 | assert(atlas); | ||
35 | const int index = c - FontGlyphStart; | ||
36 | assert(index >= 0); | ||
37 | assert(index < FontAtlasNumGlyphs); | ||
38 | return atlas->pixels + | ||
39 | index * (atlas->header.glyph_width * atlas->header.glyph_height); | ||
40 | } | ||