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