Truly independent web browser
LibWeb: Condense text for maxWidth around where it starts
Andreas Kling0e26f483f831
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rsindex 037048f1d23..701f7976891 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs@@ -124,7 +124,7 @@ fn translation(x: f32, y: f32) -> AffineTransform {}/// Where text of the width goes relative to the point that it is drawn at, and how much it is condensed to fit in the-/// maximum width.+/// maximum width. The text starts at x, around which it is condensed.#[allow(clippy::cast_possible_truncation,reason = "the transform is in floats, as the C++ had it"@@ -132,6 +132,7 @@ fn translation(x: f32, y: f32) -> AffineTransform {pub(crate) fn text_transform(state: &DrawingState,first_font: &FontHandle,+ x: f32,mut text_width: f32,max_width: Option<f64>,) -> AffineTransform {@@ -146,7 +147,7 @@ pub(crate) fn text_transform(&& text_width > max_width as f32{let horizontal_scale = max_width as f32 / text_width;- transform = AffineTransform::new(horizontal_scale, 0.0, 0.0, 1.0, 0.0, 0.0);+ transform = AffineTransform::new(horizontal_scale, 0.0, 0.0, 1.0, x - horizontal_scale * x, 0.0);text_width *= horizontal_scale;}diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rsindex 8369d9541f1..d0357de72c3 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs@@ -34,7 +34,7 @@ impl ContextState {let runs = shape_text(&font.cascade, text, FloatPoint { x, y }, letter_spacing);let path = path_of_runs(&runs);let text_width: f32 = runs.iter().map(|run| run.width).sum();- let transform = text_transform(&self.drawing_state(), &font.cascade.first(), text_width, max_width);+ let transform = text_transform(&self.drawing_state(), &font.cascade.first(), x, text_width, max_width);path.copy_transformed(transform.values)}}@@ -105,7 +105,7 @@ impl<K: CanvasKind> RenderingContext2D<K> {.collect();let first_font = font.cascade.first();let state = self.drawing_state();- let translation = text_transform(&state, &first_font, run.width, None).map_point(FloatPoint { x, y });+ let translation = text_transform(&state, &first_font, x, run.width, None).map_point(FloatPoint { x, y });let lent_font = LentFont(NonNull::new(run.font.as_raw().cast_mut()).expect("a font has a font"));self.record(|recorder| {let paint = paint_of(recorder, &state.fill_style);diff --git a/Tests/LibWeb/Screenshot/expected/canvas-text.png b/Tests/LibWeb/Screenshot/expected/canvas-text.pngindex 08b8a157ea0..befedd16d81 100644Binary files a/Tests/LibWeb/Screenshot/expected/canvas-text.png and b/Tests/LibWeb/Screenshot/expected/canvas-text.png differdiff --git a/Tests/LibWeb/Text/expected/canvas/fill-text-max-width-anchor.txt b/Tests/LibWeb/Text/expected/canvas/fill-text-max-width-anchor.txtnew file mode 100644index 00000000000..14fb67e21b1--- /dev/null+++ b/Tests/LibWeb/Text/expected/canvas/fill-text-max-width-anchor.txt@@ -0,0 +1,3 @@+fillText, start: ink outside the box: false, ink inside the box: true+strokeText, start: ink outside the box: false, ink inside the box: true+fillText, center: ink outside the box: false, ink inside the box: truediff --git a/Tests/LibWeb/Text/input/canvas/fill-text-max-width-anchor.html b/Tests/LibWeb/Text/input/canvas/fill-text-max-width-anchor.htmlnew file mode 100644index 00000000000..1f470aed4db--- /dev/null+++ b/Tests/LibWeb/Text/input/canvas/fill-text-max-width-anchor.html@@ -0,0 +1,30 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // Text that maxWidth condenses still starts at its x, rather than at x condensed towards the origin.+ function columnsWithInk(context, from, to) {+ const data = context.getImageData(from, 0, to - from, context.canvas.height).data;+ const columns = new Set();+ for (let i = 3; i < data.length; i += 4) {+ if (data[i] !== 0)+ columns.add(((i - 3) / 4) % (to - from));+ }+ return columns.size;+ }++ test(() => {+ for (const [method, align] of [["fillText", "start"], ["strokeText", "start"], ["fillText", "center"]]) {+ const canvas = document.createElement("canvas");+ canvas.width = 300;+ canvas.height = 100;+ const context = canvas.getContext("2d");+ context.font = "20px sans-serif";+ context.textAlign = align;+ context[method]("long text that is squeezed", 200, 50, 20);+ const [left, right] = align === "center" ? [190, 210] : [200, 220];+ println(+ `${method}, ${align}: ink outside the box: ${columnsWithInk(context, 0, left - 1) + columnsWithInk(context, right + 1, 300) > 0}, ink inside the box: ${columnsWithInk(context, left, right) > 0}`+ );+ }+ });+</script>