Towards normalization
[framework/uifw/harfbuzz.git] / src / hb-open-file-private.hh
1 /*
2  * Copyright © 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 HB_BEGIN_DECLS
33
34
35 /*
36  *
37  * The OpenType Font File
38  *
39  */
40
41
42 /*
43  * Organization of an OpenType Font
44  */
45
46 struct OpenTypeFontFile;
47 struct OffsetTable;
48 struct TTCHeader;
49
50
51 typedef struct TableRecord
52 {
53   inline bool sanitize (hb_sanitize_context_t *c) {
54     TRACE_SANITIZE ();
55     return c->check_struct (this);
56   }
57
58   Tag           tag;            /* 4-byte identifier. */
59   CheckSum      checkSum;       /* CheckSum for this table. */
60   ULONG         offset;         /* Offset from beginning of TrueType font
61                                  * file. */
62   ULONG         length;         /* Length of this table. */
63   public:
64   DEFINE_SIZE_STATIC (16);
65 } OpenTypeTable;
66
67 typedef struct OffsetTable
68 {
69   friend struct OpenTypeFontFile;
70
71   inline unsigned int get_table_count (void) const
72   { return numTables; }
73   inline const TableRecord& get_table (unsigned int i) const
74   {
75     if (unlikely (i >= numTables)) return Null(TableRecord);
76     return tables[i];
77   }
78   inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
79   {
80     Tag t;
81     t.set (tag);
82     unsigned int count = numTables;
83     for (unsigned int i = 0; i < count; i++)
84     {
85       if (t == tables[i].tag)
86       {
87         if (table_index) *table_index = i;
88         return true;
89       }
90     }
91     if (table_index) *table_index = Index::NOT_FOUND_INDEX;
92     return false;
93   }
94   inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
95   {
96     unsigned int table_index;
97     find_table_index (tag, &table_index);
98     return get_table (table_index);
99   }
100
101   public:
102   inline bool sanitize (hb_sanitize_context_t *c) {
103     TRACE_SANITIZE ();
104     return c->check_struct (this)
105         && c->check_array (tables, TableRecord::static_size, numTables);
106   }
107
108   private:
109   Tag           sfnt_version;   /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
110   USHORT        numTables;      /* Number of tables. */
111   USHORT        searchRange;    /* (Maximum power of 2 <= numTables) x 16 */
112   USHORT        entrySelector;  /* Log2(maximum power of 2 <= numTables). */
113   USHORT        rangeShift;     /* NumTables x 16-searchRange. */
114   TableRecord   tables[VAR];    /* TableRecord entries. numTables items */
115   public:
116   DEFINE_SIZE_ARRAY (12, tables);
117 } OpenTypeFontFace;
118
119
120 /*
121  * TrueType Collections
122  */
123
124 struct TTCHeaderVersion1
125 {
126   friend struct TTCHeader;
127
128   inline unsigned int get_face_count (void) const { return table.len; }
129   inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
130
131   inline bool sanitize (hb_sanitize_context_t *c) {
132     TRACE_SANITIZE ();
133     return table.sanitize (c, this);
134   }
135
136   private:
137   Tag           ttcTag;         /* TrueType Collection ID string: 'ttcf' */
138   FixedVersion  version;        /* Version of the TTC Header (1.0),
139                                  * 0x00010000 */
140   LongOffsetLongArrayOf<OffsetTable>
141                 table;          /* Array of offsets to the OffsetTable for each font
142                                  * from the beginning of the file */
143   public:
144   DEFINE_SIZE_ARRAY (12, table);
145 };
146
147 struct TTCHeader
148 {
149   friend struct OpenTypeFontFile;
150
151   private:
152
153   inline unsigned int get_face_count (void) const
154   {
155     switch (u.header.version.major) {
156     case 2: /* version 2 is compatible with version 1 */
157     case 1: return u.version1.get_face_count ();
158     default:return 0;
159     }
160   }
161   inline const OpenTypeFontFace& get_face (unsigned int i) const
162   {
163     switch (u.header.version.major) {
164     case 2: /* version 2 is compatible with version 1 */
165     case 1: return u.version1.get_face (i);
166     default:return Null(OpenTypeFontFace);
167     }
168   }
169
170   inline bool sanitize (hb_sanitize_context_t *c) {
171     TRACE_SANITIZE ();
172     if (unlikely (!u.header.version.sanitize (c))) return false;
173     switch (u.header.version.major) {
174     case 2: /* version 2 is compatible with version 1 */
175     case 1: return u.version1.sanitize (c);
176     default:return true;
177     }
178   }
179
180   private:
181   union {
182   struct {
183   Tag           ttcTag;         /* TrueType Collection ID string: 'ttcf' */
184   FixedVersion  version;        /* Version of the TTC Header (1.0 or 2.0),
185                                  * 0x00010000 or 0x00020000 */
186   }                     header;
187   TTCHeaderVersion1     version1;
188   } u;
189 };
190
191
192 /*
193  * OpenType Font File
194  */
195
196 struct OpenTypeFontFile
197 {
198   static const hb_tag_t CFFTag          = HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */
199   static const hb_tag_t TrueTypeTag     = HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */
200   static const hb_tag_t TTCTag          = HB_TAG ('t','t','c','f'); /* TrueType Collection */
201   static const hb_tag_t TrueTag         = HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */
202   static const hb_tag_t Typ1Tag         = HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */
203
204   inline hb_tag_t get_tag (void) const { return u.tag; }
205
206   inline unsigned int get_face_count (void) const
207   {
208     switch (u.tag) {
209     case CFFTag:        /* All the non-collection tags */
210     case TrueTag:
211     case Typ1Tag:
212     case TrueTypeTag:   return 1;
213     case TTCTag:        return u.ttcHeader.get_face_count ();
214     default:            return 0;
215     }
216   }
217   inline const OpenTypeFontFace& get_face (unsigned int i) const
218   {
219     switch (u.tag) {
220     /* Note: for non-collection SFNT data we ignore index.  This is because
221      * Apple dfont container is a container of SFNT's.  So each SFNT is a
222      * non-TTC, but the index is more than zero. */
223     case CFFTag:        /* All the non-collection tags */
224     case TrueTag:
225     case Typ1Tag:
226     case TrueTypeTag:   return u.fontFace;
227     case TTCTag:        return u.ttcHeader.get_face (i);
228     default:            return Null(OpenTypeFontFace);
229     }
230   }
231
232   inline bool sanitize (hb_sanitize_context_t *c) {
233     TRACE_SANITIZE ();
234     if (unlikely (!u.tag.sanitize (c))) return false;
235     switch (u.tag) {
236     case CFFTag:        /* All the non-collection tags */
237     case TrueTag:
238     case Typ1Tag:
239     case TrueTypeTag:   return u.fontFace.sanitize (c);
240     case TTCTag:        return u.ttcHeader.sanitize (c);
241     default:            return true;
242     }
243   }
244
245   private:
246   union {
247   Tag                   tag;            /* 4-byte identifier. */
248   OpenTypeFontFace      fontFace;
249   TTCHeader             ttcHeader;
250   } u;
251   public:
252   DEFINE_SIZE_UNION (4, tag);
253 };
254
255
256 HB_END_DECLS
257
258 #endif /* HB_OPEN_FILE_PRIVATE_HH */