HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
font_book.hpp
1// Copyright Take Vos 2020-2021.
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_description.hpp"
8#include "grapheme.hpp"
9#include "font.hpp"
10#include "font_family_id.hpp"
11#include "font_id.hpp"
12#include "font_grapheme_id.hpp"
13#include "font_glyph_ids.hpp"
14#include "../URL.hpp"
15#include "../alignment.hpp"
16#include "../subsystem.hpp"
17#include <limits>
18#include <array>
19#include <new>
20#include <atomic>
21
22namespace tt {
23
30class font_book {
31public:
32 font_book(std::vector<URL> const &font_directories);
33
46
51 void post_process() noexcept;
52
56 [[nodiscard]] font_family_id find_family(std::string_view family_name) const noexcept;
57
61 [[nodiscard]] font_family_id register_family(std::string_view family_name) noexcept;
62
70 [[nodiscard]] font_id find_font(font_family_id family_id, font_variant variant) const noexcept;
71
80 [[nodiscard]] font_id find_font(font_family_id family_id, font_weight weight, bool italic) const noexcept;
81
90 [[nodiscard]] font_id find_font(std::string_view family_name, font_weight weight, bool italic) const noexcept;
91
92 [[nodiscard]] font const &get_font(font_id font_id) const noexcept;
93
103
104 [[nodiscard]] static font_book &global() noexcept
105 {
106 return *start_subsystem_or_terminate(_global, nullptr, subsystem_init, subsystem_deinit);
107 }
108
109private:
110 struct fontEntry {
111 URL url;
112 font_description description;
114 std::vector<font_id> fallbacks;
115
116 fontEntry(URL url, font_description description) noexcept :
117 url(std::move(url)), description(std::move(description)), font(), fallbacks()
118 {
119 }
120 };
121
122 static inline std::atomic<font_book *> _global;
123
127
130 std::unordered_map<std::string, std::string> family_name_fallback_chain;
131
134 std::vector<std::array<font_id, font_variant::max()>> font_variants;
135
136 std::vector<fontEntry> font_entries;
137
141 mutable std::unordered_map<std::string, font_family_id> family_name_cache;
142
147 void calculate_fallback_fonts(
148 fontEntry &entry,
149 std::function<bool(font_description const &, font_description const &)> predicate) noexcept;
150
154 [[nodiscard]] font_glyph_ids find_glyph_actual(font_id font_id, grapheme grapheme) const noexcept;
155
158 // void morph_glyphs(glyph_array &glyphs) const noexcept;
159
162 // void atlas_lookup(glyph &glyph) noexcept;
163
166 // void atlas_lookup(glyph_array &glyphs) noexcept;
167
168 // void kern_glyphs(glyph_array &glyphs) const noexcept;
169
173 [[nodiscard]] std::string const &find_fallback_family_name(std::string const &name) const noexcept;
174
175 void create_family_name_fallback_chain() noexcept;
176
177 [[nodiscard]] static font_book *subsystem_init() noexcept
178 {
179 return new font_book(std::vector<URL>{URL::urlFromSystemfontDirectory()});
180 }
181
182 static void subsystem_deinit() noexcept
183 {
184 if (auto tmp = _global.exchange(nullptr)) {
185 delete tmp;
186 }
187 }
188};
189
190} // namespace tt
STL namespace.
Definition font.hpp:26
font_book keeps track of multiple fonts.
Definition font_book.hpp:30
font_glyph_ids find_glyph(font_id font_id, grapheme grapheme) const noexcept
Find a glyph using the given code-point.
font_family_id find_family(std::string_view family_name) const noexcept
Find font family id.
font_id find_font(font_family_id family_id, font_variant variant) const noexcept
Find a font closest to the variant.
font_family_id register_family(std::string_view family_name) noexcept
Register font family id.
void post_process() noexcept
Post process font_book Should be called after a set of register_font() calls This calculates font fal...
font_id register_font(URL url, bool post_process=true)
Register a font.
Definition font_description.hpp:16
Definition font_glyph_ids.hpp:78
A font variant is one of 16 different fonts that can be part of a family.
Definition font_variant.hpp:16
Definition grapheme.hpp:21
Definition URL.hpp:47
T exchange(T... args)
T move(T... args)