tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

2d.text.measure.getActualBoundingBox-full-text.tentative.worker.js (8144B)


      1 // DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py.
      2 // OffscreenCanvas test in a worker:2d.text.measure.getActualBoundingBox-full-text.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(800, 200);
     11  const ctx = canvas.getContext('2d');
     12 
     13  // Use measureText to create a rect for the whole text
     14  function getFullTextBoundingBox(text) {
     15    const tm = ctx.measureText(text);
     16    return {
     17      x: -tm.actualBoundingBoxLeft,
     18      y: -tm.actualBoundingBoxAscent,
     19      width: tm.actualBoundingBoxLeft + tm.actualBoundingBoxRight,
     20      height: tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent
     21    };
     22  }
     23 
     24  function checkRectsMatch(rect_a, rect_b) {
     25    assert_approx_equals(rect_a.x, rect_b.x, 1.0);
     26    assert_approx_equals(rect_a.y, rect_b.y, 1.0);
     27    assert_approx_equals(rect_a.width, rect_b.width, 1.0);
     28    assert_approx_equals(rect_a.height, rect_b.height, 1.0);
     29  }
     30 
     31  const kAligns = [
     32    'left',
     33    'center',
     34    'right',
     35  ];
     36 
     37  const kBaselines = [
     38    'top',
     39    'hanging',
     40    'middle',
     41    'alphabetic',
     42    'ideographic',
     43    'bottom',
     44  ];
     45 
     46  const kTexts = [
     47    'UNAVAILABLE',
     48    '🏁🎶🏁',
     49    ')(あ)(',
     50    '-abcd ',
     51    'اين المكتبة؟',
     52    'bidiالرياضيات'
     53  ]
     54 
     55  ctx.font = '50px sans-serif';
     56  ctx.direction = 'ltr';
     57  for (const align of kAligns) {
     58    for (const baseline of kBaselines) {
     59      ctx.textAlign = align;
     60      ctx.textBaseline = baseline;
     61      for (text of kTexts) {
     62        const tm = ctx.measureText(text);
     63        const rect_from_api = tm.getActualBoundingBox(0, text.length);
     64        const rect_from_full_bounds = getFullTextBoundingBox(text);
     65        checkRectsMatch(rect_from_api, rect_from_full_bounds)
     66      }
     67    }
     68  }
     69 }, "Test TextMetrics::getActualBoundingBox() for the full length of the string for some edge cases, with direction ltr and no-directional-override");
     70 
     71 test(t => {
     72  const canvas = new OffscreenCanvas(800, 200);
     73  const ctx = canvas.getContext('2d');
     74 
     75  // Use measureText to create a rect for the whole text
     76  function getFullTextBoundingBox(text) {
     77    const tm = ctx.measureText(text);
     78    return {
     79      x: -tm.actualBoundingBoxLeft,
     80      y: -tm.actualBoundingBoxAscent,
     81      width: tm.actualBoundingBoxLeft + tm.actualBoundingBoxRight,
     82      height: tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent
     83    };
     84  }
     85 
     86  function checkRectsMatch(rect_a, rect_b) {
     87    assert_approx_equals(rect_a.x, rect_b.x, 1.0);
     88    assert_approx_equals(rect_a.y, rect_b.y, 1.0);
     89    assert_approx_equals(rect_a.width, rect_b.width, 1.0);
     90    assert_approx_equals(rect_a.height, rect_b.height, 1.0);
     91  }
     92 
     93  const kAligns = [
     94    'left',
     95    'center',
     96    'right',
     97  ];
     98 
     99  const kBaselines = [
    100    'top',
    101    'hanging',
    102    'middle',
    103    'alphabetic',
    104    'ideographic',
    105    'bottom',
    106  ];
    107 
    108  const kTexts = [
    109    'UNAVAILABLE',
    110    '🏁🎶🏁',
    111    ')(あ)(',
    112    '-abcd ',
    113    'اين المكتبة؟',
    114    'bidiالرياضيات'
    115  ]
    116 
    117  ctx.font = '50px sans-serif';
    118  ctx.direction = 'rtl';
    119  for (const align of kAligns) {
    120    for (const baseline of kBaselines) {
    121      ctx.textAlign = align;
    122      ctx.textBaseline = baseline;
    123      for (text of kTexts) {
    124        const tm = ctx.measureText(text);
    125        const rect_from_api = tm.getActualBoundingBox(0, text.length);
    126        const rect_from_full_bounds = getFullTextBoundingBox(text);
    127        checkRectsMatch(rect_from_api, rect_from_full_bounds)
    128      }
    129    }
    130  }
    131 }, "Test TextMetrics::getActualBoundingBox() for the full length of the string for some edge cases, with direction rtl and no-directional-override");
    132 
    133 test(t => {
    134  const canvas = new OffscreenCanvas(800, 200);
    135  const ctx = canvas.getContext('2d');
    136 
    137  // Use measureText to create a rect for the whole text
    138  function getFullTextBoundingBox(text) {
    139    const tm = ctx.measureText(text);
    140    return {
    141      x: -tm.actualBoundingBoxLeft,
    142      y: -tm.actualBoundingBoxAscent,
    143      width: tm.actualBoundingBoxLeft + tm.actualBoundingBoxRight,
    144      height: tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent
    145    };
    146  }
    147 
    148  function checkRectsMatch(rect_a, rect_b) {
    149    assert_approx_equals(rect_a.x, rect_b.x, 1.0);
    150    assert_approx_equals(rect_a.y, rect_b.y, 1.0);
    151    assert_approx_equals(rect_a.width, rect_b.width, 1.0);
    152    assert_approx_equals(rect_a.height, rect_b.height, 1.0);
    153  }
    154 
    155  function addDirectionalOverrideCharacters(text, direction_is_ltr) {
    156    // source: www.w3.org/International/questions/qa-bidi-unicode-controls.en
    157    const LTR_OVERRIDE = '\u202D';
    158    const RTL_OVERRIDE = '\u202E';
    159    const OVERRIDE_END = '\u202C';
    160    if (direction_is_ltr) {
    161      return LTR_OVERRIDE + text + OVERRIDE_END;
    162    }
    163    return RTL_OVERRIDE + text + OVERRIDE_END;
    164  }
    165 
    166  const kAligns = [
    167    'left',
    168    'center',
    169    'right',
    170  ];
    171 
    172  const kBaselines = [
    173    'top',
    174    'hanging',
    175    'middle',
    176    'alphabetic',
    177    'ideographic',
    178    'bottom',
    179  ];
    180 
    181  const kTexts = [
    182    'UNAVAILABLE',
    183    '🏁🎶🏁',
    184    ')(あ)(',
    185    '-abcd ',
    186    'اين المكتبة؟',
    187    'bidiالرياضيات'
    188  ]
    189 
    190  ctx.font = '50px sans-serif';
    191  ctx.direction = 'ltr';
    192  for (const align of kAligns) {
    193    for (const baseline of kBaselines) {
    194      ctx.textAlign = align;
    195      ctx.textBaseline = baseline;
    196      for (text of kTexts) {
    197        text = addDirectionalOverrideCharacters(text, ctx.direction == 'ltr');
    198        const tm = ctx.measureText(text);
    199        const rect_from_api = tm.getActualBoundingBox(0, text.length);
    200        const rect_from_full_bounds = getFullTextBoundingBox(text);
    201        checkRectsMatch(rect_from_api, rect_from_full_bounds)
    202      }
    203    }
    204  }
    205 }, "Test TextMetrics::getActualBoundingBox() for the full length of the string for some edge cases, with direction ltr and directional-override");
    206 
    207 test(t => {
    208  const canvas = new OffscreenCanvas(800, 200);
    209  const ctx = canvas.getContext('2d');
    210 
    211  // Use measureText to create a rect for the whole text
    212  function getFullTextBoundingBox(text) {
    213    const tm = ctx.measureText(text);
    214    return {
    215      x: -tm.actualBoundingBoxLeft,
    216      y: -tm.actualBoundingBoxAscent,
    217      width: tm.actualBoundingBoxLeft + tm.actualBoundingBoxRight,
    218      height: tm.actualBoundingBoxAscent + tm.actualBoundingBoxDescent
    219    };
    220  }
    221 
    222  function checkRectsMatch(rect_a, rect_b) {
    223    assert_approx_equals(rect_a.x, rect_b.x, 1.0);
    224    assert_approx_equals(rect_a.y, rect_b.y, 1.0);
    225    assert_approx_equals(rect_a.width, rect_b.width, 1.0);
    226    assert_approx_equals(rect_a.height, rect_b.height, 1.0);
    227  }
    228 
    229  function addDirectionalOverrideCharacters(text, direction_is_ltr) {
    230    // source: www.w3.org/International/questions/qa-bidi-unicode-controls.en
    231    const LTR_OVERRIDE = '\u202D';
    232    const RTL_OVERRIDE = '\u202E';
    233    const OVERRIDE_END = '\u202C';
    234    if (direction_is_ltr) {
    235      return LTR_OVERRIDE + text + OVERRIDE_END;
    236    }
    237    return RTL_OVERRIDE + text + OVERRIDE_END;
    238  }
    239 
    240  const kAligns = [
    241    'left',
    242    'center',
    243    'right',
    244  ];
    245 
    246  const kBaselines = [
    247    'top',
    248    'hanging',
    249    'middle',
    250    'alphabetic',
    251    'ideographic',
    252    'bottom',
    253  ];
    254 
    255  const kTexts = [
    256    'UNAVAILABLE',
    257    '🏁🎶🏁',
    258    ')(あ)(',
    259    '-abcd ',
    260    'اين المكتبة؟',
    261    'bidiالرياضيات'
    262  ]
    263 
    264  ctx.font = '50px sans-serif';
    265  ctx.direction = 'rtl';
    266  for (const align of kAligns) {
    267    for (const baseline of kBaselines) {
    268      ctx.textAlign = align;
    269      ctx.textBaseline = baseline;
    270      for (text of kTexts) {
    271        text = addDirectionalOverrideCharacters(text, ctx.direction == 'ltr');
    272        const tm = ctx.measureText(text);
    273        const rect_from_api = tm.getActualBoundingBox(0, text.length);
    274        const rect_from_full_bounds = getFullTextBoundingBox(text);
    275        checkRectsMatch(rect_from_api, rect_from_full_bounds)
    276      }
    277    }
    278  }
    279 }, "Test TextMetrics::getActualBoundingBox() for the full length of the string for some edge cases, with direction rtl and directional-override");
    280 
    281 done();