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