use super::*;
use crate::css::{FontSizeAdjust, FontSizeAdjustValue, FontVariationSettings};
use crate::text::system::api::{font_feature_family, font_size_adjust_metric_ratio};
#[derive(Debug)]
pub(in crate::text) struct SelectedFaceStyleView<'a> {
authored: &'a ComputedStyle,
selected_family: Option<FontFamily>,
}
impl<'a> SelectedFaceStyleView<'a> {
pub(in crate::text) fn new(
authored: &'a ComputedStyle,
selected_family: Option<FontFamily>,
) -> Self {
Self {
authored,
selected_family,
}
}
pub(in crate::text) fn authored(&self) -> &'a ComputedStyle {
self.authored
}
pub(in crate::text) fn font_family(&self) -> &FontFamily {
self.selected_family
.as_ref()
.unwrap_or(&self.authored.font_family)
}
pub(in crate::text) fn has_same_effective_style(&self, other: &Self) -> bool {
self.authored == other.authored && self.font_family() == other.font_family()
}
pub(in crate::text) fn boundary_effect(&self, other: &Self) -> InlineBoundaryEffect {
if self.has_same_shaping_inputs(other) {
InlineBoundaryEffect::PaintOnly
} else {
InlineBoundaryEffect::ShapingInputChange
}
}
fn has_same_shaping_inputs(&self, other: &Self) -> bool {
let left = self.authored;
let right = other.authored;
self.font_family() == other.font_family()
&& left.font_size == right.font_size
&& left.font_size_adjust == right.font_size_adjust
&& left.line_height == right.line_height
&& left.font_weight == right.font_weight
&& left.font_style == right.font_style
&& left.font_width == right.font_width
&& left.font_synthesis == right.font_synthesis
&& left.font_feature_settings == right.font_feature_settings
&& left.text_spacing_trim == right.text_spacing_trim
&& left.font_variation_settings == right.font_variation_settings
&& left.font_kerning == right.font_kerning
&& left.font_variant_ligatures == right.font_variant_ligatures
&& left.font_variant_position == right.font_variant_position
&& left.font_variant_caps == right.font_variant_caps
&& left.font_variant_numeric == right.font_variant_numeric
&& left.font_variant_alternates == right.font_variant_alternates
&& left.font_variant_east_asian == right.font_variant_east_asian
&& left.font_variant_emoji == right.font_variant_emoji
&& left.overflow_wrap == right.overflow_wrap
&& left.text_wrap_mode == right.text_wrap_mode
&& left.word_spacing == right.word_spacing
&& left.letter_spacing == right.letter_spacing
&& left.language == right.language
&& left.writing_mode == right.writing_mode
&& left.text_orientation == right.text_orientation
}
}
impl FontSystem {
pub(in crate::text) fn with_reusable_parley_layout<T>(
&mut self,
shape: impl FnOnce(&mut Self, &mut ParleyLayout<FontPalette>) -> T,
) -> T {
let mut layout = std::mem::take(&mut self.parley_layout_scratch);
let result = shape(self, &mut layout);
self.parley_layout_scratch = layout;
result
}
#[cfg(test)]
pub(crate) fn new() -> Self {
Self::from_seed(Self::sync_seed())
}
pub(crate) fn into_fonts(self) -> Vec<DocumentFont> {
self.document_fonts.into_fonts()
}
pub(in crate::text) fn font_feature_context_for_style(
&mut self,
style: &ComputedStyle,
) -> Option<FontFeatureContext> {
self.font_feature_context_for_family(style, &style.font_family)
}
pub(in crate::text) fn font_feature_context_for_selected_face(
&mut self,
style: &SelectedFaceStyleView<'_>,
) -> Option<FontFeatureContext> {
self.font_feature_context_for_family(style.authored(), style.font_family())
}
fn font_feature_context_for_family(
&mut self,
style: &ComputedStyle,
family: &FontFamily,
) -> Option<FontFeatureContext> {
let selected_face = self
.resolve_font_family(
family,
style.font_weight,
style.font_style,
style.font_width,
)
.and_then(|font_id| self.document_fonts.selected_face_features(font_id));
let (family, face_defaults) = selected_face
.map(|(family, defaults)| (Some(family), Some(defaults)))
.unwrap_or_else(|| (font_feature_family(family), None));
if face_defaults.is_none() && self.font_feature_values.values.is_empty() {
return None;
}
Some(FontFeatureContext {
family,
face_defaults,
font_feature_values: self.font_feature_values.clone(),
})
}
fn selected_face_variation_settings(
&mut self,
style: &ComputedStyle,
family: &FontFamily,
) -> Option<FontVariationSettings> {
let font_id = self.resolve_font_family(
family,
style.font_weight,
style.font_style,
style.font_width,
)?;
let descriptor = self.document_fonts.selected_face_variations(font_id)?;
if descriptor.0.is_empty() {
return None;
}
let mut variations = descriptor.0;
for setting in &style.font_variation_settings.0 {
if let Some(existing) = variations
.iter_mut()
.find(|existing| existing.tag == setting.tag)
{
*existing = *setting;
} else {
variations.push(*setting);
}
}
variations.sort_by_key(|setting| setting.tag);
Some(FontVariationSettings(variations))
}
pub(in crate::text) fn shaping_style_for_selected_face<'a>(
&mut self,
style: &'a ComputedStyle,
) -> ParleyStyleView<'a> {
let selected_style = SelectedFaceStyleView::new(style, None);
self.shaping_style_for_selected_face_view(&selected_style)
}
pub(in crate::text) fn shaping_style_for_selected_face_view<'a>(
&mut self,
style: &SelectedFaceStyleView<'a>,
) -> ParleyStyleView<'a> {
let authored = style.authored();
let mut shaping_style = ParleyStyleView::new(authored);
if let Some(variations) =
self.selected_face_variation_settings(authored, style.font_family())
{
shaping_style.set_font_variation_settings(variations);
}
let Some(font_id) = self.resolve_metric_font_for_family(authored, style.font_family())
else {
return shaping_style;
};
let selected_attributes = self.document_fonts.selected_face_fixed_attributes(font_id);
let is_registered_css_family = named_font_families(style.font_family())
.iter()
.any(|family| self.document_fonts.has_registered_css_family(family));
let intrinsic_attributes = is_registered_css_family
.then(|| {
self.document_fonts.get(font_id).and_then(|font| {
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
Some((
FontWeight::from_number(face.weight().to_number() as f32)?,
if face.is_italic() {
FontStyle::Italic
} else {
FontStyle::Normal
},
))
})
})
.flatten();
let (weight, face_style) = match (selected_attributes, intrinsic_attributes) {
(Some((weight, style)), _) => (weight, style),
(None, Some((weight, style))) => (Some(weight), style),
(None, None) => return shaping_style,
};
if !authored.font_synthesis.weight
&& let Some(weight) = weight
&& weight != authored.font_weight
{
shaping_style.set_font_weight(weight);
}
if !authored.font_synthesis.style && face_style != authored.font_style {
shaping_style.set_font_style(face_style);
}
shaping_style
}
pub(crate) fn resolve_style(&mut self, style: &ComputedStyle) -> Option<usize> {
if let Some(id) = self.resolve_font_family(
&style.font_family,
style.font_weight,
style.font_style,
style.font_width,
) {
return Some(id);
}
self.resolve_system_fallback_for_character(
'M',
style.font_weight,
style.font_style,
style.font_width,
)
}
pub(crate) fn selected_font_supports_caps_feature(&mut self, style: &ComputedStyle) -> bool {
let Some(font_id) = self.resolve_metric_font_for_style(style) else {
return false;
};
let Some(font) = self.document_fonts.get(font_id) else {
return false;
};
let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
return false;
};
let Some(gsub) = face.tables().gsub else {
return false;
};
[b"smcp", b"c2sc", b"pcap", b"c2pc"]
.into_iter()
.any(|wanted| {
let wanted = ttf_parser::Tag::from_bytes(wanted);
gsub.features
.into_iter()
.any(|feature| feature.tag == wanted)
})
}
pub(in crate::text) fn used_font_size_for_font(
&mut self,
style: &ComputedStyle,
font_id: usize,
) -> Option<f32> {
self.font_size_adjusted_size_for_font_id(style, font_id)
}
pub(in crate::text) fn font_size_adjusted_size_for_font_id(
&mut self,
style: &ComputedStyle,
font_id: usize,
) -> Option<f32> {
let adjusted = match style.font_size_adjust {
FontSizeAdjust::None => self
.document_fonts
.font_size_adjust(font_id)
.map_or(style.font_size, |factor| style.font_size * factor),
FontSizeAdjust::Value { metric, value } => {
let target_ratio = match value {
FontSizeAdjustValue::Number(value) => value,
FontSizeAdjustValue::FromFont => {
let primary_font_id = self.resolve_metric_font_for_style(style)?;
let font = self.document_fonts.get(primary_font_id)?;
font_size_adjust_metric_ratio(font, metric)?
}
};
let font = self.document_fonts.get(font_id)?;
let selected_ratio = font_size_adjust_metric_ratio(font, metric)?;
if selected_ratio <= 0.0 {
return None;
}
style.font_size * target_ratio / selected_ratio
}
};
adjusted.is_finite().then_some(adjusted)
}
pub(in crate::text) fn character_font_match(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<CharacterFontMatch> {
let families = match &style.font_family {
FontFamily::List(families) => families.as_slice(),
family => std::slice::from_ref(family),
};
for family in families {
let matched = match family {
FontFamily::Named(name) => self
.resolve_single_family(
name.as_str(),
style.font_weight,
style.font_style,
style.font_width,
)
.and_then(|font_id| {
self.document_fonts.character_font_match(font_id, character)
}),
_ => self
.resolve_font_family(
family,
style.font_weight,
style.font_style,
style.font_width,
)
.and_then(|font_id| {
self.document_fonts.character_font_match(font_id, character)
}),
};
if matched.is_some() {
return matched;
}
}
self.resolve_system_fallback_for_character(
character,
style.font_weight,
style.font_style,
style.font_width,
)
.and_then(|font_id| self.document_fonts.character_font_match(font_id, character))
}
#[cfg(test)]
pub(in crate::text) fn font_for_character(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<usize> {
self.character_font_match(style, character)
.map(|matched| matched.font_id)
}
pub(in crate::text) fn font_for_character_in_family(
&mut self,
style: &ComputedStyle,
family: &FontFamily,
character: char,
) -> Option<usize> {
self.resolve_family_fallback_for_character_in_family(style, family, character)
}
pub(in crate::text) fn vertical_upright_ch_advance(
&mut self,
style: &ComputedStyle,
) -> Option<f32> {
self.vertical_glyph_advance_for_style(style, '0')
}
pub(super) fn vertical_glyph_advance_for_style(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<f32> {
let matched = self.metric_glyph_match_for_style(style, character)?;
let used_font_size = self
.font_size_adjusted_size_for_font_id(style, matched.font_id)
.unwrap_or(style.font_size);
let font = self.document_fonts.get(matched.font_id)?;
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
let units_per_em = font.units_per_em.max(1) as f32;
let advance = face
.glyph_ver_advance(matched.glyph_id.raw())
.map(|advance| advance as f32)
.unwrap_or(units_per_em);
Some(advance * used_font_size / units_per_em)
}
}
pub(super) fn named_font_families(family: &FontFamily) -> Vec<String> {
match family {
FontFamily::Named(name) => vec![name.as_str().to_owned()],
FontFamily::List(families) => families.iter().flat_map(named_font_families).collect(),
_ => Vec::new(),
}
}
pub(super) fn parley_run_fallback_character(text: &str, range: Range<usize>) -> char {
text.get(range)
.and_then(|text| text.chars().find(|character| !character.is_control()))
.unwrap_or(' ')
}