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/include/daguerre.hpp | 68 ++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 5 deletions(-) (limited to 'libraries/daguerre/include') diff --git a/libraries/daguerre/include/daguerre.hpp b/libraries/daguerre/include/daguerre.hpp index 62d10f0..c236cb9 100644 --- a/libraries/daguerre/include/daguerre.hpp +++ b/libraries/daguerre/include/daguerre.hpp @@ -14,6 +14,16 @@ namespace daguerre { uint8_t b; }; + static inline void default_overlay(hilbert_color &dest, const rgb24 &src) { + dest = __euler_encode_color(src.r, src.g, src.b); + } + + static inline void default_overlay(rgb24 &dest, const bool &src) { + dest.r = src ? 255 : 0; + dest.g = src ? 255 : 0; + dest.b = src ? 255 : 0; + } + template class image { @@ -100,7 +110,7 @@ namespace daguerre { //from [from_x, from_x + width) x [from_y, from_y + height). template < class to_color_t, class from_color_t, - void overlay(to_color_t &dest, const from_color_t &src)> + void overlay(to_color_t &dest, const from_color_t &src) = default_overlay> void overlay_region( image &to, unsigned to_x, unsigned to_y, const image &from, unsigned from_x, unsigned from_y, @@ -116,12 +126,60 @@ namespace daguerre { } - static inline void encode(hilbert_color &dest, const rgb24 &src) { - dest = __euler_encode_color(src.r, src.g, src.b); - } - image get_hilbert_framebuffer(); bool try_load_ppm(std::FILE *input, image &into); + static inline bool try_load_ppm(const char *path, image &into) { + std::FILE *f = std::fopen(path, "r"); + if (!f) + return false; + bool success = try_load_ppm(f, into); + std::fclose(f); + return success; + } + + //TODO: unicode + template + class fixed_bitmap_font { + + public: + unsigned width; + unsigned height; + image glyphs[128]; + + template + void overlay_text( + image &target, unsigned x, + unsigned y, const char *text) const { + + while (1) { + uint8_t ch = (uint8_t)*text; + if (ch == 0) + return; + if (ch < 128) { + overlay_region( + target, x, y, glyphs[ch], 0, 0, width, height); + x += width; + } + ++text; + } + + } + + }; + + bool try_load_psf(std::FILE *input, fixed_bitmap_font &into); + + static inline bool try_load_psf( + const char *path, fixed_bitmap_font &into) { + std::FILE *f = std::fopen(path, "r"); + if (!f) + return false; + bool success = try_load_psf(f, into); + std::fclose(f); + return success; + } + } -- cgit v1.2.3