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