2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine library.
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.
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
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.
24 * Red Hat Author(s): Behdad Esfahbod
27 #ifndef HB_OPEN_FILE_PRIVATE_HH
28 #define HB_OPEN_FILE_PRIVATE_HH
30 #include "hb-open-type-private.hh"
35 * The OpenType Font File
41 * Organization of an OpenType Font
44 struct OpenTypeFontFile;
48 typedef struct TableDirectory
50 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
52 return SANITIZE_SELF () && SANITIZE (tag) &&
53 SANITIZE_MEM (CONST_CHARP(base) + (unsigned long) offset, length);
56 Tag tag; /* 4-byte identifier. */
57 CheckSum checkSum; /* CheckSum for this table. */
58 ULONG offset; /* Offset from beginning of TrueType font
60 ULONG length; /* Length of this table. */
62 ASSERT_SIZE (TableDirectory, 16);
64 typedef struct OffsetTable
66 friend struct OpenTypeFontFile;
67 friend struct TTCHeader;
69 STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
71 inline unsigned int get_table_count (void) const
73 inline const Tag& get_table_tag (unsigned int i) const
75 if (HB_UNLIKELY (i >= numTables)) return Null(Tag);
76 return tableDir[i].tag;
78 inline const TableDirectory& get_table (unsigned int i) const
80 if (HB_UNLIKELY (i >= numTables)) return Null(TableDirectory);
83 inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
87 unsigned int count = numTables;
88 for (unsigned int i = 0; i < count; i++)
90 if (t == tableDir[i].tag)
92 if (table_index) *table_index = i;
96 if (table_index) *table_index = NO_INDEX;
99 inline const TableDirectory& get_table_by_tag (hb_tag_t tag) const
101 unsigned int table_index;
102 find_table_index (tag, &table_index);
103 return get_table (table_index);
106 inline unsigned int get_face_count (void) const { return 1; }
109 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
111 if (!(SANITIZE_SELF () && SANITIZE_MEM (tableDir, sizeof (tableDir[0]) * numTables))) return false;
112 unsigned int count = numTables;
113 for (unsigned int i = 0; i < count; i++)
114 if (!SANITIZE_BASE (tableDir[i], base))
120 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
121 USHORT numTables; /* Number of tables. */
122 USHORT searchRange; /* (Maximum power of 2 <= numTables) x 16 */
123 USHORT entrySelector; /* Log2(maximum power of 2 <= numTables). */
124 USHORT rangeShift; /* NumTables x 16-searchRange. */
125 TableDirectory tableDir[VAR]; /* TableDirectory entries. numTables items */
127 ASSERT_SIZE_VAR (OffsetTable, 12, TableDirectory);
130 * TrueType Collections
135 friend struct OpenTypeFontFile;
137 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
139 inline unsigned int get_face_count (void) const { return table.len; }
141 inline const OpenTypeFontFace& get_face (unsigned int i) const
143 return this+table[i];
146 inline bool sanitize (SANITIZE_ARG_DEF) {
148 if (!SANITIZE (version)) return false;
149 if (version.major < 1 || version.major > 2) return true;
150 /* TODO Maybe we shouldn't NEUTER these offsets, they may cause a full copy
151 * of the whole font right now. */
152 return table.sanitize (SANITIZE_ARG, CONST_CHARP(this), CONST_CHARP(this));
156 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
157 FixedVersion version; /* Version of the TTC Header (1.0 or 2.0),
158 * 0x00010000 or 0x00020000 */
159 LongOffsetLongArrayOf<OffsetTable>
160 table; /* Array of offsets to the OffsetTable for each font
161 * from the beginning of the file */
163 ASSERT_SIZE (TTCHeader, 12);
170 struct OpenTypeFontFile
172 static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 );
173 static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O');
174 static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f');
176 STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
178 inline unsigned int get_face_count (void) const
182 case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (CONST_CHARP(this)).get_face_count ();
183 case TTCTag: return TTCHeader::get_for_data (CONST_CHARP(this)).get_face_count ();
186 inline const OpenTypeFontFace& get_face (unsigned int i) const
189 default: return Null(OpenTypeFontFace);
190 /* Note: for non-collection SFNT data we ignore index. This is because
191 * Apple dfont container is a container of SFNT's. So each SFNT is a
192 * non-TTC, but the index is more than zero. */
193 case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (CONST_CHARP(this));
194 case TTCTag: return TTCHeader::get_for_data (CONST_CHARP(this)).get_face (i);
198 /* This is how you get a table */
199 inline const char* get_table_data (const OpenTypeTable& table) const
201 if (HB_UNLIKELY (table.offset == 0)) return NULL;
202 return ((const char*) this) + table.offset;
205 inline bool sanitize (SANITIZE_ARG_DEF) {
207 if (!SANITIZE_SELF ()) return false;
209 default: return true;
210 case TrueTypeTag: case CFFTag: return SANITIZE_THIS (CAST (OffsetTable, *this, 0));
211 case TTCTag: return SANITIZE (CAST (TTCHeader, *this, 0));
215 Tag tag; /* 4-byte identifier. */
217 ASSERT_SIZE (OpenTypeFontFile, 4);
220 #endif /* HB_OPEN_FILE_PRIVATE_HH */