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