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