1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2012,2013  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_PRIVATE_HH
30 #define HB_OT_LAYOUT_PRIVATE_HH
31 
32 #include "hb-private.hh"
33 
34 #include "hb-font-private.hh"
35 #include "hb-buffer-private.hh"
36 #include "hb-set-digest-private.hh"
37 #include "hb-open-type-private.hh"
38 
39 
40 /* Private API corresponding to hb-ot-layout.h: */
41 
42 HB_INTERNAL hb_bool_t
43 hb_ot_layout_table_find_feature (hb_face_t    *face,
44 				 hb_tag_t      table_tag,
45 				 hb_tag_t      feature_tag,
46 				 unsigned int *feature_index);
47 
48 
49 /*
50  * GDEF
51  */
52 
53 enum hb_ot_layout_glyph_props_flags_t
54 {
55   /* The following three match LookupFlags::Ignore* numbers. */
56   HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH	= 0x02u,
57   HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE	= 0x04u,
58   HB_OT_LAYOUT_GLYPH_PROPS_MARK		= 0x08u,
59 
60   /* The following are used internally; not derived from GDEF. */
61   HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED	= 0x10u,
62   HB_OT_LAYOUT_GLYPH_PROPS_LIGATED	= 0x20u,
63   HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED	= 0x40u,
64 
65   HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE     = HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED |
66 					  HB_OT_LAYOUT_GLYPH_PROPS_LIGATED |
67 					  HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED
68 };
69 HB_MARK_AS_FLAG_T (hb_ot_layout_glyph_props_flags_t);
70 
71 
72 /*
73  * GSUB/GPOS
74  */
75 
76 HB_INTERNAL hb_bool_t
77 hb_ot_layout_lookup_would_substitute_fast (hb_face_t            *face,
78 					   unsigned int          lookup_index,
79 					   const hb_codepoint_t *glyphs,
80 					   unsigned int          glyphs_length,
81 					   hb_bool_t             zero_context);
82 
83 
84 /* Should be called before all the substitute_lookup's are done. */
85 HB_INTERNAL void
86 hb_ot_layout_substitute_start (hb_font_t    *font,
87 			       hb_buffer_t  *buffer);
88 
89 
90 struct hb_ot_layout_lookup_accelerator_t;
91 
92 namespace OT {
93   struct hb_ot_apply_context_t;
94   struct SubstLookup;
95 }
96 
97 HB_INTERNAL void
98 hb_ot_layout_substitute_lookup (OT::hb_ot_apply_context_t *c,
99 				const OT::SubstLookup &lookup,
100 				const hb_ot_layout_lookup_accelerator_t &accel);
101 
102 
103 /* Should be called before all the position_lookup's are done. */
104 HB_INTERNAL void
105 hb_ot_layout_position_start (hb_font_t    *font,
106 			     hb_buffer_t  *buffer);
107 
108 /* Should be called after all the position_lookup's are done, to finish advances. */
109 HB_INTERNAL void
110 hb_ot_layout_position_finish_advances (hb_font_t    *font,
111 				       hb_buffer_t  *buffer);
112 
113 /* Should be called after hb_ot_layout_position_finish_advances, to finish offsets. */
114 HB_INTERNAL void
115 hb_ot_layout_position_finish_offsets (hb_font_t    *font,
116 				      hb_buffer_t  *buffer);
117 
118 
119 
120 /*
121  * hb_ot_layout_t
122  */
123 
124 namespace OT {
125   struct BASE;
126   struct COLR;
127   struct CPAL;
128   struct GDEF;
129   struct GSUB;
130   struct GPOS;
131   struct MATH;
132   struct fvar;
133   struct avar;
134 }
135 
136 namespace AAT {
137   struct ankr;
138   struct kerx;
139   struct morx;
140   struct trak;
141 }
142 
143 struct hb_ot_layout_lookup_accelerator_t
144 {
145   template <typename TLookup>
inithb_ot_layout_lookup_accelerator_t146   inline void init (const TLookup &lookup)
147   {
148     digest.init ();
149     lookup.add_coverage (&digest);
150   }
151 
finihb_ot_layout_lookup_accelerator_t152   inline void fini (void)
153   {
154   }
155 
may_havehb_ot_layout_lookup_accelerator_t156   inline bool may_have (hb_codepoint_t g) const {
157     return digest.may_have (g);
158   }
159 
160   private:
161   hb_set_digest_t digest;
162 };
163 
164 struct hb_ot_layout_t
165 {
166   hb_blob_t *gdef_blob;
167   hb_blob_t *gsub_blob;
168   hb_blob_t *gpos_blob;
169 
170   const struct OT::GDEF *gdef;
171   const struct OT::GSUB *gsub;
172   const struct OT::GPOS *gpos;
173 
174   /* TODO Move the following out of this struct. */
175   OT::hb_lazy_table_loader_t<struct OT::BASE> base;
176   OT::hb_lazy_table_loader_t<struct OT::COLR> colr;
177   OT::hb_lazy_table_loader_t<struct OT::CPAL> cpal;
178   OT::hb_lazy_table_loader_t<struct OT::MATH> math;
179   OT::hb_lazy_table_loader_t<struct OT::fvar> fvar;
180   OT::hb_lazy_table_loader_t<struct OT::avar> avar;
181   OT::hb_lazy_table_loader_t<struct AAT::ankr> ankr;
182   OT::hb_lazy_table_loader_t<struct AAT::kerx> kerx;
183   OT::hb_lazy_table_loader_t<struct AAT::morx> morx;
184   OT::hb_lazy_table_loader_t<struct AAT::trak> trak;
185 
186   unsigned int gsub_lookup_count;
187   unsigned int gpos_lookup_count;
188 
189   hb_ot_layout_lookup_accelerator_t *gsub_accels;
190   hb_ot_layout_lookup_accelerator_t *gpos_accels;
191 };
192 
193 
194 HB_INTERNAL hb_ot_layout_t *
195 _hb_ot_layout_create (hb_face_t *face);
196 
197 HB_INTERNAL void
198 _hb_ot_layout_destroy (hb_ot_layout_t *layout);
199 
200 
201 #define hb_ot_layout_from_face(face) ((hb_ot_layout_t *) face->shaper_data.ot)
202 
203 
204 /*
205  * Buffer var routines.
206  */
207 
208 /* buffer var allocations, used during the entire shaping process */
209 #define unicode_props()		var2.u16[0]
210 
211 /* buffer var allocations, used during the GSUB/GPOS processing */
212 #define glyph_props()		var1.u16[0] /* GDEF glyph properties */
213 #define lig_props()		var1.u8[2] /* GSUB/GPOS ligature tracking */
214 #define syllable()		var1.u8[3] /* GSUB/GPOS shaping boundaries */
215 
216 
217 /* Loop over syllables. Based on foreach_cluster(). */
218 #define foreach_syllable(buffer, start, end) \
219   for (unsigned int \
220        _count = buffer->len, \
221        start = 0, end = _count ? _next_syllable (buffer, 0) : 0; \
222        start < _count; \
223        start = end, end = _next_syllable (buffer, start))
224 
225 static inline unsigned int
_next_syllable(hb_buffer_t * buffer,unsigned int start)226 _next_syllable (hb_buffer_t *buffer, unsigned int start)
227 {
228   hb_glyph_info_t *info = buffer->info;
229   unsigned int count = buffer->len;
230 
231   unsigned int syllable = info[start].syllable();
232   while (++start < count && syllable == info[start].syllable())
233     ;
234 
235   return start;
236 }
237 
238 
239 /* unicode_props */
240 
241 /* Design:
242  * unicode_props() is a two-byte number.  The low byte includes:
243  * - General_Category: 5 bits.
244  * - A bit each for:
245  *   * Is it Default_Ignorable(); we have a modified Default_Ignorable().
246  *   * Whether it's one of the three Mongolian Free Variation Selectors,
247  *     CGJ, or other characters that are hidden but should not be ignored
248  *     like most other Default_Ignorable()s do during matching.
249  *   * One free bit right now.
250  *
251  * The high-byte has different meanings, switched by the Gen-Cat:
252  * - For Mn,Mc,Me: the modified Combining_Class.
253  * - For Cf: whether it's ZWJ, ZWNJ, or something else.
254  * - For Ws: index of which space character this is, if space fallback
255  *   is needed, ie. we don't set this by default, only if asked to.
256  */
257 
258 enum hb_unicode_props_flags_t {
259   UPROPS_MASK_GEN_CAT	= 0x001Fu,
260   UPROPS_MASK_IGNORABLE	= 0x0020u,
261   UPROPS_MASK_HIDDEN	= 0x0040u, /* MONGOLIAN FREE VARIATION SELECTOR 1..3,
262                                     * or TAG characters */
263 
264   /* If GEN_CAT=FORMAT, top byte masks: */
265   UPROPS_MASK_Cf_ZWJ	= 0x0100u,
266   UPROPS_MASK_Cf_ZWNJ	= 0x0200u
267 };
268 HB_MARK_AS_FLAG_T (hb_unicode_props_flags_t);
269 
270 static inline void
_hb_glyph_info_set_unicode_props(hb_glyph_info_t * info,hb_buffer_t * buffer)271 _hb_glyph_info_set_unicode_props (hb_glyph_info_t *info, hb_buffer_t *buffer)
272 {
273   hb_unicode_funcs_t *unicode = buffer->unicode;
274   unsigned int u = info->codepoint;
275   unsigned int gen_cat = (unsigned int) unicode->general_category (u);
276   unsigned int props = gen_cat;
277 
278   if (u >= 0x80)
279   {
280     buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII;
281     if (unlikely (unicode->is_default_ignorable (u)))
282     {
283       buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES;
284       props |=  UPROPS_MASK_IGNORABLE;
285       if (u == 0x200Cu) props |= UPROPS_MASK_Cf_ZWNJ;
286       else if (u == 0x200Du) props |= UPROPS_MASK_Cf_ZWJ;
287       /* Mongolian Free Variation Selectors need to be remembered
288        * because although we need to hide them like default-ignorables,
289        * they need to non-ignorable during shaping.  This is similar to
290        * what we do for joiners in Indic-like shapers, but since the
291        * FVSes are GC=Mn, we have use a separate bit to remember them.
292        * Fixes:
293        * https://github.com/harfbuzz/harfbuzz/issues/234 */
294       else if (unlikely (hb_in_range (u, 0x180Bu, 0x180Du))) props |= UPROPS_MASK_HIDDEN;
295       /* TAG characters need similar treatment. Fixes:
296        * https://github.com/harfbuzz/harfbuzz/issues/463 */
297       else if (unlikely (hb_in_range (u, 0xE0020u, 0xE007Fu))) props |= UPROPS_MASK_HIDDEN;
298       /* COMBINING GRAPHEME JOINER should not be skipped; at least some times.
299        * https://github.com/harfbuzz/harfbuzz/issues/554 */
300       else if (unlikely (u == 0x034Fu))
301       {
302 	buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_CGJ;
303 	props |= UPROPS_MASK_HIDDEN;
304       }
305     }
306     else if (unlikely (HB_UNICODE_GENERAL_CATEGORY_IS_NON_ENCLOSING_MARK_OR_MODIFIER_SYMBOL (gen_cat)))
307     {
308       /* The above check is just an optimization to let in only things we need further
309        * processing on. */
310 
311       /* Only Mn and Mc can have non-zero ccc:
312        * http://www.unicode.org/policies/stability_policy.html#Property_Value
313        * """
314        * Canonical_Combining_Class, General_Category
315        * All characters other than those with General_Category property values
316        * Spacing_Mark (Mc) and Nonspacing_Mark (Mn) have the Canonical_Combining_Class
317        * property value 0.
318        * 1.1.5+
319        * """
320        *
321        * Also, all Mn's that are Default_Ignorable, have ccc=0, hence
322        * the "else if".
323        */
324       props |= unicode->modified_combining_class (info->codepoint)<<8;
325 
326       /* Recategorize emoji skin-tone modifiers as Unicode mark, so they
327        * behave correctly in non-native directionality.  They originally
328        * are MODIFIER_SYMBOL.  Fixes:
329        * https://github.com/harfbuzz/harfbuzz/issues/169
330        */
331       if (unlikely (hb_in_range (u, 0x1F3FBu, 0x1F3FFu)))
332       {
333 	props = gen_cat = HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK;
334       }
335     }
336   }
337 
338   info->unicode_props() = props;
339 }
340 
341 static inline void
_hb_glyph_info_set_general_category(hb_glyph_info_t * info,hb_unicode_general_category_t gen_cat)342 _hb_glyph_info_set_general_category (hb_glyph_info_t *info,
343 				     hb_unicode_general_category_t gen_cat)
344 {
345   /* Clears top-byte. */
346   info->unicode_props() = (unsigned int) gen_cat | (info->unicode_props() & (0xFF & ~UPROPS_MASK_GEN_CAT));
347 }
348 
349 static inline hb_unicode_general_category_t
_hb_glyph_info_get_general_category(const hb_glyph_info_t * info)350 _hb_glyph_info_get_general_category (const hb_glyph_info_t *info)
351 {
352   return (hb_unicode_general_category_t) (info->unicode_props() & UPROPS_MASK_GEN_CAT);
353 }
354 
355 static inline bool
_hb_glyph_info_is_unicode_mark(const hb_glyph_info_t * info)356 _hb_glyph_info_is_unicode_mark (const hb_glyph_info_t *info)
357 {
358   return HB_UNICODE_GENERAL_CATEGORY_IS_MARK (info->unicode_props() & UPROPS_MASK_GEN_CAT);
359 }
360 static inline void
_hb_glyph_info_set_modified_combining_class(hb_glyph_info_t * info,unsigned int modified_class)361 _hb_glyph_info_set_modified_combining_class (hb_glyph_info_t *info,
362 					     unsigned int modified_class)
363 {
364   if (unlikely (!_hb_glyph_info_is_unicode_mark (info)))
365     return;
366   info->unicode_props() = (modified_class<<8) | (info->unicode_props() & 0xFF);
367 }
368 static inline unsigned int
_hb_glyph_info_get_modified_combining_class(const hb_glyph_info_t * info)369 _hb_glyph_info_get_modified_combining_class (const hb_glyph_info_t *info)
370 {
371   return _hb_glyph_info_is_unicode_mark (info) ? info->unicode_props()>>8 : 0;
372 }
373 
374 
375 /* Loop over grapheme. Based on foreach_cluster(). */
376 #define foreach_grapheme(buffer, start, end) \
377   for (unsigned int \
378        _count = buffer->len, \
379        start = 0, end = _count ? _next_grapheme (buffer, 0) : 0; \
380        start < _count; \
381        start = end, end = _next_grapheme (buffer, start))
382 
383 static inline unsigned int
_next_grapheme(hb_buffer_t * buffer,unsigned int start)384 _next_grapheme (hb_buffer_t *buffer, unsigned int start)
385 {
386   hb_glyph_info_t *info = buffer->info;
387   unsigned int count = buffer->len;
388 
389   while (++start < count && _hb_glyph_info_is_unicode_mark (&info[start]))
390     ;
391 
392   return start;
393 }
394 
395 
396 #define info_cc(info) (_hb_glyph_info_get_modified_combining_class (&(info)))
397 
398 static inline bool
_hb_glyph_info_is_unicode_space(const hb_glyph_info_t * info)399 _hb_glyph_info_is_unicode_space (const hb_glyph_info_t *info)
400 {
401   return _hb_glyph_info_get_general_category (info) ==
402 	 HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR;
403 }
404 static inline void
_hb_glyph_info_set_unicode_space_fallback_type(hb_glyph_info_t * info,hb_unicode_funcs_t::space_t s)405 _hb_glyph_info_set_unicode_space_fallback_type (hb_glyph_info_t *info, hb_unicode_funcs_t::space_t s)
406 {
407   if (unlikely (!_hb_glyph_info_is_unicode_space (info)))
408     return;
409   info->unicode_props() = (((unsigned int) s)<<8) | (info->unicode_props() & 0xFF);
410 }
411 static inline hb_unicode_funcs_t::space_t
_hb_glyph_info_get_unicode_space_fallback_type(const hb_glyph_info_t * info)412 _hb_glyph_info_get_unicode_space_fallback_type (const hb_glyph_info_t *info)
413 {
414   return _hb_glyph_info_is_unicode_space (info) ?
415 	 (hb_unicode_funcs_t::space_t) (info->unicode_props()>>8) :
416 	 hb_unicode_funcs_t::NOT_SPACE;
417 }
418 
419 static inline bool _hb_glyph_info_ligated (const hb_glyph_info_t *info);
420 
421 static inline hb_bool_t
_hb_glyph_info_is_default_ignorable(const hb_glyph_info_t * info)422 _hb_glyph_info_is_default_ignorable (const hb_glyph_info_t *info)
423 {
424   return (info->unicode_props() & UPROPS_MASK_IGNORABLE) &&
425 	 !_hb_glyph_info_ligated (info);
426 }
427 static inline hb_bool_t
_hb_glyph_info_is_default_ignorable_and_not_hidden(const hb_glyph_info_t * info)428 _hb_glyph_info_is_default_ignorable_and_not_hidden (const hb_glyph_info_t *info)
429 {
430   return ((info->unicode_props() & (UPROPS_MASK_IGNORABLE|UPROPS_MASK_HIDDEN))
431 	  == UPROPS_MASK_IGNORABLE) &&
432 	 !_hb_glyph_info_ligated (info);
433 }
434 static inline void
_hb_glyph_info_unhide(hb_glyph_info_t * info)435 _hb_glyph_info_unhide (hb_glyph_info_t *info)
436 {
437   info->unicode_props() &= ~ UPROPS_MASK_HIDDEN;
438 }
439 
440 static inline bool
_hb_glyph_info_is_unicode_format(const hb_glyph_info_t * info)441 _hb_glyph_info_is_unicode_format (const hb_glyph_info_t *info)
442 {
443   return _hb_glyph_info_get_general_category (info) ==
444 	 HB_UNICODE_GENERAL_CATEGORY_FORMAT;
445 }
446 static inline hb_bool_t
_hb_glyph_info_is_zwnj(const hb_glyph_info_t * info)447 _hb_glyph_info_is_zwnj (const hb_glyph_info_t *info)
448 {
449   return _hb_glyph_info_is_unicode_format (info) && (info->unicode_props() & UPROPS_MASK_Cf_ZWNJ);
450 }
451 static inline hb_bool_t
_hb_glyph_info_is_zwj(const hb_glyph_info_t * info)452 _hb_glyph_info_is_zwj (const hb_glyph_info_t *info)
453 {
454   return _hb_glyph_info_is_unicode_format (info) && (info->unicode_props() & UPROPS_MASK_Cf_ZWJ);
455 }
456 static inline hb_bool_t
_hb_glyph_info_is_joiner(const hb_glyph_info_t * info)457 _hb_glyph_info_is_joiner (const hb_glyph_info_t *info)
458 {
459   return _hb_glyph_info_is_unicode_format (info) && (info->unicode_props() & (UPROPS_MASK_Cf_ZWNJ|UPROPS_MASK_Cf_ZWJ));
460 }
461 static inline void
_hb_glyph_info_flip_joiners(hb_glyph_info_t * info)462 _hb_glyph_info_flip_joiners (hb_glyph_info_t *info)
463 {
464   if (!_hb_glyph_info_is_unicode_format (info))
465     return;
466   info->unicode_props() ^= UPROPS_MASK_Cf_ZWNJ | UPROPS_MASK_Cf_ZWJ;
467 }
468 
469 /* lig_props: aka lig_id / lig_comp
470  *
471  * When a ligature is formed:
472  *
473  *   - The ligature glyph and any marks in between all the same newly allocated
474  *     lig_id,
475  *   - The ligature glyph will get lig_num_comps set to the number of components
476  *   - The marks get lig_comp > 0, reflecting which component of the ligature
477  *     they were applied to.
478  *   - This is used in GPOS to attach marks to the right component of a ligature
479  *     in MarkLigPos,
480  *   - Note that when marks are ligated together, much of the above is skipped
481  *     and the current lig_id reused.
482  *
483  * When a multiple-substitution is done:
484  *
485  *   - All resulting glyphs will have lig_id = 0,
486  *   - The resulting glyphs will have lig_comp = 0, 1, 2, ... respectively.
487  *   - This is used in GPOS to attach marks to the first component of a
488  *     multiple substitution in MarkBasePos.
489  *
490  * The numbers are also used in GPOS to do mark-to-mark positioning only
491  * to marks that belong to the same component of the same ligature.
492  */
493 
494 static inline void
_hb_glyph_info_clear_lig_props(hb_glyph_info_t * info)495 _hb_glyph_info_clear_lig_props (hb_glyph_info_t *info)
496 {
497   info->lig_props() = 0;
498 }
499 
500 #define IS_LIG_BASE 0x10
501 
502 static inline void
_hb_glyph_info_set_lig_props_for_ligature(hb_glyph_info_t * info,unsigned int lig_id,unsigned int lig_num_comps)503 _hb_glyph_info_set_lig_props_for_ligature (hb_glyph_info_t *info,
504 					   unsigned int lig_id,
505 					   unsigned int lig_num_comps)
506 {
507   info->lig_props() = (lig_id << 5) | IS_LIG_BASE | (lig_num_comps & 0x0F);
508 }
509 
510 static inline void
_hb_glyph_info_set_lig_props_for_mark(hb_glyph_info_t * info,unsigned int lig_id,unsigned int lig_comp)511 _hb_glyph_info_set_lig_props_for_mark (hb_glyph_info_t *info,
512 				       unsigned int lig_id,
513 				       unsigned int lig_comp)
514 {
515   info->lig_props() = (lig_id << 5) | (lig_comp & 0x0F);
516 }
517 
518 static inline void
_hb_glyph_info_set_lig_props_for_component(hb_glyph_info_t * info,unsigned int comp)519 _hb_glyph_info_set_lig_props_for_component (hb_glyph_info_t *info, unsigned int comp)
520 {
521   _hb_glyph_info_set_lig_props_for_mark (info, 0, comp);
522 }
523 
524 static inline unsigned int
_hb_glyph_info_get_lig_id(const hb_glyph_info_t * info)525 _hb_glyph_info_get_lig_id (const hb_glyph_info_t *info)
526 {
527   return info->lig_props() >> 5;
528 }
529 
530 static inline bool
_hb_glyph_info_ligated_internal(const hb_glyph_info_t * info)531 _hb_glyph_info_ligated_internal (const hb_glyph_info_t *info)
532 {
533   return !!(info->lig_props() & IS_LIG_BASE);
534 }
535 
536 static inline unsigned int
_hb_glyph_info_get_lig_comp(const hb_glyph_info_t * info)537 _hb_glyph_info_get_lig_comp (const hb_glyph_info_t *info)
538 {
539   if (_hb_glyph_info_ligated_internal (info))
540     return 0;
541   else
542     return info->lig_props() & 0x0F;
543 }
544 
545 static inline unsigned int
_hb_glyph_info_get_lig_num_comps(const hb_glyph_info_t * info)546 _hb_glyph_info_get_lig_num_comps (const hb_glyph_info_t *info)
547 {
548   if ((info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE) &&
549       _hb_glyph_info_ligated_internal (info))
550     return info->lig_props() & 0x0F;
551   else
552     return 1;
553 }
554 
555 static inline uint8_t
_hb_allocate_lig_id(hb_buffer_t * buffer)556 _hb_allocate_lig_id (hb_buffer_t *buffer) {
557   uint8_t lig_id = buffer->next_serial () & 0x07;
558   if (unlikely (!lig_id))
559     lig_id = _hb_allocate_lig_id (buffer); /* in case of overflow */
560   return lig_id;
561 }
562 
563 /* glyph_props: */
564 
565 static inline void
_hb_glyph_info_set_glyph_props(hb_glyph_info_t * info,unsigned int props)566 _hb_glyph_info_set_glyph_props (hb_glyph_info_t *info, unsigned int props)
567 {
568   info->glyph_props() = props;
569 }
570 
571 static inline unsigned int
_hb_glyph_info_get_glyph_props(const hb_glyph_info_t * info)572 _hb_glyph_info_get_glyph_props (const hb_glyph_info_t *info)
573 {
574   return info->glyph_props();
575 }
576 
577 static inline bool
_hb_glyph_info_is_base_glyph(const hb_glyph_info_t * info)578 _hb_glyph_info_is_base_glyph (const hb_glyph_info_t *info)
579 {
580   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH);
581 }
582 
583 static inline bool
_hb_glyph_info_is_ligature(const hb_glyph_info_t * info)584 _hb_glyph_info_is_ligature (const hb_glyph_info_t *info)
585 {
586   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE);
587 }
588 
589 static inline bool
_hb_glyph_info_is_mark(const hb_glyph_info_t * info)590 _hb_glyph_info_is_mark (const hb_glyph_info_t *info)
591 {
592   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_MARK);
593 }
594 
595 static inline bool
_hb_glyph_info_substituted(const hb_glyph_info_t * info)596 _hb_glyph_info_substituted (const hb_glyph_info_t *info)
597 {
598   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED);
599 }
600 
601 static inline bool
_hb_glyph_info_ligated(const hb_glyph_info_t * info)602 _hb_glyph_info_ligated (const hb_glyph_info_t *info)
603 {
604   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_LIGATED);
605 }
606 
607 static inline bool
_hb_glyph_info_multiplied(const hb_glyph_info_t * info)608 _hb_glyph_info_multiplied (const hb_glyph_info_t *info)
609 {
610   return !!(info->glyph_props() & HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED);
611 }
612 
613 static inline bool
_hb_glyph_info_ligated_and_didnt_multiply(const hb_glyph_info_t * info)614 _hb_glyph_info_ligated_and_didnt_multiply (const hb_glyph_info_t *info)
615 {
616   return _hb_glyph_info_ligated (info) && !_hb_glyph_info_multiplied (info);
617 }
618 
619 static inline void
_hb_glyph_info_clear_ligated_and_multiplied(hb_glyph_info_t * info)620 _hb_glyph_info_clear_ligated_and_multiplied (hb_glyph_info_t *info)
621 {
622   info->glyph_props() &= ~(HB_OT_LAYOUT_GLYPH_PROPS_LIGATED |
623 			   HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED);
624 }
625 
626 static inline void
_hb_glyph_info_clear_substituted(hb_glyph_info_t * info)627 _hb_glyph_info_clear_substituted (hb_glyph_info_t *info)
628 {
629   info->glyph_props() &= ~(HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED);
630 }
631 
632 
633 /* Allocation / deallocation. */
634 
635 static inline void
_hb_buffer_allocate_unicode_vars(hb_buffer_t * buffer)636 _hb_buffer_allocate_unicode_vars (hb_buffer_t *buffer)
637 {
638   HB_BUFFER_ALLOCATE_VAR (buffer, unicode_props);
639 }
640 
641 static inline void
_hb_buffer_deallocate_unicode_vars(hb_buffer_t * buffer)642 _hb_buffer_deallocate_unicode_vars (hb_buffer_t *buffer)
643 {
644   HB_BUFFER_DEALLOCATE_VAR (buffer, unicode_props);
645 }
646 
647 static inline void
_hb_buffer_assert_unicode_vars(hb_buffer_t * buffer)648 _hb_buffer_assert_unicode_vars (hb_buffer_t *buffer)
649 {
650   HB_BUFFER_ASSERT_VAR (buffer, unicode_props);
651 }
652 
653 static inline void
_hb_buffer_allocate_gsubgpos_vars(hb_buffer_t * buffer)654 _hb_buffer_allocate_gsubgpos_vars (hb_buffer_t *buffer)
655 {
656   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_props);
657   HB_BUFFER_ALLOCATE_VAR (buffer, lig_props);
658   HB_BUFFER_ALLOCATE_VAR (buffer, syllable);
659 }
660 
661 static inline void
_hb_buffer_deallocate_gsubgpos_vars(hb_buffer_t * buffer)662 _hb_buffer_deallocate_gsubgpos_vars (hb_buffer_t *buffer)
663 {
664   HB_BUFFER_DEALLOCATE_VAR (buffer, syllable);
665   HB_BUFFER_DEALLOCATE_VAR (buffer, lig_props);
666   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_props);
667 }
668 
669 static inline void
_hb_buffer_assert_gsubgpos_vars(hb_buffer_t * buffer)670 _hb_buffer_assert_gsubgpos_vars (hb_buffer_t *buffer)
671 {
672   HB_BUFFER_ASSERT_VAR (buffer, glyph_props);
673   HB_BUFFER_ASSERT_VAR (buffer, lig_props);
674   HB_BUFFER_ASSERT_VAR (buffer, syllable);
675 }
676 
677 /* Make sure no one directly touches our props... */
678 #undef unicode_props0
679 #undef unicode_props1
680 #undef lig_props
681 #undef glyph_props
682 
683 #endif /* HB_OT_LAYOUT_PRIVATE_HH */
684