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