1 // Copyright (c) 2009-2017 The OTS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
17 #include "../RLBoxWOFF2Host.h"
19 // The OpenType Font File
20 // http://www.microsoft.com/typography/otspec/otff.htm
75 for (auto& hunk
: hunks_
) {
80 uint8_t* Allocate(size_t length
) {
81 uint8_t* p
= new uint8_t[length
];
87 std::vector
<uint8_t*> hunks_
;
90 bool CheckTag(uint32_t tag_value
) {
91 for (unsigned i
= 0; i
< 4; ++i
) {
92 const uint32_t check
= tag_value
& 0xff;
93 if (check
< 32 || check
> 126) {
94 return false; // non-ASCII character found.
105 #define OTS_MSG_TAG_(level,otf_,msg_,tag_) \
106 (OTS_MESSAGE_(level,otf_,"%c%c%c%c: %s", OTS_UNTAG(tag_), msg_), false)
108 // Generate a message with or without a table tag, when 'header' is the FontFile pointer
109 #define OTS_FAILURE_MSG_TAG(msg_,tag_) OTS_MSG_TAG_(0, header, msg_, tag_)
110 #define OTS_FAILURE_MSG_HDR(...) OTS_FAILURE_MSG_(header, __VA_ARGS__)
111 #define OTS_WARNING_MSG_HDR(...) OTS_WARNING_MSG_(header, __VA_ARGS__)
117 } supported_tables
[] = {
118 { OTS_TAG_MAXP
, true },
119 { OTS_TAG_HEAD
, true },
120 { OTS_TAG_OS2
, true },
121 { OTS_TAG_CMAP
, true },
122 { OTS_TAG_HHEA
, true },
123 { OTS_TAG_HMTX
, true },
124 { OTS_TAG_NAME
, true },
125 { OTS_TAG_POST
, true },
126 { OTS_TAG_LOCA
, false },
127 { OTS_TAG_GLYF
, false },
128 { OTS_TAG_CFF
, false },
129 { OTS_TAG_VDMX
, false },
130 { OTS_TAG_HDMX
, false },
131 { OTS_TAG_GASP
, false },
132 { OTS_TAG_CVT
, false },
133 { OTS_TAG_FPGM
, false },
134 { OTS_TAG_PREP
, false },
135 { OTS_TAG_LTSH
, false },
136 { OTS_TAG_VORG
, false },
137 { OTS_TAG_KERN
, false },
138 // We need to parse fvar table before other tables that may need to know
139 // the number of variation axes (if any)
140 { OTS_TAG_FVAR
, false },
141 { OTS_TAG_AVAR
, false },
142 { OTS_TAG_CVAR
, false },
143 { OTS_TAG_GVAR
, false },
144 { OTS_TAG_HVAR
, false },
145 { OTS_TAG_MVAR
, false },
146 { OTS_TAG_STAT
, false },
147 { OTS_TAG_VVAR
, false },
148 { OTS_TAG_CFF2
, false },
149 // Color font tables.
150 // We need to parse CPAL before COLR so that the number of palette entries
151 // is known; and these tables follow fvar because COLR may use variations.
152 { OTS_TAG_CPAL
, false },
153 { OTS_TAG_COLR
, false },
154 // We need to parse GDEF table in advance of parsing GSUB/GPOS tables
155 // because they could refer GDEF table.
156 { OTS_TAG_GDEF
, false },
157 { OTS_TAG_GPOS
, false },
158 { OTS_TAG_GSUB
, false },
159 { OTS_TAG_VHEA
, false },
160 { OTS_TAG_VMTX
, false },
161 { OTS_TAG_MATH
, false },
164 { OTS_TAG_GLOC
, false },
165 { OTS_TAG_GLAT
, false },
166 { OTS_TAG_FEAT
, false },
167 { OTS_TAG_SILF
, false },
168 { OTS_TAG_SILE
, false },
169 { OTS_TAG_SILL
, false },
174 bool ValidateVersionTag(ots::Font
*font
) {
175 switch (font
->version
) {
177 case OTS_TAG('O','T','T','O'):
179 case OTS_TAG('t','r','u','e'):
180 font
->version
= 0x000010000;
187 bool ProcessGeneric(ots::FontFile
*header
,
190 ots::OTSStream
*output
,
191 const uint8_t *data
, size_t length
,
192 const std::vector
<ots::TableEntry
>& tables
,
195 bool ProcessTTF(ots::FontFile
*header
,
197 ots::OTSStream
*output
, const uint8_t *data
, size_t length
,
198 uint32_t offset
= 0) {
199 ots::Buffer
file(data
+ offset
, length
- offset
);
201 if (offset
> length
) {
202 return OTS_FAILURE_MSG_HDR("offset beyond end of file");
205 // we disallow all files > 1GB in size for sanity.
206 if (length
> 1024 * 1024 * 1024) {
207 return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
210 if (!file
.ReadU32(&font
->version
)) {
211 return OTS_FAILURE_MSG_HDR("error reading sfntVersion");
213 if (!ValidateVersionTag(font
)) {
214 return OTS_FAILURE_MSG_HDR("invalid sfntVersion: %d", font
->version
);
217 if (!file
.ReadU16(&font
->num_tables
) ||
218 !file
.ReadU16(&font
->search_range
) ||
219 !file
.ReadU16(&font
->entry_selector
) ||
220 !file
.ReadU16(&font
->range_shift
)) {
221 return OTS_FAILURE_MSG_HDR("error reading table directory search header");
224 // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid
225 // overflow num_tables is, at most, 2^16 / 16 = 2^12
226 if (font
->num_tables
>= 4096 || font
->num_tables
< 1) {
227 return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables");
230 unsigned max_pow2
= 0;
231 while (1u << (max_pow2
+ 1) <= font
->num_tables
) {
234 const uint16_t expected_search_range
= (1u << max_pow2
) << 4;
236 // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in
237 // http://www.princexml.com/fonts/ have unexpected search_range value.
238 if (font
->search_range
!= expected_search_range
) {
239 OTS_WARNING_MSG_HDR("bad table directory searchRange");
240 font
->search_range
= expected_search_range
; // Fix the value.
243 // entry_selector is Log2(maximum power of 2 <= numTables)
244 if (font
->entry_selector
!= max_pow2
) {
245 OTS_WARNING_MSG_HDR("bad table directory entrySelector");
246 font
->entry_selector
= max_pow2
; // Fix the value.
249 // range_shift is NumTables x 16-searchRange. We know that 16*num_tables
250 // doesn't over flow because we range checked it above. Also, we know that
251 // it's > font->search_range by construction of search_range.
252 const uint16_t expected_range_shift
=
253 16 * font
->num_tables
- font
->search_range
;
254 if (font
->range_shift
!= expected_range_shift
) {
255 OTS_WARNING_MSG_HDR("bad table directory rangeShift");
256 font
->range_shift
= expected_range_shift
; // the same as above.
259 // Next up is the list of tables.
260 std::vector
<ots::TableEntry
> tables
;
262 for (unsigned i
= 0; i
< font
->num_tables
; ++i
) {
263 ots::TableEntry table
;
264 if (!file
.ReadU32(&table
.tag
) ||
265 !file
.ReadU32(&table
.chksum
) ||
266 !file
.ReadU32(&table
.offset
) ||
267 !file
.ReadU32(&table
.length
)) {
268 return OTS_FAILURE_MSG_HDR("error reading table directory");
271 table
.uncompressed_length
= table
.length
;
272 tables
.push_back(table
);
275 return ProcessGeneric(header
, font
, font
->version
, output
, data
, length
,
279 bool ProcessTTC(ots::FontFile
*header
,
280 ots::OTSStream
*output
,
284 ots::Buffer
file(data
, length
);
286 // we disallow all files > 1GB in size for sanity.
287 if (length
> 1024 * 1024 * 1024) {
288 return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
292 if (!file
.ReadU32(&ttc_tag
)) {
293 return OTS_FAILURE_MSG_HDR("Error reading TTC tag");
295 if (ttc_tag
!= OTS_TAG('t','t','c','f')) {
296 return OTS_FAILURE_MSG_HDR("Invalid TTC tag");
299 uint32_t ttc_version
;
300 if (!file
.ReadU32(&ttc_version
)) {
301 return OTS_FAILURE_MSG_HDR("Error reading TTC version");
303 if (ttc_version
!= 0x00010000 && ttc_version
!= 0x00020000) {
304 return OTS_FAILURE_MSG_HDR("Invalid TTC version");
308 if (!file
.ReadU32(&num_fonts
)) {
309 return OTS_FAILURE_MSG_HDR("Error reading number of TTC fonts");
311 // Limit the allowed number of subfonts to have same memory allocation.
312 if (num_fonts
> 0x10000) {
313 return OTS_FAILURE_MSG_HDR("Too many fonts in TTC");
316 std::vector
<uint32_t> offsets(num_fonts
);
317 for (unsigned i
= 0; i
< num_fonts
; i
++) {
318 if (!file
.ReadU32(&offsets
[i
])) {
319 return OTS_FAILURE_MSG_HDR("Error reading offset to OffsetTable");
323 if (ttc_version
== 0x00020000) {
324 // We don't care about these fields of the header:
325 // uint32_t dsig_tag, dsig_length, dsig_offset
326 if (!file
.Skip(3 * 4)) {
327 return OTS_FAILURE_MSG_HDR("Error reading DSIG offset and length in TTC font");
331 if (index
== static_cast<uint32_t>(-1)) {
332 if (!output
->WriteU32(ttc_tag
) ||
333 !output
->WriteU32(0x00010000) ||
334 !output
->WriteU32(num_fonts
) ||
335 !output
->Seek((3 + num_fonts
) * 4)) {
336 return OTS_FAILURE_MSG_HDR("Error writing output");
339 // Keep references to the fonts processed in the loop below, as we need
340 // them for reused tables.
341 std::vector
<ots::Font
> fonts(num_fonts
, ots::Font(header
));
343 for (unsigned i
= 0; i
< num_fonts
; i
++) {
344 uint32_t out_offset
= output
->Tell();
345 if (!output
->Seek((3 + i
) * 4) ||
346 !output
->WriteU32(out_offset
) ||
347 !output
->Seek(out_offset
)) {
348 return OTS_FAILURE_MSG_HDR("Error writing output");
350 if (!ProcessTTF(header
, &fonts
[i
], output
, data
, length
, offsets
[i
])) {
357 if (index
>= num_fonts
) {
358 return OTS_FAILURE_MSG_HDR("Requested font index is bigger than the number of fonts in the TTC file");
361 ots::Font
font(header
);
362 return ProcessTTF(header
, &font
, output
, data
, length
, offsets
[index
]);
366 bool ProcessWOFF(ots::FontFile
*header
,
368 ots::OTSStream
*output
, const uint8_t *data
, size_t length
) {
369 ots::Buffer
file(data
, length
);
371 // we disallow all files > 1GB in size for sanity.
372 if (length
> 1024 * 1024 * 1024) {
373 return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
377 if (!file
.ReadU32(&woff_tag
)) {
378 return OTS_FAILURE_MSG_HDR("error reading WOFF marker");
381 if (woff_tag
!= OTS_TAG('w','O','F','F')) {
382 return OTS_FAILURE_MSG_HDR("invalid WOFF marker");
385 if (!file
.ReadU32(&font
->version
)) {
386 return OTS_FAILURE_MSG_HDR("error reading sfntVersion");
388 if (!ValidateVersionTag(font
)) {
389 return OTS_FAILURE_MSG_HDR("invalid sfntVersion: %d", font
->version
);
392 uint32_t reported_length
;
393 if (!file
.ReadU32(&reported_length
) || length
!= reported_length
) {
394 return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header");
397 if (!file
.ReadU16(&font
->num_tables
) || !font
->num_tables
) {
398 return OTS_FAILURE_MSG_HDR("error reading number of tables");
401 uint16_t reserved_value
;
402 if (!file
.ReadU16(&reserved_value
) || reserved_value
) {
403 return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header");
406 uint32_t reported_total_sfnt_size
;
407 if (!file
.ReadU32(&reported_total_sfnt_size
)) {
408 return OTS_FAILURE_MSG_HDR("error reading total sfnt size");
411 // We don't care about these fields of the header:
412 // uint16_t major_version, minor_version
413 if (!file
.Skip(2 * 2)) {
414 return OTS_FAILURE_MSG_HDR("Failed to read 'majorVersion' or 'minorVersion'");
417 // Checks metadata block size.
418 uint32_t meta_offset
;
419 uint32_t meta_length
;
420 uint32_t meta_length_orig
;
421 if (!file
.ReadU32(&meta_offset
) ||
422 !file
.ReadU32(&meta_length
) ||
423 !file
.ReadU32(&meta_length_orig
)) {
424 return OTS_FAILURE_MSG_HDR("Failed to read header metadata block fields");
427 if (meta_offset
>= length
|| length
- meta_offset
< meta_length
) {
428 return OTS_FAILURE_MSG_HDR("Invalid metadata block offset or length");
432 // Checks private data block size.
433 uint32_t priv_offset
;
434 uint32_t priv_length
;
435 if (!file
.ReadU32(&priv_offset
) ||
436 !file
.ReadU32(&priv_length
)) {
437 return OTS_FAILURE_MSG_HDR("Failed to read header private block fields");
440 if (priv_offset
>= length
|| length
- priv_offset
< priv_length
) {
441 return OTS_FAILURE_MSG_HDR("Invalid private block offset or length");
445 // Next up is the list of tables.
446 std::vector
<ots::TableEntry
> tables
;
448 uint32_t first_index
= 0;
449 uint32_t last_index
= 0;
450 // Size of sfnt header plus size of table records.
451 uint64_t total_sfnt_size
= 12 + 16 * font
->num_tables
;
452 for (unsigned i
= 0; i
< font
->num_tables
; ++i
) {
453 ots::TableEntry table
;
454 if (!file
.ReadU32(&table
.tag
) ||
455 !file
.ReadU32(&table
.offset
) ||
456 !file
.ReadU32(&table
.length
) ||
457 !file
.ReadU32(&table
.uncompressed_length
) ||
458 !file
.ReadU32(&table
.chksum
)) {
459 return OTS_FAILURE_MSG_HDR("error reading table directory");
462 total_sfnt_size
+= ots::Round4(table
.uncompressed_length
);
463 if (total_sfnt_size
> std::numeric_limits
<uint32_t>::max()) {
464 return OTS_FAILURE_MSG_HDR("sfnt size overflow");
466 tables
.push_back(table
);
467 if (i
== 0 || tables
[first_index
].offset
> table
.offset
)
469 if (i
== 0 || tables
[last_index
].offset
< table
.offset
)
473 if (reported_total_sfnt_size
!= total_sfnt_size
) {
474 return OTS_FAILURE_MSG_HDR("uncompressed sfnt size mismatch");
477 // Table data must follow immediately after the header.
478 if (tables
[first_index
].offset
!= ots::Round4(file
.offset())) {
479 return OTS_FAILURE_MSG_HDR("junk before tables in WOFF file");
482 if (tables
[last_index
].offset
>= length
||
483 length
- tables
[last_index
].offset
< tables
[last_index
].length
) {
484 return OTS_FAILURE_MSG_HDR("invalid table location/size");
486 // Blocks must follow immediately after the previous block.
487 // (Except for padding with a maximum of three null bytes)
488 uint64_t block_end
= ots::Round4(
489 static_cast<uint64_t>(tables
[last_index
].offset
) +
490 static_cast<uint64_t>(tables
[last_index
].length
));
491 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
492 return OTS_FAILURE_MSG_HDR("invalid table location/size");
495 if (block_end
!= meta_offset
) {
496 return OTS_FAILURE_MSG_HDR("Invalid metadata block offset");
498 block_end
= ots::Round4(static_cast<uint64_t>(meta_offset
) +
499 static_cast<uint64_t>(meta_length
));
500 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
501 return OTS_FAILURE_MSG_HDR("Invalid metadata block length");
505 if (block_end
!= priv_offset
) {
506 return OTS_FAILURE_MSG_HDR("Invalid private block offset");
508 block_end
= ots::Round4(static_cast<uint64_t>(priv_offset
) +
509 static_cast<uint64_t>(priv_length
));
510 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
511 return OTS_FAILURE_MSG_HDR("Invalid private block length");
514 if (block_end
!= ots::Round4(length
)) {
515 return OTS_FAILURE_MSG_HDR("File length mismatch (trailing junk?)");
518 return ProcessGeneric(header
, font
, woff_tag
, output
, data
, length
, tables
, file
);
521 bool ProcessWOFF2(ots::FontFile
* header
, ots::OTSStream
* output
,
522 const uint8_t* data
, size_t length
, uint32_t index
) {
523 return RLBoxProcessWOFF2(header
, output
, data
, length
, index
, ProcessTTC
, ProcessTTF
);
526 ots::TableAction
GetTableAction(const ots::FontFile
*header
, uint32_t tag
) {
527 ots::TableAction action
= header
->context
->GetTableAction(tag
);
529 if (action
== ots::TABLE_ACTION_DEFAULT
) {
530 action
= ots::TABLE_ACTION_DROP
;
532 for (unsigned i
= 0; ; ++i
) {
533 if (supported_tables
[i
].tag
== 0) break;
535 if (supported_tables
[i
].tag
== tag
) {
536 action
= ots::TABLE_ACTION_SANITIZE
;
542 assert(action
!= ots::TABLE_ACTION_DEFAULT
); // Should never return this.
546 bool GetTableData(const uint8_t *data
,
547 const ots::TableEntry
& table
,
549 size_t *table_length
,
550 const uint8_t **table_data
) {
551 if (table
.uncompressed_length
!= table
.length
) {
552 // Compressed table. Need to uncompress into memory first.
553 *table_length
= table
.uncompressed_length
;
554 *table_data
= arena
.Allocate(*table_length
);
555 uLongf dest_len
= *table_length
;
556 int r
= uncompress((Bytef
*) *table_data
, &dest_len
,
557 data
+ table
.offset
, table
.length
);
558 if (r
!= Z_OK
|| dest_len
!= *table_length
) {
562 // Uncompressed table. We can process directly from memory.
563 *table_data
= data
+ table
.offset
;
564 *table_length
= table
.length
;
570 bool ProcessGeneric(ots::FontFile
*header
,
573 ots::OTSStream
*output
,
574 const uint8_t *data
, size_t length
,
575 const std::vector
<ots::TableEntry
>& tables
,
577 const size_t data_offset
= file
.offset();
579 uint32_t uncompressed_sum
= 0;
581 for (unsigned i
= 0; i
< font
->num_tables
; ++i
) {
582 // the tables must be sorted by tag (when taken as big-endian numbers).
583 // This also remove the possibility of duplicate tables.
585 const uint32_t this_tag
= tables
[i
].tag
;
586 const uint32_t prev_tag
= tables
[i
- 1].tag
;
587 if (this_tag
<= prev_tag
) {
588 OTS_WARNING_MSG_HDR("Table directory is not correctly ordered");
592 // all tag names must be built from printable ASCII characters
593 if (!ots::CheckTag(tables
[i
].tag
)) {
594 OTS_WARNING_MSG_HDR("Invalid table tag: 0x%X", tables
[i
].tag
);
597 // tables must be 4-byte aligned
598 if (tables
[i
].offset
& 3) {
599 return OTS_FAILURE_MSG_TAG("misaligned table", tables
[i
].tag
);
602 // and must be within the file
603 if (tables
[i
].offset
< data_offset
|| tables
[i
].offset
>= length
) {
604 return OTS_FAILURE_MSG_TAG("invalid table offset", tables
[i
].tag
);
606 // disallow all tables with a zero length
607 if (tables
[i
].length
< 1) {
608 // Note: malayalam.ttf has zero length CVT table...
609 return OTS_FAILURE_MSG_TAG("zero-length table", tables
[i
].tag
);
611 // disallow all tables with a length > 1GB
612 if (tables
[i
].length
> 1024 * 1024 * 1024) {
613 return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", tables
[i
].tag
);
615 // disallow tables where the uncompressed size is < the compressed size.
616 if (tables
[i
].uncompressed_length
< tables
[i
].length
) {
617 return OTS_FAILURE_MSG_TAG("invalid compressed table", tables
[i
].tag
);
619 if (tables
[i
].uncompressed_length
> tables
[i
].length
) {
620 // We'll probably be decompressing this table.
622 // disallow all tables which decompress to > OTS_MAX_DECOMPRESSED_TABLE_SIZE
623 if (tables
[i
].uncompressed_length
> OTS_MAX_DECOMPRESSED_TABLE_SIZE
) {
624 return OTS_FAILURE_MSG_HDR("%c%c%c%c: decompressed table length exceeds %gMB",
625 OTS_UNTAG(tables
[i
].tag
),
626 OTS_MAX_DECOMPRESSED_TABLE_SIZE
/ (1024.0 * 1024.0));
628 if (uncompressed_sum
+ tables
[i
].uncompressed_length
< uncompressed_sum
) {
629 return OTS_FAILURE_MSG_TAG("overflow of decompressed sum", tables
[i
].tag
);
632 uncompressed_sum
+= tables
[i
].uncompressed_length
;
634 // since we required that the file be < 1GB in length, and that the table
635 // length is < 1GB, the following addtion doesn't overflow
636 uint32_t end_byte
= tables
[i
].offset
+ tables
[i
].length
;
637 // Tables in the WOFF file must be aligned 4-byte boundary.
638 if (signature
== OTS_TAG('w','O','F','F')) {
639 end_byte
= ots::Round4(end_byte
);
641 if (!end_byte
|| end_byte
> length
) {
642 return OTS_FAILURE_MSG_TAG("table overruns end of file", tables
[i
].tag
);
646 // All decompressed tables decompressed must be <= OTS_MAX_DECOMPRESSED_FILE_SIZE.
647 if (uncompressed_sum
> OTS_MAX_DECOMPRESSED_FILE_SIZE
) {
648 return OTS_FAILURE_MSG_HDR("decompressed sum exceeds %gMB",
649 OTS_MAX_DECOMPRESSED_FILE_SIZE
/ (1024.0 * 1024.0));
652 if (uncompressed_sum
> output
->size()) {
653 return OTS_FAILURE_MSG_HDR("decompressed sum exceeds output size (%gMB)", output
->size() / (1024.0 * 1024.0));
656 // check that the tables are not overlapping.
657 std::vector
<std::pair
<uint32_t, uint8_t> > overlap_checker
;
658 for (unsigned i
= 0; i
< font
->num_tables
; ++i
) {
659 overlap_checker
.push_back(
660 std::make_pair(tables
[i
].offset
, static_cast<uint8_t>(1) /* start */));
661 overlap_checker
.push_back(
662 std::make_pair(tables
[i
].offset
+ tables
[i
].length
,
663 static_cast<uint8_t>(0) /* end */));
665 std::sort(overlap_checker
.begin(), overlap_checker
.end());
666 int overlap_count
= 0;
667 for (unsigned i
= 0; i
< overlap_checker
.size(); ++i
) {
668 overlap_count
+= (overlap_checker
[i
].second
? 1 : -1);
669 if (overlap_count
> 1) {
670 return OTS_FAILURE_MSG_HDR("overlapping tables");
674 std::map
<uint32_t, ots::TableEntry
> table_map
;
675 for (unsigned i
= 0; i
< font
->num_tables
; ++i
) {
676 table_map
[tables
[i
].tag
] = tables
[i
];
680 // Parse known tables first as we need to parse them in specific order.
681 for (unsigned i
= 0; ; ++i
) {
682 if (supported_tables
[i
].tag
== 0) break;
684 uint32_t tag
= supported_tables
[i
].tag
;
685 const auto &it
= table_map
.find(tag
);
686 if (it
== table_map
.cend()) {
687 if (supported_tables
[i
].required
) {
688 return OTS_FAILURE_MSG_TAG("missing required table", tag
);
691 if (!font
->ParseTable(it
->second
, data
, arena
)) {
692 return OTS_FAILURE_MSG_TAG("Failed to parse table", tag
);
697 // Then parse any tables left.
698 for (const auto &table_entry
: tables
) {
699 if (!font
->GetTable(table_entry
.tag
)) {
700 if (!font
->ParseTable(table_entry
, data
, arena
)) {
701 return OTS_FAILURE_MSG_TAG("Failed to parse table", table_entry
.tag
);
706 #ifdef OTS_SYNTHESIZE_MISSING_GVAR
707 // If there was an fvar table but no gvar, synthesize an empty gvar to avoid
708 // issues with rasterizers (e.g. Core Text) that assume it must be present.
709 if (font
->GetTable(OTS_TAG_FVAR
) && !font
->GetTable(OTS_TAG_GVAR
)) {
710 ots::TableEntry table_entry
{ OTS_TAG_GVAR
, 0, 0, 0, 0 };
711 const auto &it
= font
->file
->tables
.find(table_entry
);
712 if (it
!= font
->file
->tables
.end()) {
713 table_map
[OTS_TAG_GVAR
] = table_entry
;
714 font
->AddTable(table_entry
, it
->second
);
716 ots::OpenTypeGVAR
*gvar
= new ots::OpenTypeGVAR(font
, OTS_TAG_GVAR
);
717 if (gvar
->InitEmpty()) {
718 table_map
[OTS_TAG_GVAR
] = table_entry
;
719 font
->AddTable(table_entry
, gvar
);
727 ots::Table
*glyf
= font
->GetTable(OTS_TAG_GLYF
);
728 ots::Table
*loca
= font
->GetTable(OTS_TAG_LOCA
);
729 ots::Table
*cff
= font
->GetTable(OTS_TAG_CFF
);
730 ots::Table
*cff2
= font
->GetTable(OTS_TAG_CFF2
);
731 ots::OpenTypeMAXP
*maxp
= static_cast<ots::OpenTypeMAXP
*>(
732 font
->GetTypedTable(OTS_TAG_MAXP
));
735 if (font
->version
!= 0x000010000) {
736 OTS_WARNING_MSG_HDR("wrong sfntVersion for glyph data");
737 font
->version
= 0x000010000;
739 if (!maxp
->version_1
) {
740 return OTS_FAILURE_MSG_TAG("wrong maxp version for glyph data",
744 cff
->Drop("font contains both CFF and glyf/loca tables");
746 cff2
->Drop("font contains both CFF and glyf/loca tables");
747 } else if (cff
|| cff2
) {
748 if (font
->version
!= OTS_TAG('O','T','T','O')) {
749 OTS_WARNING_MSG_HDR("wrong sfntVersion for glyph data");
750 font
->version
= OTS_TAG('O','T','T','O');
753 glyf
->Drop("font contains both CFF and glyf tables");
755 loca
->Drop("font contains both CFF and loca tables");
756 if (maxp
->version_1
) {
757 OTS_WARNING_MSG_HDR("fixing incorrect maxp version for CFF font");
758 maxp
->version_1
= false;
760 } else if (font
->GetTable(OTS_TAG('C','B','D','T')) &&
761 font
->GetTable(OTS_TAG('C','B','L','C'))) {
762 // We don't sanitize bitmap tables, but don’t reject bitmap-only fonts if
763 // we are asked to pass them thru.
765 return OTS_FAILURE_MSG_HDR("no supported glyph data table(s) present");
768 uint16_t num_output_tables
= 0;
769 for (const auto &it
: table_map
) {
770 ots::Table
*table
= font
->GetTable(it
.first
);
775 uint16_t max_pow2
= 0;
776 while (1u << (max_pow2
+ 1) <= num_output_tables
) {
779 const uint16_t output_search_range
= (1u << max_pow2
) << 4;
781 // most of the errors here are highly unlikely - they'd only occur if the
782 // output stream returns a failure, e.g. lack of space to write
783 output
->ResetChecksum();
784 if (!output
->WriteU32(font
->version
) ||
785 !output
->WriteU16(num_output_tables
) ||
786 !output
->WriteU16(output_search_range
) ||
787 !output
->WriteU16(max_pow2
) ||
788 !output
->WriteU16((num_output_tables
<< 4) - output_search_range
)) {
789 return OTS_FAILURE_MSG_HDR("error writing output");
791 const uint32_t offset_table_chksum
= output
->chksum();
793 const size_t table_record_offset
= output
->Tell();
794 if (!output
->Pad(16 * num_output_tables
)) {
795 return OTS_FAILURE_MSG_HDR("error writing output");
798 std::vector
<ots::TableEntry
> out_tables
;
800 size_t head_table_offset
= 0;
801 for (const auto &it
: table_map
) {
802 uint32_t input_offset
= it
.second
.offset
;
803 const auto &ot
= header
->table_entries
.find(input_offset
);
804 if (ot
!= header
->table_entries
.end()) {
805 ots::TableEntry out
= ot
->second
;
806 if (out
.tag
== OTS_TAG('h','e','a','d')) {
807 head_table_offset
= out
.offset
;
809 out_tables
.push_back(out
);
813 out
.offset
= output
->Tell();
815 if (out
.tag
== OTS_TAG('h','e','a','d')) {
816 head_table_offset
= out
.offset
;
819 ots::Table
*table
= font
->GetTable(out
.tag
);
821 output
->ResetChecksum();
822 if (!table
->Serialize(output
)) {
823 return OTS_FAILURE_MSG_TAG("Failed to serialize table", out
.tag
);
826 const size_t end_offset
= output
->Tell();
827 if (end_offset
<= out
.offset
) {
828 // paranoid check. |end_offset| is supposed to be greater than the offset,
829 // as long as the Tell() interface is implemented correctly.
830 return OTS_FAILURE_MSG_TAG("Table is empty or have -ve size", out
.tag
);
832 out
.length
= end_offset
- out
.offset
;
834 // align tables to four bytes
835 if (!output
->Pad((4 - (end_offset
& 3)) % 4)) {
836 return OTS_FAILURE_MSG_TAG("Failed to pad table to 4 bytes", out
.tag
);
838 out
.chksum
= output
->chksum();
839 out_tables
.push_back(out
);
840 header
->table_entries
[input_offset
] = out
;
845 const size_t end_of_file
= output
->Tell();
847 // Need to sort the output tables for inclusion in the file
848 std::sort(out_tables
.begin(), out_tables
.end());
849 if (!output
->Seek(table_record_offset
)) {
850 return OTS_FAILURE_MSG_HDR("error writing output");
853 output
->ResetChecksum();
854 uint32_t tables_chksum
= 0;
855 for (unsigned i
= 0; i
< out_tables
.size(); ++i
) {
856 if (!output
->WriteU32(out_tables
[i
].tag
) ||
857 !output
->WriteU32(out_tables
[i
].chksum
) ||
858 !output
->WriteU32(out_tables
[i
].offset
) ||
859 !output
->WriteU32(out_tables
[i
].length
)) {
860 return OTS_FAILURE_MSG_HDR("error writing output");
862 tables_chksum
+= out_tables
[i
].chksum
;
864 const uint32_t table_record_chksum
= output
->chksum();
866 // http://www.microsoft.com/typography/otspec/otff.htm
867 const uint32_t file_chksum
868 = offset_table_chksum
+ tables_chksum
+ table_record_chksum
;
869 const uint32_t chksum_magic
= static_cast<uint32_t>(0xb1b0afba) - file_chksum
;
871 // seek into the 'head' table and write in the checksum magic value
872 if (!head_table_offset
) {
873 return OTS_FAILURE_MSG_HDR("internal error!");
875 if (!output
->Seek(head_table_offset
+ 8)) {
876 return OTS_FAILURE_MSG_HDR("error writing output");
878 if (!output
->WriteU32(chksum_magic
)) {
879 return OTS_FAILURE_MSG_HDR("error writing output");
882 if (!output
->Seek(end_of_file
)) {
883 return OTS_FAILURE_MSG_HDR("error writing output");
889 bool IsGraphiteTag(uint32_t tag
) {
890 if (tag
== OTS_TAG_FEAT
||
891 tag
== OTS_TAG_GLAT
||
892 tag
== OTS_TAG_GLOC
||
893 tag
== OTS_TAG_SILE
||
894 tag
== OTS_TAG_SILF
||
895 tag
== OTS_TAG_SILL
) {
901 bool IsVariationsTag(uint32_t tag
) {
902 if (tag
== OTS_TAG_AVAR
||
903 tag
== OTS_TAG_CVAR
||
904 tag
== OTS_TAG_FVAR
||
905 tag
== OTS_TAG_GVAR
||
906 tag
== OTS_TAG_HVAR
||
907 tag
== OTS_TAG_MVAR
||
908 tag
== OTS_TAG_STAT
||
909 tag
== OTS_TAG_VVAR
) {
919 FontFile::~FontFile() {
920 for (const auto& it
: tables
) {
926 bool Font::ParseTable(const TableEntry
& table_entry
, const uint8_t* data
,
928 uint32_t tag
= table_entry
.tag
;
929 TableAction action
= GetTableAction(file
, tag
);
930 if (action
== TABLE_ACTION_DROP
) {
934 const auto &it
= file
->tables
.find(table_entry
);
935 if (it
!= file
->tables
.end()) {
936 m_tables
[tag
] = it
->second
;
943 if (action
== TABLE_ACTION_PASSTHRU
) {
944 table
= new TablePassthru(this, tag
);
947 case OTS_TAG_AVAR
: table
= new OpenTypeAVAR(this, tag
); break;
948 case OTS_TAG_CFF
: table
= new OpenTypeCFF(this, tag
); break;
949 case OTS_TAG_CFF2
: table
= new OpenTypeCFF2(this, tag
); break;
950 case OTS_TAG_CMAP
: table
= new OpenTypeCMAP(this, tag
); break;
951 case OTS_TAG_COLR
: table
= new OpenTypeCOLR(this, tag
); break;
952 case OTS_TAG_CPAL
: table
= new OpenTypeCPAL(this, tag
); break;
953 case OTS_TAG_CVAR
: table
= new OpenTypeCVAR(this, tag
); break;
954 case OTS_TAG_CVT
: table
= new OpenTypeCVT(this, tag
); break;
955 case OTS_TAG_FPGM
: table
= new OpenTypeFPGM(this, tag
); break;
956 case OTS_TAG_FVAR
: table
= new OpenTypeFVAR(this, tag
); break;
957 case OTS_TAG_GASP
: table
= new OpenTypeGASP(this, tag
); break;
958 case OTS_TAG_GDEF
: table
= new OpenTypeGDEF(this, tag
); break;
959 case OTS_TAG_GLYF
: table
= new OpenTypeGLYF(this, tag
); break;
960 case OTS_TAG_GPOS
: table
= new OpenTypeGPOS(this, tag
); break;
961 case OTS_TAG_GSUB
: table
= new OpenTypeGSUB(this, tag
); break;
962 case OTS_TAG_GVAR
: table
= new OpenTypeGVAR(this, tag
); break;
963 case OTS_TAG_HDMX
: table
= new OpenTypeHDMX(this, tag
); break;
964 case OTS_TAG_HEAD
: table
= new OpenTypeHEAD(this, tag
); break;
965 case OTS_TAG_HHEA
: table
= new OpenTypeHHEA(this, tag
); break;
966 case OTS_TAG_HMTX
: table
= new OpenTypeHMTX(this, tag
); break;
967 case OTS_TAG_HVAR
: table
= new OpenTypeHVAR(this, tag
); break;
968 case OTS_TAG_KERN
: table
= new OpenTypeKERN(this, tag
); break;
969 case OTS_TAG_LOCA
: table
= new OpenTypeLOCA(this, tag
); break;
970 case OTS_TAG_LTSH
: table
= new OpenTypeLTSH(this, tag
); break;
971 case OTS_TAG_MATH
: table
= new OpenTypeMATH(this, tag
); break;
972 case OTS_TAG_MAXP
: table
= new OpenTypeMAXP(this, tag
); break;
973 case OTS_TAG_MVAR
: table
= new OpenTypeMVAR(this, tag
); break;
974 case OTS_TAG_NAME
: table
= new OpenTypeNAME(this, tag
); break;
975 case OTS_TAG_OS2
: table
= new OpenTypeOS2(this, tag
); break;
976 case OTS_TAG_POST
: table
= new OpenTypePOST(this, tag
); break;
977 case OTS_TAG_PREP
: table
= new OpenTypePREP(this, tag
); break;
978 case OTS_TAG_STAT
: table
= new OpenTypeSTAT(this, tag
); break;
979 case OTS_TAG_VDMX
: table
= new OpenTypeVDMX(this, tag
); break;
980 case OTS_TAG_VHEA
: table
= new OpenTypeVHEA(this, tag
); break;
981 case OTS_TAG_VMTX
: table
= new OpenTypeVMTX(this, tag
); break;
982 case OTS_TAG_VORG
: table
= new OpenTypeVORG(this, tag
); break;
983 case OTS_TAG_VVAR
: table
= new OpenTypeVVAR(this, tag
); break;
986 case OTS_TAG_FEAT
: table
= new OpenTypeFEAT(this, tag
); break;
987 case OTS_TAG_GLAT
: table
= new OpenTypeGLAT(this, tag
); break;
988 case OTS_TAG_GLOC
: table
= new OpenTypeGLOC(this, tag
); break;
989 case OTS_TAG_SILE
: table
= new OpenTypeSILE(this, tag
); break;
990 case OTS_TAG_SILF
: table
= new OpenTypeSILF(this, tag
); break;
991 case OTS_TAG_SILL
: table
= new OpenTypeSILL(this, tag
); break;
998 const uint8_t* table_data
;
1001 ret
= GetTableData(data
, table_entry
, arena
, &table_length
, &table_data
);
1003 ret
= table
->Parse(table_data
, table_length
);
1005 AddTable(table_entry
, table
);
1015 Table
* Font::GetTable(uint32_t tag
) const {
1016 const auto &it
= m_tables
.find(tag
);
1017 if (it
!= m_tables
.end() && it
->second
&& it
->second
->ShouldSerialize())
1022 Table
* Font::GetTypedTable(uint32_t tag
) const {
1023 Table
* t
= GetTable(tag
);
1024 if (t
&& t
->Type() == tag
)
1029 void Font::AddTable(TableEntry entry
, Table
* table
) {
1030 // Attempting to add a duplicate table would be an error; this should only
1031 // be used to add a table that does not already exist.
1032 assert(m_tables
.find(table
->Tag()) == m_tables
.end());
1033 m_tables
[table
->Tag()] = table
;
1034 file
->tables
[entry
] = table
;
1037 void Font::DropGraphite() {
1038 file
->context
->Message(0, "Dropping all Graphite tables");
1039 for (const std::pair
<uint32_t, Table
*> entry
: m_tables
) {
1040 if (IsGraphiteTag(entry
.first
)) {
1041 entry
.second
->Drop("Discarding Graphite table");
1046 void Font::DropVariations() {
1047 file
->context
->Message(0, "Dropping all Variation tables");
1048 for (const std::pair
<uint32_t, Table
*> entry
: m_tables
) {
1049 if (IsVariationsTag(entry
.first
)) {
1050 entry
.second
->Drop("Discarding Variations table");
1055 bool Table::ShouldSerialize() {
1056 return m_shouldSerialize
;
1059 void Table::Message(int level
, const char *format
, va_list va
) {
1060 char msg
[206] = { OTS_UNTAG(m_tag
), ':', ' ' };
1061 std::vsnprintf(msg
+ 6, 200, format
, va
);
1062 m_font
->file
->context
->Message(level
, msg
);
1065 bool Table::Error(const char *format
, ...) {
1067 va_start(va
, format
);
1068 Message(0, format
, va
);
1074 bool Table::Warning(const char *format
, ...) {
1076 va_start(va
, format
);
1077 Message(1, format
, va
);
1083 bool Table::Drop(const char *format
, ...) {
1084 m_shouldSerialize
= false;
1087 va_start(va
, format
);
1088 Message(0, format
, va
);
1089 m_font
->file
->context
->Message(0, "Table discarded");
1095 bool Table::DropGraphite(const char *format
, ...) {
1097 va_start(va
, format
);
1098 Message(0, format
, va
);
1101 m_font
->DropGraphite();
1102 if (IsGraphiteTag(m_tag
))
1103 Drop("Discarding Graphite table");
1108 bool Table::DropVariations(const char *format
, ...) {
1110 va_start(va
, format
);
1111 Message(0, format
, va
);
1114 m_font
->DropVariations();
1115 if (IsVariationsTag(m_tag
))
1116 Drop("Discarding Variations table");
1121 bool TablePassthru::Parse(const uint8_t *data
, size_t length
) {
1127 bool TablePassthru::Serialize(OTSStream
*out
) {
1128 if (!out
->Write(m_data
, m_length
)) {
1129 return Error("Failed to write table");
1135 bool OTSContext::Process(OTSStream
*output
,
1136 const uint8_t *data
,
1141 header
.context
= this;
1144 return OTS_FAILURE_MSG_(&header
, "file less than 4 bytes");
1148 if (data
[0] == 'w' && data
[1] == 'O' && data
[2] == 'F' && data
[3] == 'F') {
1149 result
= ProcessWOFF(&header
, &font
, output
, data
, length
);
1150 } else if (data
[0] == 'w' && data
[1] == 'O' && data
[2] == 'F' && data
[3] == '2') {
1151 result
= ProcessWOFF2(&header
, output
, data
, length
, index
);
1152 } else if (data
[0] == 't' && data
[1] == 't' && data
[2] == 'c' && data
[3] == 'f') {
1153 result
= ProcessTTC(&header
, output
, data
, length
, index
);
1155 result
= ProcessTTF(&header
, &font
, output
, data
, length
);