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