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