Skip to content

Commit

Permalink
Implement ctx.strokeTextCluster()
Browse files Browse the repository at this point in the history
This CL creates a new method for CanvasRenderingContext2d that strokes
a TextCluster object. Its fill counterpart, ctx.fillTextCluster(), was
merged in https://crrev.com/c/5854270.

This new method came to be from the discussion around the spec for the
TextMetrics additions: whatwg/html#11000

The existing tests for fillTextCluster() were renamed to include the
method name in the title, since before only "rendering" was mentioned.
The new tests are explicitly named using strokeTextCluster().

Bug: 341213359
Change-Id: Icd792b56e48f3ea5091e958671c19fcefe67fdec
  • Loading branch information
AndresRPerez12 authored and chromium-wpt-export-bot committed Feb 25, 2025
1 parent a52f979 commit 96c3947
Show file tree
Hide file tree
Showing 47 changed files with 2,120 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-align.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-align.tentative</h1>
<title>Canvas test: 2d.text.measure.fillTextCluster-align.tentative</title>
<h1>2d.text.measure.fillTextCluster-align.tentative</h1>
<p class="desc">Test that fillTextCluster() correctly positions the text, taking into account the textAlign from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<link rel="match" href="2d.text.measure.text-clusters-rendering-align.tentative-expected.html">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-align.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-align.tentative</h1>
<link rel="match" href="2d.text.measure.fillTextCluster-align.tentative-expected.html">
<title>Canvas test: 2d.text.measure.fillTextCluster-align.tentative</title>
<h1>2d.text.measure.fillTextCluster-align.tentative</h1>
<p class="desc">Test that fillTextCluster() correctly positions the text, taking into account the textAlign from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-baseline.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-baseline.tentative</h1>
<title>Canvas test: 2d.text.measure.fillTextCluster-baseline.tentative</title>
<h1>2d.text.measure.fillTextCluster-baseline.tentative</h1>
<p class="desc">Test that fillTextCluster() correctly positions the text, taking into account the textBaseline from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<link rel="match" href="2d.text.measure.text-clusters-rendering-baseline.tentative-expected.html">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-baseline.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-baseline.tentative</h1>
<link rel="match" href="2d.text.measure.fillTextCluster-baseline.tentative-expected.html">
<title>Canvas test: 2d.text.measure.fillTextCluster-baseline.tentative</title>
<h1>2d.text.measure.fillTextCluster-baseline.tentative</h1>
<p class="desc">Test that fillTextCluster() correctly positions the text, taking into account the textBaseline from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</h1>
<title>Canvas test: 2d.text.measure.fillTextCluster-drawing-styles-change.tentative</title>
<h1>2d.text.measure.fillTextCluster-drawing-styles-change.tentative</h1>
<p class="desc">Test that fillTextCluster() renders using the drawing styles as they were when `ctx.measureText()` was called, regardless of any changes in the context since.</p>

<div class="grid-container" style="--grid-width: 2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<link rel="match" href="2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative-expected.html">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</h1>
<link rel="match" href="2d.text.measure.fillTextCluster-drawing-styles-change.tentative-expected.html">
<title>Canvas test: 2d.text.measure.fillTextCluster-drawing-styles-change.tentative</title>
<h1>2d.text.measure.fillTextCluster-drawing-styles-change.tentative</h1>
<p class="desc">Test that fillTextCluster() renders using the drawing styles as they were when `ctx.measureText()` was called, regardless of any changes in the context since.</p>

<div class="grid-container" style="--grid-width: 2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!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.text-clusters-rendering-font-change.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-font-change.tentative</h1>
<title>Canvas test: 2d.text.measure.fillTextCluster-font-change.tentative</title>
<h1>2d.text.measure.fillTextCluster-font-change.tentative</h1>
<p class="desc">Test that fillTextCluster() renders in the font used originally when the text was measured, even if the font set on the context has changed since.</p>
<canvas id="canvas" width="500" height="200">
<p class="fallback">FAIL (fallback content)</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="match" href="2d.text.measure.text-clusters-rendering-font-change.tentative-expected.html">
<title>Canvas test: 2d.text.measure.text-clusters-rendering-font-change.tentative</title>
<h1>2d.text.measure.text-clusters-rendering-font-change.tentative</h1>
<link rel="match" href="2d.text.measure.fillTextCluster-font-change.tentative-expected.html">
<title>Canvas test: 2d.text.measure.fillTextCluster-font-change.tentative</title>
<h1>2d.text.measure.fillTextCluster-font-change.tentative</h1>
<p class="desc">Test that fillTextCluster() renders in the font used originally when the text was measured, even if the font set on the context has changed since.</p>
<canvas id="canvas" width="500" height="200">
<p class="fallback">FAIL (fallback content)</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!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.text-clusters-rendering-options.tentative</title>
<title>Canvas test: 2d.text.measure.fillTextCluster-options.tentative</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
Expand All @@ -14,7 +14,7 @@
</style>
<body class="show_output">

