1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 // This file is available under and governed by the GNU General Public 26 // License version 2 only, as published by the Free Software Foundation. 27 // However, the following notice accompanied the original version of this 28 // file: 29 // 30 /* 31 * Copyright © 2011,2012 Google, Inc. 32 * 33 * This is part of HarfBuzz, a text shaping library. 34 * 35 * Permission is hereby granted, without written agreement and without 36 * license or royalty fees, to use, copy, modify, and distribute this 37 * software and its documentation for any purpose, provided that the 38 * above copyright notice and the following two paragraphs appear in 39 * all copies of this software. 40 * 41 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 42 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 43 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 44 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 45 * DAMAGE. 46 * 47 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 48 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 49 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 50 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 51 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 52 * 53 * Google Author(s): Behdad Esfahbod 54 */ 55 56 #include "hb-ot-shape-fallback-private.hh" 57 #include "hb-ot-layout-gsubgpos-private.hh" 58 59 static unsigned int 60 recategorize_combining_class (hb_codepoint_t u, 61 unsigned int klass) 62 { 63 if (klass >= 200) 64 return klass; 65 66 /* Thai / Lao need some per-character work. */ 67 if ((u & ~0xFF) == 0x0E00u) 68 { 69 if (unlikely (klass == 0)) 70 { 71 switch (u) 72 { 73 case 0x0E31u: 74 case 0x0E34u: 75 case 0x0E35u: 76 case 0x0E36u: 77 case 0x0E37u: 78 case 0x0E47u: 79 case 0x0E4Cu: 80 case 0x0E4Du: 81 case 0x0E4Eu: 82 klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; 83 break; 84 85 case 0x0EB1u: 86 case 0x0EB4u: 87 case 0x0EB5u: 88 case 0x0EB6u: 89 case 0x0EB7u: 90 case 0x0EBBu: 91 case 0x0ECCu: 92 case 0x0ECDu: 93 klass = HB_UNICODE_COMBINING_CLASS_ABOVE; 94 break; 95 96 case 0x0EBCu: 97 klass = HB_UNICODE_COMBINING_CLASS_BELOW; 98 break; 99 } 100 } else { 101 /* Thai virama is below-right */ 102 if (u == 0x0E3Au) 103 klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; 104 } 105 } 106 107 switch (klass) 108 { 109 110 /* Hebrew */ 111 112 case HB_MODIFIED_COMBINING_CLASS_CCC10: /* sheva */ 113 case HB_MODIFIED_COMBINING_CLASS_CCC11: /* hataf segol */ 114 case HB_MODIFIED_COMBINING_CLASS_CCC12: /* hataf patah */ 115 case HB_MODIFIED_COMBINING_CLASS_CCC13: /* hataf qamats */ 116 case HB_MODIFIED_COMBINING_CLASS_CCC14: /* hiriq */ 117 case HB_MODIFIED_COMBINING_CLASS_CCC15: /* tsere */ 118 case HB_MODIFIED_COMBINING_CLASS_CCC16: /* segol */ 119 case HB_MODIFIED_COMBINING_CLASS_CCC17: /* patah */ 120 case HB_MODIFIED_COMBINING_CLASS_CCC18: /* qamats */ 121 case HB_MODIFIED_COMBINING_CLASS_CCC20: /* qubuts */ 122 case HB_MODIFIED_COMBINING_CLASS_CCC22: /* meteg */ 123 return HB_UNICODE_COMBINING_CLASS_BELOW; 124 125 case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */ 126 return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE; 127 128 case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */ 129 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; 130 131 case HB_MODIFIED_COMBINING_CLASS_CCC25: /* sin dot */ 132 case HB_MODIFIED_COMBINING_CLASS_CCC19: /* holam */ 133 return HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT; 134 135 case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */ 136 return HB_UNICODE_COMBINING_CLASS_ABOVE; 137 138 case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */ 139 break; 140 141 142 /* Arabic and Syriac */ 143 144 case HB_MODIFIED_COMBINING_CLASS_CCC27: /* fathatan */ 145 case HB_MODIFIED_COMBINING_CLASS_CCC28: /* dammatan */ 146 case HB_MODIFIED_COMBINING_CLASS_CCC30: /* fatha */ 147 case HB_MODIFIED_COMBINING_CLASS_CCC31: /* damma */ 148 case HB_MODIFIED_COMBINING_CLASS_CCC33: /* shadda */ 149 case HB_MODIFIED_COMBINING_CLASS_CCC34: /* sukun */ 150 case HB_MODIFIED_COMBINING_CLASS_CCC35: /* superscript alef */ 151 case HB_MODIFIED_COMBINING_CLASS_CCC36: /* superscript alaph */ 152 return HB_UNICODE_COMBINING_CLASS_ABOVE; 153 154 case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */ 155 case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */ 156 return HB_UNICODE_COMBINING_CLASS_BELOW; 157 158 159 /* Thai */ 160 161 case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */ 162 return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; 163 164 case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */ 165 return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; 166 167 168 /* Lao */ 169 170 case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */ 171 return HB_UNICODE_COMBINING_CLASS_BELOW; 172 173 case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */ 174 return HB_UNICODE_COMBINING_CLASS_ABOVE; 175 176 177 /* Tibetan */ 178 179 case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */ 180 return HB_UNICODE_COMBINING_CLASS_BELOW; 181 182 case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/ 183 return HB_UNICODE_COMBINING_CLASS_ABOVE; 184 185 case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */ 186 return HB_UNICODE_COMBINING_CLASS_BELOW; 187 188 } 189 190 return klass; 191 } 192 193 void 194 _hb_ot_shape_fallback_position_recategorize_marks (const hb_ot_shape_plan_t *plan HB_UNUSED, 195 hb_font_t *font HB_UNUSED, 196 hb_buffer_t *buffer) 197 { 198 unsigned int count = buffer->len; 199 hb_glyph_info_t *info = buffer->info; 200 for (unsigned int i = 0; i < count; i++) 201 if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) { 202 unsigned int combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]); 203 combining_class = recategorize_combining_class (info[i].codepoint, combining_class); 204 _hb_glyph_info_set_modified_combining_class (&info[i], combining_class); 205 } 206 } 207 208 209 static void 210 zero_mark_advances (hb_buffer_t *buffer, 211 unsigned int start, 212 unsigned int end) 213 { 214 hb_glyph_info_t *info = buffer->info; 215 for (unsigned int i = start; i < end; i++) 216 if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) 217 { 218 buffer->pos[i].x_advance = 0; 219 buffer->pos[i].y_advance = 0; 220 } 221 } 222 223 static inline void 224 position_mark (const hb_ot_shape_plan_t *plan, 225 hb_font_t *font, 226 hb_buffer_t *buffer, 227 hb_glyph_extents_t &base_extents, 228 unsigned int i, 229 unsigned int combining_class) 230 { 231 hb_glyph_extents_t mark_extents; 232 if (!font->get_glyph_extents (buffer->info[i].codepoint, 233 &mark_extents)) 234 return; 235 236 hb_position_t y_gap = font->y_scale / 16; 237 238 hb_glyph_position_t &pos = buffer->pos[i]; 239 pos.x_offset = pos.y_offset = 0; 240 241 242 /* We dont position LEFT and RIGHT marks. */ 243 244 /* X positioning */ 245 switch (combining_class) 246 { 247 case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: 248 case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: 249 if (buffer->props.direction == HB_DIRECTION_LTR) { 250 pos.x_offset += base_extents.x_bearing - mark_extents.width / 2 - mark_extents.x_bearing; 251 break; 252 } else if (buffer->props.direction == HB_DIRECTION_RTL) { 253 pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width / 2 - mark_extents.x_bearing; 254 break; 255 } 256 /* Fall through */ 257 258 default: 259 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: 260 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: 261 case HB_UNICODE_COMBINING_CLASS_BELOW: 262 case HB_UNICODE_COMBINING_CLASS_ABOVE: 263 /* Center align. */ 264 pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing; 265 break; 266 267 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: 268 case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: 269 case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: 270 /* Left align. */ 271 pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing; 272 break; 273 274 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: 275 case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: 276 case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: 277 /* Right align. */ 278 pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing; 279 break; 280 } 281 282 /* Y positioning */ 283 switch (combining_class) 284 { 285 case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: 286 case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: 287 case HB_UNICODE_COMBINING_CLASS_BELOW: 288 case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: 289 /* Add gap, fall-through. */ 290 base_extents.height -= y_gap; 291 292 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: 293 case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: 294 pos.y_offset = base_extents.y_bearing + base_extents.height - mark_extents.y_bearing; 295 /* Never shift up "below" marks. */ 296 if ((y_gap > 0) == (pos.y_offset > 0)) 297 { 298 base_extents.height -= pos.y_offset; 299 pos.y_offset = 0; 300 } 301 base_extents.height += mark_extents.height; 302 break; 303 304 case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: 305 case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: 306 case HB_UNICODE_COMBINING_CLASS_ABOVE: 307 case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: 308 /* Add gap, fall-through. */ 309 base_extents.y_bearing += y_gap; 310 base_extents.height -= y_gap; 311 312 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: 313 case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: 314 pos.y_offset = base_extents.y_bearing - (mark_extents.y_bearing + mark_extents.height); 315 /* Don't shift down "above" marks too much. */ 316 if ((y_gap > 0) != (pos.y_offset > 0)) 317 { 318 unsigned int correction = -pos.y_offset / 2; 319 base_extents.y_bearing += correction; 320 base_extents.height -= correction; 321 pos.y_offset += correction; 322 } 323 base_extents.y_bearing -= mark_extents.height; 324 base_extents.height += mark_extents.height; 325 break; 326 } 327 } 328 329 static inline void 330 position_around_base (const hb_ot_shape_plan_t *plan, 331 hb_font_t *font, 332 hb_buffer_t *buffer, 333 unsigned int base, 334 unsigned int end) 335 { 336 hb_direction_t horiz_dir = HB_DIRECTION_INVALID; 337 hb_glyph_extents_t base_extents; 338 if (!font->get_glyph_extents (buffer->info[base].codepoint, 339 &base_extents)) 340 { 341 /* If extents don't work, zero marks and go home. */ 342 zero_mark_advances (buffer, base + 1, end); 343 return; 344 } 345 base_extents.x_bearing += buffer->pos[base].x_offset; 346 base_extents.y_bearing += buffer->pos[base].y_offset; 347 348 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[base]); 349 unsigned int num_lig_components = _hb_glyph_info_get_lig_num_comps (&buffer->info[base]); 350 351 hb_position_t x_offset = 0, y_offset = 0; 352 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { 353 x_offset -= buffer->pos[base].x_advance; 354 y_offset -= buffer->pos[base].y_advance; 355 } 356 357 hb_glyph_extents_t component_extents = base_extents; 358 unsigned int last_lig_component = (unsigned int) -1; 359 unsigned int last_combining_class = 255; 360 hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */ 361 hb_glyph_info_t *info = buffer->info; 362 for (unsigned int i = base + 1; i < end; i++) 363 if (_hb_glyph_info_get_modified_combining_class (&info[i])) 364 { 365 if (num_lig_components > 1) { 366 unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&info[i]); 367 unsigned int this_lig_component = _hb_glyph_info_get_lig_comp (&info[i]) - 1; 368 /* Conditions for attaching to the last component. */ 369 if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components) 370 this_lig_component = num_lig_components - 1; 371 if (last_lig_component != this_lig_component) 372 { 373 last_lig_component = this_lig_component; 374 last_combining_class = 255; 375 component_extents = base_extents; 376 if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) { 377 if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction)) 378 horiz_dir = plan->props.direction; 379 else 380 horiz_dir = hb_script_get_horizontal_direction (plan->props.script); 381 } 382 if (horiz_dir == HB_DIRECTION_LTR) 383 component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components; 384 else 385 component_extents.x_bearing += ((num_lig_components - 1 - this_lig_component) * component_extents.width) / num_lig_components; 386 component_extents.width /= num_lig_components; 387 } 388 } 389 390 unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]); 391 if (last_combining_class != this_combining_class) 392 { 393 last_combining_class = this_combining_class; 394 cluster_extents = component_extents; 395 } 396 397 position_mark (plan, font, buffer, cluster_extents, i, this_combining_class); 398 399 buffer->pos[i].x_advance = 0; 400 buffer->pos[i].y_advance = 0; 401 buffer->pos[i].x_offset += x_offset; 402 buffer->pos[i].y_offset += y_offset; 403 404 } else { 405 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { 406 x_offset -= buffer->pos[i].x_advance; 407 y_offset -= buffer->pos[i].y_advance; 408 } else { 409 x_offset += buffer->pos[i].x_advance; 410 y_offset += buffer->pos[i].y_advance; 411 } 412 } 413 } 414 415 static inline void 416 position_cluster (const hb_ot_shape_plan_t *plan, 417 hb_font_t *font, 418 hb_buffer_t *buffer, 419 unsigned int start, 420 unsigned int end) 421 { 422 if (end - start < 2) 423 return; 424 425 /* Find the base glyph */ 426 hb_glyph_info_t *info = buffer->info; 427 for (unsigned int i = start; i < end; i++) 428 if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))) 429 { 430 /* Find mark glyphs */ 431 unsigned int j; 432 for (j = i + 1; j < end; j++) 433 if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[j]))) 434 break; 435 436 position_around_base (plan, font, buffer, i, j); 437 438 i = j - 1; 439 } 440 } 441 442 void 443 _hb_ot_shape_fallback_position (const hb_ot_shape_plan_t *plan, 444 hb_font_t *font, 445 hb_buffer_t *buffer) 446 { 447 _hb_buffer_assert_gsubgpos_vars (buffer); 448 449 unsigned int start = 0; 450 unsigned int count = buffer->len; 451 hb_glyph_info_t *info = buffer->info; 452 for (unsigned int i = 1; i < count; i++) 453 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])))) { 454 position_cluster (plan, font, buffer, start, i); 455 start = i; 456 } 457 position_cluster (plan, font, buffer, start, count); 458 } 459 460 461 /* Performs old-style TrueType kerning. */ 462 void 463 _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan, 464 hb_font_t *font, 465 hb_buffer_t *buffer) 466 { 467 if (!plan->has_kern) return; 468 469 OT::hb_apply_context_t c (1, font, buffer); 470 c.set_lookup_mask (plan->kern_mask); 471 c.set_lookup_props (OT::LookupFlag::IgnoreMarks); 472 OT::hb_apply_context_t::skipping_iterator_t &skippy_iter = c.iter_input; 473 skippy_iter.init (&c); 474 475 unsigned int count = buffer->len; 476 hb_glyph_info_t *info = buffer->info; 477 hb_glyph_position_t *pos = buffer->pos; 478 for (unsigned int idx = 0; idx < count;) 479 { 480 skippy_iter.reset (idx, 1); 481 if (!skippy_iter.next ()) 482 { 483 idx++; 484 continue; 485 } 486 487 hb_position_t x_kern, y_kern; 488 font->get_glyph_kerning_for_direction (info[idx].codepoint, 489 info[skippy_iter.idx].codepoint, 490 buffer->props.direction, 491 &x_kern, &y_kern); 492 493 if (x_kern) 494 { 495 hb_position_t kern1 = x_kern >> 1; 496 hb_position_t kern2 = x_kern - kern1; 497 pos[idx].x_advance += kern1; 498 pos[skippy_iter.idx].x_advance += kern2; 499 pos[skippy_iter.idx].x_offset += kern2; 500 } 501 502 if (y_kern) 503 { 504 hb_position_t kern1 = y_kern >> 1; 505 hb_position_t kern2 = y_kern - kern1; 506 pos[idx].y_advance += kern1; 507 pos[skippy_iter.idx].y_advance += kern2; 508 pos[skippy_iter.idx].y_offset += kern2; 509 } 510 511 idx = skippy_iter.idx; 512 } 513 }