blob: 04f144a72c7b42888320d168f64f7cf654d17b42 [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 Esfahbodc77ae402018-08-25 22:36:36 -070029#ifndef HB_OPEN_FILE_HH
30#define HB_OPEN_FILE_HH
Behdad Esfahbod2098a022009-08-02 19:57:00 -040031
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070032#include "hb-open-type.hh"
Behdad Esfahboddc5c7922018-02-23 16:42:06 -080033#include "hb-ot-head-table.hh"
Behdad Esfahbod2098a022009-08-02 19:57:00 -040034
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040035
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040036namespace OT {
37
Behdad Esfahbod2098a022009-08-02 19:57:00 -040038/*
39 *
40 * The OpenType Font File
41 *
42 */
43
44
45/*
46 * Organization of an OpenType Font
47 */
48
49struct OpenTypeFontFile;
Behdad Esfahbod505b3fc2021-02-17 11:34:47 -070050struct OpenTypeOffsetTable;
Behdad Esfahbod2098a022009-08-02 19:57:00 -040051struct 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{
Ebrahim Byagowie4120082018-12-17 21:31:01 +033056 int cmp (Tag t) const { return -t.cmp (tag); }
Behdad Esfahbod6c04dcb2017-10-31 20:11:00 -060057
Behdad Esfahbod95df00a2019-04-12 17:50:03 -040058 HB_INTERNAL static int cmp (const void *pa, const void *pb)
Behdad Esfahbodc479a592018-02-07 21:13:10 -060059 {
60 const TableRecord *a = (const TableRecord *) pa;
61 const TableRecord *b = (const TableRecord *) pb;
62 return b->cmp (a->tag);
63 }
64
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033065 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +030066 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050067 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +010068 return_trace (c->check_struct (this));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -040069 }
70
Behdad Esfahbod2098a022009-08-02 19:57:00 -040071 Tag tag; /* 4-byte identifier. */
72 CheckSum checkSum; /* CheckSum for this table. */
Behdad Esfahbodc479a592018-02-07 21:13:10 -060073 Offset32 offset; /* Offset from beginning of TrueType font
Behdad Esfahbod2098a022009-08-02 19:57:00 -040074 * file. */
Behdad Esfahbodc9acab32018-02-07 17:12:55 -060075 HBUINT32 length; /* Length of this table. */
Behdad Esfahbod569da922010-05-10 16:38:32 -040076 public:
77 DEFINE_SIZE_STATIC (16);
Behdad Esfahbod2098a022009-08-02 19:57:00 -040078} OpenTypeTable;
Behdad Esfahbod2098a022009-08-02 19:57:00 -040079
Behdad Esfahbod505b3fc2021-02-17 11:34:47 -070080typedef struct OpenTypeOffsetTable
Behdad Esfahbod2098a022009-08-02 19:57:00 -040081{
82 friend struct OpenTypeFontFile;
Behdad Esfahbod2098a022009-08-02 19:57:00 -040083
Ebrahim Byagowie4120082018-12-17 21:31:01 +033084 unsigned int get_table_count () const { return tables.len; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033085 const TableRecord& get_table (unsigned int i) const
Ebrahim Byagowie4120082018-12-17 21:31:01 +033086 { return tables[i]; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033087 unsigned int get_table_tags (unsigned int start_offset,
Behdad Esfahbod07776b62019-04-15 16:43:34 -040088 unsigned int *table_count, /* IN/OUT */
89 hb_tag_t *table_tags /* OUT */) const
Behdad Esfahbod94b3caf2017-10-11 17:22:44 +020090 {
91 if (table_count)
92 {
Behdad Esfahboda0bde1e2022-11-16 21:27:12 -070093 + tables.as_array ().sub_array (start_offset, table_count)
Ebrahim Byagowi25cf6872020-06-20 01:36:50 +043094 | hb_map (&TableRecord::tag)
95 | hb_sink (hb_array (table_tags, *table_count))
96 ;
Behdad Esfahbod94b3caf2017-10-11 17:22:44 +020097 }
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -060098 return tables.len;
Behdad Esfahbod94b3caf2017-10-11 17:22:44 +020099 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330100 bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400101 {
Behdad Esfahbodffd321a2010-04-21 00:14:12 -0400102 Tag t;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700103 t = tag;
Behdad Esfahbodba6db262021-07-22 11:37:39 -0700104 /* Use lfind for small fonts; there are fonts that have unsorted table entries;
105 * those tend to work in other tools, so tolerate them.
106 * https://github.com/harfbuzz/harfbuzz/issues/3065 */
107 if (tables.len < 16)
108 return tables.lfind (t, table_index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
109 else
110 return tables.bfind (t, table_index, HB_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400111 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330112 const TableRecord& get_table_by_tag (hb_tag_t tag) const
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400113 {
114 unsigned int table_index;
115 find_table_index (tag, &table_index);
116 return get_table (table_index);
117 }
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400118
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400119 public:
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600120
Behdad Esfahboda110a472021-08-05 12:28:39 -0600121 template <typename Iterator,
Behdad Esfahbodf7a9c3e2021-08-05 13:05:07 -0600122 hb_requires ((hb_is_source_of<Iterator, hb_pair_t<hb_tag_t, hb_blob_t *>>::value))>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330123 bool serialize (hb_serialize_context_t *c,
124 hb_tag_t sfnt_tag,
Behdad Esfahbodb57ea3b2022-08-05 14:29:27 -0600125 Iterator it)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600126 {
127 TRACE_SERIALIZE (this);
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800128 /* Alloc 12 for the OTHeader. */
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600129 if (unlikely (!c->extend_min (this))) return_trace (false);
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800130 /* Write sfntVersion (bytes 0..3). */
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700131 sfnt_version = sfnt_tag;
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800132 /* Take space for numTables, searchRange, entrySelector, RangeShift
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800133 * and the TableRecords themselves. */
Behdad Esfahbod2c359632023-06-26 18:28:11 -0600134 unsigned num_items = hb_len (it);
Garret Riegerdea0fe52021-08-04 16:36:20 -0700135 if (unlikely (!tables.serialize (c, num_items))) return_trace (false);
Behdad Esfahbod941bbd92018-02-07 21:49:01 -0600136
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800137 const char *dir_end = (const char *) c->head;
138 HBUINT32 *checksum_adjustment = nullptr;
139
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800140 /* Write OffsetTables, alloc for and write actual table blobs. */
Garret Riegerdea0fe52021-08-04 16:36:20 -0700141 unsigned i = 0;
142 for (hb_pair_t<hb_tag_t, hb_blob_t*> entry : it)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600143 {
Garret Riegerdea0fe52021-08-04 16:36:20 -0700144 hb_blob_t *blob = entry.second;
Behdad Esfahbod29c98332021-07-09 11:58:36 -0600145 unsigned len = blob->length;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800146
147 /* Allocate room for the table and copy it. */
Behdad Esfahbod2c359632023-06-26 18:28:11 -0600148 char *start = (char *) c->allocate_size<void> (len, false);
Ebrahim Byagowia6065d02019-07-10 16:41:40 +0430149 if (unlikely (!start)) return false;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800150
Behdad Esfahbod29c98332021-07-09 11:58:36 -0600151 TableRecord &rec = tables.arrayZ[i];
Garret Riegerdea0fe52021-08-04 16:36:20 -0700152 rec.tag = entry.first;
Behdad Esfahbod29c98332021-07-09 11:58:36 -0600153 rec.length = len;
154 rec.offset = 0;
155 if (unlikely (!c->check_assign (rec.offset,
156 (unsigned) ((char *) start - (char *) this),
157 HB_SERIALIZE_ERROR_OFFSET_OVERFLOW)))
158 return_trace (false);
159
160 if (likely (len))
Behdad Esfahbod59c45f62022-11-22 12:54:50 -0700161 hb_memcpy (start, blob->data, len);
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800162
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700163 /* 4-byte alignment. */
Behdad Esfahbod22acd422018-08-31 16:38:04 -0700164 c->align (4);
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800165 const char *end = (const char *) c->head;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800166
Garret Riegerdea0fe52021-08-04 16:36:20 -0700167 if (entry.first == HB_OT_TAG_head &&
Behdad Esfahbod6e1c3ea2019-01-28 14:12:41 -0500168 (unsigned) (end - start) >= head::static_size)
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800169 {
170 head *h = (head *) start;
171 checksum_adjustment = &h->checkSumAdjustment;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700172 *checksum_adjustment = 0;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800173 }
174
Behdad Esfahbodec2538c2018-02-23 15:51:26 -0800175 rec.checkSum.set_for_data (start, end - start);
Garret Riegerdea0fe52021-08-04 16:36:20 -0700176 i++;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600177 }
Behdad Esfahbod941bbd92018-02-07 21:49:01 -0600178
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600179 tables.qsort ();
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800180
181 if (checksum_adjustment)
182 {
183 CheckSum checksum;
184
185 /* The following line is a slower version of the following block. */
186 //checksum.set_for_data (this, (const char *) c->head - (const char *) this);
187 checksum.set_for_data (this, dir_end - (const char *) this);
Garret Riegerdea0fe52021-08-04 16:36:20 -0700188 for (unsigned int i = 0; i < num_items; i++)
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800189 {
Behdad Esfahbod63f57f42018-05-08 16:56:11 -0700190 TableRecord &rec = tables.arrayZ[i];
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700191 checksum = checksum + rec.checkSum;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800192 }
193
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700194 *checksum_adjustment = 0xB1B0AFBAu - checksum;
Behdad Esfahboddc5c7922018-02-23 16:42:06 -0800195 }
196
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600197 return_trace (true);
198 }
199
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330200 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300201 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500202 TRACE_SANITIZE (this);
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600203 return_trace (c->check_struct (this) && tables.sanitize (c));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400204 }
205
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400206 protected:
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400207 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
Behdad Esfahbodb0e33da2017-10-31 20:05:37 -0600208 BinSearchArrayOf<TableRecord>
209 tables;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400210 public:
Behdad Esfahbod22c53762010-12-14 23:51:29 -0500211 DEFINE_SIZE_ARRAY (12, tables);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400212} OpenTypeFontFace;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400213
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400214
215/*
216 * TrueType Collections
217 */
218
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400219struct TTCHeaderVersion1
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400220{
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400221 friend struct TTCHeader;
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400222
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330223 unsigned int get_face_count () const { return table.len; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330224 const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400225
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330226 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300227 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500228 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100229 return_trace (table.sanitize (c, this));
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400230 }
231
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400232 protected:
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400233 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900234 FixedVersion<>version; /* Version of the TTC Header (1.0),
Behdad Esfahbod76271002014-07-11 14:54:42 -0400235 * 0x00010000u */
Behdad Esfahbod2520a822021-03-31 15:34:26 -0600236 Array32Of<Offset32To<OpenTypeOffsetTable>>
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400237 table; /* Array of offsets to the OffsetTable for each font
238 * from the beginning of the file */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400239 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400240 DEFINE_SIZE_ARRAY (12, table);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400241};
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400242
243struct TTCHeader
244{
245 friend struct OpenTypeFontFile;
246
247 private:
248
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330249 unsigned int get_face_count () const
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400250 {
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400251 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400252 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400253 case 1: return u.version1.get_face_count ();
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400254 default:return 0;
255 }
256 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330257 const OpenTypeFontFace& get_face (unsigned int i) const
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400258 {
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400259 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400260 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400261 case 1: return u.version1.get_face (i);
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430262 default:return Null (OpenTypeFontFace);
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400263 }
264 }
265
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330266 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300267 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500268 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100269 if (unlikely (!u.header.version.sanitize (c))) return_trace (false);
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400270 switch (u.header.version.major) {
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400271 case 2: /* version 2 is compatible with version 1 */
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100272 case 1: return_trace (u.version1.sanitize (c));
273 default:return_trace (true);
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400274 }
275 }
276
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400277 protected:
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400278 union {
279 struct {
280 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
Behdad Esfahbod9a13ed42016-02-22 11:44:45 +0900281 FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0),
Behdad Esfahbod76271002014-07-11 14:54:42 -0400282 * 0x00010000u or 0x00020000u */
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400283 } header;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400284 TTCHeaderVersion1 version1;
Behdad Esfahbodae4190c2010-04-23 12:33:02 -0400285 } u;
286};
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400287
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430288/*
289 * Mac Resource Fork
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200290 *
291 * http://mirror.informatimago.com/next/developer.apple.com/documentation/mac/MoreToolbox/MoreToolbox-99.html
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430292 */
293
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200294struct ResourceRecord
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430295{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330296 const OpenTypeFontFace & get_face (const void *data_base) const
Behdad Esfahbod858b6272019-12-10 13:18:32 -0600297 { return * reinterpret_cast<const OpenTypeFontFace *> ((data_base+offset).arrayZ); }
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200298
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330299 bool sanitize (hb_sanitize_context_t *c,
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330300 const void *data_base) const
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430301 {
302 TRACE_SANITIZE (this);
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200303 return_trace (c->check_struct (this) &&
Behdad Esfahbod4479d3a2018-09-13 19:05:59 +0200304 offset.sanitize (c, data_base) &&
305 get_face (data_base).sanitize (c));
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430306 }
307
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200308 protected:
Behdad Esfahbod82f4d772018-09-13 18:27:20 +0200309 HBUINT16 id; /* Resource ID. */
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430310 HBINT16 nameOffset; /* Offset from beginning of resource name list
Behdad Esfahbod4134ec12018-09-11 17:56:03 +0200311 * to resource name, -1 means there is none. */
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200312 HBUINT8 attrs; /* Resource attributes */
Behdad Esfahbod2520a822021-03-31 15:34:26 -0600313 NNOffset24To<Array32Of<HBUINT8>>
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200314 offset; /* Offset from beginning of data block to
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430315 * data for this resource */
316 HBUINT32 reserved; /* Reserved for handle to resource */
317 public:
318 DEFINE_SIZE_STATIC (12);
319};
320
Behdad Esfahbod4479d3a2018-09-13 19:05:59 +0200321#define HB_TAG_sfnt HB_TAG ('s','f','n','t')
322
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200323struct ResourceTypeRecord
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430324{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330325 unsigned int get_resource_count () const
Behdad Esfahbod180a88a2018-09-13 19:19:57 +0200326 { return tag == HB_TAG_sfnt ? resCountM1 + 1 : 0; }
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430327
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330328 bool is_sfnt () const { return tag == HB_TAG_sfnt; }
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430329
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330330 const ResourceRecord& get_resource_record (unsigned int i,
331 const void *type_base) const
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330332 { return (type_base+resourcesZ).as_array (get_resource_count ())[i]; }
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200333
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330334 bool sanitize (hb_sanitize_context_t *c,
335 const void *type_base,
336 const void *data_base) const
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200337 {
338 TRACE_SANITIZE (this);
339 return_trace (c->check_struct (this) &&
340 resourcesZ.sanitize (c, type_base,
341 get_resource_count (),
342 data_base));
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430343 }
344
345 protected:
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200346 Tag tag; /* Resource type. */
347 HBUINT16 resCountM1; /* Number of resources minus 1. */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600348 NNOffset16To<UnsizedArrayOf<ResourceRecord>>
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200349 resourcesZ; /* Offset from beginning of resource type list
Behdad Esfahbodd5c50922018-09-11 17:18:21 +0200350 * to reference item list for this type. */
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430351 public:
352 DEFINE_SIZE_STATIC (8);
353};
354
355struct ResourceMap
356{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330357 unsigned int get_face_count () const
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200358 {
359 unsigned int count = get_type_count ();
360 for (unsigned int i = 0; i < count; i++)
361 {
362 const ResourceTypeRecord& type = get_type_record (i);
363 if (type.is_sfnt ())
364 return type.get_resource_count ();
365 }
366 return 0;
367 }
368
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330369 const OpenTypeFontFace& get_face (unsigned int idx,
370 const void *data_base) const
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200371 {
372 unsigned int count = get_type_count ();
373 for (unsigned int i = 0; i < count; i++)
374 {
375 const ResourceTypeRecord& type = get_type_record (i);
Behdad Esfahbod180a88a2018-09-13 19:19:57 +0200376 /* The check for idx < count is here because ResourceRecord is NOT null-safe.
377 * Because an offset of 0 there does NOT mean null. */
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200378 if (type.is_sfnt () && idx < type.get_resource_count ())
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200379 return type.get_resource_record (idx, &(this+typeList)).get_face (data_base);
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200380 }
381 return Null (OpenTypeFontFace);
382 }
383
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330384 bool sanitize (hb_sanitize_context_t *c, const void *data_base) const
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200385 {
386 TRACE_SANITIZE (this);
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200387 return_trace (c->check_struct (this) &&
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200388 typeList.sanitize (c, this,
Behdad Esfahbod67449c32018-09-14 10:58:00 +0200389 &(this+typeList),
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200390 data_base));
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430391 }
392
Behdad Esfahbod180a88a2018-09-13 19:19:57 +0200393 private:
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330394 unsigned int get_type_count () const { return (this+typeList).lenM1 + 1; }
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200395
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330396 const ResourceTypeRecord& get_type_record (unsigned int i) const
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200397 { return (this+typeList)[i]; }
Behdad Esfahbod180a88a2018-09-13 19:19:57 +0200398
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430399 protected:
Behdad Esfahbodbd75fd42018-09-11 18:12:26 +0200400 HBUINT8 reserved0[16]; /* Reserved for copy of resource header */
401 HBUINT32 reserved1; /* Reserved for handle to next resource map */
402 HBUINT16 resreved2; /* Reserved for file reference number */
403 HBUINT16 attrs; /* Resource fork attribute */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600404 NNOffset16To<ArrayOfM1<ResourceTypeRecord>>
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200405 typeList; /* Offset from beginning of map to
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430406 * resource type list */
Behdad Esfahbod6b5e4d02018-09-11 17:26:24 +0200407 Offset16 nameList; /* Offset from beginning of map to
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430408 * resource name list */
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430409 public:
Behdad Esfahbod3789c552018-09-13 20:30:04 +0200410 DEFINE_SIZE_STATIC (28);
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430411};
412
413struct ResourceForkHeader
414{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330415 unsigned int get_face_count () const
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200416 { return (this+map).get_face_count (); }
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430417
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330418 const OpenTypeFontFace& get_face (unsigned int idx,
419 unsigned int *base_offset = nullptr) const
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430420 {
Behdad Esfahbod0ab0f1e2018-09-13 19:13:01 +0200421 const OpenTypeFontFace &face = (this+map).get_face (idx, &(this+data));
422 if (base_offset)
423 *base_offset = (const char *) &face - (const char *) this;
424 return face;
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430425 }
426
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330427 bool sanitize (hb_sanitize_context_t *c) const
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430428 {
429 TRACE_SANITIZE (this);
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200430 return_trace (c->check_struct (this) &&
Behdad Esfahbodca746f22018-09-13 20:35:21 +0200431 data.sanitize (c, this, dataLen) &&
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200432 map.sanitize (c, this, &(this+data)));
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430433 }
434
435 protected:
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600436 NNOffset32To<UnsizedArrayOf<HBUINT8>>
Behdad Esfahboddbb764d2018-09-13 16:49:26 +0200437 data; /* Offset from beginning of resource fork
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430438 * to resource data */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600439 NNOffset32To<ResourceMap >
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430440 map; /* Offset from beginning of resource fork
441 * to resource map */
442 HBUINT32 dataLen; /* Length of resource data */
443 HBUINT32 mapLen; /* Length of resource map */
444 public:
445 DEFINE_SIZE_STATIC (16);
446};
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400447
448/*
449 * OpenType Font File
450 */
451
452struct OpenTypeFontFile
453{
Ebrahim Byagowi2a236062018-03-23 18:37:01 +0430454 enum {
455 CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */
Behdad Esfahboda1814e22018-09-11 14:45:23 +0200456 TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */
Ebrahim Byagowi2a236062018-03-23 18:37:01 +0430457 TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430458 DFontTag = HB_TAG ( 0 , 0 , 1 , 0 ), /* DFont Mac Resource Fork */
Ebrahim Byagowi2a236062018-03-23 18:37:01 +0430459 TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */
460 Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */
461 };
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400462
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330463 hb_tag_t get_tag () const { return u.tag; }
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400464
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330465 unsigned int get_face_count () const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400466 {
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400467 switch (u.tag) {
468 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400469 case TrueTag:
470 case Typ1Tag:
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400471 case TrueTypeTag: return 1;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400472 case TTCTag: return u.ttcHeader.get_face_count ();
Behdad Esfahbod9479ffe2018-09-11 16:41:26 +0200473 case DFontTag: return u.rfHeader.get_face_count ();
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400474 default: return 0;
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400475 }
476 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330477 const OpenTypeFontFace& get_face (unsigned int i, unsigned int *base_offset = nullptr) const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400478 {
Behdad Esfahbod9479ffe2018-09-11 16:41:26 +0200479 if (base_offset)
480 *base_offset = 0;
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400481 switch (u.tag) {
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400482 /* Note: for non-collection SFNT data we ignore index. This is because
483 * Apple dfont container is a container of SFNT's. So each SFNT is a
484 * non-TTC, but the index is more than zero. */
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400485 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400486 case TrueTag:
487 case Typ1Tag:
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400488 case TrueTypeTag: return u.fontFace;
489 case TTCTag: return u.ttcHeader.get_face (i);
Behdad Esfahbod9479ffe2018-09-11 16:41:26 +0200490 case DFontTag: return u.rfHeader.get_face (i, base_offset);
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430491 default: return Null (OpenTypeFontFace);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400492 }
493 }
494
Behdad Esfahboda110a472021-08-05 12:28:39 -0600495 template <typename Iterator,
Behdad Esfahbodf7a9c3e2021-08-05 13:05:07 -0600496 hb_requires ((hb_is_source_of<Iterator, hb_pair_t<hb_tag_t, hb_blob_t *>>::value))>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330497 bool serialize_single (hb_serialize_context_t *c,
498 hb_tag_t sfnt_tag,
Behdad Esfahbodb57ea3b2022-08-05 14:29:27 -0600499 Iterator items)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600500 {
501 TRACE_SERIALIZE (this);
502 assert (sfnt_tag != TTCTag);
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600503 if (unlikely (!c->extend_min (this))) return_trace (false);
Behdad Esfahbodf1e95e42018-12-18 16:49:08 -0500504 return_trace (u.fontFace.serialize (c, sfnt_tag, items));
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600505 }
506
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330507 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300508 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500509 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100510 if (unlikely (!u.tag.sanitize (c))) return_trace (false);
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400511 switch (u.tag) {
512 case CFFTag: /* All the non-collection tags */
Jeff Muizelaar4ce578e2010-05-03 15:03:53 -0400513 case TrueTag:
514 case Typ1Tag:
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100515 case TrueTypeTag: return_trace (u.fontFace.sanitize (c));
516 case TTCTag: return_trace (u.ttcHeader.sanitize (c));
Behdad Esfahbod9479ffe2018-09-11 16:41:26 +0200517 case DFontTag: return_trace (u.rfHeader.sanitize (c));
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100518 default: return_trace (true);
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400519 }
520 }
521
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400522 protected:
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400523 union {
524 Tag tag; /* 4-byte identifier. */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400525 OpenTypeFontFace fontFace;
526 TTCHeader ttcHeader;
Ebrahim Byagowi225b92b2018-07-01 14:32:00 +0430527 ResourceForkHeader rfHeader;
Behdad Esfahbod1aa46662010-04-23 13:32:03 -0400528 } u;
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400529 public:
530 DEFINE_SIZE_UNION (4, tag);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400531};
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400532
533
Behdad Esfahbod7d52e662012-11-16 18:49:54 -0800534} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400535
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400536
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700537#endif /* HB_OPEN_FILE_HH */