Commit 9c810822 authored by Yi Xu's avatar Yi Xu Committed by Commit Bot

Fix Canvas text color when gradients and maxWidth are used

The location is always set to (0, 0) when maxWidth is used, so any
coloring system based on location doesn't work with maxWidth. Fix it
by avoiding reset the location for FillText.

Test is also added.

Bug: 1030655
Change-Id: I36a41212b3d165145e4e25dd3bfd923aad70d7c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2040536
Commit-Queue: Yi Xu <yiyix@chromium.org>
Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Reviewed-by: default avatarJuanmi Huertas <juanmihd@chromium.org>
Reviewed-by: default avatarAaron Krajeski <aaronhk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742925}
parent 4ca2df79
...@@ -907,11 +907,12 @@ void CanvasRenderingContext2D::DrawTextInternal( ...@@ -907,11 +907,12 @@ void CanvasRenderingContext2D::DrawTextInternal(
CanvasRenderingContext2DAutoRestoreSkCanvas state_restorer(this); CanvasRenderingContext2DAutoRestoreSkCanvas state_restorer(this);
if (use_max_width) { if (use_max_width) {
c->save(); c->save();
c->translate(location.X(), location.Y());
// We draw when fontWidth is 0 so compositing operations (eg, a "copy" op) // We draw when fontWidth is 0 so compositing operations (eg, a "copy" op)
// still work. // still work. As the width of canvas is scaled, so text can be scaled to
c->scale((font_width > 0 ? clampTo<float>(width / font_width) : 0), 1); // match the given maxwidth, update text location so it appears on desired
location = FloatPoint(); // place.
c->scale(clampTo<float>(width / font_width), 1);
location.SetX(location.X() / clampTo<float>(width / font_width));
} }
Draw( Draw(
......
<html>
<body>
<canvas id="c" width=800 height=1000></canvas>
<script>
var canvas = document.getElementById('c');
let ctx = canvas.getContext("2d")
let grd = ctx.createLinearGradient(0,800,0,0)
grd.addColorStop(0, "#FF0000")
grd.addColorStop(1, "#0000FF")
ctx.fillStyle = grd
ctx.font = "bold 20px Arial"
ctx.scale(0.5, 1)
ctx.fillText("LoMaxWidth", 500, 200)
</script>
</body>
</html>
<html>
<body>
<canvas id="c" width=800 height=1000></canvas>
<script>
var canvas = document.getElementById('c');
let ctx = canvas.getContext("2d")
let grd = ctx.createLinearGradient(0,800,0,0)
grd.addColorStop(0, "#FF0000")
grd.addColorStop(1, "#0000FF")
ctx.fillStyle = grd
ctx.font = "bold 20px Arial"
ctx.fillText("LoMaxWidth", 250, 200, ctx.measureText("LoMaxWidth").width/2)
</script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment