Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
michael@0 | 3 | * Copyright © 2012 Google, Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 6 | * |
michael@0 | 7 | * Permission is hereby granted, without written agreement and without |
michael@0 | 8 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 9 | * software and its documentation for any purpose, provided that the |
michael@0 | 10 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 11 | * all copies of this software. |
michael@0 | 12 | * |
michael@0 | 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 17 | * DAMAGE. |
michael@0 | 18 | * |
michael@0 | 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 24 | * |
michael@0 | 25 | * Red Hat Author(s): Behdad Esfahbod |
michael@0 | 26 | * Google Author(s): Behdad Esfahbod |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | #ifndef HB_OPEN_FILE_PRIVATE_HH |
michael@0 | 30 | #define HB_OPEN_FILE_PRIVATE_HH |
michael@0 | 31 | |
michael@0 | 32 | #include "hb-open-type-private.hh" |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | namespace OT { |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | * |
michael@0 | 40 | * The OpenType Font File |
michael@0 | 41 | * |
michael@0 | 42 | */ |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | /* |
michael@0 | 46 | * Organization of an OpenType Font |
michael@0 | 47 | */ |
michael@0 | 48 | |
michael@0 | 49 | struct OpenTypeFontFile; |
michael@0 | 50 | struct OffsetTable; |
michael@0 | 51 | struct TTCHeader; |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | typedef struct TableRecord |
michael@0 | 55 | { |
michael@0 | 56 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 57 | TRACE_SANITIZE (this); |
michael@0 | 58 | return TRACE_RETURN (c->check_struct (this)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | Tag tag; /* 4-byte identifier. */ |
michael@0 | 62 | CheckSum checkSum; /* CheckSum for this table. */ |
michael@0 | 63 | ULONG offset; /* Offset from beginning of TrueType font |
michael@0 | 64 | * file. */ |
michael@0 | 65 | ULONG length; /* Length of this table. */ |
michael@0 | 66 | public: |
michael@0 | 67 | DEFINE_SIZE_STATIC (16); |
michael@0 | 68 | } OpenTypeTable; |
michael@0 | 69 | |
michael@0 | 70 | typedef struct OffsetTable |
michael@0 | 71 | { |
michael@0 | 72 | friend struct OpenTypeFontFile; |
michael@0 | 73 | |
michael@0 | 74 | inline unsigned int get_table_count (void) const |
michael@0 | 75 | { return numTables; } |
michael@0 | 76 | inline const TableRecord& get_table (unsigned int i) const |
michael@0 | 77 | { |
michael@0 | 78 | if (unlikely (i >= numTables)) return Null(TableRecord); |
michael@0 | 79 | return tables[i]; |
michael@0 | 80 | } |
michael@0 | 81 | inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const |
michael@0 | 82 | { |
michael@0 | 83 | Tag t; |
michael@0 | 84 | t.set (tag); |
michael@0 | 85 | unsigned int count = numTables; |
michael@0 | 86 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 87 | { |
michael@0 | 88 | if (t == tables[i].tag) |
michael@0 | 89 | { |
michael@0 | 90 | if (table_index) *table_index = i; |
michael@0 | 91 | return true; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | if (table_index) *table_index = Index::NOT_FOUND_INDEX; |
michael@0 | 95 | return false; |
michael@0 | 96 | } |
michael@0 | 97 | inline const TableRecord& get_table_by_tag (hb_tag_t tag) const |
michael@0 | 98 | { |
michael@0 | 99 | unsigned int table_index; |
michael@0 | 100 | find_table_index (tag, &table_index); |
michael@0 | 101 | return get_table (table_index); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | public: |
michael@0 | 105 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 106 | TRACE_SANITIZE (this); |
michael@0 | 107 | return TRACE_RETURN (c->check_struct (this) && c->check_array (tables, TableRecord::static_size, numTables)); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | protected: |
michael@0 | 111 | Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */ |
michael@0 | 112 | USHORT numTables; /* Number of tables. */ |
michael@0 | 113 | USHORT searchRange; /* (Maximum power of 2 <= numTables) x 16 */ |
michael@0 | 114 | USHORT entrySelector; /* Log2(maximum power of 2 <= numTables). */ |
michael@0 | 115 | USHORT rangeShift; /* NumTables x 16-searchRange. */ |
michael@0 | 116 | TableRecord tables[VAR]; /* TableRecord entries. numTables items */ |
michael@0 | 117 | public: |
michael@0 | 118 | DEFINE_SIZE_ARRAY (12, tables); |
michael@0 | 119 | } OpenTypeFontFace; |
michael@0 | 120 | |
michael@0 | 121 | |
michael@0 | 122 | /* |
michael@0 | 123 | * TrueType Collections |
michael@0 | 124 | */ |
michael@0 | 125 | |
michael@0 | 126 | struct TTCHeaderVersion1 |
michael@0 | 127 | { |
michael@0 | 128 | friend struct TTCHeader; |
michael@0 | 129 | |
michael@0 | 130 | inline unsigned int get_face_count (void) const { return table.len; } |
michael@0 | 131 | inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; } |
michael@0 | 132 | |
michael@0 | 133 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 134 | TRACE_SANITIZE (this); |
michael@0 | 135 | return TRACE_RETURN (table.sanitize (c, this)); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | protected: |
michael@0 | 139 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
michael@0 | 140 | FixedVersion version; /* Version of the TTC Header (1.0), |
michael@0 | 141 | * 0x00010000 */ |
michael@0 | 142 | LongOffsetLongArrayOf<OffsetTable> |
michael@0 | 143 | table; /* Array of offsets to the OffsetTable for each font |
michael@0 | 144 | * from the beginning of the file */ |
michael@0 | 145 | public: |
michael@0 | 146 | DEFINE_SIZE_ARRAY (12, table); |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | struct TTCHeader |
michael@0 | 150 | { |
michael@0 | 151 | friend struct OpenTypeFontFile; |
michael@0 | 152 | |
michael@0 | 153 | private: |
michael@0 | 154 | |
michael@0 | 155 | inline unsigned int get_face_count (void) const |
michael@0 | 156 | { |
michael@0 | 157 | switch (u.header.version.major) { |
michael@0 | 158 | case 2: /* version 2 is compatible with version 1 */ |
michael@0 | 159 | case 1: return u.version1.get_face_count (); |
michael@0 | 160 | default:return 0; |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | inline const OpenTypeFontFace& get_face (unsigned int i) const |
michael@0 | 164 | { |
michael@0 | 165 | switch (u.header.version.major) { |
michael@0 | 166 | case 2: /* version 2 is compatible with version 1 */ |
michael@0 | 167 | case 1: return u.version1.get_face (i); |
michael@0 | 168 | default:return Null(OpenTypeFontFace); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 173 | TRACE_SANITIZE (this); |
michael@0 | 174 | if (unlikely (!u.header.version.sanitize (c))) return TRACE_RETURN (false); |
michael@0 | 175 | switch (u.header.version.major) { |
michael@0 | 176 | case 2: /* version 2 is compatible with version 1 */ |
michael@0 | 177 | case 1: return TRACE_RETURN (u.version1.sanitize (c)); |
michael@0 | 178 | default:return TRACE_RETURN (true); |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | protected: |
michael@0 | 183 | union { |
michael@0 | 184 | struct { |
michael@0 | 185 | Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ |
michael@0 | 186 | FixedVersion version; /* Version of the TTC Header (1.0 or 2.0), |
michael@0 | 187 | * 0x00010000 or 0x00020000 */ |
michael@0 | 188 | } header; |
michael@0 | 189 | TTCHeaderVersion1 version1; |
michael@0 | 190 | } u; |
michael@0 | 191 | }; |
michael@0 | 192 | |
michael@0 | 193 | |
michael@0 | 194 | /* |
michael@0 | 195 | * OpenType Font File |
michael@0 | 196 | */ |
michael@0 | 197 | |
michael@0 | 198 | struct OpenTypeFontFile |
michael@0 | 199 | { |
michael@0 | 200 | static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */ |
michael@0 | 201 | static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */ |
michael@0 | 202 | static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f'); /* TrueType Collection */ |
michael@0 | 203 | static const hb_tag_t TrueTag = HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */ |
michael@0 | 204 | static const hb_tag_t Typ1Tag = HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */ |
michael@0 | 205 | |
michael@0 | 206 | inline hb_tag_t get_tag (void) const { return u.tag; } |
michael@0 | 207 | |
michael@0 | 208 | inline unsigned int get_face_count (void) const |
michael@0 | 209 | { |
michael@0 | 210 | switch (u.tag) { |
michael@0 | 211 | case CFFTag: /* All the non-collection tags */ |
michael@0 | 212 | case TrueTag: |
michael@0 | 213 | case Typ1Tag: |
michael@0 | 214 | case TrueTypeTag: return 1; |
michael@0 | 215 | case TTCTag: return u.ttcHeader.get_face_count (); |
michael@0 | 216 | default: return 0; |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | inline const OpenTypeFontFace& get_face (unsigned int i) const |
michael@0 | 220 | { |
michael@0 | 221 | switch (u.tag) { |
michael@0 | 222 | /* Note: for non-collection SFNT data we ignore index. This is because |
michael@0 | 223 | * Apple dfont container is a container of SFNT's. So each SFNT is a |
michael@0 | 224 | * non-TTC, but the index is more than zero. */ |
michael@0 | 225 | case CFFTag: /* All the non-collection tags */ |
michael@0 | 226 | case TrueTag: |
michael@0 | 227 | case Typ1Tag: |
michael@0 | 228 | case TrueTypeTag: return u.fontFace; |
michael@0 | 229 | case TTCTag: return u.ttcHeader.get_face (i); |
michael@0 | 230 | default: return Null(OpenTypeFontFace); |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 235 | TRACE_SANITIZE (this); |
michael@0 | 236 | if (unlikely (!u.tag.sanitize (c))) return TRACE_RETURN (false); |
michael@0 | 237 | switch (u.tag) { |
michael@0 | 238 | case CFFTag: /* All the non-collection tags */ |
michael@0 | 239 | case TrueTag: |
michael@0 | 240 | case Typ1Tag: |
michael@0 | 241 | case TrueTypeTag: return TRACE_RETURN (u.fontFace.sanitize (c)); |
michael@0 | 242 | case TTCTag: return TRACE_RETURN (u.ttcHeader.sanitize (c)); |
michael@0 | 243 | default: return TRACE_RETURN (true); |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | protected: |
michael@0 | 248 | union { |
michael@0 | 249 | Tag tag; /* 4-byte identifier. */ |
michael@0 | 250 | OpenTypeFontFace fontFace; |
michael@0 | 251 | TTCHeader ttcHeader; |
michael@0 | 252 | } u; |
michael@0 | 253 | public: |
michael@0 | 254 | DEFINE_SIZE_UNION (4, tag); |
michael@0 | 255 | }; |
michael@0 | 256 | |
michael@0 | 257 | |
michael@0 | 258 | } /* namespace OT */ |
michael@0 | 259 | |
michael@0 | 260 | |
michael@0 | 261 | #endif /* HB_OPEN_FILE_PRIVATE_HH */ |