Project Alice
Loading...
Searching...
No Matches
fonts.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "freetype/freetype.h"
4#include "freetype/ftglyph.h"
5#include "unordered_dense.h"
6#include "hb.h"
7#include "bmfont.hpp"
8#include <span>
9
10namespace sys {
11struct state;
12}
13
14namespace text {
15
16inline constexpr uint32_t max_texture_layers = 256;
17inline constexpr int magnification_factor = 4;
18inline constexpr int dr_size = 64 * magnification_factor;
19
20enum class font_selection {
24};
25
26uint16_t name_into_font_id(sys::state& state, std::string_view text);
27int32_t size_from_font_id(uint16_t id);
28bool is_black_from_font_id(uint16_t id);
30
32 float x = 0.0f;
33 float y = 0.0f;
34 float x_advance = 0.0f;
35 uint16_t texture_slot = 0;
36};
37
38class font_manager;
39
40enum class font_feature {
42};
43
44class font;
45
46inline bool requires_surrogate_pair(uint32_t codepoint) {
47 return codepoint >= 0x10000;
48}
49
51 uint16_t high = 0; // aka leading
52 uint16_t low = 0; // aka trailing
53};
54
56 uint32_t v = val - 0x10000;
57 uint32_t h = ((v >> 10) & 0x03FF) | 0xD800;
58 uint32_t l = (v & 0x03FF) | 0xDC00;
59 return surrogate_pair{ uint16_t(h), uint16_t(l) };
60}
61
65 hb_position_t x_advance = 0;
66 hb_position_t y_advance = 0;
67 hb_position_t x_offset = 0;
68 hb_position_t y_offset = 0;
69
70 stored_glyph() noexcept = default;
71 stored_glyph(hb_glyph_info_t const& gi, hb_glyph_position_t const& gp) {
72 codepoint = gi.codepoint;
73 cluster = gi.cluster;
74 x_advance = gp.x_advance;
75 y_advance = gp.y_advance;
76 x_offset = gp.x_offset;
77 y_offset = gp.y_offset;
78 }
79};
80
82 std::vector<stored_glyph> glyph_info;
83
84 struct no_bidi { };
85
86 stored_glyphs() = default;
87 stored_glyphs(stored_glyphs const& other) noexcept = default;
88 stored_glyphs(stored_glyphs&& other) noexcept = default;
89 stored_glyphs(sys::state& state, font_selection type, std::string const& s);
90 stored_glyphs(std::string const& s, font& f);
92 stored_glyphs(sys::state& state, font_selection type, std::span<uint16_t> s);
93 stored_glyphs(sys::state& state, font_selection type, std::span<uint16_t> s, no_bidi);
94
95 void set_text(sys::state& state, font_selection type, std::string const& s);
96 void clear() {
97 glyph_info.clear();
98 }
99};
100
101class font {
102private:
103 font(font const&) = delete;
104 font& operator=(font const&) = delete;
105public:
106 font() = default;
107
108 std::string file_name;
109 FT_Face font_face = nullptr;
110 hb_font_t* hb_font_face = nullptr;
111 hb_buffer_t* hb_buf = nullptr;
112
114 float internal_ascender = 0.0f;
115 float internal_descender = 0.0f;
116 float internal_top_adj = 0.0f;
117 ankerl::unordered_dense::map<char32_t, glyph_sub_offset> glyph_positions;
118 std::vector<uint32_t> textures;
119 std::array<FT_ULong, 256> win1252_codepoints;
120
121 uint16_t first_free_slot = 0;
122 std::unique_ptr<FT_Byte[]> file_data;
124
125 ~font();
126 bool can_display(char32_t ch_in) const;
127 void make_glyph(char32_t ch_in);
128 float base_glyph_width(char32_t ch_in);
129 float line_height(int32_t size) const;
130 float ascender(int32_t size) const;
131 float descender(int32_t size) const;
132 float top_adjustment(int32_t size) const;
133 float text_extent(sys::state& state, stored_glyphs const& txt, uint32_t starting_offset, uint32_t count, int32_t size);
134 void remake_cache(sys::state& state, font_selection type, stored_glyphs& txt, std::string const& source);
135 void remake_cache(stored_glyphs& txt, std::string const& source);
136 void remake_cache(sys::state& state, font_selection type, stored_glyphs& txt, std::span<uint16_t> source);
138
139 friend class font_manager;
140
141 font(font&& o) noexcept : file_name(std::move(o.file_name)), textures(std::move(o.textures)), glyph_positions(std::move(o.glyph_positions)), file_data(std::move(o.file_data)), first_free_slot(o.first_free_slot), only_raw_codepoints(o.only_raw_codepoints) {
142 font_face = o.font_face;
143 o.font_face = nullptr;
144 hb_font_face = o.hb_font_face;
145 o.hb_font_face = nullptr;
146 hb_buf = o.hb_buf;
147 o.hb_buf = nullptr;
148 internal_line_height = o.internal_line_height;
149 internal_ascender = o.internal_ascender;
150 internal_descender = o.internal_descender;
151 internal_top_adj = o.internal_top_adj;
152 }
153 font& operator=(font&& o) noexcept {
154 file_name = std::move(o.file_name);
155 file_data = std::move(o.file_data);
156 glyph_positions = std::move(o.glyph_positions);
157 textures = std::move(o.textures);
158 font_face = o.font_face;
159 o.font_face = nullptr;
160 hb_font_face = o.hb_font_face;
161 o.hb_font_face = nullptr;
162 hb_buf = o.hb_buf;
163 o.hb_buf = nullptr;
164 internal_line_height = o.internal_line_height;
165 internal_ascender = o.internal_ascender;
166 internal_descender = o.internal_descender;
167 internal_top_adj = o.internal_top_adj;
168 first_free_slot = o.first_free_slot;
169 only_raw_codepoints = o.only_raw_codepoints;
170 }
171};
172
174public:
175 font_manager();
177
178 ankerl::unordered_dense::map<uint16_t, dcon::text_key> font_names;
179 ankerl::unordered_dense::map<uint16_t, bm_font> bitmap_fonts;
180 FT_Library ft_library;
181private:
182 std::vector<font> font_array;
183 dcon::locale_id current_locale;
184public:
185 std::vector<uint8_t> compiled_ubrk_rules;
186 bool map_font_is_black = false;
187
188 dcon::locale_id get_current_locale() const {
189 return current_locale;
190 }
191 void change_locale(sys::state& state, dcon::locale_id l);
193 void load_font(font& fnt, char const* file_data, uint32_t file_size);
194 float line_height(sys::state& state, uint16_t font_id);
195 float text_extent(sys::state& state, stored_glyphs const& txt, uint32_t starting_offset, uint32_t count, uint16_t font_id);
196 void set_classic_fonts(bool v);
197};
198
199std::string_view classic_unligate_utf8(text::font& font, char32_t c);
200
201} // namespace text
void change_locale(sys::state &state, dcon::locale_id l)
Definition: fonts.cpp:281
std::vector< uint8_t > compiled_ubrk_rules
Definition: fonts.hpp:185
dcon::locale_id get_current_locale() const
Definition: fonts.hpp:188
void load_font(font &fnt, char const *file_data, uint32_t file_size)
Definition: fonts.cpp:438
bool map_font_is_black
Definition: fonts.hpp:186
FT_Library ft_library
Definition: fonts.hpp:180
float line_height(sys::state &state, uint16_t font_id)
Definition: fonts.cpp:151
ankerl::unordered_dense::map< uint16_t, bm_font > bitmap_fonts
Definition: fonts.hpp:179
float text_extent(sys::state &state, stored_glyphs const &txt, uint32_t starting_offset, uint32_t count, uint16_t font_id)
Definition: fonts.cpp:138
font & get_font(sys::state &state, font_selection s=font_selection::body_font)
Definition: fonts.cpp:423
ankerl::unordered_dense::map< uint16_t, dcon::text_key > font_names
Definition: fonts.hpp:178
void set_classic_fonts(bool v)
Definition: fonts.cpp:885
float top_adjustment(int32_t size) const
Definition: fonts.cpp:463
float descender(int32_t size) const
Definition: fonts.cpp:460
float line_height(int32_t size) const
Definition: fonts.cpp:454
std::array< FT_ULong, 256 > win1252_codepoints
Definition: fonts.hpp:119
std::string file_name
Definition: fonts.hpp:108
bool only_raw_codepoints
Definition: fonts.hpp:123
hb_font_t * hb_font_face
Definition: fonts.hpp:110
font()=default
float text_extent(sys::state &state, stored_glyphs const &txt, uint32_t starting_offset, uint32_t count, int32_t size)
Definition: fonts.cpp:875
ankerl::unordered_dense::map< char32_t, glyph_sub_offset > glyph_positions
Definition: fonts.hpp:117
float internal_ascender
Definition: fonts.hpp:114
uint16_t first_free_slot
Definition: fonts.hpp:121
font & operator=(font &&o) noexcept
Definition: fonts.hpp:153
float internal_descender
Definition: fonts.hpp:115
FT_Face font_face
Definition: fonts.hpp:109
float internal_top_adj
Definition: fonts.hpp:116
float ascender(int32_t size) const
Definition: fonts.cpp:457
std::unique_ptr< FT_Byte[]> file_data
Definition: fonts.hpp:122
void remake_cache(sys::state &state, font_selection type, stored_glyphs &txt, std::string const &source)
Definition: fonts.cpp:747
void remake_bidiless_cache(sys::state &state, font_selection type, stored_glyphs &txt, std::span< uint16_t > source)
Definition: fonts.cpp:659
float base_glyph_width(char32_t ch_in)
Definition: fonts.cpp:471
font(font &&o) noexcept
Definition: fonts.hpp:141
hb_buffer_t * hb_buf
Definition: fonts.hpp:111
void make_glyph(char32_t ch_in)
Definition: fonts.cpp:478
bool can_display(char32_t ch_in) const
Definition: fonts.cpp:467
float internal_line_height
Definition: fonts.hpp:113
std::vector< uint32_t > textures
Definition: fonts.hpp:118
Definition: constants.hpp:4
Definition: bmfont.cpp:118
constexpr int magnification_factor
Definition: fonts.hpp:17
std::string_view classic_unligate_utf8(text::font &font, char32_t c)
int32_t size_from_font_id(uint16_t id)
Definition: fonts.cpp:119
font_selection
Definition: fonts.hpp:20
constexpr int dr_size
Definition: fonts.hpp:18
constexpr uint32_t max_texture_layers
Definition: fonts.hpp:16
font_feature
Definition: fonts.hpp:40
bool is_black_from_font_id(uint16_t id)
Definition: fonts.cpp:127
surrogate_pair make_surrogate_pair(uint32_t val) noexcept
Definition: fonts.hpp:55
uint16_t name_into_font_id(sys::state &state, std::string_view txt)
Definition: fonts.cpp:93
bool requires_surrogate_pair(uint32_t codepoint)
Definition: fonts.hpp:46
font_selection font_index_from_font_id(sys::state &state, uint16_t id)
Definition: fonts.cpp:130
uint uint32_t
uint16_t texture_slot
Definition: fonts.hpp:35
stored_glyph() noexcept=default
uint32_t cluster
Definition: fonts.hpp:64
hb_position_t y_offset
Definition: fonts.hpp:68
uint32_t codepoint
Definition: fonts.hpp:63
hb_position_t y_advance
Definition: fonts.hpp:66
hb_position_t x_advance
Definition: fonts.hpp:65
hb_position_t x_offset
Definition: fonts.hpp:67
stored_glyphs()=default
stored_glyphs(stored_glyphs const &other) noexcept=default
std::vector< stored_glyph > glyph_info
Definition: fonts.hpp:82
void set_text(sys::state &state, font_selection type, std::string const &s)
Definition: fonts.cpp:572
stored_glyphs(stored_glyphs &&other) noexcept=default