2d.text.measure.index-from-offset-edges.tentative.html (19213B)
1 <!DOCTYPE html> 2 <!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. --> 3 <meta charset="UTF-8"> 4 <title>OffscreenCanvas test: 2d.text.measure.index-from-offset-edges.tentative</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/html/canvas/resources/canvas-tests.js"></script> 8 9 <h1>2d.text.measure.index-from-offset-edges.tentative</h1> 10 11 <script> 12 13 test(t => { 14 const canvas = new OffscreenCanvas(100, 50); 15 const ctx = canvas.getContext('2d'); 16 17 function computeExpected(text, text_width, offset) { 18 expected_position = 0; 19 if ('left' == 'center' && offset == 0) { 20 return text.length / 2; 21 } 22 if ('ltr' == 'ltr') { 23 if (offset >= text_width) { 24 return text.length; 25 } 26 if (offset <= -text_width) { 27 return 0; 28 } 29 // offset must be 0. 30 if ('left' == 'start' || 'left' == 'left') { 31 return 0; 32 } else { 33 return text.length; 34 } 35 } else { 36 if (offset >= text_width) { 37 return 0; 38 } 39 if (offset <= -text_width) { 40 return text.length; 41 } 42 // offset must be 0. 43 if ('left' == 'start' || 'left' == 'right') { 44 return 0; 45 } else { 46 return text.length; 47 } 48 } 49 return expected_position; 50 } 51 52 ctx.font = '50px sans-serif'; 53 ctx.direction = 'ltr'; 54 ctx.textAlign = 'left'; 55 ctx.letterSpacing = ''; 56 57 // The leading and trailing '-' cause the string to always follow 58 // the specified direction, even though the interior will always be ltr. 59 const text = '-0123456789-'; 60 61 // Points are multiples of the string width as reported by 62 // textMetrics.width. 63 const kPoints = [ 64 -2, 65 -1, 66 0, 67 1, 68 2 69 ] 70 71 const tm = ctx.measureText(text); 72 text_width = tm.width; 73 for (const multiple of kPoints) { 74 offset = multiple * text_width; 75 tm_position = tm.getIndexFromOffset(offset); 76 expected_position = computeExpected(text, text_width, offset); 77 assert_equals(tm_position, 78 expected_position, 79 "for " + text + " multiple " + multiple); 80 } 81 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction ltr and text align left."); 82 83 test(t => { 84 const canvas = new OffscreenCanvas(100, 50); 85 const ctx = canvas.getContext('2d'); 86 87 function computeExpected(text, text_width, offset) { 88 expected_position = 0; 89 if ('left' == 'center' && offset == 0) { 90 return text.length / 2; 91 } 92 if ('rtl' == 'ltr') { 93 if (offset >= text_width) { 94 return text.length; 95 } 96 if (offset <= -text_width) { 97 return 0; 98 } 99 // offset must be 0. 100 if ('left' == 'start' || 'left' == 'left') { 101 return 0; 102 } else { 103 return text.length; 104 } 105 } else { 106 if (offset >= text_width) { 107 return 0; 108 } 109 if (offset <= -text_width) { 110 return text.length; 111 } 112 // offset must be 0. 113 if ('left' == 'start' || 'left' == 'right') { 114 return 0; 115 } else { 116 return text.length; 117 } 118 } 119 return expected_position; 120 } 121 122 ctx.font = '50px sans-serif'; 123 ctx.direction = 'rtl'; 124 ctx.textAlign = 'left'; 125 ctx.letterSpacing = ''; 126 127 // The leading and trailing '-' cause the string to always follow 128 // the specified direction, even though the interior will always be ltr. 129 const text = '-0123456789-'; 130 131 // Points are multiples of the string width as reported by 132 // textMetrics.width. 133 const kPoints = [ 134 -2, 135 -1, 136 0, 137 1, 138 2 139 ] 140 141 const tm = ctx.measureText(text); 142 text_width = tm.width; 143 for (const multiple of kPoints) { 144 offset = multiple * text_width; 145 tm_position = tm.getIndexFromOffset(offset); 146 expected_position = computeExpected(text, text_width, offset); 147 assert_equals(tm_position, 148 expected_position, 149 "for " + text + " multiple " + multiple); 150 } 151 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction rtl and text align left."); 152 153 test(t => { 154 const canvas = new OffscreenCanvas(100, 50); 155 const ctx = canvas.getContext('2d'); 156 157 function computeExpected(text, text_width, offset) { 158 expected_position = 0; 159 if ('center' == 'center' && offset == 0) { 160 return text.length / 2; 161 } 162 if ('ltr' == 'ltr') { 163 if (offset >= text_width) { 164 return text.length; 165 } 166 if (offset <= -text_width) { 167 return 0; 168 } 169 // offset must be 0. 170 if ('center' == 'start' || 'center' == 'left') { 171 return 0; 172 } else { 173 return text.length; 174 } 175 } else { 176 if (offset >= text_width) { 177 return 0; 178 } 179 if (offset <= -text_width) { 180 return text.length; 181 } 182 // offset must be 0. 183 if ('center' == 'start' || 'center' == 'right') { 184 return 0; 185 } else { 186 return text.length; 187 } 188 } 189 return expected_position; 190 } 191 192 ctx.font = '50px sans-serif'; 193 ctx.direction = 'ltr'; 194 ctx.textAlign = 'center'; 195 ctx.letterSpacing = ''; 196 197 // The leading and trailing '-' cause the string to always follow 198 // the specified direction, even though the interior will always be ltr. 199 const text = '-0123456789-'; 200 201 // Points are multiples of the string width as reported by 202 // textMetrics.width. 203 const kPoints = [ 204 -2, 205 -1, 206 0, 207 1, 208 2 209 ] 210 211 const tm = ctx.measureText(text); 212 text_width = tm.width; 213 for (const multiple of kPoints) { 214 offset = multiple * text_width; 215 tm_position = tm.getIndexFromOffset(offset); 216 expected_position = computeExpected(text, text_width, offset); 217 assert_equals(tm_position, 218 expected_position, 219 "for " + text + " multiple " + multiple); 220 } 221 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction ltr and text align center."); 222 223 test(t => { 224 const canvas = new OffscreenCanvas(100, 50); 225 const ctx = canvas.getContext('2d'); 226 227 function computeExpected(text, text_width, offset) { 228 expected_position = 0; 229 if ('center' == 'center' && offset == 0) { 230 return text.length / 2; 231 } 232 if ('rtl' == 'ltr') { 233 if (offset >= text_width) { 234 return text.length; 235 } 236 if (offset <= -text_width) { 237 return 0; 238 } 239 // offset must be 0. 240 if ('center' == 'start' || 'center' == 'left') { 241 return 0; 242 } else { 243 return text.length; 244 } 245 } else { 246 if (offset >= text_width) { 247 return 0; 248 } 249 if (offset <= -text_width) { 250 return text.length; 251 } 252 // offset must be 0. 253 if ('center' == 'start' || 'center' == 'right') { 254 return 0; 255 } else { 256 return text.length; 257 } 258 } 259 return expected_position; 260 } 261 262 ctx.font = '50px sans-serif'; 263 ctx.direction = 'rtl'; 264 ctx.textAlign = 'center'; 265 ctx.letterSpacing = ''; 266 267 // The leading and trailing '-' cause the string to always follow 268 // the specified direction, even though the interior will always be ltr. 269 const text = '-0123456789-'; 270 271 // Points are multiples of the string width as reported by 272 // textMetrics.width. 273 const kPoints = [ 274 -2, 275 -1, 276 0, 277 1, 278 2 279 ] 280 281 const tm = ctx.measureText(text); 282 text_width = tm.width; 283 for (const multiple of kPoints) { 284 offset = multiple * text_width; 285 tm_position = tm.getIndexFromOffset(offset); 286 expected_position = computeExpected(text, text_width, offset); 287 assert_equals(tm_position, 288 expected_position, 289 "for " + text + " multiple " + multiple); 290 } 291 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction rtl and text align center."); 292 293 test(t => { 294 const canvas = new OffscreenCanvas(100, 50); 295 const ctx = canvas.getContext('2d'); 296 297 function computeExpected(text, text_width, offset) { 298 expected_position = 0; 299 if ('right' == 'center' && offset == 0) { 300 return text.length / 2; 301 } 302 if ('ltr' == 'ltr') { 303 if (offset >= text_width) { 304 return text.length; 305 } 306 if (offset <= -text_width) { 307 return 0; 308 } 309 // offset must be 0. 310 if ('right' == 'start' || 'right' == 'left') { 311 return 0; 312 } else { 313 return text.length; 314 } 315 } else { 316 if (offset >= text_width) { 317 return 0; 318 } 319 if (offset <= -text_width) { 320 return text.length; 321 } 322 // offset must be 0. 323 if ('right' == 'start' || 'right' == 'right') { 324 return 0; 325 } else { 326 return text.length; 327 } 328 } 329 return expected_position; 330 } 331 332 ctx.font = '50px sans-serif'; 333 ctx.direction = 'ltr'; 334 ctx.textAlign = 'right'; 335 ctx.letterSpacing = ''; 336 337 // The leading and trailing '-' cause the string to always follow 338 // the specified direction, even though the interior will always be ltr. 339 const text = '-0123456789-'; 340 341 // Points are multiples of the string width as reported by 342 // textMetrics.width. 343 const kPoints = [ 344 -2, 345 -1, 346 0, 347 1, 348 2 349 ] 350 351 const tm = ctx.measureText(text); 352 text_width = tm.width; 353 for (const multiple of kPoints) { 354 offset = multiple * text_width; 355 tm_position = tm.getIndexFromOffset(offset); 356 expected_position = computeExpected(text, text_width, offset); 357 assert_equals(tm_position, 358 expected_position, 359 "for " + text + " multiple " + multiple); 360 } 361 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction ltr and text align right."); 362 363 test(t => { 364 const canvas = new OffscreenCanvas(100, 50); 365 const ctx = canvas.getContext('2d'); 366 367 function computeExpected(text, text_width, offset) { 368 expected_position = 0; 369 if ('right' == 'center' && offset == 0) { 370 return text.length / 2; 371 } 372 if ('rtl' == 'ltr') { 373 if (offset >= text_width) { 374 return text.length; 375 } 376 if (offset <= -text_width) { 377 return 0; 378 } 379 // offset must be 0. 380 if ('right' == 'start' || 'right' == 'left') { 381 return 0; 382 } else { 383 return text.length; 384 } 385 } else { 386 if (offset >= text_width) { 387 return 0; 388 } 389 if (offset <= -text_width) { 390 return text.length; 391 } 392 // offset must be 0. 393 if ('right' == 'start' || 'right' == 'right') { 394 return 0; 395 } else { 396 return text.length; 397 } 398 } 399 return expected_position; 400 } 401 402 ctx.font = '50px sans-serif'; 403 ctx.direction = 'rtl'; 404 ctx.textAlign = 'right'; 405 ctx.letterSpacing = ''; 406 407 // The leading and trailing '-' cause the string to always follow 408 // the specified direction, even though the interior will always be ltr. 409 const text = '-0123456789-'; 410 411 // Points are multiples of the string width as reported by 412 // textMetrics.width. 413 const kPoints = [ 414 -2, 415 -1, 416 0, 417 1, 418 2 419 ] 420 421 const tm = ctx.measureText(text); 422 text_width = tm.width; 423 for (const multiple of kPoints) { 424 offset = multiple * text_width; 425 tm_position = tm.getIndexFromOffset(offset); 426 expected_position = computeExpected(text, text_width, offset); 427 assert_equals(tm_position, 428 expected_position, 429 "for " + text + " multiple " + multiple); 430 } 431 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction rtl and text align right."); 432 433 test(t => { 434 const canvas = new OffscreenCanvas(100, 50); 435 const ctx = canvas.getContext('2d'); 436 437 function computeExpected(text, text_width, offset) { 438 expected_position = 0; 439 if ('start' == 'center' && offset == 0) { 440 return text.length / 2; 441 } 442 if ('ltr' == 'ltr') { 443 if (offset >= text_width) { 444 return text.length; 445 } 446 if (offset <= -text_width) { 447 return 0; 448 } 449 // offset must be 0. 450 if ('start' == 'start' || 'start' == 'left') { 451 return 0; 452 } else { 453 return text.length; 454 } 455 } else { 456 if (offset >= text_width) { 457 return 0; 458 } 459 if (offset <= -text_width) { 460 return text.length; 461 } 462 // offset must be 0. 463 if ('start' == 'start' || 'start' == 'right') { 464 return 0; 465 } else { 466 return text.length; 467 } 468 } 469 return expected_position; 470 } 471 472 ctx.font = '50px sans-serif'; 473 ctx.direction = 'ltr'; 474 ctx.textAlign = 'start'; 475 ctx.letterSpacing = ''; 476 477 // The leading and trailing '-' cause the string to always follow 478 // the specified direction, even though the interior will always be ltr. 479 const text = '-0123456789-'; 480 481 // Points are multiples of the string width as reported by 482 // textMetrics.width. 483 const kPoints = [ 484 -2, 485 -1, 486 0, 487 1, 488 2 489 ] 490 491 const tm = ctx.measureText(text); 492 text_width = tm.width; 493 for (const multiple of kPoints) { 494 offset = multiple * text_width; 495 tm_position = tm.getIndexFromOffset(offset); 496 expected_position = computeExpected(text, text_width, offset); 497 assert_equals(tm_position, 498 expected_position, 499 "for " + text + " multiple " + multiple); 500 } 501 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction ltr and text align start."); 502 503 test(t => { 504 const canvas = new OffscreenCanvas(100, 50); 505 const ctx = canvas.getContext('2d'); 506 507 function computeExpected(text, text_width, offset) { 508 expected_position = 0; 509 if ('start' == 'center' && offset == 0) { 510 return text.length / 2; 511 } 512 if ('rtl' == 'ltr') { 513 if (offset >= text_width) { 514 return text.length; 515 } 516 if (offset <= -text_width) { 517 return 0; 518 } 519 // offset must be 0. 520 if ('start' == 'start' || 'start' == 'left') { 521 return 0; 522 } else { 523 return text.length; 524 } 525 } else { 526 if (offset >= text_width) { 527 return 0; 528 } 529 if (offset <= -text_width) { 530 return text.length; 531 } 532 // offset must be 0. 533 if ('start' == 'start' || 'start' == 'right') { 534 return 0; 535 } else { 536 return text.length; 537 } 538 } 539 return expected_position; 540 } 541 542 ctx.font = '50px sans-serif'; 543 ctx.direction = 'rtl'; 544 ctx.textAlign = 'start'; 545 ctx.letterSpacing = ''; 546 547 // The leading and trailing '-' cause the string to always follow 548 // the specified direction, even though the interior will always be ltr. 549 const text = '-0123456789-'; 550 551 // Points are multiples of the string width as reported by 552 // textMetrics.width. 553 const kPoints = [ 554 -2, 555 -1, 556 0, 557 1, 558 2 559 ] 560 561 const tm = ctx.measureText(text); 562 text_width = tm.width; 563 for (const multiple of kPoints) { 564 offset = multiple * text_width; 565 tm_position = tm.getIndexFromOffset(offset); 566 expected_position = computeExpected(text, text_width, offset); 567 assert_equals(tm_position, 568 expected_position, 569 "for " + text + " multiple " + multiple); 570 } 571 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction rtl and text align start."); 572 573 test(t => { 574 const canvas = new OffscreenCanvas(100, 50); 575 const ctx = canvas.getContext('2d'); 576 577 function computeExpected(text, text_width, offset) { 578 expected_position = 0; 579 if ('end' == 'center' && offset == 0) { 580 return text.length / 2; 581 } 582 if ('ltr' == 'ltr') { 583 if (offset >= text_width) { 584 return text.length; 585 } 586 if (offset <= -text_width) { 587 return 0; 588 } 589 // offset must be 0. 590 if ('end' == 'start' || 'end' == 'left') { 591 return 0; 592 } else { 593 return text.length; 594 } 595 } else { 596 if (offset >= text_width) { 597 return 0; 598 } 599 if (offset <= -text_width) { 600 return text.length; 601 } 602 // offset must be 0. 603 if ('end' == 'start' || 'end' == 'right') { 604 return 0; 605 } else { 606 return text.length; 607 } 608 } 609 return expected_position; 610 } 611 612 ctx.font = '50px sans-serif'; 613 ctx.direction = 'ltr'; 614 ctx.textAlign = 'end'; 615 ctx.letterSpacing = ''; 616 617 // The leading and trailing '-' cause the string to always follow 618 // the specified direction, even though the interior will always be ltr. 619 const text = '-0123456789-'; 620 621 // Points are multiples of the string width as reported by 622 // textMetrics.width. 623 const kPoints = [ 624 -2, 625 -1, 626 0, 627 1, 628 2 629 ] 630 631 const tm = ctx.measureText(text); 632 text_width = tm.width; 633 for (const multiple of kPoints) { 634 offset = multiple * text_width; 635 tm_position = tm.getIndexFromOffset(offset); 636 expected_position = computeExpected(text, text_width, offset); 637 assert_equals(tm_position, 638 expected_position, 639 "for " + text + " multiple " + multiple); 640 } 641 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction ltr and text align end."); 642 643 test(t => { 644 const canvas = new OffscreenCanvas(100, 50); 645 const ctx = canvas.getContext('2d'); 646 647 function computeExpected(text, text_width, offset) { 648 expected_position = 0; 649 if ('end' == 'center' && offset == 0) { 650 return text.length / 2; 651 } 652 if ('rtl' == 'ltr') { 653 if (offset >= text_width) { 654 return text.length; 655 } 656 if (offset <= -text_width) { 657 return 0; 658 } 659 // offset must be 0. 660 if ('end' == 'start' || 'end' == 'left') { 661 return 0; 662 } else { 663 return text.length; 664 } 665 } else { 666 if (offset >= text_width) { 667 return 0; 668 } 669 if (offset <= -text_width) { 670 return text.length; 671 } 672 // offset must be 0. 673 if ('end' == 'start' || 'end' == 'right') { 674 return 0; 675 } else { 676 return text.length; 677 } 678 } 679 return expected_position; 680 } 681 682 ctx.font = '50px sans-serif'; 683 ctx.direction = 'rtl'; 684 ctx.textAlign = 'end'; 685 ctx.letterSpacing = ''; 686 687 // The leading and trailing '-' cause the string to always follow 688 // the specified direction, even though the interior will always be ltr. 689 const text = '-0123456789-'; 690 691 // Points are multiples of the string width as reported by 692 // textMetrics.width. 693 const kPoints = [ 694 -2, 695 -1, 696 0, 697 1, 698 2 699 ] 700 701 const tm = ctx.measureText(text); 702 text_width = tm.width; 703 for (const multiple of kPoints) { 704 offset = multiple * text_width; 705 tm_position = tm.getIndexFromOffset(offset); 706 expected_position = computeExpected(text, text_width, offset); 707 assert_equals(tm_position, 708 expected_position, 709 "for " + text + " multiple " + multiple); 710 } 711 }, "Check that TextMetrics::getIndexFromOffset() gives correct edges when the requested point is outside the range, with direction rtl and text align end."); 712 713 </script>