diff options
author | Benji Dial <benji@benjidial.net> | 2024-05-19 11:39:07 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-05-19 11:39:07 -0400 |
commit | d2448d151edad03fb2ca2274cb02426f5fb69582 (patch) | |
tree | f5e92bf96fe3a87068a9365d693456d258c22180 /libraries/daguerre/source | |
parent | 17c1bd441de115720d7c6758550c49c1b4689900 (diff) | |
download | hilbert-os-d2448d151edad03fb2ca2274cb02426f5fb69582.tar.gz |
add font loading and rendering to daguerre
Diffstat (limited to 'libraries/daguerre/source')
-rw-r--r-- | libraries/daguerre/source/daguerre.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libraries/daguerre/source/daguerre.cpp b/libraries/daguerre/source/daguerre.cpp index 7c13a88..2f7fe4d 100644 --- a/libraries/daguerre/source/daguerre.cpp +++ b/libraries/daguerre/source/daguerre.cpp @@ -26,6 +26,7 @@ namespace daguerre { return n; } + //only supports p6 format bool try_load_ppm(std::FILE *input, image<rgb24> &into) { char header[3]; @@ -62,4 +63,48 @@ namespace daguerre { } + //assumes the font is in psf2 format, and has a unicode table + bool try_load_psf(std::FILE *input, fixed_bitmap_font<bool> &into) { + + uint32_t header[8]; + if (std::fread(header, 4, 8, input) != 8) + return false; + + const uint32_t glyphs_start = header[2]; + const uint32_t glyph_count = header[4]; + const uint32_t glyph_length = header[5]; + into.height = header[6]; + into.width = header[7]; + + const uint32_t unicode_start = glyphs_start + glyph_count * glyph_length; + std::fseek(input, unicode_start, SEEK_SET); + + uint32_t indices[128]; + + for (uint32_t index = 0; index < glyph_count; ++index) { + uint8_t ch; + std::fread(&ch, 1, 1, input); + if (ch < 128) + indices[ch] = index; + do + std::fread(&ch, 1, 1, input); + while (ch != 0xff); + } + + for (uint8_t ch = 0; ch < 128; ++ch) { + std::fseek(input, glyphs_start + glyph_length * indices[ch], SEEK_SET); + into.glyphs[ch] = image<bool>(into.width, into.height); + for (unsigned h = 0; h < into.height; ++h) + for (unsigned wb = 0; wb < into.width; wb += 8) { + uint8_t byte; + std::fread(&byte, 1, 1, input); + for (unsigned x = 0; x < 8 && wb + x < into.width; ++x) + into.glyphs[ch].set(wb + x, h, (byte >> (7 - x)) & 1); + } + } + + return true; + + } + } |