Skip to content

Truly independent web browser

LibWeb: Refresh font faces after descriptor mutations

sideshowbarker18b2b3043ac6
diff --git a/Libraries/LibWeb/CSS/CSSDescriptors.cpp b/Libraries/LibWeb/CSS/CSSDescriptors.cpp
index 1d2d9e1cc3a..148d674a2c1 100644
--- a/Libraries/LibWeb/CSS/CSSDescriptors.cpp
+++ b/Libraries/LibWeb/CSS/CSSDescriptors.cpp
@@ -67,7 +67,7 @@ WebIDL::ExceptionOr<void> CSSDescriptors::set_property(Utf16FlyString const& pro
{
TRY(set_property_internal(property, value, priority));
- if (m_at_rule_id == AtRuleID::FontFace) {
+ if (m_at_rule_id == AtRuleID::FontFace && !value.is_empty()) {
if (auto* font_face_rule = as_if<CSSFontFaceRule>(parent_rule().ptr()))
font_face_rule->handle_descriptor_change(property);
}
@@ -187,6 +187,11 @@ WebIDL::ExceptionOr<Utf16String> CSSDescriptors::remove_property(Utf16FlyString
invalidate_owners();
}
+ if (removed && m_at_rule_id == AtRuleID::FontFace) {
+ if (auto* font_face_rule = as_if<CSSFontFaceRule>(parent_rule().ptr()))
+ font_face_rule->handle_descriptor_change(property);
+ }
+
// 8. Return value.
return value;
}
@@ -227,6 +232,8 @@ WebIDL::ExceptionOr<void> CSSDescriptors::set_css_text(Utf16View value)
if (is_readonly())
return WebIDL::NoModificationAllowedError::create("Cannot modify properties of readonly CSSStyleDeclaration"_utf16);
+ auto previous_src = m_at_rule_id == AtRuleID::FontFace ? get_property_value("src"_utf16_fly_string) : Utf16String {};
+
// 2. Empty the declarations.
prepare_to_update_style_attribute();
m_descriptors.replace(RustDescriptorBlock { Vector<Descriptor> {} });
@@ -240,6 +247,8 @@ WebIDL::ExceptionOr<void> CSSDescriptors::set_css_text(Utf16View value)
// 4. Update style attribute for the CSS declaration block.
update_style_attribute();
invalidate_owners();
+ if (auto rule = parent_rule(); rule && rule->type() == CSSRule::Type::FontFace)
+ static_cast<CSSFontFaceRule&>(*rule).handle_descriptor_block_change(previous_src != get_property_value("src"_utf16_fly_string));
return {};
}
diff --git a/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp b/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp
index 273d0a1606b..652fdf73720 100644
--- a/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp
+++ b/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp
@@ -39,29 +39,42 @@ RefPtr<FontFaceState> CSSFontFaceRule::css_connected_font_face() const
}
void CSSFontFaceRule::handle_descriptor_change(Utf16FlyString const& property)
+{
+ auto descriptor_affects_font_matching = property.equals_ignoring_ascii_case("font-family"_utf16_fly_string)
+ || property.equals_ignoring_ascii_case("font-weight"_utf16_fly_string)
+ || property.equals_ignoring_ascii_case("font-style"_utf16_fly_string)
+ || property.equals_ignoring_ascii_case("font-width"_utf16_fly_string)
+ || property.equals_ignoring_ascii_case("font-stretch"_utf16_fly_string);
+
+ handle_descriptor_change(property.equals_ignoring_ascii_case("src"_utf16_fly_string), descriptor_affects_font_matching);
+}
+
+void CSSFontFaceRule::handle_descriptor_block_change(bool src_changed)
+{
+ handle_descriptor_change(src_changed, true);
+}
+
+void CSSFontFaceRule::handle_descriptor_change(bool src_changed, bool affects_font_matching)
{
auto font_face = css_connected_font_face();
- if (!font_face)
+ if (!font_face) {
+ if (auto* style_sheet = parent_style_sheet())
+ style_sheet->synchronize_fonts_after_rule_change();
return;
+ }
if (!is_valid()) {
disconnect_font_face();
return;
}
- if (property.equals_ignoring_ascii_case("src"_utf16_fly_string)) {
+ if (src_changed) {
handle_src_descriptor_change();
return;
}
- auto descriptor_affects_font_matching = property.equals_ignoring_ascii_case("font-family"_utf16_fly_string)
- || property.equals_ignoring_ascii_case("font-weight"_utf16_fly_string)
- || property.equals_ignoring_ascii_case("font-style"_utf16_fly_string)
- || property.equals_ignoring_ascii_case("font-width"_utf16_fly_string)
- || property.equals_ignoring_ascii_case("font-stretch"_utf16_fly_string);
-
FontComputer* font_computer = nullptr;
- if (descriptor_affects_font_matching) {
+ if (affects_font_matching) {
if (auto document = parent_style_sheet() ? parent_style_sheet()->owning_document() : nullptr) {
font_computer = &document->font_computer();
font_computer->unregister_font_face(*font_face);
diff --git a/Libraries/LibWeb/CSS/CSSFontFaceRule.h b/Libraries/LibWeb/CSS/CSSFontFaceRule.h
index de2f0190f74..ae0c88d4bc8 100644
--- a/Libraries/LibWeb/CSS/CSSFontFaceRule.h
+++ b/Libraries/LibWeb/CSS/CSSFontFaceRule.h
@@ -24,9 +24,11 @@ public:
RefPtr<FontFaceState> css_connected_font_face() const;
void handle_descriptor_change(Utf16FlyString const& property);
+ void handle_descriptor_block_change(bool src_changed);
void disconnect_font_face();
private:
+ void handle_descriptor_change(bool src_changed, bool affects_font_matching);
void handle_src_descriptor_change();
};
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 60c58ee60bd..e7867e9b683 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -246,6 +246,7 @@ Text/input/wpt-import/media-playback-quality/idlharness.window.html
Text/input/css/font-face-load-dedups-same-url.html
Text/input/css/font-load-delays-document-load-event.html
Text/input/css/font-face-unused-face-not-fetched.html
+Text/input/css/font-face-reconnect-after-descriptor-change.html
Text/input/css/style-engine/removing-stylesheet-preserves-descendant-animation-overlay.html
Text/input/css/unrecognized-font-family-fallback-honors-weight.html
Text/input/wpt-import/html/syntax/parsing/html5lib_url.html
diff --git a/Tests/LibWeb/Text/expected/css/font-face-css-text.txt b/Tests/LibWeb/Text/expected/css/font-face-css-text.txt
new file mode 100644
index 00000000000..dbff110cda4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-face-css-text.txt
@@ -0,0 +1,11 @@
+initial: 700 / 80
+replacement: 400 / U+42
+excluded characters use fallback: true
+same source preserves identity: true
+removed unicode-range: U+0-10FFFF / 80
+renamed: Renamed / auto / 80
+rename preserves identity: true
+changed source replaces identity: true / false
+changed source width: 80
+invalid rule disconnects face: false
+disconnected characters use fallback: true
diff --git a/Tests/LibWeb/Text/expected/css/font-face-reconnect-after-descriptor-change.txt b/Tests/LibWeb/Text/expected/css/font-face-reconnect-after-descriptor-change.txt
new file mode 100644
index 00000000000..944422e102c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-face-reconnect-after-descriptor-change.txt
@@ -0,0 +1,10 @@
+ByBlock initial width: 80
+ByBlock invalid: disconnected=true, fallback=true
+ByBlock restored: connected=true, replacement=true, width=80
+ByBlock original stays disconnected: true
+ByProperty initial width: 80
+ByProperty invalid: disconnected=true, fallback=true
+ByProperty restored: connected=true, replacement=true, width=80
+ByProperty original stays disconnected: true
+inactive rule stays disconnected: true
+activated rule connects: true
diff --git a/Tests/LibWeb/Text/expected/css/font-face-remove-property.txt b/Tests/LibWeb/Text/expected/css/font-face-remove-property.txt
new file mode 100644
index 00000000000..db826e11bce
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/font-face-remove-property.txt
@@ -0,0 +1,2 @@
+before: 700 / 700
+after: / auto
diff --git a/Tests/LibWeb/Text/input/css/font-face-css-text.html b/Tests/LibWeb/Text/input/css/font-face-css-text.html
new file mode 100644
index 00000000000..d18331d2477
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-face-css-text.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @font-face { font-family: Probe; src: local(Ahem); font-weight: 700; }
+ span { display: inline-block; font: 700 20px monospace; }
+ #sample { font-family: Probe, monospace; }
+</style>
+<span id="sample">AAAA</span>
+<span id="fallback">AAAA</span>
+<script>
+promiseTest(async () => {
+ await document.fonts.load("700 20px Probe");
+ const rule = document.styleSheets[0].cssRules[0];
+ const face = [...document.fonts][0];
+ const width = element => element.getBoundingClientRect().width;
+ println(`initial: ${face.weight} / ${width(sample)}`);
+
+ rule.style.cssText = "font-family: Probe; src: local(Ahem); font-weight: 400; unicode-range: U+42";
+ sample.style.fontWeight = fallback.style.fontWeight = "400";
+ println(`replacement: ${face.weight} / ${face.unicodeRange}`);
+ println(`excluded characters use fallback: ${width(sample) === width(fallback)}`);
+ println(`same source preserves identity: ${[...document.fonts][0] === face}`);
+
+ rule.style.removeProperty("unicode-range");
+ println(`removed unicode-range: ${face.unicodeRange} / ${width(sample)}`);
+ rule.style.cssText = "font-family: Renamed; src: local(Ahem)";
+ sample.style.fontFamily = "Renamed, monospace";
+ println(`renamed: ${face.family} / ${face.weight} / ${width(sample)}`);
+ println(`rename preserves identity: ${[...document.fonts][0] === face}`);
+
+ rule.style.cssText = "font-family: Renamed; src: local(Ahem), local(UnusedSource)";
+ const replacement = [...document.fonts][0];
+ await replacement.load();
+ println(`changed source replaces identity: ${replacement !== face} / ${document.fonts.has(face)}`);
+ println(`changed source width: ${width(sample)}`);
+ rule.style.cssText = "";
+ println(`invalid rule disconnects face: ${document.fonts.has(replacement)}`);
+ println(`disconnected characters use fallback: ${width(sample) === width(fallback)}`);
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/css/font-face-reconnect-after-descriptor-change.html b/Tests/LibWeb/Text/input/css/font-face-reconnect-after-descriptor-change.html
new file mode 100644
index 00000000000..c11ec22580d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-face-reconnect-after-descriptor-change.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ span { display: inline-block; font: 20px monospace; }
+</style>
+<span id="fallback">AAAA</span>
+<script>
+promiseTest(async () => {
+ const width = element => element.getBoundingClientRect().width;
+ const findFace = family => [...document.fonts].find(face => face.family === family);
+ async function checkRestoration(family, descriptor, replaceBlock) {
+ const style = document.createElement("style");
+ const descriptors = `font-family: ${family}; src: url(../wpt-import/fonts/Ahem.ttf);`;
+ style.textContent = `@font-face { ${descriptors} }`;
+ document.head.append(style);
+ const sample = document.createElement("span");
+ sample.style.fontFamily = `${family}, monospace`;
+ sample.textContent = "AAAA";
+ document.body.append(sample);
+ await document.fonts.load(`20px ${family}`);
+ const rule = style.sheet.cssRules[0];
+ const original = findFace(family);
+ println(`${family} initial width: ${width(sample)}`);
+
+ rule.style.removeProperty(descriptor);
+ const usesFallback = width(sample) === width(fallback);
+ println(`${family} invalid: disconnected=${!document.fonts.has(original)}, fallback=${usesFallback}`);
+ if (replaceBlock)
+ rule.style.cssText = descriptors;
+ else
+ rule.style.setProperty(descriptor, "url(../wpt-import/fonts/Ahem.ttf)");
+ await document.fonts.load(`20px ${family}`);
+ const restoredWidth = width(sample);
+ const replacement = findFace(family);
+ println(`${family} restored: connected=${!!replacement}, replacement=${!!replacement && replacement !== original}, width=${restoredWidth}`);
+ println(`${family} original stays disconnected: ${!document.fonts.has(original)}`);
+ style.remove();
+ sample.remove();
+ }
+ await checkRestoration("ByBlock", "font-family", true);
+ await checkRestoration("ByProperty", "src", false);
+
+ const style = document.createElement("style");
+ style.textContent = "@media not all { @font-face { font-family: Inactive; } }";
+ document.head.append(style);
+ const mediaRule = style.sheet.cssRules[0];
+ mediaRule.cssRules[0].style.src = "url(../wpt-import/fonts/Ahem.ttf)";
+ println(`inactive rule stays disconnected: ${!findFace("Inactive")}`);
+ mediaRule.media.mediaText = "all";
+ await document.fonts.load("20px Inactive");
+ println(`activated rule connects: ${!!findFace("Inactive")}`);
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/css/font-face-remove-property.html b/Tests/LibWeb/Text/input/css/font-face-remove-property.html
new file mode 100644
index 00000000000..0649985c5c8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/font-face-remove-property.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @font-face { font-family: Probe; src: local(Ahem); font-weight: 700; }
+</style>
+<script>
+ test(() => {
+ const style = document.styleSheets[0].cssRules[0].style;
+ const face = [...document.fonts][0];
+ println(`before: ${style.fontWeight} / ${face.weight}`);
+ style.removeProperty("font-weight");
+ println(`after: ${style.fontWeight} / ${face.weight}`);
+ });
+</script>