Skip to content

Commit e6b7551

Browse files
committed
Add CoreText support for palette-mix()
https://bugs.webkit.org/show_bug.cgi?id=327309 Reviewed by Darin Adler. Adds the final piece need for palette-mix() to actually do anything. Due to CoreText not natively supporting a mixing of components, we can't utilize the built in support that was used previously. Instead, we have to manually parse the font's CPAL table, using a new OpenTypeCPAL parser, and manually mix ourselves. We also now need to pass a set of palettes from CSSFontSelector to the platform layer as a mix function can reference multiple palettes. With this complete, the feature is working so the CSSFontPaletteMixFunctionEnabled preference is now enabled by default. Test: imported/w3c/web-platform-tests/css/css-fonts/font-palette-mix.html * LayoutTests/imported/w3c/web-platform-tests/css/css-fonts/font-palette-mix-expected.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-fonts/font-palette-mix-ref.html: Added. * LayoutTests/imported/w3c/web-platform-tests/css/css-fonts/font-palette-mix.html: Added. * Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: * Source/WebCore/Sources.txt: * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/css/CSSFontSelector.cpp: * Source/WebCore/css/CSSFontSelector.h: * Source/WebCore/css/StyleRule.cpp: * Source/WebCore/css/StyleRule.h: * Source/WebCore/platform/graphics/FontPalette.cpp: * Source/WebCore/platform/graphics/FontPalette.h: * Source/WebCore/platform/graphics/FontPaletteMix.cpp: * Source/WebCore/platform/graphics/FontPaletteMix.h: * Source/WebCore/platform/graphics/FontPaletteValues.h: * Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.cpp: * Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.h: * Source/WebCore/platform/graphics/opentype/OpenTypeCPAL.cpp: Added. * Source/WebCore/platform/graphics/opentype/OpenTypeCPAL.h: Added. * Source/WebCore/platform/libwebrtc/LibWebRTCVPXVideoEncoder.cpp: Add missing include of CoreVideoSoftLink.h exposed by unified source bundle changes. Canonical link: https://commits.webkit.org/324044@main
1 parent 09db792 commit e6b7551

20 files changed

