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