Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Google, Inc. |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 6 | * |
| 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 Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | 5f5b24f | 2009-08-02 20:03:12 -0400 | [diff] [blame] | 29 | #ifndef HB_OPEN_FILE_PRIVATE_HH |
| 30 | #define HB_OPEN_FILE_PRIVATE_HH |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | 7edb430 | 2009-08-04 22:06:57 -0400 | [diff] [blame] | 32 | #include "hb-open-type-private.hh" |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 33 | #include "hb-ot-head-table.hh" |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 34 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 35 | |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 36 | namespace OT { |
| 37 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * |
| 41 | * The OpenType Font File |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Organization of an OpenType Font |
| 48 | */ |
| 49 | |
| 50 | struct OpenTypeFontFile; |
| 51 | struct OffsetTable; |
| 52 | struct TTCHeader; |
| 53 | |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 54 | |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 55 | typedef struct TableRecord |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 56 | { |
Behdad Esfahbod | 6c04dcb | 2017-10-31 20:11:00 -0600 | [diff] [blame] | 57 | int cmp (Tag t) const |
Garret Rieger | 8e614ad | 2018-02-20 17:36:54 -0800 | [diff] [blame] | 58 | { return -t.cmp (tag); } |
Behdad Esfahbod | 6c04dcb | 2017-10-31 20:11:00 -0600 | [diff] [blame] | 59 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 60 | static int cmp (const void *pa, const void *pb) |
| 61 | { |
| 62 | const TableRecord *a = (const TableRecord *) pa; |
| 63 | const TableRecord *b = (const TableRecord *) pb; |
| 64 | return b->cmp (a->tag); |
| 65 | } |
| 66 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 67 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 68 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 69 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 70 | return_trace (c->check_struct (this)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 73 | Tag tag; /* 4-byte identifier. */ |
| 74 | CheckSum checkSum; /* CheckSum for this table. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 75 | Offset32 offset; /* Offset from beginning of TrueType font |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 76 | * file. */ |
Behdad Esfahbod | c9acab3 | 2018-02-07 17:12:55 -0600 | [diff] [blame] | 77 | HBUINT32 length; /* Length of this table. */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 78 | public: |
| 79 | DEFINE_SIZE_STATIC (16); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 80 | } OpenTypeTable; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 81 | |
| 82 | typedef struct OffsetTable |
| 83 | { |
| 84 | friend struct OpenTypeFontFile; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 85 | |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 86 | inline unsigned int get_table_count (void) const |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 87 | { return tables.len; } |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 88 | inline const TableRecord& get_table (unsigned int i) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 89 | { |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 90 | return tables[i]; |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 91 | } |
Behdad Esfahbod | c9acab3 | 2018-02-07 17:12:55 -0600 | [diff] [blame] | 92 | inline unsigned int get_table_tags (unsigned int start_offset, |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 93 | unsigned int *table_count, /* IN/OUT */ |
| 94 | hb_tag_t *table_tags /* OUT */) const |
| 95 | { |
| 96 | if (table_count) |
| 97 | { |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 98 | if (start_offset >= tables.len) |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 99 | *table_count = 0; |
| 100 | else |
ebraminio | 102f5ea | 2017-11-01 02:10:18 -0700 | [diff] [blame] | 101 | *table_count = MIN<unsigned int> (*table_count, tables.len - start_offset); |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 102 | |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 103 | const TableRecord *sub_tables = tables.array + start_offset; |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 104 | unsigned int count = *table_count; |
| 105 | for (unsigned int i = 0; i < count; i++) |
| 106 | table_tags[i] = sub_tables[i].tag; |
| 107 | } |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 108 | return tables.len; |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 109 | } |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 110 | inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const |
| 111 | { |
Behdad Esfahbod | ffd321a | 2010-04-21 00:14:12 -0400 | [diff] [blame] | 112 | Tag t; |
| 113 | t.set (tag); |
Behdad Esfahbod | 6c04dcb | 2017-10-31 20:11:00 -0600 | [diff] [blame] | 114 | /* Linear-search for small tables to work around fonts with unsorted |
| 115 | * table list. */ |
| 116 | int i = tables.len < 64 ? tables.lsearch (t) : tables.bsearch (t); |
| 117 | if (table_index) |
| 118 | *table_index = i == -1 ? Index::NOT_FOUND_INDEX : (unsigned int) i; |
| 119 | return i != -1; |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 120 | } |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 121 | inline const TableRecord& get_table_by_tag (hb_tag_t tag) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 122 | { |
| 123 | unsigned int table_index; |
| 124 | find_table_index (tag, &table_index); |
| 125 | return get_table (table_index); |
| 126 | } |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 127 | |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 128 | public: |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 129 | |
| 130 | inline bool serialize (hb_serialize_context_t *c, |
| 131 | hb_tag_t sfnt_tag, |
| 132 | Supplier<hb_tag_t> &tags, |
| 133 | Supplier<hb_blob_t *> &blobs, |
| 134 | unsigned int table_count) |
| 135 | { |
| 136 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 137 | /* Alloc 12 for the OTHeader. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 138 | if (unlikely (!c->extend_min (*this))) return_trace (false); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 139 | /* Write sfntVersion (bytes 0..3). */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 140 | sfnt_version.set (sfnt_tag); |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 141 | /* Take space for numTables, searchRange, entrySelector, RangeShift |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 142 | * and the TableRecords themselves. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 143 | if (unlikely (!tables.serialize (c, table_count))) return_trace (false); |
Behdad Esfahbod | 941bbd9 | 2018-02-07 21:49:01 -0600 | [diff] [blame] | 144 | |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 145 | const char *dir_end = (const char *) c->head; |
| 146 | HBUINT32 *checksum_adjustment = nullptr; |
| 147 | |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 148 | /* Write OffsetTables, alloc for and write actual table blobs. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 149 | for (unsigned int i = 0; i < table_count; i++) |
| 150 | { |
| 151 | TableRecord &rec = tables.array[i]; |
| 152 | hb_blob_t *blob = blobs[i]; |
Behdad Esfahbod | 941bbd9 | 2018-02-07 21:49:01 -0600 | [diff] [blame] | 153 | rec.tag.set (tags[i]); |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 154 | rec.length.set (hb_blob_get_length (blob)); |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 155 | rec.offset.serialize (c, this); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 156 | |
| 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;} |
| 160 | |
| 161 | memcpy (start, hb_blob_get_data (blob, nullptr), rec.length); |
| 162 | |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 163 | /* 4-byte allignment. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 164 | if (rec.length % 4) |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 165 | c->allocate_size<void> (4 - rec.length % 4); |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 166 | const char *end = (const char *) c->head; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 167 | |
| 168 | if (tags[i] == HB_OT_TAG_head && end - start >= head::static_size) |
| 169 | { |
| 170 | head *h = (head *) start; |
| 171 | checksum_adjustment = &h->checkSumAdjustment; |
| 172 | checksum_adjustment->set (0); |
| 173 | } |
| 174 | |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 175 | rec.checkSum.set_for_data (start, end - start); |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 176 | } |
Behdad Esfahbod | 71130a2 | 2018-02-10 13:15:57 -0600 | [diff] [blame] | 177 | tags += table_count; |
| 178 | blobs += table_count; |
Behdad Esfahbod | 941bbd9 | 2018-02-07 21:49:01 -0600 | [diff] [blame] | 179 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 180 | tables.qsort (); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 181 | |
| 182 | if (checksum_adjustment) |
| 183 | { |
| 184 | CheckSum checksum; |
| 185 | |
| 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++) |
| 190 | { |
| 191 | TableRecord &rec = tables.array[i]; |
| 192 | checksum.set (checksum + rec.checkSum); |
| 193 | } |
| 194 | |
| 195 | checksum_adjustment->set (0xB1B0AFBAu - checksum); |
| 196 | } |
| 197 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 198 | return_trace (true); |
| 199 | } |
| 200 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 201 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 202 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 203 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 204 | return_trace (c->check_struct (this) && tables.sanitize (c)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 207 | protected: |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 208 | Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */ |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 209 | BinSearchArrayOf<TableRecord> |
| 210 | tables; |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 211 | public: |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 212 | DEFINE_SIZE_ARRAY (12, tables); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 213 | } OpenTypeFontFace; |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 214 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * TrueType Collections |
| 218 | */ |
| 219 | |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 220 | struct TTCHeaderVersion1 |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 221 | { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 222 | friend struct TTCHeader; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 223 | |
Behdad Esfahbod | 20b035d | 2009-08-10 19:00:36 -0400 | [diff] [blame] | 224 | inline unsigned int get_face_count (void) const { return table.len; } |
Behdad Esfahbod | df3f505 | 2010-04-22 14:11:33 -0400 | [diff] [blame] | 225 | inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; } |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 226 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 227 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 228 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 229 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 230 | return_trace (table.sanitize (c, this)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 233 | protected: |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 234 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
Behdad Esfahbod | 9a13ed4 | 2016-02-22 11:44:45 +0900 | [diff] [blame] | 235 | FixedVersion<>version; /* Version of the TTC Header (1.0), |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 236 | * 0x00010000u */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 237 | ArrayOf<LOffsetTo<OffsetTable>, HBUINT32> |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 238 | table; /* Array of offsets to the OffsetTable for each font |
| 239 | * from the beginning of the file */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 240 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 241 | DEFINE_SIZE_ARRAY (12, table); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 242 | }; |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 243 | |
| 244 | struct TTCHeader |
| 245 | { |
| 246 | friend struct OpenTypeFontFile; |
| 247 | |
| 248 | private: |
| 249 | |
| 250 | inline unsigned int get_face_count (void) const |
| 251 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 252 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 253 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 254 | case 1: return u.version1.get_face_count (); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 255 | default:return 0; |
| 256 | } |
| 257 | } |
| 258 | inline const OpenTypeFontFace& get_face (unsigned int i) const |
| 259 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 260 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 261 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 262 | case 1: return u.version1.get_face (i); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 263 | default:return Null(OpenTypeFontFace); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 267 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 268 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 269 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 270 | if (unlikely (!u.header.version.sanitize (c))) return_trace (false); |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 271 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 272 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 273 | case 1: return_trace (u.version1.sanitize (c)); |
| 274 | default:return_trace (true); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 278 | protected: |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 279 | union { |
| 280 | struct { |
| 281 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
Behdad Esfahbod | 9a13ed4 | 2016-02-22 11:44:45 +0900 | [diff] [blame] | 282 | FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0), |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 283 | * 0x00010000u or 0x00020000u */ |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 284 | } header; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 285 | TTCHeaderVersion1 version1; |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 286 | } u; |
| 287 | }; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 288 | |
| 289 | |
| 290 | /* |
| 291 | * OpenType Font File |
| 292 | */ |
| 293 | |
| 294 | struct OpenTypeFontFile |
| 295 | { |
Behdad Esfahbod | 153beeb | 2014-12-12 19:46:09 -0800 | [diff] [blame] | 296 | static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */ |
| 297 | |
Ebrahim Byagowi | 2a23606 | 2018-03-23 18:37:01 +0430 | [diff] [blame] | 298 | enum { |
| 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 */ |
| 304 | }; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 305 | |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 306 | inline hb_tag_t get_tag (void) const { return u.tag; } |
| 307 | |
Behdad Esfahbod | 20b035d | 2009-08-10 19:00:36 -0400 | [diff] [blame] | 308 | inline unsigned int get_face_count (void) const |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 309 | { |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 310 | switch (u.tag) { |
| 311 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 312 | case TrueTag: |
| 313 | case Typ1Tag: |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 314 | case TrueTypeTag: return 1; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 315 | case TTCTag: return u.ttcHeader.get_face_count (); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 316 | default: return 0; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 317 | } |
| 318 | } |
Behdad Esfahbod | 20b035d | 2009-08-10 19:00:36 -0400 | [diff] [blame] | 319 | inline const OpenTypeFontFace& get_face (unsigned int i) const |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 320 | { |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 321 | switch (u.tag) { |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 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. */ |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 325 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 326 | case TrueTag: |
| 327 | case Typ1Tag: |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 328 | case TrueTypeTag: return u.fontFace; |
| 329 | case TTCTag: return u.ttcHeader.get_face (i); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 330 | default: return Null(OpenTypeFontFace); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 334 | inline bool serialize_single (hb_serialize_context_t *c, |
| 335 | hb_tag_t sfnt_tag, |
| 336 | Supplier<hb_tag_t> &tags, |
| 337 | Supplier<hb_blob_t *> &blobs, |
| 338 | unsigned int table_count) |
| 339 | { |
| 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)); |
| 344 | } |
| 345 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 346 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 347 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 348 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 349 | if (unlikely (!u.tag.sanitize (c))) return_trace (false); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 350 | switch (u.tag) { |
| 351 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 352 | case TrueTag: |
| 353 | case Typ1Tag: |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 354 | case TrueTypeTag: return_trace (u.fontFace.sanitize (c)); |
| 355 | case TTCTag: return_trace (u.ttcHeader.sanitize (c)); |
| 356 | default: return_trace (true); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 360 | protected: |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 361 | union { |
| 362 | Tag tag; /* 4-byte identifier. */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 363 | OpenTypeFontFace fontFace; |
| 364 | TTCHeader ttcHeader; |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 365 | } u; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 366 | public: |
| 367 | DEFINE_SIZE_UNION (4, tag); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 368 | }; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 369 | |
| 370 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 371 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 372 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 373 | |
Behdad Esfahbod | 5f5b24f | 2009-08-02 20:03:12 -0400 | [diff] [blame] | 374 | #endif /* HB_OPEN_FILE_PRIVATE_HH */ |