-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
shaper.h
202 lines (172 loc) · 6.18 KB
/
shaper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <vtbackend/primitives.h>
#include <text_shaper/font.h>
#include <crispy/logstore.h>
#include <crispy/point.h>
#include <crispy/size.h>
#include <libunicode/emoji_segmenter.h>
#include <libunicode/ucd.h>
#include <gsl/span>
#include <gsl/span_ext>
#include <cstdint>
#include <format>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
namespace text
{
auto const inline rasterizerLog = logstore::category("font.render", "Logs details about rendering glyphs.");
auto const inline textShapingLog = logstore::category("font.textshaping", "Logs details about text shaping.");
// NOLINTBEGIN(readability-identifier-naming)
enum class bitmap_format : uint8_t
{
alpha_mask,
rgb,
rgba
};
// NOLINTEND(readability-identifier-naming)
constexpr size_t pixel_size(bitmap_format format) noexcept
{
switch (format)
{
case bitmap_format::rgba: return 4;
case bitmap_format::rgb: return 3;
case bitmap_format::alpha_mask: return 1;
}
return 1;
}
struct rasterized_glyph
{
glyph_index index;
vtbackend::ImageSize bitmapSize; // Glyph bitmap size in pixels.
crispy::point position; // top-left position of the bitmap, relative to the basline's origin.
bitmap_format format;
std::vector<uint8_t> bitmap;
[[nodiscard]] bool valid() const
{
return bitmap.size()
== text::pixel_size(format) * unbox<size_t>(bitmapSize.width)
* unbox<size_t>(bitmapSize.height);
}
};
std::tuple<rasterized_glyph, float> scale(rasterized_glyph const& bitmap, vtbackend::ImageSize boundingBox);
struct glyph_position
{
glyph_key glyph;
crispy::point offset;
crispy::point advance;
unicode::PresentationStyle presentation {};
};
using shape_result = std::vector<glyph_position>;
class font_locator;
/**
* Platform-independent font loading, text shaping, and glyph rendering API.
*/
class shaper
{
public:
virtual ~shaper() = default;
/**
* Sets or updates DPI to the given value.
*/
virtual void set_dpi(DPI dpi) = 0;
/**
* Configures the font location API to be used.
*/
virtual void set_locator(font_locator& locator) = 0;
/**
* Clears internal caches (if any).
*/
virtual void clear_cache() = 0;
/**
* Returns a font matching the given font description.
*
* On Linux this font will be using Freetype, whereas
* on Windows it will be a DirectWrite font,
* and on Apple it will be using CoreText (but for now it'll be freetype, too).
*/
[[nodiscard]] virtual std::optional<font_key> load_font(font_description const& description,
font_size size) = 0;
/**
* Retrieves global font metrics of font identified by @p key.
*/
[[nodiscard]] virtual font_metrics metrics(font_key key) const = 0;
/**
* Shapes the given text @p text using the font face @p font.
*
* @param font font_key identifying the font to use for text shaping.
* @param font the font to use for text shaping.
* @param text the sequence of codepoints to shape (must be all of the same script).
* @param clusters codepoint clusters
* @param script the script of the given text.
* @param presentation the pre-determined presentation style that is being stored in each glyph position.
* @param result vector at which the text shaping result will be stored.
*
* The call always returns a usable shape result, optionally using font fallback if the given
* font did not satisfy.
*/
virtual void shape(font_key font,
std::u32string_view text,
gsl::span<unsigned> clusters,
unicode::Script script,
unicode::PresentationStyle presentation,
shape_result& result) = 0;
[[nodiscard]] virtual std::optional<glyph_position> shape(font_key font, char32_t codepoint) = 0;
/**
* Rasterizes (renders) the glyph using the given render mode.
*
* @param glyph glyph identifier.
* @param mode render technique to use.
*/
[[nodiscard]] virtual std::optional<rasterized_glyph> rasterize(glyph_key glyph, render_mode mode) = 0;
};
} // end namespace text
// {{{ fmtlib support
template <>
struct std::formatter<text::bitmap_format>: std::formatter<std::string_view>
{
auto format(text::bitmap_format value, auto& ctx) const
{
string_view name;
switch (value)
{
case text::bitmap_format::alpha_mask: name = "alpha_mask"; break;
case text::bitmap_format::rgb: name = "rgb"; break;
case text::bitmap_format::rgba: name = "rgba"; break;
}
return formatter<string_view>::format(name, ctx);
}
};
template <>
struct std::formatter<text::glyph_position>: std::formatter<std::string>
{
auto format(text::glyph_position const& gpos, auto& ctx) const
{
return formatter<std::string>::format(std::format("({}+{}+{}|{}+{})",
gpos.glyph.index.value,
gpos.offset.x,
gpos.offset.y,
gpos.advance.x,
gpos.advance.y),
ctx);
}
};
template <>
struct std::formatter<text::rasterized_glyph>: std::formatter<std::string>
{
auto format(text::rasterized_glyph const& glyph, auto& ctx) const
{
return formatter<std::string>::format(std::format("rasterized_glyph({}, {}+{}, {})",
glyph.index.value,
glyph.bitmapSize,
glyph.position,
glyph.format),
ctx);
}
};
// }}}