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 "../exception.hpp"
19#include "../utility.hpp"
20#include "../hash_map.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
58 hi::unicode_mask unicode_mask;
59
64 hi::font_metrics metrics;
65
69
70 font() = default;
71 virtual ~font() = default;
72 font(font const &) = delete;
73 font &operator=(font const &) = delete;
74 font(font &&) = delete;
75 font &operator=(font &&) = delete;
76
81 [[nodiscard]] virtual bool loaded() const noexcept = 0;
82
86 [[nodiscard]] virtual hi::glyph_id find_glyph(char32_t c) const noexcept = 0;
87
91 [[nodiscard]] hi::glyph_ids find_glyph(grapheme g) const noexcept;
92
100 virtual std::optional<hi::glyph_id> load_glyph(hi::glyph_id glyph_id, graphic_path &path) const noexcept = 0;
101
110 virtual bool load_glyph_metrics(
112 glyph_metrics &metrics,
113 hi::glyph_id lookahead_glyph_id = hi::glyph_id{}) const noexcept = 0;
114
121 [[nodiscard]] virtual vector2 get_kerning(hi::glyph_id current_glyph, hi::glyph_id next_glyph) const noexcept = 0;
122
141
149 const noexcept = 0;
150
151 glyph_atlas_info &atlas_info(glyph_ids const &glyphs) const noexcept
152 {
153 if (glyphs.has_num_glyphs<1>()) [[likely]] {
154 hilet index = static_cast<std::size_t>(get<0>(glyphs));
155 if (index >= _single_glyph_atlas_table.size()) [[unlikely]] {
156 _single_glyph_atlas_table.resize(index + 1);
157 }
158 return _single_glyph_atlas_table[index];
159
160 } else {
161 return _multi_glyph_atlas_table[glyphs];
162 }
163 }
164
165 [[nodiscard]] font_variant font_variant() const noexcept
166 {
167 return {weight, italic};
168 }
169
170 [[nodiscard]] friend std::string to_string(font const &rhs) noexcept
171 {
172 return std::format(
173 "{} - {}: style={}{}{}{}{}{}, features={}",
174 rhs.family_name,
175 rhs.sub_family_name,
176 rhs.monospace ? 'M' : '_',
177 rhs.serif ? 'S' : '_',
178 rhs.italic ? 'I' : '_',
179 rhs.condensed ? 'C' : '_',
180 to_char(rhs.weight),
181 rhs.optical_size,
182 rhs.features);
183 }
184
185private:
186 mutable std::vector<glyph_atlas_info> _single_glyph_atlas_table;
187 mutable hash_map<glyph_ids, glyph_atlas_info> _multi_glyph_atlas_table;
188};
189
190} // namespace hi::inline v1
191
192template<typename CharT>
193struct std::formatter<hi::font, CharT> : formatter<std::string_view, CharT> {
194 auto format(hi::font const &t, auto &fc)
195 {
196 return formatter<string_view, CharT>::format(to_string(t), fc);
197 }
198};
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:19
ISO-639 language code.
Definition iso_639.hpp:25
Definition language.hpp:17
Definition font.hpp:32
virtual bool loaded() const noexcept=0
Return if the font is loaded.
hi::font_metrics metrics
The metrics of a font.
Definition font.hpp:64
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:56
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: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:68
vector2 advance
The advance in font-unit coordinate system.
Definition font.hpp:139
glyph_id glyph
The glyph.
Definition font.hpp:130
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)