HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
true_type_font.hpp
1// Copyright Take Vos 2019-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.hpp"
8#include "../graphic_path.hpp"
9#include "../resource_view.hpp"
10#include "../URL.hpp"
11#include <memory>
12
13namespace tt {
14
15class true_type_font final: public font {
16private:
18
19 std::span<std::byte const> file_bytes;
20
21 uint16_t OS2_xHeight = 0;
22 uint16_t OS2_HHeight = 0;
23
25 std::span<std::byte const> cmapTableBytes;
26
28 std::span<std::byte const> cmapBytes;
29
31 std::span<std::byte const> glyfTableBytes;
32
34 std::span<std::byte const> headTableBytes;
35 float unitsPerEm;
36 float emScale;
37 bool locaTableIsOffset32;
38
40 std::span<std::byte const> hheaTableBytes;
41 float ascender;
42 float descender;
43 float lineGap;
44 uint16_t numberOfHMetrics;
45
47 std::span<std::byte const> hmtxTableBytes;
48
50 std::span<std::byte const> locaTableBytes;
51
53 std::span<std::byte const> maxpTableBytes;
54 int numGlyphs;
55
57 std::span<std::byte const> nameTableBytes;
58
60 std::span<std::byte const> postTableBytes;
61
63 std::span<std::byte const> os2TableBytes;
64
66 std::span<std::byte const> kernTableBytes;
67
68public:
74 true_type_font(std::span<std::byte const> bytes) :
75 file_bytes(bytes)
76 {
77 parsefontDirectory();
78 }
79
81 view(std::move(view))
82 {
83 file_bytes = this->view->bytes();
84 parsefontDirectory();
85 }
86
87 true_type_font(URL const &url) :
88 view(url.loadView())
89 {
90 file_bytes = this->view->bytes();
91 try {
92 parsefontDirectory();
93
94 } catch (std::exception const &e) {
95 throw parse_error("{}: Could not parse font directory.\n{}", url, e.what());
96 }
97 }
98
99 true_type_font() = delete;
100 true_type_font(true_type_font const &other) = delete;
101 true_type_font &operator=(true_type_font const &other) = delete;
102 true_type_font(true_type_font &&other) = delete;
103 true_type_font &operator=(true_type_font &&other) = delete;
104 ~true_type_font() = default;
105
109 [[nodiscard]] tt::glyph_id find_glyph(char32_t c) const noexcept override;
110
118 std::optional<tt::glyph_id> loadGlyph(tt::glyph_id glyph_id, graphic_path &path) const noexcept override;
119
129 const noexcept override;
130
131private:
136 void parsefontDirectory();
137
141 void parseHeadTable(std::span<std::byte const> headTableBytes);
142
143 void parseHheaTable(std::span<std::byte const> bytes);
144
145 void parseNameTable(std::span<std::byte const> bytes);
146
147 void parseOS2Table(std::span<std::byte const> bytes);
148
151 [[nodiscard]] unicode_ranges parseCharacterMap();
152
153
157 void parseMaxpTable(std::span<std::byte const> bytes);
158
162 bool getGlyphBytes(tt::glyph_id glyph_id, std::span<std::byte const> &bytes) const noexcept;
163
167 bool updateglyph_metrics(
169 glyph_metrics &metrics,
170 tt::glyph_id kern_glyph1_id = tt::glyph_id{},
171 tt::glyph_id kern_glyph2_id = tt::glyph_id{}) const noexcept;
172
173 bool loadSimpleGlyph(std::span<std::byte const> bytes, graphic_path &glyph) const noexcept;
174
183 bool loadCompoundGlyph(std::span<std::byte const> bytes, graphic_path &glyph, tt::glyph_id &metrics_glyph_id) const noexcept;
184
192 bool loadCompoundglyph_metrics(std::span<std::byte const> bytes, tt::glyph_id &metrics_glyph_id) const noexcept;
193
194
195};
196
197}
198
STL namespace.
A path is a vector graphics object.
Definition graphic_path.hpp:28
Definition tagged_id.hpp:18
Definition font.hpp:26
Definition glyph_metrics.hpp:17
Definition true_type_font.hpp:15
std::optional< tt::glyph_id > loadGlyph(tt::glyph_id glyph_id, graphic_path &path) const noexcept override
Load a glyph into a path.
bool loadglyph_metrics(tt::glyph_id glyph_id, glyph_metrics &metrics, tt::glyph_id lookahead_glyph_id=tt::glyph_id{}) const noexcept override
Load a glyphMetrics into a path.
true_type_font(std::span< std::byte const > bytes)
Load a true type font.
Definition true_type_font.hpp:74
tt::glyph_id find_glyph(char32_t c) const noexcept override
Get the glyph for a code-point.
Unicode Ranges based on the OS/2 table in TrueType fonts.
Definition unicode_ranges.hpp:15
T what(T... args)