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