Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(fonts): reuse native descriptors for font queries

ldm02593e7639459
diff --git a/moli-layout/src/text.rs b/moli-layout/src/text.rs
index 25176efb6..e2b52bc6f 100644
--- a/moli-layout/src/text.rs
+++ b/moli-layout/src/text.rs
@@ -522,6 +522,7 @@ impl WebFontStyle {
#[derive(Clone, Debug, PartialEq)]
pub struct WebFontFace {
family_name: String,
+ family_name_key: String,
weight: Option<f32>,
stretch: Option<f32>,
style: Option<WebFontStyle>,
@@ -531,8 +532,11 @@ pub struct WebFontFace {
impl WebFontFace {
pub fn new(family_name: impl Into<String>) -> Self {
+ let family_name = family_name.into();
+ let family_name_key = font_family_name_key(&family_name);
Self {
- family_name: family_name.into(),
+ family_name,
+ family_name_key,
weight: None,
stretch: None,
style: None,
@@ -600,6 +604,16 @@ impl WebFontFace {
&self.family_name
}
+ /// Chromium-compatible CSS family matching key, computed with the descriptor.
+ pub fn family_name_key(&self) -> &str {
+ &self.family_name_key
+ }
+
+ /// Full computed CSS ranges, when descriptors override font metadata.
+ pub const fn css_capabilities(&self) -> Option<WebFontCapabilities> {
+ self.capabilities
+ }
+
pub const fn weight(&self) -> Option<f32> {
self.weight
}
@@ -1319,7 +1333,7 @@ impl ParleyDocumentServices {
.unwrap_or((!font.membership.is_stylesheet_only(), usize::MAX));
let family = self
.web_font_families
- .entry(font_family_name_key(font.face.family_name()))
+ .entry(font.face.family_name_key().to_owned())
.or_default();
for (_, fonts) in internal_fonts {
for selector_font in fonts {
diff --git a/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/query.rs b/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/query.rs
index 178bf1a59..4401d603e 100644
--- a/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/query.rs
+++ b/moli-renderer-v8/src/context_bootstrap/css_fontface_runtime/query.rs
@@ -1,14 +1,12 @@
use super::storage::font_face_set_faces_array;
-use super::*;
use crate::util::serialize_v8_iter_array;
mod selection;
struct Candidate<'s> {
face: v8::Local<'s, v8::Value>,
- family: String,
+ descriptor: std::sync::Arc<moli_layout::WebFontFace>,
selection: selection::Selection,
- ranges: Vec<moli_layout::WebFontUnicodeRange>,
}
pub(super) fn font_face_set_matching_faces_array<'s>(
@@ -35,19 +33,17 @@ pub(super) fn font_face_set_matching_faces_array<'s>(
let Ok(face_object) = v8::Local::<v8::Object>::try_from(face) else {
continue;
};
- let family = super::loading::string_slot(scope, face_object, FONT_FACE_FAMILY_SLOT);
- let weight = super::loading::string_slot(scope, face_object, FONT_FACE_WEIGHT_SLOT);
- let stretch = super::loading::string_slot(scope, face_object, FONT_FACE_STRETCH_SLOT);
- let style = super::loading::string_slot(scope, face_object, FONT_FACE_STYLE_SLOT);
- let ranges = super::loading::string_slot(scope, face_object, FONT_FACE_UNICODE_RANGE_SLOT);
+ let load = super::state::get(scope, face_object).expect("FontFace has native state");
+ let descriptor = load.borrow().descriptor().clone();
+ // Constructors and successful setters update this same typed descriptor
+ // for query matching and layout. Querying never reparses JS slots.
+ let selection = descriptor
+ .css_capabilities()
+ .expect("FontFace has computed CSS descriptors");
candidates.push(Candidate {
face,
- family: moli_css_parse::font_family_name_key(&moli_css_parse::unquote_css_string(
- &family,
- )),
- selection: selection::Selection::new(&weight, &stretch, &style),
- ranges: crate::css_resource_urls::parse_font_unicode_ranges(&ranges)
- .unwrap_or_default(),
+ descriptor,
+ selection,
});
}
let text = text.chars().collect::<Vec<_>>();
@@ -59,7 +55,7 @@ pub(super) fn font_face_set_matching_faces_array<'s>(
let family = moli_css_parse::font_family_name_key(&family);
let family_faces = candidates
.iter()
- .filter(|face| family == face.family)
+ .filter(|face| family == face.descriptor.family_name_key())
.collect::<Vec<_>>();
let Some(best) = family_faces
.iter()
@@ -73,8 +69,12 @@ pub(super) fn font_face_set_matching_faces_array<'s>(
// weight/style instead of the CSS-matched segmented face.
if face.selection.compare(best.selection, requested).is_eq()
&& text.iter().any(|&character| {
- face.ranges.is_empty()
- || face.ranges.iter().any(|range| range.contains(character))
+ face.descriptor.unicode_ranges().is_empty()
+ || face
+ .descriptor
+ .unicode_ranges()
+ .iter()
+ .any(|range| range.contains(character))
})
{
matching.push(face.face);