Lines changed: 618 additions & 44 deletions
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>palette-mix() applies the mixed palette to the font (reference)</title>
6+
<link rel="author" title="Sam Weinig" href="mailto:[email protected]">
7+
<style>
8+
@font-face {
9+
font-family: "COLR-test-font";
10+
src: url("resources/COLR-palettes-test-font.ttf") format("truetype");
11+
}
12+
div { font: 48px "COLR-test-font"; }
13+
14+
@font-palette-values --Mix1 {
15+
font-family: "COLR-test-font";
16+
override-colors: 3 rgb(100 0 100);
17+
}
18+
@font-palette-values --Mix2 {
19+
font-family: "COLR-test-font";
20+
override-colors: 3 rgb(50 0 150);
21+
}
22+
@font-palette-values --Mix3 {
23+
font-family: "COLR-test-font";
24+
override-colors: 3 rgb(125 0 75);
25+
}
26+
@font-palette-values --Mix5 {
27+
font-family: "COLR-test-font";
28+
override-colors: 0 rgb(0 51 204), 1 rgb(255 51 204), 2 rgb(0 204 255), 3 rgb(255 204 255), 4 rgb(0 51 51), 5 rgb(255 51 51), 6 rgb(0 204 0), 7 rgb(255 204 0);
29+
}
30+
@font-palette-values --Mix6 {
31+
font-family: "COLR-test-font";
32+
override-colors: 3 rgb(211 51 0);
33+
}
34+
@font-palette-values --Mix7 {
35+
font-family: "COLR-test-font";
36+
override-colors: 0 rgb(0 0 0), 1 rgb(255 0 0), 2 rgb(0 255 0), 3 rgb(244 204 0), 4 rgb(0 0 255), 5 rgb(255 0 255), 6 rgb(0 255 255), 7 rgb(255 255 255);
37+
}
38+
@font-palette-values --Mix8 {
39+
font-family: "COLR-test-font";
40+
override-colors: 0 rgb(0 204 0), 1 rgb(255 204 0), 2 rgb(0 51 204), 3 rgb(244 0 204), 4 rgb(0 204 255), 5 rgb(255 204 255), 6 rgb(0 51 51), 7 rgb(255 51 51);
41+
}
42+
@font-palette-values --Mix9 {
43+
font-family: "COLR-test-font";
44+
override-colors: 0 rgb(204 204 0), 1 rgb(51 0 204), 2 rgb(204 51 204), 3 rgb(40 204 204), 4 rgb(204 204 255), 5 rgb(51 0 51), 6 rgb(204 51 51), 7 rgb(51 255 51);
45+
}
46+
</style>
47+
</head>
48+
<body>
49+
<div style="font-palette: --Mix1;">A</div>
50+
<div style="font-palette: --Mix2;">A</div>
51+
<div style="font-palette: --Mix3;">A</div>
52+
<div style="font-palette: dark;">A</div>
53+
<div style="font-palette: --Mix5;">A</div>
54+
<div style="font-palette: --Mix6;">A</div>
55+
<div style="font-palette: --Mix7;">A</div>
56+
<div style="font-palette: --Mix8;">A</div>
57+
<div style="font-palette: --Mix9;">A</div>
58+
</body>
59+
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>palette-mix() applies the mixed palette to the font (reference)</title>
6+
<link rel="author" title="Sam Weinig" href="mailto:[email protected]">
7+
<style>
8+
@font-face {
9+
font-family: "COLR-test-font";
10+
src: url("resources/COLR-palettes-test-font.ttf") format("truetype");
11+
}
12+
div { font: 48px "COLR-test-font"; }
13+
14+
@font-palette-values --Mix1 {
15+
font-family: "COLR-test-font";
16+
override-colors: 3 rgb(100 0 100);
17+
}
18+
@font-palette-values --Mix2 {
19+
font-family: "COLR-test-font";
20+
override-colors: 3 rgb(50 0 150);
21+
}
22+
@font-palette-values --Mix3 {
23+
font-family: "COLR-test-font";
24+
override-colors: 3 rgb(125 0 75);
25+
}
26+
@font-palette-values --Mix5 {
27+
font-family: "COLR-test-font";
28+
override-colors: 0 rgb(0 51 204), 1 rgb(255 51 204), 2 rgb(0 204 255), 3 rgb(255 204 255), 4 rgb(0 51 51), 5 rgb(255 51 51), 6 rgb(0 204 0), 7 rgb(255 204 0);
29+
}
30+
@font-palette-values --Mix6 {
31+
font-family: "COLR-test-font";
32+
override-colors: 3 rgb(211 51 0);
33+
}
34+
@font-palette-values --Mix7 {
35+
font-family: "COLR-test-font";
36+
override-colors: 0 rgb(0 0 0), 1 rgb(255 0 0), 2 rgb(0 255 0), 3 rgb(244 204 0), 4 rgb(0 0 255), 5 rgb(255 0 255), 6 rgb(0 255 255), 7 rgb(255 255 255);
37+
}
38+
@font-palette-values --Mix8 {
39+
font-family: "COLR-test-font";
40+
override-colors: 0 rgb(0 204 0), 1 rgb(255 204 0), 2 rgb(0 51 204), 3 rgb(244 0 204), 4 rgb(0 204 255), 5 rgb(255 204 255), 6 rgb(0 51 51), 7 rgb(255 51 51);
41+
}
42+
@font-palette-values --Mix9 {
43+
font-family: "COLR-test-font";
44+
override-colors: 0 rgb(204 204 0), 1 rgb(51 0 204), 2 rgb(204 51 204), 3 rgb(40 204 204), 4 rgb(204 204 255), 5 rgb(51 0 51), 6 rgb(204 51 51), 7 rgb(51 255 51);
45+
}
46+
</style>
47+
</head>
48+
<body>
49+
<div style="font-palette: --Mix1;">A</div>
50+
<div style="font-palette: --Mix2;">A</div>
51+
<div style="font-palette: --Mix3;">A</div>
52+
<div style="font-palette: dark;">A</div>
53+
<div style="font-palette: --Mix5;">A</div>
54+
<div style="font-palette: --Mix6;">A</div>
55+
<div style="font-palette: --Mix7;">A</div>
56+
<div style="font-palette: --Mix8;">A</div>
57+
<div style="font-palette: --Mix9;">A</div>
58+
</body>
59+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>palette-mix() applies the mixed palette to the font</title>
6+
<link rel="help" href="https://drafts.csswg.org/css-fonts-4/#funcdef-palette-mix">
7+
<link rel="author" title="Sam Weinig" href="mailto:[email protected]">
8+
<link rel="match" href="font-palette-mix-ref.html">
9+
<style>
10+
@font-face {
11+
font-family: "COLR-test-font";
12+
src: url("resources/COLR-palettes-test-font.ttf") format("truetype");
13+
}
14+
div { font: 48px "COLR-test-font"; }
15+
16+
@font-palette-values --A {
17+
font-family: "COLR-test-font";
18+
override-colors: 3 rgb(200 0 0);
19+
}
20+
@font-palette-values --B {
21+
font-family: "COLR-test-font";
22+
override-colors: 3 rgb(0 0 200);
23+
}
24+
@font-palette-values --P2 {
25+
font-family: "COLR-test-font";
26+
base-palette: 2;
27+
}
28+
@font-palette-values --P4 {
29+
font-family: "COLR-test-font";
30+
base-palette: 4;
31+
}
32+
</style>
33+
</head>
34+
<body>
35+
<div style="font-palette: palette-mix(in srgb, --A, --B);">A</div>
36+
<div style="font-palette: palette-mix(in srgb, --A 25%, --B 75%);">A</div>
37+
<div style="font-palette: palette-mix(in srgb, palette-mix(in srgb, --A 25%, --B 75%), --A);">A</div>
38+
<div style="font-palette: palette-mix(in srgb, dark, dark);">A</div>
39+
<div style="font-palette: palette-mix(in srgb, --P2 20%, --P4 80%);">A</div>
40+
<div style="font-palette: palette-mix(in srgb, --A 80%, --Missing 20%);">A</div>
41+
<div style="font-palette: palette-mix(in srgb, --A 20%, normal 80%);">A</div>
42+
<div style="font-palette: palette-mix(in srgb, --A 20%, light 80%);">A</div>
43+
<div style="font-palette: palette-mix(in srgb, --A 20%, dark 80%);">A</div>
44+
</body>
45+
</html>

