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