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 --- applications/init/source/main.cpp | 29 +++++----- euler/include/cstdio | 5 ++ euler/makefile | 2 +- euler/source/io/fseek.cpp | 11 ++++ libraries/daguerre/include/daguerre.hpp | 68 +++++++++++++++++++++-- libraries/daguerre/source/daguerre.cpp | 45 +++++++++++++++ readme.txt | 5 ++ skeleton/assets/readme.txt | 4 ++ skeleton/assets/terminus-bold-18x10.psf | Bin 0 -> 10741 bytes skeleton/assets/terminus-ofl.txt | 94 ++++++++++++++++++++++++++++++++ 10 files changed, 241 insertions(+), 22 deletions(-) create mode 100644 euler/source/io/fseek.cpp create mode 100644 skeleton/assets/terminus-bold-18x10.psf create mode 100644 skeleton/assets/terminus-ofl.txt diff --git a/applications/init/source/main.cpp b/applications/init/source/main.cpp index 6aa85a4..c5df77c 100644 --- a/applications/init/source/main.cpp +++ b/applications/init/source/main.cpp @@ -1,9 +1,9 @@ #include -void overlay_encode( +void alpha_overlay( daguerre::hilbert_color &dest, const daguerre::rgb24 &src) { if (src.r != 0xff || src.g != 0x00 || src.b != 0xff) - daguerre::encode(dest, src); + daguerre::default_overlay(dest, src); } void invert(daguerre::rgb24 &dest, const daguerre::rgb24 &src) { @@ -17,14 +17,15 @@ int main(int, char **) { auto framebuffer = daguerre::get_hilbert_framebuffer(); daguerre::image burden; - std::FILE *burden_file = std::fopen("/assets/burden.ppm", "r"); - daguerre::try_load_ppm(burden_file, burden); - std::fclose(burden_file); + daguerre::try_load_ppm("/assets/burden.ppm", burden); daguerre::image pointer; - std::FILE *pointer_file = std::fopen("/assets/pointer.ppm", "r"); - daguerre::try_load_ppm(pointer_file, pointer); - std::fclose(pointer_file); + daguerre::try_load_ppm("/assets/pointer.ppm", pointer); + + daguerre::fixed_bitmap_font terminus; + daguerre::try_load_psf("/assets/terminus-bold-18x10.psf", terminus); + + terminus.overlay_text<>(burden, 0, 0, "this is a test"); int32_t width = burden.width < framebuffer.width ? burden.width : framebuffer.width; @@ -39,9 +40,7 @@ int main(int, char **) { int32_t old_mouse_y = new_mouse_y; bool was_left_mouse_down = false; - daguerre::overlay_region< - daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>( - framebuffer, x, y, burden, 0, 0, width, height); + daguerre::overlay_region<>(framebuffer, x, y, burden, 0, 0, width, height); while (1) { @@ -96,8 +95,7 @@ int main(int, char **) { daguerre::overlay_region( burden, 0, 0, burden, 0, 0, width, height); - daguerre::overlay_region< - daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>( + daguerre::overlay_region<>( framebuffer, x, y, burden, 0, 0, width, height); anything_changed = true; @@ -105,12 +103,11 @@ int main(int, char **) { } if (anything_changed) { - daguerre::overlay_region< - daguerre::hilbert_color, daguerre::rgb24, daguerre::encode>( + daguerre::overlay_region<>( framebuffer, old_mouse_x + x, old_mouse_y + y, burden, old_mouse_x, old_mouse_y, pointer.width, pointer.height); daguerre::overlay_region< - daguerre::hilbert_color, daguerre::rgb24, overlay_encode>( + daguerre::hilbert_color, daguerre::rgb24, alpha_overlay>( framebuffer, new_mouse_x + x, new_mouse_y + y, pointer, 0, 0, pointer.width, pointer.height); } diff --git a/euler/include/cstdio b/euler/include/cstdio index 27073db..75472c1 100644 --- a/euler/include/cstdio +++ b/euler/include/cstdio @@ -10,6 +10,11 @@ namespace std { FILE *fopen(const char *filename, const char *mode); int fclose(FILE *stream); + int fseek(FILE *stream, long offset, int origin); + #define SEEK_SET 0 + #define SEEK_CUR 2 + #define SEEK_END 1 + size_t fread(void *buffer, size_t size, size_t count, FILE *stream); } diff --git a/euler/makefile b/euler/makefile index 3b8022d..b9d440f 100644 --- a/euler/makefile +++ b/euler/makefile @@ -1,6 +1,6 @@ LIBSTDCPP_SOURCES = euler/stream.cpp strings/strlen.cpp euler/syscall.asm \ euler/entry.cpp io/fopen.cpp euler/gcc.asm memory/delete.cpp euler/heap.cpp \ - memory/new.cpp io/fclose.cpp io/fread.cpp strings/memcpy.cpp + memory/new.cpp io/fclose.cpp io/fread.cpp strings/memcpy.cpp io/fseek.cpp clean: rm -rf build diff --git a/euler/source/io/fseek.cpp b/euler/source/io/fseek.cpp new file mode 100644 index 0000000..3254468 --- /dev/null +++ b/euler/source/io/fseek.cpp @@ -0,0 +1,11 @@ +#include + +namespace std { + + int fseek(FILE *stream, long offset, int origin) { + if (origin < 0 || origin > 2) + return 1; + return stream->try_seek((__euler_seek_from)origin, offset) ? 0 : 2; + } + +} 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; + } + } 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; + + } + } diff --git a/readme.txt b/readme.txt index d48c43c..3cd85d9 100644 --- a/readme.txt +++ b/readme.txt @@ -39,6 +39,11 @@ acknowledgements (any under "dependencies" are downloaded during build): license: https://unsplash.com/license source: https://unsplash.com/photos/selective-focus-photography-snowflakes-9yhy1FXlKwI + - skeleton/assets/terminus-bold-18x10.psf (terminus font, bold, 18x10) + copyright 2020 dimitar toshkov zhekov + license: skeleton/assets/terminus-ofl.txt (sil open font license v1.1) + homepage: https://terminus-font.sourceforge.net/ + everything in the following directories is copyright 2024 benji dial, under the license in license.txt (the isc license): diff --git a/skeleton/assets/readme.txt b/skeleton/assets/readme.txt index 72476c5..3991684 100644 --- a/skeleton/assets/readme.txt +++ b/skeleton/assets/readme.txt @@ -4,3 +4,7 @@ https://unsplash.com/photos/selective-focus-photography-snowflakes-9yhy1FXlKwI. its license can be found online at https://unsplash.com/license. the icon in pointer.ppm is by me :) + +the font in terminus-bold-18x10.psf is the "terminus" font, in bold weight, at +18x10. it can be found only at https://terminus-font.sourceforge.net/ and is +under the license in terminus-ofl.txt (the sil open font license v1.1). diff --git a/skeleton/assets/terminus-bold-18x10.psf b/skeleton/assets/terminus-bold-18x10.psf new file mode 100644 index 0000000..6a7e29c Binary files /dev/null and b/skeleton/assets/terminus-bold-18x10.psf differ diff --git a/skeleton/assets/terminus-ofl.txt b/skeleton/assets/terminus-ofl.txt new file mode 100644 index 0000000..5168372 --- /dev/null +++ b/skeleton/assets/terminus-ofl.txt @@ -0,0 +1,94 @@ +Copyright (C) 2020 Dimitar Toshkov Zhekov, +with Reserved Font Name "Terminus Font". + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. -- cgit v1.2.3