blob: 5357ddcf5bede4dfef896c8ee74652189b3e0402 [file] [log] [blame]
Behdad Esfahbod2098a022009-08-02 19:57:00 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2012 Google, Inc.
Behdad Esfahbod2098a022009-08-02 19:57:00 -04004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod2098a022009-08-02 19:57:00 -04006 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +020026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod2098a022009-08-02 19:57:00 -040027 */
28
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040029#ifndef HB_OPEN_FILE_PRIVATE_HH
30#define HB_OPEN_FILE_PRIVATE_HH
Behdad Esfahbod2098a022009-08-02 19:57:00 -040031
Behdad Esfahbod7edb4302009-08-04 22:06:57 -040032#include "hb-open-type-private.hh"
Behdad Esfahbod2098a022009-08-02 19:57:00 -040033
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040034
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040035namespace OT {
36
Behdad Esfahbod2098a022009-08-02 19:57:00 -040037
38/*
39 *
40 * The OpenType Font File
41 *
42 */
43
44
45/*
46 * Organization of an OpenType Font
47 */
48
49struct OpenTypeFontFile;
50struct OffsetTable;
51struct TTCHeader;
52
Behdad Esfahbod569da922010-05-10 16:38:32 -040053
Behdad Esfahbod22c53762010-12-14 23:51:29 -050054typedef struct TableRecord
Behdad Esfahbod2098a022009-08-02 19:57:00 -040055{
Behdad Esfahbodde2118e2015-02-17 17:27:44 +030056 inline bool sanitize (hb_sanitize_context_t *c) const
57 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050058 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +010059 return_trace (c->check_struct (this));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -040060 }
61
Behdad Esfahbod2098a022009-08-02 19:57:00 -040062 Tag tag; /* 4-byte identifier. */
63 CheckSum checkSum; /* CheckSum for this table. */
64 ULONG offset; /* Offset from beginning of TrueType font
65 * file. */
66 ULONG length; /* Length of this table. */
Behdad Esfahbod569da922010-05-10 16:38:32 -040067 public:
68 DEFINE_SIZE_STATIC (16);
Behdad Esfahbod2098a022009-08-02 19:57:00 -040069} OpenTypeTable;
Behdad Esfahbod2098a022009-08-02 19:57:00 -040070
71typedef struct OffsetTable
72{
73 friend struct OpenTypeFontFile;
Behdad Esfahbod2098a022009-08-02 19:57:00 -040074
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040075 inline unsigned int get_table_count (void) const
76 { return numTables; }
Behdad Esfahbod22c53762010-12-14 23:51:29 -050077 inline const TableRecord& get_table (unsigned int i) const
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040078 {
Behdad Esfahbod22c53762010-12-14 23:51:29 -050079 if (unlikely (i >= numTables)) return Null(TableRecord);
80 return tables[i];
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040081 }
82 inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
83 {
Behdad Esfahbodffd321a2010-04-21 00:14:12 -040084 Tag t;
85 t.set (tag);
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040086 unsigned int count = numTables;
87 for (unsigned int i = 0; i < count; i++)
88 {
Behdad Esfahbod22c53762010-12-14 23:51:29 -050089 if (t == tables[i].tag)
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040090 {
91 if (table_index) *table_index = i;
92 return true;
93 }
94 }
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -040095 if (table_index) *table_index = Index::NOT_FOUND_INDEX;
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040096 return false;
97 }
Behdad Esfahbod22c53762010-12-14 23:51:29 -050098 inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040099 {
100 unsigned int table_index;
101 find_table_index (tag, &table_index);
102 return get_table (table_index);
103 }
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400104
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400105 public:
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300106 inline bool sanitize (hb_sanitize_context_t *c) const
107 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500108 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100109 return_trace (c->check_struct (this) && c->check_array (tables, TableRecord::static_size, numTables));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400110 }
111
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400112 protected:
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400113 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
114 USHORT numTables; /* Number of tables. */
Behdad Esfahbod7d4ada62014-06-27 17:30:59 -0400115 USHORT searchRangeZ; /* (Maximum power of 2 <= numTables) x 16 */
116 USHORT entrySelectorZ; /* Log2(maximum power of 2 <= numTables). */
117 USHORT rangeShiftZ; /* NumTables x 16-searchRange. */
Behdad Esfahbod22c53762010-12-14 23:51:29 -0500118 TableRecord tables[VAR]; /* TableRecord entries. numTables items */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400119 public:
Behdad Esfahbod22c53762010-12-14 23:51:29 -0500120 DEFINE_SIZE_ARRAY (12, tables);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400121} OpenTypeFontFace;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400122
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400123
124/*
125 * TrueType Collections
126 */
127
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400128struct TTCHeaderVersion1
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400129{
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400130 friend struct TTCHeader;
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400131
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400132 inline unsigned int get_face_count (void) const { return table.len; }
Behdad Esfahboddf3f5052010-04-22 14:11:33 -0400133 inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400134
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300135 inline bool sanitize (hb_sanitize_context_t *c) const
136 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500137 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100138 return_trace (table.sanitize (c, this));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400139 }
140
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400141 protected:
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400142 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900143 FixedVersion<>version; /* Version of the TTC Header (1.0),
Behdad Esfahbod76271002014-07-11 14:54:42 -0400144 * 0x00010000u */
Behdad Esfahbod30847672014-06-27 15:24:35 -0400145 ArrayOf<OffsetTo<OffsetTable, ULONG>, ULONG>
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400146 table; /* Array of offsets to the OffsetTable for each font
147 * from the beginning of the file */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400148 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400149 DEFINE_SIZE_ARRAY (12, table);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400150};
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400151
152struct TTCHeader
153{
154 friend struct OpenTypeFontFile;
155
156 private:
157
158 inline unsigned int get_face_count (void) const
159 {
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400160 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400161 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400162 case 1: return u.version1.get_face_count ();
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400163 default:return 0;
164 }
165 }
166 inline const OpenTypeFontFace& get_face (unsigned int i) const
167 {
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400168 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400169 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400170 case 1: return u.version1.get_face (i);
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400171 default:return Null(OpenTypeFontFace);
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400172 }
173 }
174
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300175 inline bool sanitize (hb_sanitize_context_t *c) const
176 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500177 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100178 if (unlikely (!u.header.version.sanitize (c))) return_trace (false);
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400179 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400180 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100181 case 1: return_trace (u.version1.sanitize (c));
182 default:return_trace (true);
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400183 }
184 }
185
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400186 protected:
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400187 union {
188 struct {
189 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900190 FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0),
Behdad Esfahbod76271002014-07-11 14:54:42 -0400191 * 0x00010000u or 0x00020000u */
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400192 } header;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400193 TTCHeaderVersion1 version1;
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400194 } u;
195};
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400196
197
198/*
199 * OpenType Font File
200 */
201
202struct OpenTypeFontFile
203{
Behdad Esfahbod153beeb2014-12-12 19:46:09 -0800204 static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */
205
Behdad Esfahbod710500a2010-05-03 23:11:16 -0400206 static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */
207 static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */
208 static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f'); /* TrueType Collection */
Behdad Esfahbodce5694c2010-05-04 14:10:18 -0400209 static const hb_tag_t TrueTag = HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */
210 static const hb_tag_t Typ1Tag = HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400211
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400212 inline hb_tag_t get_tag (void) const { return u.tag; }
213
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400214 inline unsigned int get_face_count (void) const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400215 {
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400216 switch (u.tag) {
217 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400218 case TrueTag:
219 case Typ1Tag:
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400220 case TrueTypeTag: return 1;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400221 case TTCTag: return u.ttcHeader.get_face_count ();
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400222 default: return 0;
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400223 }
224 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400225 inline const OpenTypeFontFace& get_face (unsigned int i) const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400226 {
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400227 switch (u.tag) {
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400228 /* Note: for non-collection SFNT data we ignore index. This is because
229 * Apple dfont container is a container of SFNT's. So each SFNT is a
230 * non-TTC, but the index is more than zero. */
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400231 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400232 case TrueTag:
233 case Typ1Tag:
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400234 case TrueTypeTag: return u.fontFace;
235 case TTCTag: return u.ttcHeader.get_face (i);
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400236 default: return Null(OpenTypeFontFace);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400237 }
238 }
239
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300240 inline bool sanitize (hb_sanitize_context_t *c) const
241 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500242 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100243 if (unlikely (!u.tag.sanitize (c))) return_trace (false);
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400244 switch (u.tag) {
245 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400246 case TrueTag:
247 case Typ1Tag:
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100248 case TrueTypeTag: return_trace (u.fontFace.sanitize (c));
249 case TTCTag: return_trace (u.ttcHeader.sanitize (c));
250 default: return_trace (true);
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400251 }
252 }
253
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400254 protected:
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400255 union {
256 Tag tag; /* 4-byte identifier. */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400257 OpenTypeFontFace fontFace;
258 TTCHeader ttcHeader;
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400259 } u;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400260 public:
261 DEFINE_SIZE_UNION (4, tag);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400262};
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400263
264
Behdad Esfahbod7d52e662012-11-16 18:49:54 -0800265} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400266
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400267
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400268#endif /* HB_OPEN_FILE_PRIVATE_HH */