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 "../error_info.hpp"
12#include <memory>
13
14namespace tt {
15
16class true_type_font final: public font {
17private:
19
20 std::span<std::byte const> file_bytes;
21
22 uint16_t OS2_xHeight = 0;
23 uint16_t OS2_HHeight = 0;
24
26 std::span<std::byte const> cmapTableBytes;
27
29 std::span<std::byte const> cmapBytes;
30
32 std::span<std::byte const> glyfTableBytes;
33
35 std::span<std::byte const> headTableBytes;
36 float unitsPerEm;
37 float emScale;
38 bool locaTableIsOffset32;
39
41 std::span<std::byte const> hheaTableBytes;
42 float ascender;
43 float descender;
44 float lineGap;
45 uint16_t numberOfHMetrics;
46
48 std::span<std::byte const> hmtxTableBytes;
49
51 std::span<std::byte const> locaTableBytes;
52
54 std::span<std::byte const> maxpTableBytes;
55 int numGlyphs;
56
58 std::span<std::byte const> nameTableBytes;
59
61 std::span<std::byte const> postTableBytes;
62
64 std::span<std::byte const> os2TableBytes;
65
67 std::span<std::byte const> kernTableBytes;
68
69public:
75 true_type_font(std::span<std::byte const> bytes) :
76 file_bytes(bytes)
77 {
78 parsefontDirectory();
79 }
80
82 view(std::move(view))
83 {
84 file_bytes = this->view->bytes();
85 parsefontDirectory();
86 }
87
88 true_type_font(URL const &url) :
89 view(url.loadView())
90 {
91 file_bytes = this->view->bytes();
92 try {
93 parsefontDirectory();
94
95 } catch (...) {
96 error_info(true).set<"url">(url);
97 throw;
98 }
99 }
100
101 true_type_font() = delete;
102 true_type_font(true_type_font const &other) = delete;
103 true_type_font &operator=(true_type_font const &other) = delete;
104 true_type_font(true_type_font &&other) = delete;
105 true_type_font &operator=(true_type_font &&other) = delete;
106 ~true_type_font() = default;
107
111 [[nodiscard]] tt::glyph_id find_glyph(char32_t c) const noexcept override;
112
120 std::optional<tt::glyph_id> loadGlyph(tt::glyph_id glyph_id, graphic_path &path) const noexcept override;
121
131 const noexcept override;
132
133private:
138 void parsefontDirectory();
139
143 void parseHeadTable(std::span<std::byte const> headTableBytes);
144
145 void parseHheaTable(std::span<std::byte const> bytes);
146
147 void parseNameTable(std::span<std::byte const> bytes);
148
149 void parseOS2Table(std::span<std::byte const> bytes);
150
153 [[nodiscard]] unicode_ranges parseCharacterMap();
154
155
159 void parseMaxpTable(std::span<std::byte const> bytes);
160
164 bool getGlyphBytes(tt::glyph_id glyph_id, std::span<std::byte const> &bytes) const noexcept;
165
169 bool updateglyph_metrics(
171 glyph_metrics &metrics,
172 tt::glyph_id kern_glyph1_id = tt::glyph_id{},
173 tt::glyph_id kern_glyph2_id = tt::glyph_id{}) const noexcept;
174
175 bool loadSimpleGlyph(std::span<std::byte const> bytes, graphic_path &glyph) const noexcept;
176
185 bool loadCompoundGlyph(std::span<std::byte const> bytes, graphic_path &glyph, tt::glyph_id &metrics_glyph_id) const noexcept;
186
194 bool loadCompoundglyph_metrics(std::span<std::byte const> bytes, tt::glyph_id &metrics_glyph_id) const noexcept;
195
196
197};
198
199}
200
STL namespace.
A path is a vector graphics object.
Definition graphic_path.hpp:29
Definition tagged_id.hpp:18
Definition font.hpp:26
Definition glyph_metrics.hpp:18
Definition true_type_font.hpp:16
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:75
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