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.shape.position-to-advance.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.shape.position-to-advance.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.shape.position-to-advance.tentative</h1>
<p class="desc">Test ShapedText::positionToAdvance() and its symmetry with advanceToPosition() and glyphClusters().</p>
<script>
promise_test(async t => {
var canvas = new OffscreenCanvas(100, 50);
var ctx = canvas.getContext('2d');
var f = new FontFace("CanvasTest", "url('/fonts/CanvasTest.ttf')");
f.load();
document.fonts.add(f);
await document.fonts.ready;
ctx.font = '50px CanvasTest';
ctx.direction = 'ltr';
ctx.textAlign = 'left';
ctx.textBaseline = 'alphabetic';
const text = 'ABC';
const st = ctx.shapeText(text);
const clusters = st.glyphClusters();
_assertSame(clusters.length, 3, "clusters.length", "3");
assert_approx_equals(st.positionToAdvance(0), 0, 1e-4);
assert_approx_equals(st.positionToAdvance(1), clusters[1].x, 1e-4);
assert_approx_equals(st.positionToAdvance(2), clusters[2].x, 1e-4);
assert_approx_equals(st.positionToAdvance(3), st.width, 1e-4);
for (let i = 0; i <= text.length; ++i) {
const adv = st.positionToAdvance(i);
_assertSame(st.advanceToPosition(adv), i, "st.advanceToPosition(adv)", "i");
}
assert_throws_js(TypeError, () => st.positionToAdvance(-1));
assert_throws_dom("IndexSizeError", () => st.positionToAdvance(text.length + 1));
}, "Test ShapedText::positionToAdvance() and its symmetry with advanceToPosition() and glyphClusters().");
</script>