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 "../utility/module.hpp"
19#include "../hash_map.hpp"
20#include <span>
21#include <vector>
22#include <map>
23#include <string>
24
25namespace hi::inline v1 {
26
31class font {
32public:
38
44
45 bool monospace = false;
46 bool serif = false;
47 bool italic = false;
48 bool condensed = false;
49 font_weight weight = font_weight::Regular;
50 float optical_size = 12.0;
51
56
57 hi::unicode_mask unicode_mask;
58
63 hi::font_metrics metrics;
64
68
69 font() = default;
70 virtual ~font() = default;
71 font(font const &) = delete;
72 font &operator=(font const &) = delete;
73 font(font &&) = delete;
74 font &operator=(font &&) = delete;
75
80 [[nodiscard]] virtual bool loaded() const noexcept = 0;
81
85 [[nodiscard]] virtual hi::glyph_id find_glyph(char32_t c) const = 0;
86
90 [[nodiscard]] hi::glyph_ids find_glyph(grapheme g) const;
91
99 virtual std::optional<hi::glyph_id> load_glyph(hi::glyph_id glyph_id, graphic_path &path) const = 0;
100
109 virtual bool load_glyph_metrics(
111 glyph_metrics &metrics,
112 hi::glyph_id lookahead_glyph_id = hi::glyph_id{}) const = 0;
113
120 [[nodiscard]] virtual vector2 get_kerning(hi::glyph_id current_glyph, hi::glyph_id next_glyph) const = 0;
121
140
148 const = 0;
149
150 glyph_atlas_info &atlas_info(glyph_ids const &glyphs) const
151 {
152 if (glyphs.has_num_glyphs<1>()) [[likely]] {
153 hilet index = static_cast<std::size_t>(get<0>(glyphs));
154 if (index >= _single_glyph_atlas_table.size()) [[unlikely]] {
155 _single_glyph_atlas_table.resize(index + 1);
156 }
157 return _single_glyph_atlas_table[index];
158
159 } else {
160 return _multi_glyph_atlas_table[glyphs];
161 }
162 }
163
164 [[nodiscard]] font_variant font_variant() const noexcept
165 {
166 return {weight, italic};
167 }
168
169 [[nodiscard]] friend std::string to_string(font const &rhs) noexcept
170 {
171 return std::format(
172 "{} - {}: style={}{}{}{}{}{}, features={}",
173 rhs.family_name,
174 rhs.sub_family_name,
175 rhs.monospace ? 'M' : '_',
176 rhs.serif ? 'S' : '_',
177 rhs.italic ? 'I' : '_',
178 rhs.condensed ? 'C' : '_',
179 to_char(rhs.weight),
180 rhs.optical_size,
181 rhs.features);
182 }
183
184private:
185 mutable std::vector<glyph_atlas_info> _single_glyph_atlas_table;
186 mutable hash_map<glyph_ids, glyph_atlas_info> _multi_glyph_atlas_table;
187};
188
189} // namespace hi::inline v1
190
191template<typename CharT>
192struct std::formatter<hi::font, CharT> : formatter<std::string_view, CharT> {
193 auto format(hi::font const &t, auto &fc)
194 {
195 return formatter<string_view, CharT>::format(to_string(t), fc);
196 }
197};
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
font_weight
Definition font_weight.hpp:16
geometry/margins.hpp
Definition cache.hpp:11
Definition font.hpp:31
virtual bool loaded() const noexcept=0
Return if the font is loaded.
virtual vector2 get_kerning(hi::glyph_id current_glyph, hi::glyph_id next_glyph) const =0
Get the kerning between two glyphs.
hi::font_metrics metrics
The metrics of a font.
Definition font.hpp:63
std::string features
A string representing the features of a font.
Definition font.hpp:55
std::string family_name
The family name as parsed from the font file.
Definition font.hpp:37
std::string sub_family_name
The sub-family name as parsed from the font file.
Definition font.hpp:43
std::vector< hi::font * > fallback_chain
List of fonts to use as a fallback for this font.
Definition font.hpp:67
virtual void substitution_and_kerning(iso_639 language, iso_15924 script, std::vector< substitution_and_kerning_type > &word) const =0
Substitute and kern a run of glyphs.
vector2 advance
The advance in font-unit coordinate system.
Definition font.hpp:138
glyph_id glyph
The glyph.
Definition font.hpp:129
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:134
constexpr bool has_num_glyphs() const noexcept
Check if this object contains a number of glyphs.
Definition glyph_ids.hpp:238
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
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:39
A mask of unicode code-points.
Definition unicode_mask.hpp:101
T to_string(T... args)