#include #include namespace psemek::ui { kerned_font::kerned_font(std::string_view name, geom::vector size, int baseline_offset, gfx::texture_2d atlas, std::unordered_map glyphs) : name_{name} , size_{size} , baseline_offset_{baseline_offset} , atlas_{std::move(atlas)} , glyphs_{std::move(glyphs)} { if (!supports_character('?')) throw std::runtime_error("Kerned font must support '?' character"); if (!supports_character(' ')) throw std::runtime_error("Kerned font must support ' ' character"); std::vector chars; for (auto const & g : glyphs_) chars.push_back(g.first); std::sort(chars.begin(), chars.end()); for (auto c : chars) { if (!ranges_.empty() && ranges_.back().end == c) ++ranges_.back().end; else ranges_.push_back({c, c + 1}); } } static geom::vector advance_dir(shape_options::direction_t direction) { switch (direction) { case shape_options::left_to_right: return {1.f, 0.f}; case shape_options::right_to_left: return {-1.f, 0.f}; case shape_options::top_to_bottom: return {0.f, 1.f}; case shape_options::bottom_to_top: return {0.f, -1.f}; } return {1.f, 0.f}; } bool kerned_font::supports_character(char32_t c) const { return std::isspace(c) || glyphs_.contains(c); } std::vector kerned_font::shape(std::string_view str, shape_options const & options) const { return shape_impl(util::utf8_range(str), options); } std::vector kerned_font::shape(std::u32string_view str, shape_options const & options) const { return shape_impl(str, options); } template std::vector kerned_font::shape_impl(String const & str, shape_options const & options) const { char32_t const unknown = supports_character(options.unknown_character) ? options.unknown_character : '?'; geom::vector const advance_mask = advance_dir(options.direction); std::vector result; geom::vector pos{0.f, (size_[1] - baseline_offset_) * options.scale}; for (char32_t c : str) { if (!supports_character(c)) c = unknown; char32_t cc = c; if (std::isspace(c)) cc = ' '; auto const & data = glyphs_.at(cc); glyph g; g.character = c; g.position[0].min = pos[0] + data.offset_x * options.scale; g.position[1].min = pos[1] - (data.offset_y + data.size_y) * options.scale; g.position[0].max = pos[0] + (data.offset_x + data.size_x) * options.scale; g.position[1].max = pos[1] - data.offset_y * options.scale; result.push_back(g); geom::vector advance{data.advance * options.scale, size_[1] * options.scale}; pos += geom::pointwise_mult(advance_mask, advance); } return result; } std::optional> kerned_font::texcoords(char32_t c) const { if (std::isspace(c)) c = ' '; auto it = glyphs_.find(c); if (it == glyphs_.end()) return std::nullopt; geom::box box; box[0].min = it->second.start_x; box[1].min = it->second.start_y; box[0].max = box[0].min + it->second.size_x; box[1].max = box[1].min + it->second.size_y; return box; } }