Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fonts): accept legacy variable font format hints

Arjen de Vosa1e3651dfff0
diff --git a/moli-renderer-v8/src/css_resource_urls.rs b/moli-renderer-v8/src/css_resource_urls.rs
index 553401929..2190492b3 100644
--- a/moli-renderer-v8/src/css_resource_urls.rs
+++ b/moli-renderer-v8/src/css_resource_urls.rs
@@ -415,8 +415,11 @@ fn font_format_function_is_supported<'i, 't>(input: &mut Parser<'i, 't>) -> bool
_ => return false,
};
saw_format = true;
+ // Chrome still accepts the pre-`tech()` variable font hints, and font
+ // packages such as Fontsource emit `woff2-variations` for every face.
+ let value = legacy_variations_font_format(&value).unwrap_or(&value);
if !matches!(
- value.as_ref(),
+ value,
"woff" | "woff2" | "truetype" | "opentype" | "ttf" | "otf" | "collection"
) {
return false;
@@ -425,6 +428,14 @@ fn font_format_function_is_supported<'i, 't>(input: &mut Parser<'i, 't>) -> bool
saw_format
}
+/// Maps a legacy variable font format hint such as `woff2-variations` to the
+/// container format it names.
+pub(crate) fn legacy_variations_font_format(format: &str) -> Option<&str> {
+ format
+ .strip_suffix("-variations")
+ .filter(|format| matches!(*format, "woff" | "woff2" | "truetype" | "opentype"))
+}
+
fn resolve_font_source_url(raw_url: &str, base_url: &Url) -> Option<Url> {
let raw_url = raw_url.trim();
let url = base_url
@@ -654,4 +665,29 @@ mod tests {
);
}
}
+
+ #[test]
+ fn legacy_variations_format_hints_select_their_container_format() {
+ let resources = stylesheet_load_blocking_font_resources(
+ r#"
+ @font-face { font-family: Variable; src: url(fonts/variable.woff2) format('woff2-variations'); }
+ @font-face { font-family: VariableTtf;
+ src: url(fonts/variable.ttf) format("truetype-variations"),
+ url(fonts/fallback.ttf) format("truetype"); }
+ @font-face { font-family: Unknown; src: url(fonts/unknown.bin) format("svg-variations"); }
+ "#,
+ &base_url(),
+ OptionalResourceFetchMask::FONT,
+ );
+ assert_eq!(
+ resources
+ .iter()
+ .map(|resource| resource.request_url().to_string())
+ .collect::<Vec<_>>(),
+ vec![
+ "https://example.test/assets/fonts/variable.woff2",
+ "https://example.test/assets/fonts/variable.ttf",
+ ]
+ );
+ }
}
diff --git a/moli-renderer-v8/src/style_engine/stylesheet.rs b/moli-renderer-v8/src/style_engine/stylesheet.rs
index 016a4d006..9254096a8 100644
--- a/moli-renderer-v8/src/style_engine/stylesheet.rs
+++ b/moli-renderer-v8/src/style_engine/stylesheet.rs
@@ -309,10 +309,15 @@ fn native_font_source_format_is_supported(format: Option<&FontFaceSourceFormat>)
| FontFaceSourceFormatKeyword::Woff
| FontFaceSourceFormatKeyword::Woff2
),
- Some(FontFaceSourceFormat::String(format)) => matches!(
- format.to_ascii_lowercase().as_str(),
- "collection" | "opentype" | "otf" | "truetype" | "ttf" | "woff" | "woff2"
- ),
+ Some(FontFaceSourceFormat::String(format)) => {
+ let format = format.to_ascii_lowercase();
+ let format =
+ crate::css_resource_urls::legacy_variations_font_format(&format).unwrap_or(&format);
+ matches!(
+ format,
+ "collection" | "opentype" | "otf" | "truetype" | "ttf" | "woff" | "woff2"
+ )
+ }
}
}