Git init
[external/pango1.0.git] / pango / opentype / hb-open-file-private.hh
1 /*
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
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
44 struct OpenTypeFontFile;
45 struct OffsetTable;
46 struct TTCHeader;
47
48 typedef struct TableDirectory
49 {
50   inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
51     TRACE_SANITIZE ();
52     return SANITIZE_SELF () && SANITIZE (tag) &&
53            SANITIZE_MEM (CONST_CHARP(base) + (unsigned long) offset, length);
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 } OpenTypeTable;
62 ASSERT_SIZE (TableDirectory, 16);
63
64 typedef struct OffsetTable
65 {
66   friend struct OpenTypeFontFile;
67   friend struct TTCHeader;
68
69   STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
70
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   }
105
106   inline unsigned int get_face_count (void) const { return 1; }
107
108   public:
109   inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
110     TRACE_SANITIZE ();
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))
115         return false;
116     return true;
117   }
118
119   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. */
125   TableDirectory tableDir[];    /* TableDirectory entries. numTables items */
126 } OpenTypeFontFace;
127 ASSERT_SIZE (OffsetTable, 12);
128
129 /*
130  * TrueType Collections
131  */
132
133 struct TTCHeader
134 {
135   friend struct OpenTypeFontFile;
136
137   STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
138
139   inline unsigned int get_face_count (void) const { return table.len; }
140
141   inline const OpenTypeFontFace& get_face (unsigned int i) const
142   {
143     return this+table[i];
144   }
145
146   inline bool sanitize (SANITIZE_ARG_DEF) {
147     TRACE_SANITIZE ();
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));
153   }
154
155   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 };
163 ASSERT_SIZE (TTCHeader, 12);
164
165
166 /*
167  * OpenType Font File
168  */
169
170 struct 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
178   inline unsigned int get_face_count (void) const
179   {
180     switch (tag) {
181     default: return 0;
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 ();
184     }
185   }
186   inline const OpenTypeFontFace& get_face (unsigned int i) const
187   {
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. */
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);
195     }
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
205   inline bool sanitize (SANITIZE_ARG_DEF) {
206     TRACE_SANITIZE ();
207     if (!SANITIZE_SELF ()) return false;
208     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
215   Tag           tag;            /* 4-byte identifier. */
216 };
217 ASSERT_SIZE (OpenTypeFontFile, 4);
218
219
220 #endif /* HB_OPEN_FILE_PRIVATE_HH */