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(font_family_id family_id, font_weight weight, font_style style) const noexcept;
116
125 [[nodiscard]] font const& find_font(std::string const& family_name, font_weight weight, font_style style) const noexcept;
126
135 [[nodiscard]] font_glyphs_type find_glyph(font const& font, grapheme grapheme) const noexcept;
136
145 [[nodiscard]] font_glyph_type find_glyph(font const& font, char32_t code_point) const noexcept;
146
147private:
148 inline static std::unique_ptr<font_book> _global = nullptr;
149
152 std::unordered_map<std::string, font_family_id> _family_names;
153
156 std::vector<std::array<font const *, font_variant::max()>> _font_variants;
157
158 std::vector<std::unique_ptr<font>> _fonts;
159 std::vector<hi::font *> _font_ptrs;
160
161 [[nodiscard]] std::vector<hi::font *> make_fallback_chain(font_weight weight, font_style style) noexcept;
162};
163
174inline font& register_font_file(std::filesystem::path const& path)
175{
176 return font_book::global().register_font_file(path);
177}
178
179inline void register_font_directory(std::filesystem::path const& path)
180{
181 return font_book::global().register_font_directory(path);
182}
183
184template<typename Range>
185inline void register_font_directories(Range &&range) noexcept
186{
187 for (auto const &path: range) {
188 font_book::global().register_font_directory(path, false);
189 }
190 font_book::global().post_process();
191}
192
196[[nodiscard]] inline font_family_id find_font_family(std::string const& family_name) noexcept
197{
198 return font_book::global().find_family(family_name);
199}
200
208[[nodiscard]] inline font const& find_font(font_family_id family_id, font_variant variant = font_variant{}) noexcept
209{
210 return font_book::global().find_font(family_id, variant);
211}
212
220[[nodiscard]] inline font const& find_font(std::string const& family_name, font_variant variant = font_variant{}) noexcept
221{
222 hilet family_id = find_font_family(family_name);
223 return font_book::global().find_font(family_id, variant);
224}
225
234[[nodiscard]] inline auto find_glyph(font const& font, grapheme grapheme) noexcept
235{
236 return font_book::global().find_glyph(font, grapheme);
237}
238
247[[nodiscard]] inline auto find_glyph(font const& font, char32_t code_point) noexcept
248{
249 return font_book::global().find_glyph(font, code_point);
250}
251
252[[nodiscard]] inline auto find_glyph(elusive_icon rhs) noexcept
253{
254 hilet& font = find_font("Elusive", font_variant{});
255 return find_glyph(font, to_underlying(rhs));
256}
257
258[[nodiscard]] inline auto find_glyph(hikogui_icon rhs) noexcept
259{
260 hilet& font = find_font("HikoGUI", font_variant{});
261 return find_glyph(font, to_underlying(rhs));
262}
263
264} // namespace hi::inline v1
#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:16
auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:234
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:208
font_family_id find_font_family(std::string const &family_name) noexcept
Find font family id.
Definition font_book.hpp:196
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