hb-ot-shape.cc revision e3e4bb011ae1a2f1ba05e7ea450595b185304bec
1/*
2 * Copyright © 2009,2010  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#define HB_SHAPER ot
30#define hb_ot_shaper_face_data_t hb_ot_layout_t
31#define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
32#include "hb-shaper-impl-private.hh"
33
34#include "hb-ot-shape-private.hh"
35#include "hb-ot-shape-complex-private.hh"
36#include "hb-ot-shape-fallback-private.hh"
37#include "hb-ot-shape-normalize-private.hh"
38
39#include "hb-ot-layout-private.hh"
40#include "hb-unicode-private.hh"
41#include "hb-set-private.hh"
42
43
44static hb_tag_t common_features[] = {
45  HB_TAG('c','c','m','p'),
46  HB_TAG('l','o','c','l'),
47  HB_TAG('m','a','r','k'),
48  HB_TAG('m','k','m','k'),
49  HB_TAG('r','l','i','g'),
50};
51
52
53static hb_tag_t horizontal_features[] = {
54  HB_TAG('c','a','l','t'),
55  HB_TAG('c','l','i','g'),
56  HB_TAG('c','u','r','s'),
57  HB_TAG('k','e','r','n'),
58  HB_TAG('l','i','g','a'),
59  HB_TAG('r','c','l','t'),
60};
61
62
63
64static void
65hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
66			      const hb_segment_properties_t  *props,
67			      const hb_feature_t             *user_features,
68			      unsigned int                    num_user_features)
69{
70  hb_ot_map_builder_t *map = &planner->map;
71
72  switch (props->direction) {
73    case HB_DIRECTION_LTR:
74      map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
75      map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
76      break;
77    case HB_DIRECTION_RTL:
78      map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
79      map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
80      break;
81    case HB_DIRECTION_TTB:
82    case HB_DIRECTION_BTT:
83    case HB_DIRECTION_INVALID:
84    default:
85      break;
86  }
87
88  map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
89  map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
90  map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
91
92  if (planner->shaper->collect_features)
93    planner->shaper->collect_features (planner);
94
95  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
96    map->add_global_bool_feature (common_features[i]);
97
98  if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
99    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
100      map->add_feature (horizontal_features[i], 1, F_GLOBAL |
101			(horizontal_features[i] == HB_TAG('k','e','r','n') ?
102			 F_HAS_FALLBACK : F_NONE));
103  else
104  {
105    /* We really want to find a 'vert' feature if there's any in the font, no
106     * matter which script/langsys it is listed (or not) under.
107     * See various bugs referenced from:
108     * https://github.com/behdad/harfbuzz/issues/63 */
109    map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
110  }
111
112  if (planner->shaper->override_features)
113    planner->shaper->override_features (planner);
114
115  for (unsigned int i = 0; i < num_user_features; i++) {
116    const hb_feature_t *feature = &user_features[i];
117    map->add_feature (feature->tag, feature->value,
118		      (feature->start == 0 && feature->end == (unsigned int) -1) ?
119		       F_GLOBAL : F_NONE);
120  }
121}
122
123
124/*
125 * shaper face data
126 */
127
128hb_ot_shaper_face_data_t *
129_hb_ot_shaper_face_data_create (hb_face_t *face)
130{
131  return _hb_ot_layout_create (face);
132}
133
134void
135_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
136{
137  _hb_ot_layout_destroy (data);
138}
139
140
141/*
142 * shaper font data
143 */
144
145struct hb_ot_shaper_font_data_t {};
146
147hb_ot_shaper_font_data_t *
148_hb_ot_shaper_font_data_create (hb_font_t *font)
149{
150  return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
151}
152
153void
154_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
155{
156}
157
158
159/*
160 * shaper shape_plan data
161 */
162
163hb_ot_shaper_shape_plan_data_t *
164_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
165				      const hb_feature_t *user_features,
166				      unsigned int        num_user_features)
167{
168  hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
169  if (unlikely (!plan))
170    return NULL;
171
172  hb_ot_shape_planner_t planner (shape_plan);
173
174  planner.shaper = hb_ot_shape_complex_categorize (&planner);
175
176  hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
177
178  planner.compile (*plan);
179
180  if (plan->shaper->data_create) {
181    plan->data = plan->shaper->data_create (plan);
182    if (unlikely (!plan->data))
183      return NULL;
184  }
185
186  return plan;
187}
188
189void
190_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
191{
192  if (plan->shaper->data_destroy)
193    plan->shaper->data_destroy (const_cast<void *> (plan->data));
194
195  plan->finish ();
196
197  free (plan);
198}
199
200
201/*
202 * shaper
203 */
204
205struct hb_ot_shape_context_t
206{
207  hb_ot_shape_plan_t *plan;
208  hb_font_t *font;
209  hb_face_t *face;
210  hb_buffer_t  *buffer;
211  const hb_feature_t *user_features;
212  unsigned int        num_user_features;
213
214  /* Transient stuff */
215  hb_direction_t target_direction;
216};
217
218
219
220/* Main shaper */
221
222
223/* Prepare */
224
225static void
226hb_set_unicode_props (hb_buffer_t *buffer)
227{
228  unsigned int count = buffer->len;
229  hb_glyph_info_t *info = buffer->info;
230  for (unsigned int i = 0; i < count; i++)
231    _hb_glyph_info_set_unicode_props (&info[i], buffer);
232}
233
234static void
235hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
236{
237  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
238      buffer->context_len[0] ||
239      _hb_glyph_info_get_general_category (&buffer->info[0]) !=
240      HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
241    return;
242
243  if (!font->has_glyph (0x25CCu))
244    return;
245
246  hb_glyph_info_t dottedcircle = {0};
247  dottedcircle.codepoint = 0x25CCu;
248  _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
249
250  buffer->clear_output ();
251
252  buffer->idx = 0;
253  hb_glyph_info_t info = dottedcircle;
254  info.cluster = buffer->cur().cluster;
255  info.mask = buffer->cur().mask;
256  buffer->output_info (info);
257  while (buffer->idx < buffer->len)
258    buffer->next_glyph ();
259
260  buffer->swap_buffers ();
261}
262
263static void
264hb_form_clusters (hb_buffer_t *buffer)
265{
266  if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
267    return;
268
269  /* Loop duplicated in hb_ensure_native_direction(). */
270  unsigned int base = 0;
271  unsigned int count = buffer->len;
272  hb_glyph_info_t *info = buffer->info;
273  for (unsigned int i = 1; i < count; i++)
274  {
275    if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
276    {
277      buffer->merge_clusters (base, i);
278      base = i;
279    }
280  }
281  buffer->merge_clusters (base, count);
282}
283
284static void
285hb_ensure_native_direction (hb_buffer_t *buffer)
286{
287  hb_direction_t direction = buffer->props.direction;
288
289  /* TODO vertical:
290   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
291   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
292   * first. */
293  if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
294      (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
295  {
296    /* Same loop as hb_form_clusters().
297     * Since form_clusters() merged clusters already, we don't merge. */
298    unsigned int base = 0;
299    unsigned int count = buffer->len;
300    hb_glyph_info_t *info = buffer->info;
301    for (unsigned int i = 1; i < count; i++)
302    {
303      if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
304      {
305	if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
306	  buffer->merge_clusters (base, i);
307	buffer->reverse_range (base, i);
308
309	base = i;
310      }
311    }
312    if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
313      buffer->merge_clusters (base, count);
314    buffer->reverse_range (base, count);
315
316    buffer->reverse ();
317
318    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
319  }
320}
321
322
323/* Substitute */
324
325static inline void
326hb_ot_mirror_chars (hb_ot_shape_context_t *c)
327{
328  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
329    return;
330
331  hb_buffer_t *buffer = c->buffer;
332  hb_unicode_funcs_t *unicode = buffer->unicode;
333  hb_mask_t rtlm_mask = c->plan->rtlm_mask;
334
335  unsigned int count = buffer->len;
336  hb_glyph_info_t *info = buffer->info;
337  for (unsigned int i = 0; i < count; i++) {
338    hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
339    if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
340      info[i].mask |= rtlm_mask;
341    else
342      info[i].codepoint = codepoint;
343  }
344}
345
346static inline void
347hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
348{
349  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
350      !c->plan->has_frac)
351    return;
352
353  hb_buffer_t *buffer = c->buffer;
354
355  /* TODO look in pre/post context text also. */
356  unsigned int count = buffer->len;
357  hb_glyph_info_t *info = buffer->info;
358  for (unsigned int i = 0; i < count; i++)
359  {
360    if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
361    {
362      unsigned int start = i, end = i + 1;
363      while (start &&
364	     _hb_glyph_info_get_general_category (&info[start - 1]) ==
365	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
366        start--;
367      while (end < count &&
368	     _hb_glyph_info_get_general_category (&info[end]) ==
369	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
370        end++;
371
372      for (unsigned int j = start; j < i; j++)
373        info[j].mask |= c->plan->numr_mask | c->plan->frac_mask;
374      info[i].mask |= c->plan->frac_mask;
375      for (unsigned int j = i + 1; j < end; j++)
376        info[j].mask |= c->plan->frac_mask | c->plan->dnom_mask;
377
378      i = end - 1;
379    }
380  }
381}
382
383static inline void
384hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
385{
386  hb_ot_map_t *map = &c->plan->map;
387  hb_buffer_t *buffer = c->buffer;
388
389  hb_mask_t global_mask = map->get_global_mask ();
390  buffer->reset_masks (global_mask);
391}
392
393static inline void
394hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
395{
396  hb_ot_map_t *map = &c->plan->map;
397  hb_buffer_t *buffer = c->buffer;
398
399  hb_ot_shape_setup_masks_fraction (c);
400
401  if (c->plan->shaper->setup_masks)
402    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
403
404  for (unsigned int i = 0; i < c->num_user_features; i++)
405  {
406    const hb_feature_t *feature = &c->user_features[i];
407    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
408      unsigned int shift;
409      hb_mask_t mask = map->get_mask (feature->tag, &shift);
410      buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
411    }
412  }
413}
414
415static void
416hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
417{
418  hb_buffer_t *buffer = c->buffer;
419
420  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
421      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
422    return;
423
424  unsigned int count = buffer->len;
425  hb_glyph_info_t *info = buffer->info;
426  hb_glyph_position_t *pos = buffer->pos;
427  unsigned int i = 0;
428  for (i = 0; i < count; i++)
429    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
430      pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
431}
432
433static void
434hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
435{
436  hb_buffer_t *buffer = c->buffer;
437
438  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
439      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
440    return;
441
442  unsigned int count = buffer->len;
443  hb_glyph_info_t *info = buffer->info;
444  hb_glyph_position_t *pos = buffer->pos;
445  unsigned int i = 0;
446  for (i = 0; i < count; i++)
447  {
448    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
449      break;
450  }
451
452  /* No default-ignorables found; return. */
453  if (i == count)
454    return;
455
456  hb_codepoint_t space;
457  if (c->font->get_glyph (' ', 0, &space))
458  {
459    /* Replace default-ignorables with a zero-advance space glyph. */
460    for (/*continue*/; i < count; i++)
461    {
462      if (_hb_glyph_info_is_default_ignorable (&info[i]))
463	info[i].codepoint = space;
464    }
465  }
466  else
467  {
468    /* Merge clusters and delete default-ignorables.
469     * NOTE! We can't use out-buffer as we have positioning data. */
470    unsigned int j = i;
471    for (; i < count; i++)
472    {
473      if (_hb_glyph_info_is_default_ignorable (&info[i]))
474      {
475	/* Merge clusters.
476	 * Same logic as buffer->delete_glyph(), but for in-place removal. */
477
478	unsigned int cluster = info[i].cluster;
479	if (i + 1 < count && cluster == info[i + 1].cluster)
480	  continue; /* Cluster survives; do nothing. */
481
482	if (j)
483	{
484	  /* Merge cluster backward. */
485	  if (cluster < info[j - 1].cluster)
486	  {
487	    unsigned int old_cluster = info[j - 1].cluster;
488	    for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
489	      info[k - 1].cluster = cluster;
490	  }
491	  continue;
492	}
493
494	if (i + 1 < count)
495	  buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
496
497	continue;
498      }
499
500      if (j != i)
501      {
502	info[j] = info[i];
503	pos[j] = pos[i];
504      }
505      j++;
506    }
507    buffer->len = j;
508  }
509}
510
511
512static inline void
513hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
514{
515  /* Normalization process sets up glyph_index(), we just copy it. */
516  unsigned int count = buffer->len;
517  hb_glyph_info_t *info = buffer->info;
518  for (unsigned int i = 0; i < count; i++)
519    info[i].codepoint = info[i].glyph_index();
520
521  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
522}
523
524static inline void
525hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
526{
527  unsigned int count = c->buffer->len;
528  hb_glyph_info_t *info = c->buffer->info;
529  for (unsigned int i = 0; i < count; i++)
530  {
531    hb_ot_layout_glyph_props_flags_t klass;
532
533    /* Never mark default-ignorables as marks.
534     * They won't get in the way of lookups anyway,
535     * but having them as mark will cause them to be skipped
536     * over if the lookup-flag says so, but at least for the
537     * Mongolian variation selectors, looks like Uniscribe
538     * marks them as non-mark.  Some Mongolian fonts without
539     * GDEF rely on this.  Another notable character that
540     * this applies to is COMBINING GRAPHEME JOINER. */
541    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
542	     HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
543	     _hb_glyph_info_is_default_ignorable (&info[i])) ?
544	    HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
545	    HB_OT_LAYOUT_GLYPH_PROPS_MARK;
546    _hb_glyph_info_set_glyph_props (&info[i], klass);
547  }
548}
549
550static inline void
551hb_ot_substitute_default (hb_ot_shape_context_t *c)
552{
553  hb_buffer_t *buffer = c->buffer;
554
555  if (c->plan->shaper->preprocess_text)
556    c->plan->shaper->preprocess_text (c->plan, buffer, c->font);
557
558  hb_ot_shape_initialize_masks (c);
559
560  hb_ot_mirror_chars (c);
561
562  HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
563
564  _hb_ot_shape_normalize (c->plan, buffer, c->font);
565
566  hb_ot_shape_setup_masks (c);
567
568  /* This is unfortunate to go here, but necessary... */
569  if (!hb_ot_layout_has_positioning (c->face))
570    _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
571
572  hb_ot_map_glyphs_fast (buffer);
573
574  HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
575}
576
577static inline void
578hb_ot_substitute_complex (hb_ot_shape_context_t *c)
579{
580  hb_buffer_t *buffer = c->buffer;
581
582  _hb_buffer_allocate_gsubgpos_vars (buffer);
583  hb_ot_layout_substitute_start (c->font, buffer);
584
585  if (!hb_ot_layout_has_glyph_classes (c->face))
586    hb_synthesize_glyph_classes (c);
587
588  c->plan->substitute (c->font, buffer);
589
590  hb_ot_layout_substitute_finish (c->font, buffer);
591
592  return;
593}
594
595static inline void
596hb_ot_substitute (hb_ot_shape_context_t *c)
597{
598  hb_ot_substitute_default (c);
599  hb_ot_substitute_complex (c);
600}
601
602/* Position */
603
604static inline void
605adjust_mark_offsets (hb_glyph_position_t *pos)
606{
607  pos->x_offset -= pos->x_advance;
608  pos->y_offset -= pos->y_advance;
609}
610
611static inline void
612zero_mark_width (hb_glyph_position_t *pos)
613{
614  pos->x_advance = 0;
615  pos->y_advance = 0;
616}
617
618static inline void
619zero_mark_widths_by_unicode (hb_buffer_t *buffer, bool adjust_offsets)
620{
621  unsigned int count = buffer->len;
622  hb_glyph_info_t *info = buffer->info;
623  for (unsigned int i = 0; i < count; i++)
624    if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
625    {
626      if (adjust_offsets)
627        adjust_mark_offsets (&buffer->pos[i]);
628      zero_mark_width (&buffer->pos[i]);
629    }
630}
631
632static inline void
633zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
634{
635  unsigned int count = buffer->len;
636  hb_glyph_info_t *info = buffer->info;
637  for (unsigned int i = 0; i < count; i++)
638    if (_hb_glyph_info_is_mark (&info[i]))
639    {
640      if (adjust_offsets)
641        adjust_mark_offsets (&buffer->pos[i]);
642      zero_mark_width (&buffer->pos[i]);
643    }
644}
645
646static inline void
647hb_ot_position_default (hb_ot_shape_context_t *c)
648{
649  hb_direction_t direction = c->buffer->props.direction;
650  unsigned int count = c->buffer->len;
651  hb_glyph_info_t *info = c->buffer->info;
652  hb_glyph_position_t *pos = c->buffer->pos;
653  for (unsigned int i = 0; i < count; i++)
654  {
655    c->font->get_glyph_advance_for_direction (info[i].codepoint,
656					      direction,
657					      &pos[i].x_advance,
658					      &pos[i].y_advance);
659    c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
660						  direction,
661						  &pos[i].x_offset,
662						  &pos[i].y_offset);
663
664  }
665  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
666    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
667}
668
669static inline bool
670hb_ot_position_complex (hb_ot_shape_context_t *c)
671{
672  bool ret = false;
673  unsigned int count = c->buffer->len;
674  bool has_positioning = hb_ot_layout_has_positioning (c->face);
675  /* If the font has no GPOS, AND, no fallback positioning will
676   * happen, AND, direction is forward, then when zeroing mark
677   * widths, we shift the mark with it, such that the mark
678   * is positioned hanging over the previous glyph.  When
679   * direction is backward we don't shift and it will end up
680   * hanging over the next glyph after the final reordering.
681   * If fallback positinoing happens or GPOS is present, we don't
682   * care.
683   */
684  bool adjust_offsets_when_zeroing = !(has_positioning || c->plan->shaper->fallback_position ||
685                                       HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction));
686
687  switch (c->plan->shaper->zero_width_marks)
688  {
689    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
690      zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
691      break;
692
693    /* Not currently used for any shaper:
694    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
695      zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
696      break;
697    */
698
699    default:
700    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
701    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
702    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
703      break;
704  }
705
706  if (has_positioning)
707  {
708    hb_glyph_info_t *info = c->buffer->info;
709    hb_glyph_position_t *pos = c->buffer->pos;
710
711    /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
712
713    for (unsigned int i = 0; i < count; i++) {
714      c->font->add_glyph_origin_for_direction (info[i].codepoint,
715					       HB_DIRECTION_LTR,
716					       &pos[i].x_offset,
717					       &pos[i].y_offset);
718    }
719
720    c->plan->position (c->font, c->buffer);
721
722    for (unsigned int i = 0; i < count; i++) {
723      c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
724						    HB_DIRECTION_LTR,
725						    &pos[i].x_offset,
726						    &pos[i].y_offset);
727    }
728
729    ret = true;
730  }
731
732  switch (c->plan->shaper->zero_width_marks)
733  {
734    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
735      zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
736      break;
737
738    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
739      zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
740      break;
741
742    default:
743    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
744    //case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
745    case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
746      break;
747  }
748
749  return ret;
750}
751
752static inline void
753hb_ot_position (hb_ot_shape_context_t *c)
754{
755  hb_ot_layout_position_start (c->font, c->buffer);
756
757  hb_ot_position_default (c);
758
759  hb_bool_t fallback = !hb_ot_position_complex (c);
760
761  hb_ot_zero_width_default_ignorables (c);
762
763  hb_ot_layout_position_finish (c->font, c->buffer);
764
765  if (fallback && c->plan->shaper->fallback_position)
766    _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
767
768  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
769    hb_buffer_reverse (c->buffer);
770
771  /* Visual fallback goes here. */
772
773  if (fallback)
774    _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
775
776  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
777}
778
779
780/* Pull it all together! */
781
782static void
783hb_ot_shape_internal (hb_ot_shape_context_t *c)
784{
785  c->buffer->deallocate_var_all ();
786  c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
787
788  /* Save the original direction, we use it later. */
789  c->target_direction = c->buffer->props.direction;
790
791  _hb_buffer_allocate_unicode_vars (c->buffer);
792
793  c->buffer->clear_output ();
794
795  hb_set_unicode_props (c->buffer);
796  hb_insert_dotted_circle (c->buffer, c->font);
797  hb_form_clusters (c->buffer);
798
799  hb_ensure_native_direction (c->buffer);
800
801  hb_ot_substitute (c);
802  hb_ot_position (c);
803
804  hb_ot_hide_default_ignorables (c);
805
806  _hb_buffer_deallocate_unicode_vars (c->buffer);
807
808  c->buffer->props.direction = c->target_direction;
809
810  c->buffer->deallocate_var_all ();
811}
812
813
814hb_bool_t
815_hb_ot_shape (hb_shape_plan_t    *shape_plan,
816	      hb_font_t          *font,
817	      hb_buffer_t        *buffer,
818	      const hb_feature_t *features,
819	      unsigned int        num_features)
820{
821  hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
822  hb_ot_shape_internal (&c);
823
824  return true;
825}
826
827
828/**
829 * Since: 0.9.7
830 **/
831void
832hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
833				  hb_tag_t         table_tag,
834				  hb_set_t        *lookup_indexes /* OUT */)
835{
836  /* XXX Does the first part always succeed? */
837  HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
838}
839
840
841/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
842static void
843add_char (hb_font_t          *font,
844	  hb_unicode_funcs_t *unicode,
845	  hb_bool_t           mirror,
846	  hb_codepoint_t      u,
847	  hb_set_t           *glyphs)
848{
849  hb_codepoint_t glyph;
850  if (font->get_glyph (u, 0, &glyph))
851    glyphs->add (glyph);
852  if (mirror)
853  {
854    hb_codepoint_t m = unicode->mirroring (u);
855    if (m != u && font->get_glyph (m, 0, &glyph))
856      glyphs->add (glyph);
857  }
858}
859
860
861/**
862 * Since: 0.9.2
863 **/
864void
865hb_ot_shape_glyphs_closure (hb_font_t          *font,
866			    hb_buffer_t        *buffer,
867			    const hb_feature_t *features,
868			    unsigned int        num_features,
869			    hb_set_t           *glyphs)
870{
871  hb_ot_shape_plan_t plan;
872
873  const char *shapers[] = {"ot", NULL};
874  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
875							     features, num_features, shapers);
876
877  bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
878
879  unsigned int count = buffer->len;
880  hb_glyph_info_t *info = buffer->info;
881  for (unsigned int i = 0; i < count; i++)
882    add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
883
884  hb_set_t lookups;
885  lookups.init ();
886  hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
887
888  /* And find transitive closure. */
889  hb_set_t copy;
890  copy.init ();
891  do {
892    copy.set (glyphs);
893    for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
894      hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
895  } while (!copy.is_equal (glyphs));
896
897  hb_shape_plan_destroy (shape_plan);
898}
899