From be0fb7e9eb6d47f0c8a7ada18e1b5951b3fb320e Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 26 Feb 2021 12:19:35 +0300 Subject: [PATCH] Fonts are guaranteed to preserve space symbols when shaping --- libs/ui/include/psemek/ui/monospace_font.hpp | 2 +- libs/ui/source/monospace_font.cpp | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libs/ui/include/psemek/ui/monospace_font.hpp b/libs/ui/include/psemek/ui/monospace_font.hpp index 30a01e87..ca251967 100644 --- a/libs/ui/include/psemek/ui/monospace_font.hpp +++ b/libs/ui/include/psemek/ui/monospace_font.hpp @@ -16,7 +16,7 @@ namespace psemek::ui geom::vector size() const override { return size_; } - bool supports_character(char32_t c) const override { return c >= range_.begin && c < range_.end; } + bool supports_character(char32_t c) const override; std::vector supported_characters() const override { return {range_}; } std::vector shape(std::string_view str, shape_options const & options) const override; diff --git a/libs/ui/source/monospace_font.cpp b/libs/ui/source/monospace_font.cpp index 791a1954..dbbab4aa 100644 --- a/libs/ui/source/monospace_font.cpp +++ b/libs/ui/source/monospace_font.cpp @@ -32,6 +32,11 @@ namespace psemek::ui return {1.f, 0.f}; } + bool monospace_font::supports_character(char32_t c) const + { + return std::isspace(c) || (c >= range_.begin && c < range_.end); + } + std::vector monospace_font::shape(std::string_view str, shape_options const & options) const { char32_t const unknown = supports_character(options.unknown_character) ? options.unknown_character : '?'; @@ -44,8 +49,16 @@ namespace psemek::ui for (char32_t c : util::utf8_range(str)) { glyph g; - g.character = supports_character(c) ? c : unknown; - g.position = {{{pos[0], pos[0] + size[0]}, {pos[1], pos[1] + size[1]}}}; + if (std::isspace(c)) + { + g.character = c; + g.position = {{{pos[0], pos[0]}, {pos[1], pos[1]}}}; + } + else + { + g.character = supports_character(c) ? c : unknown; + g.position = {{{pos[0], pos[0] + size[0]}, {pos[1], pos[1] + size[1]}}}; + } result.push_back(g); pos += advance; } @@ -57,6 +70,9 @@ namespace psemek::ui { if (!supports_character(c)) return std::nullopt; + if (std::isspace(c)) + return texcoords_[' ' - range_.begin]; + return texcoords_[c - range_.begin]; }