Always allocate new ligature id
[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_direction_t direction, hb_codepoint_t glyph_id HB_UNUSED) const
98   {
99     return HB_DIRECTION_IS_HORIZONTAL (direction) ? c->scale_x (coordinate) : c->scale_y (coordinate);
100   }
101
102   inline bool sanitize (hb_sanitize_context_t *c) {
103     TRACE_SANITIZE ();
104     return c->check_struct (this);
105   }
106
107   private:
108   USHORT        caretValueFormat;       /* Format identifier--format = 1 */
109   SHORT         coordinate;             /* X or Y value, in design units */
110   public:
111   DEFINE_SIZE_STATIC (4);
112 };
113
114 struct CaretValueFormat2
115 {
116   friend struct CaretValue;
117
118   private:
119   inline int get_caret_value (hb_ot_layout_context_t *c, hb_direction_t direction, hb_codepoint_t glyph_id) const
120   {
121     hb_position_t x, y;
122     if (hb_font_get_contour_point (c->font, c->face, caretValuePoint, glyph_id, &x, &y))
123       return HB_DIRECTION_IS_HORIZONTAL (direction) ? x : y;
124     else
125       return 0;
126   }
127
128   inline bool sanitize (hb_sanitize_context_t *c) {
129     TRACE_SANITIZE ();
130     return c->check_struct (this);
131   }
132
133   private:
134   USHORT        caretValueFormat;       /* Format identifier--format = 2 */
135   USHORT        caretValuePoint;        /* Contour point index on glyph */
136   public:
137   DEFINE_SIZE_STATIC (4);
138 };
139
140 struct CaretValueFormat3
141 {
142   friend struct CaretValue;
143
144   inline int get_caret_value (hb_ot_layout_context_t *c, hb_direction_t direction, hb_codepoint_t glyph_id) const
145   {
146     return HB_DIRECTION_IS_HORIZONTAL (direction) ?
147            c->scale_x (coordinate) + (this+deviceTable).get_x_delta (c) :
148            c->scale_y (coordinate) + (this+deviceTable).get_y_delta (c);
149   }
150
151   inline bool sanitize (hb_sanitize_context_t *c) {
152     TRACE_SANITIZE ();
153     return c->check_struct (this)
154         && deviceTable.sanitize (c, this);
155   }
156
157   private:
158   USHORT        caretValueFormat;       /* Format identifier--format = 3 */
159   SHORT         coordinate;             /* X or Y value, in design units */
160   OffsetTo<Device>
161                 deviceTable;            /* Offset to Device table for X or Y
162                                          * value--from beginning of CaretValue
163                                          * table */
164   public:
165   DEFINE_SIZE_STATIC (6);
166 };
167
168 struct CaretValue
169 {
170   inline int get_caret_value (hb_ot_layout_context_t *c, hb_direction_t direction, hb_codepoint_t glyph_id) const
171   {
172     switch (u.format) {
173     case 1: return u.format1.get_caret_value (c, direction, glyph_id);
174     case 2: return u.format2.get_caret_value (c, direction, glyph_id);
175     case 3: return u.format3.get_caret_value (c, direction, glyph_id);
176     default:return 0;
177     }
178   }
179
180   inline bool sanitize (hb_sanitize_context_t *c) {
181     TRACE_SANITIZE ();
182     if (!u.format.sanitize (c)) return false;
183     switch (u.format) {
184     case 1: return u.format1.sanitize (c);
185     case 2: return u.format2.sanitize (c);
186     case 3: return u.format3.sanitize (c);
187     default:return true;
188     }
189   }
190
191   private:
192   union {
193   USHORT                format;         /* Format identifier */
194   CaretValueFormat1     format1;
195   CaretValueFormat2     format2;
196   CaretValueFormat3     format3;
197   } u;
198   public:
199   DEFINE_SIZE_UNION (2, format);
200 };
201
202 struct LigGlyph
203 {
204   inline unsigned int get_lig_carets (hb_ot_layout_context_t *c,
205                                       hb_direction_t direction,
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, direction, 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_direction_t direction,
239                                       hb_codepoint_t glyph_id,
240                                       unsigned int start_offset,
241                                       unsigned int *caret_count /* IN/OUT */,
242                                       int *caret_array /* OUT */) const
243   {
244     unsigned int index = (this+coverage) (glyph_id);
245     if (index == NOT_COVERED)
246     {
247       if (caret_count)
248         *caret_count = 0;
249       return 0;
250     }
251     const LigGlyph &lig_glyph = this+ligGlyph[index];
252     return lig_glyph.get_lig_carets (c, direction, glyph_id, start_offset, caret_count, caret_array);
253   }
254
255   inline bool sanitize (hb_sanitize_context_t *c) {
256     TRACE_SANITIZE ();
257     return coverage.sanitize (c, this)
258         && ligGlyph.sanitize (c, this);
259   }
260
261   private:
262   OffsetTo<Coverage>
263                 coverage;               /* Offset to Coverage table--from
264                                          * beginning of LigCaretList table */
265   OffsetArrayOf<LigGlyph>
266                 ligGlyph;               /* Array of LigGlyph tables
267                                          * in Coverage Index order */
268   public:
269   DEFINE_SIZE_ARRAY (4, ligGlyph);
270 };
271
272
273 struct MarkGlyphSetsFormat1
274 {
275   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
276   { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }
277
278   inline bool sanitize (hb_sanitize_context_t *c) {
279     TRACE_SANITIZE ();
280     return coverage.sanitize (c, this);
281   }
282
283   private:
284   USHORT        format;                 /* Format identifier--format = 1 */
285   LongOffsetArrayOf<Coverage>
286                 coverage;               /* Array of long offsets to mark set
287                                          * coverage tables */
288   public:
289   DEFINE_SIZE_ARRAY (4, coverage);
290 };
291
292 struct MarkGlyphSets
293 {
294   inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
295   {
296     switch (u.format) {
297     case 1: return u.format1.covers (set_index, glyph_id);
298     default:return false;
299     }
300   }
301
302   inline bool sanitize (hb_sanitize_context_t *c) {
303     TRACE_SANITIZE ();
304     if (!u.format.sanitize (c)) return false;
305     switch (u.format) {
306     case 1: return u.format1.sanitize (c);
307     default:return true;
308     }
309   }
310
311   private:
312   union {
313   USHORT                format;         /* Format identifier */
314   MarkGlyphSetsFormat1  format1;
315   } u;
316   public:
317   DEFINE_SIZE_UNION (2, format);
318 };
319
320
321 /*
322  * GDEF
323  */
324
325 struct GDEF
326 {
327   static const hb_tag_t Tag     = HB_OT_TAG_GDEF;
328
329   enum {
330     UnclassifiedGlyph   = 0,
331     BaseGlyph           = 1,
332     LigatureGlyph       = 2,
333     MarkGlyph           = 3,
334     ComponentGlyph      = 4
335   };
336
337   inline bool has_glyph_classes (void) const { return glyphClassDef != 0; }
338   inline hb_ot_layout_class_t get_glyph_class (hb_codepoint_t glyph) const
339   { return (this+glyphClassDef).get_class (glyph); }
340
341   inline bool has_mark_attachment_types (void) const { return markAttachClassDef != 0; }
342   inline hb_ot_layout_class_t get_mark_attachment_type (hb_codepoint_t glyph) const
343   { return (this+markAttachClassDef).get_class (glyph); }
344
345   inline bool has_attach_points (void) const { return attachList != 0; }
346   inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
347                                          unsigned int start_offset,
348                                          unsigned int *point_count /* IN/OUT */,
349                                          unsigned int *point_array /* OUT */) const
350   { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
351
352   inline bool has_lig_carets (void) const { return ligCaretList != 0; }
353   inline unsigned int get_lig_carets (hb_ot_layout_context_t *c,
354                                       hb_direction_t direction,
355                                       hb_codepoint_t glyph_id,
356                                       unsigned int start_offset,
357                                       unsigned int *caret_count /* IN/OUT */,
358                                       int *caret_array /* OUT */) const
359   { return (this+ligCaretList).get_lig_carets (c, direction, glyph_id, start_offset, caret_count, caret_array); }
360
361   inline bool has_mark_sets (void) const { return version >= 0x00010002 && markGlyphSetsDef[0] != 0; }
362   inline bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
363   { return version >= 0x00010002 && (this+markGlyphSetsDef[0]).covers (set_index, glyph_id); }
364
365   inline bool sanitize (hb_sanitize_context_t *c) {
366     TRACE_SANITIZE ();
367     return version.sanitize (c) && likely (version.major == 1)
368         && glyphClassDef.sanitize (c, this)
369         && attachList.sanitize (c, this)
370         && ligCaretList.sanitize (c, this)
371         && markAttachClassDef.sanitize (c, this)
372         && (version < 0x00010002 || markGlyphSetsDef[0].sanitize (c, this));
373   }
374
375   private:
376   FixedVersion  version;                /* Version of the GDEF table--currently
377                                          * 0x00010002 */
378   OffsetTo<ClassDef>
379                 glyphClassDef;          /* Offset to class definition table
380                                          * for glyph type--from beginning of
381                                          * GDEF header (may be Null) */
382   OffsetTo<AttachList>
383                 attachList;             /* Offset to list of glyphs with
384                                          * attachment points--from beginning
385                                          * of GDEF header (may be Null) */
386   OffsetTo<LigCaretList>
387                 ligCaretList;           /* Offset to list of positioning points
388                                          * for ligature carets--from beginning
389                                          * of GDEF header (may be Null) */
390   OffsetTo<ClassDef>
391                 markAttachClassDef;     /* Offset to class definition table for
392                                          * mark attachment type--from beginning
393                                          * of GDEF header (may be Null) */
394   OffsetTo<MarkGlyphSets>
395                 markGlyphSetsDef[VAR];  /* Offset to the table of mark set
396                                          * definitions--from beginning of GDEF
397                                          * header (may be NULL).  Introduced
398                                          * in version 00010002. */
399   public:
400   DEFINE_SIZE_ARRAY (12, markGlyphSetsDef);
401 };
402
403
404 HB_END_DECLS
405
406 #endif /* HB_OT_LAYOUT_GDEF_PRIVATE_HH */