‎Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,11 @@ CSSFontFaceMetricOverrideDescriptorsEnabled:
942942

943943
CSSFontPaletteMixFunctionEnabled:
944944
type: bool
945-
status: testable
945+
status: stable
946946
category: css
947947
humanReadableName: "CSS palette-mix() function"
948948
humanReadableDescription: "Enable the CSS palette-mix() function"
949-
defaultValue: false
949+
defaultValue: true
950950

951951
CSSFontSynthesisStyleObliqueOnlyEnabled:
952952
type: bool

‎Source/WebCore/Sources.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,7 @@ platform/graphics/iso/ISOSchemeInformationBox.cpp
28762876
platform/graphics/iso/ISOSchemeTypeBox.cpp
28772877
platform/graphics/iso/ISOTrackEncryptionBox.cpp
28782878
platform/graphics/iso/ISOVTTCue.cpp
2879+
platform/graphics/opentype/OpenTypeCPAL.cpp
28792880
platform/graphics/opentype/OpenTypeMathData.cpp
28802881
platform/graphics/transforms/AffineTransform.cpp
28812882
platform/graphics/transforms/IdentityTransformOperation.cpp

‎Source/WebCore/WebCore.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18700,6 +18700,8 @@
1870018700
B2D3DA530D006CD600EF6F3A /* OpenTypeCG.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = OpenTypeCG.cpp; sourceTree = "<group>"; };
1870118701
B2D3DA540D006CD600EF6F27 /* OpenTypeMathData.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeMathData.h; sourceTree = "<group>"; };
1870218702
B2D3DA540D006CD600EF6F3A /* OpenTypeCG.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeCG.h; sourceTree = "<group>"; };
18703+
BC01033C30473B01000021F4 /* OpenTypeCPAL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OpenTypeCPAL.cpp; sourceTree = "<group>"; };
18704+
BC01033D30473B02000021F4 /* OpenTypeCPAL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OpenTypeCPAL.h; sourceTree = "<group>"; };
1870318705
B2D3EA540D006CD600EF6F28 /* OpenTypeTypes.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = OpenTypeTypes.h; sourceTree = "<group>"; };
1870418706
B2E27C9D0B0F2B0900F17C7B /* FloatPoint3D.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FloatPoint3D.cpp; sourceTree = "<group>"; };
1870518707
B2E27C9E0B0F2B0900F17C7B /* FloatPoint3D.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FloatPoint3D.h; sourceTree = "<group>"; };
@@ -27143,6 +27145,8 @@
2714327145
children = (
2714427146
B2D3DA530D006CD600EF6F3A /* OpenTypeCG.cpp */,
2714527147
B2D3DA540D006CD600EF6F3A /* OpenTypeCG.h */,
27148+
BC01033C30473B01000021F4 /* OpenTypeCPAL.cpp */,
27149+
BC01033D30473B02000021F4 /* OpenTypeCPAL.h */,
2714627150
B2D3DA530D006CD600EF6F27 /* OpenTypeMathData.cpp */,
2714727151
B2D3DA540D006CD600EF6F27 /* OpenTypeMathData.h */,
2714827152
B2D3EA540D006CD600EF6F28 /* OpenTypeTypes.h */,

