Use templates for const char * casts
[profile/ivi/org.tizen.video-player.git] / src / 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   static inline unsigned int get_size () { return sizeof (TableDirectory); }
51
52   inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
53     TRACE_SANITIZE ();
54     return SANITIZE_SELF () && SANITIZE (tag) &&
55            SANITIZE_MEM (ConstCharP(base) + (unsigned long) offset, length);
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 } OpenTypeTable;
64 ASSERT_SIZE (TableDirectory, 16);
65
66 typedef struct OffsetTable
67 {
68   friend struct OpenTypeFontFile;
69   friend struct TTCHeader;
70
71   STATIC_DEFINE_GET_FOR_DATA (OffsetTable);
72
73   inline unsigned int get_table_count (void) const
74   { return numTables; }
75   inline const Tag& get_table_tag (unsigned int i) const
76   {
77     if (HB_UNLIKELY (i >= numTables)) return Null(Tag);
78     return tableDir[i].tag;
79   }
80   inline const TableDirectory& get_table (unsigned int i) const
81   {
82     if (HB_UNLIKELY (i >= numTables)) return Null(TableDirectory);
83     return tableDir[i];
84   }
85   inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
86   {
87     Tag t;
88     t.set (tag);
89     // TODO bsearch
90     unsigned int count = numTables;
91     for (unsigned int i = 0; i < count; i++)
92     {
93       if (t == tableDir[i].tag)
94       {
95         if (table_index) *table_index = i;
96         return true;
97       }
98     }
99     if (table_index) *table_index = NO_INDEX;
100     return false;
101   }
102   inline const TableDirectory& get_table_by_tag (hb_tag_t tag) const
103   {
104     unsigned int table_index;
105     find_table_index (tag, &table_index);
106     return get_table (table_index);
107   }
108
109   inline unsigned int get_face_count (void) const { return 1; }
110
111   public:
112   inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
113     TRACE_SANITIZE ();
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))
118         return false;
119     return true;
120   }
121
122   private:
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 */
129 } OpenTypeFontFace;
130 ASSERT_SIZE_VAR (OffsetTable, 12, TableDirectory);
131
132 /*
133  * TrueType Collections
134  */
135
136 struct TTCHeader
137 {
138   friend struct OpenTypeFontFile;
139
140   STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2);
141
142   inline unsigned int get_face_count (void) const { return table.len; }
143
144   inline const OpenTypeFontFace& get_face (unsigned int i) const
145   {
146     return this+table[i];
147   }
148
149   inline bool sanitize (SANITIZE_ARG_DEF) {
150     TRACE_SANITIZE ();
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));
154   }
155
156   private:
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 */
163 };
164 ASSERT_SIZE (TTCHeader, 12);
165
166
167 /*
168  * OpenType Font File
169  */
170
171 struct OpenTypeFontFile
172 {
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');
176
177   STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
178
179   inline unsigned int get_face_count (void) const
180   {
181     switch (tag) {
182     default: return 0;
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 ();
185     }
186   }
187   inline const OpenTypeFontFace& get_face (unsigned int i) const
188   {
189     switch (tag) {
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);
196     }
197   }
198
199   /* This is how you get a table */
200   inline const char* get_table_data (const OpenTypeTable& table) const
201   {
202     if (HB_UNLIKELY (table.offset == 0)) return NULL;
203     return ((const char*) this) + table.offset;
204   }
205
206   inline bool sanitize (SANITIZE_ARG_DEF) {
207     TRACE_SANITIZE ();
208     if (!SANITIZE_SELF ()) return false;
209     switch (tag) {
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));
213     }
214   }
215
216   Tag           tag;            /* 4-byte identifier. */
217 };
218 ASSERT_SIZE (OpenTypeFontFile, 4);
219
220
221 #endif /* HB_OPEN_FILE_PRIVATE_HH */