1/*
2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2010,2011,2012 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29#ifndef HB_OT_LAYOUT_GDEF_TABLE_HH
30#define HB_OT_LAYOUT_GDEF_TABLE_HH
31
32#include "hb-ot-layout-common.hh"
33
34#include "hb-font.hh"
35
36
37namespace OT {
38
39
40/*
41 * Attachment List Table
42 */
43
44typedef ArrayOf<HBUINT16> AttachPoint; /* Array of contour point indices--in
45 * increasing numerical order */
46
47struct AttachList
48{
49 inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
50 unsigned int start_offset,
51 unsigned int *point_count /* IN/OUT */,
52 unsigned int *point_array /* OUT */) const
53 {
54 unsigned int index = (this+coverage).get_coverage (glyph_id);
55 if (index == NOT_COVERED)
56 {
57 if (point_count)
58 *point_count = 0;
59 return 0;
60 }
61
62 const AttachPoint &points = this+attachPoint[index];
63
64 if (point_count) {
65 const HBUINT16 *array = points.sub_array (start_offset, point_count);
66 unsigned int count = *point_count;
67 for (unsigned int i = 0; i < count; i++)
68 point_array[i] = array[i];
69 }
70
71 return points.len;
72 }
73
74 inline bool sanitize (hb_sanitize_context_t *c) const
75 {
76 TRACE_SANITIZE (this);
77 return_trace (coverage.sanitize (c, this) && attachPoint.sanitize (c, this));
78 }
79
80 protected:
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 public:
88 DEFINE_SIZE_ARRAY (4, attachPoint);
89};
90
91/*
92 * Ligature Caret Table
93 */
94
95struct CaretValueFormat1
96{
97 friend struct CaretValue;
98
99 private:
100 inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction) const
101 {
102 return HB_DIRECTION_IS_HORIZONTAL (direction) ? font->em_scale_x (coordinate) : font->em_scale_y (coordinate);
103 }
104
105 inline bool sanitize (hb_sanitize_context_t *c) const
106 {
107 TRACE_SANITIZE (this);
108 return_trace (c->check_struct (this));
109 }
110
111 protected:
112 HBUINT16 caretValueFormat; /* Format identifier--format = 1 */
113 FWORD coordinate; /* X or Y value, in design units */
114 public:
115 DEFINE_SIZE_STATIC (4);
116};
117
118struct CaretValueFormat2
119{
120 friend struct CaretValue;
121
122 private:
123 inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id) const
124 {
125 hb_position_t x, y;
126 if (font->get_glyph_contour_point_for_origin (glyph_id, caretValuePoint, direction, &x, &y))
127 return HB_DIRECTION_IS_HORIZONTAL (direction) ? x : y;
128 else
129 return 0;
130 }
131
132 inline bool sanitize (hb_sanitize_context_t *c) const
133 {
134 TRACE_SANITIZE (this);
135 return_trace (c->check_struct (this));
136 }
137
138 protected:
139 HBUINT16 caretValueFormat; /* Format identifier--format = 2 */
140 HBUINT16 caretValuePoint; /* Contour point index on glyph */
141 public:
142 DEFINE_SIZE_STATIC (4);
143};
144
145struct CaretValueFormat3
146{
147 friend struct CaretValue;
148
149 inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, const VariationStore &var_store) const
150 {
151 return HB_DIRECTION_IS_HORIZONTAL (direction) ?
152 font->em_scale_x (coordinate) + (this+deviceTable).get_x_delta (font, var_store) :
153 font->em_scale_y (coordinate) + (this+deviceTable).get_y_delta (font, var_store);
154 }
155
156 inline bool sanitize (hb_sanitize_context_t *c) const
157 {
158 TRACE_SANITIZE (this);
159 return_trace (c->check_struct (this) && deviceTable.sanitize (c, this));
160 }
161
162 protected:
163 HBUINT16 caretValueFormat; /* Format identifier--format = 3 */
164 FWORD coordinate; /* X or Y value, in design units */
165 OffsetTo<Device>
166 deviceTable; /* Offset to Device table for X or Y
167 * value--from beginning of CaretValue
168 * table */
169 public:
170 DEFINE_SIZE_STATIC (6);
171};
172
173struct CaretValue
174{
175 inline hb_position_t get_caret_value (hb_font_t *font,
176 hb_direction_t direction,
177 hb_codepoint_t glyph_id,
178 const VariationStore &var_store) const
179 {
180 switch (u.format) {
181 case 1: return u.format1.get_caret_value (font, direction);
182 case 2: return u.format2.get_caret_value (font, direction, glyph_id);
183 case 3: return u.format3.get_caret_value (font, direction, var_store);
184 default:return 0;
185 }
186 }
187
188 inline bool sanitize (hb_sanitize_context_t *c) const
189 {
190 TRACE_SANITIZE (this);
191 if (!u.format.sanitize (c)) return_trace (false);
192 switch (u.format) {
193 case 1: return_trace (u.format1.sanitize (c));
194 case 2: return_trace (u.format2.sanitize (c));
195 case 3: return_trace (u.format3.sanitize (c));
196 default:return_trace (true);
197 }
198 }
199
200 protected:
201 union {
202 HBUINT16 format; /* Format identifier */
203 CaretValueFormat1 format1;
204 CaretValueFormat2 format2;
205 CaretValueFormat3 format3;
206 } u;
207 public:
208 DEFINE_SIZE_UNION (2, format);
209};
210
211struct LigGlyph
212{
213 inline unsigned int get_lig_carets (hb_font_t *font,
214 hb_direction_t direction,
215 hb_codepoint_t glyph_id,
216 const VariationStore &var_store,
217 unsigned int start_offset,
218 unsigned int *caret_count /* IN/OUT */,
219 hb_position_t *caret_array /* OUT */) const
220 {
221 if (caret_count) {
222 const OffsetTo<CaretValue> *array = carets.sub_array (start_offset, caret_count);
223 unsigned int count = *caret_count;
224 for (unsigned int i = 0; i < count; i++)
225 caret_array[i] = (this+array[i]).get_caret_value (font, direction, glyph_id, var_store);
226 }
227
228 return carets.len;
229 }
230
231 inline bool sanitize (hb_sanitize_context_t *c) const
232 {
233 TRACE_SANITIZE (this);
234 return_trace (carets.sanitize (c, this));
235 }
236
237 protected:
238 OffsetArrayOf<CaretValue>
239 carets; /* Offset array of CaretValue tables
240 * --from beginning of LigGlyph table
241 * --in increasing coordinate order */
242 public:
243 DEFINE_SIZE_ARRAY (2, carets);
244};
245
246struct LigCaretList
247{
248 inline unsigned int get_lig_carets (hb_font_t *font,
249 hb_direction_t direction,
250 hb_codepoint_t glyph_id,
251 const VariationStore &var_store,
252 unsigned int start_offset,
253 unsigned int *caret_count /* IN/OUT */,
254 hb_position_t *caret_array /* OUT */) const
255 {
256 unsigned int index = (this+coverage).get_coverage (glyph_id);
257 if (index == NOT_COVERED)
258 {
259 if (caret_count)
260 *caret_count = 0;
261 return 0;
262 }
263 const LigGlyph &lig_glyph = this+ligGlyph[index];
264 return lig_glyph.get_lig_carets (font, direction, glyph_id, var_store, start_offset, caret_count, caret_array);
265 }
266
267 inline bool sanitize (hb_sanitize_context_t *c) const
268 {
269 TRACE_SANITIZE (this);
270 return_trace (coverage.sanitize (c, this) && ligGlyph.sanitize (c, this));
271 }
272
273 protected:
274 OffsetTo<Coverage>
275 coverage; /* Offset to Coverage table--from
276 * beginning of LigCaretList table */
277 OffsetArrayOf<LigGlyph>
278 ligGlyph; /* Array of LigGlyph tables
279 * in Coverage Index order */
280 public:
281 DEFINE_SIZE_ARRAY (4, ligGlyph);
282};
283
284
285struct MarkGlyphSetsFormat1
286{
287 inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
288 { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }
289
290 inline bool sanitize (hb_sanitize_context_t *c) const
291 {
292 TRACE_SANITIZE (this);
293 return_trace (coverage.sanitize (c, this));
294 }
295
296 protected:
297 HBUINT16 format; /* Format identifier--format = 1 */
298 ArrayOf<LOffsetTo<Coverage> >
299 coverage; /* Array of long offsets to mark set
300 * coverage tables */
301 public:
302 DEFINE_SIZE_ARRAY (4, coverage);
303};
304
305struct MarkGlyphSets
306{
307 inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
308 {
309 switch (u.format) {
310 case 1: return u.format1.covers (set_index, glyph_id);
311 default:return false;
312 }
313 }
314
315 inline bool sanitize (hb_sanitize_context_t *c) const
316 {
317 TRACE_SANITIZE (this);
318 if (!u.format.sanitize (c)) return_trace (false);
319 switch (u.format) {
320 case 1: return_trace (u.format1.sanitize (c));
321 default:return_trace (true);
322 }
323 }
324
325 protected:
326 union {
327 HBUINT16 format; /* Format identifier */
328 MarkGlyphSetsFormat1 format1;
329 } u;
330 public:
331 DEFINE_SIZE_UNION (2, format);
332};
333
334
335/*
336 * GDEF -- Glyph Definition
337 * https://docs.microsoft.com/en-us/typography/opentype/spec/gdef
338 */
339
340
341struct GDEF
342{
343 static const hb_tag_t tableTag = HB_OT_TAG_GDEF;
344
345 enum GlyphClasses {
346 UnclassifiedGlyph = 0,
347 BaseGlyph = 1,
348 LigatureGlyph = 2,
349 MarkGlyph = 3,
350 ComponentGlyph = 4
351 };
352
353 inline bool has_data (void) const { return version.to_int () != 0; }
354 inline bool has_glyph_classes (void) const { return glyphClassDef != 0; }
355 inline unsigned int get_glyph_class (hb_codepoint_t glyph) const
356 { return (this+glyphClassDef).get_class (glyph); }
357 inline void get_glyphs_in_class (unsigned int klass, hb_set_t *glyphs) const
358 { (this+glyphClassDef).add_class (glyphs, klass); }
359
360 inline bool has_mark_attachment_types (void) const { return markAttachClassDef != 0; }
361 inline unsigned int get_mark_attachment_type (hb_codepoint_t glyph) const
362 { return (this+markAttachClassDef).get_class (glyph); }
363
364 inline bool has_attach_points (void) const { return attachList != 0; }
365 inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
366 unsigned int start_offset,
367 unsigned int *point_count /* IN/OUT */,
368 unsigned int *point_array /* OUT */) const
369 { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
370
371 inline bool has_lig_carets (void) const { return ligCaretList != 0; }
372 inline unsigned int get_lig_carets (hb_font_t *font,
373 hb_direction_t direction,
374 hb_codepoint_t glyph_id,
375 unsigned int start_offset,
376 unsigned int *caret_count /* IN/OUT */,
377 hb_position_t *caret_array /* OUT */) const
378 { return (this+ligCaretList).get_lig_carets (font,
379 direction, glyph_id, get_var_store(),
380 start_offset, caret_count, caret_array); }
381
382 inline bool has_mark_sets (void) const { return version.to_int () >= 0x00010002u && markGlyphSetsDef != 0; }
383 inline bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
384 { return version.to_int () >= 0x00010002u && (this+markGlyphSetsDef).covers (set_index, glyph_id); }
385
386 inline bool has_var_store (void) const { return version.to_int () >= 0x00010003u && varStore != 0; }
387 inline const VariationStore &get_var_store (void) const
388 { return version.to_int () >= 0x00010003u ? this+varStore : Null(VariationStore); }
389
390 /* glyph_props is a 16-bit integer where the lower 8-bit have bits representing
391 * glyph class and other bits, and high 8-bit gthe mark attachment type (if any).
392 * Not to be confused with lookup_props which is very similar. */
393 inline unsigned int get_glyph_props (hb_codepoint_t glyph) const
394 {
395 unsigned int klass = get_glyph_class (glyph);
396
397 static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH == (unsigned int) LookupFlag::IgnoreBaseGlyphs), "");
398 static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE == (unsigned int) LookupFlag::IgnoreLigatures), "");
399 static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_MARK == (unsigned int) LookupFlag::IgnoreMarks), "");
400
401 switch (klass) {
402 default: return 0;
403 case BaseGlyph: return HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH;
404 case LigatureGlyph: return HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
405 case MarkGlyph:
406 klass = get_mark_attachment_type (glyph);
407 return HB_OT_LAYOUT_GLYPH_PROPS_MARK | (klass << 8);
408 }
409 }
410
411 struct accelerator_t
412 {
413 HB_INTERNAL inline void init (hb_face_t *face);
414
415 inline void fini (void)
416 {
417 hb_blob_destroy (this->blob);
418 }
419
420 hb_blob_t *blob;
421 const GDEF *table;
422 };
423
424 inline unsigned int get_size (void) const
425 {
426 return min_size +
427 (version.to_int () >= 0x00010002u ? markGlyphSetsDef.static_size : 0) +
428 (version.to_int () >= 0x00010003u ? varStore.static_size : 0);
429 }
430
431 inline bool sanitize (hb_sanitize_context_t *c) const
432 {
433 TRACE_SANITIZE (this);
434 return_trace (version.sanitize (c) &&
435 likely (version.major == 1) &&
436 glyphClassDef.sanitize (c, this) &&
437 attachList.sanitize (c, this) &&
438 ligCaretList.sanitize (c, this) &&
439 markAttachClassDef.sanitize (c, this) &&
440 (version.to_int () < 0x00010002u || markGlyphSetsDef.sanitize (c, this)) &&
441 (version.to_int () < 0x00010003u || varStore.sanitize (c, this)));
442 }
443
444 protected:
445 FixedVersion<>version; /* Version of the GDEF table--currently
446 * 0x00010003u */
447 OffsetTo<ClassDef>
448 glyphClassDef; /* Offset to class definition table
449 * for glyph type--from beginning of
450 * GDEF header (may be Null) */
451 OffsetTo<AttachList>
452 attachList; /* Offset to list of glyphs with
453 * attachment points--from beginning
454 * of GDEF header (may be Null) */
455 OffsetTo<LigCaretList>
456 ligCaretList; /* Offset to list of positioning points
457 * for ligature carets--from beginning
458 * of GDEF header (may be Null) */
459 OffsetTo<ClassDef>
460 markAttachClassDef; /* Offset to class definition table for
461 * mark attachment type--from beginning
462 * of GDEF header (may be Null) */
463 OffsetTo<MarkGlyphSets>
464 markGlyphSetsDef; /* Offset to the table of mark set
465 * definitions--from beginning of GDEF
466 * header (may be NULL). Introduced
467 * in version 0x00010002. */
468 LOffsetTo<VariationStore>
469 varStore; /* Offset to the table of Item Variation
470 * Store--from beginning of GDEF
471 * header (may be NULL). Introduced
472 * in version 0x00010003. */
473 public:
474 DEFINE_SIZE_MIN (12);
475};
476
477struct GDEF_accelerator_t : GDEF::accelerator_t {};
478
479} /* namespace OT */
480
481
482#endif /* HB_OT_LAYOUT_GDEF_TABLE_HH */
483