Skip to content

Commit

Permalink
Serialize canvas font families as needed
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265993
rdar://problem/119312574

Reviewed by Dean Jackson.

In a canvas rendering context, the `font` attribute contains a list of font families,
which should only contain valid CSS custom-ident's and strings (in quotes, with escaped
characters as appropriate).

The original code used to only serialize family names with spaces in them, which could
have missed some cases, such as the one in the WPT test
2d.text.drawing.style.absolute.spacing.html: `QuotedFont\",` and others.

This patch uses the CSS markup function serializeFontFamily to serialize the canvas
rendering context's `font` family names when needed.

* LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/text/2d.text.font.parse.family-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.family-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.family.worker-expected.txt:
* Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp:
(WebCore::CanvasRenderingContext2DBase::State::fontString const):

Canonical link: https://commits.webkit.org/271804@main
  • Loading branch information
squelart committed Dec 9, 2023
1 parent 365b474 commit b94074d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
2d.text.font.parse.family
Actual output:

FAIL Canvas test: 2d.text.font.parse.family assert_equals: ctx.font === '20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\\\\","' (got 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\",[string], expected 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\","[string]) expected "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, \"QuotedFont\\\\\\\",\"" but got "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\\\","
PASS Canvas test: 2d.text.font.parse.family

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
2d.text.font.parse.family


FAIL OffscreenCanvas test: 2d.text.font.parse.family assert_equals: ctx.font === '20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\\\\","' (got 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\",[string], expected 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\","[string]) expected "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, \"QuotedFont\\\\\\\",\"" but got "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\\\","
PASS OffscreenCanvas test: 2d.text.font.parse.family

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

FAIL 2d assert_equals: ctx.font === '20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\\\\","' (got 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\",[string], expected 20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, "QuotedFont\\\","[string]) expected "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, \"QuotedFont\\\\\\\",\"" but got "20px cursive, fantasy, monospace, sans-serif, serif, UnquotedFont, QuotedFont\\\","
PASS 2d

4 changes: 2 additions & 2 deletions Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "BitmapImage.h"
#include "CSSFontSelector.h"
#include "CSSMarkup.h"
#include "CSSParser.h"
#include "CSSPropertyNames.h"
#include "CSSStyleImageValue.h"
Expand Down Expand Up @@ -318,8 +319,7 @@ String CanvasRenderingContext2DBase::State::fontString() const
family = family.substring(8);

auto separator = i ? ", " : " ";
auto quote = family.contains(' ') ? "\"" : "";
serializedFont.append(separator, quote, family, quote);
serializedFont.append(separator, serializeFontFamily(family.toString()));
}

return serializedFont.toString();
Expand Down

0 comments on commit b94074d

Please sign in to comment.