blob: dda6b9453be3a088249a409ed8f475fc5207e6aa [file] [log] [blame]
Behdad Esfahbod2098a022009-08-02 19:57:00 -04001/*
2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
3 *
4 * This is part of HarfBuzz, an OpenType Layout engine 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
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040027#ifndef HB_OPEN_FILE_PRIVATE_HH
28#define HB_OPEN_FILE_PRIVATE_HH
Behdad Esfahbod2098a022009-08-02 19:57:00 -040029
Behdad Esfahbod7edb4302009-08-04 22:06:57 -040030#include "hb-open-type-private.hh"
Behdad Esfahbod2098a022009-08-02 19:57:00 -040031
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
48typedef struct TableDirectory
49{
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -040050 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -040051 TRACE_SANITIZE ();
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -040052 return SANITIZE_SELF () && SANITIZE (tag) &&
53 SANITIZE_MEM (CONST_CHARP(base) + (unsigned long) offset, length);
54 }
55
Behdad Esfahbod2098a022009-08-02 19:57:00 -040056 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} OpenTypeTable;
62ASSERT_SIZE (TableDirectory, 16);
63
64typedef struct OffsetTable
65{
66 friend struct OpenTypeFontFile;
67 friend struct TTCHeader;
68
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -040069 STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040070
71 inline unsigned int get_table_count (void) const
72 { return numTables; }
73 inline const Tag& get_table_tag (unsigned int i) const
74 {
75 if (HB_UNLIKELY (i >= numTables)) return Null(Tag);
76 return tableDir[i].tag;
77 }
78 inline const TableDirectory& get_table (unsigned int i) const
79 {
80 if (HB_UNLIKELY (i >= numTables)) return Null(TableDirectory);
81 return tableDir[i];
82 }
83 inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
84 {
85 const Tag t = tag;
86 // TODO bsearch
87 unsigned int count = numTables;
88 for (unsigned int i = 0; i < count; i++)
89 {
90 if (t == tableDir[i].tag)
91 {
92 if (table_index) *table_index = i;
93 return true;
94 }
95 }
96 if (table_index) *table_index = NO_INDEX;
97 return false;
98 }
99 inline const TableDirectory& get_table_by_tag (hb_tag_t tag) const
100 {
101 unsigned int table_index;
102 find_table_index (tag, &table_index);
103 return get_table (table_index);
104 }
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400105
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400106 inline unsigned int get_face_count (void) const { return 1; }
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400107
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400108 public:
109 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400110 TRACE_SANITIZE ();
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400111 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))
115 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400116 return true;
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400117 }
118
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400119 private:
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. */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500125 TableDirectory tableDir[VAR]; /* TableDirectory entries. numTables items */
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400126} OpenTypeFontFace;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500127ASSERT_SIZE_VAR (OffsetTable, 12, TableDirectory);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400128
129/*
130 * TrueType Collections
131 */
132
133struct TTCHeader
134{
135 friend struct OpenTypeFontFile;
136
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400137 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400138
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400139 inline unsigned int get_face_count (void) const { return table.len; }
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400140
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400141 inline const OpenTypeFontFace& get_face (unsigned int i) const
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400142 {
143 return this+table[i];
144 }
145
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400146 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400147 TRACE_SANITIZE ();
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400148 if (!SANITIZE (version)) return false;
149 if (version.major < 1 || version.major > 2) return true;
Behdad Esfahbode605c362009-08-06 18:27:28 -0400150 /* TODO Maybe we shouldn't NEUTER these offsets, they may cause a full copy
151 * of the whole font right now. */
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400152 return table.sanitize (SANITIZE_ARG, CONST_CHARP(this), CONST_CHARP(this));
153 }
154
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400155 private:
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 */
162};
163ASSERT_SIZE (TTCHeader, 12);
164
165
166/*
167 * OpenType Font File
168 */
169
170struct OpenTypeFontFile
171{
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');
175
176 STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
177
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400178 inline unsigned int get_face_count (void) const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400179 {
180 switch (tag) {
181 default: return 0;
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400182 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 ();
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400184 }
185 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400186 inline const OpenTypeFontFace& get_face (unsigned int i) const
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400187 {
188 switch (tag) {
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. */
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400193 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);
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400195 }
196 }
197
198 /* This is how you get a table */
199 inline const char* get_table_data (const OpenTypeTable& table) const
200 {
201 if (HB_UNLIKELY (table.offset == 0)) return NULL;
202 return ((const char*) this) + table.offset;
203 }
204
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400205 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400206 TRACE_SANITIZE ();
Behdad Esfahbod2870ac52009-08-04 22:43:47 -0400207 if (!SANITIZE_SELF ()) return false;
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400208 switch (tag) {
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));
212 }
213 }
214
Behdad Esfahbod2098a022009-08-02 19:57:00 -0400215 Tag tag; /* 4-byte identifier. */
216};
217ASSERT_SIZE (OpenTypeFontFile, 4);
218
219
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400220#endif /* HB_OPEN_FILE_PRIVATE_HH */