‎Source/WebCore/css/CSSFontSelector.cpp‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "StyleProperties.h"
5353
#include "StyleResolver.h"
5454
#include "StyleRule.h"
55+
#include <wtf/OrderedHashSet.h>
5556
#include <wtf/Ref.h>
5657
#include <wtf/SetForScope.h>
5758
#include <wtf/text/AtomString.h>
@@ -256,7 +257,7 @@ void CSSFontSelector::addFontPaletteValuesRule(const StyleRuleFontPaletteValues&
256257
return;
257258

258259
for (auto& fontFamily : fontFamilies)
259-
m_paletteMap.set(std::make_pair(fontFamily, name), fontPaletteValuesRule.fontPaletteValues());
260+
m_paletteMap.set(std::make_pair(fontFamily, name), fontPaletteValuesRule.customPalette());
260261

261262
++m_version;
262263
}
@@ -361,19 +362,18 @@ std::optional<AtomString> CSSFontSelector::resolveGenericFamily(const FontDescri
361362
return std::nullopt;
362363
}
363364

364-
const FontPaletteValues& CSSFontSelector::lookupFontPaletteValues(const AtomString& familyName, const FontDescription& fontDescription) const
365+
FontPaletteValues CSSFontSelector::lookupFontPaletteValues(const AtomString& familyName, const FontDescription& fontDescription) const
365366
{
366-
static NeverDestroyed<FontPaletteValues> emptyFontPaletteValues;
367-
368-
auto paletteName = fontDescription.fontPalette().ident();
369-
if (!paletteName)
370-
return emptyFontPaletteValues.get();
371-
372-
auto iterator = m_paletteMap.find(std::make_pair(familyName, *paletteName));
373-
if (iterator == m_paletteMap.end())
374-
return emptyFontPaletteValues.get();
375-
376-
return iterator->value;
367+
Vector<FontPaletteValues::CustomPalette> customPalettes;
368+
for (auto& paletteName : fontDescription.fontPalette().referencedIdentifiers()) {
369+
auto iterator = m_paletteMap.find(std::make_pair(familyName, paletteName));
370+
if (iterator == m_paletteMap.end())
371+
continue;
372+
auto& customPalette = iterator->value;
373+
if (customPalette.basePalette || !customPalette.overrideColors.isEmpty())
374+
customPalettes.append(customPalette);
375+
}
376+
return FontPaletteValues { WTF::move(customPalettes) };
377377
}
378378

379379
RefPtr<FontFeatureValues> CSSFontSelector::lookupFontFeatureValues(const AtomString& familyName) const
@@ -459,7 +459,7 @@ FontRanges CSSFontSelector::fontRangesForFamily(const FontDescription& fontDescr
459459
}
460460
};
461461

