2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OPEN_FILE_PRIVATE_HH
30 #define HB_OPEN_FILE_PRIVATE_HH
32 #include "hb-open-type-private.hh"
33 #include "hb-ot-head-table.hh"
41 * The OpenType Font File
47 * Organization of an OpenType Font
50 struct OpenTypeFontFile;
55 typedef struct TableRecord
58 { return -t.cmp (tag); }
60 static int cmp (const void *pa, const void *pb)
62 const TableRecord *a = (const TableRecord *) pa;
63 const TableRecord *b = (const TableRecord *) pb;
64 return b->cmp (a->tag);
67 inline bool sanitize (hb_sanitize_context_t *c) const
69 TRACE_SANITIZE (this);
70 return_trace (c->check_struct (this));
73 Tag tag; /* 4-byte identifier. */
74 CheckSum checkSum; /* CheckSum for this table. */
75 Offset32 offset; /* Offset from beginning of TrueType font
77 HBUINT32 length; /* Length of this table. */
79 DEFINE_SIZE_STATIC (16);
82 typedef struct OffsetTable
84 friend struct OpenTypeFontFile;
86 inline unsigned int get_table_count (void) const
87 { return tables.len; }
88 inline const TableRecord& get_table (unsigned int i) const
92 inline unsigned int get_table_tags (unsigned int start_offset,
93 unsigned int *table_count, /* IN/OUT */
94 hb_tag_t *table_tags /* OUT */) const
98 if (start_offset >= tables.len)
101 *table_count = MIN<unsigned int> (*table_count, tables.len - start_offset);
103 const TableRecord *sub_tables = tables.arrayZ + start_offset;
104 unsigned int count = *table_count;
105 for (unsigned int i = 0; i < count; i++)
106 table_tags[i] = sub_tables[i].tag;
110 inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
114 /* Linear-search for small tables to work around fonts with unsorted
116 int i = tables.len < 64 ? tables.lsearch (t) : tables.bsearch (t);
118 *table_index = i == -1 ? Index::NOT_FOUND_INDEX : (unsigned int) i;
121 inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
123 unsigned int table_index;
124 find_table_index (tag, &table_index);
125 return get_table (table_index);
130 inline bool serialize (hb_serialize_context_t *c,
132 Supplier<hb_tag_t> &tags,
133 Supplier<hb_blob_t *> &blobs,
134 unsigned int table_count)
136 TRACE_SERIALIZE (this);
137 /* Alloc 12 for the OTHeader. */
138 if (unlikely (!c->extend_min (*this))) return_trace (false);
139 /* Write sfntVersion (bytes 0..3). */
140 sfnt_version.set (sfnt_tag);
141 /* Take space for numTables, searchRange, entrySelector, RangeShift
142 * and the TableRecords themselves. */
143 if (unlikely (!tables.serialize (c, table_count))) return_trace (false);
145 const char *dir_end = (const char *) c->head;
146 HBUINT32 *checksum_adjustment = nullptr;
148 /* Write OffsetTables, alloc for and write actual table blobs. */
149 for (unsigned int i = 0; i < table_count; i++)
151 TableRecord &rec = tables.arrayZ[i];
152 hb_blob_t *blob = blobs[i];
153 rec.tag.set (tags[i]);
154 rec.length.set (hb_blob_get_length (blob));
155 rec.offset.serialize (c, this);
157 /* Allocate room for the table and copy it. */
158 char *start = (char *) c->allocate_size<void> (rec.length);
159 if (unlikely (!start)) {return false;}
161 memcpy (start, hb_blob_get_data (blob, nullptr), rec.length);
163 /* 4-byte allignment. */
165 c->allocate_size<void> (4 - rec.length % 4);
166 const char *end = (const char *) c->head;
168 if (tags[i] == HB_OT_TAG_head && end - start >= head::static_size)
170 head *h = (head *) start;
171 checksum_adjustment = &h->checkSumAdjustment;
172 checksum_adjustment->set (0);
175 rec.checkSum.set_for_data (start, end - start);
178 blobs += table_count;
182 if (checksum_adjustment)
186 /* The following line is a slower version of the following block. */
187 //checksum.set_for_data (this, (const char *) c->head - (const char *) this);
188 checksum.set_for_data (this, dir_end - (const char *) this);
189 for (unsigned int i = 0; i < table_count; i++)
191 TableRecord &rec = tables.arrayZ[i];
192 checksum.set (checksum + rec.checkSum);
195 checksum_adjustment->set (0xB1B0AFBAu - checksum);
201 inline bool sanitize (hb_sanitize_context_t *c) const
203 TRACE_SANITIZE (this);
204 return_trace (c->check_struct (this) && tables.sanitize (c));
208 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
209 BinSearchArrayOf<TableRecord>
212 DEFINE_SIZE_ARRAY (12, tables);
217 * TrueType Collections
220 struct TTCHeaderVersion1
222 friend struct TTCHeader;
224 inline unsigned int get_face_count (void) const { return table.len; }
225 inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
227 inline bool sanitize (hb_sanitize_context_t *c) const
229 TRACE_SANITIZE (this);
230 return_trace (table.sanitize (c, this));
234 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
235 FixedVersion<>version; /* Version of the TTC Header (1.0),
237 LArrayOf<LOffsetTo<OffsetTable> >
238 table; /* Array of offsets to the OffsetTable for each font
239 * from the beginning of the file */
241 DEFINE_SIZE_ARRAY (12, table);
246 friend struct OpenTypeFontFile;
250 inline unsigned int get_face_count (void) const
252 switch (u.header.version.major) {
253 case 2: /* version 2 is compatible with version 1 */
254 case 1: return u.version1.get_face_count ();
258 inline const OpenTypeFontFace& get_face (unsigned int i) const
260 switch (u.header.version.major) {
261 case 2: /* version 2 is compatible with version 1 */
262 case 1: return u.version1.get_face (i);
263 default:return Null(OpenTypeFontFace);
267 inline bool sanitize (hb_sanitize_context_t *c) const
269 TRACE_SANITIZE (this);
270 if (unlikely (!u.header.version.sanitize (c))) return_trace (false);
271 switch (u.header.version.major) {
272 case 2: /* version 2 is compatible with version 1 */
273 case 1: return_trace (u.version1.sanitize (c));
274 default:return_trace (true);
281 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
282 FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0),
283 * 0x00010000u or 0x00020000u */
285 TTCHeaderVersion1 version1;
294 struct OpenTypeFontFile
296 static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */
299 CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */
300 TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */
301 TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */
302 TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */
303 Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */
306 inline hb_tag_t get_tag (void) const { return u.tag; }
308 inline unsigned int get_face_count (void) const
311 case CFFTag: /* All the non-collection tags */
314 case TrueTypeTag: return 1;
315 case TTCTag: return u.ttcHeader.get_face_count ();
319 inline const OpenTypeFontFace& get_face (unsigned int i) const
322 /* Note: for non-collection SFNT data we ignore index. This is because
323 * Apple dfont container is a container of SFNT's. So each SFNT is a
324 * non-TTC, but the index is more than zero. */
325 case CFFTag: /* All the non-collection tags */
328 case TrueTypeTag: return u.fontFace;
329 case TTCTag: return u.ttcHeader.get_face (i);
330 default: return Null(OpenTypeFontFace);
334 inline bool serialize_single (hb_serialize_context_t *c,
336 Supplier<hb_tag_t> &tags,
337 Supplier<hb_blob_t *> &blobs,
338 unsigned int table_count)
340 TRACE_SERIALIZE (this);
341 assert (sfnt_tag != TTCTag);
342 if (unlikely (!c->extend_min (*this))) return_trace (false);
343 return_trace (u.fontFace.serialize (c, sfnt_tag, tags, blobs, table_count));
346 inline bool sanitize (hb_sanitize_context_t *c) const
348 TRACE_SANITIZE (this);
349 if (unlikely (!u.tag.sanitize (c))) return_trace (false);
351 case CFFTag: /* All the non-collection tags */
354 case TrueTypeTag: return_trace (u.fontFace.sanitize (c));
355 case TTCTag: return_trace (u.ttcHeader.sanitize (c));
356 default: return_trace (true);
362 Tag tag; /* 4-byte identifier. */
363 OpenTypeFontFace fontFace;
367 DEFINE_SIZE_UNION (4, tag);
374 #endif /* HB_OPEN_FILE_PRIVATE_HH */