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/gstring.hpp"
15#include "../i18n/iso_15924.hpp"
16#include "../i18n/iso_639.hpp"
17#include "../graphic_path.hpp"
18#include "../utility/module.hpp"
19#include "../hash_map.hpp"
20#include "../lean_vector.hpp"
21#include <span>
22#include <vector>
23#include <map>
24#include <string>
25
26namespace hi::inline v1 {
27
32class font {
33public:
39
45
46 bool monospace = false;
47 bool serif = false;
48 bool italic = false;
49 bool condensed = false;
50 font_weight weight = font_weight::Regular;
51 float optical_size = 12.0;
52
57 font_char_map char_map;
58
63
68 hi::font_metrics metrics;
69
73
74 font() = default;
75 virtual ~font() = default;
76 font(font const&) = delete;
77 font& operator=(font const&) = delete;
78 font(font&&) = delete;
79 font& operator=(font&&) = delete;
80
85 [[nodiscard]] virtual bool loaded() const noexcept = 0;
86
90 [[nodiscard]] glyph_id find_glyph(char32_t c) const noexcept
91 {
92 return char_map.find(c);
93 }
94
98 [[nodiscard]] lean_vector<glyph_id> find_glyph(grapheme g) const;
99
108 [[nodiscard]] virtual graphic_path get_path(hi::glyph_id glyph_id) const = 0;
109
116 [[nodiscard]] virtual float get_advance(hi::glyph_id glyph_id) const = 0;
117
126 [[nodiscard]] virtual glyph_metrics get_metrics(hi::glyph_id glyph_id) const = 0;
127
132
136
144
148
157
158 void reserve(size_t count) noexcept
159 {
160 grapheme_advances.reserve(count);
161 glyph_count.reserve(count);
162 glyphs.reserve(count);
163 glyph_positions.reserve(count);
164 glyph_bounding_rectangles.reserve(count);
165 }
166
167 void scale(float s) noexcept
168 {
169 auto M = scale2{s};
170 for (auto& tmp : grapheme_advances) {
171 tmp = s * tmp;
172 }
173 for (auto& tmp : glyph_positions) {
174 tmp = M * tmp;
175 }
176 for (auto& tmp : glyph_bounding_rectangles) {
177 tmp = M * tmp;
178 }
179 }
180 };
181
199 [[nodiscard]] virtual shape_run_result_type shape_run(iso_639 language, iso_15924 script, gstring run) const = 0;
200
201 glyph_atlas_info& atlas_info(glyph_ids const& glyphs) const
202 {
203 if (glyphs.has_num_glyphs<1>()) [[likely]] {
204 hilet index = static_cast<std::size_t>(get<0>(glyphs));
205 if (index >= _single_glyph_atlas_table.size()) [[unlikely]] {
206 _single_glyph_atlas_table.resize(index + 1);
207 }
208 return _single_glyph_atlas_table[index];
209
210 } else {
211 return _multi_glyph_atlas_table[glyphs];
212 }
213 }
214
215 [[nodiscard]] font_variant font_variant() const noexcept
216 {
217 return {weight, italic};
218 }
219
220 [[nodiscard]] friend std::string to_string(font const& rhs) noexcept
221 {
222 return std::format(
223 "{} - {}: style={}{}{}{}{}{}, features={}",
224 rhs.family_name,
225 rhs.sub_family_name,
226 rhs.monospace ? 'M' : '_',
227 rhs.serif ? 'S' : '_',
228 rhs.italic ? 'I' : '_',
229 rhs.condensed ? 'C' : '_',
230 to_char(rhs.weight),
231 rhs.optical_size,
232 rhs.features);
233 }
234
235private:
236 mutable std::vector<glyph_atlas_info> _single_glyph_atlas_table;
237 mutable hash_map<glyph_ids, glyph_atlas_info> _multi_glyph_atlas_table;
238};
239
240} // namespace hi::inline v1
241
242template<typename CharT>
243struct std::formatter<hi::font, CharT> : formatter<std::string_view, CharT> {
244 auto format(hi::font const& t, auto& fc)
245 {
246 return formatter<string_view, CharT>::format(to_string(t), fc);
247 }
248};
Defined lean_vector<>.
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:32
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:68
std::string features
A string representing the features of a font.
Definition font.hpp:62
std::string family_name
The family name as parsed from the font file.
Definition font.hpp:38
std::string sub_family_name
The sub-family name as parsed from the font file.
Definition font.hpp:44
std::vector< hi::font * > fallback_chain
List of fonts to use as a fallback for this font.
Definition font.hpp:72
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:57
Definition font.hpp:128
std::vector< aarectangle > glyph_bounding_rectangles
The bounding rectangle for each glyph.
Definition font.hpp:156
std::vector< float > grapheme_advances
Position of each grapheme.
Definition font.hpp:131
std::vector< size_t > glyph_count
The number of glyphs used by each grapheme.
Definition font.hpp:135
std::vector< point2 > glyph_positions
Position of each glyph.
Definition font.hpp:147
std::vector< glyph_id > glyphs
The glyphs representing all the graphemes.
Definition font.hpp:143
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:19
ISO-639 language code.
Definition iso_639.hpp:24
Definition language.hpp:17
Definition tagged_id.hpp:17
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:39
T reserve(T... args)
T to_string(T... args)