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