-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcompiler.h
More file actions
401 lines (330 loc) · 13.4 KB
/
compiler.h
File metadata and controls
401 lines (330 loc) · 13.4 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#ifndef IFT_ENCODER_COMPILER_H_
#define IFT_ENCODER_COMPILER_H_
#include <cstdint>
#include <random>
#include <string>
#include <vector>
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "hb-subset.h"
#include "ift/common/compat_id.h"
#include "ift/common/font_data.h"
#include "ift/common/int_set.h"
#include "ift/encoder/activation_condition.h"
#include "ift/encoder/subset_definition.h"
#include "ift/encoder/types.h"
#include "ift/table_keyed_diff.h"
namespace ift::encoder {
/*
* IFT font compiler.
*
* The compiler is configured with a description of a desired segmentation
* of an IFT font and then compiles an original non IFT font into an
* IFT font following the configured segmentation.
*/
class Compiler {
public:
// TODO(garretrieger): add api to configure brotli quality level (for glyph
// and table keyed). Default to 11 but in tests run
// lower quality.
Compiler()
: face_(ift::common::make_hb_face(nullptr))
{}
Compiler(const Compiler&) = delete;
Compiler(Compiler&& other) = delete;
Compiler& operator=(const Compiler&) = delete;
Compiler& operator=(Compiler&& other) = delete;
/*
* Configures how many graph levels can be reached from each node in the
* encoded graph. Defaults to 1.
*/
void SetJumpAhead(uint32_t count) { this->jump_ahead_ = count; }
/*
* If enabled then for jump ahead entries preload lists will be used instead
* of a single patch which jumps multiple levels.
*/
void SetUsePrefetchLists(bool value) { this->use_prefetch_lists_ = value; }
void SetWoff2Encode(bool value) { this->woff2_encode_ = value; }
/*
* Adds a segmentation of glyph data.
*
* In the generated encoding there will be one glyph keyed patch (containing
* all data for all of the glyphs in the segment) per segment and unique
* design space configuration.
*
* An id is provided which uniquely identifies this segment and can be used to
* specify dependencies against this segment.
*/
absl::Status AddGlyphDataPatch(uint32_t patch_id,
const ift::common::IntSet& gids);
/*
* Adds a condition which may trigger the inclusion of a glyph data patch.
*/
absl::Status AddGlyphDataPatchCondition(proto::PatchMap::Entry condition);
void SetFace(hb_face_t* face) { face_.reset(hb_face_reference(face)); }
/*
* Configure the base subset to cover the provided codepoints, and the set of
* layout features retained by default in the harfbuzz subsetter.
*/
template <typename Set>
absl::Status SetInitSubset(const Set& init_codepoints) {
if (!init_subset_.Empty()) {
return absl::FailedPreconditionError("Base subset has already been set.");
}
init_subset_.codepoints.insert(init_codepoints.begin(),
init_codepoints.end());
return absl::OkStatus();
}
/*
* Adds a segment around which the non glyph data in the font will be split.
*/
template <typename Set>
void AddNonGlyphDataSegment(const Set& codepoints) {
SubsetDefinition def;
def.codepoints.insert(codepoints.begin(), codepoints.end());
extension_subsets_.push_back(def);
}
/*
* Adds a segment around which the non glyph data in the font will be split.
*/
void AddNonGlyphDataSegment(const SubsetDefinition& segment) {
extension_subsets_.push_back(segment);
}
/*
* Marks the provided group offeature tags as optional. In the dependent
* patch graph it will be possible to add support for the features at any
* node via a patch. Once enabled data for all codepoints and those features
* will always be available.
*/
void AddFeatureGroupSegment(const absl::btree_set<hb_tag_t>& feature_tag);
void AddDesignSpaceSegment(const design_space_t& space);
struct Encoding {
ift::common::FontData init_font;
absl::flat_hash_map<std::string, ift::common::FontData> patches;
};
/*
* Create an IFT encoded version of 'font' that initially supports
* the configured init subset but can be extended via patches to support any
* combination of of extension subsets.
*
* Returns: the IFT encoded initial font. Patches() will be populated with the
* set of associated patch files.
*/
absl::StatusOr<Encoding> Compile() const;
static absl::StatusOr<ift::common::FontData> RoundTripWoff2(
absl::string_view font, bool glyf_transform = true);
public:
absl::Status SetInitSubsetFromDef(const SubsetDefinition& init_subset) {
if (!init_subset_.Empty()) {
return absl::FailedPreconditionError("Base subset has already been set.");
}
init_subset_ = init_subset;
return absl::OkStatus();
}
struct Jump {
Jump(SubsetDefinition start_, SubsetDefinition end_)
: start(start_), end(end_) {}
SubsetDefinition start;
SubsetDefinition end;
bool operator==(const Jump& other) const {
return start == other.start && end == other.end;
}
template <typename H>
friend H AbslHashValue(H h, const Jump& s) {
return H::combine(std::move(h), s.start, s.end);
}
};
/*
* An edge in an IFT patch graph, traversing this edge adds one or more
* subsets to the font.
*/
class Edge {
public:
Edge(std::initializer_list<SubsetDefinition> values) : subsets_(values) {
for (const auto& s : subsets_) {
combined_.Union(s);
}
}
void Add(const SubsetDefinition& s) {
subsets_.insert(subsets_.begin(), s);
combined_.Union(s);
}
bool operator==(const Edge& other) const {
return subsets_ == other.subsets_;
}
// Returns the total effective subset definition added by this edge.
const SubsetDefinition& Combined() const { return combined_; }
bool ChangesDesignSpace(const SubsetDefinition& base) const {
SubsetDefinition combined_and_base = Combined();
combined_and_base.Union(base);
return combined_and_base.design_space != base.design_space;
}
std::vector<Jump> Jumps(const SubsetDefinition& base,
bool use_prefetch_lists) const {
std::vector<Jump> result;
if (!use_prefetch_lists) {
SubsetDefinition next = base;
next.Union(Combined());
if (next == base) {
// Base does not need to be extended further
return result;
}
result.push_back(Jump(base, next));
} else {
SubsetDefinition current_base = base;
for (const auto& s : subsets_) {
SubsetDefinition next = current_base;
next.Union(s);
if (!(next == current_base)) {
result.push_back(Jump(current_base, next));
current_base = next;
}
}
}
return result;
}
const std::vector<SubsetDefinition>& Subsets() const { return subsets_; }
private:
std::vector<SubsetDefinition> subsets_;
SubsetDefinition combined_;
};
std::vector<Edge> OutgoingEdges(const SubsetDefinition& base,
uint32_t choose) const;
private:
struct ProcessingContext;
// Returns the font subset which would be reach if all segments where added to
// the font.
absl::StatusOr<ift::common::FontData> FullyExpandedSubset(
const ProcessingContext& context) const;
static void AppendLiteralToTemplate(absl::string_view value,
std::vector<uint8_t>& out) {
out.push_back(value.length());
out.insert(out.end(), value.begin(), value.end());
}
std::vector<uint8_t> UrlTemplate(uint32_t patch_set_id) const {
constexpr uint8_t insert_id_op_code = 128;
std::vector<uint8_t> out;
if (patch_set_id == 0) {
// patch_set_id 0 is always used for table keyed patches
out.push_back(insert_id_op_code);
AppendLiteralToTemplate(".ift_tk", out);
return out;
}
// All other ids are for glyph keyed.
AppendLiteralToTemplate(absl::StrCat(patch_set_id, "_"), out);
out.push_back(insert_id_op_code);
AppendLiteralToTemplate(".ift_gk", out);
return out;
}
/*
* Create an IFT encoded version of 'font' that initially supports
* 'base_subset' but can be extended via patches to support any combination of
* 'subsets'.
*
* Returns: the IFT encoded initial font. Patches() will be populated with the
* set of associated patch files.
*/
absl::StatusOr<ift::common::FontData> Compile(
ProcessingContext& context, const SubsetDefinition& base_subset,
bool is_root = true) const;
/*
* Returns true if this encoding will contain both glyph keyed and table keyed
* patches.
*/
bool IsMixedMode() const { return !glyph_data_patches_.empty(); }
absl::Status EnsureGlyphKeyedPatchesPopulated(
ProcessingContext& context, const design_space_t& design_space,
std::vector<uint8_t>& url_template,
ift::common::CompatId& compat_id) const;
absl::Status PopulateGlyphKeyedPatchMap(
ift::proto::PatchMap& patch_map) const;
std::vector<ActivationCondition> EdgesToActivationConditions(
ProcessingContext& context, const SubsetDefinition& node_subset,
absl::Span<const Compiler::Edge> edges, proto::PatchEncoding encoding,
absl::flat_hash_map<segment_index_t, SubsetDefinition>& segments) const;
absl::Status PopulateTableKeyedPatchMap(
ProcessingContext& context, const SubsetDefinition& base_subset,
const std::vector<Compiler::Edge>& edges, proto::PatchEncoding encoding,
proto::PatchMap& table_keyed_patch_map) const;
absl::StatusOr<hb_subset_plan_t*> CreateSubsetPlan(
const ProcessingContext& context, hb_face_t* font,
const SubsetDefinition& def) const;
absl::StatusOr<ift::common::hb_face_unique_ptr> CutSubsetFaceBuilder(
const ProcessingContext& context, hb_face_t* font,
const SubsetDefinition& def) const;
absl::StatusOr<ift::common::FontData> GenerateBaseGvar(
const ProcessingContext& context, hb_face_t* font,
const design_space_t& design_space) const;
absl::StatusOr<ift::common::FontData> GenerateBaseCff2(
const ProcessingContext& context, hb_face_t* font,
const design_space_t& design_space) const;
void SetMixedModeSubsettingFlagsIfNeeded(const ProcessingContext& context,
hb_subset_input_t* input) const;
absl::StatusOr<ift::common::FontData> CutSubset(
const ProcessingContext& context, hb_face_t* font,
const SubsetDefinition& def, bool generate_glyph_keyed_bases) const;
absl::StatusOr<ift::common::FontData> Instance(
const ProcessingContext& context, hb_face_t* font,
const design_space_t& design_space) const;
absl::StatusOr<std::unique_ptr<const ift::common::BinaryDiff>> GetDifferFor(
const ift::common::FontData& font_data, ift::common::CompatId compat_id,
bool replace_url_template) const;
static ift::TableKeyedDiff* FullFontTableKeyedDiff(
ift::common::CompatId base_compat_id) {
return new TableKeyedDiff(base_compat_id);
}
static ift::TableKeyedDiff* MixedModeTableKeyedDiff(
ift::common::CompatId base_compat_id) {
return new TableKeyedDiff(base_compat_id,
{"IFTX", "glyf", "loca", "gvar", "CFF ", "CFF2"});
}
static ift::TableKeyedDiff* ReplaceIftMapTableKeyedDiff(
ift::common::CompatId base_compat_id) {
// the replacement differ is used during design space expansions, both
// gvar and "IFT " are overwritten to be compatible with the new design
// space. Glyph segment patches for all prev loaded glyphs will be
// downloaded to repopulate variation data for any already loaded glyphs.
return new TableKeyedDiff(base_compat_id, {"glyf", "loca", "CFF "},
{"IFTX", "gvar", "CFF2"});
}
bool AllocatePatchSet(ProcessingContext& context,
const design_space_t& design_space,
std::vector<uint8_t>& url_template,
ift::common::CompatId& compat_id) const;
ift::common::hb_face_unique_ptr face_;
absl::btree_map<uint32_t, ift::common::IntSet> glyph_data_patches_;
std::vector<proto::PatchMap::Entry> glyph_patch_conditions_;
SubsetDefinition init_subset_;
std::vector<SubsetDefinition> extension_subsets_;
uint32_t jump_ahead_ = 1;
uint32_t next_id_ = 0;
bool use_prefetch_lists_ = false;
bool woff2_encode_ = false;
struct ProcessingContext {
ProcessingContext(uint32_t next_id)
: gen_(),
random_values_(0, std::numeric_limits<uint32_t>::max()),
next_id_(next_id) {}
std::mt19937 gen_;
std::uniform_int_distribution<uint32_t> random_values_;
ift::common::FontData fully_expanded_subset_;
bool force_long_loca_and_gvar_ = false;
uint32_t next_id_ = 0;
uint32_t next_patch_set_id_ =
1; // id 0 is reserved for table keyed patches.
absl::flat_hash_map<design_space_t, std::vector<uint8_t>>
patch_set_url_templates_;
absl::flat_hash_map<design_space_t, ift::common::CompatId>
glyph_keyed_compat_ids_;
absl::flat_hash_map<SubsetDefinition, ift::common::FontData> built_subsets_;
absl::flat_hash_map<std::string, ift::common::FontData> patches_;
absl::flat_hash_map<Jump, uint32_t> table_keyed_patch_id_map_;
ift::common::IntSet built_table_keyed_patches_;
SubsetDefinition init_subset_;
ift::common::CompatId GenerateCompatId();
};
};
} // namespace ift::encoder
#endif // IFT_ENCODER_COMPILER_H_