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 "../unicode/unicode_mask.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 "../resource_view.hpp"
19#include "../exception.hpp"
20#include "../utility.hpp"
21#include "../URL.hpp"
22#include "../hash_map.hpp"
23#include <span>
24#include <vector>
25#include <map>
26#include <string>
27
28namespace hi::inline v1 {
29
34class font {
35public:
41
47
48 bool monospace = false;
49 bool serif = false;
50 bool italic = false;
51 bool condensed = false;
52 font_weight weight = font_weight::Regular;
53 float optical_size = 12.0;
54
59
60 hi::unicode_mask unicode_mask;
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]] virtual hi::glyph_id find_glyph(char32_t c) const noexcept = 0;
89
93 [[nodiscard]] hi::glyph_ids find_glyph(grapheme g) const noexcept;
94
102 virtual std::optional<hi::glyph_id> load_glyph(hi::glyph_id glyph_id, graphic_path &path) const noexcept = 0;
103
112 virtual bool load_glyph_metrics(
114 glyph_metrics &metrics,
115 hi::glyph_id lookahead_glyph_id = hi::glyph_id{}) const noexcept = 0;
116
123 [[nodiscard]] virtual vector2 get_kerning(hi::glyph_id current_glyph, hi::glyph_id next_glyph) const noexcept = 0;
124
143
151 const noexcept = 0;
152
153 glyph_atlas_info &atlas_info(glyph_ids const &glyphs) const noexcept
154 {
155 if (glyphs.has_num_glyphs<1>()) [[likely]] {
156 hilet index = static_cast<std::size_t>(get<0>(glyphs));
157 if (index >= _single_glyph_atlas_table.size()) [[unlikely]] {
158 _single_glyph_atlas_table.resize(index + 1);
159 }
160 return _single_glyph_atlas_table[index];
161
162 } else {
163 return _multi_glyph_atlas_table[glyphs];
164 }
165 }
166
167 [[nodiscard]] font_variant font_variant() const noexcept
168 {
169 return {weight, italic};
170 }
171
172 [[nodiscard]] friend std::string to_string(font const &rhs) noexcept
173 {
174 return std::format(
175 "{} - {}: style={}{}{}{}{}{}, features={}",
176 rhs.family_name,
177 rhs.sub_family_name,
178 rhs.monospace ? 'M' : '_',
179 rhs.serif ? 'S' : '_',
180 rhs.italic ? 'I' : '_',
181 rhs.condensed ? 'C' : '_',
182 to_char(rhs.weight),
183 rhs.optical_size,
184 rhs.features);
185 }
186
187private:
188 mutable std::vector<glyph_atlas_info> _single_glyph_atlas_table;
189 mutable hash_map<glyph_ids, glyph_atlas_info> _multi_glyph_atlas_table;
190};
191
192} // namespace hi::inline v1
193
194template<typename CharT>
195struct std::formatter<hi::font, CharT> : formatter<std::string_view, CharT> {
196 auto format(hi::font const &t, auto &fc)
197 {
198 return formatter<string_view, CharT>::format(to_string(t), fc);
199 }
200};
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
font_weight
Definition font_weight.hpp:17
The HikoGUI namespace.
Definition ascii.hpp:19
A path is a vector graphics object.
Definition graphic_path.hpp:29
ISO-15924 script code.
Definition iso_15924.hpp:18
ISO-639 language code.
Definition iso_639.hpp:25
Definition language.hpp:18
Definition font.hpp:34
virtual bool loaded() const noexcept=0
Return if the font is loaded.
hi::font_metrics metrics
The metrics of a font.
Definition font.hpp:66
virtual vector2 get_kerning(hi::glyph_id current_glyph, hi::glyph_id next_glyph) const noexcept=0
Get the kerning between two glyphs.
std::string features
A string representing the features of a font.
Definition font.hpp:58
virtual void substitution_and_kerning(iso_639 language, iso_15924 script, std::vector< substitution_and_kerning_type > &word) const noexcept=0
Substitute and kern a run of glyphs.
std::string family_name
The family name as parsed from the font file.
Definition font.hpp:40
std::string sub_family_name
The sub-family name as parsed from the font file.
Definition font.hpp:46
std::vector< hi::font * > fallback_chain
List of fonts to use as a fallback for this font.
Definition font.hpp:70
vector2 advance
The advance in font-unit coordinate system.
Definition font.hpp:141
glyph_id glyph
The glyph.
Definition font.hpp:132
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:15
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:135
Definition glyph_metrics.hpp:17
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:41
A mask of unicode code-points.
Definition unicode_mask.hpp:101
T to_string(T... args)