HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_book.hpp
1// Copyright Take Vos 2020-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 "font.hpp"
8#include "font_family_id.hpp"
9#include "font_grapheme_id.hpp"
10#include "glyph_ids.hpp"
11#include "elusive_icon.hpp"
12#include "hikogui_icon.hpp"
13#include "../unicode/grapheme.hpp"
14#include "../geometry/module.hpp"
15#include "../utility/module.hpp"
16#include "../generator.hpp"
17#include <limits>
18#include <array>
19#include <new>
20#include <atomic>
21#include <filesystem>
22
23namespace hi::inline v1 {
24
31class font_book {
32public:
33 static font_book &global() noexcept;
34
35 ~font_book();
36 font_book(font_book const&) = delete;
37 font_book(font_book&&) = delete;
38 font_book& operator=(font_book const&) = delete;
39 font_book& operator=(font_book&&) = delete;
40 font_book();
41
53 font& register_font(std::filesystem::path const& path, bool post_process = true);
54
59 void register_font_directory(std::filesystem::path const& path, bool post_process = true);
60
61 void register_elusive_icon_font(std::filesystem::path const& path)
62 {
63 _elusive_icon_font = &register_font(path, false);
64 }
65
66 void register_hikogui_icon_font(std::filesystem::path const& path)
67 {
68 _hikogui_icon_font = &register_font(path, false);
69 }
70
75 void post_process() noexcept;
76
80 [[nodiscard]] font_family_id find_family(std::string_view family_name) const noexcept;
81
85 [[nodiscard]] font_family_id register_family(std::string_view family_name) noexcept;
86
94 [[nodiscard]] font const& find_font(font_family_id family_id, font_variant variant) const noexcept;
95
104 [[nodiscard]] font const& find_font(font_family_id family_id, font_weight weight, bool italic) const noexcept;
105
114 [[nodiscard]] font const& find_font(std::string_view family_name, font_weight weight, bool italic) const noexcept;
115
124 [[nodiscard]] glyph_ids find_glyph(font const& font, grapheme grapheme) const noexcept;
125
126 [[nodiscard]] glyph_ids find_glyph(elusive_icon rhs) const noexcept
127 {
128 hi_assert_not_null(_elusive_icon_font);
129 return _elusive_icon_font->find_glyph(grapheme{static_cast<char32_t>(rhs)});
130 }
131
132 [[nodiscard]] glyph_ids find_glyph(hikogui_icon rhs) const noexcept
133 {
134 hi_assert_not_null(_hikogui_icon_font);
135 return _hikogui_icon_font->find_glyph(grapheme{static_cast<char32_t>(rhs)});
136 }
137
138private:
139 inline static std::unique_ptr<font_book> _global = nullptr;
140
141 font const *_elusive_icon_font = nullptr;
142 font const *_hikogui_icon_font = nullptr;
143
147
150 std::unordered_map<std::string, std::string> _family_name_fallback_chain;
151
154 std::vector<std::array<font const *, font_variant::max()>> _font_variants;
155
157 std::vector<hi::font *> _font_ptrs;
158
162 mutable std::unordered_map<std::string, font_family_id> _family_name_cache;
163
168
169 [[nodiscard]] std::vector<hi::font *> make_fallback_chain(font_weight weight, bool italic) noexcept;
170
173 // void morph_glyphs(glyph_array &glyphs) const noexcept;
174
177 // void atlas_lookup(glyph &glyph) noexcept;
178
181 // void atlas_lookup(glyph_array &glyphs) noexcept;
182
183 // void kern_glyphs(glyph_array &glyphs) const noexcept;
184
187 [[nodiscard]] generator<std::string> generate_family_names(std::string_view name) const noexcept;
188
189 void create_family_name_fallback_chain() noexcept;
190};
191
200[[nodiscard]] inline glyph_ids find_glyph(font const& font, grapheme grapheme) noexcept
201{
202 return font_book::global().find_glyph(font, grapheme);
203}
204
205[[nodiscard]] inline glyph_ids find_glyph(elusive_icon rhs) noexcept
206{
207 return font_book::global().find_glyph(rhs);
208}
209
210[[nodiscard]] inline glyph_ids find_glyph(hikogui_icon rhs) noexcept
211{
212 return font_book::global().find_glyph(rhs);
213}
214
215} // namespace hi::inline v1
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
font_weight
Definition font_weight.hpp:16
glyph_ids find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:200
Definition font.hpp:31
font_book keeps track of multiple fonts.
Definition font_book.hpp:31
void post_process() noexcept
Post process font_book Should be called after a set of register_font() calls This calculates font fal...
void register_font_directory(std::filesystem::path const &path, bool post_process=true)
Register all fonts found in a directory.
font & register_font(std::filesystem::path const &path, bool post_process=true)
Register a font.
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:16
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:134
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:39