Skip to content

Commit e515834

Browse files
Implement ctx.strokeTextCluster()
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(). A line that was duplicated on the tests for fillTextCluster() was also removed. Bug: 341213359 Change-Id: Icd792b56e48f3ea5091e958671c19fcefe67fdec Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6282180 Commit-Queue: Andres Ricardo Perez <[email protected]> Reviewed-by: Fernando Serboncini <[email protected]> Reviewed-by: Stephen Chenney <[email protected]> Cr-Commit-Position: refs/heads/main@{#1425171}
1 parent 1171011 commit e515834

File tree

53 files changed

+2660
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2660
-72
lines changed

html/canvas/offscreen/text/2d.text.measure.text-clusters-rendering-align.tentative-expected.html html/canvas/element/text/2d.text.measure.fillTextCluster-align.tentative-expected.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-align.tentative</title>
6-
<h1>2d.text.measure.text-clusters-rendering-align.tentative</h1>
5+
<title>Canvas test: 2d.text.measure.fillTextCluster-align.tentative</title>
6+
<h1>2d.text.measure.fillTextCluster-align.tentative</h1>
77
<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>
88

99
<div class="grid-container" style="--grid-width: 3">

html/canvas/element/text/2d.text.measure.text-clusters-rendering-align.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-align.tentative.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<link rel="match" href="2d.text.measure.text-clusters-rendering-align.tentative-expected.html">
6-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-align.tentative</title>
7-
<h1>2d.text.measure.text-clusters-rendering-align.tentative</h1>
5+
<link rel="match" href="2d.text.measure.fillTextCluster-align.tentative-expected.html">
6+
<title>Canvas test: 2d.text.measure.fillTextCluster-align.tentative</title>
7+
<h1>2d.text.measure.fillTextCluster-align.tentative</h1>
88
<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>
99

1010
<div class="grid-container" style="--grid-width: 3">

html/canvas/offscreen/text/2d.text.measure.text-clusters-rendering-baseline.tentative-expected.html html/canvas/element/text/2d.text.measure.fillTextCluster-baseline.tentative-expected.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-baseline.tentative</title>
6-
<h1>2d.text.measure.text-clusters-rendering-baseline.tentative</h1>
5+
<title>Canvas test: 2d.text.measure.fillTextCluster-baseline.tentative</title>
6+
<h1>2d.text.measure.fillTextCluster-baseline.tentative</h1>
77
<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>
88

99
<div class="grid-container" style="--grid-width: 4">

html/canvas/element/text/2d.text.measure.text-clusters-rendering-baseline.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-baseline.tentative.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<link rel="match" href="2d.text.measure.text-clusters-rendering-baseline.tentative-expected.html">
6-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-baseline.tentative</title>
7-
<h1>2d.text.measure.text-clusters-rendering-baseline.tentative</h1>
5+
<link rel="match" href="2d.text.measure.fillTextCluster-baseline.tentative-expected.html">
6+
<title>Canvas test: 2d.text.measure.fillTextCluster-baseline.tentative</title>
7+
<h1>2d.text.measure.fillTextCluster-baseline.tentative</h1>
88
<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>
99

1010
<div class="grid-container" style="--grid-width: 4">

html/canvas/element/text/2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative-expected.html html/canvas/element/text/2d.text.measure.fillTextCluster-drawing-styles-change.tentative-expected.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</title>
6-
<h1>2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</h1>
5+
<title>Canvas test: 2d.text.measure.fillTextCluster-drawing-styles-change.tentative</title>
6+
<h1>2d.text.measure.fillTextCluster-drawing-styles-change.tentative</h1>
77
<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>
88

99
<div class="grid-container" style="--grid-width: 2">

html/canvas/element/text/2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-drawing-styles-change.tentative.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
44
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5-
<link rel="match" href="2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative-expected.html">
6-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</title>
7-
<h1>2d.text.measure.text-clusters-rendering-drawing-styles-change.tentative</h1>
5+
<link rel="match" href="2d.text.measure.fillTextCluster-drawing-styles-change.tentative-expected.html">
6+
<title>Canvas test: 2d.text.measure.fillTextCluster-drawing-styles-change.tentative</title>
7+
<h1>2d.text.measure.fillTextCluster-drawing-styles-change.tentative</h1>
88
<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>
99

1010
<div class="grid-container" style="--grid-width: 2">

html/canvas/element/text/2d.text.measure.text-clusters-rendering-font-change.tentative-expected.html html/canvas/element/text/2d.text.measure.fillTextCluster-font-change.tentative-expected.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
4-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-font-change.tentative</title>
5-
<h1>2d.text.measure.text-clusters-rendering-font-change.tentative</h1>
4+
<title>Canvas test: 2d.text.measure.fillTextCluster-font-change.tentative</title>
5+
<h1>2d.text.measure.fillTextCluster-font-change.tentative</h1>
66
<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>
77
<canvas id="canvas" width="500" height="200">
88
<p class="fallback">FAIL (fallback content)</p>

html/canvas/element/text/2d.text.measure.text-clusters-rendering-font-change.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-font-change.tentative.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!DOCTYPE html>
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
4-
<link rel="match" href="2d.text.measure.text-clusters-rendering-font-change.tentative-expected.html">
5-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-font-change.tentative</title>
6-
<h1>2d.text.measure.text-clusters-rendering-font-change.tentative</h1>
4+
<link rel="match" href="2d.text.measure.fillTextCluster-font-change.tentative-expected.html">
5+
<title>Canvas test: 2d.text.measure.fillTextCluster-font-change.tentative</title>
6+
<h1>2d.text.measure.fillTextCluster-font-change.tentative</h1>
77
<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>
88
<canvas id="canvas" width="500" height="200">
99
<p class="fallback">FAIL (fallback content)</p>

html/canvas/element/text/2d.text.measure.text-clusters-rendering-options.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-options.tentative.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
4-
<title>Canvas test: 2d.text.measure.text-clusters-rendering-options.tentative</title>
4+
<title>Canvas test: 2d.text.measure.fillTextCluster-options.tentative</title>
55
<script src="/resources/testharness.js"></script>
66
<script src="/resources/testharnessreport.js"></script>
77
<script src="/html/canvas/resources/canvas-tests.js"></script>
@@ -14,7 +14,7 @@
1414
</style>
1515
<body class="show_output">
1616

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

2020

@@ -52,7 +52,6 @@ <h1>2d.text.measure.text-clusters-rendering-options.tentative</h1>
5252
_assertPixelApprox(canvas, 45,45, 0,255,0,255, 2);
5353
_assertPixelApprox(canvas, 55,5, 255,0,0,255, 2);
5454
_assertPixelApprox(canvas, 5,55, 255,0,0,255, 2);
55-
_assertPixelApprox(canvas, 5,55, 255,0,0,255, 2);
5655
_assertPixelApprox(canvas, 55,55, 255,0,0,255, 2);
5756

5857
// Override the x and y values of the cluster.

html/canvas/element/text/2d.text.measure.text-clusters-range.tentative.html html/canvas/element/text/2d.text.measure.fillTextCluster-range.tentative.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
33
<meta charset="UTF-8">
4-
<title>Canvas test: 2d.text.measure.text-clusters-range.tentative</title>
4+
<title>Canvas test: 2d.text.measure.fillTextCluster-range.tentative</title>
55
<script src="/resources/testharness.js"></script>
66
<script src="/resources/testharnessreport.js"></script>
77
<script src="/html/canvas/resources/canvas-tests.js"></script>
@@ -14,7 +14,7 @@
1414
</style>
1515
<body class="show_output">
1616

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

2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5+
<title>Canvas test: 2d.text.measure.strokeTextCluster-align.tentative</title>
6+
<h1>2d.text.measure.strokeTextCluster-align.tentative</h1>
7+
<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>
8+
9+
<div class="grid-container" style="--grid-width: 3">
10+
<span>
11+
<div>ctx_align_left</div>
12+
<canvas class="grid-cell-content" id="canvas0" width="250" height="43">
13+
<p class="fallback">FAIL (fallback content)</p>
14+
</canvas>
15+
<script type="module">
16+
const canvas = document.getElementById("canvas0");
17+
const ctx = canvas.getContext('2d');
18+
19+
ctx.font = '20px serif';
20+
const text = 'Test ☺️ א';
21+
const x = canvas.width / 2;
22+
const y = canvas.height / 2;
23+
24+
ctx.textAlign = 'left';
25+
26+
// Rendering all clusters with the same (x, y) parameters must be
27+
// equivalent to a strokeText() call at (x, y).
28+
ctx.strokeText(text, x, y);
29+
</script>
30+
</span>
31+
32+
<span>
33+
<div>ctx_align_center</div>
34+
<canvas class="grid-cell-content" id="canvas1" width="250" height="43">
35+
<p class="fallback">FAIL (fallback content)</p>
36+
</canvas>
37+
<script type="module">
38+
const canvas = document.getElementById("canvas1");
39+
const ctx = canvas.getContext('2d');
40+
41+
ctx.font = '20px serif';
42+
const text = 'Test ☺️ א';
43+
const x = canvas.width / 2;
44+
const y = canvas.height / 2;
45+
46+
ctx.textAlign = 'center';
47+
48+
// Rendering all clusters with the same (x, y) parameters must be
49+
// equivalent to a strokeText() call at (x, y).
50+
ctx.strokeText(text, x, y);
51+
</script>
52+
</span>
53+
54+
<span>
55+
<div>ctx_align_right</div>
56+
<canvas class="grid-cell-content" id="canvas2" width="250" height="43">
57+
<p class="fallback">FAIL (fallback content)</p>
58+
</canvas>
59+
<script type="module">
60+
const canvas = document.getElementById("canvas2");
61+
const ctx = canvas.getContext('2d');
62+
63+
ctx.font = '20px serif';
64+
const text = 'Test ☺️ א';
65+
const x = canvas.width / 2;
66+
const y = canvas.height / 2;
67+
68+
ctx.textAlign = 'right';
69+
70+
// Rendering all clusters with the same (x, y) parameters must be
71+
// equivalent to a strokeText() call at (x, y).
72+
ctx.strokeText(text, x, y);
73+
</script>
74+
</span>
75+
76+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5+
<link rel="match" href="2d.text.measure.strokeTextCluster-align.tentative-expected.html">
6+
<title>Canvas test: 2d.text.measure.strokeTextCluster-align.tentative</title>
7+
<h1>2d.text.measure.strokeTextCluster-align.tentative</h1>
8+
<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>
9+
10+
<div class="grid-container" style="--grid-width: 3">
11+
<span>
12+
<div>ctx_align_left</div>
13+
<canvas class="grid-cell-content" id="canvas0" width="250" height="43">
14+
<p class="fallback">FAIL (fallback content)</p>
15+
</canvas>
16+
<script type="module">
17+
const canvas = document.getElementById("canvas0");
18+
const ctx = canvas.getContext('2d');
19+
20+
ctx.font = '20px serif';
21+
const text = 'Test ☺️ א';
22+
const x = canvas.width / 2;
23+
const y = canvas.height / 2;
24+
25+
ctx.textAlign = 'left';
26+
let tm = ctx.measureText(text);
27+
const clusters = tm.getTextClusters();
28+
29+
// Rendering all clusters with the same (x, y) parameters must be
30+
// equivalent to a strokeText() call at (x, y).
31+
for (const cluster of clusters) {
32+
ctx.strokeTextCluster(cluster, x, y);
33+
}
34+
</script>
35+
</span>
36+
37+
<span>
38+
<div>ctx_align_center</div>
39+
<canvas class="grid-cell-content" id="canvas1" width="250" height="43">
40+
<p class="fallback">FAIL (fallback content)</p>
41+
</canvas>
42+
<script type="module">
43+
const canvas = document.getElementById("canvas1");
44+
const ctx = canvas.getContext('2d');
45+
46+
ctx.font = '20px serif';
47+
const text = 'Test ☺️ א';
48+
const x = canvas.width / 2;
49+
const y = canvas.height / 2;
50+
51+
ctx.textAlign = 'center';
52+
let tm = ctx.measureText(text);
53+
const clusters = tm.getTextClusters();
54+
55+
// Rendering all clusters with the same (x, y) parameters must be
56+
// equivalent to a strokeText() call at (x, y).
57+
for (const cluster of clusters) {
58+
ctx.strokeTextCluster(cluster, x, y);
59+
}
60+
</script>
61+
</span>
62+
63+
<span>
64+
<div>ctx_align_right</div>
65+
<canvas class="grid-cell-content" id="canvas2" width="250" height="43">
66+
<p class="fallback">FAIL (fallback content)</p>
67+
</canvas>
68+
<script type="module">
69+
const canvas = document.getElementById("canvas2");
70+
const ctx = canvas.getContext('2d');
71+
72+
ctx.font = '20px serif';
73+
const text = 'Test ☺️ א';
74+
const x = canvas.width / 2;
75+
const y = canvas.height / 2;
76+
77+
ctx.textAlign = 'right';
78+
let tm = ctx.measureText(text);
79+
const clusters = tm.getTextClusters();
80+
81+
// Rendering all clusters with the same (x, y) parameters must be
82+
// equivalent to a strokeText() call at (x, y).
83+
for (const cluster of clusters) {
84+
ctx.strokeTextCluster(cluster, x, y);
85+
}
86+
</script>
87+
</span>
88+
89+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<link rel="stylesheet" href="/html/canvas/resources/canvas-grid-reftest.css">
5+
<title>Canvas test: 2d.text.measure.strokeTextCluster-baseline.tentative</title>
6+
<h1>2d.text.measure.strokeTextCluster-baseline.tentative</h1>
7+
<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>
8+
9+
<div class="grid-container" style="--grid-width: 4">
10+
<span>
11+
<div>ctx_baseline_top</div>
12+
<canvas class="grid-cell-content" id="canvas0" width="180" height="43">
13+
<p class="fallback">FAIL (fallback content)</p>
14+
</canvas>
15+
<script type="module">
16+
const canvas = document.getElementById("canvas0");
17+
const ctx = canvas.getContext('2d');
18+
19+
ctx.font = '20px serif';
20+
const text = 'Test ☺️ א';
21+
const x = 20;
22+
const y = canvas.height / 2;
23+
24+
ctx.textBaseline = 'top';
25+
ctx.strokeText(text, x, y);
26+
</script>
27+
</span>
28+
29+
<span>
30+
<div>ctx_baseline_middle</div>
31+
<canvas class="grid-cell-content" id="canvas1" width="180" height="43">
32+
<p class="fallback">FAIL (fallback content)</p>
33+
</canvas>
34+
<script type="module">
35+
const canvas = document.getElementById("canvas1");
36+
const ctx = canvas.getContext('2d');
37+
38+
ctx.font = '20px serif';
39+
const text = 'Test ☺️ א';
40+
const x = 20;
41+
const y = canvas.height / 2;
42+
43+
ctx.textBaseline = 'middle';
44+
ctx.strokeText(text, x, y);
45+
</script>
46+
</span>
47+
48+
<span>
49+
<div>ctx_baseline_bottom</div>
50+
<canvas class="grid-cell-content" id="canvas2" width="180" height="43">
51+
<p class="fallback">FAIL (fallback content)</p>
52+
</canvas>
53+
<script type="module">
54+
const canvas = document.getElementById("canvas2");
55+
const ctx = canvas.getContext('2d');
56+
57+
ctx.font = '20px serif';
58+
const text = 'Test ☺️ א';
59+
const x = 20;
60+
const y = canvas.height / 2;
61+
62+
ctx.textBaseline = 'bottom';
63+
ctx.strokeText(text, x, y);
64+
</script>
65+
</span>
66+
67+
<span>
68+
<div>ctx_baseline_alphabetic</div>
69+
<canvas class="grid-cell-content" id="canvas3" width="180" height="43">
70+
<p class="fallback">FAIL (fallback content)</p>
71+
</canvas>
72+
<script type="module">
73+
const canvas = document.getElementById("canvas3");
74+
const ctx = canvas.getContext('2d');
75+
76+
ctx.font = '20px serif';
77+
const text = 'Test ☺️ א';
78+
const x = 20;
79+
const y = canvas.height / 2;
80+
81+
ctx.textBaseline = 'alphabetic';
82+
ctx.strokeText(text, x, y);
83+
</script>
84+
</span>
85+
86+
</div>

0 commit comments

Comments
 (0)