HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
TrueTypeFont.hpp
1// Copyright 2019, 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Text/Font.hpp"
7#include "TTauri/Foundation/Path.hpp"
8#include "TTauri/Foundation/ResourceView.hpp"
9#include <memory>
10
11namespace tt {
12
13class TrueTypeFont final: public Font {
14private:
16
17 nonstd::span<std::byte const> file_bytes;
18
19 uint16_t OS2_xHeight = 0;
20 uint16_t OS2_HHeight = 0;
21
22
24 nonstd::span<std::byte const> cmapTableBytes;
25
27 nonstd::span<std::byte const> cmapBytes;
28
30 nonstd::span<std::byte const> glyfTableBytes;
31
33 nonstd::span<std::byte const> headTableBytes;
34 float unitsPerEm;
35 float emScale;
36 bool locaTableIsOffset32;
37
39 nonstd::span<std::byte const> hheaTableBytes;
40 float ascender;
41 float descender;
42 float lineGap;
43 uint16_t numberOfHMetrics;
44
46 nonstd::span<std::byte const> hmtxTableBytes;
47
49 nonstd::span<std::byte const> locaTableBytes;
50
52 nonstd::span<std::byte const> maxpTableBytes;
53 int numGlyphs;
54
56 nonstd::span<std::byte const> nameTableBytes;
57
59 nonstd::span<std::byte const> postTableBytes;
60
62 nonstd::span<std::byte const> os2TableBytes;
63
65 nonstd::span<std::byte const> kernTableBytes;
66
67public:
73 TrueTypeFont(nonstd::span<std::byte const> bytes) :
74 file_bytes(bytes) {
75 parseFontDirectory();
76 }
77
79 view(std::move(view)) {
80 file_bytes = this->view->bytes();
81 parseFontDirectory();
82 }
83
84 TrueTypeFont() = delete;
85 TrueTypeFont(TrueTypeFont const &other) = delete;
86 TrueTypeFont &operator=(TrueTypeFont const &other) = delete;
87 TrueTypeFont(TrueTypeFont &&other) = delete;
88 TrueTypeFont &operator=(TrueTypeFont &&other) = delete;
89 ~TrueTypeFont() = default;
90
94 [[nodiscard]] GlyphID find_glyph(char32_t c) const noexcept override;
95
103 std::optional<GlyphID> loadGlyph(GlyphID glyph_id, Path &path) const noexcept override;
104
113 bool loadGlyphMetrics(GlyphID glyph_id, GlyphMetrics &metrics, GlyphID lookahead_glyph_id=GlyphID{}) const noexcept override;
114
115private:
120 void parseFontDirectory();
121
125 void parseHeadTable(nonstd::span<std::byte const> headTableBytes);
126
127 void parseHheaTable(nonstd::span<std::byte const> bytes);
128
129 void parseNameTable(nonstd::span<std::byte const> bytes);
130
131 void parseOS2Table(nonstd::span<std::byte const> bytes);
132
135 [[nodiscard]] UnicodeRanges parseCharacterMap();
136
137
141 void parseMaxpTable(nonstd::span<std::byte const> bytes);
142
146 bool getGlyphBytes(GlyphID glyph_id, nonstd::span<std::byte const> &bytes) const noexcept;
147
151 bool updateGlyphMetrics(GlyphID glyph_id, GlyphMetrics &metrics, GlyphID kern_glyph1_id=GlyphID{}, GlyphID kern_glyph2_id=GlyphID{}) const noexcept;
152
153 bool loadSimpleGlyph(nonstd::span<std::byte const> bytes, Path &glyph) const noexcept;
154
163 bool loadCompoundGlyph(nonstd::span<std::byte const> bytes, Path &glyph, GlyphID &metrics_glyph_id) const noexcept;
164
172 bool loadCompoundGlyphMetrics(nonstd::span<std::byte const> bytes, GlyphID &metrics_glyph_id) const noexcept;
173
174
175};
176
177}
178
STL namespace.
Definition Path.hpp:29
Definition tagged_id.hpp:17
Definition Font.hpp:25
Definition GlyphMetrics.hpp:17
Definition TrueTypeFont.hpp:13
TrueTypeFont(nonstd::span< std::byte const > bytes)
Definition TrueTypeFont.hpp:73
bool loadGlyphMetrics(GlyphID glyph_id, GlyphMetrics &metrics, GlyphID lookahead_glyph_id=GlyphID{}) const noexcept override
std::optional< GlyphID > loadGlyph(GlyphID glyph_id, Path &path) const noexcept override
GlyphID 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 UnicodeRanges.hpp:13