1 // Copyright (c) 2009 The Chromium 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.
19 // The OpenType Font File
20 // http://www.microsoft.com/typography/otspec/cmap.htm
24 // Generate a message with or without a table tag, when 'header' is the OpenTypeFile pointer
25 #define OTS_FAILURE_MSG_TAG(msg_,tag_) OTS_FAILURE_MSG_TAG_(header, msg_, tag_)
26 #define OTS_FAILURE_MSG_HDR(msg_) OTS_FAILURE_MSG_(header, msg_)
29 struct OpenTypeTable
{
34 uint32_t uncompressed_length
;
37 bool CheckTag(uint32_t tag_value
) {
38 for (unsigned i
= 0; i
< 4; ++i
) {
39 const uint32_t check
= tag_value
& 0xff;
40 if (check
< 32 || check
> 126) {
41 return false; // non-ASCII character found.
48 uint32_t Tag(const char *tag_str
) {
50 std::memcpy(&ret
, tag_str
, 4);
60 static bool SortByTag(const OutputTable
& a
, const OutputTable
& b
) {
61 const uint32_t atag
= ntohl(a
.tag
);
62 const uint32_t btag
= ntohl(b
.tag
);
70 for (std::vector
<uint8_t*>::iterator
71 i
= hunks_
.begin(); i
!= hunks_
.end(); ++i
) {
76 uint8_t* Allocate(size_t length
) {
77 uint8_t* p
= new uint8_t[length
];
83 std::vector
<uint8_t*> hunks_
;
88 bool (*parse
)(ots::OpenTypeFile
*otf
, const uint8_t *data
, size_t length
);
89 bool (*serialise
)(ots::OTSStream
*out
, ots::OpenTypeFile
*file
);
90 bool (*should_serialise
)(ots::OpenTypeFile
*file
);
91 void (*free
)(ots::OpenTypeFile
*file
);
94 { "maxp", ots::ots_maxp_parse
, ots::ots_maxp_serialise
,
95 ots::ots_maxp_should_serialise
, ots::ots_maxp_free
, true },
96 { "head", ots::ots_head_parse
, ots::ots_head_serialise
,
97 ots::ots_head_should_serialise
, ots::ots_head_free
, true },
98 { "OS/2", ots::ots_os2_parse
, ots::ots_os2_serialise
,
99 ots::ots_os2_should_serialise
, ots::ots_os2_free
, true },
100 { "cmap", ots::ots_cmap_parse
, ots::ots_cmap_serialise
,
101 ots::ots_cmap_should_serialise
, ots::ots_cmap_free
, true },
102 { "hhea", ots::ots_hhea_parse
, ots::ots_hhea_serialise
,
103 ots::ots_hhea_should_serialise
, ots::ots_hhea_free
, true },
104 { "hmtx", ots::ots_hmtx_parse
, ots::ots_hmtx_serialise
,
105 ots::ots_hmtx_should_serialise
, ots::ots_hmtx_free
, true },
106 { "name", ots::ots_name_parse
, ots::ots_name_serialise
,
107 ots::ots_name_should_serialise
, ots::ots_name_free
, true },
108 { "post", ots::ots_post_parse
, ots::ots_post_serialise
,
109 ots::ots_post_should_serialise
, ots::ots_post_free
, true },
110 { "loca", ots::ots_loca_parse
, ots::ots_loca_serialise
,
111 ots::ots_loca_should_serialise
, ots::ots_loca_free
, false },
112 { "glyf", ots::ots_glyf_parse
, ots::ots_glyf_serialise
,
113 ots::ots_glyf_should_serialise
, ots::ots_glyf_free
, false },
114 { "CFF ", ots::ots_cff_parse
, ots::ots_cff_serialise
,
115 ots::ots_cff_should_serialise
, ots::ots_cff_free
, false },
116 { "VDMX", ots::ots_vdmx_parse
, ots::ots_vdmx_serialise
,
117 ots::ots_vdmx_should_serialise
, ots::ots_vdmx_free
, false },
118 { "hdmx", ots::ots_hdmx_parse
, ots::ots_hdmx_serialise
,
119 ots::ots_hdmx_should_serialise
, ots::ots_hdmx_free
, false },
120 { "gasp", ots::ots_gasp_parse
, ots::ots_gasp_serialise
,
121 ots::ots_gasp_should_serialise
, ots::ots_gasp_free
, false },
122 { "cvt ", ots::ots_cvt_parse
, ots::ots_cvt_serialise
,
123 ots::ots_cvt_should_serialise
, ots::ots_cvt_free
, false },
124 { "fpgm", ots::ots_fpgm_parse
, ots::ots_fpgm_serialise
,
125 ots::ots_fpgm_should_serialise
, ots::ots_fpgm_free
, false },
126 { "prep", ots::ots_prep_parse
, ots::ots_prep_serialise
,
127 ots::ots_prep_should_serialise
, ots::ots_prep_free
, false },
128 { "LTSH", ots::ots_ltsh_parse
, ots::ots_ltsh_serialise
,
129 ots::ots_ltsh_should_serialise
, ots::ots_ltsh_free
, false },
130 { "VORG", ots::ots_vorg_parse
, ots::ots_vorg_serialise
,
131 ots::ots_vorg_should_serialise
, ots::ots_vorg_free
, false },
132 { "kern", ots::ots_kern_parse
, ots::ots_kern_serialise
,
133 ots::ots_kern_should_serialise
, ots::ots_kern_free
, false },
134 // We need to parse GDEF table in advance of parsing GSUB/GPOS tables
135 // because they could refer GDEF table.
136 { "GDEF", ots::ots_gdef_parse
, ots::ots_gdef_serialise
,
137 ots::ots_gdef_should_serialise
, ots::ots_gdef_free
, false },
138 { "GPOS", ots::ots_gpos_parse
, ots::ots_gpos_serialise
,
139 ots::ots_gpos_should_serialise
, ots::ots_gpos_free
, false },
140 { "GSUB", ots::ots_gsub_parse
, ots::ots_gsub_serialise
,
141 ots::ots_gsub_should_serialise
, ots::ots_gsub_free
, false },
142 { "vhea", ots::ots_vhea_parse
, ots::ots_vhea_serialise
,
143 ots::ots_vhea_should_serialise
, ots::ots_vhea_free
, false },
144 { "vmtx", ots::ots_vmtx_parse
, ots::ots_vmtx_serialise
,
145 ots::ots_vmtx_should_serialise
, ots::ots_vmtx_free
, false },
146 { "MATH", ots::ots_math_parse
, ots::ots_math_serialise
,
147 ots::ots_math_should_serialise
, ots::ots_math_free
, false },
148 // TODO(bashi): Support mort, base, and jstf tables.
149 { 0, NULL
, NULL
, NULL
, NULL
, false },
152 bool ProcessGeneric(ots::OpenTypeFile
*header
,
154 ots::OTSStream
*output
,
155 const uint8_t *data
, size_t length
,
156 const std::vector
<OpenTypeTable
>& tables
,
159 bool ProcessTTF(ots::OpenTypeFile
*header
,
160 ots::OTSStream
*output
, const uint8_t *data
, size_t length
) {
161 ots::Buffer
file(data
, length
);
163 // we disallow all files > 1GB in size for sanity.
164 if (length
> 1024 * 1024 * 1024) {
165 return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
168 if (!file
.ReadTag(&header
->version
)) {
169 return OTS_FAILURE_MSG_HDR("error reading version tag");
171 if (!ots::IsValidVersionTag(header
->version
)) {
172 return OTS_FAILURE_MSG_HDR("invalid version tag");
175 if (!file
.ReadU16(&header
->num_tables
) ||
176 !file
.ReadU16(&header
->search_range
) ||
177 !file
.ReadU16(&header
->entry_selector
) ||
178 !file
.ReadU16(&header
->range_shift
)) {
179 return OTS_FAILURE_MSG_HDR("error reading table directory search header");
182 // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid
183 // overflow num_tables is, at most, 2^16 / 16 = 2^12
184 if (header
->num_tables
>= 4096 || header
->num_tables
< 1) {
185 return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables");
188 unsigned max_pow2
= 0;
189 while (1u << (max_pow2
+ 1) <= header
->num_tables
) {
192 const uint16_t expected_search_range
= (1u << max_pow2
) << 4;
194 // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in
195 // http://www.princexml.com/fonts/ have unexpected search_range value.
196 if (header
->search_range
!= expected_search_range
) {
197 OTS_FAILURE_MSG_HDR("bad search range");
198 header
->search_range
= expected_search_range
; // Fix the value.
201 // entry_selector is Log2(maximum power of 2 <= numTables)
202 if (header
->entry_selector
!= max_pow2
) {
203 return OTS_FAILURE_MSG_HDR("incorrect entrySelector for table directory");
206 // range_shift is NumTables x 16-searchRange. We know that 16*num_tables
207 // doesn't over flow because we range checked it above. Also, we know that
208 // it's > header->search_range by construction of search_range.
209 const uint16_t expected_range_shift
=
210 16 * header
->num_tables
- header
->search_range
;
211 if (header
->range_shift
!= expected_range_shift
) {
212 OTS_FAILURE_MSG_HDR("bad range shift");
213 header
->range_shift
= expected_range_shift
; // the same as above.
216 // Next up is the list of tables.
217 std::vector
<OpenTypeTable
> tables
;
219 for (unsigned i
= 0; i
< header
->num_tables
; ++i
) {
221 if (!file
.ReadTag(&table
.tag
) ||
222 !file
.ReadU32(&table
.chksum
) ||
223 !file
.ReadU32(&table
.offset
) ||
224 !file
.ReadU32(&table
.length
)) {
225 return OTS_FAILURE_MSG_HDR("error reading table directory");
228 table
.uncompressed_length
= table
.length
;
229 tables
.push_back(table
);
232 return ProcessGeneric(header
, header
->version
, output
, data
, length
,
236 bool ProcessWOFF(ots::OpenTypeFile
*header
,
237 ots::OTSStream
*output
, const uint8_t *data
, size_t length
) {
238 ots::Buffer
file(data
, length
);
240 // we disallow all files > 1GB in size for sanity.
241 if (length
> 1024 * 1024 * 1024) {
242 return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
246 if (!file
.ReadTag(&woff_tag
)) {
247 return OTS_FAILURE_MSG_HDR("error reading WOFF marker");
250 if (woff_tag
!= Tag("wOFF")) {
251 return OTS_FAILURE_MSG_HDR("invalid WOFF marker");
254 if (!file
.ReadTag(&header
->version
)) {
255 return OTS_FAILURE_MSG_HDR("error reading version tag");
257 if (!ots::IsValidVersionTag(header
->version
)) {
258 return OTS_FAILURE_MSG_HDR("invalid version tag");
261 header
->search_range
= 0;
262 header
->entry_selector
= 0;
263 header
->range_shift
= 0;
265 uint32_t reported_length
;
266 if (!file
.ReadU32(&reported_length
) || length
!= reported_length
) {
267 return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header");
270 if (!file
.ReadU16(&header
->num_tables
) || !header
->num_tables
) {
271 return OTS_FAILURE_MSG_HDR("error reading number of tables");
274 uint16_t reserved_value
;
275 if (!file
.ReadU16(&reserved_value
) || reserved_value
) {
276 return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header");
279 uint32_t reported_total_sfnt_size
;
280 if (!file
.ReadU32(&reported_total_sfnt_size
)) {
281 return OTS_FAILURE_MSG_HDR("error reading total sfnt size");
284 // We don't care about these fields of the header:
285 // uint16_t major_version, minor_version
286 if (!file
.Skip(2 * 2)) {
287 return OTS_FAILURE_MSG_HDR("error skipping WOFF header fields");
290 // Checks metadata block size.
291 uint32_t meta_offset
;
292 uint32_t meta_length
;
293 uint32_t meta_length_orig
;
294 if (!file
.ReadU32(&meta_offset
) ||
295 !file
.ReadU32(&meta_length
) ||
296 !file
.ReadU32(&meta_length_orig
)) {
297 return OTS_FAILURE_MSG_HDR("error reading WOFF header fields");
300 if (meta_offset
>= length
|| length
- meta_offset
< meta_length
) {
301 return OTS_FAILURE_MSG_HDR("invalid metadata block location/size");
305 // Checks private data block size.
306 uint32_t priv_offset
;
307 uint32_t priv_length
;
308 if (!file
.ReadU32(&priv_offset
) ||
309 !file
.ReadU32(&priv_length
)) {
310 return OTS_FAILURE_MSG_HDR("error reading WOFF header fields");
313 if (priv_offset
>= length
|| length
- priv_offset
< priv_length
) {
314 return OTS_FAILURE_MSG_HDR("invalid private block location/size");
318 // Next up is the list of tables.
319 std::vector
<OpenTypeTable
> tables
;
321 uint32_t first_index
= 0;
322 uint32_t last_index
= 0;
323 // Size of sfnt header plus size of table records.
324 uint64_t total_sfnt_size
= 12 + 16 * header
->num_tables
;
325 for (unsigned i
= 0; i
< header
->num_tables
; ++i
) {
327 if (!file
.ReadTag(&table
.tag
) ||
328 !file
.ReadU32(&table
.offset
) ||
329 !file
.ReadU32(&table
.length
) ||
330 !file
.ReadU32(&table
.uncompressed_length
) ||
331 !file
.ReadU32(&table
.chksum
)) {
332 return OTS_FAILURE_MSG_HDR("error reading table directory");
335 total_sfnt_size
+= ots::Round4(table
.uncompressed_length
);
336 if (total_sfnt_size
> std::numeric_limits
<uint32_t>::max()) {
337 return OTS_FAILURE_MSG_HDR("sfnt size overflow");
339 tables
.push_back(table
);
340 if (i
== 0 || tables
[first_index
].offset
> table
.offset
)
342 if (i
== 0 || tables
[last_index
].offset
< table
.offset
)
346 if (reported_total_sfnt_size
!= total_sfnt_size
) {
347 return OTS_FAILURE_MSG_HDR("uncompressed sfnt size mismatch");
350 // Table data must follow immediately after the header.
351 if (tables
[first_index
].offset
!= ots::Round4(file
.offset())) {
352 return OTS_FAILURE_MSG_HDR("junk before tables in WOFF file");
355 if (tables
[last_index
].offset
>= length
||
356 length
- tables
[last_index
].offset
< tables
[last_index
].length
) {
357 return OTS_FAILURE_MSG_HDR("invalid table location/size");
359 // Blocks must follow immediately after the previous block.
360 // (Except for padding with a maximum of three null bytes)
361 uint64_t block_end
= ots::Round4(
362 static_cast<uint64_t>(tables
[last_index
].offset
) +
363 static_cast<uint64_t>(tables
[last_index
].length
));
364 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
365 return OTS_FAILURE_MSG_HDR("invalid table location/size");
368 if (block_end
!= meta_offset
) {
369 return OTS_FAILURE_MSG_HDR("invalid metadata block location");
371 block_end
= ots::Round4(static_cast<uint64_t>(meta_offset
) +
372 static_cast<uint64_t>(meta_length
));
373 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
374 return OTS_FAILURE_MSG_HDR("invalid metadata block size");
378 if (block_end
!= priv_offset
) {
379 return OTS_FAILURE_MSG_HDR("invalid private block location");
381 block_end
= ots::Round4(static_cast<uint64_t>(priv_offset
) +
382 static_cast<uint64_t>(priv_length
));
383 if (block_end
> std::numeric_limits
<uint32_t>::max()) {
384 return OTS_FAILURE_MSG_HDR("invalid private block size");
387 if (block_end
!= ots::Round4(length
)) {
388 return OTS_FAILURE_MSG_HDR("file length mismatch (trailing junk?)");
391 return ProcessGeneric(header
, woff_tag
, output
, data
, length
, tables
, file
);
394 bool ProcessWOFF2(ots::OpenTypeFile
*header
,
395 ots::OTSStream
*output
, const uint8_t *data
, size_t length
) {
396 size_t decompressed_size
= ots::ComputeWOFF2FinalSize(data
, length
);
397 if (decompressed_size
== 0) {
398 return OTS_FAILURE();
400 // decompressed font must be <= 30MB
401 if (decompressed_size
> 30 * 1024 * 1024) {
402 return OTS_FAILURE();
405 std::vector
<uint8_t> decompressed_buffer(decompressed_size
);
406 if (!ots::ConvertWOFF2ToTTF(header
, &decompressed_buffer
[0], decompressed_size
,
408 return OTS_FAILURE();
410 return ProcessTTF(header
, output
, &decompressed_buffer
[0], decompressed_size
);
413 ots::TableAction
GetTableAction(ots::OpenTypeFile
*header
, uint32_t tag
) {
414 ots::TableAction action
= ots::TABLE_ACTION_DEFAULT
;
416 action
= header
->context
->GetTableAction(htonl(tag
));
418 if (action
== ots::TABLE_ACTION_DEFAULT
) {
419 action
= ots::TABLE_ACTION_DROP
;
421 for (unsigned i
= 0; ; ++i
) {
422 if (table_parsers
[i
].parse
== NULL
) break;
424 if (Tag(table_parsers
[i
].tag
) == tag
) {
425 action
= ots::TABLE_ACTION_SANITIZE
;
431 assert(action
!= ots::TABLE_ACTION_DEFAULT
); // Should never return this.
435 bool GetTableData(const uint8_t *data
,
436 const OpenTypeTable table
,
438 size_t *table_length
,
439 const uint8_t **table_data
) {
440 if (table
.uncompressed_length
!= table
.length
) {
441 // Compressed table. Need to uncompress into memory first.
442 *table_length
= table
.uncompressed_length
;
443 *table_data
= (*arena
).Allocate(*table_length
);
444 uLongf dest_len
= *table_length
;
445 int r
= uncompress((Bytef
*) *table_data
, &dest_len
,
446 data
+ table
.offset
, table
.length
);
447 if (r
!= Z_OK
|| dest_len
!= *table_length
) {
451 // Uncompressed table. We can process directly from memory.
452 *table_data
= data
+ table
.offset
;
453 *table_length
= table
.length
;
459 bool ProcessGeneric(ots::OpenTypeFile
*header
, uint32_t signature
,
460 ots::OTSStream
*output
,
461 const uint8_t *data
, size_t length
,
462 const std::vector
<OpenTypeTable
>& tables
,
464 const size_t data_offset
= file
.offset();
466 uint32_t uncompressed_sum
= 0;
468 for (unsigned i
= 0; i
< header
->num_tables
; ++i
) {
469 // the tables must be sorted by tag (when taken as big-endian numbers).
470 // This also remove the possibility of duplicate tables.
472 const uint32_t this_tag
= ntohl(tables
[i
].tag
);
473 const uint32_t prev_tag
= ntohl(tables
[i
- 1].tag
);
474 if (this_tag
<= prev_tag
) {
475 return OTS_FAILURE_MSG_HDR("table directory not correctly ordered");
479 // all tag names must be built from printable ASCII characters
480 if (!CheckTag(tables
[i
].tag
)) {
481 return OTS_FAILURE_MSG_TAG("invalid table tag", &tables
[i
].tag
);
484 // tables must be 4-byte aligned
485 if (tables
[i
].offset
& 3) {
486 return OTS_FAILURE_MSG_TAG("misaligned table", &tables
[i
].tag
);
489 // and must be within the file
490 if (tables
[i
].offset
< data_offset
|| tables
[i
].offset
>= length
) {
491 return OTS_FAILURE_MSG_TAG("invalid table offset", &tables
[i
].tag
);
493 // disallow all tables with a zero length
494 if (tables
[i
].length
< 1) {
495 // Note: malayalam.ttf has zero length CVT table...
496 return OTS_FAILURE_MSG_TAG("zero-length table", &tables
[i
].tag
);
498 // disallow all tables with a length > 1GB
499 if (tables
[i
].length
> 1024 * 1024 * 1024) {
500 return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", &tables
[i
].tag
);
502 // disallow tables where the uncompressed size is < the compressed size.
503 if (tables
[i
].uncompressed_length
< tables
[i
].length
) {
504 return OTS_FAILURE_MSG_TAG("invalid compressed table", &tables
[i
].tag
);
506 if (tables
[i
].uncompressed_length
> tables
[i
].length
) {
507 // We'll probably be decompressing this table.
509 // disallow all tables which uncompress to > 30 MB
510 if (tables
[i
].uncompressed_length
> 30 * 1024 * 1024) {
511 return OTS_FAILURE_MSG_TAG("uncompressed length exceeds 30MB", &tables
[i
].tag
);
513 if (uncompressed_sum
+ tables
[i
].uncompressed_length
< uncompressed_sum
) {
514 return OTS_FAILURE_MSG_TAG("overflow of uncompressed sum", &tables
[i
].tag
);
517 uncompressed_sum
+= tables
[i
].uncompressed_length
;
519 // since we required that the file be < 1GB in length, and that the table
520 // length is < 1GB, the following addtion doesn't overflow
521 uint32_t end_byte
= tables
[i
].offset
+ tables
[i
].length
;
522 // Tables in the WOFF file must be aligned 4-byte boundary.
523 if (signature
== Tag("wOFF")) {
524 end_byte
= ots::Round4(end_byte
);
526 if (!end_byte
|| end_byte
> length
) {
527 return OTS_FAILURE_MSG_TAG("table overruns end of file", &tables
[i
].tag
);
531 // All decompressed tables uncompressed must be <= 30MB.
532 if (uncompressed_sum
> 30 * 1024 * 1024) {
533 return OTS_FAILURE_MSG_HDR("uncompressed sum exceeds 30MB");
536 std::map
<uint32_t, OpenTypeTable
> table_map
;
537 for (unsigned i
= 0; i
< header
->num_tables
; ++i
) {
538 table_map
[tables
[i
].tag
] = tables
[i
];
541 // check that the tables are not overlapping.
542 std::vector
<std::pair
<uint32_t, uint8_t> > overlap_checker
;
543 for (unsigned i
= 0; i
< header
->num_tables
; ++i
) {
544 overlap_checker
.push_back(
545 std::make_pair(tables
[i
].offset
, static_cast<uint8_t>(1) /* start */));
546 overlap_checker
.push_back(
547 std::make_pair(tables
[i
].offset
+ tables
[i
].length
,
548 static_cast<uint8_t>(0) /* end */));
550 std::sort(overlap_checker
.begin(), overlap_checker
.end());
551 int overlap_count
= 0;
552 for (unsigned i
= 0; i
< overlap_checker
.size(); ++i
) {
553 overlap_count
+= (overlap_checker
[i
].second
? 1 : -1);
554 if (overlap_count
> 1) {
555 return OTS_FAILURE_MSG_HDR("overlapping tables");
561 for (unsigned i
= 0; ; ++i
) {
562 if (table_parsers
[i
].parse
== NULL
) break;
564 uint32_t tag
= Tag(table_parsers
[i
].tag
);
565 const std::map
<uint32_t, OpenTypeTable
>::const_iterator it
= table_map
.find(tag
);
567 ots::TableAction action
= GetTableAction(header
, tag
);
568 if (it
== table_map
.end()) {
569 if (table_parsers
[i
].required
&& action
== ots::TABLE_ACTION_SANITIZE
) {
570 return OTS_FAILURE_MSG_TAG("missing required table", table_parsers
[i
].tag
);
575 const uint8_t* table_data
;
578 if (!GetTableData(data
, it
->second
, &arena
, &table_length
, &table_data
)) {
579 return OTS_FAILURE_MSG_TAG("uncompress failed", table_parsers
[i
].tag
);
582 if (action
== ots::TABLE_ACTION_SANITIZE
&&
583 !table_parsers
[i
].parse(header
, table_data
, table_length
)) {
584 // TODO: parsers should generate specific messages detailing the failure;
585 // once those are all added, we won't need a generic failure message here
586 return OTS_FAILURE_MSG_TAG("failed to parse table", table_parsers
[i
].tag
);
591 // font with PostScript glyph
592 if (header
->version
!= Tag("OTTO")) {
593 return OTS_FAILURE_MSG_HDR("wrong font version for PostScript glyph data");
595 if (header
->glyf
|| header
->loca
) {
596 // mixing outline formats is not recommended
597 return OTS_FAILURE_MSG_HDR("font contains both PS and TT glyphs");
600 if (!header
->glyf
|| !header
->loca
) {
601 // No TrueType glyph found.
602 // Note: bitmap-only fonts are not supported.
603 return OTS_FAILURE_MSG_HDR("neither PS nor TT glyphs present");
607 uint16_t num_output_tables
= 0;
608 for (unsigned i
= 0; ; ++i
) {
609 if (table_parsers
[i
].parse
== NULL
) {
613 if (table_parsers
[i
].should_serialise(header
)) {
618 for (std::map
<uint32_t, OpenTypeTable
>::const_iterator it
= table_map
.begin();
619 it
!= table_map
.end(); ++it
) {
620 ots::TableAction action
= GetTableAction(header
, it
->first
);
621 if (action
== ots::TABLE_ACTION_PASSTHRU
) {
626 uint16_t max_pow2
= 0;
627 while (1u << (max_pow2
+ 1) <= num_output_tables
) {
630 const uint16_t output_search_range
= (1u << max_pow2
) << 4;
632 // most of the errors here are highly unlikely - they'd only occur if the
633 // output stream returns a failure, e.g. lack of space to write
634 output
->ResetChecksum();
635 if (!output
->WriteTag(header
->version
) ||
636 !output
->WriteU16(num_output_tables
) ||
637 !output
->WriteU16(output_search_range
) ||
638 !output
->WriteU16(max_pow2
) ||
639 !output
->WriteU16((num_output_tables
<< 4) - output_search_range
)) {
640 return OTS_FAILURE_MSG_HDR("error writing output");
642 const uint32_t offset_table_chksum
= output
->chksum();
644 const size_t table_record_offset
= output
->Tell();
645 if (!output
->Pad(16 * num_output_tables
)) {
646 return OTS_FAILURE_MSG_HDR("error writing output");
649 std::vector
<OutputTable
> out_tables
;
651 size_t head_table_offset
= 0;
652 for (unsigned i
= 0; ; ++i
) {
653 if (table_parsers
[i
].parse
== NULL
) {
657 if (!table_parsers
[i
].should_serialise(header
)) {
662 uint32_t tag
= Tag(table_parsers
[i
].tag
);
664 out
.offset
= output
->Tell();
666 output
->ResetChecksum();
667 if (tag
== Tag("head")) {
668 head_table_offset
= out
.offset
;
670 if (!table_parsers
[i
].serialise(output
, header
)) {
671 return OTS_FAILURE_MSG_TAG("failed to serialize table", table_parsers
[i
].tag
);
674 const size_t end_offset
= output
->Tell();
675 if (end_offset
<= out
.offset
) {
676 // paranoid check. |end_offset| is supposed to be greater than the offset,
677 // as long as the Tell() interface is implemented correctly.
678 return OTS_FAILURE_MSG_HDR("error writing output");
680 out
.length
= end_offset
- out
.offset
;
682 // align tables to four bytes
683 if (!output
->Pad((4 - (end_offset
& 3)) % 4)) {
684 return OTS_FAILURE_MSG_HDR("error writing output");
686 out
.chksum
= output
->chksum();
687 out_tables
.push_back(out
);
690 for (std::map
<uint32_t, OpenTypeTable
>::const_iterator it
= table_map
.begin();
691 it
!= table_map
.end(); ++it
) {
692 ots::TableAction action
= GetTableAction(header
, it
->first
);
693 if (action
== ots::TABLE_ACTION_PASSTHRU
) {
695 out
.tag
= it
->second
.tag
;
696 out
.offset
= output
->Tell();
698 output
->ResetChecksum();
699 if (it
->second
.tag
== Tag("head")) {
700 head_table_offset
= out
.offset
;
703 const uint8_t* table_data
;
706 if (!GetTableData(data
, it
->second
, &arena
, &table_length
, &table_data
)) {
707 return OTS_FAILURE_MSG_HDR("Failed to uncompress table");
710 if (!output
->Write(table_data
, table_length
)) {
711 return OTS_FAILURE_MSG_HDR("Failed to serialize table");
714 const size_t end_offset
= output
->Tell();
715 if (end_offset
<= out
.offset
) {
716 // paranoid check. |end_offset| is supposed to be greater than the offset,
717 // as long as the Tell() interface is implemented correctly.
718 return OTS_FAILURE_MSG_HDR("error writing output");
720 out
.length
= end_offset
- out
.offset
;
722 // align tables to four bytes
723 if (!output
->Pad((4 - (end_offset
& 3)) % 4)) {
724 return OTS_FAILURE_MSG_HDR("error writing output");
726 out
.chksum
= output
->chksum();
727 out_tables
.push_back(out
);
731 const size_t end_of_file
= output
->Tell();
733 // Need to sort the output tables for inclusion in the file
734 std::sort(out_tables
.begin(), out_tables
.end(), OutputTable::SortByTag
);
735 if (!output
->Seek(table_record_offset
)) {
736 return OTS_FAILURE_MSG_HDR("error writing output");
739 output
->ResetChecksum();
740 uint32_t tables_chksum
= 0;
741 for (unsigned i
= 0; i
< out_tables
.size(); ++i
) {
742 if (!output
->WriteTag(out_tables
[i
].tag
) ||
743 !output
->WriteU32(out_tables
[i
].chksum
) ||
744 !output
->WriteU32(out_tables
[i
].offset
) ||
745 !output
->WriteU32(out_tables
[i
].length
)) {
746 return OTS_FAILURE_MSG_HDR("error writing output");
748 tables_chksum
+= out_tables
[i
].chksum
;
750 const uint32_t table_record_chksum
= output
->chksum();
752 // http://www.microsoft.com/typography/otspec/otff.htm
753 const uint32_t file_chksum
754 = offset_table_chksum
+ tables_chksum
+ table_record_chksum
;
755 const uint32_t chksum_magic
= static_cast<uint32_t>(0xb1b0afba) - file_chksum
;
757 // seek into the 'head' table and write in the checksum magic value
758 if (!head_table_offset
) {
759 return OTS_FAILURE_MSG_HDR("internal error!");
761 if (!output
->Seek(head_table_offset
+ 8)) {
762 return OTS_FAILURE_MSG_HDR("error writing output");
764 if (!output
->WriteU32(chksum_magic
)) {
765 return OTS_FAILURE_MSG_HDR("error writing output");
768 if (!output
->Seek(end_of_file
)) {
769 return OTS_FAILURE_MSG_HDR("error writing output");
782 bool IsValidVersionTag(uint32_t tag
) {
783 return tag
== Tag("\x00\x01\x00\x00") ||
784 // OpenType fonts with CFF data have 'OTTO' tag.
785 tag
== Tag("OTTO") ||
786 // Older Mac fonts might have 'true' or 'typ1' tag.
787 tag
== Tag("true") ||
791 bool OTSContext::Process(OTSStream
*output
,
796 header
.context
= this;
799 return OTS_FAILURE_MSG_(&header
, "file less than 4 bytes");
803 if (data
[0] == 'w' && data
[1] == 'O' && data
[2] == 'F' && data
[3] == 'F') {
804 result
= ProcessWOFF(&header
, output
, data
, length
);
805 } else if (data
[0] == 'w' && data
[1] == 'O' && data
[2] == 'F' && data
[3] == '2') {
806 result
= ProcessWOFF2(&header
, output
, data
, length
);
808 result
= ProcessTTF(&header
, output
, data
, length
);
811 for (unsigned i
= 0; ; ++i
) {
812 if (table_parsers
[i
].parse
== NULL
) break;
813 table_parsers
[i
].free(&header
);
818 // For backward compatibility
819 bool Process(OTSStream
*output
, const uint8_t *data
, size_t length
) {
820 static OTSContext context
;
821 return context
.Process(output
, data
, length
);