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