HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font.hpp
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "glyph_metrics.hpp"
8#include "glyph_atlas_info.hpp"
9#include "glyph_ids.hpp"
10#include "font_weight.hpp"
11#include "font_variant.hpp"
12#include "font_metrics.hpp"
13#include "font_char_map.hpp"
14#include "../unicode/module.hpp"
15#include "../i18n/module.hpp"
16#include "../graphic_path/module.hpp"
17#include "../utility/module.hpp"
18#include "../container/module.hpp"
19#include <span>
20#include <vector>
21#include <map>
22#include <string>
23
24namespace hi::inline v1 {
25
30class font {
31public:
37
43
44 bool monospace = false;
45 bool serif = false;
46 bool italic = false;
47 bool condensed = false;
48 font_weight weight = font_weight::Regular;
49 float optical_size = 12.0;
50
55 font_char_map char_map;
56
61
66 hi::font_metrics metrics;
67
71
72 font() = default;
73 virtual ~font() = default;
74 font(font const&) = delete;
75 font& operator=(font const&) = delete;
76 font(font&&) = delete;
77 font& operator=(font&&) = delete;
78
83 [[nodiscard]] virtual bool loaded() const noexcept = 0;
84
88 [[nodiscard]] glyph_id find_glyph(char32_t c) const noexcept
89 {
90 return char_map.find(c);
91 }
92
96 [[nodiscard]] lean_vector<glyph_id> find_glyph(grapheme g) const;
97
106 [[nodiscard]] virtual graphic_path get_path(hi::glyph_id glyph_id) const = 0;
107
114 [[nodiscard]] virtual float get_advance(hi::glyph_id glyph_id) const = 0;
115
124 [[nodiscard]] virtual glyph_metrics get_metrics(hi::glyph_id glyph_id) const = 0;
125
130
134
142
146
155
156 void reserve(size_t count) noexcept
157 {
158 grapheme_advances.reserve(count);
159 glyph_count.reserve(count);
160 glyphs.reserve(count);
161 glyph_positions.reserve(count);
162 glyph_bounding_rectangles.reserve(count);
163 }
164
165 void scale(float s) noexcept
166 {
167 auto M = scale2{s};
168 for (auto& tmp : grapheme_advances) {
169 tmp = s * tmp;
170 }
171 for (auto& tmp : glyph_positions) {
172 tmp = M * tmp;
173 }
174 for (auto& tmp : glyph_bounding_rectangles) {
175 tmp = M * tmp;
176 }
177 }
178 };
179
197 [[nodiscard]] virtual shape_run_result_type shape_run(iso_639 language, iso_15924 script, gstring run) const = 0;
198
199 glyph_atlas_info& atlas_info(glyph_ids const& glyphs) const
200 {
201 if (glyphs.has_num_glyphs<1>()) [[likely]] {
202 hilet index = static_cast<std::size_t>(get<0>(glyphs));
203 if (index >= _single_glyph_atlas_table.size()) [[unlikely]] {
204 _single_glyph_atlas_table.resize(index + 1);
205 }
206 return _single_glyph_atlas_table[index];
207
208 } else {
209 return _multi_glyph_atlas_table[glyphs];
210 }
211 }
212
213 [[nodiscard]] font_variant font_variant() const noexcept
214 {
215 return {weight, italic};
216 }
217
218 [[nodiscard]] friend std::string to_string(font const& rhs) noexcept
219 {
220 return std::format(
221 "{} - {}: style={}{}{}{}{}{}, features={}",
222 rhs.family_name,
223 rhs.sub_family_name,
224 rhs.monospace ? 'M' : '_',
225 rhs.serif ? 'S' : '_',
226 rhs.italic ? 'I' : '_',
227 rhs.condensed ? 'C' : '_',
228 to_char(rhs.weight),
229 rhs.optical_size,
230 rhs.features);
231 }
232
233private:
234 mutable std::vector<glyph_atlas_info> _single_glyph_atlas_table;
235 mutable hash_map<glyph_ids, glyph_atlas_info> _multi_glyph_atlas_table;
236};
237
238} // namespace hi::inline v1
239
240template<typename CharT>
241struct std::formatter<hi::font, CharT> : formatter<std::string_view, CharT> {
242 auto format(hi::font const& t, auto& fc)
243 {
244 return formatter<string_view, CharT>::format(to_string(t), fc);
245 }
246};
Defined font_char_map type.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
font_weight
Definition font_weight.hpp:16
geometry/margins.hpp
Definition cache.hpp:11
Definition font.hpp:30
virtual glyph_metrics get_metrics(hi::glyph_id glyph_id) const =0
Load a glyph into a path.
virtual bool loaded() const noexcept=0
Return if the font is loaded.
virtual shape_run_result_type shape_run(iso_639 language, iso_15924 script, gstring run) const =0
Shape a run of graphemes.
hi::font_metrics metrics
The metrics of a font.
Definition font.hpp:66
std::string features
A string representing the features of a font.
Definition font.hpp:60
std::string family_name
The family name as parsed from the font file.
Definition font.hpp:36
std::string sub_family_name
The sub-family name as parsed from the font file.
Definition font.hpp:42
std::vector< hi::font * > fallback_chain
List of fonts to use as a fallback for this font.
Definition font.hpp:70
virtual graphic_path get_path(hi::glyph_id glyph_id) const =0
Load a glyph into a path.
virtual float get_advance(hi::glyph_id glyph_id) const =0
Get the advance for a glyph.
lean_vector< glyph_id > find_glyph(grapheme g) const
Get the glyphs for a grapheme.
font_char_map char_map
A optimized character map.
Definition font.hpp:55
Definition font.hpp:126
std::vector< aarectangle > glyph_bounding_rectangles
The bounding rectangle for each glyph.
Definition font.hpp:154
std::vector< float > grapheme_advances
Position of each grapheme.
Definition font.hpp:129
std::vector< size_t > glyph_count
The number of glyphs used by each grapheme.
Definition font.hpp:133
std::vector< point2 > glyph_positions
Position of each glyph.
Definition font.hpp:145
std::vector< glyph_id > glyphs
The glyphs representing all the graphemes.
Definition font.hpp:141
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:16
Definition glyph_atlas_info.hpp:12
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:135
constexpr bool has_num_glyphs() const noexcept
Check if this object contains a number of glyphs.
Definition glyph_ids.hpp:255
Definition glyph_metrics.hpp:16
A path is a vector graphics object.
Definition graphic_path.hpp:26
ISO-15924 script code.
Definition iso_15924.hpp:17
ISO-639 language code.
Definition iso_639.hpp:23
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:44
Definition tagged_id.hpp:17
T reserve(T... args)
T to_string(T... args)