• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
37 namespace OT {
38 
39 
40 /*
41  * Attachment List Table
42  */
43 
44 typedef ArrayOf<HBUINT16> AttachPoint;	/* Array of contour point indices--in
45 					 * increasing numerical order */
46 
47 struct AttachList
48 {
get_attach_pointsOT::AttachList49   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     {
66       hb_array_t<const HBUINT16> array = points.sub_array (start_offset, point_count);
67       unsigned int count = array.len;
68       for (unsigned int i = 0; i < count; i++)
69 	point_array[i] = array[i];
70     }
71 
72     return points.len;
73   }
74 
sanitizeOT::AttachList75   bool sanitize (hb_sanitize_context_t *c) const
76   {
77     TRACE_SANITIZE (this);
78     return_trace (coverage.sanitize (c, this) && attachPoint.sanitize (c, this));
79   }
80 
81   protected:
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   public:
89   DEFINE_SIZE_ARRAY (4, attachPoint);
90 };
91 
92 /*
93  * Ligature Caret Table
94  */
95 
96 struct CaretValueFormat1
97 {
98   friend struct CaretValue;
99 
100   private:
get_caret_valueOT::CaretValueFormat1101   hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction) const
102   {
103     return HB_DIRECTION_IS_HORIZONTAL (direction) ? font->em_scale_x (coordinate) : font->em_scale_y (coordinate);
104   }
105 
sanitizeOT::CaretValueFormat1106   bool sanitize (hb_sanitize_context_t *c) const
107   {
108     TRACE_SANITIZE (this);
109     return_trace (c->check_struct (this));
110   }
111 
112   protected:
113   HBUINT16	caretValueFormat;	/* Format identifier--format = 1 */
114   FWORD		coordinate;		/* X or Y value, in design units */
115   public:
116   DEFINE_SIZE_STATIC (4);
117 };
118 
119 struct CaretValueFormat2
120 {
121   friend struct CaretValue;
122 
123   private:
get_caret_valueOT::CaretValueFormat2124   hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id) const
125   {
126     hb_position_t x, y;
127     font->get_glyph_contour_point_for_origin (glyph_id, caretValuePoint, direction, &x, &y);
128     return HB_DIRECTION_IS_HORIZONTAL (direction) ? x : y;
129   }
130 
sanitizeOT::CaretValueFormat2131   bool sanitize (hb_sanitize_context_t *c) const
132   {
133     TRACE_SANITIZE (this);
134     return_trace (c->check_struct (this));
135   }
136 
137   protected:
138   HBUINT16	caretValueFormat;	/* Format identifier--format = 2 */
139   HBUINT16	caretValuePoint;	/* Contour point index on glyph */
140   public:
141   DEFINE_SIZE_STATIC (4);
142 };
143 
144 struct CaretValueFormat3
145 {
146   friend struct CaretValue;
147 
get_caret_valueOT::CaretValueFormat3148   hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction,
149 				 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 
sanitizeOT::CaretValueFormat3156   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 
173 struct CaretValue
174 {
get_caret_valueOT::CaretValue175   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 
sanitizeOT::CaretValue188   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 
211 struct LigGlyph
212 {
get_lig_caretsOT::LigGlyph213   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     {
223       hb_array_t <const OffsetTo<CaretValue> > array = carets.sub_array (start_offset, caret_count);
224       unsigned int count = array.len;
225       for (unsigned int i = 0; i < count; i++)
226 	caret_array[i] = (this+array[i]).get_caret_value (font, direction, glyph_id, var_store);
227     }
228 
229     return carets.len;
230   }
231 
sanitizeOT::LigGlyph232   bool sanitize (hb_sanitize_context_t *c) const
233   {
234     TRACE_SANITIZE (this);
235     return_trace (carets.sanitize (c, this));
236   }
237 
238   protected:
239   OffsetArrayOf<CaretValue>
240 		carets;			/* Offset array of CaretValue tables
241 					 * --from beginning of LigGlyph table
242 					 * --in increasing coordinate order */
243   public:
244   DEFINE_SIZE_ARRAY (2, carets);
245 };
246 
247 struct LigCaretList
248 {
get_lig_caretsOT::LigCaretList249   unsigned int get_lig_carets (hb_font_t *font,
250 			       hb_direction_t direction,
251 			       hb_codepoint_t glyph_id,
252 			       const VariationStore &var_store,
253 			       unsigned int start_offset,
254 			       unsigned int *caret_count /* IN/OUT */,
255 			       hb_position_t *caret_array /* OUT */) const
256   {
257     unsigned int index = (this+coverage).get_coverage (glyph_id);
258     if (index == NOT_COVERED)
259     {
260       if (caret_count)
261 	*caret_count = 0;
262       return 0;
263     }
264     const LigGlyph &lig_glyph = this+ligGlyph[index];
265     return lig_glyph.get_lig_carets (font, direction, glyph_id, var_store, start_offset, caret_count, caret_array);
266   }
267 
sanitizeOT::LigCaretList268   bool sanitize (hb_sanitize_context_t *c) const
269   {
270     TRACE_SANITIZE (this);
271     return_trace (coverage.sanitize (c, this) && ligGlyph.sanitize (c, this));
272   }
273 
274   protected:
275   OffsetTo<Coverage>
276 		coverage;		/* Offset to Coverage table--from
277 					 * beginning of LigCaretList table */
278   OffsetArrayOf<LigGlyph>
279 		ligGlyph;		/* Array of LigGlyph tables
280 					 * in Coverage Index order */
281   public:
282   DEFINE_SIZE_ARRAY (4, ligGlyph);
283 };
284 
285 
286 struct MarkGlyphSetsFormat1
287 {
coversOT::MarkGlyphSetsFormat1288   bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
289   { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }
290 
sanitizeOT::MarkGlyphSetsFormat1291   bool sanitize (hb_sanitize_context_t *c) const
292   {
293     TRACE_SANITIZE (this);
294     return_trace (coverage.sanitize (c, this));
295   }
296 
297   protected:
298   HBUINT16	format;			/* Format identifier--format = 1 */
299   ArrayOf<LOffsetTo<Coverage> >
300 		coverage;		/* Array of long offsets to mark set
301 					 * coverage tables */
302   public:
303   DEFINE_SIZE_ARRAY (4, coverage);
304 };
305 
306 struct MarkGlyphSets
307 {
coversOT::MarkGlyphSets308   bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
309   {
310     switch (u.format) {
311     case 1: return u.format1.covers (set_index, glyph_id);
312     default:return false;
313     }
314   }
315 
sanitizeOT::MarkGlyphSets316   bool sanitize (hb_sanitize_context_t *c) const
317   {
318     TRACE_SANITIZE (this);
319     if (!u.format.sanitize (c)) return_trace (false);
320     switch (u.format) {
321     case 1: return_trace (u.format1.sanitize (c));
322     default:return_trace (true);
323     }
324   }
325 
326   protected:
327   union {
328   HBUINT16		format;		/* Format identifier */
329   MarkGlyphSetsFormat1	format1;
330   } u;
331   public:
332   DEFINE_SIZE_UNION (2, format);
333 };
334 
335 
336 /*
337  * GDEF -- Glyph Definition
338  * https://docs.microsoft.com/en-us/typography/opentype/spec/gdef
339  */
340 
341 
342 struct GDEF
343 {
344   enum { tableTag = HB_OT_TAG_GDEF };
345 
346   enum GlyphClasses {
347     UnclassifiedGlyph	= 0,
348     BaseGlyph		= 1,
349     LigatureGlyph	= 2,
350     MarkGlyph		= 3,
351     ComponentGlyph	= 4
352   };
353 
has_dataOT::GDEF354   bool has_data () const { return version.to_int (); }
has_glyph_classesOT::GDEF355   bool has_glyph_classes () const { return glyphClassDef != 0; }
get_glyph_classOT::GDEF356   unsigned int get_glyph_class (hb_codepoint_t glyph) const
357   { return (this+glyphClassDef).get_class (glyph); }
get_glyphs_in_classOT::GDEF358   void get_glyphs_in_class (unsigned int klass, hb_set_t *glyphs) const
359   { (this+glyphClassDef).add_class (glyphs, klass); }
360 
has_mark_attachment_typesOT::GDEF361   bool has_mark_attachment_types () const { return markAttachClassDef != 0; }
get_mark_attachment_typeOT::GDEF362   unsigned int get_mark_attachment_type (hb_codepoint_t glyph) const
363   { return (this+markAttachClassDef).get_class (glyph); }
364 
has_attach_pointsOT::GDEF365   bool has_attach_points () const { return attachList != 0; }
get_attach_pointsOT::GDEF366   unsigned int get_attach_points (hb_codepoint_t glyph_id,
367 				  unsigned int start_offset,
368 				  unsigned int *point_count /* IN/OUT */,
369 				  unsigned int *point_array /* OUT */) const
370   { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
371 
has_lig_caretsOT::GDEF372   bool has_lig_carets () const { return ligCaretList != 0; }
get_lig_caretsOT::GDEF373   unsigned int get_lig_carets (hb_font_t *font,
374 			       hb_direction_t direction,
375 			       hb_codepoint_t glyph_id,
376 			       unsigned int start_offset,
377 			       unsigned int *caret_count /* IN/OUT */,
378 			       hb_position_t *caret_array /* OUT */) const
379   { return (this+ligCaretList).get_lig_carets (font,
380 					       direction, glyph_id, get_var_store(),
381 					       start_offset, caret_count, caret_array); }
382 
has_mark_setsOT::GDEF383   bool has_mark_sets () const { return version.to_int () >= 0x00010002u && markGlyphSetsDef != 0; }
mark_set_coversOT::GDEF384   bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
385   { return version.to_int () >= 0x00010002u && (this+markGlyphSetsDef).covers (set_index, glyph_id); }
386 
has_var_storeOT::GDEF387   bool has_var_store () const { return version.to_int () >= 0x00010003u && varStore != 0; }
get_var_storeOT::GDEF388   const VariationStore &get_var_store () const
389   { return version.to_int () >= 0x00010003u ? this+varStore : Null(VariationStore); }
390 
391   /* glyph_props is a 16-bit integer where the lower 8-bit have bits representing
392    * glyph class and other bits, and high 8-bit the mark attachment type (if any).
393    * Not to be confused with lookup_props which is very similar. */
get_glyph_propsOT::GDEF394   unsigned int get_glyph_props (hb_codepoint_t glyph) const
395   {
396     unsigned int klass = get_glyph_class (glyph);
397 
398     static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH == (unsigned int) LookupFlag::IgnoreBaseGlyphs), "");
399     static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE == (unsigned int) LookupFlag::IgnoreLigatures), "");
400     static_assert (((unsigned int) HB_OT_LAYOUT_GLYPH_PROPS_MARK == (unsigned int) LookupFlag::IgnoreMarks), "");
401 
402     switch (klass) {
403     default:			return 0;
404     case BaseGlyph:		return HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH;
405     case LigatureGlyph:		return HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
406     case MarkGlyph:
407 	  klass = get_mark_attachment_type (glyph);
408 	  return HB_OT_LAYOUT_GLYPH_PROPS_MARK | (klass << 8);
409     }
410   }
411 
412   HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
413 				   hb_face_t *face) const;
414 
415   struct accelerator_t
416   {
initOT::GDEF::accelerator_t417     void init (hb_face_t *face)
418     {
419       this->table = hb_sanitize_context_t().reference_table<GDEF> (face);
420       if (unlikely (this->table->is_blacklisted (this->table.get_blob (), face)))
421       {
422 	hb_blob_destroy (this->table.get_blob ());
423 	this->table = hb_blob_get_empty ();
424       }
425     }
426 
finiOT::GDEF::accelerator_t427     void fini () { this->table.destroy (); }
428 
429     hb_blob_ptr_t<GDEF> table;
430   };
431 
get_sizeOT::GDEF432   unsigned int get_size () const
433   {
434     return min_size +
435 	   (version.to_int () >= 0x00010002u ? markGlyphSetsDef.static_size : 0) +
436 	   (version.to_int () >= 0x00010003u ? varStore.static_size : 0);
437   }
438 
subsetOT::GDEF439   bool subset (hb_subset_context_t *c) const
440   {
441     TRACE_SUBSET (this);
442     struct GDEF *out = c->serializer->embed (*this);
443     if (unlikely (!out)) return_trace (false);
444 
445     out->glyphClassDef.serialize_subset (c, this+glyphClassDef, out);
446     out->attachList.set (0);//TODO(subset) serialize_subset (c, this+attachList, out);
447     out->ligCaretList.set (0);//TODO(subset) serialize_subset (c, this+ligCaretList, out);
448     out->markAttachClassDef.serialize_subset (c, this+markAttachClassDef, out);
449 
450     if (version.to_int () >= 0x00010002u)
451       out->markGlyphSetsDef.set (0);// TODO(subset) serialize_subset (c, this+markGlyphSetsDef, out);
452 
453     if (version.to_int () >= 0x00010003u)
454       out->varStore.set (0);// TODO(subset) serialize_subset (c, this+varStore, out);
455 
456     return_trace (true);
457   }
458 
sanitizeOT::GDEF459   bool sanitize (hb_sanitize_context_t *c) const
460   {
461     TRACE_SANITIZE (this);
462     return_trace (version.sanitize (c) &&
463 		  likely (version.major == 1) &&
464 		  glyphClassDef.sanitize (c, this) &&
465 		  attachList.sanitize (c, this) &&
466 		  ligCaretList.sanitize (c, this) &&
467 		  markAttachClassDef.sanitize (c, this) &&
468 		  (version.to_int () < 0x00010002u || markGlyphSetsDef.sanitize (c, this)) &&
469 		  (version.to_int () < 0x00010003u || varStore.sanitize (c, this)));
470   }
471 
472   protected:
473   FixedVersion<>version;		/* Version of the GDEF table--currently
474 					 * 0x00010003u */
475   OffsetTo<ClassDef>
476 		glyphClassDef;		/* Offset to class definition table
477 					 * for glyph type--from beginning of
478 					 * GDEF header (may be Null) */
479   OffsetTo<AttachList>
480 		attachList;		/* Offset to list of glyphs with
481 					 * attachment points--from beginning
482 					 * of GDEF header (may be Null) */
483   OffsetTo<LigCaretList>
484 		ligCaretList;		/* Offset to list of positioning points
485 					 * for ligature carets--from beginning
486 					 * of GDEF header (may be Null) */
487   OffsetTo<ClassDef>
488 		markAttachClassDef;	/* Offset to class definition table for
489 					 * mark attachment type--from beginning
490 					 * of GDEF header (may be Null) */
491   OffsetTo<MarkGlyphSets>
492 		markGlyphSetsDef;	/* Offset to the table of mark set
493 					 * definitions--from beginning of GDEF
494 					 * header (may be NULL).  Introduced
495 					 * in version 0x00010002. */
496   LOffsetTo<VariationStore>
497 		varStore;		/* Offset to the table of Item Variation
498 					 * Store--from beginning of GDEF
499 					 * header (may be NULL).  Introduced
500 					 * in version 0x00010003. */
501   public:
502   DEFINE_SIZE_MIN (12);
503 };
504 
505 struct GDEF_accelerator_t : GDEF::accelerator_t {};
506 
507 } /* namespace OT */
508 
509 
510 #endif /* HB_OT_LAYOUT_GDEF_TABLE_HH */
511