993e7eb6f2438daa7d70523964c5fafc93607ad8
[framework/uifw/harfbuzz.git] / src / hb-open-file-private.hh
1 /*
2  * Copyright (C) 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
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   static inline unsigned int get_size () { return sizeof (TableDirectory); }
51
52   inline bool sanitize (SANITIZE_ARG_DEF) {
53     TRACE_SANITIZE ();
54     return SANITIZE_SELF ();
55   }
56
57   Tag           tag;            /* 4-byte identifier. */
58   CheckSum      checkSum;       /* CheckSum for this table. */
59   ULONG         offset;         /* Offset from beginning of TrueType font
60                                  * file. */
61   ULONG         length;         /* Length of this table. */
62 } OpenTypeTable;
63 ASSERT_SIZE (TableDirectory, 16);
64
65 typedef struct OffsetTable
66 {
67   friend struct OpenTypeFontFile;
68   friend struct TTCHeader;
69
70   STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
71
72   inline unsigned int get_table_count (void) const
73   { return numTables; }
74   inline const Tag& get_table_tag (unsigned int i) const
75   {
76     if (HB_UNLIKELY (i >= numTables)) return Null(Tag);
77     return tableDir[i].tag;
78   }
79   inline const TableDirectory& get_table (unsigned int i) const
80   {
81     if (HB_UNLIKELY (i >= numTables)) return Null(TableDirectory);
82     return tableDir[i];
83   }
84   inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
85   {
86     Tag t;
87     t.set (tag);
88     // TODO bsearch
89     unsigned int count = numTables;
90     for (unsigned int i = 0; i < count; i++)
91     {
92       if (t == tableDir[i].tag)
93       {
94         if (table_index) *table_index = i;
95         return true;
96       }
97     }
98     if (table_index) *table_index = NO_INDEX;
99     return false;
100   }
101   inline const TableDirectory& get_table_by_tag (hb_tag_t tag) const
102   {
103     unsigned int table_index;
104     find_table_index (tag, &table_index);
105     return get_table (table_index);
106   }
107
108   inline unsigned int get_face_count (void) const { return 1; }
109
110   public:
111   inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
112     TRACE_SANITIZE ();
113     if (!(SANITIZE_SELF () && SANITIZE_ARRAY (tableDir, TableDirectory::get_size (), numTables))) return false;
114     return true;
115     /* No need to check tables individually since we don't sanitize the
116      * referenced table, just the table directory.  Code retaind to make
117      * sure TableDirectory has a baseless sanitize(). */
118     unsigned int count = numTables;
119     for (unsigned int i = 0; i < count; i++)
120       if (!SANITIZE (tableDir[i]))
121         return false;
122     return true;
123   }
124
125   private:
126   Tag           sfnt_version;   /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
127   USHORT        numTables;      /* Number of tables. */
128   USHORT        searchRange;    /* (Maximum power of 2 <= numTables) x 16 */
129   USHORT        entrySelector;  /* Log2(maximum power of 2 <= numTables). */
130   USHORT        rangeShift;     /* NumTables x 16-searchRange. */
131   TableDirectory tableDir[VAR]; /* TableDirectory entries. numTables items */
132 } OpenTypeFontFace;
133 ASSERT_SIZE_VAR (OffsetTable, 12, TableDirectory);
134
135 /*
136  * TrueType Collections
137  */
138
139 struct TTCHeader
140 {
141   friend struct OpenTypeFontFile;
142
143   STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
144
145   inline unsigned int get_face_count (void) const { return table.len; }
146
147   inline const OpenTypeFontFace& get_face (unsigned int i) const
148   {
149     return this+table[i];
150   }
151
152   inline bool sanitize (SANITIZE_ARG_DEF) {
153     TRACE_SANITIZE ();
154     if (!SANITIZE (version)) return false;
155     if (version.major < 1 || version.major > 2) return true;
156     return table.sanitize (SANITIZE_ARG, CharP(this), CharP(this));
157   }
158
159   private:
160   Tag           ttcTag;         /* TrueType Collection ID string: 'ttcf' */
161   FixedVersion  version;        /* Version of the TTC Header (1.0 or 2.0),
162                                  * 0x00010000 or 0x00020000 */
163   LongOffsetLongArrayOf<OffsetTable>
164                 table;          /* Array of offsets to the OffsetTable for each font
165                                  * from the beginning of the file */
166 };
167 ASSERT_SIZE (TTCHeader, 12);
168
169
170 /*
171  * OpenType Font File
172  */
173
174 struct OpenTypeFontFile
175 {
176   static const hb_tag_t TrueTypeTag     = HB_TAG ( 0 , 1 , 0 , 0 );
177   static const hb_tag_t CFFTag          = HB_TAG ('O','T','T','O');
178   static const hb_tag_t TTCTag          = HB_TAG ('t','t','c','f');
179
180   STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
181
182   inline unsigned int get_face_count (void) const
183   {
184     switch (tag) {
185     default: return 0;
186     case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (CharP(this)).get_face_count ();
187     case TTCTag: return TTCHeader::get_for_data (CharP(this)).get_face_count ();
188     }
189   }
190   inline const OpenTypeFontFace& get_face (unsigned int i) const
191   {
192     switch (tag) {
193     default: return Null(OpenTypeFontFace);
194     /* Note: for non-collection SFNT data we ignore index.  This is because
195      * Apple dfont container is a container of SFNT's.  So each SFNT is a
196      * non-TTC, but the index is more than zero. */
197     case TrueTypeTag: case CFFTag: return OffsetTable::get_for_data (CharP(this));
198     case TTCTag: return TTCHeader::get_for_data (CharP(this)).get_face (i);
199     }
200   }
201
202   /* This is how you get a table */
203   inline const char* get_table_data (const OpenTypeTable& table) const
204   {
205     if (HB_UNLIKELY (table.offset == 0)) return NULL;
206     return CharP(this) + table.offset;
207   }
208
209   inline bool sanitize (SANITIZE_ARG_DEF) {
210     TRACE_SANITIZE ();
211     if (!SANITIZE_SELF ()) return false;
212     switch (tag) {
213     default: return true;
214     case TrueTypeTag: case CFFTag: return SANITIZE_THIS (CAST (OffsetTable, *this, 0));
215     case TTCTag: return SANITIZE (CAST (TTCHeader, *this, 0));
216     }
217   }
218
219   Tag           tag;            /* 4-byte identifier. */
220 };
221 ASSERT_SIZE (OpenTypeFontFile, 4);
222
223
224 #endif /* HB_OPEN_FILE_PRIVATE_HH */