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