462-
const auto& fontPaletteValues = lookupFontPaletteValues(familyName, fontDescription);
462+
auto fontPaletteValues = lookupFontPaletteValues(familyName, fontDescription);
463463
auto fontFeatureValues = lookupFontFeatureValues(familyName);
464464

465465
// Handle the generic math font family a bit differently.

‎Source/WebCore/css/CSSFontSelector.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CSSFontSelector final : public FontSelector, public CSSFontFaceClient, pub
109109

110110
std::optional<AtomString> resolveGenericFamily(const FontDescription&, const AtomString& family);
111111

112-
const FontPaletteValues& lookupFontPaletteValues(const AtomString& familyName, const FontDescription&) const;
112+
FontPaletteValues lookupFontPaletteValues(const AtomString& familyName, const FontDescription&) const;
113113
RefPtr<FontFeatureValues> lookupFontFeatureValues(const AtomString& familyName) const;
114114

115115
// CSSFontFaceClient
@@ -140,7 +140,7 @@ class CSSFontSelector final : public FontSelector, public CSSFontFaceClient, pub
140140
return ASCIICaseInsensitiveHash::equal(a.first, b.first) && DefaultHash<AtomString>::equal(a.second, b.second);
141141
}
142142
};
143-
HashMap<std::pair<AtomString, AtomString>, FontPaletteValues, PaletteMapHash> m_paletteMap;
143+
HashMap<std::pair<AtomString, AtomString>, FontPaletteValues::CustomPalette, PaletteMapHash> m_paletteMap;
144144
HashMap<String, Ref<FontFeatureValues>> m_featureValues;
145145

146146
HashSet<Ref<CSSFontFace>> m_cssConnectionsPossiblyToRemove;

‎Source/WebCore/css/StyleRule.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ StyleRuleFontPaletteValues::StyleRuleFontPaletteValues(const AtomString& name, V
522522
: StyleRuleBase(StyleRuleType::FontPaletteValues)
523523
, m_name(name)
524524
, m_fontFamilies(WTF::move(fontFamilies))
525-
, m_fontPaletteValues(basePalette, WTF::move(overrideColors))
525+
, m_basePalette(basePalette)
526+
, m_overrideColors(WTF::move(overrideColors))
526527
{
527528
}
528529

‎Source/WebCore/css/StyleRule.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ class StyleRuleFontPaletteValues final : public StyleRuleBase {
232232

233233
const AtomString& name() const LIFETIME_BOUND { return m_name; }
234234
const Vector<AtomString>& fontFamilies() const LIFETIME_BOUND { return m_fontFamilies; }
235-
const FontPaletteValues& fontPaletteValues() const LIFETIME_BOUND { return m_fontPaletteValues; }
236-
std::optional<FontPaletteIndex> basePalette() const { return m_fontPaletteValues.basePalette(); }
237-
const Vector<FontPaletteValues::OverriddenColor>& overrideColors() const LIFETIME_BOUND { return m_fontPaletteValues.overrideColors(); }
235+
std::optional<FontPaletteIndex> basePalette() const { return m_basePalette; }
236+
const Vector<FontPaletteValues::OverriddenColor>& overrideColors() const LIFETIME_BOUND { return m_overrideColors; }
237+
FontPaletteValues::CustomPalette customPalette() const { return { m_name, m_basePalette, m_overrideColors }; }
238238

239239
Ref<StyleRuleFontPaletteValues> copy() const { return adoptRef(*new StyleRuleFontPaletteValues(*this)); }
240240

@@ -244,7 +244,8 @@ class StyleRuleFontPaletteValues final : public StyleRuleBase {
244244

245245
AtomString m_name;
246246
Vector<AtomString> m_fontFamilies;
247-
FontPaletteValues m_fontPaletteValues;
247+
std::optional<FontPaletteIndex> m_basePalette;
248+
Vector<FontPaletteValues::OverriddenColor> m_overrideColors;
248249
};
249250

250251
class StyleRuleFontFeatureValuesBlock final : public StyleRuleBase {

0 commit comments

Comments
 (0)