Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 1 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /html/canvas/offscreen/text/2d.text.measure.selection-rects-baselines.tentative.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>OffscreenCanvas test: 2d.text.measure.selection-rects-baselines.tentative</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
<h1>2d.text.measure.selection-rects-baselines.tentative</h1>
<p class="desc">Check that TextMetrics::getSelectionRects() works correctly with textBaseline.</p>
<script>
var t = async_test("Check that TextMetrics::getSelectionRects() works correctly with textBaseline.");
var t_pass = t.done.bind(t);
var t_fail = t.step_func(function(reason) {
throw reason;
});
t.step(function() {
var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');
ctx.font = '50px sans-serif';
const kBaselines = [
"top",
"hanging",
"middle",
"alphabetic",
"ideographic",
"bottom",
];
const kTexts = [
'UNAVAILABLE',
'🏁🎶🏁',
')(あ)(',
'-abcd_',
'اين المكتبة؟',
'bidiالرياضيات'
]
for (const text of kTexts) {
for (const baseline of kBaselines) {
const tm = ctx.measureText(text);
// First character.
for (const r of tm.getSelectionRects(0, 1)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
// Last character.
for (const r of tm.getSelectionRects(text.length - 1, text.length)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
// Whole string.
for (const r of tm.getSelectionRects(0, text.length)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
// Intermediate string.
for (const r of tm.getSelectionRects(1, text.length - 1)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
// Invalid start > end string. Creates 0 width rectangle.
for (const r of tm.getSelectionRects(3, 2)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
for (const r of tm.getSelectionRects(1, 0)) {
assert_approx_equals(r.top, -tm.fontBoundingBoxAscent, 1.0);
assert_approx_equals(r.bottom, tm.fontBoundingBoxDescent, 1.0);
}
}
}
t.done();
});
</script>