From d2448d151edad03fb2ca2274cb02426f5fb69582 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 19 May 2024 11:39:07 -0400 Subject: add font loading and rendering to daguerre --- libraries/daguerre/source/daguerre.cpp | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'libraries/daguerre/source') 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 &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 &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(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; + + } + } -- cgit v1.2.3