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
27 #include "hb-ot-shape-fallback.hh"
31 recategorize_combining_class (hb_codepoint_t u,
37 /* Thai / Lao need some per-character work. */
38 if ((u & ~0xFF) == 0x0E00u)
40 if (unlikely (klass == 0))
53 klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
64 klass = HB_UNICODE_COMBINING_CLASS_ABOVE;
68 klass = HB_UNICODE_COMBINING_CLASS_BELOW;
72 /* Thai virama is below-right */
74 klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
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;
96 case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */
97 return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE;
99 case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */
100 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
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;
106 case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */
107 return HB_UNICODE_COMBINING_CLASS_ABOVE;
109 case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */
113 /* Arabic and Syriac */
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;
125 case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */
126 case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */
127 return HB_UNICODE_COMBINING_CLASS_BELOW;
132 case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */
133 return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
135 case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */
136 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
141 case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */
142 return HB_UNICODE_COMBINING_CLASS_BELOW;
144 case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */
145 return HB_UNICODE_COMBINING_CLASS_ABOVE;
150 case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */
151 return HB_UNICODE_COMBINING_CLASS_BELOW;
153 case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/
154 return HB_UNICODE_COMBINING_CLASS_ABOVE;
156 case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */
157 return HB_UNICODE_COMBINING_CLASS_BELOW;
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,
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);
181 zero_mark_advances (hb_buffer_t *buffer,
184 bool adjust_offsets_when_zeroing)
186 hb_glyph_info_t *info = buffer->info;
187 for (unsigned int i = start; i < end; i++)
188 if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
190 if (adjust_offsets_when_zeroing)
192 buffer->pos[i].x_offset -= buffer->pos[i].x_advance;
193 buffer->pos[i].y_offset -= buffer->pos[i].y_advance;
195 buffer->pos[i].x_advance = 0;
196 buffer->pos[i].y_advance = 0;
201 position_mark (const hb_ot_shape_plan_t *plan HB_UNUSED,
204 hb_glyph_extents_t &base_extents,
206 unsigned int combining_class)
208 hb_glyph_extents_t mark_extents;
209 if (!font->get_glyph_extents (buffer->info[i].codepoint, &mark_extents))
212 hb_position_t y_gap = font->y_scale / 16;
214 hb_glyph_position_t &pos = buffer->pos[i];
215 pos.x_offset = pos.y_offset = 0;
218 /* We don't position LEFT and RIGHT marks. */
221 switch (combining_class)
223 case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
224 case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
225 if (buffer->props.direction == HB_DIRECTION_LTR) {
226 pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width / 2 - mark_extents.x_bearing;
228 } else if (buffer->props.direction == HB_DIRECTION_RTL) {
229 pos.x_offset += base_extents.x_bearing - mark_extents.width / 2 - mark_extents.x_bearing;
235 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
236 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
237 case HB_UNICODE_COMBINING_CLASS_BELOW:
238 case HB_UNICODE_COMBINING_CLASS_ABOVE:
240 pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing;
243 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
244 case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
245 case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
247 pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing;
250 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
251 case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
252 case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
254 pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing;
259 switch (combining_class)
261 case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
262 case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
263 case HB_UNICODE_COMBINING_CLASS_BELOW:
264 case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
265 /* Add gap, fall-through. */
266 base_extents.height -= y_gap;
269 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
270 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
271 pos.y_offset = base_extents.y_bearing + base_extents.height - mark_extents.y_bearing;
272 /* Never shift up "below" marks. */
273 if ((y_gap > 0) == (pos.y_offset > 0))
275 base_extents.height -= pos.y_offset;
278 base_extents.height += mark_extents.height;
281 case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
282 case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
283 case HB_UNICODE_COMBINING_CLASS_ABOVE:
284 case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
285 /* Add gap, fall-through. */
286 base_extents.y_bearing += y_gap;
287 base_extents.height -= y_gap;
290 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
291 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
292 pos.y_offset = base_extents.y_bearing - (mark_extents.y_bearing + mark_extents.height);
293 /* Don't shift down "above" marks too much. */
294 if ((y_gap > 0) != (pos.y_offset > 0))
296 unsigned int correction = -pos.y_offset / 2;
297 base_extents.y_bearing += correction;
298 base_extents.height -= correction;
299 pos.y_offset += correction;
301 base_extents.y_bearing -= mark_extents.height;
302 base_extents.height += mark_extents.height;
308 position_around_base (const hb_ot_shape_plan_t *plan,
313 bool adjust_offsets_when_zeroing)
315 hb_direction_t horiz_dir = HB_DIRECTION_INVALID;
317 buffer->unsafe_to_break (base, end);
319 hb_glyph_extents_t base_extents;
320 if (!font->get_glyph_extents (buffer->info[base].codepoint,
323 /* If extents don't work, zero marks and go home. */
324 zero_mark_advances (buffer, base + 1, end, adjust_offsets_when_zeroing);
327 base_extents.y_bearing += buffer->pos[base].y_offset;
328 /* Use horizontal advance for horizontal positioning.
329 * Generally a better idea. Also works for zero-ink glyphs. See:
330 * https://github.com/harfbuzz/harfbuzz/issues/1532 */
331 base_extents.x_bearing = 0;
332 base_extents.width = font->get_glyph_h_advance (buffer->info[base].codepoint);
334 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[base]);
335 /* Use integer for num_lig_components such that it doesn't convert to unsigned
336 * when we divide or multiply by it. */
337 int num_lig_components = _hb_glyph_info_get_lig_num_comps (&buffer->info[base]);
339 hb_position_t x_offset = 0, y_offset = 0;
340 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
341 x_offset -= buffer->pos[base].x_advance;
342 y_offset -= buffer->pos[base].y_advance;
345 hb_glyph_extents_t component_extents = base_extents;
346 int last_lig_component = -1;
347 unsigned int last_combining_class = 255;
348 hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */
349 hb_glyph_info_t *info = buffer->info;
350 for (unsigned int i = base + 1; i < end; i++)
351 if (_hb_glyph_info_get_modified_combining_class (&info[i]))
353 if (num_lig_components > 1) {
354 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&info[i]);
355 int this_lig_component = _hb_glyph_info_get_lig_comp (&info[i]) - 1;
356 /* Conditions for attaching to the last component. */
357 if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components)
358 this_lig_component = num_lig_components - 1;
359 if (last_lig_component != this_lig_component)
361 last_lig_component = this_lig_component;
362 last_combining_class = 255;
363 component_extents = base_extents;
364 if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) {
365 if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction))
366 horiz_dir = plan->props.direction;
368 horiz_dir = hb_script_get_horizontal_direction (plan->props.script);
370 if (horiz_dir == HB_DIRECTION_LTR)
371 component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components;
373 component_extents.x_bearing += ((num_lig_components - 1 - this_lig_component) * component_extents.width) / num_lig_components;
374 component_extents.width /= num_lig_components;
378 unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]);
379 if (last_combining_class != this_combining_class)
381 last_combining_class = this_combining_class;
382 cluster_extents = component_extents;
385 position_mark (plan, font, buffer, cluster_extents, i, this_combining_class);
387 buffer->pos[i].x_advance = 0;
388 buffer->pos[i].y_advance = 0;
389 buffer->pos[i].x_offset += x_offset;
390 buffer->pos[i].y_offset += y_offset;
393 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
394 x_offset -= buffer->pos[i].x_advance;
395 y_offset -= buffer->pos[i].y_advance;
397 x_offset += buffer->pos[i].x_advance;
398 y_offset += buffer->pos[i].y_advance;
404 position_cluster (const hb_ot_shape_plan_t *plan,
409 bool adjust_offsets_when_zeroing)
414 /* Find the base glyph */
415 hb_glyph_info_t *info = buffer->info;
416 for (unsigned int i = start; i < end; i++)
417 if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])))
419 /* Find mark glyphs */
421 for (j = i + 1; j < end; j++)
422 if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[j])))
425 position_around_base (plan, font, buffer, i, j, adjust_offsets_when_zeroing);
432 _hb_ot_shape_fallback_mark_position (const hb_ot_shape_plan_t *plan,
435 bool adjust_offsets_when_zeroing)
437 _hb_buffer_assert_gsubgpos_vars (buffer);
439 unsigned int start = 0;
440 unsigned int count = buffer->len;
441 hb_glyph_info_t *info = buffer->info;
442 for (unsigned int i = 1; i < count; i++)
443 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])))) {
444 position_cluster (plan, font, buffer, start, i, adjust_offsets_when_zeroing);
447 position_cluster (plan, font, buffer, start, count, adjust_offsets_when_zeroing);
451 struct hb_ot_shape_fallback_kern_driver_t
453 hb_ot_shape_fallback_kern_driver_t (hb_font_t *font_,
454 hb_buffer_t *buffer) :
455 font (font_), direction (buffer->props.direction) {}
457 hb_position_t get_kerning (hb_codepoint_t first, hb_codepoint_t second) const
459 hb_position_t kern = 0;
460 font->get_glyph_kerning_for_direction (first, second,
467 hb_direction_t direction;
470 /* Performs font-assisted kerning. */
472 _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan,
476 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction) ?
477 !font->has_glyph_h_kerning_func () :
478 !font->has_glyph_v_kerning_func ())
481 bool reverse = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
486 hb_ot_shape_fallback_kern_driver_t driver (font, buffer);
487 OT::hb_kern_machine_t<hb_ot_shape_fallback_kern_driver_t> machine (driver);
488 machine.kern (font, buffer, plan->kern_mask, false);
495 /* Adjusts width of various spaces. */
497 _hb_ot_shape_fallback_spaces (const hb_ot_shape_plan_t *plan HB_UNUSED,
501 hb_glyph_info_t *info = buffer->info;
502 hb_glyph_position_t *pos = buffer->pos;
503 bool horizontal = HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction);
504 unsigned int count = buffer->len;
505 for (unsigned int i = 0; i < count; i++)
506 if (_hb_glyph_info_is_unicode_space (&info[i]) && !_hb_glyph_info_ligated (&info[i]))
508 hb_unicode_funcs_t::space_t space_type = _hb_glyph_info_get_unicode_space_fallback_type (&info[i]);
509 hb_codepoint_t glyph;
510 typedef hb_unicode_funcs_t t;
513 case t::NOT_SPACE: /* Shouldn't happen. */
525 pos[i].x_advance = +(font->x_scale + ((int) space_type)/2) / (int) space_type;
527 pos[i].y_advance = -(font->y_scale + ((int) space_type)/2) / (int) space_type;
530 case t::SPACE_4_EM_18:
532 pos[i].x_advance = (int64_t) +font->x_scale * 4 / 18;
534 pos[i].y_advance = (int64_t) -font->y_scale * 4 / 18;
537 case t::SPACE_FIGURE:
538 for (char u = '0'; u <= '9'; u++)
539 if (font->get_nominal_glyph (u, &glyph))
542 pos[i].x_advance = font->get_glyph_h_advance (glyph);
544 pos[i].y_advance = font->get_glyph_v_advance (glyph);
549 case t::SPACE_PUNCTUATION:
550 if (font->get_nominal_glyph ('.', &glyph) ||
551 font->get_nominal_glyph (',', &glyph))
554 pos[i].x_advance = font->get_glyph_h_advance (glyph);
556 pos[i].y_advance = font->get_glyph_v_advance (glyph);
560 case t::SPACE_NARROW:
562 * Unicode doc https://unicode.org/charts/PDF/U2000.pdf says ~1/4 or 1/5 of EM.
563 * However, in my testing, many fonts have their regular space being about that
564 * size. To me, a percentage of the space width makes more sense. Half is as
567 pos[i].x_advance /= 2;
569 pos[i].y_advance /= 2;