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