Truly independent web browser
LibWeb: Replace ASCII whitespace in the text of fillText and strokeText
Andreas Klingd1d9f63e6cee
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rsindex d0357de72c3..7017d9f2924 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs@@ -58,7 +58,9 @@ impl<K: CanvasKind> RenderingContext2D<K> {if !are_finite(x, y, max_width) {return Ok(());}- let text = text.to_utf16();+ // https://html.spec.whatwg.org/multipage/canvas.html#text-preparation-algorithm+ // 2. Replace all ASCII whitespace in text with U+0020 SPACE characters.+ let text = replace_ascii_whitespace(&text.to_utf16());// Complex text effects continue to use the combined outline so they are applied once to the entire text,// including runs that use different fallback fonts.@@ -135,7 +137,10 @@ impl<K: CanvasKind> RenderingContext2D<K> {if !are_finite(x, y, max_width) {return Ok(());}- self.stroke_internal(&self.text_path(&text.to_utf16(), x, y, max_width));+ // https://html.spec.whatwg.org/multipage/canvas.html#text-preparation-algorithm+ // 2. Replace all ASCII whitespace in text with U+0020 SPACE characters.+ let text = replace_ascii_whitespace(&text.to_utf16());+ self.stroke_internal(&self.text_path(&text, x, y, max_width));Ok(())}diff --git a/Tests/LibWeb/Text/expected/canvas/text-ascii-whitespace.txt b/Tests/LibWeb/Text/expected/canvas/text-ascii-whitespace.txtnew file mode 100644index 00000000000..62083136f30--- /dev/null+++ b/Tests/LibWeb/Text/expected/canvas/text-ascii-whitespace.txt@@ -0,0 +1,16 @@+tab: measureText(): true+tab: fillText(): true+tab: fillText() with maxWidth: true+tab: strokeText(): true+line feed: measureText(): true+line feed: fillText(): true+line feed: fillText() with maxWidth: true+line feed: strokeText(): true+form feed: measureText(): true+form feed: fillText(): true+form feed: fillText() with maxWidth: true+form feed: strokeText(): true+carriage return: measureText(): true+carriage return: fillText(): true+carriage return: fillText() with maxWidth: true+carriage return: strokeText(): truediff --git a/Tests/LibWeb/Text/input/canvas/text-ascii-whitespace.html b/Tests/LibWeb/Text/input/canvas/text-ascii-whitespace.htmlnew file mode 100644index 00000000000..9faa11a1c09--- /dev/null+++ b/Tests/LibWeb/Text/input/canvas/text-ascii-whitespace.html@@ -0,0 +1,37 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // fillText() and strokeText() replace ASCII whitespace with spaces, as measureText() does.+ function pixelsOf(method, text, maxWidth) {+ const canvas = document.createElement("canvas");+ canvas.width = 200;+ canvas.height = 50;+ const context = canvas.getContext("2d");+ context.font = "20px sans-serif";+ if (maxWidth === undefined)+ context[method](text, 10, 30);+ else+ context[method](text, 10, 30, maxWidth);+ return context.getImageData(0, 0, canvas.width, canvas.height).data.join();+ }++ test(() => {+ const context = document.createElement("canvas").getContext("2d");+ context.font = "20px sans-serif";+ for (const [name, whitespace] of [+ ["tab", "\t"],+ ["line feed", "\n"],+ ["form feed", "\f"],+ ["carriage return", "\r"],+ ]) {+ const text = `a${whitespace}b`;+ println(`${name}: measureText(): ${context.measureText(text).width === context.measureText("a b").width}`);+ for (const [label, method, maxWidth] of [+ ["fillText()", "fillText"],+ ["fillText() with maxWidth", "fillText", 1000],+ ["strokeText()", "strokeText"],+ ])+ println(`${name}: ${label}: ${pixelsOf(method, text, maxWidth) === pixelsOf(method, "a b", maxWidth)}`);+ }+ });+</script>