Remove SANITIZE macro
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gdef-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_OT_LAYOUT_GDEF_PRIVATE_HH
28 #define HB_OT_LAYOUT_GDEF_PRIVATE_HH
29
30 #include "hb-ot-layout-common-private.hh"
31
32 #include "hb-font-private.h"
33
34
35 /*
36  * Attachment List Table
37  */
38
39 typedef ArrayOf<USHORT> AttachPoint;    /* Array of contour point indices--in
40                                          * increasing numerical order */
41 ASSERT_SIZE (AttachPoint, 2);
42
43 struct AttachList
44 {
45   inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
46                                          unsigned int start_offset,
47                                          unsigned int *point_count /* IN/OUT */,
48                                          unsigned int *point_array /* OUT */) const
49   {
50     unsigned int index = (this+coverage) (glyph_id);
51     if (index == NOT_COVERED)
52     {
53       if (point_count)
54         *point_count = 0;
55       return 0;
56     }
57
58     const AttachPoint &points = this+attachPoint[index];
59
60     if (point_count) {
61       const USHORT *array = points.sub_array (start_offset, point_count);
62       unsigned int count = *point_count;
63       for (unsigned int i = 0; i < count; i++)
64         point_array[i] = array[i];
65     }
66
67     return points.len;
68   }
69
70   inline bool sanitize (hb_sanitize_context_t *context) {
71     TRACE_SANITIZE ();
72     return coverage.sanitize (context, this)
73         && attachPoint.sanitize (context, this);
74   }
75
76   private:
77   OffsetTo<Coverage>
78                 coverage;               /* Offset to Coverage table -- from
79                                          * beginning of AttachList table */
80   OffsetArrayOf<AttachPoint>
81                 attachPoint;            /* Array of AttachPoint tables
82                                          * in Coverage Index order */
83 };
84 ASSERT_SIZE (AttachList, 4);
85
86 /*
87  * Ligature Caret Table
88  */
89
90 struct CaretValueFormat1
91 {
92   friend struct CaretValue;
93
94   private:
95   inline int get_caret_value (hb_ot_layout_context_t *context, hb_codepoint_t glyph_id HB_UNUSED) const
96   {
97     /* TODO vertical */
98     return _hb_16dot16_mul_round (context->font->x_scale, coordinate);
99   }
100
101   inline bool sanitize (hb_sanitize_context_t *context) {
102     TRACE_SANITIZE ();
103     return SANITIZE_SELF ();
104   }
105
106   private:
107   USHORT        caretValueFormat;       /* Format identifier--format = 1 */
108   SHORT         coordinate;             /* X or Y value, in design units */
109 };
110 ASSERT_SIZE (CaretValueFormat1, 4);
111
112 struct CaretValueFormat2
113 {
114   friend struct CaretValue;
115
116   private:
117   inline int get_caret_value (hb_ot_layout_context_t *context, hb_codepoint_t glyph_id) const
118   {
119     /* TODO vertical */
120     hb_position_t x, y;
121     if (hb_font_get_contour_point (context->font, context->face, caretValuePoint, glyph_id, &x, &y))
122       return x;
123     else
124       return 0;
125   }
126
127   inline bool sanitize (hb_sanitize_context_t *context) {
128     TRACE_SANITIZE ();
129     return SANITIZE_SELF ();
130   }
131
132   private:
133   USHORT        caretValueFormat;       /* Format identifier--format = 2 */
134   USHORT        caretValuePoint;        /* Contour point index on glyph */
135 };
136 ASSERT_SIZE (CaretValueFormat2, 4);
137
138 struct CaretValueFormat3
139 {
140   friend struct CaretValue;
141
142   inline int get_caret_value (hb_ot_layout_context_t *context, hb_codepoint_t glyph_id HB_UNUSED) const
143   {
144     /* TODO vertical */
145     return _hb_16dot16_mul_round (context->font->x_scale, coordinate) +
146            ((this+deviceTable).get_delta (context->font->x_ppem) << 16);
147   }
148
149   inline bool sanitize (hb_sanitize_context_t *context) {
150     TRACE_SANITIZE ();
151     return SANITIZE_SELF ()
152         && deviceTable.sanitize (context, this);
153   }
154
155   private:
156   USHORT        caretValueFormat;       /* Format identifier--format = 3 */
157   SHORT         coordinate;             /* X or Y value, in design units */
158   OffsetTo<Device>
159                 deviceTable;            /* Offset to Device table for X or Y
160                                          * value--from beginning of CaretValue
161                                          * table */
162 };
163 ASSERT_SIZE (CaretValueFormat3, 6);
164
165 struct CaretValue
166 {
167   inline int get_caret_value (hb_ot_layout_context_t *context, hb_codepoint_t glyph_id) const
168   {
169     switch (u.format) {
170     case 1: return u.format1->get_caret_value (context, glyph_id);
171     case 2: return u.format2->get_caret_value (context, glyph_id);
172     case 3: return u.format3->get_caret_value (context, glyph_id);
173     default:return 0;
174     }
175   }
176
177   inline bool sanitize (hb_sanitize_context_t *context) {
178     TRACE_SANITIZE ();
179     if (!u.format.sanitize (context)) return false;
180     switch (u.format) {
181     case 1: return u.format1->sanitize (context);
182     case 2: return u.format2->sanitize (context);
183     case 3: return u.format3->sanitize (context);
184     default:return true;
185     }
186   }
187
188   private:
189   union {
190   USHORT                format;         /* Format identifier */
191   CaretValueFormat1     format1[VAR];
192   CaretValueFormat2     format2[VAR];
193   CaretValueFormat3     format3[VAR];
194   } u;
195 };
196
197 struct LigGlyph
198 {
199   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
200                                       hb_codepoint_t glyph_id,
201                                       unsigned int start_offset,
202                                       unsigned int *caret_count /* IN/OUT */,
203                                       int *caret_array /* OUT */) const
204   {
205     if (caret_count) {
206       const OffsetTo<CaretValue> *array = carets.sub_array (start_offset, caret_count);
207       unsigned int count = *caret_count;
208       for (unsigned int i = 0; i < count; i++)
209         caret_array[i] = (this+array[i]).get_caret_value (context, glyph_id);
210     }
211
212     return carets.len;
213   }
214
215   inline bool sanitize (hb_sanitize_context_t *context) {
216     TRACE_SANITIZE ();
217     return carets.sanitize (context, this);
218   }
219
220   private:
221   OffsetArrayOf<CaretValue>
222                 carets;                 /* Offset array of CaretValue tables
223                                          * --from beginning of LigGlyph table
224                                          * --in increasing coordinate order */
225 };
226 ASSERT_SIZE (LigGlyph, 2);
227
228 struct LigCaretList
229 {
230   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
231                                       hb_codepoint_t glyph_id,
232                                       unsigned int start_offset,
233                                       unsigned int *caret_count /* IN/OUT */,
234                                       int *caret_array /* OUT */) const
235   {
236     unsigned int index = (this+coverage) (glyph_id);
237     if (index == NOT_COVERED)
238     {
239       if (caret_count)
240         *caret_count = 0;
241       return 0;
242     }
243     const LigGlyph &lig_glyph = this+ligGlyph[index];
244     return lig_glyph.get_lig_carets (context, glyph_id, start_offset, caret_count, caret_array);
245   }
246
247   inline bool sanitize (hb_sanitize_context_t *context) {
248     TRACE_SANITIZE ();
249     return coverage.sanitize (context, this)
250         && ligGlyph.sanitize (context, this);
251   }
252
253   private:
254   OffsetTo<Coverage>
255                 coverage;               /* Offset to Coverage table--from
256                                          * beginning of LigCaretList table */
257   OffsetArrayOf<LigGlyph>
258                 ligGlyph;               /* Array of LigGlyph tables
259                                          * in Coverage Index order */
260 };
261 ASSERT_SIZE (LigCaretList, 4);
262
263
264 struct MarkGlyphSetsFormat1
265 {
266   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
267   { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }
268
269   inline bool sanitize (hb_sanitize_context_t *context) {
270     TRACE_SANITIZE ();
271     return coverage.sanitize (context, this);
272   }
273
274   private:
275   USHORT        format;                 /* Format identifier--format = 1 */
276   LongOffsetArrayOf<Coverage>
277                 coverage;               /* Array of long offsets to mark set
278                                          * coverage tables */
279 };
280 ASSERT_SIZE (MarkGlyphSetsFormat1, 4);
281
282 struct MarkGlyphSets
283 {
284   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
285   {
286     switch (u.format) {
287     case 1: return u.format1->covers (set_index, glyph_id);
288     default:return false;
289     }
290   }
291
292   inline bool sanitize (hb_sanitize_context_t *context) {
293     TRACE_SANITIZE ();
294     if (!u.format.sanitize (context)) return false;
295     switch (u.format) {
296     case 1: return u.format1->sanitize (context);
297     default:return true;
298     }
299   }
300
301   private:
302   union {
303   USHORT                format;         /* Format identifier */
304   MarkGlyphSetsFormat1  format1[VAR];
305   } u;
306 };
307
308
309 /*
310  * GDEF
311  */
312
313 struct GDEF
314 {
315   static const hb_tag_t Tag     = HB_OT_TAG_GDEF;
316
317   enum {
318     UnclassifiedGlyph   = 0,
319     BaseGlyph           = 1,
320     LigatureGlyph       = 2,
321     MarkGlyph           = 3,
322     ComponentGlyph      = 4
323   };
324
325   inline bool has_glyph_classes () const { return glyphClassDef != 0; }
326   inline hb_ot_layout_class_t get_glyph_class (hb_codepoint_t glyph) const
327   { return (this+glyphClassDef).get_class (glyph); }
328
329   inline bool has_mark_attachment_types () const { return markAttachClassDef != 0; }
330   inline hb_ot_layout_class_t get_mark_attachment_type (hb_codepoint_t glyph) const
331   { return (this+markAttachClassDef).get_class (glyph); }
332
333   inline bool has_attach_points () const { return attachList != 0; }
334   inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
335                                          unsigned int start_offset,
336                                          unsigned int *point_count /* IN/OUT */,
337                                          unsigned int *point_array /* OUT */) const
338   { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
339
340   inline bool has_lig_carets () const { return ligCaretList != 0; }
341   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
342                                       hb_codepoint_t glyph_id,
343                                       unsigned int start_offset,
344                                       unsigned int *caret_count /* IN/OUT */,
345                                       int *caret_array /* OUT */) const
346   { return (this+ligCaretList).get_lig_carets (context, glyph_id, start_offset, caret_count, caret_array); }
347
348   inline bool has_mark_sets () const { return version >= 0x00010002 && markGlyphSetsDef[0] != 0; }
349   inline bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
350   { return version >= 0x00010002 && (this+markGlyphSetsDef[0]).covers (set_index, glyph_id); }
351
352   inline bool sanitize (hb_sanitize_context_t *context) {
353     TRACE_SANITIZE ();
354     return version.sanitize (context) && likely (version.major == 1)
355         && glyphClassDef.sanitize (context, this)
356         && attachList.sanitize (context, this)
357         && ligCaretList.sanitize (context, this)
358         && markAttachClassDef.sanitize (context, this)
359         && (version < 0x00010002 || markGlyphSetsDef[0].sanitize (context, this));
360   }
361
362   private:
363   FixedVersion  version;                /* Version of the GDEF table--currently
364                                          * 0x00010002 */
365   OffsetTo<ClassDef>
366                 glyphClassDef;          /* Offset to class definition table
367                                          * for glyph type--from beginning of
368                                          * GDEF header (may be Null) */
369   OffsetTo<AttachList>
370                 attachList;             /* Offset to list of glyphs with
371                                          * attachment points--from beginning
372                                          * of GDEF header (may be Null) */
373   OffsetTo<LigCaretList>
374                 ligCaretList;           /* Offset to list of positioning points
375                                          * for ligature carets--from beginning
376                                          * of GDEF header (may be Null) */
377   OffsetTo<ClassDef>
378                 markAttachClassDef;     /* Offset to class definition table for
379                                          * mark attachment type--from beginning
380                                          * of GDEF header (may be Null) */
381   OffsetTo<MarkGlyphSets>
382                 markGlyphSetsDef[VAR];  /* Offset to the table of mark set
383                                          * definitions--from beginning of GDEF
384                                          * header (may be NULL).  Introduced
385                                          * in version 00010002. */
386 };
387 ASSERT_SIZE_VAR (GDEF, 12, OffsetTo<MarkGlyphSets>);
388
389
390 #endif /* HB_OT_LAYOUT_GDEF_PRIVATE_HH */