Cleanup DEFINE_SIZE_VAR2
[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   public:
199   DEFINE_SIZE_UNION (2, format);
200 };
201
202 struct LigGlyph
203 {
204   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
205                                       hb_codepoint_t glyph_id,
206                                       unsigned int start_offset,
207                                       unsigned int *caret_count /* IN/OUT */,
208                                       int *caret_array /* OUT */) const
209   {
210     if (caret_count) {
211       const OffsetTo<CaretValue> *array = carets.sub_array (start_offset, caret_count);
212       unsigned int count = *caret_count;
213       for (unsigned int i = 0; i < count; i++)
214         caret_array[i] = (this+array[i]).get_caret_value (context, glyph_id);
215     }
216
217     return carets.len;
218   }
219
220   inline bool sanitize (hb_sanitize_context_t *context) {
221     TRACE_SANITIZE ();
222     return carets.sanitize (context, this);
223   }
224
225   private:
226   OffsetArrayOf<CaretValue>
227                 carets;                 /* Offset array of CaretValue tables
228                                          * --from beginning of LigGlyph table
229                                          * --in increasing coordinate order */
230   public:
231   DEFINE_SIZE_VAR (2, OffsetTo<CaretValue>);
232 };
233
234 struct LigCaretList
235 {
236   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
237                                       hb_codepoint_t glyph_id,
238                                       unsigned int start_offset,
239                                       unsigned int *caret_count /* IN/OUT */,
240                                       int *caret_array /* OUT */) const
241   {
242     unsigned int index = (this+coverage) (glyph_id);
243     if (index == NOT_COVERED)
244     {
245       if (caret_count)
246         *caret_count = 0;
247       return 0;
248     }
249     const LigGlyph &lig_glyph = this+ligGlyph[index];
250     return lig_glyph.get_lig_carets (context, glyph_id, start_offset, caret_count, caret_array);
251   }
252
253   inline bool sanitize (hb_sanitize_context_t *context) {
254     TRACE_SANITIZE ();
255     return coverage.sanitize (context, this)
256         && ligGlyph.sanitize (context, this);
257   }
258
259   private:
260   OffsetTo<Coverage>
261                 coverage;               /* Offset to Coverage table--from
262                                          * beginning of LigCaretList table */
263   OffsetArrayOf<LigGlyph>
264                 ligGlyph;               /* Array of LigGlyph tables
265                                          * in Coverage Index order */
266   public:
267   DEFINE_SIZE_VAR (4, OffsetTo<LigGlyph>);
268 };
269
270
271 struct MarkGlyphSetsFormat1
272 {
273   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
274   { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }
275
276   inline bool sanitize (hb_sanitize_context_t *context) {
277     TRACE_SANITIZE ();
278     return coverage.sanitize (context, this);
279   }
280
281   private:
282   USHORT        format;                 /* Format identifier--format = 1 */
283   LongOffsetArrayOf<Coverage>
284                 coverage;               /* Array of long offsets to mark set
285                                          * coverage tables */
286   public:
287   DEFINE_SIZE_VAR (4, LongOffsetTo<Coverage>);
288 };
289
290 struct MarkGlyphSets
291 {
292   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
293   {
294     switch (u.format) {
295     case 1: return u.format1->covers (set_index, glyph_id);
296     default:return false;
297     }
298   }
299
300   inline bool sanitize (hb_sanitize_context_t *context) {
301     TRACE_SANITIZE ();
302     if (!u.format.sanitize (context)) return false;
303     switch (u.format) {
304     case 1: return u.format1->sanitize (context);
305     default:return true;
306     }
307   }
308
309   private:
310   union {
311   USHORT                format;         /* Format identifier */
312   MarkGlyphSetsFormat1  format1[VAR];
313   } u;
314   public:
315   DEFINE_SIZE_UNION (2, format);
316 };
317
318
319 /*
320  * GDEF
321  */
322
323 struct GDEF
324 {
325   static const hb_tag_t Tag     = HB_OT_TAG_GDEF;
326
327   enum {
328     UnclassifiedGlyph   = 0,
329     BaseGlyph           = 1,
330     LigatureGlyph       = 2,
331     MarkGlyph           = 3,
332     ComponentGlyph      = 4
333   };
334
335   inline bool has_glyph_classes () const { return glyphClassDef != 0; }
336   inline hb_ot_layout_class_t get_glyph_class (hb_codepoint_t glyph) const
337   { return (this+glyphClassDef).get_class (glyph); }
338
339   inline bool has_mark_attachment_types () const { return markAttachClassDef != 0; }
340   inline hb_ot_layout_class_t get_mark_attachment_type (hb_codepoint_t glyph) const
341   { return (this+markAttachClassDef).get_class (glyph); }
342
343   inline bool has_attach_points () const { return attachList != 0; }
344   inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
345                                          unsigned int start_offset,
346                                          unsigned int *point_count /* IN/OUT */,
347                                          unsigned int *point_array /* OUT */) const
348   { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
349
350   inline bool has_lig_carets () const { return ligCaretList != 0; }
351   inline unsigned int get_lig_carets (hb_ot_layout_context_t *context,
352                                       hb_codepoint_t glyph_id,
353                                       unsigned int start_offset,
354                                       unsigned int *caret_count /* IN/OUT */,
355                                       int *caret_array /* OUT */) const
356   { return (this+ligCaretList).get_lig_carets (context, glyph_id, start_offset, caret_count, caret_array); }
357
358   inline bool has_mark_sets () const { return version >= 0x00010002 && markGlyphSetsDef[0] != 0; }
359   inline bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
360   { return version >= 0x00010002 && (this+markGlyphSetsDef[0]).covers (set_index, glyph_id); }
361
362   inline bool sanitize (hb_sanitize_context_t *context) {
363     TRACE_SANITIZE ();
364     return version.sanitize (context) && likely (version.major == 1)
365         && glyphClassDef.sanitize (context, this)
366         && attachList.sanitize (context, this)
367         && ligCaretList.sanitize (context, this)
368         && markAttachClassDef.sanitize (context, this)
369         && (version < 0x00010002 || markGlyphSetsDef[0].sanitize (context, this));
370   }
371
372   private:
373   FixedVersion  version;                /* Version of the GDEF table--currently
374                                          * 0x00010002 */
375   OffsetTo<ClassDef>
376                 glyphClassDef;          /* Offset to class definition table
377                                          * for glyph type--from beginning of
378                                          * GDEF header (may be Null) */
379   OffsetTo<AttachList>
380                 attachList;             /* Offset to list of glyphs with
381                                          * attachment points--from beginning
382                                          * of GDEF header (may be Null) */
383   OffsetTo<LigCaretList>
384                 ligCaretList;           /* Offset to list of positioning points
385                                          * for ligature carets--from beginning
386                                          * of GDEF header (may be Null) */
387   OffsetTo<ClassDef>
388                 markAttachClassDef;     /* Offset to class definition table for
389                                          * mark attachment type--from beginning
390                                          * of GDEF header (may be Null) */
391   OffsetTo<MarkGlyphSets>
392                 markGlyphSetsDef[VAR];  /* Offset to the table of mark set
393                                          * definitions--from beginning of GDEF
394                                          * header (may be NULL).  Introduced
395                                          * in version 00010002. */
396   public:
397   DEFINE_SIZE_VAR (12, OffsetTo<MarkGlyphSets>);
398 };
399
400
401 #endif /* HB_OT_LAYOUT_GDEF_PRIVATE_HH */