Skip to content

armgit / moli

Public Imported from GitHub
0

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

Fix unicode-range web font selection

ldm06924b69e20af
diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rs
index 17d4508dc..59b99a19e 100644
--- a/moli-layout/src/inline.rs
+++ b/moli-layout/src/inline.rs
@@ -1305,6 +1305,48 @@ struct InlineBuildInput {
root_style: LayoutBoxId,
}
+fn intern_resolved_inline_style(
+ styles: &mut Vec<TextStyle<'static, 'static, TextBrush>>,
+ style_parents: &mut Vec<LayoutBoxId>,
+ style_samples: &mut Vec<Option<char>>,
+ style: TextStyle<'static, 'static, TextBrush>,
+ structural_parent: LayoutBoxId,
+ sample: Option<char>,
+) -> usize {
+ let style_slot = styles
+ .iter()
+ .enumerate()
+ .position(|(index, candidate)| {
+ *candidate == style && style_parents[index] == structural_parent
+ })
+ .unwrap_or_else(|| {
+ let index = styles.len();
+ styles.push(style);
+ style_parents.push(structural_parent);
+ style_samples.push(None);
+ index
+ });
+ if style_samples[style_slot].is_none() {
+ style_samples[style_slot] = sample;
+ }
+ style_slot
+}
+
+fn append_resolved_inline_run(
+ runs: &mut Vec<(Range<usize>, usize)>,
+ range: Range<usize>,
+ style_slot: usize,
+) {
+ match runs.last_mut() {
+ Some((previous_range, previous_slot))
+ if *previous_slot == style_slot && previous_range.end == range.start =>
+ {
+ previous_range.end = range.end;
+ }
+ _ => runs.push((range, style_slot)),
+ }
+}
+
impl InlineBuildInput {
fn build<N>(
mut self,
@@ -1318,34 +1360,56 @@ impl InlineBuildInput {
let mut root_text_style = world.boxes[self.root_style.index()]
.style
.parley_text_style();
- parley.resolve_system_font_families(&mut root_text_style);
+ parley.resolve_font_families(&mut root_text_style, None);
let quantize = true;
let mut styles = Vec::new();
let mut style_parents = Vec::new();
- let mut style_slots = Vec::with_capacity(self.units.len());
+ let mut style_samples = Vec::new();
+ let mut resolved_runs = Vec::<(Range<usize>, usize)>::new();
for unit in &self.units {
- let mut style = world.boxes[unit.style_box.index()]
+ let mut base_style = world.boxes[unit.style_box.index()]
.style
.parley_text_style();
// `vertical-align` belongs to the structural inline box, not to
// each descendant glyph. Keep glyphs baseline-aligned within their
// direct box state; closing that state moves the complete subtree.
- style.brush.paint = !unit.control;
- parley.resolve_system_font_families(&mut style);
+ base_style.brush.paint = !unit.control;
let structural_parent = unit.ancestors.last().copied().unwrap_or(self.root_style);
- let style_slot = styles
- .iter()
- .enumerate()
- .position(|(index, candidate)| {
- *candidate == style && style_parents[index] == structural_parent
- })
- .unwrap_or_else(|| {
- let index = styles.len();
- styles.push(style);
- style_parents.push(structural_parent);
- index
- });
- style_slots.push(style_slot);
+ if !parley.requires_character_font_resolution(&base_style) {
+ let sample = (!unit.control)
+ .then(|| self.text[unit.output_range.clone()].chars().next())
+ .flatten();
+ parley.resolve_font_families(&mut base_style, None);
+ let style_slot = intern_resolved_inline_style(
+ &mut styles,
+ &mut style_parents,
+ &mut style_samples,
+ base_style,
+ structural_parent,
+ sample,
+ );
+ append_resolved_inline_run(
+ &mut resolved_runs,
+ unit.output_range.clone(),
+ style_slot,
+ );
+ continue;
+ }
+ for (relative_start, character) in self.text[unit.output_range.clone()].char_indices() {
+ let start = unit.output_range.start + relative_start;
+ let end = start + character.len_utf8();
+ let mut style = base_style.clone();
+ parley.resolve_font_families(&mut style, Some(character));
+ let style_slot = intern_resolved_inline_style(
+ &mut styles,
+ &mut style_parents,
+ &mut style_samples,
+ style,
+ structural_parent,
+ (!unit.control).then_some(character),
+ );
+ append_resolved_inline_run(&mut resolved_runs, start..end, style_slot);
+ }
}
let mut builder = parley.layout_context.style_run_builder(
&mut parley.font_context,
@@ -1357,24 +1421,13 @@ impl InlineBuildInput {
.iter()
.map(|style| builder.push_style(style.clone()))
.collect::<Vec<_>>();
- if self.units.is_empty() {
+ if resolved_runs.is_empty() {
let style_index = builder.push_style(root_text_style.clone());
builder.push_style_run(style_index, 0..0);
} else {
- let mut run_start = 0;
- let mut current_slot = style_slots[0];
- for (index, unit) in self.units.iter().enumerate().skip(1) {
- if style_slots[index] == current_slot {
- continue;
- }
- builder.push_style_run(
- style_indices[current_slot],
- run_start..unit.output_range.start,
- );
- run_start = unit.output_range.start;
- current_slot = style_slots[index];
+ for (range, style_slot) in &resolved_runs {
+ builder.push_style_run(style_indices[*style_slot], range.clone());
}
- builder.push_style_run(style_indices[current_slot], run_start..self.text.len());
}
for (object_id, (byte_index, _, kind)) in self.objects.iter().enumerate() {
builder.push_inline_box(InlineBox {
@@ -1386,12 +1439,6 @@ impl InlineBuildInput {
});
}
let layout = builder.build(&self.text);
- let mut style_samples = vec![None; styles.len()];
- for (unit, style_slot) in self.units.iter().zip(&style_slots) {
- if style_samples[*style_slot].is_none() && !unit.control {
- style_samples[*style_slot] = self.text[unit.output_range.clone()].chars().next();
- }
- }
let font_metrics = styles
.iter()
.zip(style_samples)
@@ -1408,7 +1455,7 @@ impl InlineBuildInput {
continue;
}
let mut style = world.boxes[object.box_id.index()].style.parley_text_style();
- parley.resolve_system_font_families(&mut style);
+ parley.resolve_font_families(&mut style, None);
structural_boxes.push(InlineStructuralBox {
box_id: object.box_id,
parent: object.ancestors.last().copied().unwrap_or(self.root_style),
diff --git a/moli-layout/src/lib.rs b/moli-layout/src/lib.rs
index 49f548090..9fac49e07 100644
--- a/moli-layout/src/lib.rs
+++ b/moli-layout/src/lib.rs
@@ -82,7 +82,7 @@ pub use style::{
};
pub use text::{
DocumentLayoutServices, SystemFontPolicy, WebFontFace, WebFontRegistration,
- WebFontRegistrationError, WebFontRegistrationOutcome, WebFontStyle,
+ WebFontRegistrationError, WebFontRegistrationOutcome, WebFontStyle, WebFontUnicodeRange,
};
pub use world::{
LayoutAnonymousReason, LayoutBox, LayoutBoxId, LayoutBoxKind, LayoutCapabilityDiagnostic,
diff --git a/moli-layout/src/text.rs b/moli-layout/src/text.rs
index 9c9592fdf..83f92c122 100644
--- a/moli-layout/src/text.rs
+++ b/moli-layout/src/text.rs
@@ -5,7 +5,7 @@
// - packages/blitz-dom/src/node/text.rs
// - packages/blitz-paint/src/text.rs
-use std::{collections::BTreeMap, sync::Arc};
+use std::{borrow::Cow, collections::BTreeMap, sync::Arc};
use parley::{
FontContext, FontFamily, FontFamilyName, LayoutContext, TextStyle,
@@ -23,12 +23,48 @@ pub(crate) struct ParleyDocumentServices {
pub(crate) font_context: FontContext,
pub(crate) layout_context: LayoutContext<TextBrush>,
system_font_family_resolver: Option<SystemFontFamilyResolver>,
+ web_font_families: BTreeMap<String, SegmentedWebFontFamily>,
inline_font_metrics_cache: Vec<(
TextStyle<'static, 'static, TextBrush>,
Option<InlineFontMetrics>,
)>,
}
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct WebFontCapabilities {
+ width: FontWidth,
+ style: FontStyle,
+ weight: FontWeight,
+}
+
+#[derive(Clone, Debug)]
+struct SegmentedWebFontFace {
+ internal_family_name: String,
+ unicode_ranges: Vec<WebFontUnicodeRange>,
+}
+
+impl SegmentedWebFontFace {
+ fn contains(&self, character: char) -> bool {
+ self.unicode_ranges.is_empty()
+ || self
+ .unicode_ranges
+ .iter()
+ .any(|range| range.contains(character))
+ }
+}
+
+#[derive(Clone, Debug)]
+struct SegmentedWebFontCapabilityGroup {
+ capabilities: WebFontCapabilities,
+ selector_font_identities: Vec<(u64, u32)>,
+ faces: Vec<SegmentedWebFontFace>,
+}
+
+#[derive(Clone, Debug, Default)]
+struct SegmentedWebFontFamily {
+ groups: Vec<SegmentedWebFontCapabilityGroup>,
+}
+
/// Primary-font metrics attached to one resolved Parley text style.
///
/// A shaped run may use a fallback font for its glyphs. CSSOM text geometry,
@@ -51,16 +87,131 @@ impl ParleyDocumentServices {
self.inline_font_metrics_cache.clear();
}
- pub(crate) fn resolve_system_font_families(
+ /// Resolves CSS downloadable-font families into the selected segmented
+ /// face, then applies explicit platform-family substitutions.
+ ///
+ /// Blink first chooses one font-capability group (width/style/weight),
+ /// then considers only that group's faces whose `unicode-range` contains
+ /// the character. Fontique has no segmented-face abstraction, so each
+ /// physical face is registered under an internal family and this method
+ /// preserves that capability-before-range behavior before shaping.
+ pub(crate) fn resolve_font_families(
&mut self,
style: &mut TextStyle<'static, 'static, TextBrush>,
+ character: Option<char>,
) {
+ self.resolve_segmented_web_font_families(style, character);
let Some(resolver) = self.system_font_family_resolver.as_mut() else {
return;
};
resolver.resolve_text_style(&mut self.font_context.collection, style);
}
+ pub(crate) fn requires_character_font_resolution(
+ &self,
+ style: &TextStyle<'static, 'static, TextBrush>,
+ ) -> bool {
+ let family_uses_ranges = |family: &FontFamilyName<'_>| {
+ let FontFamilyName::Named(name) = family else {
+ return false;
+ };
+ self.web_font_families
+ .get(&normalized_web_font_family_name(name))
+ .is_some_and(|family| {
+ family.groups.iter().any(|group| {
+ group
+ .faces
+ .iter()
+ .any(|face| !face.unicode_ranges.is_empty())
+ })
+ })
+ };
+ match &style.font_family {
+ FontFamily::Single(family) => family_uses_ranges(family),
+ FontFamily::List(families) => families.iter().any(family_uses_ranges),
+ FontFamily::Source(source) => FontFamilyName::parse_css_list(source)
+ .filter_map(Result::ok)
+ .any(|family| family_uses_ranges(&family)),
+ }
+ }
+
+ fn resolve_segmented_web_font_families(
+ &mut self,
+ style: &mut TextStyle<'static, 'static, TextBrush>,
+ character: Option<char>,
+ ) {
+ let parsed_source;
+ let families = match &style.font_family {
+ FontFamily::Single(family) => vec![family.clone()],
+ FontFamily::List(families) => families.iter().cloned().collect(),
+ FontFamily::Source(source) => {
+ parsed_source = FontFamilyName::parse_css_list(source)
+ .filter_map(Result::ok)
+ .map(FontFamilyName::into_owned)
+ .collect::<Vec<_>>();
+ parsed_source
+ }
+ };
+ let attributes = Attributes::new(style.font_width, style.font_style, style.font_weight);
+ let mut resolved = Vec::with_capacity(families.len());
+ for family in families {
+ let FontFamilyName::Named(name) = &family else {
+ resolved.push(family);
+ continue;
+ };
+ let family_key = normalized_web_font_family_name(name);
+ if !self.web_font_families.contains_key(&family_key) {
+ resolved.push(family);
+ continue;
+ }
+
+ let selected_identity = {
+ let FontContext {
+ collection,
+ source_cache,
+ } = &mut self.font_context;
+ let mut query = collection.query(source_cache);
+ query.set_families([QueryFamily::Named(name)]);
+ query.set_attributes(attributes);
+ let mut identity = None;
+ query.matches_with(|font| {
+ identity = Some((font.blob.id(), font.index));
+ QueryStatus::Stop
+ });
+ identity
+ };
+ let Some(selected_identity) = selected_identity else {
+ resolved.push(family);
+ continue;
+ };
+ let Some(group) = self.web_font_families[&family_key]
+ .groups
+ .iter()
+ .find(|group| group.selector_font_identities.contains(&selected_identity))
+ else {
+ resolved.push(family);
+ continue;
+ };
+ let default_uses_latin_metrics =
+ character.is_none() && group.faces.iter().any(|face| face.contains('x'));
+ resolved.extend(
+ group
+ .faces
+ .iter()
+ .rev()
+ .filter(|face| match character {
+ Some(character) => face.contains(character),
+ None if default_uses_latin_metrics => face.contains('x'),
+ None => true,
+ })
+ .map(|face| {
+ FontFamilyName::Named(Cow::Owned(face.internal_family_name.clone()))
+ }),
+ );
+ }
+ style.font_family = FontFamily::List(Cow::Owned(resolved));
+ }
+
pub(crate) fn inline_font_metrics(
&mut self,
style: &TextStyle<'static, 'static, TextBrush>,
@@ -191,6 +342,37 @@ pub enum WebFontStyle {
Oblique(Option<f32>),
}
+/// One inclusive CSS `unicode-range` interval attached to a downloadable
+/// font face.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct WebFontUnicodeRange {
+ start: u32,
+ end: u32,
+}
+
+impl WebFontUnicodeRange {
+ pub const fn new(start: u32, end: u32) -> Self {
+ Self { start, end }
+ }
+
+ pub const fn start(self) -> u32 {
+ self.start
+ }
+
+ pub const fn end(self) -> u32 {
+ self.end
+ }
+
+ pub const fn contains(self, character: char) -> bool {
+ let codepoint = character as u32;
+ self.start <= codepoint && codepoint <= self.end
+ }
+
+ const fn is_valid(self) -> bool {
+ self.start <= self.end && self.end <= char::MAX as u32
+ }
+}
+
impl WebFontStyle {
fn to_fontique(self) -> FontStyle {
match self {
@@ -212,6 +394,7 @@ pub struct WebFontFace {
weight: Option<f32>,
stretch: Option<f32>,
style: Option<WebFontStyle>,
+ unicode_ranges: Vec<WebFontUnicodeRange>,
}
impl WebFontFace {
@@ -221,6 +404,7 @@ impl WebFontFace {
weight: None,
stretch: None,
style: None,
+ unicode_ranges: Vec::new(),
}
}
@@ -239,6 +423,16 @@ impl WebFontFace {
self
}
+ /// Sets the face's CSS `unicode-range` intervals. An empty list means the
+ /// full Unicode range, matching the descriptor's initial value.
+ pub fn with_unicode_ranges(
+ mut self,
+ ranges: impl IntoIterator<Item = WebFontUnicodeRange>,
+ ) -> Self {
+ self.unicode_ranges = ranges.into_iter().collect();
+ self
+ }
+
pub fn family_name(&self) -> &str {
&self.family_name
}
@@ -255,6 +449,10 @@ impl WebFontFace {
self.style
}
+ pub fn unicode_ranges(&self) -> &[WebFontUnicodeRange] {
+ &self.unicode_ranges
+ }
+
fn validate(&self) -> Result<(), WebFontRegistrationError> {
if self.family_name.trim().is_empty() {
return Err(WebFontRegistrationError::InvalidDescriptor {
@@ -284,12 +482,17 @@ impl WebFontFace {
detail: "font-style oblique angle must be finite".to_owned(),
});
}
+ if self.unicode_ranges.iter().any(|range| !range.is_valid()) {
+ return Err(WebFontRegistrationError::InvalidDescriptor {
+ detail: "unicode-range intervals must be ordered Unicode scalar bounds".to_owned(),
+ });
+ }
Ok(())
}
- fn fontique_override(&self) -> FontInfoOverride<'_> {
+ fn fontique_override_for_family<'a>(&self, family_name: &'a str) -> FontInfoOverride<'a> {
FontInfoOverride {
- family_name: Some(&self.family_name),
+ family_name: Some(family_name),
width: self.stretch.map(FontWidth::from_percentage),
style: self.style.map(WebFontStyle::to_fontique),
weight: self.weight.map(FontWeight::new),
@@ -499,13 +702,81 @@ fn build_parley_services(
collection,
source_cache: Default::default(),
};
- for font in web_fonts.values() {
- register_font(&mut font_context, font);
+ let mut web_font_families = BTreeMap::<String, SegmentedWebFontFamily>::new();
+ for (source_order, font) in web_fonts.values().enumerate() {
+ let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(Arc::clone(&font.sfnt_bytes));
+ let blob = Blob::new(data);
+ let selector_fonts = font_context.collection.register_fonts(
+ blob.clone(),
+ Some(
+ font.face
+ .fontique_override_for_family(font.face.family_name()),
+ ),
+ );
+ if selector_fonts.is_empty() {
+ continue;
+ }
+
+ let internal_family_name = format!("\0moli-web-font:{source_order}");
+ if font_context
+ .collection
+ .register_fonts(
+ blob.clone(),
+ Some(
+ font.face
+ .fontique_override_for_family(&internal_family_name),
+ ),
+ )
+ .is_empty()
+ {
+ continue;
+ }
+
+ let family = web_font_families
+ .entry(normalized_web_font_family_name(font.face.family_name()))
+ .or_default();
+ for (_, fonts) in selector_fonts {
+ for selector_font in fonts {
+ let capabilities = WebFontCapabilities {
+ width: selector_font.width(),
+ style: selector_font.style(),
+ weight: selector_font.weight(),
+ };
+ let group_index = family
+ .groups
+ .iter()
+ .position(|group| group.capabilities == capabilities)
+ .unwrap_or_else(|| {
+ let index = family.groups.len();
+ family.groups.push(SegmentedWebFontCapabilityGroup {
+ capabilities,
+ selector_font_identities: Vec::new(),
+ faces: Vec::new(),
+ });
+ index
+ });
+ let group = &mut family.groups[group_index];
+ group
+ .selector_font_identities
+ .push((blob.id(), selector_font.index()));
+ if !group
+ .faces
+ .iter()
+ .any(|face| face.internal_family_name == internal_family_name)
+ {
+ group.faces.push(SegmentedWebFontFace {
+ internal_family_name: internal_family_name.clone(),
+ unicode_ranges: font.face.unicode_ranges.clone(),
+ });
+ }
+ }
+ }
}
ParleyDocumentServices {
font_context,
layout_context: LayoutContext::new(),
system_font_family_resolver,
+ web_font_families,
inline_font_metrics_cache: Vec::new(),
}
}
@@ -514,10 +785,20 @@ fn register_font(font_context: &mut FontContext, font: &RegisteredWebFont) -> bo
let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(Arc::clone(&font.sfnt_bytes));
!font_context
.collection
- .register_fonts(Blob::new(data), Some(font.face.fontique_override()))
+ .register_fonts(
+ Blob::new(data),
+ Some(
+ font.face
+ .fontique_override_for_family(font.face.family_name()),
+ ),
+ )
.is_empty()
}
+fn normalized_web_font_family_name(name: &str) -> String {
+ name.chars().flat_map(char::to_lowercase).collect()
+}
+
fn validate_registered_font(
face: &WebFontFace,
sfnt_bytes: Arc<[u8]>,
@@ -598,6 +879,107 @@ mod tests {
);
}
+ fn web_font_style(family: &'static str, weight: f32) -> TextStyle<'static, 'static, TextBrush> {
+ TextStyle {
+ font_family: FontFamily::Single(FontFamilyName::Named(Cow::Borrowed(family))),
+ font_weight: FontWeight::new(weight),
+ ..TextStyle::default()
+ }
+ }
+
+ fn shape_one_character(
+ parley: &mut ParleyDocumentServices,
+ mut style: TextStyle<'static, 'static, TextBrush>,
+ character: char,
+ ) -> Vec<u8> {
+ parley.resolve_font_families(&mut style, Some(character));
+ let text = character.to_string();
+ let mut builder =
+ parley
+ .layout_context
+ .style_run_builder(&mut parley.font_context, &text, 1.0, true);
+ let style_index = builder.push_style(style);
+ builder.push_style_run(style_index, ..);
+ let mut layout = builder.build(&text);
+ layout.break_all_lines(None);
+ layout
+ .lines()
+ .next()
+ .expect("one line")
+ .runs()
+ .next()
+ .expect("one shaped run")
+ .font()
+ .data
+ .as_ref()
+ .to_vec()
+ }
+
+ #[test]
+ fn segmented_web_font_faces_select_the_range_for_each_character() {
+ let mut services =
+ DocumentLayoutServices::with_system_font_policy(SystemFontPolicy::Disabled);
+ services
+ .register_web_font(WebFontRegistration::new(
+ "latin",
+ WebFontFace::new("Moli Segmented")
+ .with_unicode_ranges([WebFontUnicodeRange::new(0x0000, 0x00ff)]),
+ TEST_TTF.to_vec(),
+ ))
+ .unwrap();
+ services
+ .register_web_font(WebFontRegistration::new(
+ "cjk",
+ WebFontFace::new("Moli Segmented")
+ .with_unicode_ranges([WebFontUnicodeRange::new(0x4e00, 0x9fff)]),
+ TEST_CJK_TTF.to_vec(),
+ ))
+ .unwrap();
+ let parley = services.parley_mut();
+
+ assert_eq!(
+ shape_one_character(parley, web_font_style("Moli Segmented", 400.0), 'R'),
+ TEST_TTF
+ );
+ assert_eq!(
+ shape_one_character(parley, web_font_style("moli segmented", 400.0), '中'),
+ TEST_CJK_TTF,
+ "CSS family matching and segmented range selection must both be case-insensitive"
+ );
+ }
+
+ #[test]
+ fn capability_matching_precedes_unicode_range_selection_like_blink() {
+ let mut services =
+ DocumentLayoutServices::with_system_font_policy(SystemFontPolicy::Disabled);
+ services
+ .register_web_font(WebFontRegistration::new(
+ "regular-latin",
+ WebFontFace::new("Moli Capability First")
+ .with_weight(400.0)
+ .with_unicode_ranges([WebFontUnicodeRange::new(0x0000, 0x00ff)]),
+ TEST_TTF.to_vec(),
+ ))
+ .unwrap();
+ services
+ .register_web_font(WebFontRegistration::new(
+ "bold-cjk",
+ WebFontFace::new("Moli Capability First")
+ .with_weight(700.0)
+ .with_unicode_ranges([WebFontUnicodeRange::new(0x4e00, 0x9fff)]),
+ TEST_CJK_TTF.to_vec(),
+ ))
+ .unwrap();
+ let parley = services.parley_mut();
+ let mut style = web_font_style("Moli Capability First", 700.0);
+ parley.resolve_font_families(&mut style, Some('R'));
+
+ assert!(
+ matches!(&style.font_family, FontFamily::List(families) if families.is_empty()),
+ "the regular face must not be used after the bold capability group wins but does not cover the character"
+ );
+ }
+
fn registration(slot: &str, family: &str, bytes: &[u8]) -> WebFontRegistration {
WebFontRegistration::new(
slot,
@@ -716,6 +1098,10 @@ mod tests {
WebFontFace::new("Bad Weight").with_weight(0.0),
WebFontFace::new("Bad Stretch").with_stretch(f32::NAN),
WebFontFace::new("Bad Style").with_style(WebFontStyle::Oblique(Some(f32::INFINITY))),
+ WebFontFace::new("Bad Range")
+ .with_unicode_ranges([WebFontUnicodeRange::new(0x100, 0xff)]),
+ WebFontFace::new("Too High")
+ .with_unicode_ranges([WebFontUnicodeRange::new(0, 0x11_0000)]),
] {
let error = services
.register_web_font(WebFontRegistration::new("slot", face, TEST_TTF.to_vec()))
diff --git a/moli-renderer-v8/src/css_resource_urls.rs b/moli-renderer-v8/src/css_resource_urls.rs
index 195826c05..75dfa97c6 100644
--- a/moli-renderer-v8/src/css_resource_urls.rs
+++ b/moli-renderer-v8/src/css_resource_urls.rs
@@ -1,12 +1,12 @@
use std::collections::HashSet;
-use cssparser::{Parser, ParserInput, Token};
+use cssparser::{Parser, ParserInput, Token, UnicodeRange};
use moli_crypto::sha256_hex;
use moli_css_parse::{
DeclarationParseOptions, parse_declaration_list, parse_font_face_rule_view_with_stylo,
parse_font_faces, parse_stylesheet_rule_snapshots_with_stylo,
};
-use moli_layout::{WebFontFace, WebFontRegistration, WebFontStyle};
+use moli_layout::{WebFontFace, WebFontRegistration, WebFontStyle, WebFontUnicodeRange};
use url::Url;
use crate::protocol_types::OptionalResourceFetchMask;
@@ -198,6 +198,7 @@ struct ParsedWebFontFace {
weight: Option<f32>,
stretch: Option<f32>,
style: Option<WebFontStyle>,
+ unicode_ranges: Vec<WebFontUnicodeRange>,
}
impl ParsedWebFontFace {
@@ -216,7 +217,7 @@ impl ParsedWebFontFace {
if let Some(style) = self.style {
face = face.with_style(style);
}
- face
+ face.with_unicode_ranges(self.unicode_ranges)
}
}
@@ -244,9 +245,23 @@ fn parsed_web_font_face(css_text: &str) -> Option<ParsedWebFontFace> {
weight: descriptor("font-weight").and_then(parse_font_weight_lower_bound),
stretch: descriptor("font-stretch").and_then(parse_font_stretch_lower_bound),
style: descriptor("font-style").and_then(parse_font_style_lower_bound),
+ unicode_ranges: descriptor("unicode-range")
+ .and_then(parse_font_unicode_ranges)
+ .unwrap_or_default(),
})
}
+fn parse_font_unicode_ranges(value: &str) -> Option<Vec<WebFontUnicodeRange>> {
+ let mut input = ParserInput::new(value);
+ let mut input = Parser::new(&mut input);
+ input
+ .parse_comma_separated(|input| {
+ let range = UnicodeRange::parse(input)?;
+ Ok::<_, cssparser::ParseError<'_, ()>>(WebFontUnicodeRange::new(range.start, range.end))
+ })
+ .ok()
+}
+
fn parse_font_weight_lower_bound(value: &str) -> Option<f32> {
match value
.split_ascii_whitespace()
@@ -621,6 +636,7 @@ mod tests {
font-weight: 625 800;
font-stretch: semi-condensed expanded;
font-style: oblique 30deg 45deg;
+ unicode-range: U+0000-00FF, U+4E??;
}
@font-face {
font-family: DemoBold;
@@ -666,6 +682,37 @@ mod tests {
web_font.face().style(),
Some(WebFontStyle::Oblique(Some(30.0)))
);
+ assert_eq!(
+ web_font.face().unicode_ranges(),
+ [
+ WebFontUnicodeRange::new(0x0000, 0x00ff),
+ WebFontUnicodeRange::new(0x4e00, 0x4eff),
+ ]
+ );
+ }
+
+ #[test]
+ fn invalid_unicode_range_falls_back_to_the_full_font_face_range() {
+ let resources = stylesheet_load_blocking_resources(
+ r#"
+ @font-face {
+ font-family: Demo;
+ src: url(fonts/demo.woff2);
+ unicode-range: U+110000-120000;
+ }
+ "#,
+ &base_url(),
+ OptionalResourceFetchMask::FONT,
+ );
+ assert_eq!(resources.len(), 1);
+ assert!(
+ resources[0]
+ .web_font()
+ .expect("font metadata")
+ .face()
+ .unicode_ranges()
+ .is_empty()
+ );
}
#[test]
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
index 2d94c8ac4..fdc87b20d 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
@@ -2639,6 +2639,86 @@ document.close();
.expect("document-owned data web-font layout test should run");
}
+#[tokio::test(flavor = "current_thread")]
+async fn same_family_unicode_range_faces_shape_mixed_text_with_both_subsets() {
+ run_page_vm_async_test(async move {
+ let loader =
+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
+ loader.set_optional_resource_fetch_mask(
+ crate::protocol_types::OptionalResourceFetchMask::FONT,
+ );
+ let mut page_vm = test_page_vm_with_loader_and_document_url(
+ &loader,
+ Vec::new(),
+ Url::parse("https://example.com/segmented-web-font.html")?,
+ );
+ let latin = include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../moli-layout/tests/fixtures/moli-ahem.ttf"
+ ));
+ let cjk = include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../moli-layout/tests/fixtures/moli-cjk.ttf"
+ ));
+ let latin = base64::engine::general_purpose::STANDARD.encode(latin);
+ let cjk = base64::engine::general_purpose::STANDARD.encode(cjk);
+ page_vm.vm_mut().eval(&format!(
+ r#"
+document.head.innerHTML = `<style>
+@font-face {{
+ font-family: MoliSegmented;
+ src: url(data:font/ttf;base64,{latin}) format('truetype');
+ font-weight: 400;
+ unicode-range: U+0000-00FF;
+}}
+@font-face {{
+ font-family: MoliSegmented;
+ src: url(data:font/ttf;base64,{cjk}) format('truetype');
+ font-weight: 400;
+ unicode-range: U+4E00-9FFF;
+}}
+body {{ margin: 0; font: 32px/40px MoliSegmented, sans-serif; }}
+</style>`;
+document.body.textContent = 'R中';
+'installed'
+"#
+ ))?;
+
+ let snapshot = page_vm
+ .vm_mut()
+ .screenshot_layout_snapshot(moli_layout::PaintViewport::new(160, 80, 1.0))?
+ .expect("segmented web-font fixture should have a layout root");
+ let used_fonts = snapshot
+ .fragments
+ .iter()
+ .filter_map(|fragment| match fragment {
+ moli_layout::PaintFragment::GlyphRun(run) => snapshot.font(run.font),
+ _ => None,
+ })
+ .map(|font| font.font.data.as_ref())
+ .collect::<Vec<_>>();
+ assert!(
+ used_fonts.iter().any(|font| *font
+ == include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../moli-layout/tests/fixtures/moli-ahem.ttf"
+ ))),
+ "the Latin character must use the Latin subset"
+ );
+ assert!(
+ used_fonts.iter().any(|font| *font
+ == include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../moli-layout/tests/fixtures/moli-cjk.ttf"
+ ))),
+ "the CJK character must use the CJK subset"
+ );
+ Ok::<_, anyhow::Error>(())
+ })
+ .await
+ .expect("segmented web-font layout test should run");
+}
+
#[tokio::test(flavor = "current_thread")]
async fn cjk_regular_face_uses_chromium_synthetic_bold_threshold() {
run_page_vm_async_test(async move {