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 | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #ifndef HB_OPEN_FILE_HH |
| 30 | #define HB_OPEN_FILE_HH |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 32 | #include "hb-open-type.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 | * The OpenType Font File |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | /* |
| 46 | * Organization of an OpenType Font |
| 47 | */ |
| 48 | |
| 49 | struct OpenTypeFontFile; |
Behdad Esfahbod | 505b3fc | 2021-02-17 11:34:47 -0700 | [diff] [blame] | 50 | struct OpenTypeOffsetTable; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 51 | struct TTCHeader; |
| 52 | |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 53 | |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 54 | typedef struct TableRecord |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 55 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 56 | int cmp (Tag t) const { return -t.cmp (tag); } |
Behdad Esfahbod | 6c04dcb | 2017-10-31 20:11:00 -0600 | [diff] [blame] | 57 | |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 58 | HB_INTERNAL static int cmp (const void *pa, const void *pb) |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 59 | { |
| 60 | const TableRecord *a = (const TableRecord *) pa; |
| 61 | const TableRecord *b = (const TableRecord *) pb; |
| 62 | return b->cmp (a->tag); |
| 63 | } |
| 64 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 65 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 66 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 67 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 68 | return_trace (c->check_struct (this)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 71 | Tag tag; /* 4-byte identifier. */ |
| 72 | CheckSum checkSum; /* CheckSum for this table. */ |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 73 | Offset32 offset; /* Offset from beginning of TrueType font |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 74 | * file. */ |
Behdad Esfahbod | c9acab3 | 2018-02-07 17:12:55 -0600 | [diff] [blame] | 75 | HBUINT32 length; /* Length of this table. */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 76 | public: |
| 77 | DEFINE_SIZE_STATIC (16); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 78 | } OpenTypeTable; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 79 | |
Behdad Esfahbod | 505b3fc | 2021-02-17 11:34:47 -0700 | [diff] [blame] | 80 | typedef struct OpenTypeOffsetTable |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 81 | { |
| 82 | friend struct OpenTypeFontFile; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 83 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 84 | unsigned int get_table_count () const { return tables.len; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 85 | const TableRecord& get_table (unsigned int i) const |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 86 | { return tables[i]; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 87 | unsigned int get_table_tags (unsigned int start_offset, |
Behdad Esfahbod | 07776b6 | 2019-04-15 16:43:34 -0400 | [diff] [blame] | 88 | unsigned int *table_count, /* IN/OUT */ |
| 89 | hb_tag_t *table_tags /* OUT */) const |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 90 | { |
| 91 | if (table_count) |
| 92 | { |
Behdad Esfahbod | a0bde1e | 2022-11-16 21:27:12 -0700 | [diff] [blame] | 93 | + tables.as_array ().sub_array (start_offset, table_count) |
Ebrahim Byagowi | 25cf687 | 2020-06-20 01:36:50 +0430 | [diff] [blame] | 94 | | hb_map (&TableRecord::tag) |
| 95 | | hb_sink (hb_array (table_tags, *table_count)) |
| 96 | ; |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 97 | } |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 98 | return tables.len; |
Behdad Esfahbod | 94b3caf | 2017-10-11 17:22:44 +0200 | [diff] [blame] | 99 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 100 | bool find_table_index (hb_tag_t tag, unsigned int *table_index) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 101 | { |
Behdad Esfahbod | ffd321a | 2010-04-21 00:14:12 -0400 | [diff] [blame] | 102 | Tag t; |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 103 | t = tag; |
Behdad Esfahbod | ba6db26 | 2021-07-22 11:37:39 -0700 | [diff] [blame] | 104 | /* 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 Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 111 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 112 | const TableRecord& get_table_by_tag (hb_tag_t tag) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 113 | { |
| 114 | unsigned int table_index; |
| 115 | find_table_index (tag, &table_index); |
| 116 | return get_table (table_index); |
| 117 | } |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 118 | |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 119 | public: |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 120 | |
Behdad Esfahbod | a110a47 | 2021-08-05 12:28:39 -0600 | [diff] [blame] | 121 | template <typename Iterator, |
Behdad Esfahbod | f7a9c3e | 2021-08-05 13:05:07 -0600 | [diff] [blame] | 122 | hb_requires ((hb_is_source_of<Iterator, hb_pair_t<hb_tag_t, hb_blob_t *>>::value))> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 123 | bool serialize (hb_serialize_context_t *c, |
| 124 | hb_tag_t sfnt_tag, |
Behdad Esfahbod | b57ea3b | 2022-08-05 14:29:27 -0600 | [diff] [blame] | 125 | Iterator it) |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 126 | { |
| 127 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 128 | /* Alloc 12 for the OTHeader. */ |
Behdad Esfahbod | f0a1892 | 2021-07-28 17:36:22 -0600 | [diff] [blame] | 129 | if (unlikely (!c->extend_min (this))) return_trace (false); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 130 | /* Write sfntVersion (bytes 0..3). */ |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 131 | sfnt_version = sfnt_tag; |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 132 | /* Take space for numTables, searchRange, entrySelector, RangeShift |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 133 | * and the TableRecords themselves. */ |
Behdad Esfahbod | 2c35963 | 2023-06-26 18:28:11 -0600 | [diff] [blame] | 134 | unsigned num_items = hb_len (it); |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 135 | if (unlikely (!tables.serialize (c, num_items))) return_trace (false); |
Behdad Esfahbod | 941bbd9 | 2018-02-07 21:49:01 -0600 | [diff] [blame] | 136 | |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 137 | const char *dir_end = (const char *) c->head; |
| 138 | HBUINT32 *checksum_adjustment = nullptr; |
| 139 | |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 140 | /* Write OffsetTables, alloc for and write actual table blobs. */ |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 141 | unsigned i = 0; |
| 142 | for (hb_pair_t<hb_tag_t, hb_blob_t*> entry : it) |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 143 | { |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 144 | hb_blob_t *blob = entry.second; |
Behdad Esfahbod | 29c9833 | 2021-07-09 11:58:36 -0600 | [diff] [blame] | 145 | unsigned len = blob->length; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 146 | |
| 147 | /* Allocate room for the table and copy it. */ |
Behdad Esfahbod | 2c35963 | 2023-06-26 18:28:11 -0600 | [diff] [blame] | 148 | char *start = (char *) c->allocate_size<void> (len, false); |
Ebrahim Byagowi | a6065d0 | 2019-07-10 16:41:40 +0430 | [diff] [blame] | 149 | if (unlikely (!start)) return false; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 150 | |
Behdad Esfahbod | 29c9833 | 2021-07-09 11:58:36 -0600 | [diff] [blame] | 151 | TableRecord &rec = tables.arrayZ[i]; |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 152 | rec.tag = entry.first; |
Behdad Esfahbod | 29c9833 | 2021-07-09 11:58:36 -0600 | [diff] [blame] | 153 | 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 Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 161 | hb_memcpy (start, blob->data, len); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 162 | |
Bruce Mitchener | 257d0e5 | 2018-10-19 22:49:21 +0700 | [diff] [blame] | 163 | /* 4-byte alignment. */ |
Behdad Esfahbod | 22acd42 | 2018-08-31 16:38:04 -0700 | [diff] [blame] | 164 | c->align (4); |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 165 | const char *end = (const char *) c->head; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 166 | |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 167 | if (entry.first == HB_OT_TAG_head && |
Behdad Esfahbod | 6e1c3ea | 2019-01-28 14:12:41 -0500 | [diff] [blame] | 168 | (unsigned) (end - start) >= head::static_size) |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 169 | { |
| 170 | head *h = (head *) start; |
| 171 | checksum_adjustment = &h->checkSumAdjustment; |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 172 | *checksum_adjustment = 0; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Behdad Esfahbod | ec2538c | 2018-02-23 15:51:26 -0800 | [diff] [blame] | 175 | rec.checkSum.set_for_data (start, end - start); |
Garret Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 176 | i++; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 177 | } |
Behdad Esfahbod | 941bbd9 | 2018-02-07 21:49:01 -0600 | [diff] [blame] | 178 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 179 | tables.qsort (); |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 180 | |
| 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 Rieger | dea0fe5 | 2021-08-04 16:36:20 -0700 | [diff] [blame] | 188 | for (unsigned int i = 0; i < num_items; i++) |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 189 | { |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 190 | TableRecord &rec = tables.arrayZ[i]; |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 191 | checksum = checksum + rec.checkSum; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 194 | *checksum_adjustment = 0xB1B0AFBAu - checksum; |
Behdad Esfahbod | dc5c792 | 2018-02-23 16:42:06 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 197 | return_trace (true); |
| 198 | } |
| 199 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 200 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 201 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 202 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 203 | return_trace (c->check_struct (this) && tables.sanitize (c)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 206 | protected: |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 207 | Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */ |
Behdad Esfahbod | b0e33da | 2017-10-31 20:05:37 -0600 | [diff] [blame] | 208 | BinSearchArrayOf<TableRecord> |
| 209 | tables; |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 210 | public: |
Behdad Esfahbod | 22c5376 | 2010-12-14 23:51:29 -0500 | [diff] [blame] | 211 | DEFINE_SIZE_ARRAY (12, tables); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 212 | } OpenTypeFontFace; |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 213 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * TrueType Collections |
| 217 | */ |
| 218 | |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 219 | struct TTCHeaderVersion1 |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 220 | { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 221 | friend struct TTCHeader; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 222 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 223 | unsigned int get_face_count () const { return table.len; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 224 | const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; } |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 225 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 226 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 227 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 228 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 229 | return_trace (table.sanitize (c, this)); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 232 | protected: |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 233 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
Behdad Esfahbod | 9a13ed4 | 2016-02-22 11:44:45 +0900 | [diff] [blame] | 234 | FixedVersion<>version; /* Version of the TTC Header (1.0), |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 235 | * 0x00010000u */ |
Behdad Esfahbod | 2520a82 | 2021-03-31 15:34:26 -0600 | [diff] [blame] | 236 | Array32Of<Offset32To<OpenTypeOffsetTable>> |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 237 | table; /* Array of offsets to the OffsetTable for each font |
| 238 | * from the beginning of the file */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 239 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 240 | DEFINE_SIZE_ARRAY (12, table); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 241 | }; |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 242 | |
| 243 | struct TTCHeader |
| 244 | { |
| 245 | friend struct OpenTypeFontFile; |
| 246 | |
| 247 | private: |
| 248 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 249 | unsigned int get_face_count () const |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 250 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 251 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 252 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 253 | case 1: return u.version1.get_face_count (); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 254 | default:return 0; |
| 255 | } |
| 256 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 257 | const OpenTypeFontFace& get_face (unsigned int i) const |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 258 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 259 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 260 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 261 | case 1: return u.version1.get_face (i); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 262 | default:return Null (OpenTypeFontFace); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 266 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 267 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 268 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 269 | if (unlikely (!u.header.version.sanitize (c))) return_trace (false); |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 270 | switch (u.header.version.major) { |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 271 | case 2: /* version 2 is compatible with version 1 */ |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 272 | case 1: return_trace (u.version1.sanitize (c)); |
| 273 | default:return_trace (true); |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 277 | protected: |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 278 | union { |
| 279 | struct { |
| 280 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
Behdad Esfahbod | 9a13ed4 | 2016-02-22 11:44:45 +0900 | [diff] [blame] | 281 | FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0), |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 282 | * 0x00010000u or 0x00020000u */ |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 283 | } header; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 284 | TTCHeaderVersion1 version1; |
Behdad Esfahbod | ae4190c | 2010-04-23 12:33:02 -0400 | [diff] [blame] | 285 | } u; |
| 286 | }; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 287 | |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 288 | /* |
| 289 | * Mac Resource Fork |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 290 | * |
| 291 | * http://mirror.informatimago.com/next/developer.apple.com/documentation/mac/MoreToolbox/MoreToolbox-99.html |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 292 | */ |
| 293 | |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 294 | struct ResourceRecord |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 295 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 296 | const OpenTypeFontFace & get_face (const void *data_base) const |
Behdad Esfahbod | 858b627 | 2019-12-10 13:18:32 -0600 | [diff] [blame] | 297 | { return * reinterpret_cast<const OpenTypeFontFace *> ((data_base+offset).arrayZ); } |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 298 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 299 | bool sanitize (hb_sanitize_context_t *c, |
Ebrahim Byagowi | 0558413 | 2019-10-01 13:49:55 +0330 | [diff] [blame] | 300 | const void *data_base) const |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 301 | { |
| 302 | TRACE_SANITIZE (this); |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 303 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | 4479d3a | 2018-09-13 19:05:59 +0200 | [diff] [blame] | 304 | offset.sanitize (c, data_base) && |
| 305 | get_face (data_base).sanitize (c)); |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 306 | } |
| 307 | |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 308 | protected: |
Behdad Esfahbod | 82f4d77 | 2018-09-13 18:27:20 +0200 | [diff] [blame] | 309 | HBUINT16 id; /* Resource ID. */ |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 310 | HBINT16 nameOffset; /* Offset from beginning of resource name list |
Behdad Esfahbod | 4134ec1 | 2018-09-11 17:56:03 +0200 | [diff] [blame] | 311 | * to resource name, -1 means there is none. */ |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 312 | HBUINT8 attrs; /* Resource attributes */ |
Behdad Esfahbod | 2520a82 | 2021-03-31 15:34:26 -0600 | [diff] [blame] | 313 | NNOffset24To<Array32Of<HBUINT8>> |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 314 | offset; /* Offset from beginning of data block to |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 315 | * data for this resource */ |
| 316 | HBUINT32 reserved; /* Reserved for handle to resource */ |
| 317 | public: |
| 318 | DEFINE_SIZE_STATIC (12); |
| 319 | }; |
| 320 | |
Behdad Esfahbod | 4479d3a | 2018-09-13 19:05:59 +0200 | [diff] [blame] | 321 | #define HB_TAG_sfnt HB_TAG ('s','f','n','t') |
| 322 | |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 323 | struct ResourceTypeRecord |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 324 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 325 | unsigned int get_resource_count () const |
Behdad Esfahbod | 180a88a | 2018-09-13 19:19:57 +0200 | [diff] [blame] | 326 | { return tag == HB_TAG_sfnt ? resCountM1 + 1 : 0; } |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 327 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 328 | bool is_sfnt () const { return tag == HB_TAG_sfnt; } |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 329 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 330 | const ResourceRecord& get_resource_record (unsigned int i, |
| 331 | const void *type_base) const |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 332 | { return (type_base+resourcesZ).as_array (get_resource_count ())[i]; } |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 333 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 334 | bool sanitize (hb_sanitize_context_t *c, |
| 335 | const void *type_base, |
| 336 | const void *data_base) const |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 337 | { |
| 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 Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | protected: |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 346 | Tag tag; /* Resource type. */ |
| 347 | HBUINT16 resCountM1; /* Number of resources minus 1. */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 348 | NNOffset16To<UnsizedArrayOf<ResourceRecord>> |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 349 | resourcesZ; /* Offset from beginning of resource type list |
Behdad Esfahbod | d5c5092 | 2018-09-11 17:18:21 +0200 | [diff] [blame] | 350 | * to reference item list for this type. */ |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 351 | public: |
| 352 | DEFINE_SIZE_STATIC (8); |
| 353 | }; |
| 354 | |
| 355 | struct ResourceMap |
| 356 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 357 | unsigned int get_face_count () const |
Behdad Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 358 | { |
| 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 Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 369 | const OpenTypeFontFace& get_face (unsigned int idx, |
| 370 | const void *data_base) const |
Behdad Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 371 | { |
| 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 Esfahbod | 180a88a | 2018-09-13 19:19:57 +0200 | [diff] [blame] | 376 | /* 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 Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 378 | if (type.is_sfnt () && idx < type.get_resource_count ()) |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 379 | return type.get_resource_record (idx, &(this+typeList)).get_face (data_base); |
Behdad Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 380 | } |
| 381 | return Null (OpenTypeFontFace); |
| 382 | } |
| 383 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 384 | bool sanitize (hb_sanitize_context_t *c, const void *data_base) const |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 385 | { |
| 386 | TRACE_SANITIZE (this); |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 387 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 388 | typeList.sanitize (c, this, |
Behdad Esfahbod | 67449c3 | 2018-09-14 10:58:00 +0200 | [diff] [blame] | 389 | &(this+typeList), |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 390 | data_base)); |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 391 | } |
| 392 | |
Behdad Esfahbod | 180a88a | 2018-09-13 19:19:57 +0200 | [diff] [blame] | 393 | private: |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 394 | unsigned int get_type_count () const { return (this+typeList).lenM1 + 1; } |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 395 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 396 | const ResourceTypeRecord& get_type_record (unsigned int i) const |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 397 | { return (this+typeList)[i]; } |
Behdad Esfahbod | 180a88a | 2018-09-13 19:19:57 +0200 | [diff] [blame] | 398 | |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 399 | protected: |
Behdad Esfahbod | bd75fd4 | 2018-09-11 18:12:26 +0200 | [diff] [blame] | 400 | 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 Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 404 | NNOffset16To<ArrayOfM1<ResourceTypeRecord>> |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 405 | typeList; /* Offset from beginning of map to |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 406 | * resource type list */ |
Behdad Esfahbod | 6b5e4d0 | 2018-09-11 17:26:24 +0200 | [diff] [blame] | 407 | Offset16 nameList; /* Offset from beginning of map to |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 408 | * resource name list */ |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 409 | public: |
Behdad Esfahbod | 3789c55 | 2018-09-13 20:30:04 +0200 | [diff] [blame] | 410 | DEFINE_SIZE_STATIC (28); |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | struct ResourceForkHeader |
| 414 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 415 | unsigned int get_face_count () const |
Behdad Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 416 | { return (this+map).get_face_count (); } |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 417 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 418 | const OpenTypeFontFace& get_face (unsigned int idx, |
| 419 | unsigned int *base_offset = nullptr) const |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 420 | { |
Behdad Esfahbod | 0ab0f1e | 2018-09-13 19:13:01 +0200 | [diff] [blame] | 421 | 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 Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 425 | } |
| 426 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 427 | bool sanitize (hb_sanitize_context_t *c) const |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 428 | { |
| 429 | TRACE_SANITIZE (this); |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 430 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | ca746f2 | 2018-09-13 20:35:21 +0200 | [diff] [blame] | 431 | data.sanitize (c, this, dataLen) && |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 432 | map.sanitize (c, this, &(this+data))); |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | protected: |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 436 | NNOffset32To<UnsizedArrayOf<HBUINT8>> |
Behdad Esfahbod | dbb764d | 2018-09-13 16:49:26 +0200 | [diff] [blame] | 437 | data; /* Offset from beginning of resource fork |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 438 | * to resource data */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 439 | NNOffset32To<ResourceMap > |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 440 | 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 Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * OpenType Font File |
| 450 | */ |
| 451 | |
| 452 | struct OpenTypeFontFile |
| 453 | { |
Ebrahim Byagowi | 2a23606 | 2018-03-23 18:37:01 +0430 | [diff] [blame] | 454 | enum { |
| 455 | CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */ |
Behdad Esfahbod | a1814e2 | 2018-09-11 14:45:23 +0200 | [diff] [blame] | 456 | TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */ |
Ebrahim Byagowi | 2a23606 | 2018-03-23 18:37:01 +0430 | [diff] [blame] | 457 | TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */ |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 458 | DFontTag = HB_TAG ( 0 , 0 , 1 , 0 ), /* DFont Mac Resource Fork */ |
Ebrahim Byagowi | 2a23606 | 2018-03-23 18:37:01 +0430 | [diff] [blame] | 459 | 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 Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 462 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 463 | hb_tag_t get_tag () const { return u.tag; } |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 464 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 465 | unsigned int get_face_count () const |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 466 | { |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 467 | switch (u.tag) { |
| 468 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 469 | case TrueTag: |
| 470 | case Typ1Tag: |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 471 | case TrueTypeTag: return 1; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 472 | case TTCTag: return u.ttcHeader.get_face_count (); |
Behdad Esfahbod | 9479ffe | 2018-09-11 16:41:26 +0200 | [diff] [blame] | 473 | case DFontTag: return u.rfHeader.get_face_count (); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 474 | default: return 0; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 475 | } |
| 476 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 477 | const OpenTypeFontFace& get_face (unsigned int i, unsigned int *base_offset = nullptr) const |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 478 | { |
Behdad Esfahbod | 9479ffe | 2018-09-11 16:41:26 +0200 | [diff] [blame] | 479 | if (base_offset) |
| 480 | *base_offset = 0; |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 481 | switch (u.tag) { |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 482 | /* 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 Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 485 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 486 | case TrueTag: |
| 487 | case Typ1Tag: |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 488 | case TrueTypeTag: return u.fontFace; |
| 489 | case TTCTag: return u.ttcHeader.get_face (i); |
Behdad Esfahbod | 9479ffe | 2018-09-11 16:41:26 +0200 | [diff] [blame] | 490 | case DFontTag: return u.rfHeader.get_face (i, base_offset); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 491 | default: return Null (OpenTypeFontFace); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Behdad Esfahbod | a110a47 | 2021-08-05 12:28:39 -0600 | [diff] [blame] | 495 | template <typename Iterator, |
Behdad Esfahbod | f7a9c3e | 2021-08-05 13:05:07 -0600 | [diff] [blame] | 496 | hb_requires ((hb_is_source_of<Iterator, hb_pair_t<hb_tag_t, hb_blob_t *>>::value))> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 497 | bool serialize_single (hb_serialize_context_t *c, |
| 498 | hb_tag_t sfnt_tag, |
Behdad Esfahbod | b57ea3b | 2022-08-05 14:29:27 -0600 | [diff] [blame] | 499 | Iterator items) |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 500 | { |
| 501 | TRACE_SERIALIZE (this); |
| 502 | assert (sfnt_tag != TTCTag); |
Behdad Esfahbod | f0a1892 | 2021-07-28 17:36:22 -0600 | [diff] [blame] | 503 | if (unlikely (!c->extend_min (this))) return_trace (false); |
Behdad Esfahbod | f1e95e4 | 2018-12-18 16:49:08 -0500 | [diff] [blame] | 504 | return_trace (u.fontFace.serialize (c, sfnt_tag, items)); |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 505 | } |
| 506 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 507 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 508 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 509 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 510 | if (unlikely (!u.tag.sanitize (c))) return_trace (false); |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 511 | switch (u.tag) { |
| 512 | case CFFTag: /* All the non-collection tags */ |
Jeff Muizelaar | 4ce578e | 2010-05-03 15:03:53 -0400 | [diff] [blame] | 513 | case TrueTag: |
| 514 | case Typ1Tag: |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 515 | case TrueTypeTag: return_trace (u.fontFace.sanitize (c)); |
| 516 | case TTCTag: return_trace (u.ttcHeader.sanitize (c)); |
Behdad Esfahbod | 9479ffe | 2018-09-11 16:41:26 +0200 | [diff] [blame] | 517 | case DFontTag: return_trace (u.rfHeader.sanitize (c)); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 518 | default: return_trace (true); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 522 | protected: |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 523 | union { |
| 524 | Tag tag; /* 4-byte identifier. */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 525 | OpenTypeFontFace fontFace; |
| 526 | TTCHeader ttcHeader; |
Ebrahim Byagowi | 225b92b | 2018-07-01 14:32:00 +0430 | [diff] [blame] | 527 | ResourceForkHeader rfHeader; |
Behdad Esfahbod | 1aa4666 | 2010-04-23 13:32:03 -0400 | [diff] [blame] | 528 | } u; |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 529 | public: |
| 530 | DEFINE_SIZE_UNION (4, tag); |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 531 | }; |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 532 | |
| 533 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 534 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 535 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 536 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 537 | #endif /* HB_OPEN_FILE_HH */ |