-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfont_pack.cpp
More file actions
318 lines (284 loc) · 11.1 KB
/
Copy pathfont_pack.cpp
File metadata and controls
318 lines (284 loc) · 11.1 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "font_pack.h"
#include "grapheme.h"
#include "composer.h"
#include "font_face.h"
#include "font_resolver.h"
#include "unicode_map.h"
#include "utf8.h"
#include <std/lib/buffer.h>
#include <std/sym/i_map.h>
#include <std/lib/vector.h>
#include <std/mem/obj_pool.h>
#include <std/str/builder.h>
#include <std/sys/throw.h>
#include <errno.h>
#include <string.h>
using namespace stl;
namespace {
static constexpr u16 unresolvedFace = 0;
static constexpr u16 uncoveredFace = UINT16_MAX;
struct FontpackImpl final: public Fontpack {
FontpackImpl(Composer& composer, ObjPool& pool, const StringView* names, size_t nameCount, u16 size);
u16 getPx() const override;
u16 getPy() const override;
bool hasBold() const override;
bool hasItalic() const override;
bool hasBoldItalic() const override;
// A pictogram behind a width-one codepoint can ink far past its
Font* createOptional(Composer& composer, ObjPool& pool, StringView name, u16 size, FontStyle style, FontKind kind, FontMetrics metrics);
Font* select(FontStyle style) const noexcept;
Font* faceAt(u16 index) const noexcept;
bool coversAll(Font* font, const u32* codepoints, size_t count) const;
Font* resolveFace(const u32* codepoints, size_t count) override;
void adoptFaceFor(const FontFaceMiss& miss) override;
Font* styledFace(Font* face, FontStyle style) const override;
void markUncovered(const u32* codepoints, size_t count);
bool knownUncovered(const u32* codepoints, size_t count) const;
Composer* composer_ = nullptr;
ObjPool* pool_ = nullptr;
u16 size_ = 0;
FontMetrics metrics_;
Font* regular_ = nullptr;
Font* bold_ = nullptr;
Font* italic_ = nullptr;
Font* boldItalic_ = nullptr;
Vector<Font*> fallbacks_;
UnicodeMap<u16>* faceCache_ = nullptr;
// Multi-codepoint clusters nothing serves, by content hash; the
// single-codepoint verdicts live in faceCache_.
IntMap<u64>* missedClusters_ = nullptr;
};
// Joiners and variation selectors modify a cluster but are absent from
// most cmaps; they do not participate in coverage matching.
static FontPlane clusterPlaneWish(const u32* codepoints, size_t count) {
FontPlane wish = FontPlane::Any;
for (size_t index = 0; index < count; ++index) {
const u32 codepoint = codepoints[index];
if (codepoint == 0xfe0f) {
return FontPlane::Color;
}
if (codepoint == 0xfe0e) {
return FontPlane::Mask;
}
if (emojiPresentation(codepoint)) {
wish = FontPlane::Color;
}
}
return wish;
}
static u64 clusterHash(const u32* codepoints, size_t count) {
return StringView((const u8*)(codepoints), count * sizeof(u32)).hash64();
}
static bool significantCodepoint(u32 codepoint) {
if (codepoint == 0x200d) {
return false;
}
if (codepoint >= 0xfe00 && codepoint <= 0xfe0f) {
return false;
}
if (codepoint >= 0xe0100 && codepoint <= 0xe01ef) {
return false;
}
return true;
}
}
FontpackImpl::FontpackImpl(Composer& composer, ObjPool& pool, const StringView* names, size_t nameCount, u16 size)
: composer_(&composer)
, pool_(&pool)
, size_(size)
{
const StringView primary = nameCount != 0 ? names[0] : StringView();
regular_ = composer.loadFont(pool, {primary, size, FontStyle::Regular, FontKind::Primary}, metrics_);
if (regular_ == nullptr) {
Errno(EINVAL).raise(StringBuilder() << StringView(u8"no suitable font found for ") << primary);
}
bold_ = createOptional(composer, pool, primary, size, FontStyle::Bold, FontKind::Overlay, metrics_);
italic_ = createOptional(composer, pool, primary, size, FontStyle::Italic, FontKind::Overlay, metrics_);
boldItalic_ = createOptional(composer, pool, primary, size, FontStyle::BoldItalic, FontKind::Overlay, metrics_);
if (bold_ == nullptr) {
bold_ = regular_->synthesize(pool, FontStyle::Bold);
}
if (italic_ == nullptr) {
italic_ = regular_->synthesize(pool, FontStyle::Italic);
}
if (boldItalic_ == nullptr) {
boldItalic_ = regular_->synthesize(pool, FontStyle::BoldItalic);
}
for (size_t index = 1; index < nameCount; ++index) {
Font* const fallback = createOptional(composer, pool, names[index], size, FontStyle::Regular, FontKind::Fallback, metrics_);
if (fallback != nullptr) {
fallbacks_.pushBack(fallback);
}
}
faceCache_ = UnicodeMap<u16>::create(pool);
missedClusters_ = pool.make<IntMap<u64>>(&pool);
}
Font* FontpackImpl::createOptional(Composer& composer, ObjPool& pool, StringView name, u16 size, FontStyle style, FontKind kind, FontMetrics metrics) {
try {
return composer.loadFont(pool, {name, size, style, kind}, metrics);
} catch (Exception&) {
return nullptr;
}
}
u16 FontpackImpl::getPx() const {
return metrics_.width;
}
u16 FontpackImpl::getPy() const {
return metrics_.height;
}
bool FontpackImpl::hasBold() const {
return bold_ != nullptr;
}
bool FontpackImpl::hasItalic() const {
return italic_ != nullptr;
}
bool FontpackImpl::hasBoldItalic() const {
return boldItalic_ != nullptr;
}
Font* FontpackImpl::styledFace(Font* face, FontStyle style) const {
return face == regular_ ? select(style) : face;
}
Font* FontpackImpl::select(FontStyle style) const noexcept {
switch (style) {
case FontStyle::Bold:
return bold_ != nullptr ? bold_ : regular_;
case FontStyle::Italic:
return italic_ != nullptr ? italic_ : regular_;
case FontStyle::BoldItalic:
if (boldItalic_ != nullptr) {
return boldItalic_;
}
if (italic_ != nullptr) {
return italic_;
}
return bold_ != nullptr ? bold_ : regular_;
case FontStyle::Regular:
return regular_;
}
return regular_;
}
Font* FontpackImpl::faceAt(u16 index) const noexcept {
return index == 0 ? regular_ : fallbacks_[index - 1u];
}
bool FontpackImpl::coversAll(Font* font, const u32* codepoints, size_t count) const {
for (size_t index = 0; index < count; ++index) {
if (significantCodepoint(codepoints[index]) && !font->covers(codepoints[index])) {
return false;
}
}
return true;
}
Font* FontpackImpl::resolveFace(const u32* codepoints, size_t count) {
u16* cached = nullptr;
if (count == 1) {
cached = &(*faceCache_)[codepoints[0]];
if (*cached == uncoveredFace) {
return nullptr;
}
if (*cached != unresolvedFace) {
return faceAt((u16)(*cached - 1u));
}
}
// An emoji-presentation cluster looks for a color face across the
// whole chain first, so a monochrome face cannot shadow a color
// emoji font behind it; an explicit VS15 asks for the opposite.
const FontPlane wish = clusterPlaneWish(codepoints, count);
if (wish != FontPlane::Any) {
const bool wantColor = wish == FontPlane::Color;
if (regular_->colored() == wantColor && coversAll(regular_, codepoints, count)) {
if (cached != nullptr) {
*cached = 1;
}
return regular_;
}
for (size_t index = 0; index < fallbacks_.length(); ++index) {
Font* const fallback = fallbacks_[index];
if (fallback->colored() == wantColor && coversAll(fallback, codepoints, count)) {
if (cached != nullptr) {
*cached = (u16)(index + 2u);
}
return fallback;
}
}
}
if (coversAll(regular_, codepoints, count)) {
if (cached != nullptr) {
*cached = 1;
}
return regular_;
}
for (size_t index = 0; index < fallbacks_.length(); ++index) {
Font* const fallback = fallbacks_[index];
if (coversAll(fallback, codepoints, count)) {
if (cached != nullptr) {
*cached = (u16)(index + 2u);
}
return fallback;
}
}
if (count > 1 && knownUncovered(codepoints, count)) {
return nullptr;
}
// No verdict yet: unwind the frame like a lost surface. The renderer
// catches at the top, adoptFaceFor() settles the verdict, and the
// frame re-runs.
FontFaceMiss miss;
miss.count = count < FontFaceMiss::limit ? count : FontFaceMiss::limit;
memcpy(miss.codepoints, codepoints, miss.count * sizeof(u32));
throw miss;
}
void FontpackImpl::markUncovered(const u32* codepoints, size_t count) {
if (count == 1) {
(*faceCache_)[codepoints[0]] = uncoveredFace;
return;
}
const u64 key = clusterHash(codepoints, count);
*missedClusters_->insert(key) = key;
}
bool FontpackImpl::knownUncovered(const u32* codepoints, size_t count) const {
return missedClusters_->find(clusterHash(codepoints, count)) != nullptr;
}
void FontpackImpl::adoptFaceFor(const FontFaceMiss& miss) {
u32 significant[FontFaceMiss::limit];
size_t filtered = 0;
for (size_t index = 0; index < miss.count; ++index) {
if (significantCodepoint(miss.codepoints[index])) {
significant[filtered++] = miss.codepoints[index];
}
}
if (filtered != 0) {
const FontPlane plane = clusterPlaneWish(miss.codepoints, miss.count);
for (IntrusiveNode* node = composer_->fontResolvers.mutFront(); node != composer_->fontResolvers.mutEnd(); node = node->next) {
FontResolver* const resolver = static_cast<FontResolver*>(node);
FontFace* const face = resolver->resolveCluster(significant, filtered, plane);
if (face == nullptr) {
continue;
}
FontMetrics metrics = metrics_;
Font* const font = composer_->renderFace(*pool_, face, size_, FontKind::Fallback, metrics);
// The system can answer with a face that does not actually
// cover the cluster (fontconfig matches unconditionally);
// adopting it would not settle anything.
if (font == nullptr || !coversAll(font, miss.codepoints, miss.count)) {
continue;
}
// A cluster with an explicit plane wish only adopts a face of
// that plane: a host answer on the wrong plane would shadow
// the right face further down the chain.
if (plane != FontPlane::Any && font->colored() != (plane == FontPlane::Color)) {
continue;
}
fallbacks_.pushBack(font);
return;
}
}
markUncovered(miss.codepoints, miss.count);
}
Fontpack* Fontpack::create(Composer& composer, ObjPool& pool, const StringView* names, size_t nameCount, u16 size) {
return pool.make<FontpackImpl>(composer, pool, names, nameCount, size);
}