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 "../counters.hpp"
12#include <memory>
13
14namespace tt {
15
16class true_type_font final : public font {
17private:
20 std::optional<URL> url;
21
27
28 uint16_t OS2_xHeight = 0;
29 uint16_t OS2_HHeight = 0;
30
31 float unitsPerEm;
32 float emScale;
33 bool locaTableIsOffset32;
34
35 float ascender;
36 float descender;
37 float lineGap;
38 uint16_t numberOfHMetrics;
39
40 int numGlyphs;
41
42public:
44 {
45 parse_font_directory();
46 }
47
48 true_type_font(URL const &url) : url(url), view(url.loadView())
49 {
50 ++global_counter<"ttf:map">;
51 try {
52 parse_font_directory();
53
54 // Clear the view to reclaim resources.
55 view = {};
56 ++global_counter<"ttf:unmap">;
57
58 } catch (std::exception const &e) {
59 throw parse_error("{}: Could not parse font directory.\n{}", to_string(url), e.what());
60 }
61 }
62
63 true_type_font() = delete;
64 true_type_font(true_type_font const &other) = delete;
65 true_type_font &operator=(true_type_font const &other) = delete;
66 true_type_font(true_type_font &&other) = delete;
67 true_type_font &operator=(true_type_font &&other) = delete;
68 ~true_type_font() = default;
69
70 [[nodiscard]] bool loaded() const noexcept override
71 {
72 return static_cast<bool>(view);
73 }
74
78 [[nodiscard]] tt::glyph_id find_glyph(char32_t c) const noexcept override;
79
87 std::optional<tt::glyph_id> load_glyph(tt::glyph_id glyph_id, graphic_path &path) const noexcept override;
88
98 const noexcept override;
99
100private:
105 [[nodiscard]] std::span<std::byte const> getTableBytes(char const *table_name) const;
106
112 void parse_font_directory();
113
118 void parseHeadTable(std::span<std::byte const> headTableBytes);
119
120 void parseHheaTable(std::span<std::byte const> bytes);
121
122 void parseNameTable(std::span<std::byte const> bytes);
123
124 void parseOS2Table(std::span<std::byte const> bytes);
125
128 [[nodiscard]] tt::unicode_mask parseCharacterMap();
129
134 void parseMaxpTable(std::span<std::byte const> bytes);
135
139 bool getGlyphBytes(tt::glyph_id glyph_id, std::span<std::byte const> &bytes) const noexcept;
140
144 bool update_glyph_metrics(
146 glyph_metrics &metrics,
147 tt::glyph_id kern_glyph1_id = tt::glyph_id{},
148 tt::glyph_id kern_glyph2_id = tt::glyph_id{}) const noexcept;
149
150 bool loadSimpleGlyph(std::span<std::byte const> bytes, graphic_path &glyph) const noexcept;
151
160 bool loadCompoundGlyph(std::span<std::byte const> bytes, graphic_path &glyph, tt::glyph_id &metrics_glyph_id) const noexcept;
161
169 bool loadCompoundglyph_metrics(std::span<std::byte const> bytes, tt::glyph_id &metrics_glyph_id) const noexcept;
170};
171
172} // namespace tt
Exception thrown during parsing on an error.
Definition exception.hpp:26
A path is a vector graphics object.
Definition graphic_path.hpp:28
Definition tagged_id.hpp:18
Definition font.hpp:28
Definition glyph_metrics.hpp:17
Definition true_type_font.hpp:16
tt::glyph_id find_glyph(char32_t c) const noexcept override
Get the glyph for a code-point.
std::optional< tt::glyph_id > load_glyph(tt::glyph_id glyph_id, graphic_path &path) const noexcept override
Load a glyph into a path.
bool load_glyph_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.
bool loaded() const noexcept override
Return if the font is loaded.
Definition true_type_font.hpp:70
A mask of unicode code-points.
Definition unicode_mask.hpp:101
Definition URL.hpp:47
T move(T... args)
T what(T... args)