2 * Copyright © 2011,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_NO_OT_SHAPE
31 #include "hb-ot-shape-fallback.hh"
35 recategorize_combining_class (hb_codepoint_t u,
41 /* Thai / Lao need some per-character work. */
42 if ((u & ~0xFF) == 0x0E00u)
44 if (unlikely (klass == 0))
57 klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
68 klass = HB_UNICODE_COMBINING_CLASS_ABOVE;
72 klass = HB_UNICODE_COMBINING_CLASS_BELOW;
76 /* Thai virama is below-right */
78 klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
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;
100 case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */
101 return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE;
103 case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */
104 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
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;
110 case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */
111 return HB_UNICODE_COMBINING_CLASS_ABOVE;
113 case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */
117 /* Arabic and Syriac */
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;
129 case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */
130 case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */
131 return HB_UNICODE_COMBINING_CLASS_BELOW;
136 case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */
137 return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
139 case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */
140 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
145 case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */
146 return HB_UNICODE_COMBINING_CLASS_BELOW;
148 case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */
149 return HB_UNICODE_COMBINING_CLASS_ABOVE;
154 case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */
155 return HB_UNICODE_COMBINING_CLASS_BELOW;
157 case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/
158 return HB_UNICODE_COMBINING_CLASS_ABOVE;
160 case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */
161 return HB_UNICODE_COMBINING_CLASS_BELOW;
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,
173 #ifdef HB_NO_OT_SHAPE_FALLBACK
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);
189 zero_mark_advances (hb_buffer_t *buffer,
192 bool adjust_offsets_when_zeroing)
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)
198 if (adjust_offsets_when_zeroing)
200 buffer->pos[i].x_offset -= buffer->pos[i].x_advance;
201 buffer->pos[i].y_offset -= buffer->pos[i].y_advance;
203 buffer->pos[i].x_advance = 0;
204 buffer->pos[i].y_advance = 0;
209 position_mark (const hb_ot_shape_plan_t *plan HB_UNUSED,
212 hb_glyph_extents_t &base_extents,
214 unsigned int combining_class)
216 hb_glyph_extents_t mark_extents;
217 if (!font->get_glyph_extents (buffer->info[i].codepoint, &mark_extents))
220 hb_position_t y_gap = font->y_scale / 16;
222 hb_glyph_position_t &pos = buffer->pos[i];
223 pos.x_offset = pos.y_offset = 0;
226 /* We don't position LEFT and RIGHT marks. */
229 switch (combining_class)
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;
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;
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:
248 pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing;
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:
255 pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing;
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:
262 pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing;
267 switch (combining_class)
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;
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))
283 base_extents.height -= pos.y_offset;
286 base_extents.height += mark_extents.height;
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;
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))
304 unsigned int correction = -pos.y_offset / 2;
305 base_extents.y_bearing += correction;
306 base_extents.height -= correction;
307 pos.y_offset += correction;
309 base_extents.y_bearing -= mark_extents.height;
310 base_extents.height += mark_extents.height;
316 position_around_base (const hb_ot_shape_plan_t *plan,
321 bool adjust_offsets_when_zeroing)
323 hb_direction_t horiz_dir = HB_DIRECTION_INVALID;
325 buffer->unsafe_to_break (base, end);
327 hb_glyph_extents_t base_extents;
328 if (!font->get_glyph_extents (buffer->info[base].codepoint,
331 /* If extents don't work, zero marks and go home. */
332 zero_mark_advances (buffer, base + 1, end, adjust_offsets_when_zeroing);
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);
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]);
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;
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]))
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)
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;
376 horiz_dir = hb_script_get_horizontal_direction (plan->props.script);
378 if (horiz_dir == HB_DIRECTION_LTR)
379 component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components;
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;
386 unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]);
387 if (last_combining_class != this_combining_class)
389 last_combining_class = this_combining_class;
390 cluster_extents = component_extents;
393 position_mark (plan, font, buffer, cluster_extents, i, this_combining_class);
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;
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;
405 x_offset += buffer->pos[i].x_advance;
406 y_offset += buffer->pos[i].y_advance;
412 position_cluster (const hb_ot_shape_plan_t *plan,
417 bool adjust_offsets_when_zeroing)
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_glyph_info_is_unicode_mark (&info[i]))
427 /* Find mark glyphs */
429 for (j = i + 1; j < end; j++)
430 if (!_hb_glyph_info_is_unicode_mark (&info[j]))
433 position_around_base (plan, font, buffer, i, j, adjust_offsets_when_zeroing);
440 _hb_ot_shape_fallback_mark_position (const hb_ot_shape_plan_t *plan,
443 bool adjust_offsets_when_zeroing)
445 #ifdef HB_NO_OT_SHAPE_FALLBACK
449 _hb_buffer_assert_gsubgpos_vars (buffer);
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_glyph_info_is_unicode_mark (&info[i]))) {
456 position_cluster (plan, font, buffer, start, i, adjust_offsets_when_zeroing);
459 position_cluster (plan, font, buffer, start, count, adjust_offsets_when_zeroing);
463 #ifndef HB_DISABLE_DEPRECATED
464 struct hb_ot_shape_fallback_kern_driver_t
466 hb_ot_shape_fallback_kern_driver_t (hb_font_t *font_,
467 hb_buffer_t *buffer) :
468 font (font_), direction (buffer->props.direction) {}
470 hb_position_t get_kerning (hb_codepoint_t first, hb_codepoint_t second) const
472 hb_position_t kern = 0;
473 font->get_glyph_kerning_for_direction (first, second,
480 hb_direction_t direction;
484 /* Performs font-assisted kerning. */
486 _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan,
490 #ifdef HB_NO_OT_SHAPE_FALLBACK
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 ())
500 bool reverse = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
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);
515 /* Adjusts width of various spaces. */
517 _hb_ot_shape_fallback_spaces (const hb_ot_shape_plan_t *plan HB_UNUSED,
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]))
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;
533 case t::NOT_SPACE: /* Shouldn't happen. */
545 pos[i].x_advance = +(font->x_scale + ((int) space_type)/2) / (int) space_type;
547 pos[i].y_advance = -(font->y_scale + ((int) space_type)/2) / (int) space_type;
550 case t::SPACE_4_EM_18:
552 pos[i].x_advance = (int64_t) +font->x_scale * 4 / 18;
554 pos[i].y_advance = (int64_t) -font->y_scale * 4 / 18;
557 case t::SPACE_FIGURE:
558 for (char u = '0'; u <= '9'; u++)
559 if (font->get_nominal_glyph (u, &glyph))
562 pos[i].x_advance = font->get_glyph_h_advance (glyph);
564 pos[i].y_advance = font->get_glyph_v_advance (glyph);
569 case t::SPACE_PUNCTUATION:
570 if (font->get_nominal_glyph ('.', &glyph) ||
571 font->get_nominal_glyph (',', &glyph))
574 pos[i].x_advance = font->get_glyph_h_advance (glyph);
576 pos[i].y_advance = font->get_glyph_v_advance (glyph);
580 case t::SPACE_NARROW:
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
587 pos[i].x_advance /= 2;
589 pos[i].y_advance /= 2;