1 /*
2  * Copyright © 2011,2012  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26 
27 #include "hb.hh"
28 
29 #ifndef HB_NO_OT_SHAPE
30 
31 #include "hb-ot-shape-fallback.hh"
32 #include "hb-kern.hh"
33 
34 static unsigned int
recategorize_combining_class(hb_codepoint_t u,unsigned int klass)35 recategorize_combining_class (hb_codepoint_t u,
36 			      unsigned int klass)
37 {
38   if (klass >= 200)
39     return klass;
40 
41   /* Thai / Lao need some per-character work. */
42   if ((u & ~0xFF) == 0x0E00u)
43   {
44     if (unlikely (klass == 0))
45     {
46       switch (u)
47       {
48 	case 0x0E31u:
49 	case 0x0E34u:
50 	case 0x0E35u:
51 	case 0x0E36u:
52 	case 0x0E37u:
53 	case 0x0E47u:
54 	case 0x0E4Cu:
55 	case 0x0E4Du:
56 	case 0x0E4Eu:
57 	  klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
58 	  break;
59 
60 	case 0x0EB1u:
61 	case 0x0EB4u:
62 	case 0x0EB5u:
63 	case 0x0EB6u:
64 	case 0x0EB7u:
65 	case 0x0EBBu:
66 	case 0x0ECCu:
67 	case 0x0ECDu:
68 	  klass = HB_UNICODE_COMBINING_CLASS_ABOVE;
69 	  break;
70 
71 	case 0x0EBCu:
72 	  klass = HB_UNICODE_COMBINING_CLASS_BELOW;
73 	  break;
74       }
75     } else {
76       /* Thai virama is below-right */
77       if (u == 0x0E3Au)
78 	klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
79     }
80   }
81 
82   switch (klass)
83   {
84 
85     /* Hebrew */
86 
87     case HB_MODIFIED_COMBINING_CLASS_CCC10: /* sheva */
88     case HB_MODIFIED_COMBINING_CLASS_CCC11: /* hataf segol */
89     case HB_MODIFIED_COMBINING_CLASS_CCC12: /* hataf patah */
90     case HB_MODIFIED_COMBINING_CLASS_CCC13: /* hataf qamats */
91     case HB_MODIFIED_COMBINING_CLASS_CCC14: /* hiriq */
92     case HB_MODIFIED_COMBINING_CLASS_CCC15: /* tsere */
93     case HB_MODIFIED_COMBINING_CLASS_CCC16: /* segol */
94     case HB_MODIFIED_COMBINING_CLASS_CCC17: /* patah */
95     case HB_MODIFIED_COMBINING_CLASS_CCC18: /* qamats */
96     case HB_MODIFIED_COMBINING_CLASS_CCC20: /* qubuts */
97     case HB_MODIFIED_COMBINING_CLASS_CCC22: /* meteg */
98       return HB_UNICODE_COMBINING_CLASS_BELOW;
99 
100     case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */
101       return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE;
102 
103     case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */
104       return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
105 
106     case HB_MODIFIED_COMBINING_CLASS_CCC25: /* sin dot */
107     case HB_MODIFIED_COMBINING_CLASS_CCC19: /* holam */
108       return HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT;
109 
110     case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */
111       return HB_UNICODE_COMBINING_CLASS_ABOVE;
112 
113     case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */
114       break;
115 
116 
117     /* Arabic and Syriac */
118 
119     case HB_MODIFIED_COMBINING_CLASS_CCC27: /* fathatan */
120     case HB_MODIFIED_COMBINING_CLASS_CCC28: /* dammatan */
121     case HB_MODIFIED_COMBINING_CLASS_CCC30: /* fatha */
122     case HB_MODIFIED_COMBINING_CLASS_CCC31: /* damma */
123     case HB_MODIFIED_COMBINING_CLASS_CCC33: /* shadda */
124     case HB_MODIFIED_COMBINING_CLASS_CCC34: /* sukun */
125     case HB_MODIFIED_COMBINING_CLASS_CCC35: /* superscript alef */
126     case HB_MODIFIED_COMBINING_CLASS_CCC36: /* superscript alaph */
127       return HB_UNICODE_COMBINING_CLASS_ABOVE;
128 
129     case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */
130     case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */
131       return HB_UNICODE_COMBINING_CLASS_BELOW;
132 
133 
134     /* Thai */
135 
136     case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */
137       return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
138 
139     case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */
140       return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
141 
142 
143     /* Lao */
144 
145     case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */
146       return HB_UNICODE_COMBINING_CLASS_BELOW;
147 
148     case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */
149       return HB_UNICODE_COMBINING_CLASS_ABOVE;
150 
151 
152     /* Tibetan */
153 
154     case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */
155       return HB_UNICODE_COMBINING_CLASS_BELOW;
156 
157     case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/
158       return HB_UNICODE_COMBINING_CLASS_ABOVE;
159 
160     case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */
161       return HB_UNICODE_COMBINING_CLASS_BELOW;
162 
163   }
164 
165   return klass;
166 }
167 
168 void
_hb_ot_shape_fallback_mark_position_recategorize_marks(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)169 _hb_ot_shape_fallback_mark_position_recategorize_marks (const hb_ot_shape_plan_t *plan HB_UNUSED,
170 							hb_font_t *font HB_UNUSED,
171 							hb_buffer_t  *buffer)
172 {
173 #ifdef HB_NO_OT_SHAPE_FALLBACK
174   return;
175 #endif
176 
177   unsigned int count = buffer->len;
178   hb_glyph_info_t *info = buffer->info;
179   for (unsigned int i = 0; i < count; i++)
180     if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) {
181       unsigned int combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]);
182       combining_class = recategorize_combining_class (info[i].codepoint, combining_class);
183       _hb_glyph_info_set_modified_combining_class (&info[i], combining_class);
184     }
185 }
186 
187 
188 static void
zero_mark_advances(hb_buffer_t * buffer,unsigned int start,unsigned int end,bool adjust_offsets_when_zeroing)189 zero_mark_advances (hb_buffer_t *buffer,
190 		    unsigned int start,
191 		    unsigned int end,
192 		    bool adjust_offsets_when_zeroing)
193 {
194   hb_glyph_info_t *info = buffer->info;
195   for (unsigned int i = start; i < end; i++)
196     if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
197     {
198       if (adjust_offsets_when_zeroing)
199       {
200 	buffer->pos[i].x_offset -= buffer->pos[i].x_advance;
201 	buffer->pos[i].y_offset -= buffer->pos[i].y_advance;
202       }
203       buffer->pos[i].x_advance = 0;
204       buffer->pos[i].y_advance = 0;
205     }
206 }
207 
208 static inline void
position_mark(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer,hb_glyph_extents_t & base_extents,unsigned int i,unsigned int combining_class)209 position_mark (const hb_ot_shape_plan_t *plan HB_UNUSED,
210 	       hb_font_t *font,
211 	       hb_buffer_t  *buffer,
212 	       hb_glyph_extents_t &base_extents,
213 	       unsigned int i,
214 	       unsigned int combining_class)
215 {
216   hb_glyph_extents_t mark_extents;
217   if (!font->get_glyph_extents (buffer->info[i].codepoint, &mark_extents))
218     return;
219 
220   hb_position_t y_gap = font->y_scale / 16;
221 
222   hb_glyph_position_t &pos = buffer->pos[i];
223   pos.x_offset = pos.y_offset = 0;
224 
225 
226   /* We don't position LEFT and RIGHT marks. */
227 
228   /* X positioning */
229   switch (combining_class)
230   {
231     case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
232     case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
233       if (buffer->props.direction == HB_DIRECTION_LTR) {
234 	pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width / 2 - mark_extents.x_bearing;
235 	break;
236       } else if (buffer->props.direction == HB_DIRECTION_RTL) {
237 	pos.x_offset += base_extents.x_bearing - mark_extents.width / 2 - mark_extents.x_bearing;
238 	break;
239       }
240       HB_FALLTHROUGH;
241 
242     default:
243     case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
244     case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
245     case HB_UNICODE_COMBINING_CLASS_BELOW:
246     case HB_UNICODE_COMBINING_CLASS_ABOVE:
247       /* Center align. */
248       pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing;
249       break;
250 
251     case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
252     case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
253     case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
254       /* Left align. */
255       pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing;
256       break;
257 
258     case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
259     case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
260     case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
261       /* Right align. */
262       pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing;
263       break;
264   }
265 
266   /* Y positioning */
267   switch (combining_class)
268   {
269     case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
270     case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
271     case HB_UNICODE_COMBINING_CLASS_BELOW:
272     case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
273       /* Add gap, fall-through. */
274       base_extents.height -= y_gap;
275       HB_FALLTHROUGH;
276 
277     case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
278     case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
279       pos.y_offset = base_extents.y_bearing + base_extents.height - mark_extents.y_bearing;
280       /* Never shift up "below" marks. */
281       if ((y_gap > 0) == (pos.y_offset > 0))
282       {
283 	base_extents.height -= pos.y_offset;
284 	pos.y_offset = 0;
285       }
286       base_extents.height += mark_extents.height;
287       break;
288 
289     case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
290     case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
291     case HB_UNICODE_COMBINING_CLASS_ABOVE:
292     case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
293       /* Add gap, fall-through. */
294       base_extents.y_bearing += y_gap;
295       base_extents.height -= y_gap;
296       HB_FALLTHROUGH;
297 
298     case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
299     case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
300       pos.y_offset = base_extents.y_bearing - (mark_extents.y_bearing + mark_extents.height);
301       /* Don't shift down "above" marks too much. */
302       if ((y_gap > 0) != (pos.y_offset > 0))
303       {
304 	unsigned int correction = -pos.y_offset / 2;
305 	base_extents.y_bearing += correction;
306 	base_extents.height -= correction;
307 	pos.y_offset += correction;
308       }
309       base_extents.y_bearing -= mark_extents.height;
310       base_extents.height += mark_extents.height;
311       break;
312   }
313 }
314 
315 static inline void
position_around_base(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer,unsigned int base,unsigned int end,bool adjust_offsets_when_zeroing)316 position_around_base (const hb_ot_shape_plan_t *plan,
317 		      hb_font_t *font,
318 		      hb_buffer_t  *buffer,
319 		      unsigned int base,
320 		      unsigned int end,
321 		      bool adjust_offsets_when_zeroing)
322 {
323   hb_direction_t horiz_dir = HB_DIRECTION_INVALID;
324 
325   buffer->unsafe_to_break (base, end);
326 
327   hb_glyph_extents_t base_extents;
328   if (!font->get_glyph_extents (buffer->info[base].codepoint,
329 				&base_extents))
330   {
331     /* If extents don't work, zero marks and go home. */
332     zero_mark_advances (buffer, base + 1, end, adjust_offsets_when_zeroing);
333     return;
334   }
335   base_extents.y_bearing += buffer->pos[base].y_offset;
336   /* Use horizontal advance for horizontal positioning.
337    * Generally a better idea.  Also works for zero-ink glyphs.  See:
338    * https://github.com/harfbuzz/harfbuzz/issues/1532 */
339   base_extents.x_bearing = 0;
340   base_extents.width = font->get_glyph_h_advance (buffer->info[base].codepoint);
341 
342   unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[base]);
343   /* Use integer for num_lig_components such that it doesn't convert to unsigned
344    * when we divide or multiply by it. */
345   int num_lig_components = _hb_glyph_info_get_lig_num_comps (&buffer->info[base]);
346 
347   hb_position_t x_offset = 0, y_offset = 0;
348   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
349     x_offset -= buffer->pos[base].x_advance;
350     y_offset -= buffer->pos[base].y_advance;
351   }
352 
353   hb_glyph_extents_t component_extents = base_extents;
354   int last_lig_component = -1;
355   unsigned int last_combining_class = 255;
356   hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */
357   hb_glyph_info_t *info = buffer->info;
358   for (unsigned int i = base + 1; i < end; i++)
359     if (_hb_glyph_info_get_modified_combining_class (&info[i]))
360     {
361       if (num_lig_components > 1) {
362 	unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&info[i]);
363 	int this_lig_component = _hb_glyph_info_get_lig_comp (&info[i]) - 1;
364 	/* Conditions for attaching to the last component. */
365 	if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components)
366 	  this_lig_component = num_lig_components - 1;
367 	if (last_lig_component != this_lig_component)
368 	{
369 	  last_lig_component = this_lig_component;
370 	  last_combining_class = 255;
371 	  component_extents = base_extents;
372 	  if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) {
373 	    if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction))
374 	      horiz_dir = plan->props.direction;
375 	    else
376 	      horiz_dir = hb_script_get_horizontal_direction (plan->props.script);
377 	  }
378 	  if (horiz_dir == HB_DIRECTION_LTR)
379 	    component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components;
380 	  else
381 	    component_extents.x_bearing += ((num_lig_components - 1 - this_lig_component) * component_extents.width) / num_lig_components;
382 	  component_extents.width /= num_lig_components;
383 	}
384       }
385 
386       unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]);
387       if (last_combining_class != this_combining_class)
388       {
389 	last_combining_class = this_combining_class;
390 	cluster_extents = component_extents;
391       }
392 
393       position_mark (plan, font, buffer, cluster_extents, i, this_combining_class);
394 
395       buffer->pos[i].x_advance = 0;
396       buffer->pos[i].y_advance = 0;
397       buffer->pos[i].x_offset += x_offset;
398       buffer->pos[i].y_offset += y_offset;
399 
400     } else {
401       if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
402 	x_offset -= buffer->pos[i].x_advance;
403 	y_offset -= buffer->pos[i].y_advance;
404       } else {
405 	x_offset += buffer->pos[i].x_advance;
406 	y_offset += buffer->pos[i].y_advance;
407       }
408     }
409 }
410 
411 static inline void
position_cluster(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer,unsigned int start,unsigned int end,bool adjust_offsets_when_zeroing)412 position_cluster (const hb_ot_shape_plan_t *plan,
413 		  hb_font_t *font,
414 		  hb_buffer_t  *buffer,
415 		  unsigned int start,
416 		  unsigned int end,
417 		  bool adjust_offsets_when_zeroing)
418 {
419   if (end - start < 2)
420     return;
421 
422   /* Find the base glyph */
423   hb_glyph_info_t *info = buffer->info;
424   for (unsigned int i = start; i < end; i++)
425     if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])))
426     {
427       /* Find mark glyphs */
428       unsigned int j;
429       for (j = i + 1; j < end; j++)
430 	if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[j])))
431 	  break;
432 
433       position_around_base (plan, font, buffer, i, j, adjust_offsets_when_zeroing);
434 
435       i = j - 1;
436     }
437 }
438 
439 void
_hb_ot_shape_fallback_mark_position(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer,bool adjust_offsets_when_zeroing)440 _hb_ot_shape_fallback_mark_position (const hb_ot_shape_plan_t *plan,
441 				     hb_font_t *font,
442 				     hb_buffer_t  *buffer,
443 				     bool adjust_offsets_when_zeroing)
444 {
445 #ifdef HB_NO_OT_SHAPE_FALLBACK
446   return;
447 #endif
448 
449   _hb_buffer_assert_gsubgpos_vars (buffer);
450 
451   unsigned int start = 0;
452   unsigned int count = buffer->len;
453   hb_glyph_info_t *info = buffer->info;
454   for (unsigned int i = 1; i < count; i++)
455     if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])))) {
456       position_cluster (plan, font, buffer, start, i, adjust_offsets_when_zeroing);
457       start = i;
458     }
459   position_cluster (plan, font, buffer, start, count, adjust_offsets_when_zeroing);
460 }
461 
462 
463 #ifndef HB_DISABLE_DEPRECATED
464 struct hb_ot_shape_fallback_kern_driver_t
465 {
hb_ot_shape_fallback_kern_driver_thb_ot_shape_fallback_kern_driver_t466   hb_ot_shape_fallback_kern_driver_t (hb_font_t   *font_,
467 				      hb_buffer_t *buffer) :
468     font (font_), direction (buffer->props.direction) {}
469 
get_kerninghb_ot_shape_fallback_kern_driver_t470   hb_position_t get_kerning (hb_codepoint_t first, hb_codepoint_t second) const
471   {
472     hb_position_t kern = 0;
473     font->get_glyph_kerning_for_direction (first, second,
474 					   direction,
475 					   &kern, &kern);
476     return kern;
477   }
478 
479   hb_font_t *font;
480   hb_direction_t direction;
481 };
482 #endif
483 
484 /* Performs font-assisted kerning. */
485 void
_hb_ot_shape_fallback_kern(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)486 _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan,
487 			    hb_font_t *font,
488 			    hb_buffer_t *buffer)
489 {
490 #ifdef HB_NO_OT_SHAPE_FALLBACK
491   return;
492 #endif
493 
494 #ifndef HB_DISABLE_DEPRECATED
495   if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction) ?
496       !font->has_glyph_h_kerning_func () :
497       !font->has_glyph_v_kerning_func ())
498     return;
499 
500   bool reverse = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
501 
502   if (reverse)
503     buffer->reverse ();
504 
505   hb_ot_shape_fallback_kern_driver_t driver (font, buffer);
506   OT::hb_kern_machine_t<hb_ot_shape_fallback_kern_driver_t> machine (driver);
507   machine.kern (font, buffer, plan->kern_mask, false);
508 
509   if (reverse)
510     buffer->reverse ();
511 #endif
512 }
513 
514 
515 /* Adjusts width of various spaces. */
516 void
_hb_ot_shape_fallback_spaces(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)517 _hb_ot_shape_fallback_spaces (const hb_ot_shape_plan_t *plan HB_UNUSED,
518 			      hb_font_t *font,
519 			      hb_buffer_t  *buffer)
520 {
521   hb_glyph_info_t *info = buffer->info;
522   hb_glyph_position_t *pos = buffer->pos;
523   bool horizontal = HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction);
524   unsigned int count = buffer->len;
525   for (unsigned int i = 0; i < count; i++)
526     if (_hb_glyph_info_is_unicode_space (&info[i]) && !_hb_glyph_info_ligated (&info[i]))
527     {
528       hb_unicode_funcs_t::space_t space_type = _hb_glyph_info_get_unicode_space_fallback_type (&info[i]);
529       hb_codepoint_t glyph;
530       typedef hb_unicode_funcs_t t;
531       switch (space_type)
532       {
533 	case t::NOT_SPACE: /* Shouldn't happen. */
534 	case t::SPACE:
535 	  break;
536 
537 	case t::SPACE_EM:
538 	case t::SPACE_EM_2:
539 	case t::SPACE_EM_3:
540 	case t::SPACE_EM_4:
541 	case t::SPACE_EM_5:
542 	case t::SPACE_EM_6:
543 	case t::SPACE_EM_16:
544 	  if (horizontal)
545 	    pos[i].x_advance = +(font->x_scale + ((int) space_type)/2) / (int) space_type;
546 	  else
547 	    pos[i].y_advance = -(font->y_scale + ((int) space_type)/2) / (int) space_type;
548 	  break;
549 
550 	case t::SPACE_4_EM_18:
551 	  if (horizontal)
552 	    pos[i].x_advance = (int64_t) +font->x_scale * 4 / 18;
553 	  else
554 	    pos[i].y_advance = (int64_t) -font->y_scale * 4 / 18;
555 	  break;
556 
557 	case t::SPACE_FIGURE:
558 	  for (char u = '0'; u <= '9'; u++)
559 	    if (font->get_nominal_glyph (u, &glyph))
560 	    {
561 	      if (horizontal)
562 		pos[i].x_advance = font->get_glyph_h_advance (glyph);
563 	      else
564 		pos[i].y_advance = font->get_glyph_v_advance (glyph);
565 	      break;
566 	    }
567 	  break;
568 
569 	case t::SPACE_PUNCTUATION:
570 	  if (font->get_nominal_glyph ('.', &glyph) ||
571 	      font->get_nominal_glyph (',', &glyph))
572 	  {
573 	    if (horizontal)
574 	      pos[i].x_advance = font->get_glyph_h_advance (glyph);
575 	    else
576 	      pos[i].y_advance = font->get_glyph_v_advance (glyph);
577 	  }
578 	  break;
579 
580 	case t::SPACE_NARROW:
581 	  /* Half-space?
582 	   * Unicode doc https://unicode.org/charts/PDF/U2000.pdf says ~1/4 or 1/5 of EM.
583 	   * However, in my testing, many fonts have their regular space being about that
584 	   * size.  To me, a percentage of the space width makes more sense.  Half is as
585 	   * good as any. */
586 	  if (horizontal)
587 	    pos[i].x_advance /= 2;
588 	  else
589 	    pos[i].y_advance /= 2;
590 	  break;
591       }
592     }
593 }
594 
595 
596 #endif
597