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 static inline unsigned int get_size () { return sizeof (TableDirectory); }
52 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
54 return SANITIZE_SELF () && SANITIZE (tag) &&
55 SANITIZE_MEM (ConstCharP(base) + (unsigned long) offset, length);
58 Tag tag; /* 4-byte identifier. */
59 CheckSum checkSum; /* CheckSum for this table. */
60 ULONG offset; /* Offset from beginning of TrueType font
62 ULONG length; /* Length of this table. */
64 ASSERT_SIZE (TableDirectory, 16);
66 typedef struct OffsetTable
68 friend struct OpenTypeFontFile;
69 friend struct TTCHeader;
71 STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
73 inline unsigned int get_table_count (void) const
75 inline const Tag& get_table_tag (unsigned int i) const
77 if (HB_UNLIKELY (i >= numTables)) return Null(Tag);
78 return tableDir[i].tag;
80 inline const TableDirectory& get_table (unsigned int i) const
82 if (HB_UNLIKELY (i >= numTables)) return Null(TableDirectory);
85 inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
90 unsigned int count = numTables;
91 for (unsigned int i = 0; i < count; i++)
93 if (t == tableDir[i].tag)
95 if (table_index) *table_index = i;
99 if (table_index) *table_index = NO_INDEX;
102 inline const TableDirectory& get_table_by_tag (hb_tag_t tag) const
104 unsigned int table_index;
105 find_table_index (tag, &table_index);
106 return get_table (table_index);
109 inline unsigned int get_face_count (void) const { return 1; }
112 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
114 if (!(SANITIZE_SELF () && SANITIZE_MEM (tableDir, tableDir[0].get_size () * numTables))) return false;
115 unsigned int count = numTables;
116 for (unsigned int i = 0; i < count; i++)
117 if (!SANITIZE_BASE (tableDir[i], base))
123 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
124 USHORT numTables; /* Number of tables. */
125 USHORT searchRange; /* (Maximum power of 2 <= numTables) x 16 */
126 USHORT entrySelector; /* Log2(maximum power of 2 <= numTables). */
127 USHORT rangeShift; /* NumTables x 16-searchRange. */
128 TableDirectory tableDir[VAR]; /* TableDirectory entries. numTables items */
130 ASSERT_SIZE_VAR (OffsetTable, 12, TableDirectory);
133 * TrueType Collections
138 friend struct OpenTypeFontFile;
140 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
142 inline unsigned int get_face_count (void) const { return table.len; }
144 inline const OpenTypeFontFace& get_face (unsigned int i) const
146 return this+table[i];
149 inline bool sanitize (SANITIZE_ARG_DEF) {
151 if (!SANITIZE (version)) return false;
152 if (version.major < 1 || version.major > 2) return true;
153 return table.sanitize (SANITIZE_ARG, ConstCharP(this), ConstCharP(this));
157 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
158 FixedVersion version; /* Version of the TTC Header (1.0 or 2.0),
159 * 0x00010000 or 0x00020000 */
160 LongOffsetLongArrayOf<OffsetTable>
161 table; /* Array of offsets to the OffsetTable for each font
162 * from the beginning of the file */
164 ASSERT_SIZE (TTCHeader, 12);
171 struct OpenTypeFontFile
173 static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 );
174 static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O');
175 static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f');
177 STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
179 inline unsigned int get_face_count (void) const
183 case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (ConstCharP(this)).get_face_count ();
184 case TTCTag: return TTCHeader::get_for_data (ConstCharP(this)).get_face_count ();
187 inline const OpenTypeFontFace& get_face (unsigned int i) const
190 default: return Null(OpenTypeFontFace);
191 /* Note: for non-collection SFNT data we ignore index. This is because
192 * Apple dfont container is a container of SFNT's. So each SFNT is a
193 * non-TTC, but the index is more than zero. */
194 case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (ConstCharP(this));
195 case TTCTag: return TTCHeader::get_for_data (ConstCharP(this)).get_face (i);
199 /* This is how you get a table */
200 inline const char* get_table_data (const OpenTypeTable& table) const
202 if (HB_UNLIKELY (table.offset == 0)) return NULL;
203 return ((const char*) this) + table.offset;
206 inline bool sanitize (SANITIZE_ARG_DEF) {
208 if (!SANITIZE_SELF ()) return false;
210 default: return true;
211 case TrueTypeTag: case CFFTag: return SANITIZE_THIS (CAST (OffsetTable, *this, 0));
212 case TTCTag: return SANITIZE (CAST (TTCHeader, *this, 0));
216 Tag tag; /* 4-byte identifier. */
218 ASSERT_SIZE (OpenTypeFontFile, 4);
221 #endif /* HB_OPEN_FILE_PRIVATE_HH */