<h1>2d.text.measure.text-clusters-rendering-options.tentative</h1>
<h1>2d.text.measure.fillTextCluster-options.tentative</h1>
<p class="desc">Test that fillTextCluster() correctly applies the options passed as a dictionary.</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!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.text-clusters-range.tentative</title>
<title>Canvas test: 2d.text.measure.fillTextCluster-range.tentative</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/canvas-tests.js"></script>
Expand All @@ -14,7 +14,7 @@
</style>
<body class="show_output">

<h1>2d.text.measure.text-clusters-range.tentative</h1>
<h1>2d.text.measure.fillTextCluster-range.tentative</h1>
<p class="desc">Test that getTextClusters() and fillTextCluster() correctly render different ranges of the input text.</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<title>Canvas test: 2d.text.measure.strokeTextCluster-align.tentative</title>
<h1>2d.text.measure.strokeTextCluster-align.tentative</h1>
<p class="desc">Test that strokeTextCluster() correctly positions the text, taking into account the textAlign from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 3">
<span>
<div>ctx_align_left</div>
<canvas class="grid-cell-content" id="canvas0" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas0");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'left';

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
ctx.strokeText(text, x, y);
</script>
</span>

<span>
<div>ctx_align_center</div>
<canvas class="grid-cell-content" id="canvas1" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'center';

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
ctx.strokeText(text, x, y);
</script>
</span>

<span>
<div>ctx_align_right</div>
<canvas class="grid-cell-content" id="canvas2" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas2");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'right';

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
ctx.strokeText(text, x, y);
</script>
</span>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<link rel="match" href="2d.text.measure.strokeTextCluster-align.tentative-expected.html">
<title>Canvas test: 2d.text.measure.strokeTextCluster-align.tentative</title>
<h1>2d.text.measure.strokeTextCluster-align.tentative</h1>
<p class="desc">Test that strokeTextCluster() correctly positions the text, taking into account the textAlign from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 3">
<span>
<div>ctx_align_left</div>
<canvas class="grid-cell-content" id="canvas0" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas0");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'left';
let tm = ctx.measureText(text);
const clusters = tm.getTextClusters();

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
for (const cluster of clusters) {
ctx.strokeTextCluster(cluster, x, y);
}
</script>
</span>

<span>
<div>ctx_align_center</div>
<canvas class="grid-cell-content" id="canvas1" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'center';
let tm = ctx.measureText(text);
const clusters = tm.getTextClusters();

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
for (const cluster of clusters) {
ctx.strokeTextCluster(cluster, x, y);
}
</script>
</span>

<span>
<div>ctx_align_right</div>
<canvas class="grid-cell-content" id="canvas2" width="250" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas2");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = canvas.width / 2;
const y = canvas.height / 2;

ctx.textAlign = 'right';
let tm = ctx.measureText(text);
const clusters = tm.getTextClusters();

// Rendering all clusters with the same (x, y) parameters must be
// equivalent to a strokeText() call at (x, y).
for (const cluster of clusters) {
ctx.strokeTextCluster(cluster, x, y);
}
</script>
</span>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
<title>Canvas test: 2d.text.measure.strokeTextCluster-baseline.tentative</title>
<h1>2d.text.measure.strokeTextCluster-baseline.tentative</h1>
<p class="desc">Test that strokeTextCluster() correctly positions the text, taking into account the textBaseline from the context at the time the text was measured.</p>

<div class="grid-container" style="--grid-width: 4">
<span>
<div>ctx_baseline_top</div>
<canvas class="grid-cell-content" id="canvas0" width="180" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas0");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = 20;
const y = canvas.height / 2;

ctx.textBaseline = 'top';
ctx.strokeText(text, x, y);
</script>
</span>

<span>
<div>ctx_baseline_middle</div>
<canvas class="grid-cell-content" id="canvas1" width="180" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = 20;
const y = canvas.height / 2;

ctx.textBaseline = 'middle';
ctx.strokeText(text, x, y);
</script>
</span>

<span>
<div>ctx_baseline_bottom</div>
<canvas class="grid-cell-content" id="canvas2" width="180" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas2");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = 20;
const y = canvas.height / 2;

ctx.textBaseline = 'bottom';
ctx.strokeText(text, x, y);
</script>
</span>

<span>
<div>ctx_baseline_alphabetic</div>
<canvas class="grid-cell-content" id="canvas3" width="180" height="43">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script type="module">
const canvas = document.getElementById("canvas3");
const ctx = canvas.getContext('2d');

ctx.font = '20px serif';
const text = 'Test ☺️ א';
const x = 20;
const y = canvas.height / 2;

ctx.textBaseline = 'alphabetic';
ctx.strokeText(text, x, y);
</script>
</span>

</div>
Loading

0 comments on commit 96c3947

Please sign in to comment.