HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
true_type_font.hpp
1// Copyright Take Vos 2019-2022.
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 "otype_sfnt.hpp"
9#include "otype_kern.hpp"
10#include "font_char_map.hpp"
11#include "../file/file_view.hpp"
12#include "../graphic_path.hpp"
13#include "../counters.hpp"
14#include "../utility/module.hpp"
15#include <memory>
16#include <filesystem>
17
18namespace hi::inline v1 {
19
20class true_type_font final : public font {
21public:
22 true_type_font(std::filesystem::path const& path) : _path(path), _view(file_view{path})
23 {
24 ++global_counter<"ttf:map">;
25 try {
26 _bytes = as_span<std::byte const>(_view);
27 parse_font_directory(_bytes);
28
29 // Clear the view to reclaim resources.
30 _view = {};
31 _bytes = {};
32 ++global_counter<"ttf:unmap">;
33
34 } catch (std::exception const& e) {
35 throw parse_error(std::format("{}: Could not parse font directory.\n{}", path.string(), e.what()));
36 }
37 }
38
39 true_type_font() = delete;
40 true_type_font(true_type_font const& other) = delete;
41 true_type_font& operator=(true_type_font const& other) = delete;
42 true_type_font(true_type_font&& other) = delete;
43 true_type_font& operator=(true_type_font&& other) = delete;
44 ~true_type_font() = default;
45
46 [[nodiscard]] bool loaded() const noexcept override
47 {
48 return to_bool(_view);
49 }
50
51 [[nodiscard]] graphic_path get_path(hi::glyph_id glyph_id) const override;
52 [[nodiscard]] float get_advance(hi::glyph_id glyph_id) const override;
53 [[nodiscard]] glyph_metrics get_metrics(hi::glyph_id glyph_id) const override;
54 [[nodiscard]] shape_run_result_type shape_run(iso_639 language, iso_15924 script, gstring run) const override;
55
56private:
59 std::filesystem::path _path;
60
65 mutable file_view _view;
66
67 float OS2_x_height = 0;
68 float OS2_cap_height = 0;
69
70 float _em_scale;
71
72 uint16_t _num_horizontal_metrics;
73
74 int num_glyphs;
75 mutable std::span<std::byte const> _bytes;
76 mutable std::span<std::byte const> _loca_table_bytes;
77 mutable std::span<std::byte const> _glyf_table_bytes;
78 mutable std::span<std::byte const> _hmtx_table_bytes;
79 mutable std::span<std::byte const> _kern_table_bytes;
80 mutable std::span<std::byte const> _GSUB_table_bytes;
81 bool _loca_is_offset32;
82
83 void cache_tables(std::span<std::byte const> bytes) const
84 {
85 _loca_table_bytes = otype_sfnt_search<"loca">(bytes);
86 _glyf_table_bytes = otype_sfnt_search<"glyf">(bytes);
87 _hmtx_table_bytes = otype_sfnt_search<"hmtx">(bytes);
88
89 // Optional tables.
90 _kern_table_bytes = otype_sfnt_search<"kern">(bytes);
91 _GSUB_table_bytes = otype_sfnt_search<"GSUB">(bytes);
92 }
93
94 void load_view() const noexcept
95 {
96 if (_view) {
97 [[likely]] return;
98 }
99
100 _view = file_view{_path};
101 _bytes = as_span<std::byte const>(_view);
102 ++global_counter<"ttf:map">;
103 cache_tables(_bytes);
104 }
105
111 void parse_font_directory(std::span<std::byte const> bytes);
112
119 [[nodiscard]] std::ptrdiff_t get_coverage_index(std::span<std::byte const> bytes, hi::glyph_id glyph);
120
123 [[nodiscard]] font::shape_run_result_type shape_run_basic(gstring run) const;
124
125 void shape_run_kern(font::shape_run_result_type &shape_result) const;
126};
127
128} // namespace hi::inline v1
Defines the file_view class.
Defined font_char_map type.
DOXYGEN BUG.
Definition algorithm.hpp:13
Definition font.hpp:30
Definition font.hpp:126
Definition glyph_metrics.hpp:16
Definition true_type_font.hpp:20
graphic_path get_path(hi::glyph_id glyph_id) const override
Load a glyph into a path.
shape_run_result_type shape_run(iso_639 language, iso_15924 script, gstring run) const override
Shape a run of graphemes.
bool loaded() const noexcept override
Return if the font is loaded.
Definition true_type_font.hpp:46
glyph_metrics get_metrics(hi::glyph_id glyph_id) const override
Load a glyph into a path.
float get_advance(hi::glyph_id glyph_id) const override
Get the advance for a glyph.
A path is a vector graphics object.
Definition graphic_path.hpp:26
ISO-15924 script code.
Definition iso_15924.hpp:17
ISO-639 language code.
Definition iso_639.hpp:23
Definition tagged_id.hpp:17
T what(T... args)