Skip to content

armgit / moli

Public Imported from GitHub
0

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

perf(fonts): register validated font data without copying

ldm0a2cf5c816b74
diff --git a/moli-layout/src/text.rs b/moli-layout/src/text.rs
index 7bf82bd21..d7964f6b0 100644
--- a/moli-layout/src/text.rs
+++ b/moli-layout/src/text.rs
@@ -672,16 +672,48 @@ impl WebFontFace {
pub struct WebFontRegistration {
slot: String,
face: WebFontFace,
- bytes: Vec<u8>,
+ payload: WebFontPayload,
stylesheet: bool,
}
+#[derive(Clone, Debug)]
+enum WebFontPayload {
+ Encoded(Vec<u8>),
+ Validated(FontFaceData),
+}
+
+impl WebFontPayload {
+ fn bytes(&self) -> &[u8] {
+ match self {
+ Self::Encoded(bytes) => bytes,
+ Self::Validated(data) => data.bytes(),
+ }
+ }
+}
+
+impl PartialEq for WebFontPayload {
+ fn eq(&self, other: &Self) -> bool {
+ self.bytes() == other.bytes()
+ }
+}
+
impl WebFontRegistration {
pub fn new(slot: impl Into<String>, face: WebFontFace, bytes: Vec<u8>) -> Self {
Self {
slot: slot.into(),
face,
- bytes,
+ payload: WebFontPayload::Encoded(bytes),
+ stylesheet: false,
+ }
+ }
+
+ /// Retains already validated font data without copying or decoding its
+ /// payload again. Descriptors are still validated for this registration.
+ pub fn from_font_data(slot: impl Into<String>, face: WebFontFace, data: FontFaceData) -> Self {
+ Self {
+ slot: slot.into(),
+ face,
+ payload: WebFontPayload::Validated(data),
stylesheet: false,
}
}
@@ -702,7 +734,7 @@ impl WebFontRegistration {
}
pub fn bytes(&self) -> &[u8] {
- &self.bytes
+ self.payload.bytes()
}
}
@@ -894,7 +926,10 @@ impl DocumentLayoutServices {
return Err(WebFontRegistrationError::EmptySlot);
}
registration.face.validate()?;
- let sfnt_bytes = decode_web_font_bytes(registration.bytes)?;
+ let (sfnt_bytes, validated) = match registration.payload {
+ WebFontPayload::Encoded(bytes) => (decode_web_font_bytes(bytes)?, false),
+ WebFontPayload::Validated(data) => (data.bytes, true),
+ };
let font = RegisteredWebFont {
face: registration.face,
sfnt_bytes,
@@ -920,13 +955,17 @@ impl DocumentLayoutServices {
if outcome == WebFontRegistrationOutcome::Added
&& let Some(parley) = self.parley.as_mut()
{
- validate_sfnt_table_bounds(font.sfnt_bytes.as_ref())?;
+ if !validated {
+ validate_sfnt_table_bounds(font.sfnt_bytes.as_ref())?;
+ }
parley.register_web_font(&registration.slot, &font, &self.web_font_order)?;
self.web_fonts.insert(registration.slot, font);
parley.invalidate_font_selection();
return Ok(outcome);
}
- validate_registered_font(&font.face, font.sfnt_bytes.clone())?;
+ if !validated {
+ validate_registered_font(&font.face, font.sfnt_bytes.clone())?;
+ }
self.web_fonts.insert(registration.slot, font);
if self.parley.is_some() {
// Replacement still needs a rebuild: Fontique can unregister by
diff --git a/moli-layout/src/text/tests/registration.rs b/moli-layout/src/text/tests/registration.rs
index 68b7c79c3..1d3b8ff16 100644
--- a/moli-layout/src/text/tests/registration.rs
+++ b/moli-layout/src/text/tests/registration.rs
@@ -89,6 +89,34 @@ fn owned_and_borrowed_validation_have_the_same_acceptance() {
}
}
+#[test]
+fn validated_font_data_keeps_its_blob_through_registration_and_layout() {
+ let data = FontFaceData::from_bytes(TEST_WOFF2).unwrap();
+ let original_buffer = data.bytes().as_ptr();
+ let blob_id = data.bytes.id();
+ let mut services = DocumentLayoutServices::with_system_font_policy(SystemFontPolicy::Disabled);
+ services
+ .register_web_font(WebFontRegistration::from_font_data(
+ "validated",
+ WebFontFace::new("Validated"),
+ data,
+ ))
+ .unwrap();
+ assert_eq!(services.web_fonts["validated"].sfnt_bytes.id(), blob_id);
+ assert_eq!(
+ services.web_fonts["validated"].sfnt_bytes.as_ref().as_ptr(),
+ original_buffer
+ );
+ let parley = services.parley_mut();
+ let family = resolved_registered_family_name(parley, "Validated").unwrap();
+ let font = parley
+ .font_context
+ .collection
+ .family_by_name(&family)
+ .unwrap();
+ assert_eq!(font.fonts()[0].load(None).unwrap().id(), blob_id);
+}
+
fn source_id(services: &mut ParleyDocumentServices, family: &str) -> parley::fontique::SourceId {
let family = resolved_registered_family_name(services, family).unwrap();
services
diff --git a/moli-renderer-v8/src/font_loading.rs b/moli-renderer-v8/src/font_loading.rs
index 31810fe0b..cc800e5b9 100644
--- a/moli-renderer-v8/src/font_loading.rs
+++ b/moli-renderer-v8/src/font_loading.rs
@@ -181,10 +181,10 @@ impl FontFaceResource {
let LoadState::Loaded(data) = &self.state else {
return Ok(None);
};
- let mut registration = WebFontRegistration::new(
+ let mut registration = WebFontRegistration::from_font_data(
self.slot.clone(),
self.descriptor.clone(),
- data.bytes().to_vec(),
+ data.clone(),
);
if self.stylesheet {
registration = registration.with_stylesheet_scope();