1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Library for converting WOFF2 format font files to their TTF versions.
16
17 #include "./woff2_dec.h"
18
19 #include <stdlib.h>
20 #include <algorithm>
21 #include <complex>
22 #include <cstring>
23 #include <limits>
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <memory>
28 #include <utility>
29
30 #include "mozilla/UniquePtr.h"
31 namespace std
32 {
33 using mozilla::DefaultDelete;
34 using mozilla::UniquePtr;
35 #define default_delete DefaultDelete
36 #define unique_ptr UniquePtr
37 }
38
39 #include "./decode.h"
40 #include "./buffer.h"
41 #include "./port.h"
42 #include "./round.h"
43 #include "./store_bytes.h"
44 #include "./table_tags.h"
45 #include "./variable_length.h"
46 #include "./woff2_common.h"
47
48 namespace woff2 {
49
50 namespace {
51
52 using std::string;
53 using std::vector;
54
55
56 // simple glyph flags
57 const int kGlyfOnCurve = 1 << 0;
58 const int kGlyfXShort = 1 << 1;
59 const int kGlyfYShort = 1 << 2;
60 const int kGlyfRepeat = 1 << 3;
61 const int kGlyfThisXIsSame = 1 << 4;
62 const int kGlyfThisYIsSame = 1 << 5;
63
64 // composite glyph flags
65 // See CompositeGlyph.java in sfntly for full definitions
66 const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
67 const int FLAG_WE_HAVE_A_SCALE = 1 << 3;
68 const int FLAG_MORE_COMPONENTS = 1 << 5;
69 const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
70 const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
71 const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
72
73 const size_t kCheckSumAdjustmentOffset = 8;
74
75 const size_t kEndPtsOfContoursOffset = 10;
76 const size_t kCompositeGlyphBegin = 10;
77
78 // 98% of Google Fonts have no glyph above 5k bytes
79 // Largest glyph ever observed was 72k bytes
80 const size_t kDefaultGlyphBuf = 5120;
81
82 // Over 14k test fonts the max compression ratio seen to date was ~20.
83 // >100 suggests you wrote a bad uncompressed size.
84 const float kMaxPlausibleCompressionRatio = 100.0;
85
86 // metadata for a TTC font entry
87 struct TtcFont {
88 uint32_t flavor;
89 uint32_t dst_offset;
90 uint32_t header_checksum;
91 std::vector<uint16_t> table_indices;
92 };
93
94 struct WOFF2Header {
95 uint32_t flavor;
96 uint32_t header_version;
97 uint16_t num_tables;
98 uint64_t compressed_offset;
99 uint32_t compressed_length;
100 uint32_t uncompressed_size;
101 std::vector<Table> tables; // num_tables unique tables
102 std::vector<TtcFont> ttc_fonts; // metadata to help rebuild font
103 };
104
105 /**
106 * Accumulates data we may need to reconstruct a single font. One per font
107 * created for a TTC.
108 */
109 struct WOFF2FontInfo {
110 uint16_t num_glyphs;
111 uint16_t index_format;
112 uint16_t num_hmetrics;
113 std::vector<int16_t> x_mins;
114 std::map<uint32_t, uint32_t> table_entry_by_tag;
115 };
116
117 // Accumulates metadata as we rebuild the font
118 struct RebuildMetadata {
119 uint32_t header_checksum; // set by WriteHeaders
120 std::vector<WOFF2FontInfo> font_infos;
121 // checksums for tables that have been written.
122 // (tag, src_offset) => checksum. Need both because 0-length loca.
123 std::map<std::pair<uint32_t, uint32_t>, uint32_t> checksums;
124 };
125
WithSign(int flag,int baseval)126 int WithSign(int flag, int baseval) {
127 // Precondition: 0 <= baseval < 65536 (to avoid integer overflow)
128 return (flag & 1) ? baseval : -baseval;
129 }
130
TripletDecode(const uint8_t * flags_in,const uint8_t * in,size_t in_size,unsigned int n_points,Point * result,size_t * in_bytes_consumed)131 bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
132 unsigned int n_points, Point* result, size_t* in_bytes_consumed) {
133 int x = 0;
134 int y = 0;
135
136 if (PREDICT_FALSE(n_points > in_size)) {
137 return FONT_COMPRESSION_FAILURE();
138 }
139 unsigned int triplet_index = 0;
140
141 for (unsigned int i = 0; i < n_points; ++i) {
142 uint8_t flag = flags_in[i];
143 bool on_curve = !(flag >> 7);
144 flag &= 0x7f;
145 unsigned int n_data_bytes;
146 if (flag < 84) {
147 n_data_bytes = 1;
148 } else if (flag < 120) {
149 n_data_bytes = 2;
150 } else if (flag < 124) {
151 n_data_bytes = 3;
152 } else {
153 n_data_bytes = 4;
154 }
155 if (PREDICT_FALSE(triplet_index + n_data_bytes > in_size ||
156 triplet_index + n_data_bytes < triplet_index)) {
157 return FONT_COMPRESSION_FAILURE();
158 }
159 int dx, dy;
160 if (flag < 10) {
161 dx = 0;
162 dy = WithSign(flag, ((flag & 14) << 7) + in[triplet_index]);
163 } else if (flag < 20) {
164 dx = WithSign(flag, (((flag - 10) & 14) << 7) + in[triplet_index]);
165 dy = 0;
166 } else if (flag < 84) {
167 int b0 = flag - 20;
168 int b1 = in[triplet_index];
169 dx = WithSign(flag, 1 + (b0 & 0x30) + (b1 >> 4));
170 dy = WithSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f));
171 } else if (flag < 120) {
172 int b0 = flag - 84;
173 dx = WithSign(flag, 1 + ((b0 / 12) << 8) + in[triplet_index]);
174 dy = WithSign(flag >> 1,
175 1 + (((b0 % 12) >> 2) << 8) + in[triplet_index + 1]);
176 } else if (flag < 124) {
177 int b2 = in[triplet_index + 1];
178 dx = WithSign(flag, (in[triplet_index] << 4) + (b2 >> 4));
179 dy = WithSign(flag >> 1, ((b2 & 0x0f) << 8) + in[triplet_index + 2]);
180 } else {
181 dx = WithSign(flag, (in[triplet_index] << 8) + in[triplet_index + 1]);
182 dy = WithSign(flag >> 1,
183 (in[triplet_index + 2] << 8) + in[triplet_index + 3]);
184 }
185 triplet_index += n_data_bytes;
186 // Possible overflow but coordinate values are not security sensitive
187 x += dx;
188 y += dy;
189 *result++ = {x, y, on_curve};
190 }
191 *in_bytes_consumed = triplet_index;
192 return true;
193 }
194
195 // This function stores just the point data. On entry, dst points to the
196 // beginning of a simple glyph. Returns true on success.
StorePoints(unsigned int n_points,const Point * points,unsigned int n_contours,unsigned int instruction_length,uint8_t * dst,size_t dst_size,size_t * glyph_size)197 bool StorePoints(unsigned int n_points, const Point* points,
198 unsigned int n_contours, unsigned int instruction_length,
199 uint8_t* dst, size_t dst_size, size_t* glyph_size) {
200 // I believe that n_contours < 65536, in which case this is safe. However, a
201 // comment and/or an assert would be good.
202 unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 +
203 instruction_length;
204 int last_flag = -1;
205 int repeat_count = 0;
206 int last_x = 0;
207 int last_y = 0;
208 unsigned int x_bytes = 0;
209 unsigned int y_bytes = 0;
210
211 for (unsigned int i = 0; i < n_points; ++i) {
212 const Point& point = points[i];
213 int flag = point.on_curve ? kGlyfOnCurve : 0;
214 int dx = point.x - last_x;
215 int dy = point.y - last_y;
216 if (dx == 0) {
217 flag |= kGlyfThisXIsSame;
218 } else if (dx > -256 && dx < 256) {
219 flag |= kGlyfXShort | (dx > 0 ? kGlyfThisXIsSame : 0);
220 x_bytes += 1;
221 } else {
222 x_bytes += 2;
223 }
224 if (dy == 0) {
225 flag |= kGlyfThisYIsSame;
226 } else if (dy > -256 && dy < 256) {
227 flag |= kGlyfYShort | (dy > 0 ? kGlyfThisYIsSame : 0);
228 y_bytes += 1;
229 } else {
230 y_bytes += 2;
231 }
232
233 if (flag == last_flag && repeat_count != 255) {
234 dst[flag_offset - 1] |= kGlyfRepeat;
235 repeat_count++;
236 } else {
237 if (repeat_count != 0) {
238 if (PREDICT_FALSE(flag_offset >= dst_size)) {
239 return FONT_COMPRESSION_FAILURE();
240 }
241 dst[flag_offset++] = repeat_count;
242 }
243 if (PREDICT_FALSE(flag_offset >= dst_size)) {
244 return FONT_COMPRESSION_FAILURE();
245 }
246 dst[flag_offset++] = flag;
247 repeat_count = 0;
248 }
249 last_x = point.x;
250 last_y = point.y;
251 last_flag = flag;
252 }
253
254 if (repeat_count != 0) {
255 if (PREDICT_FALSE(flag_offset >= dst_size)) {
256 return FONT_COMPRESSION_FAILURE();
257 }
258 dst[flag_offset++] = repeat_count;
259 }
260 unsigned int xy_bytes = x_bytes + y_bytes;
261 if (PREDICT_FALSE(xy_bytes < x_bytes ||
262 flag_offset + xy_bytes < flag_offset ||
263 flag_offset + xy_bytes > dst_size)) {
264 return FONT_COMPRESSION_FAILURE();
265 }
266
267 int x_offset = flag_offset;
268 int y_offset = flag_offset + x_bytes;
269 last_x = 0;
270 last_y = 0;
271 for (unsigned int i = 0; i < n_points; ++i) {
272 int dx = points[i].x - last_x;
273 if (dx == 0) {
274 // pass
275 } else if (dx > -256 && dx < 256) {
276 dst[x_offset++] = std::abs(dx);
277 } else {
278 // will always fit for valid input, but overflow is harmless
279 x_offset = Store16(dst, x_offset, dx);
280 }
281 last_x += dx;
282 int dy = points[i].y - last_y;
283 if (dy == 0) {
284 // pass
285 } else if (dy > -256 && dy < 256) {
286 dst[y_offset++] = std::abs(dy);
287 } else {
288 y_offset = Store16(dst, y_offset, dy);
289 }
290 last_y += dy;
291 }
292 *glyph_size = y_offset;
293 return true;
294 }
295
296 // Compute the bounding box of the coordinates, and store into a glyf buffer.
297 // A precondition is that there are at least 10 bytes available.
298 // dst should point to the beginning of a 'glyf' record.
ComputeBbox(unsigned int n_points,const Point * points,uint8_t * dst)299 void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) {
300 int x_min = 0;
301 int y_min = 0;
302 int x_max = 0;
303 int y_max = 0;
304
305 if (n_points > 0) {
306 x_min = points[0].x;
307 x_max = points[0].x;
308 y_min = points[0].y;
309 y_max = points[0].y;
310 }
311 for (unsigned int i = 1; i < n_points; ++i) {
312 int x = points[i].x;
313 int y = points[i].y;
314 x_min = std::min(x, x_min);
315 x_max = std::max(x, x_max);
316 y_min = std::min(y, y_min);
317 y_max = std::max(y, y_max);
318 }
319 size_t offset = 2;
320 offset = Store16(dst, offset, x_min);
321 offset = Store16(dst, offset, y_min);
322 offset = Store16(dst, offset, x_max);
323 offset = Store16(dst, offset, y_max);
324 }
325
326
SizeOfComposite(Buffer composite_stream,size_t * size,bool * have_instructions)327 bool SizeOfComposite(Buffer composite_stream, size_t* size,
328 bool* have_instructions) {
329 size_t start_offset = composite_stream.offset();
330 bool we_have_instructions = false;
331
332 uint16_t flags = FLAG_MORE_COMPONENTS;
333 while (flags & FLAG_MORE_COMPONENTS) {
334 if (PREDICT_FALSE(!composite_stream.ReadU16(&flags))) {
335 return FONT_COMPRESSION_FAILURE();
336 }
337 we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0;
338 size_t arg_size = 2; // glyph index
339 if (flags & FLAG_ARG_1_AND_2_ARE_WORDS) {
340 arg_size += 4;
341 } else {
342 arg_size += 2;
343 }
344 if (flags & FLAG_WE_HAVE_A_SCALE) {
345 arg_size += 2;
346 } else if (flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
347 arg_size += 4;
348 } else if (flags & FLAG_WE_HAVE_A_TWO_BY_TWO) {
349 arg_size += 8;
350 }
351 if (PREDICT_FALSE(!composite_stream.Skip(arg_size))) {
352 return FONT_COMPRESSION_FAILURE();
353 }
354 }
355
356 *size = composite_stream.offset() - start_offset;
357 *have_instructions = we_have_instructions;
358
359 return true;
360 }
361
Pad4(WOFF2Out * out)362 bool Pad4(WOFF2Out* out) {
363 uint8_t zeroes[] = {0, 0, 0};
364 if (PREDICT_FALSE(out->Size() + 3 < out->Size())) {
365 return FONT_COMPRESSION_FAILURE();
366 }
367 uint32_t pad_bytes = Round4(out->Size()) - out->Size();
368 if (pad_bytes > 0) {
369 if (PREDICT_FALSE(!out->Write(&zeroes, pad_bytes))) {
370 return FONT_COMPRESSION_FAILURE();
371 }
372 }
373 return true;
374 }
375
376 // Build TrueType loca table
StoreLoca(const std::vector<uint32_t> & loca_values,int index_format,uint32_t * checksum,WOFF2Out * out)377 bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
378 uint32_t* checksum, WOFF2Out* out) {
379 // TODO(user) figure out what index format to use based on whether max
380 // offset fits into uint16_t or not
381 const uint64_t loca_size = loca_values.size();
382 const uint64_t offset_size = index_format ? 4 : 2;
383 if (PREDICT_FALSE((loca_size << 2) >> 2 != loca_size)) {
384 return FONT_COMPRESSION_FAILURE();
385 }
386 std::vector<uint8_t> loca_content(loca_size * offset_size);
387 uint8_t* dst = &loca_content[0];
388 size_t offset = 0;
389 for (size_t i = 0; i < loca_values.size(); ++i) {
390 uint32_t value = loca_values[i];
391 if (index_format) {
392 offset = StoreU32(dst, offset, value);
393 } else {
394 offset = Store16(dst, offset, value >> 1);
395 }
396 }
397 *checksum = ComputeULongSum(&loca_content[0], loca_content.size());
398 if (PREDICT_FALSE(!out->Write(&loca_content[0], loca_content.size()))) {
399 return FONT_COMPRESSION_FAILURE();
400 }
401 return true;
402 }
403
404 // Reconstruct entire glyf table based on transformed original
ReconstructGlyf(const uint8_t * data,Table * glyf_table,uint32_t * glyf_checksum,Table * loca_table,uint32_t * loca_checksum,WOFF2FontInfo * info,WOFF2Out * out)405 bool ReconstructGlyf(const uint8_t* data, Table* glyf_table,
406 uint32_t* glyf_checksum, Table * loca_table,
407 uint32_t* loca_checksum, WOFF2FontInfo* info,
408 WOFF2Out* out) {
409 static const int kNumSubStreams = 7;
410 Buffer file(data, glyf_table->transform_length);
411 uint32_t version;
412 std::vector<std::pair<const uint8_t*, size_t> > substreams(kNumSubStreams);
413 const size_t glyf_start = out->Size();
414
415 if (PREDICT_FALSE(!file.ReadU32(&version))) {
416 return FONT_COMPRESSION_FAILURE();
417 }
418 if (PREDICT_FALSE(!file.ReadU16(&info->num_glyphs) ||
419 !file.ReadU16(&info->index_format))) {
420 return FONT_COMPRESSION_FAILURE();
421 }
422
423 unsigned int offset = (2 + kNumSubStreams) * 4;
424 if (PREDICT_FALSE(offset > glyf_table->transform_length)) {
425 return FONT_COMPRESSION_FAILURE();
426 }
427 // Invariant from here on: data_size >= offset
428 for (int i = 0; i < kNumSubStreams; ++i) {
429 uint32_t substream_size;
430 if (PREDICT_FALSE(!file.ReadU32(&substream_size))) {
431 return FONT_COMPRESSION_FAILURE();
432 }
433 if (PREDICT_FALSE(substream_size > glyf_table->transform_length - offset)) {
434 return FONT_COMPRESSION_FAILURE();
435 }
436 substreams[i] = std::make_pair(data + offset, substream_size);
437 offset += substream_size;
438 }
439 Buffer n_contour_stream(substreams[0].first, substreams[0].second);
440 Buffer n_points_stream(substreams[1].first, substreams[1].second);
441 Buffer flag_stream(substreams[2].first, substreams[2].second);
442 Buffer glyph_stream(substreams[3].first, substreams[3].second);
443 Buffer composite_stream(substreams[4].first, substreams[4].second);
444 Buffer bbox_stream(substreams[5].first, substreams[5].second);
445 Buffer instruction_stream(substreams[6].first, substreams[6].second);
446
447 std::vector<uint32_t> loca_values(info->num_glyphs + 1);
448 std::vector<unsigned int> n_points_vec;
449 std::unique_ptr<Point[]> points;
450 size_t points_size = 0;
451 const uint8_t* bbox_bitmap = bbox_stream.buffer();
452 // Safe because num_glyphs is bounded
453 unsigned int bitmap_length = ((info->num_glyphs + 31) >> 5) << 2;
454 if (!bbox_stream.Skip(bitmap_length)) {
455 return FONT_COMPRESSION_FAILURE();
456 }
457
458 // Temp buffer for glyph's.
459 size_t glyph_buf_size = kDefaultGlyphBuf;
460 std::unique_ptr<uint8_t[]> glyph_buf(new uint8_t[glyph_buf_size]);
461
462 info->x_mins.resize(info->num_glyphs);
463 for (unsigned int i = 0; i < info->num_glyphs; ++i) {
464 size_t glyph_size = 0;
465 uint16_t n_contours = 0;
466 bool have_bbox = false;
467 if (bbox_bitmap[i >> 3] & (0x80 >> (i & 7))) {
468 have_bbox = true;
469 }
470 if (PREDICT_FALSE(!n_contour_stream.ReadU16(&n_contours))) {
471 return FONT_COMPRESSION_FAILURE();
472 }
473
474 if (n_contours == 0xffff) {
475 // composite glyph
476 bool have_instructions = false;
477 unsigned int instruction_size = 0;
478 if (PREDICT_FALSE(!have_bbox)) {
479 // composite glyphs must have an explicit bbox
480 return FONT_COMPRESSION_FAILURE();
481 }
482
483 size_t composite_size;
484 if (PREDICT_FALSE(!SizeOfComposite(composite_stream, &composite_size,
485 &have_instructions))) {
486 return FONT_COMPRESSION_FAILURE();
487 }
488 if (have_instructions) {
489 if (PREDICT_FALSE(!Read255UShort(&glyph_stream, &instruction_size))) {
490 return FONT_COMPRESSION_FAILURE();
491 }
492 }
493
494 size_t size_needed = 12 + composite_size + instruction_size;
495 if (PREDICT_FALSE(glyph_buf_size < size_needed)) {
496 glyph_buf.reset(new uint8_t[size_needed]);
497 glyph_buf_size = size_needed;
498 }
499
500 glyph_size = Store16(glyph_buf.get(), glyph_size, n_contours);
501 if (PREDICT_FALSE(!bbox_stream.Read(glyph_buf.get() + glyph_size, 8))) {
502 return FONT_COMPRESSION_FAILURE();
503 }
504 glyph_size += 8;
505
506 if (PREDICT_FALSE(!composite_stream.Read(glyph_buf.get() + glyph_size,
507 composite_size))) {
508 return FONT_COMPRESSION_FAILURE();
509 }
510 glyph_size += composite_size;
511 if (have_instructions) {
512 glyph_size = Store16(glyph_buf.get(), glyph_size, instruction_size);
513 if (PREDICT_FALSE(!instruction_stream.Read(glyph_buf.get() + glyph_size,
514 instruction_size))) {
515 return FONT_COMPRESSION_FAILURE();
516 }
517 glyph_size += instruction_size;
518 }
519 } else if (n_contours > 0) {
520 // simple glyph
521 n_points_vec.clear();
522 unsigned int total_n_points = 0;
523 unsigned int n_points_contour;
524 for (unsigned int j = 0; j < n_contours; ++j) {
525 if (PREDICT_FALSE(
526 !Read255UShort(&n_points_stream, &n_points_contour))) {
527 return FONT_COMPRESSION_FAILURE();
528 }
529 n_points_vec.push_back(n_points_contour);
530 if (PREDICT_FALSE(total_n_points + n_points_contour < total_n_points)) {
531 return FONT_COMPRESSION_FAILURE();
532 }
533 total_n_points += n_points_contour;
534 }
535 unsigned int flag_size = total_n_points;
536 if (PREDICT_FALSE(
537 flag_size > flag_stream.length() - flag_stream.offset())) {
538 return FONT_COMPRESSION_FAILURE();
539 }
540 const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset();
541 const uint8_t* triplet_buf = glyph_stream.buffer() +
542 glyph_stream.offset();
543 size_t triplet_size = glyph_stream.length() - glyph_stream.offset();
544 size_t triplet_bytes_consumed = 0;
545 if (points_size < total_n_points) {
546 points_size = total_n_points;
547 points.reset(new Point[points_size]);
548 }
549 if (PREDICT_FALSE(!TripletDecode(flags_buf, triplet_buf, triplet_size,
550 total_n_points, points.get(), &triplet_bytes_consumed))) {
551 return FONT_COMPRESSION_FAILURE();
552 }
553 if (PREDICT_FALSE(!flag_stream.Skip(flag_size))) {
554 return FONT_COMPRESSION_FAILURE();
555 }
556 if (PREDICT_FALSE(!glyph_stream.Skip(triplet_bytes_consumed))) {
557 return FONT_COMPRESSION_FAILURE();
558 }
559 unsigned int instruction_size;
560 if (PREDICT_FALSE(!Read255UShort(&glyph_stream, &instruction_size))) {
561 return FONT_COMPRESSION_FAILURE();
562 }
563
564 if (PREDICT_FALSE(total_n_points >= (1 << 27)
565 || instruction_size >= (1 << 30))) {
566 return FONT_COMPRESSION_FAILURE();
567 }
568 size_t size_needed = 12 + 2 * n_contours + 5 * total_n_points
569 + instruction_size;
570 if (PREDICT_FALSE(glyph_buf_size < size_needed)) {
571 glyph_buf.reset(new uint8_t[size_needed]);
572 glyph_buf_size = size_needed;
573 }
574
575 glyph_size = Store16(glyph_buf.get(), glyph_size, n_contours);
576 if (have_bbox) {
577 if (PREDICT_FALSE(!bbox_stream.Read(glyph_buf.get() + glyph_size, 8))) {
578 return FONT_COMPRESSION_FAILURE();
579 }
580 } else {
581 ComputeBbox(total_n_points, points.get(), glyph_buf.get());
582 }
583 glyph_size = kEndPtsOfContoursOffset;
584 int end_point = -1;
585 for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) {
586 end_point += n_points_vec[contour_ix];
587 if (PREDICT_FALSE(end_point >= 65536)) {
588 return FONT_COMPRESSION_FAILURE();
589 }
590 glyph_size = Store16(glyph_buf.get(), glyph_size, end_point);
591 }
592
593 glyph_size = Store16(glyph_buf.get(), glyph_size, instruction_size);
594 if (PREDICT_FALSE(!instruction_stream.Read(glyph_buf.get() + glyph_size,
595 instruction_size))) {
596 return FONT_COMPRESSION_FAILURE();
597 }
598 glyph_size += instruction_size;
599
600 if (PREDICT_FALSE(!StorePoints(total_n_points, points.get(), n_contours,
601 instruction_size, glyph_buf.get(), glyph_buf_size, &glyph_size))) {
602 return FONT_COMPRESSION_FAILURE();
603 }
604 }
605
606 loca_values[i] = out->Size() - glyf_start;
607 if (PREDICT_FALSE(!out->Write(glyph_buf.get(), glyph_size))) {
608 return FONT_COMPRESSION_FAILURE();
609 }
610
611 // TODO(user) Old code aligned glyphs ... but do we actually need to?
612 if (PREDICT_FALSE(!Pad4(out))) {
613 return FONT_COMPRESSION_FAILURE();
614 }
615
616 *glyf_checksum += ComputeULongSum(glyph_buf.get(), glyph_size);
617
618 // We may need x_min to reconstruct 'hmtx'
619 if (n_contours > 0) {
620 Buffer x_min_buf(glyph_buf.get() + 2, 2);
621 if (PREDICT_FALSE(!x_min_buf.ReadS16(&info->x_mins[i]))) {
622 return FONT_COMPRESSION_FAILURE();
623 }
624 }
625 }
626
627 // glyf_table dst_offset was set by ReconstructFont
628 glyf_table->dst_length = out->Size() - glyf_table->dst_offset;
629 loca_table->dst_offset = out->Size();
630 // loca[n] will be equal the length of the glyph data ('glyf') table
631 loca_values[info->num_glyphs] = glyf_table->dst_length;
632 if (PREDICT_FALSE(!StoreLoca(loca_values, info->index_format, loca_checksum,
633 out))) {
634 return FONT_COMPRESSION_FAILURE();
635 }
636 loca_table->dst_length = out->Size() - loca_table->dst_offset;
637
638 return true;
639 }
640
FindTable(std::vector<Table * > * tables,uint32_t tag)641 Table* FindTable(std::vector<Table*>* tables, uint32_t tag) {
642 for (Table* table : *tables) {
643 if (table->tag == tag) {
644 return table;
645 }
646 }
647 return NULL;
648 }
649
650 // Get numberOfHMetrics, https://www.microsoft.com/typography/otspec/hhea.htm
ReadNumHMetrics(const uint8_t * data,size_t data_size,uint16_t * num_hmetrics)651 bool ReadNumHMetrics(const uint8_t* data, size_t data_size,
652 uint16_t* num_hmetrics) {
653 // Skip 34 to reach 'hhea' numberOfHMetrics
654 Buffer buffer(data, data_size);
655 if (PREDICT_FALSE(!buffer.Skip(34) || !buffer.ReadU16(num_hmetrics))) {
656 return FONT_COMPRESSION_FAILURE();
657 }
658 return true;
659 }
660
661 // http://dev.w3.org/webfonts/WOFF2/spec/Overview.html#hmtx_table_format
ReconstructTransformedHmtx(const uint8_t * transformed_buf,size_t transformed_size,uint16_t num_glyphs,uint16_t num_hmetrics,const std::vector<int16_t> & x_mins,uint32_t * checksum,WOFF2Out * out)662 bool ReconstructTransformedHmtx(const uint8_t* transformed_buf,
663 size_t transformed_size,
664 uint16_t num_glyphs,
665 uint16_t num_hmetrics,
666 const std::vector<int16_t>& x_mins,
667 uint32_t* checksum,
668 WOFF2Out* out) {
669 Buffer hmtx_buff_in(transformed_buf, transformed_size);
670
671 uint8_t hmtx_flags;
672 if (PREDICT_FALSE(!hmtx_buff_in.ReadU8(&hmtx_flags))) {
673 return FONT_COMPRESSION_FAILURE();
674 }
675
676 std::vector<uint16_t> advance_widths;
677 std::vector<int16_t> lsbs;
678 bool has_proportional_lsbs = (hmtx_flags & 1) == 0;
679 bool has_monospace_lsbs = (hmtx_flags & 2) == 0;
680
681 // you say you transformed but there is little evidence of it
682 if (has_proportional_lsbs && has_monospace_lsbs) {
683 return FONT_COMPRESSION_FAILURE();
684 }
685
686 assert(x_mins.size() == num_glyphs);
687
688 // num_glyphs 0 is OK if there is no 'glyf' but cannot then xform 'hmtx'.
689 if (PREDICT_FALSE(num_hmetrics > num_glyphs)) {
690 return FONT_COMPRESSION_FAILURE();
691 }
692
693 // https://www.microsoft.com/typography/otspec/hmtx.htm
694 // "...only one entry need be in the array, but that entry is required."
695 if (PREDICT_FALSE(num_hmetrics < 1)) {
696 return FONT_COMPRESSION_FAILURE();
697 }
698
699 for (uint16_t i = 0; i < num_hmetrics; i++) {
700 uint16_t advance_width;
701 if (PREDICT_FALSE(!hmtx_buff_in.ReadU16(&advance_width))) {
702 return FONT_COMPRESSION_FAILURE();
703 }
704 advance_widths.push_back(advance_width);
705 }
706
707 for (uint16_t i = 0; i < num_hmetrics; i++) {
708 int16_t lsb;
709 if (has_proportional_lsbs) {
710 if (PREDICT_FALSE(!hmtx_buff_in.ReadS16(&lsb))) {
711 return FONT_COMPRESSION_FAILURE();
712 }
713 } else {
714 lsb = x_mins[i];
715 }
716 lsbs.push_back(lsb);
717 }
718
719 for (uint16_t i = num_hmetrics; i < num_glyphs; i++) {
720 int16_t lsb;
721 if (has_monospace_lsbs) {
722 if (PREDICT_FALSE(!hmtx_buff_in.ReadS16(&lsb))) {
723 return FONT_COMPRESSION_FAILURE();
724 }
725 } else {
726 lsb = x_mins[i];
727 }
728 lsbs.push_back(lsb);
729 }
730
731 // bake me a shiny new hmtx table
732 uint32_t hmtx_output_size = 2 * num_glyphs + 2 * num_hmetrics;
733 std::vector<uint8_t> hmtx_table(hmtx_output_size);
734 uint8_t* dst = &hmtx_table[0];
735 size_t dst_offset = 0;
736 for (uint32_t i = 0; i < num_glyphs; i++) {
737 if (i < num_hmetrics) {
738 Store16(advance_widths[i], &dst_offset, dst);
739 }
740 Store16(lsbs[i], &dst_offset, dst);
741 }
742
743 *checksum = ComputeULongSum(&hmtx_table[0], hmtx_output_size);
744 if (PREDICT_FALSE(!out->Write(&hmtx_table[0], hmtx_output_size))) {
745 return FONT_COMPRESSION_FAILURE();
746 }
747
748 return true;
749 }
750
Woff2Uncompress(uint8_t * dst_buf,size_t dst_size,const uint8_t * src_buf,size_t src_size)751 bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
752 const uint8_t* src_buf, size_t src_size) {
753 size_t uncompressed_size = dst_size;
754 int ok = BrotliDecompressBuffer(src_size, src_buf,
755 &uncompressed_size, dst_buf);
756 if (PREDICT_FALSE(!ok || uncompressed_size != dst_size)) {
757 return FONT_COMPRESSION_FAILURE();
758 }
759 return true;
760 }
761
ReadTableDirectory(Buffer * file,std::vector<Table> * tables,size_t num_tables)762 bool ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
763 size_t num_tables) {
764 uint32_t src_offset = 0;
765 for (size_t i = 0; i < num_tables; ++i) {
766 Table* table = &(*tables)[i];
767 uint8_t flag_byte;
768 if (PREDICT_FALSE(!file->ReadU8(&flag_byte))) {
769 return FONT_COMPRESSION_FAILURE();
770 }
771 uint32_t tag;
772 if ((flag_byte & 0x3f) == 0x3f) {
773 if (PREDICT_FALSE(!file->ReadU32(&tag))) {
774 return FONT_COMPRESSION_FAILURE();
775 }
776 } else {
777 tag = kKnownTags[flag_byte & 0x3f];
778 }
779 uint32_t flags = 0;
780 uint8_t xform_version = (flag_byte >> 6) & 0x03;
781
782 // 0 means xform for glyph/loca, non-0 for others
783 if (tag == kGlyfTableTag || tag == kLocaTableTag) {
784 if (xform_version == 0) {
785 flags |= kWoff2FlagsTransform;
786 }
787 } else if (xform_version != 0) {
788 flags |= kWoff2FlagsTransform;
789 }
790 flags |= xform_version;
791
792 uint32_t dst_length;
793 if (PREDICT_FALSE(!ReadBase128(file, &dst_length))) {
794 return FONT_COMPRESSION_FAILURE();
795 }
796 uint32_t transform_length = dst_length;
797 if ((flags & kWoff2FlagsTransform) != 0) {
798 if (PREDICT_FALSE(!ReadBase128(file, &transform_length))) {
799 return FONT_COMPRESSION_FAILURE();
800 }
801 if (PREDICT_FALSE(tag == kLocaTableTag && transform_length)) {
802 return FONT_COMPRESSION_FAILURE();
803 }
804 }
805 if (PREDICT_FALSE(src_offset + transform_length < src_offset)) {
806 return FONT_COMPRESSION_FAILURE();
807 }
808 table->src_offset = src_offset;
809 table->src_length = transform_length;
810 src_offset += transform_length;
811
812 table->tag = tag;
813 table->flags = flags;
814 table->transform_length = transform_length;
815 table->dst_length = dst_length;
816 }
817 return true;
818 }
819
820 // Writes a single Offset Table entry
StoreOffsetTable(uint8_t * result,size_t offset,uint32_t flavor,uint16_t num_tables)821 size_t StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
822 uint16_t num_tables) {
823 offset = StoreU32(result, offset, flavor); // sfnt version
824 offset = Store16(result, offset, num_tables); // num_tables
825 unsigned max_pow2 = 0;
826 while (1u << (max_pow2 + 1) <= num_tables) {
827 max_pow2++;
828 }
829 const uint16_t output_search_range = (1u << max_pow2) << 4;
830 offset = Store16(result, offset, output_search_range); // searchRange
831 offset = Store16(result, offset, max_pow2); // entrySelector
832 // rangeShift
833 offset = Store16(result, offset, (num_tables << 4) - output_search_range);
834 return offset;
835 }
836
StoreTableEntry(uint8_t * result,uint32_t offset,uint32_t tag)837 size_t StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) {
838 offset = StoreU32(result, offset, tag);
839 offset = StoreU32(result, offset, 0);
840 offset = StoreU32(result, offset, 0);
841 offset = StoreU32(result, offset, 0);
842 return offset;
843 }
844
845 // First table goes after all the headers, table directory, etc
ComputeOffsetToFirstTable(const WOFF2Header & hdr)846 uint64_t ComputeOffsetToFirstTable(const WOFF2Header& hdr) {
847 uint64_t offset = kSfntHeaderSize +
848 kSfntEntrySize * static_cast<uint64_t>(hdr.num_tables);
849 if (hdr.header_version) {
850 offset = CollectionHeaderSize(hdr.header_version, hdr.ttc_fonts.size())
851 + kSfntHeaderSize * hdr.ttc_fonts.size();
852 for (const auto& ttc_font : hdr.ttc_fonts) {
853 offset += kSfntEntrySize * ttc_font.table_indices.size();
854 }
855 }
856 return offset;
857 }
858
Tables(WOFF2Header * hdr,size_t font_index)859 std::vector<Table*> Tables(WOFF2Header* hdr, size_t font_index) {
860 std::vector<Table*> tables;
861 if (PREDICT_FALSE(hdr->header_version)) {
862 for (auto index : hdr->ttc_fonts[font_index].table_indices) {
863 tables.push_back(&hdr->tables[index]);
864 }
865 } else {
866 for (auto& table : hdr->tables) {
867 tables.push_back(&table);
868 }
869 }
870 return tables;
871 }
872
873 // Offset tables assumed to have been written in with 0's initially.
874 // WOFF2Header isn't const so we can use [] instead of at() (which upsets FF)
ReconstructFont(uint8_t * transformed_buf,const uint32_t transformed_buf_size,RebuildMetadata * metadata,WOFF2Header * hdr,size_t font_index,WOFF2Out * out)875 bool ReconstructFont(uint8_t* transformed_buf,
876 const uint32_t transformed_buf_size,
877 RebuildMetadata* metadata,
878 WOFF2Header* hdr,
879 size_t font_index,
880 WOFF2Out* out) {
881 size_t dest_offset = out->Size();
882 uint8_t table_entry[12];
883 WOFF2FontInfo* info = &metadata->font_infos[font_index];
884 std::vector<Table*> tables = Tables(hdr, font_index);
885
886 // 'glyf' without 'loca' doesn't make sense
887 if (PREDICT_FALSE(static_cast<bool>(FindTable(&tables, kGlyfTableTag)) !=
888 static_cast<bool>(FindTable(&tables, kLocaTableTag)))) {
889 return FONT_COMPRESSION_FAILURE();
890 }
891
892 uint32_t font_checksum = metadata->header_checksum;
893 if (hdr->header_version) {
894 font_checksum = hdr->ttc_fonts[font_index].header_checksum;
895 }
896
897 uint32_t loca_checksum = 0;
898 for (size_t i = 0; i < tables.size(); i++) {
899 Table& table = *tables[i];
900
901 std::pair<uint32_t, uint32_t> checksum_key = {table.tag, table.src_offset};
902 bool reused = metadata->checksums.find(checksum_key)
903 != metadata->checksums.end();
904 if (PREDICT_FALSE(font_index == 0 && reused)) {
905 return FONT_COMPRESSION_FAILURE();
906 }
907
908 // TODO(user) a collection with optimized hmtx that reused glyf/loca
909 // would fail. We don't optimize hmtx for collections yet.
910 if (PREDICT_FALSE(static_cast<uint64_t>(table.src_offset + table.src_length)
911 > transformed_buf_size)) {
912 return FONT_COMPRESSION_FAILURE();
913 }
914
915 if (table.tag == kHheaTableTag) {
916 if (!ReadNumHMetrics(transformed_buf + table.src_offset,
917 table.src_length, &info->num_hmetrics)) {
918 return FONT_COMPRESSION_FAILURE();
919 }
920 }
921
922 uint32_t checksum = 0;
923 if (!reused) {
924 if ((table.flags & kWoff2FlagsTransform) != kWoff2FlagsTransform) {
925 if (table.tag == kHeadTableTag) {
926 if (PREDICT_FALSE(table.src_length < 12)) {
927 return FONT_COMPRESSION_FAILURE();
928 }
929 // checkSumAdjustment = 0
930 StoreU32(transformed_buf + table.src_offset, 8, 0);
931 }
932 table.dst_offset = dest_offset;
933 checksum = ComputeULongSum(transformed_buf + table.src_offset,
934 table.src_length);
935 if (PREDICT_FALSE(!out->Write(transformed_buf + table.src_offset,
936 table.src_length))) {
937 return FONT_COMPRESSION_FAILURE();
938 }
939 } else {
940 if (table.tag == kGlyfTableTag) {
941 table.dst_offset = dest_offset;
942
943 Table* loca_table = FindTable(&tables, kLocaTableTag);
944 if (PREDICT_FALSE(!ReconstructGlyf(transformed_buf + table.src_offset,
945 &table, &checksum, loca_table, &loca_checksum, info, out))) {
946 return FONT_COMPRESSION_FAILURE();
947 }
948 } else if (table.tag == kLocaTableTag) {
949 // All the work was done by ReconstructGlyf. We already know checksum.
950 checksum = loca_checksum;
951 } else if (table.tag == kHmtxTableTag) {
952 table.dst_offset = dest_offset;
953 // Tables are sorted so all the info we need has been gathered.
954 if (PREDICT_FALSE(!ReconstructTransformedHmtx(
955 transformed_buf + table.src_offset, table.src_length,
956 info->num_glyphs, info->num_hmetrics, info->x_mins, &checksum,
957 out))) {
958 return FONT_COMPRESSION_FAILURE();
959 }
960 } else {
961 return FONT_COMPRESSION_FAILURE(); // transform unknown
962 }
963 }
964 metadata->checksums[checksum_key] = checksum;
965 } else {
966 checksum = metadata->checksums[checksum_key];
967 }
968 font_checksum += checksum;
969
970 // update the table entry with real values.
971 StoreU32(table_entry, 0, checksum);
972 StoreU32(table_entry, 4, table.dst_offset);
973 StoreU32(table_entry, 8, table.dst_length);
974 if (PREDICT_FALSE(!out->Write(table_entry,
975 info->table_entry_by_tag[table.tag] + 4, 12))) {
976 return FONT_COMPRESSION_FAILURE();
977 }
978
979 // We replaced 0's. Update overall checksum.
980 font_checksum += ComputeULongSum(table_entry, 12);
981
982 if (PREDICT_FALSE(!Pad4(out))) {
983 return FONT_COMPRESSION_FAILURE();
984 }
985
986 if (PREDICT_FALSE(static_cast<uint64_t>(table.dst_offset + table.dst_length)
987 > out->Size())) {
988 return FONT_COMPRESSION_FAILURE();
989 }
990 dest_offset = out->Size();
991 }
992
993 // Update 'head' checkSumAdjustment. We already set it to 0 and summed font.
994 Table* head_table = FindTable(&tables, kHeadTableTag);
995 if (head_table) {
996 if (PREDICT_FALSE(head_table->dst_length < 12)) {
997 return FONT_COMPRESSION_FAILURE();
998 }
999 uint8_t checksum_adjustment[4];
1000 StoreU32(checksum_adjustment, 0, 0xB1B0AFBA - font_checksum);
1001 if (PREDICT_FALSE(!out->Write(checksum_adjustment,
1002 head_table->dst_offset + 8, 4))) {
1003 return FONT_COMPRESSION_FAILURE();
1004 }
1005 }
1006
1007 return true;
1008 }
1009
ReadWOFF2Header(const uint8_t * data,size_t length,WOFF2Header * hdr)1010 bool ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) {
1011 Buffer file(data, length);
1012
1013 uint32_t signature;
1014 if (PREDICT_FALSE(!file.ReadU32(&signature) || signature != kWoff2Signature ||
1015 !file.ReadU32(&hdr->flavor))) {
1016 return FONT_COMPRESSION_FAILURE();
1017 }
1018
1019 // TODO(user): Should call IsValidVersionTag() here.
1020
1021 uint32_t reported_length;
1022 if (PREDICT_FALSE(
1023 !file.ReadU32(&reported_length) || length != reported_length)) {
1024 return FONT_COMPRESSION_FAILURE();
1025 }
1026 if (PREDICT_FALSE(!file.ReadU16(&hdr->num_tables) || !hdr->num_tables)) {
1027 return FONT_COMPRESSION_FAILURE();
1028 }
1029
1030 // We don't care about these fields of the header:
1031 // uint16_t reserved
1032 // uint32_t total_sfnt_size, we don't believe this, will compute later
1033 if (PREDICT_FALSE(!file.Skip(6))) {
1034 return FONT_COMPRESSION_FAILURE();
1035 }
1036 if (PREDICT_FALSE(!file.ReadU32(&hdr->compressed_length))) {
1037 return FONT_COMPRESSION_FAILURE();
1038 }
1039 // We don't care about these fields of the header:
1040 // uint16_t major_version, minor_version
1041 if (PREDICT_FALSE(!file.Skip(2 * 2))) {
1042 return FONT_COMPRESSION_FAILURE();
1043 }
1044 uint32_t meta_offset;
1045 uint32_t meta_length;
1046 uint32_t meta_length_orig;
1047 if (PREDICT_FALSE(!file.ReadU32(&meta_offset) ||
1048 !file.ReadU32(&meta_length) ||
1049 !file.ReadU32(&meta_length_orig))) {
1050 return FONT_COMPRESSION_FAILURE();
1051 }
1052 if (meta_offset) {
1053 if (PREDICT_FALSE(
1054 meta_offset >= length || length - meta_offset < meta_length)) {
1055 return FONT_COMPRESSION_FAILURE();
1056 }
1057 }
1058 uint32_t priv_offset;
1059 uint32_t priv_length;
1060 if (PREDICT_FALSE(!file.ReadU32(&priv_offset) ||
1061 !file.ReadU32(&priv_length))) {
1062 return FONT_COMPRESSION_FAILURE();
1063 }
1064 if (priv_offset) {
1065 if (PREDICT_FALSE(
1066 priv_offset >= length || length - priv_offset < priv_length)) {
1067 return FONT_COMPRESSION_FAILURE();
1068 }
1069 }
1070 hdr->tables.resize(hdr->num_tables);
1071 if (PREDICT_FALSE(!ReadTableDirectory(
1072 &file, &hdr->tables, hdr->num_tables))) {
1073 return FONT_COMPRESSION_FAILURE();
1074 }
1075
1076 // Before we sort for output the last table end is the uncompressed size.
1077 Table& last_table = hdr->tables.back();
1078 hdr->uncompressed_size = last_table.src_offset + last_table.src_length;
1079 if (PREDICT_FALSE(hdr->uncompressed_size < last_table.src_offset)) {
1080 return FONT_COMPRESSION_FAILURE();
1081 }
1082
1083 hdr->header_version = 0;
1084
1085 if (hdr->flavor == kTtcFontFlavor) {
1086 if (PREDICT_FALSE(!file.ReadU32(&hdr->header_version))) {
1087 return FONT_COMPRESSION_FAILURE();
1088 }
1089 if (PREDICT_FALSE(hdr->header_version != 0x00010000
1090 && hdr->header_version != 0x00020000)) {
1091 return FONT_COMPRESSION_FAILURE();
1092 }
1093 uint32_t num_fonts;
1094 if (PREDICT_FALSE(!Read255UShort(&file, &num_fonts) || !num_fonts)) {
1095 return FONT_COMPRESSION_FAILURE();
1096 }
1097 hdr->ttc_fonts.resize(num_fonts);
1098
1099 for (uint32_t i = 0; i < num_fonts; i++) {
1100 TtcFont& ttc_font = hdr->ttc_fonts[i];
1101 uint32_t num_tables;
1102 if (PREDICT_FALSE(!Read255UShort(&file, &num_tables) || !num_tables)) {
1103 return FONT_COMPRESSION_FAILURE();
1104 }
1105 if (PREDICT_FALSE(!file.ReadU32(&ttc_font.flavor))) {
1106 return FONT_COMPRESSION_FAILURE();
1107 }
1108
1109 ttc_font.table_indices.resize(num_tables);
1110
1111 const Table* glyf_table = NULL;
1112 const Table* loca_table = NULL;
1113
1114 for (uint32_t j = 0; j < num_tables; j++) {
1115 unsigned int table_idx;
1116 if (PREDICT_FALSE(!Read255UShort(&file, &table_idx)) ||
1117 table_idx >= hdr->tables.size()) {
1118 return FONT_COMPRESSION_FAILURE();
1119 }
1120 ttc_font.table_indices[j] = table_idx;
1121
1122 const Table& table = hdr->tables[table_idx];
1123 if (table.tag == kLocaTableTag) {
1124 loca_table = &table;
1125 }
1126 if (table.tag == kGlyfTableTag) {
1127 glyf_table = &table;
1128 }
1129
1130 }
1131
1132 if (PREDICT_FALSE((glyf_table == NULL) != (loca_table == NULL))) {
1133 #ifdef FONT_COMPRESSION_BIN
1134 fprintf(stderr, "Cannot have just one of glyf/loca\n");
1135 #endif
1136 return FONT_COMPRESSION_FAILURE();
1137 }
1138 }
1139 }
1140
1141 const uint64_t first_table_offset = ComputeOffsetToFirstTable(*hdr);
1142
1143 hdr->compressed_offset = file.offset();
1144 if (PREDICT_FALSE(hdr->compressed_offset >
1145 std::numeric_limits<uint32_t>::max())) {
1146 return FONT_COMPRESSION_FAILURE();
1147 }
1148 uint64_t src_offset = Round4(hdr->compressed_offset + hdr->compressed_length);
1149 uint64_t dst_offset = first_table_offset;
1150
1151
1152 if (PREDICT_FALSE(src_offset > length)) {
1153 #ifdef FONT_COMPRESSION_BIN
1154 fprintf(stderr, "offset fail; src_offset %" PRIu64 " length %lu "
1155 "dst_offset %" PRIu64 "\n",
1156 src_offset, length, dst_offset);
1157 #endif
1158 return FONT_COMPRESSION_FAILURE();
1159 }
1160 if (meta_offset) {
1161 if (PREDICT_FALSE(src_offset != meta_offset)) {
1162 return FONT_COMPRESSION_FAILURE();
1163 }
1164 src_offset = Round4(meta_offset + meta_length);
1165 if (PREDICT_FALSE(src_offset > std::numeric_limits<uint32_t>::max())) {
1166 return FONT_COMPRESSION_FAILURE();
1167 }
1168 }
1169
1170 if (priv_offset) {
1171 if (PREDICT_FALSE(src_offset != priv_offset)) {
1172 return FONT_COMPRESSION_FAILURE();
1173 }
1174 src_offset = Round4(priv_offset + priv_length);
1175 if (PREDICT_FALSE(src_offset > std::numeric_limits<uint32_t>::max())) {
1176 return FONT_COMPRESSION_FAILURE();
1177 }
1178 }
1179
1180 if (PREDICT_FALSE(src_offset != Round4(length))) {
1181 return FONT_COMPRESSION_FAILURE();
1182 }
1183
1184 return true;
1185 }
1186
1187 // Write everything before the actual table data
WriteHeaders(const uint8_t * data,size_t length,RebuildMetadata * metadata,WOFF2Header * hdr,WOFF2Out * out)1188 bool WriteHeaders(const uint8_t* data, size_t length, RebuildMetadata* metadata,
1189 WOFF2Header* hdr, WOFF2Out* out) {
1190 std::vector<uint8_t> output(ComputeOffsetToFirstTable(*hdr), 0);
1191
1192 // Re-order tables in output (OTSpec) order
1193 std::vector<Table> sorted_tables(hdr->tables);
1194 if (hdr->header_version) {
1195 // collection; we have to sort the table offset vector in each font
1196 for (auto& ttc_font : hdr->ttc_fonts) {
1197 std::map<uint32_t, uint16_t> sorted_index_by_tag;
1198 for (auto table_index : ttc_font.table_indices) {
1199 sorted_index_by_tag[hdr->tables[table_index].tag] = table_index;
1200 }
1201 uint16_t index = 0;
1202 for (auto& i : sorted_index_by_tag) {
1203 ttc_font.table_indices[index++] = i.second;
1204 }
1205 }
1206 } else {
1207 // non-collection; we can just sort the tables
1208 std::sort(sorted_tables.begin(), sorted_tables.end());
1209 }
1210
1211 // Start building the font
1212 uint8_t* result = &output[0];
1213 size_t offset = 0;
1214 if (hdr->header_version) {
1215 // TTC header
1216 offset = StoreU32(result, offset, hdr->flavor); // TAG TTCTag
1217 offset = StoreU32(result, offset, hdr->header_version); // FIXED Version
1218 offset = StoreU32(result, offset, hdr->ttc_fonts.size()); // ULONG numFonts
1219 // Space for ULONG OffsetTable[numFonts] (zeroed initially)
1220 size_t offset_table = offset; // keep start of offset table for later
1221 for (size_t i = 0; i < hdr->ttc_fonts.size(); i++) {
1222 offset = StoreU32(result, offset, 0); // will fill real values in later
1223 }
1224 // space for DSIG fields for header v2
1225 if (hdr->header_version == 0x00020000) {
1226 offset = StoreU32(result, offset, 0); // ULONG ulDsigTag
1227 offset = StoreU32(result, offset, 0); // ULONG ulDsigLength
1228 offset = StoreU32(result, offset, 0); // ULONG ulDsigOffset
1229 }
1230
1231 // write Offset Tables and store the location of each in TTC Header
1232 metadata->font_infos.resize(hdr->ttc_fonts.size());
1233 for (size_t i = 0; i < hdr->ttc_fonts.size(); i++) {
1234 TtcFont& ttc_font = hdr->ttc_fonts[i];
1235
1236 // write Offset Table location into TTC Header
1237 offset_table = StoreU32(result, offset_table, offset);
1238
1239 // write the actual offset table so our header doesn't lie
1240 ttc_font.dst_offset = offset;
1241 offset = StoreOffsetTable(result, offset, ttc_font.flavor,
1242 ttc_font.table_indices.size());
1243
1244 for (const auto table_index : ttc_font.table_indices) {
1245 uint32_t tag = hdr->tables[table_index].tag;
1246 metadata->font_infos[i].table_entry_by_tag[tag] = offset;
1247 offset = StoreTableEntry(result, offset, tag);
1248 }
1249
1250 ttc_font.header_checksum = ComputeULongSum(&output[ttc_font.dst_offset],
1251 offset - ttc_font.dst_offset);
1252 }
1253 } else {
1254 metadata->font_infos.resize(1);
1255 offset = StoreOffsetTable(result, offset, hdr->flavor, hdr->num_tables);
1256 for (uint16_t i = 0; i < hdr->num_tables; ++i) {
1257 metadata->font_infos[0].table_entry_by_tag[sorted_tables[i].tag] = offset;
1258 offset = StoreTableEntry(result, offset, sorted_tables[i].tag);
1259 }
1260 }
1261
1262 if (PREDICT_FALSE(!out->Write(&output[0], output.size()))) {
1263 return FONT_COMPRESSION_FAILURE();
1264 }
1265 metadata->header_checksum = ComputeULongSum(&output[0], output.size());
1266 return true;
1267 }
1268
1269 } // namespace
1270
ComputeWOFF2FinalSize(const uint8_t * data,size_t length)1271 size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
1272 Buffer file(data, length);
1273 uint32_t total_length;
1274
1275 if (!file.Skip(16) ||
1276 !file.ReadU32(&total_length)) {
1277 return 0;
1278 }
1279 return total_length;
1280 }
1281
ConvertWOFF2ToTTF(uint8_t * result,size_t result_length,const uint8_t * data,size_t length)1282 bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
1283 const uint8_t *data, size_t length) {
1284 WOFF2MemoryOut out(result, result_length);
1285 return ConvertWOFF2ToTTF(data, length, &out);
1286 }
1287
ConvertWOFF2ToTTF(const uint8_t * data,size_t length,WOFF2Out * out)1288 bool ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
1289 WOFF2Out* out) {
1290 RebuildMetadata metadata;
1291 WOFF2Header hdr;
1292 if (!ReadWOFF2Header(data, length, &hdr)) {
1293 return FONT_COMPRESSION_FAILURE();
1294 }
1295
1296 if (!WriteHeaders(data, length, &metadata, &hdr, out)) {
1297 return FONT_COMPRESSION_FAILURE();
1298 }
1299
1300 const float compression_ratio = (float) hdr.uncompressed_size / length;
1301 if (compression_ratio > kMaxPlausibleCompressionRatio) {
1302 #ifdef FONT_COMPRESSION_BIN
1303 fprintf(stderr, "Implausible compression ratio %.01f\n", compression_ratio);
1304 #endif
1305 return FONT_COMPRESSION_FAILURE();
1306 }
1307
1308 const uint8_t* src_buf = data + hdr.compressed_offset;
1309 std::vector<uint8_t> uncompressed_buf(hdr.uncompressed_size);
1310 if (PREDICT_FALSE(!Woff2Uncompress(&uncompressed_buf[0],
1311 hdr.uncompressed_size, src_buf,
1312 hdr.compressed_length))) {
1313 return FONT_COMPRESSION_FAILURE();
1314 }
1315
1316 for (size_t i = 0; i < metadata.font_infos.size(); i++) {
1317 if (PREDICT_FALSE(!ReconstructFont(&uncompressed_buf[0],
1318 hdr.uncompressed_size,
1319 &metadata, &hdr, i, out))) {
1320 return FONT_COMPRESSION_FAILURE();
1321 }
1322 }
1323
1324 return true;
1325 }
1326
1327 } // namespace woff2
1328