-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfont2ift.cc
More file actions
362 lines (312 loc) · 12.3 KB
/
font2ift.cc
File metadata and controls
362 lines (312 loc) · 12.3 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
#include <google/protobuf/text_format.h>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <iostream>
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "hb.h"
#include "ift/common/axis_range.h"
#include "ift/common/font_data.h"
#include "ift/common/font_helper.h"
#include "ift/common/int_set.h"
#include "ift/common/try.h"
#include "ift/config/auto_segmenter_config.h"
#include "ift/config/load_codepoints.h"
#include "ift/config/segmentation_plan.pb.h"
#include "ift/config/segmenter_config.pb.h"
#include "ift/config/segmenter_config_util.h"
#include "ift/encoder/activation_condition.h"
#include "ift/encoder/compiler.h"
#include "ift/encoder/glyph_segmentation.h"
#include "ift/encoder/subset_definition.h"
#include "util/auto_config_flags.h"
using ift::config::ActivationConditionProto;
using ift::config::DesignSpace;
using ift::config::SegmentationPlan;
/*
* Utility that converts a standard font file into an IFT font file optionally
* following a supplied segmentation plan.
*
* Configuration is provided as a textproto file following the
* segmentation_plan.proto schema.
*
* If no configuration is supplied it will be auto generated.
*/
ABSL_FLAG(std::string, input_font, "in.ttf",
"Name of the font to convert to IFT.");
ABSL_FLAG(std::string, plan, "auto",
"Path to a plan file which is a textproto following the "
"segmentation_plan.proto schema. If set to \"auto\", then "
"segmentation plan will be automatically generated.");
ABSL_FLAG(std::string, output_path, "./",
"Path to write output files under (base font and patches).");
ABSL_FLAG(std::string, output_font, "out.woff2",
"Name of the outputted base font.");
ABSL_FLAG(bool, woff2_encode, true,
"If enabled the output font will be woff2 encoded. Transformations "
"in woff2 will be disabled when necessary to keep the woff2 encoding "
"compatible with IFT.");
ABSL_FLAG(
int, verbosity, 0,
"Log verbosity level from. 0 is least verbose, higher values are more.");
using absl::btree_set;
using absl::flat_hash_map;
using absl::Status;
using absl::StatusOr;
using absl::StrCat;
using ift::common::AxisRange;
using ift::common::CodepointSet;
using ift::common::FontData;
using ift::common::FontHelper;
using ift::common::hb_blob_unique_ptr;
using ift::common::hb_face_unique_ptr;
using ift::common::IntSet;
using ift::common::make_hb_blob;
using ift::common::SegmentSet;
using ift::config::AutoSegmenterConfig;
using ift::encoder::ActivationCondition;
using ift::encoder::Compiler;
using ift::encoder::design_space_t;
using ift::encoder::GlyphSegmentation;
using ift::encoder::SubsetDefinition;
// TODO(garretrieger): add check that all glyph patches have at least one
// activation condition.
// TODO(garretrieger): add check that warns when not all parts of the input font
// are reachable in the generated encoding.
// (all glyph ids covered by a patch, all codepoints, etc,
// covered by non glyph segments).
StatusOr<hb_face_unique_ptr> load_font(const char* filename) {
return TRY(ift::config::LoadFile(filename)).face();
}
Status write_file(const std::string& name, const FontData& data) {
std::ofstream output(name,
std::ios::out | std::ios::binary | std::ios::trunc);
if (!output.is_open()) {
return absl::NotFoundError(StrCat("File ", name, " was not found."));
}
output.write(data.data(), data.size());
if (output.bad()) {
output.close();
return absl::InternalError(StrCat("Failed to write to ", name, "."));
}
output.close();
return absl::OkStatus();
}
void write_patch(const std::string& url, const FontData& patch) {
std::string output_path = absl::GetFlag(FLAGS_output_path);
std::cerr << " Writing patch: " << StrCat(output_path, "/", url)
<< std::endl;
auto sc = write_file(StrCat(output_path, "/", url), patch);
if (!sc.ok()) {
std::cerr << sc.message() << std::endl;
exit(-1);
}
}
int write_output(const Compiler::Encoding& encoding) {
std::string output_path = absl::GetFlag(FLAGS_output_path);
std::string output_font = absl::GetFlag(FLAGS_output_font);
std::cerr << " Writing init font: " << StrCat(output_path, "/", output_font)
<< std::endl;
auto sc =
write_file(StrCat(output_path, "/", output_font), encoding.init_font);
if (!sc.ok()) {
std::cerr << sc.message() << std::endl;
return -1;
}
for (const auto& p : encoding.patches) {
write_patch(p.first, p.second);
}
return 0;
}
StatusOr<design_space_t> to_design_space(const DesignSpace& proto) {
design_space_t result;
for (const auto& [tag_str, range_proto] : proto.ranges()) {
auto range = TRY(AxisRange::Range(range_proto.start(), range_proto.end()));
result[FontHelper::ToTag(tag_str)] = range;
}
return result;
}
ActivationCondition FromProto(const ActivationConditionProto& condition) {
// TODO(garretrieger): once glyph segmentation activation conditions can
// support features copy those here.
std::vector<SegmentSet> groups;
for (const auto& group : condition.required_segments()) {
SegmentSet set;
set.insert(group.values().begin(), group.values().end());
groups.push_back(set);
}
return ActivationCondition::composite_condition(groups,
condition.activated_patch());
}
Status ConfigureCompiler(SegmentationPlan plan, Compiler& compiler) {
// First configure the glyph keyed segments, including features deps
for (const auto& [id, gids] : plan.glyph_patches()) {
TRYV(compiler.AddGlyphDataPatch(id, ift::config::Values(gids)));
}
std::vector<ActivationCondition> activation_conditions;
for (const auto& c : plan.glyph_patch_conditions()) {
activation_conditions.push_back(FromProto(c));
}
flat_hash_map<uint32_t, SubsetDefinition> segments;
for (const auto& [id, set] : plan.segments()) {
auto& segment = segments[id];
for (hb_codepoint_t cp : set.codepoints().values()) {
segment.codepoints.insert(cp);
}
for (const std::string& tag : set.features().values()) {
segment.feature_tags.insert(FontHelper::ToTag(tag));
}
}
auto condition_entries =
TRY(ActivationCondition::ActivationConditionsToPatchMapEntries(
activation_conditions, segments));
for (const auto& entry : condition_entries) {
TRYV(compiler.AddGlyphDataPatchCondition(entry));
}
// Initial subset definition
auto init_codepoints = ift::config::Values(plan.initial_codepoints());
auto init_glyphs = ift::config::Values(plan.initial_glyphs());
auto init_features = ift::config::TagValues(plan.initial_features());
auto init_segments = ift::config::Values(plan.initial_segments());
auto init_design_space = TRY(to_design_space(plan.initial_design_space()));
SubsetDefinition init_subset;
init_subset.codepoints.insert(init_codepoints.begin(), init_codepoints.end());
init_subset.gids.insert(init_glyphs.begin(), init_glyphs.end());
for (const auto segment_id : init_segments) {
auto segment = segments.find(segment_id);
if (segment == segments.end()) {
return absl::InvalidArgumentError(
StrCat("Segment id, ", segment_id, ", not found."));
}
init_subset.codepoints.union_set(segment->second.codepoints);
init_subset.feature_tags.insert(segment->second.feature_tags.begin(),
segment->second.feature_tags.end());
}
init_subset.feature_tags = init_features;
init_subset.design_space = init_design_space;
TRYV(compiler.SetInitSubsetFromDef(init_subset));
// Next configure the table keyed segments
for (const auto& codepoints : plan.non_glyph_codepoint_segmentation()) {
compiler.AddNonGlyphDataSegment(ift::config::Values(codepoints));
}
for (const auto& features : plan.non_glyph_feature_segmentation()) {
compiler.AddFeatureGroupSegment(ift::config::TagValues(features));
}
for (const auto& design_space_proto :
plan.non_glyph_design_space_segmentation()) {
auto design_space = TRY(to_design_space(design_space_proto));
compiler.AddDesignSpaceSegment(design_space);
}
for (const auto& segment_ids : plan.non_glyph_segments()) {
// Because we're using (codepoints or features) we can union up to the
// combined segment.
SubsetDefinition combined;
for (const auto& segment_id : segment_ids.values()) {
auto segment = segments.find(segment_id);
if (segment == segments.end()) {
return absl::InvalidArgumentError(
StrCat("Segment id, ", segment_id, ", not found."));
}
combined.Union(segment->second);
}
compiler.AddNonGlyphDataSegment(combined);
}
// Lastly graph shape parameters
if (plan.jump_ahead() > 1) {
compiler.SetJumpAhead(plan.jump_ahead());
}
compiler.SetUsePrefetchLists(plan.use_prefetch_lists());
compiler.SetWoff2Encode(absl::GetFlag(FLAGS_woff2_encode));
// Check for unsupported settings
if (plan.include_all_segment_patches()) {
return absl::UnimplementedError(
"include_all_segment_patches is not yet supported.");
}
if (plan.max_depth() > 0) {
return absl::UnimplementedError("max_depth is not yet supported.");
}
return absl::OkStatus();
}
StatusOr<SegmentationPlan> CreateSegmentationPlan(hb_face_t* font) {
SegmentationPlan plan;
if (absl::GetFlag(FLAGS_plan).empty() ||
absl::GetFlag(FLAGS_plan) == "auto") {
std::cerr << ">> auto generating segmentation plan:" << std::endl;
std::optional<int> quality_level = std::nullopt;
if (absl::GetFlag(FLAGS_auto_config_quality) > 0) {
quality_level = absl::GetFlag(FLAGS_auto_config_quality);
}
auto config = AutoSegmenterConfig::GenerateConfig(
font, absl::GetFlag(FLAGS_auto_config_primary_script), quality_level);
if (!config.ok()) {
return absl::InternalError(
StrCat("Failed to generate config: ", config.status().message()));
}
ift::config::SegmenterConfigUtil config_util("");
auto result = config_util.RunSegmenter(font, *config);
if (!result.ok()) {
return absl::InternalError(
StrCat("Failed to run segmenter: ", result.status().message()));
}
plan = std::move(result->plan);
} else {
auto config_text = ift::config::LoadFile(absl::GetFlag(FLAGS_plan).c_str());
if (!config_text.ok()) {
return absl::InternalError(StrCat("Failed to load config file: ",
config_text.status().message()));
}
if (!google::protobuf::TextFormat::ParseFromString(config_text->str(),
&plan)) {
return absl::InternalError("Failed to parse input config.");
}
}
return plan;
}
int main(int argc, char** argv) {
absl::SetProgramUsageMessage(
"Converts OpenType and TrueType fonts into IFT encoded fonts.\n"
"\n"
"Usage: font2ift --input_font=\"myfont.ttf\" --output_path=\"ift/\" "
"--output_font=\"myfont.itf.ttf\"\n"
"\n"
"Optional: a segmentation plan can be provided with the --plan flag. If "
"one is not given then it will be generated.");
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
absl::SetGlobalVLogLevel(absl::GetFlag(FLAGS_verbosity));
auto args = absl::ParseCommandLine(argc, argv);
absl::InitializeLog();
auto font = load_font(absl::GetFlag(FLAGS_input_font).c_str());
if (!font.ok()) {
std::cerr << "Failed to load input font: " << font.status() << std::endl;
return -1;
}
auto plan = CreateSegmentationPlan(font->get());
if (!plan.ok()) {
std::cerr << plan.status().message() << std::endl;
return -1;
}
Compiler compiler;
compiler.SetFace(font->get());
auto sc = ConfigureCompiler(*plan, compiler);
if (!sc.ok()) {
std::cerr << "Failed to apply configuration to the encoder: " << sc
<< std::endl;
return -1;
}
std::cout << ">> encoding:" << std::endl;
auto encoding = compiler.Compile();
if (!encoding.ok()) {
std::cerr << "Encoding failed: " << encoding.status() << std::endl;
return -1;
}
std::cout << ">> generating output patches:" << std::endl;
return write_output(*encoding);
}