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