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 "elusive_icon.hpp"
10#include "hikogui_icon.hpp"
11#include "../unicode/module.hpp"
12#include "../geometry/module.hpp"
13#include "../utility/module.hpp"
14#include "../generator.hpp"
15#include <limits>
16#include <array>
17#include <new>
18#include <atomic>
19#include <filesystem>
20
21namespace hi::inline v1 {
22
29class font_book {
30public:
32 hi::font const *font;
33 hi::glyph_id glyph;
34
35 [[nodiscard]] constexpr friend bool operator==(font_glyph_type const&, font_glyph_type const&) noexcept = default;
36
37 [[nodiscard]] glyph_metrics get_metrics() const noexcept
38 {
40 return font->get_metrics(glyph);
41 }
42
43 [[nodiscard]] aarectangle get_bounding_rectangle() const noexcept
44 {
45 return get_metrics().bounding_rectangle;
46 }
47 };
48
50 hi::font const *font;
51 lean_vector<glyph_id> glyphs;
52 };
53
54 static font_book& global() noexcept;
55
56 ~font_book();
57 font_book(font_book const&) = delete;
58 font_book(font_book&&) = delete;
59 font_book& operator=(font_book const&) = delete;
60 font_book& operator=(font_book&&) = delete;
61 font_book();
62
74 font& register_font_file(std::filesystem::path const& path, bool post_process = true);
75
80 void register_font_directory(std::filesystem::path const& path, bool post_process = true);
81
86 void post_process() noexcept;
87
91 [[nodiscard]] font_family_id find_family(std::string const& family_name) const noexcept;
92
96 [[nodiscard]] font_family_id register_family(std::string_view family_name) noexcept;
97
105 [[nodiscard]] font const& find_font(font_family_id family_id, font_variant variant) const noexcept;
106
115 [[nodiscard]] font const *find_font(std::string const& family_name, font_variant variant) const noexcept;
116
125 [[nodiscard]] font_glyphs_type find_glyph(font const& font, grapheme grapheme) const noexcept;
126
135 [[nodiscard]] font_glyph_type find_glyph(font const& font, char32_t code_point) const noexcept;
136
137private:
138 inline static std::unique_ptr<font_book> _global = nullptr;
139
142 std::unordered_map<std::string, font_family_id> _family_names;
143
146 std::vector<std::array<font const *, font_variant::size()>> _font_variants;
147
148 std::vector<std::unique_ptr<font>> _fonts;
149 std::vector<hi::font *> _font_ptrs;
150
151 [[nodiscard]] std::vector<hi::font *> make_fallback_chain(font_weight weight, font_style style) noexcept;
152};
153
164inline font& register_font_file(std::filesystem::path const& path)
165{
166 return font_book::global().register_font_file(path);
167}
168
169inline void register_font_directory(std::filesystem::path const& path)
170{
171 return font_book::global().register_font_directory(path);
172}
173
174template<typename Range>
175inline void register_font_directories(Range&& range) noexcept
176{
177 for (auto const& path : range) {
178 font_book::global().register_font_directory(path, false);
179 }
180 font_book::global().post_process();
181}
182
186[[nodiscard]] inline font_family_id find_font_family(std::string const& family_name) noexcept
187{
188 return font_book::global().find_family(family_name);
189}
190
198[[nodiscard]] inline font const& find_font(font_family_id family_id, font_variant variant = font_variant{}) noexcept
199{
200 return font_book::global().find_font(family_id, variant);
201}
202
210[[nodiscard]] inline font const *find_font(std::string const& family_name, font_variant variant = font_variant{}) noexcept
211{
212 return font_book::global().find_font(family_name, variant);
213}
214
223[[nodiscard]] inline auto find_glyph(font const& font, grapheme grapheme) noexcept
224{
225 return font_book::global().find_glyph(font, grapheme);
226}
227
236[[nodiscard]] inline auto find_glyph(font const& font, char32_t code_point) noexcept
237{
238 return font_book::global().find_glyph(font, code_point);
239}
240
241[[nodiscard]] inline auto find_glyph(elusive_icon rhs) noexcept
242{
243 hilet *font = find_font("elusiveicons", font_variant{font_weight::medium, font_style::normal});
244 hi_assert_not_null(font, "Could not find Elusive icon font");
245 return find_glyph(*font, to_underlying(rhs));
246}
247
248[[nodiscard]] inline auto find_glyph(hikogui_icon rhs) noexcept
249{
250 hilet *font = find_font("Hikogui Icons", font_variant{font_weight::regular, font_style::normal});
251 hi_assert_not_null(font, "Could not find HikoGUI icon font");
252 return find_glyph(*font, to_underlying(rhs));
253}
254
255} // namespace hi::inline v1
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hi_axiom_not_null(expression,...)
Assert if an expression is not nullptr.
Definition assert.hpp:272
#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:17
auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:223
font const & find_font(font_family_id family_id, font_variant variant=font_variant{}) noexcept
Find a font closest to the variant.
Definition font_book.hpp:198
font_family_id find_font_family(std::string const &family_name) noexcept
Find font family id.
Definition font_book.hpp:186
geometry/margins.hpp
Definition cache.hpp:11
Definition font.hpp:30
virtual glyph_metrics get_metrics(hi::glyph_id glyph_id) const =0
Load a glyph into a path.
font_book keeps track of multiple fonts.
Definition font_book.hpp:29
Definition font_book.hpp:31
Definition font_book.hpp:49
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:17
Definition glyph_metrics.hpp:16
Definition tagged_id.hpp:17
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:42