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/element/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>Canvas 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>
<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.text.measure.selection-rects-baselines.tentative</h1>
<p class="desc">Check that TextMetrics::getSelectionRects() works correctly with textBaseline.</p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("Check that TextMetrics::getSelectionRects() works correctly with textBaseline.");
_addTest(function(canvas, ctx) {
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);
}
}
}
});
</script>