diff --git a/libs/fonts/include/psemek/fonts/font.hpp b/libs/fonts/include/psemek/fonts/font.hpp index 1b73bd7c..c3dec36c 100644 --- a/libs/fonts/include/psemek/fonts/font.hpp +++ b/libs/fonts/include/psemek/fonts/font.hpp @@ -85,6 +85,8 @@ namespace psemek::fonts::inline v1 }; std::unique_ptr make_default_monospace_9x12_font(); + std::unique_ptr make_default_monospace_5x7_font(); + std::unique_ptr make_default_9x12_font(); std::unique_ptr make_default_10x12_bold_font(); diff --git a/libs/fonts/include/psemek/fonts/monospace_font.hpp b/libs/fonts/include/psemek/fonts/monospace_font.hpp index 046474ce..8918a23b 100644 --- a/libs/fonts/include/psemek/fonts/monospace_font.hpp +++ b/libs/fonts/include/psemek/fonts/monospace_font.hpp @@ -8,7 +8,7 @@ namespace psemek::fonts struct monospace_font : font { - monospace_font(character_range range, std::string_view name, math::vector size, gfx::texture_2d atlas, std::vector> texcoords); + monospace_font(character_range range, std::string_view name, math::vector size, math::vector offset, math::vector margin, gfx::texture_2d atlas, std::vector> texcoords); font_type type() const override { return font_type::bitmap; } @@ -30,6 +30,8 @@ namespace psemek::fonts character_range range_; std::string_view name_; math::vector size_; + math::vector offset_; + math::vector margin_; gfx::texture_2d atlas_; std::vector> texcoords_; diff --git a/libs/fonts/source/default_fonts.cpp b/libs/fonts/source/default_fonts.cpp index 0ede52fc..7b9246d5 100644 --- a/libs/fonts/source/default_fonts.cpp +++ b/libs/fonts/source/default_fonts.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -45,7 +46,40 @@ namespace psemek::fonts::inline v1 texcoords[c - range.begin] = b; } - return std::make_unique(range, name, size, std::move(atlas), std::move(texcoords)); + // NB: Margin is baked into the glyphs + return std::make_unique(range, name, size, math::vector{0, 0}, math::vector{0, 0}, std::move(atlas), std::move(texcoords)); + } + + std::unique_ptr make_default_monospace_5x7_font() + { + character_range range{32, 128}; + std::string_view name = "default_monospace_5x7"; + math::vector size{5, 9}; + + gfx::texture_2d atlas = gfx::texture_2d::from_pixmap(gfx::read_image(io::memory_istream{gfx::resource::font_5x7_png.data})); + atlas.nearest_filter(); + atlas.clamp(); + gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_G, gl::RED); + gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_B, gl::RED); + gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_A, gl::RED); + + std::vector> texcoords(range.end - range.begin); + + for (char32_t c = range.begin; c < range.end; ++c) + { + int const row = 16; + int x = (c - range.begin) % row; + int y = (c - range.begin) / row; + + math::box b; + b[0].min = x * 7 + 1; + b[0].max = b[0].min + 5; + b[1].min = y * 11 + 1; + b[1].max = b[1].min + 9; + texcoords[c - range.begin] = b; + } + + return std::make_unique(range, name, size, math::vector{0, -2}, math::vector{1, 1}, std::move(atlas), std::move(texcoords)); } static std::unique_ptr make_default_font(std::string_view name, math::vector const & size, rs::resource const & atlas_resource, rs::resource const & glyphs_resource) diff --git a/libs/fonts/source/monospace_font.cpp b/libs/fonts/source/monospace_font.cpp index 920f7b3e..1fd30ddc 100644 --- a/libs/fonts/source/monospace_font.cpp +++ b/libs/fonts/source/monospace_font.cpp @@ -6,10 +6,12 @@ namespace psemek::fonts { - monospace_font::monospace_font(character_range range, std::string_view name, math::vector size, gfx::texture_2d atlas, std::vector> texcoords) + monospace_font::monospace_font(character_range range, std::string_view name, math::vector size, math::vector offset, math::vector margin, gfx::texture_2d atlas, std::vector> texcoords) : range_{range} , name_{name} , size_{size} + , offset_{offset} + , margin_{margin} , atlas_{std::move(atlas)} , texcoords_{std::move(texcoords)} { @@ -53,7 +55,9 @@ namespace psemek::fonts { char32_t const unknown = supports_character(options.unknown_character) ? options.unknown_character : '?'; math::vector const size = math::cast(this->size()) * options.scale; - math::vector const advance = math::pointwise_mult(advance_dir(options.direction), size); + math::vector const margin = math::cast(margin_) * options.scale; + math::vector const offset = math::cast(offset_) * options.scale; + math::vector const advance = math::pointwise_mult(advance_dir(options.direction), size + margin); std::vector result; @@ -70,6 +74,7 @@ namespace psemek::fonts g.character = supports_character(c) ? c : unknown; g.position = {{{pen[0], pen[0] + size[0]}, {pen[1], pen[1] + size[1]}}}; } + g.position += offset; result.push_back(g); pen += advance; } diff --git a/libs/gfx/CMakeLists.txt b/libs/gfx/CMakeLists.txt index 5864a1fd..7d5101e6 100644 --- a/libs/gfx/CMakeLists.txt +++ b/libs/gfx/CMakeLists.txt @@ -12,6 +12,7 @@ if(NOT KHR_PLATFORM_FILE) endif() psemek_add_public_resources(psemek-gfx + resources/font_5x7.png psemek/gfx/resource/font_5x7_png resources/font_9x12.png psemek/gfx/resource/font_9x12_png resources/font_10x12_bold.png psemek/gfx/resource/font_10x12_bold_png ) diff --git a/libs/gfx/include/psemek/gfx/painter.hpp b/libs/gfx/include/psemek/gfx/painter.hpp index d22cbb24..8dd223aa 100644 --- a/libs/gfx/include/psemek/gfx/painter.hpp +++ b/libs/gfx/include/psemek/gfx/painter.hpp @@ -22,6 +22,7 @@ namespace psemek::gfx enum class font { + font_5x7, font_9x12, }; @@ -41,7 +42,7 @@ namespace psemek::gfx struct text_options { - font f = font::font_9x12; + font f = font::font_5x7; math::vector scale = {1.f, 1.f}; x_align x = x_align::center; y_align y = y_align::center; diff --git a/libs/gfx/resources/font_5x7.png b/libs/gfx/resources/font_5x7.png new file mode 100644 index 00000000..f480e528 Binary files /dev/null and b/libs/gfx/resources/font_5x7.png differ