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