Fonts are guaranteed to preserve space symbols when shaping

This commit is contained in:
Nikita Lisitsa 2021-02-26 12:19:35 +03:00
parent e754da5f46
commit be0fb7e9eb
2 changed files with 19 additions and 3 deletions

View file

@ -16,7 +16,7 @@ namespace psemek::ui
geom::vector<int, 2> 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<character_range> supported_characters() const override { return {range_}; }
std::vector<glyph> shape(std::string_view str, shape_options const & options) const override;

View file

@ -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<glyph> 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;
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];
}