use read_fonts::tables::variations::DeltaSetIndex;
use read_fonts::types::{F2Dot14, Fixed, Tag};
use read_fonts::{FontRef, TableProvider};
use super::*;
use crate::css::{BaselineMetric, BaselineShift, FontSizeAdjustMetric, TextLayoutPolicy};
#[cfg(test)]
use crate::document::paint::text::RenderedLine;
use crate::document::paint::text::RenderedTextRun;
use crate::units::{LayoutLength, layout_points, layout_pt};
impl FontSystem {
pub(crate) fn baseline_offset_for_style(
&mut self,
style: &ComputedStyle,
metric: BaselineMetric,
) -> LayoutLength {
let metric = baseline_metric_for_typography(metric, style.text_layout_policy());
let font_id = self.resolve_metric_font_for_style(style);
let used_font_size = font_id
.and_then(|id| self.font_size_adjusted_size_for_font_id(style, id))
.unwrap_or(style.font_size);
let Some(font_id) = font_id else {
return layout_pt(synthesized_baseline_offset(
metric,
style.font_size,
style.font_size,
self.used_x_height_for_style(style).points(),
));
};
self.baseline_offset_for_font(style, font_id, used_font_size, metric)
.unwrap_or_else(|| layout_pt(style.font_size))
}
fn baseline_offset_for_font(
&self,
style: &ComputedStyle,
font_id: usize,
used_font_size: f32,
metric: BaselineMetric,
) -> Option<LayoutLength> {
let metric = baseline_metric_for_typography(metric, style.text_layout_policy());
let x_height = self
.x_height_for_font(font_id, used_font_size)
.unwrap_or_else(|| layout_pt(used_font_size * 0.5))
.points();
let font = self.document_fonts.get(font_id)?;
let units_per_em = font.units_per_em.max(1) as f32;
let coordinate_value = |coordinate: crate::document::OpenTypeBaselineCoordinate| {
let delta = coordinate
.variation_index
.map(|index| self.base_variation_delta(font_id, index, style))
.unwrap_or(0.0);
f32::from(coordinate.design_units) + delta
};
let tagged_coordinate =
|tag| baseline_coordinate_for_style(font, style, tag).map(coordinate_value);
let (central_coordinate, ideographic_under_coordinate) =
derive_ideographic_design_coordinates(
tagged_coordinate(*b"idtp"),
tagged_coordinate(*b"ideo"),
units_per_em,
);
let content_over_coordinate = font.layout_metrics.ascender as f32;
let base_offset = |coordinate| {
css_content_baseline_offset(
content_over_coordinate,
coordinate,
used_font_size,
units_per_em,
)
};
let tagged_offset = |tag| tagged_coordinate(tag).map(base_offset);
let alphabetic = tagged_offset(*b"romn")
.unwrap_or_else(|| font.layout_metrics.ascender as f32 * used_font_size / units_per_em);
let central = central_coordinate.map(base_offset);
let ideographic = ideographic_under_coordinate.map(base_offset);
let baseline_coordinate = match metric {
BaselineMetric::Central => central,
BaselineMetric::Ideographic => ideographic,
_ => baseline_tag_for_metric(metric).and_then(|tag| tagged_offset(*tag)),
};
let measured_baseline = baseline_coordinate.is_none().then(|| {
self.measured_baseline_offset_for_style(
font_id,
style,
metric,
alphabetic,
used_font_size,
)
});
let offset = match metric {
BaselineMetric::TextTop => 0.0,
BaselineMetric::TextBottom => used_font_size,
BaselineMetric::Central => baseline_coordinate.unwrap_or(used_font_size / 2.0),
BaselineMetric::Middle => baseline_coordinate.unwrap_or(alphabetic - x_height / 2.0),
_ => baseline_coordinate
.or(measured_baseline.flatten())
.unwrap_or_else(|| {
synthesized_baseline_offset(metric, alphabetic, used_font_size, x_height)
}),
};
Some(layout_pt(offset))
}
fn content_baseline_offset_for_style(&mut self, style: &ComputedStyle) -> LayoutLength {
self.baseline_offset_for_style(style, BaselineMetric::Alphabetic)
}
fn measured_baseline_offset_for_style(
&self,
font_id: usize,
style: &ComputedStyle,
metric: BaselineMetric,
alphabetic: f32,
used_font_size: f32,
) -> Option<f32> {
let font = self.document_fonts.get(font_id)?;
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
let design_coordinate = match metric {
BaselineMetric::Mathematical => ['\u{2212}', '+'].into_iter().find_map(|character| {
let glyph = face.glyph_index(character)?;
let bounds = face.glyph_bounding_box(glyph)?;
Some((f32::from(bounds.y_min) + f32::from(bounds.y_max)) / 2.0)
}),
BaselineMetric::Hanging => hanging_measurement_characters(style.language.as_deref())
.iter()
.find_map(|&character| {
let glyph = face.glyph_index(character)?;
face.glyph_bounding_box(glyph)
.map(|bounds| f32::from(bounds.y_max))
}),
_ => None,
}?;
Some(alphabetic - design_coordinate * used_font_size / f32::from(font.units_per_em.max(1)))
}
fn base_variation_delta(
&self,
font_id: usize,
index: crate::document::OpenTypeVariationIndex,
style: &ComputedStyle,
) -> f32 {
let Some(font) = self.document_fonts.get(font_id) else {
return 0.0;
};
let Ok(font_ref) = FontRef::from_index(&font.data, font.face_index) else {
return 0.0;
};
let (Ok(base), Ok(fvar)) = (font_ref.base(), font_ref.fvar()) else {
return 0.0;
};
let Some(Ok(store)) = base.item_var_store() else {
return 0.0;
};
let Ok(axes) = fvar.axes() else {
return 0.0;
};
let axis_count = axes.len();
let mut normalized = vec![F2Dot14::ZERO; axis_count];
let avar = font_ref.avar().ok();
fvar.user_to_normalized(
avar.as_ref(),
self.used_variation_coordinates(style, font_id),
&mut normalized,
);
store
.compute_delta(
DeltaSetIndex {
outer: index.outer,
inner: index.inner,
},
&normalized,
)
.map_or(0.0, |delta| delta as f32)
}
fn used_variation_coordinates(
&self,
style: &ComputedStyle,
font_id: usize,
) -> Vec<(Tag, Fixed)> {
let mut coordinates = vec![
(
Tag::new(b"wdth"),
Fixed::from_f64(style.font_width.0 as f64 / 10.0),
),
(
Tag::new(b"wght"),
Fixed::from_f64(style.font_weight.0 as f64),
),
];
let effective_style = style.font_style;
if let Some(angle) =
effective_style
.oblique_angle()
.or(matches!(effective_style, FontStyle::Italic).then_some(14.0))
{
coordinates.push((Tag::new(b"slnt"), Fixed::from_f64(-f64::from(angle))));
}
let mut apply = |settings: &FontVariationSettings| {
for setting in &settings.0 {
let tag = Tag::new(&setting.tag);
let value = Fixed::from_f64(f64::from(f32::from_bits(setting.value)));
if let Some(existing) = coordinates
.iter_mut()
.find(|(existing, _)| *existing == tag)
{
existing.1 = value;
} else {
coordinates.push((tag, value));
}
}
};
if let Some(settings) = self.document_fonts.selected_face_variations(font_id) {
apply(&settings);
}
apply(&style.font_variation_settings);
coordinates
}
pub(crate) fn ch_advance(&mut self, style: &ComputedStyle) -> LayoutLength {
if matches!(
style.text_layout_policy(),
TextLayoutPolicy::Vertical(TextOrientation::Upright)
) {
if let Some(advance) = self.vertical_upright_ch_advance(style) {
return layout_pt(advance);
}
return layout_pt(style.font_size);
}
layout_pt(
self.font_glyph_advance_for_style(style, '0')
.unwrap_or(style.font_size * 0.5),
)
}
fn font_glyph_advance_for_style(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<f32> {
let mut glyphs = self
.shape_text_runs_with_parley(&character.to_string(), style)
.into_iter()
.flat_map(|run| run.glyphs);
let first = glyphs.next()?;
Some(
std::iter::once(first)
.chain(glyphs)
.map(|glyph| glyph.x_advance)
.sum(),
)
}
pub(super) fn metric_glyph_match_for_style(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<CharacterFontMatch> {
self.character_font_match(style, character)
}
fn selected_font_glyph_advance_for_style(
&mut self,
style: &ComputedStyle,
character: char,
) -> Option<f32> {
let matched = self.metric_glyph_match_for_style(style, character)?;
let font_size = self.used_font_size_for_font(style, matched.font_id)?;
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_hor_advance(matched.glyph_id.raw())?;
Some(advance as f32 * font_size / units_per_em)
}
pub(crate) fn ic_advance_for_style(&mut self, style: &ComputedStyle) -> LayoutLength {
if matches!(style.text_layout_policy(), TextLayoutPolicy::Vertical(_)) {
return layout_pt(
self.vertical_glyph_advance_for_style(style, '水')
.unwrap_or(style.font_size),
);
}
layout_pt(
self.selected_font_glyph_advance_for_style(style, '水')
.unwrap_or(style.font_size),
)
}
}
fn hanging_measurement_characters(language: Option<&str>) -> &'static [char] {
match opentype_script_for_language(language) {
Some([b'd', b'e', b'v', b'2']) => &['क'],
Some([b'b', b'n', b'g', b'2']) => &['ক'],
Some([b'g', b'u', b'r', b'2']) => &['ਕ'],
Some([b'g', b'j', b'r', b'2']) => &['ક'],
Some([b'o', b'r', b'y', b'2']) => &['କ'],
Some([b'm', b'l', b'm', b'2']) => &['ക'],
Some([b't', b'e', b'l', b'2']) => &['క'],
Some([b'k', b'n', b'd', b'2']) => &['ಕ'],
Some([b't', b'i', b'b', b't']) => &['ཀ'],
Some([b'h', b'e', b'b', b'r']) => &['ה'],
_ => &[],
}
}
impl FontSystem {
pub(crate) fn used_line_height(&mut self, style: &ComputedStyle) -> LayoutLength {
if !style.line_height_is_normal() {
return layout_pt(style.line_height);
}
let font_id = self.resolve_metric_font_for_style(style);
self.line_height_for_font(font_id, style)
}
pub(crate) fn line_height_for_font(
&self,
font_id: Option<usize>,
style: &ComputedStyle,
) -> LayoutLength {
if !style.line_height_is_normal() {
return layout_pt(style.line_height);
}
let Some(font) = font_id.and_then(|id| self.document_fonts.get(id)) else {
return layout_pt(style.line_height);
};
let font_height = (font.layout_metrics.ascender as f32
- font.layout_metrics.descender as f32
+ font.layout_metrics.line_gap as f32)
.max(0.0)
* style.font_size
/ font.units_per_em.max(1) as f32;
layout_pt(font_height.max(style.font_size))
}
pub(crate) fn resolved_inline_text_metrics(
&mut self,
style: &ComputedStyle,
) -> ResolvedInlineTextMetrics {
let selected_font_id = self.resolve_metric_font_for_style(style);
let content = self
.content_extents_for_style_font(selected_font_id, style)
.unwrap_or_else(|| FontRunVerticalExtents::from_points(style.font_size, 0.0));
let line = self.line_extents_for_style_font(selected_font_id, style, content);
ResolvedInlineTextMetrics { content, line }
}
pub(crate) fn resolved_inline_text_metrics_for_selected_font(
&self,
style: &ComputedStyle,
font_id: usize,
used_font_size: f32,
) -> Option<ResolvedInlineTextMetrics> {
let content_baseline_offset = self
.baseline_offset_for_font(style, font_id, used_font_size, BaselineMetric::Alphabetic)?
.points();
let content =
self.content_extents_for_font(Some(font_id), used_font_size, content_baseline_offset)?;
let line =
self.line_extents_for_style_font_at_size(Some(font_id), style, used_font_size, content);
Some(ResolvedInlineTextMetrics { content, line })
}
fn content_extents_for_style_font(
&mut self,
font_id: Option<usize>,
style: &ComputedStyle,
) -> Option<FontRunVerticalExtents> {
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
let content_baseline_offset = self.content_baseline_offset_for_style(style).points();
self.content_extents_for_font(font_id, used_font_size, content_baseline_offset)
}
fn content_extents_for_font(
&self,
font_id: Option<usize>,
font_size: f32,
content_baseline_offset: f32,
) -> Option<FontRunVerticalExtents> {
font_id.and_then(|id| self.document_fonts.get(id))?;
if !font_size.is_finite() || font_size < 0.0 {
return None;
}
let above_baseline = content_baseline_offset;
let below_baseline = font_size - above_baseline;
Some(FontRunVerticalExtents::from_points(
above_baseline,
below_baseline,
))
}
fn line_extents_for_style_font(
&mut self,
font_id: Option<usize>,
style: &ComputedStyle,
content: FontRunVerticalExtents,
) -> FontRunVerticalExtents {
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
self.line_extents_for_style_font_at_size(font_id, style, used_font_size, content)
}
fn line_extents_for_style_font_at_size(
&self,
font_id: Option<usize>,
style: &ComputedStyle,
used_font_size: f32,
content: FontRunVerticalExtents,
) -> FontRunVerticalExtents {
if style.line_height_is_normal() {
self.normal_line_extents_for_font(font_id, used_font_size, content)
} else {
self.explicit_line_extents_for_content(style.line_height, content)
}
}
fn normal_line_extents_for_font(
&self,
font_id: Option<usize>,
font_size: f32,
content: FontRunVerticalExtents,
) -> FontRunVerticalExtents {
let line_height = font_id
.and_then(|id| self.document_fonts.get(id))
.map(|font| {
(font.layout_metrics.ascender as f32 - font.layout_metrics.descender as f32
+ font.layout_metrics.line_gap as f32)
.max(0.0)
* font_size
/ font.units_per_em.max(1) as f32
})
.unwrap_or(font_size)
.max(font_size);
self.explicit_line_extents_for_content(line_height, content)
}
fn explicit_line_extents_for_content(
&self,
line_height: f32,
content: FontRunVerticalExtents,
) -> FontRunVerticalExtents {
let leading = (line_height - layout_points(content.block_size())) / 2.0;
content.with_symmetric_leading(layout_pt(leading))
}
pub(crate) fn text_decoration_metrics(
&self,
font_id: Option<usize>,
style: &ComputedStyle,
) -> TextDecorationFontMetrics {
let fallback = TextDecorationFontMetrics {
underline_position: -style.font_size / 9.0,
underline_thickness: (style.font_size / 16.0).max(0.5),
strikeout_position: style.font_size * 0.3,
strikeout_thickness: (style.font_size / 16.0).max(0.5),
descender_depth: style.font_size * 0.2,
};
let Some(font) = font_id.and_then(|id| self.document_fonts.get(id)) else {
return fallback;
};
let scale = style.font_size / font.units_per_em.max(1) as f32;
let mut metrics = TextDecorationFontMetrics {
underline_position: fallback.underline_position,
underline_thickness: fallback.underline_thickness,
strikeout_position: fallback.strikeout_position,
strikeout_thickness: fallback.strikeout_thickness,
descender_depth: (-font.program_metrics.descender as f32 * scale).max(0.0),
};
if let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) {
if let Some(underline) = face.underline_metrics() {
metrics.underline_position = underline.position as f32 * scale;
metrics.underline_thickness = (underline.thickness as f32 * scale).abs().max(0.5);
}
if let Some(strikeout) = face.strikeout_metrics() {
metrics.strikeout_position = strikeout.position as f32 * scale;
metrics.strikeout_thickness = (strikeout.thickness as f32 * scale).abs().max(0.5);
}
}
metrics
}
pub(crate) fn glyph_ink_boxes_for_runs(
&self,
runs: &[RenderedTextRun],
baseline_y: f32,
) -> Vec<GlyphInkBox> {
let mut boxes = Vec::new();
for run in runs {
let Some(font_id) = run.font_id else {
continue;
};
let Some(font) = self.document_fonts.get(font_id) else {
continue;
};
let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
continue;
};
let Some(glyphs) = &run.glyphs else {
continue;
};
let scale = run.font_size / font.units_per_em.max(1) as f32;
let mut pen_x = 0.0;
for glyph in glyphs {
if let Some(glyph_id) = glyph.painted_id()
&& let Some(bbox) = face.glyph_bounding_box(ttf_parser::GlyphId(glyph_id))
{
let origin_x = pen_x + glyph.x_offset;
let origin_y = glyph.y_offset;
let corners = [
(
origin_x + bbox.x_min as f32 * scale,
origin_y + bbox.y_min as f32 * scale,
),
(
origin_x + bbox.x_min as f32 * scale,
origin_y + bbox.y_max as f32 * scale,
),
(
origin_x + bbox.x_max as f32 * scale,
origin_y + bbox.y_min as f32 * scale,
),
(
origin_x + bbox.x_max as f32 * scale,
origin_y + bbox.y_max as f32 * scale,
),
];
let mut x_min = f32::INFINITY;
let mut x_max = f32::NEG_INFINITY;
let mut y_min = f32::INFINITY;
let mut y_max = f32::NEG_INFINITY;
for (x, y) in corners {
let transformed = run.text_matrix.transform_local_point(
crate::document::paint::text::TextRunPoint::new(x, y),
);
let transformed_x = run.x_offset + transformed.x;
let transformed_y = baseline_y + run.y_offset + transformed.y;
x_min = x_min.min(transformed_x);
x_max = x_max.max(transformed_x);
y_min = y_min.min(transformed_y);
y_max = y_max.max(transformed_y);
}
boxes.push(GlyphInkBox {
x_min,
x_max,
y_min,
y_max,
});
}
pen_x += glyph.x_advance;
}
}
boxes
}
}
impl FontSystem {
pub(crate) fn layout_to_program_baseline_adjustment(
&mut self,
font_id: Option<usize>,
style: &ComputedStyle,
_line_height: f32,
) -> LayoutLength {
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
self.layout_to_program_baseline_adjustment_for_font_size(font_id, style, used_font_size)
}
pub(in crate::text) fn shaped_runs_baseline_adjustment(
&mut self,
_runs: &[ShapedInlineRun],
style: &ComputedStyle,
line_height: f32,
) -> LayoutLength {
let font_id = self.resolve_metric_font_for_style(style);
self.layout_to_program_baseline_adjustment(font_id, style, line_height)
}
pub(crate) fn layout_to_program_baseline_adjustment_for_font_size(
&self,
font_id: Option<usize>,
_style: &ComputedStyle,
used_font_size: f32,
) -> LayoutLength {
let Some(font) = font_id.and_then(|id| self.document_fonts.get(id)) else {
return layout_pt(0.0);
};
layout_pt(layout_to_program_ascent_delta(font, used_font_size))
}
pub(crate) fn rendered_first_line_baseline_offset(
&mut self,
style: &ComputedStyle,
) -> LayoutLength {
let font_id = self.resolve_metric_font_for_style(style);
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
let ascent = font_id
.and_then(|font_id| self.document_fonts.get(font_id))
.map(|font| {
font.layout_metrics.ascender as f32 * used_font_size
/ font.units_per_em.max(1) as f32
})
.unwrap_or(style.font_size);
layout_pt(ascent)
}
pub(crate) fn x_height_for_style(&mut self, style: &ComputedStyle) -> Option<LayoutLength> {
let font_id = self.resolve_metric_font_for_style(style);
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
self.x_height_for_font(font_id?, used_font_size)
}
fn x_height_for_font(&self, font_id: usize, used_font_size: f32) -> Option<LayoutLength> {
let font = self.document_fonts.get(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 height = face
.x_height()
.map(|height| height as f32)
.or_else(|| glyph_bbox_height(&face, 'x'))?;
Some(layout_pt(height * used_font_size / units_per_em))
}
pub(crate) fn used_x_height_for_style(&mut self, style: &ComputedStyle) -> LayoutLength {
self.x_height_for_style(style)
.unwrap_or_else(|| layout_pt(style.font_size * 0.5))
}
pub(crate) fn cap_height_for_style(&mut self, style: &ComputedStyle) -> Option<LayoutLength> {
let font_id = self.resolve_metric_font_for_style(style);
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
let font = font_id.and_then(|id| self.document_fonts.get(id))?;
let units_per_em = font.units_per_em.max(1) as f32;
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
let height = face
.capital_height()
.map(|height| height as f32)
.filter(|height| *height > 0.0)
.or_else(|| glyph_bbox_height(&face, 'H'))?;
Some(layout_pt(height * used_font_size / units_per_em))
}
pub(crate) fn used_cap_height_for_style(&mut self, style: &ComputedStyle) -> LayoutLength {
self.cap_height_for_style(style)
.unwrap_or_else(|| layout_pt(style.font_size * 0.7))
}
pub(crate) fn ideographic_ink_extents_for_style(
&mut self,
style: &ComputedStyle,
) -> Option<FontRunVerticalExtents> {
let shaped = self.shape_unwrapped_line("水", style, style.line_height)?;
let run = shaped
.runs
.iter()
.find(|run| run.paints && run.text.contains('水'))?;
let font_id = run.font_id?;
let font = self.document_fonts.get(font_id)?;
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
let glyph = run
.glyphs
.iter()
.find(|glyph| glyph.source_text() == "水")?;
let bbox = face.glyph_bounding_box(ttf_parser::GlyphId(glyph.rendered.painted_id()?))?;
let scale = run.font_size / font.units_per_em.max(1) as f32;
let above = (bbox.y_max as f32 * scale).max(0.0);
let below = (-bbox.y_min as f32 * scale).max(0.0);
(above.is_finite() && below.is_finite() && above + below > 0.0)
.then_some(FontRunVerticalExtents::from_points(above, below))
}
pub(crate) fn script_vertical_align_shift(
&mut self,
style: &ComputedStyle,
baseline_shift: BaselineShift,
) -> Option<f32> {
let font_id = self.resolve_metric_font_for_style(style);
let used_font_size = font_id
.and_then(|font_id| self.font_size_adjusted_size_for_font_id(style, font_id))
.unwrap_or(style.font_size);
let font = font_id.and_then(|id| self.document_fonts.get(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;
match baseline_shift {
BaselineShift::Super => {
let metric = face.superscript_metrics()?.y_offset.unsigned_abs() as f32
* used_font_size
/ units_per_em;
Some(metric.max(style.font_size * 0.45))
}
BaselineShift::Sub => {
let metric = face.subscript_metrics()?.y_offset.unsigned_abs() as f32
* used_font_size
/ units_per_em;
Some(-metric.max(style.font_size * 0.4))
}
_ => None,
}
}
#[cfg(test)]
pub(crate) fn rendered_line_alignment_y(&self, line: &RenderedLine) -> LayoutLength {
layout_pt(line.y() + line.font_size - line.glyph_origin_adjustment().y)
}
}
fn baseline_metric_for_typography(
metric: BaselineMetric,
policy: TextLayoutPolicy,
) -> BaselineMetric {
if matches!(
(metric, policy),
(
BaselineMetric::Middle,
TextLayoutPolicy::Vertical(TextOrientation::Upright)
)
) {
BaselineMetric::Central
} else {
metric
}
}
fn layout_to_program_ascent_delta(font: &DocumentFont, used_font_size: f32) -> f32 {
let units_per_em = font.units_per_em.max(1) as f32;
let layout_ascent = font.layout_metrics.ascender as f32 * used_font_size / units_per_em;
let program_ascent = font.program_metrics.ascender as f32 * used_font_size / units_per_em;
layout_ascent - program_ascent
}
fn baseline_tag_for_metric(metric: BaselineMetric) -> Option<&'static [u8; 4]> {
match metric {
BaselineMetric::Alphabetic => Some(b"romn"),
BaselineMetric::Ideographic => Some(b"ideo"),
BaselineMetric::Central => None,
BaselineMetric::Mathematical => Some(b"math"),
BaselineMetric::Hanging => Some(b"hang"),
BaselineMetric::TextBottom | BaselineMetric::Middle | BaselineMetric::TextTop => None,
}
}
fn derive_ideographic_design_coordinates(
ideographic_over: Option<f32>,
ideographic_under: Option<f32>,
em: f32,
) -> (Option<f32>, Option<f32>) {
let central = match (ideographic_over, ideographic_under) {
(Some(over), Some(under)) => Some((over + under) / 2.0),
(Some(over), None) => Some(over - em / 2.0),
(None, Some(under)) => Some(under + em / 2.0),
(None, None) => None,
};
let ideographic_under = ideographic_under.or_else(|| ideographic_over.map(|over| over - em));
(central, ideographic_under)
}
fn css_content_baseline_offset(
content_over_coordinate: f32,
baseline_coordinate: f32,
used_font_size: f32,
units_per_em: f32,
) -> f32 {
(content_over_coordinate - baseline_coordinate) * used_font_size / units_per_em
}
fn baseline_coordinate_for_style(
font: &DocumentFont,
style: &ComputedStyle,
tag: [u8; 4],
) -> Option<crate::document::OpenTypeBaselineCoordinate> {
let axis = baseline_axis_for_policy(&font.baselines, style.text_layout_policy());
let preferred = style
.font_language_override
.opentype_tag()
.and_then(opentype_script_for_language_override)
.or_else(|| opentype_script_for_language(style.language.as_deref()));
let script = select_baseline_script(axis, preferred)?;
script
.coordinates
.iter()
.find_map(|(candidate, coordinate)| (*candidate == tag).then_some(*coordinate))
}
fn baseline_axis_for_policy(
baselines: &crate::document::OpenTypeBaselineTable,
policy: TextLayoutPolicy,
) -> &crate::document::OpenTypeBaselineAxis {
match policy {
TextLayoutPolicy::Vertical(TextOrientation::Mixed | TextOrientation::Upright) => {
&baselines.vertical
}
TextLayoutPolicy::Horizontal
| TextLayoutPolicy::Vertical(TextOrientation::Sideways)
| TextLayoutPolicy::Sideways(_) => &baselines.horizontal,
}
}
fn select_baseline_script(
axis: &crate::document::OpenTypeBaselineAxis,
preferred: Option<[u8; 4]>,
) -> Option<&crate::document::OpenTypeBaselineScript> {
preferred
.and_then(|wanted| axis.scripts.iter().find(|script| script.script == wanted))
.or_else(|| axis.scripts.iter().find(|script| script.script == *b"DFLT"))
}
fn opentype_script_for_language(language: Option<&str>) -> Option<[u8; 4]> {
let language = language?.replace('_', "-");
let mut parts = language.split('-');
let primary = parts.next()?.to_ascii_lowercase();
if let Some(script) = parts.find(|part| part.len() == 4) {
return Some(match script.to_ascii_lowercase().as_str() {
"latn" => *b"latn",
"cyrl" => *b"cyrl",
"grek" => *b"grek",
"arab" => *b"arab",
"hebr" => *b"hebr",
"deva" => *b"dev2",
"beng" => *b"bng2",
"guru" => *b"gur2",
"gujr" => *b"gjr2",
"orya" => *b"ory2",
"mlym" => *b"mlm2",
"telu" => *b"tel2",
"knda" => *b"knd2",
"tibt" => *b"tibt",
"hani" | "hans" | "hant" => *b"hani",
"hang" => *b"hang",
"kana" => *b"kana",
"thai" => *b"thai",
_ => return None,
});
}
Some(match primary.as_str() {
"ja" => *b"kana",
"ko" => *b"hang",
"zh" => *b"hani",
"ar" | "fa" | "ur" => *b"arab",
"he" | "yi" => *b"hebr",
"hi" | "mr" | "ne" => *b"dev2",
"bn" => *b"bng2",
"pa" => *b"gur2",
"gu" => *b"gjr2",
"or" => *b"ory2",
"ml" => *b"mlm2",
"te" => *b"tel2",
"kn" => *b"knd2",
"bo" | "dz" => *b"tibt",
"th" => *b"thai",
"ru" | "uk" | "bg" | "sr" => *b"cyrl",
"el" => *b"grek",
_ => *b"latn",
})
}
fn opentype_script_for_language_override(tag: [u8; 4]) -> Option<[u8; 4]> {
Some(match &tag {
b"JAN " | b"JPN " => *b"kana",
b"KOR " => *b"hang",
b"ZHS " | b"ZHT " | b"CHN " => *b"hani",
b"ARA " | b"FAR " | b"URD " => *b"arab",
b"IWR " | b"YID " => *b"hebr",
b"HIN " | b"MAR " | b"NEP " => *b"dev2",
b"RUS " | b"SRB " | b"UKR " => *b"cyrl",
b"ELL " => *b"grek",
b"THA " => *b"thai",
b"DEU " | b"ENG " | b"ENU " | b"ESP " | b"FRA " | b"ITA " | b"NLD " | b"NOR " | b"PTB "
| b"PTG " | b"SVE " | b"TRK " => *b"latn",
_ => return None,
})
}
fn synthesized_baseline_offset(
metric: BaselineMetric,
alphabetic: f32,
em: f32,
x_height: f32,
) -> f32 {
match metric {
BaselineMetric::TextTop => 0.0,
BaselineMetric::TextBottom => em,
BaselineMetric::Alphabetic => alphabetic,
BaselineMetric::Middle => alphabetic - x_height / 2.0,
BaselineMetric::Central => em / 2.0,
BaselineMetric::Hanging => alphabetic - em * 0.6,
BaselineMetric::Ideographic => em,
BaselineMetric::Mathematical => alphabetic - x_height / 2.0,
}
}
#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod baseline_tests {
use super::*;
use crate::css::{ContentLanguage, FontLanguageOverride, SidewaysOrientation};
#[test]
fn baseline_synthesis_uses_font_metric_relationships() {
assert_eq!(
synthesized_baseline_offset(BaselineMetric::Alphabetic, 8.0, 10.0, 5.0),
8.0
);
assert_eq!(
synthesized_baseline_offset(BaselineMetric::Middle, 8.0, 10.0, 5.0),
5.5
);
assert_eq!(
synthesized_baseline_offset(BaselineMetric::Central, 8.0, 10.0, 5.0),
5.0
);
assert_eq!(
synthesized_baseline_offset(BaselineMetric::TextTop, 8.0, 10.0, 5.0),
0.0
);
assert_eq!(
synthesized_baseline_offset(BaselineMetric::TextBottom, 8.0, 10.0, 5.0),
10.0
);
}
#[test]
fn declared_script_subtag_precedes_primary_language_fallback() {
assert_eq!(
opentype_script_for_language(Some("zh-Hant-TW")),
Some(*b"hani")
);
assert_eq!(
opentype_script_for_language(Some("sr-Cyrl")),
Some(*b"cyrl")
);
assert_eq!(opentype_script_for_language(Some("ja")), Some(*b"kana"));
assert_eq!(opentype_script_for_language(Some("en")), Some(*b"latn"));
assert_eq!(
opentype_script_for_language(Some("bn-Beng")),
Some(*b"bng2")
);
}
#[test]
fn language_override_supplies_a_script_hint_without_a_document_language() {
assert_eq!(
opentype_script_for_language_override(*b"JAN "),
Some(*b"kana")
);
assert_eq!(
opentype_script_for_language_override(*b"ZHS "),
Some(*b"hani")
);
assert_eq!(
opentype_script_for_language_override(*b"DEU "),
Some(*b"latn")
);
assert_eq!(opentype_script_for_language_override(*b"XXX "), None);
}
#[test]
fn language_override_replaces_the_content_language_script_hint() {
let mut style = ComputedStyle::initial();
style.language = ContentLanguage::from_html_attribute("ja");
style.font_language_override = FontLanguageOverride::OpenType(*b"RUS ");
let axis = crate::document::OpenTypeBaselineAxis {
scripts: vec![
crate::document::OpenTypeBaselineScript {
script: *b"kana",
default_baseline: None,
coordinates: Vec::new(),
},
crate::document::OpenTypeBaselineScript {
script: *b"cyrl",
default_baseline: None,
coordinates: Vec::new(),
},
],
};
let preferred = style
.font_language_override
.opentype_tag()
.and_then(opentype_script_for_language_override)
.or_else(|| opentype_script_for_language(style.language.as_deref()));
assert_eq!(
select_baseline_script(&axis, preferred).unwrap().script,
*b"cyrl"
);
style.font_language_override = FontLanguageOverride::OpenType(*b"XXX ");
let preferred = style
.font_language_override
.opentype_tag()
.and_then(opentype_script_for_language_override)
.or_else(|| opentype_script_for_language(style.language.as_deref()));
assert_eq!(
select_baseline_script(&axis, preferred).unwrap().script,
*b"kana"
);
}
#[test]
fn css_metrics_map_to_their_opentype_base_tags() {
assert_eq!(
baseline_tag_for_metric(BaselineMetric::Alphabetic),
Some(b"romn")
);
assert_eq!(
baseline_tag_for_metric(BaselineMetric::Ideographic),
Some(b"ideo")
);
assert_eq!(baseline_tag_for_metric(BaselineMetric::Central), None);
assert_eq!(
baseline_tag_for_metric(BaselineMetric::Mathematical),
Some(b"math")
);
assert_eq!(
baseline_tag_for_metric(BaselineMetric::Hanging),
Some(b"hang")
);
}
#[test]
fn central_and_ideographic_fallbacks_follow_appendix_a_relationships() {
assert_eq!(
derive_ideographic_design_coordinates(Some(12.0), None, 10.0),
(Some(7.0), Some(2.0))
);
assert_eq!(
derive_ideographic_design_coordinates(Some(12.0), Some(2.0), 10.0),
(Some(7.0), Some(2.0))
);
assert_eq!(
synthesized_baseline_offset(BaselineMetric::Hanging, 8.0, 10.0, 5.0),
2.0
);
}
#[test]
fn css_baseline_coordinates_remain_anchored_at_the_layout_ascent() {
assert_eq!(
css_content_baseline_offset(800.0, 50.0, 240.0, 1000.0),
180.0
);
assert_eq!(
css_content_baseline_offset(800.0, 650.0, 240.0, 1000.0),
36.0
);
}
#[test]
fn baseline_axis_follows_the_css_typographic_mode() {
let table = crate::document::OpenTypeBaselineTable {
horizontal: crate::document::OpenTypeBaselineAxis {
scripts: vec![crate::document::OpenTypeBaselineScript {
script: *b"hori",
default_baseline: None,
coordinates: Vec::new(),
}],
},
vertical: crate::document::OpenTypeBaselineAxis {
scripts: vec![crate::document::OpenTypeBaselineScript {
script: *b"vert",
default_baseline: None,
coordinates: Vec::new(),
}],
},
};
assert_eq!(
baseline_axis_for_policy(&table, TextLayoutPolicy::Horizontal).scripts[0].script,
*b"hori"
);
assert_eq!(
baseline_axis_for_policy(
&table,
TextLayoutPolicy::Sideways(SidewaysOrientation::Right)
)
.scripts[0]
.script,
*b"hori"
);
assert_eq!(
baseline_axis_for_policy(
&table,
TextLayoutPolicy::Vertical(TextOrientation::Sideways)
)
.scripts[0]
.script,
*b"hori"
);
assert_eq!(
baseline_axis_for_policy(&table, TextLayoutPolicy::Vertical(TextOrientation::Mixed))
.scripts[0]
.script,
*b"vert"
);
}
#[test]
fn middle_uses_central_only_for_upright_typography() {
assert_eq!(
baseline_metric_for_typography(BaselineMetric::Middle, TextLayoutPolicy::Horizontal),
BaselineMetric::Middle
);
assert_eq!(
baseline_metric_for_typography(
BaselineMetric::Middle,
TextLayoutPolicy::Vertical(TextOrientation::Mixed)
),
BaselineMetric::Middle
);
assert_eq!(
baseline_metric_for_typography(
BaselineMetric::Middle,
TextLayoutPolicy::Vertical(TextOrientation::Upright)
),
BaselineMetric::Central
);
}
#[test]
fn script_selection_uses_only_the_matching_or_default_record() {
let dflt = crate::document::OpenTypeBaselineScript {
script: *b"DFLT",
default_baseline: None,
coordinates: Vec::new(),
};
let latn = crate::document::OpenTypeBaselineScript {
script: *b"latn",
default_baseline: None,
coordinates: Vec::new(),
};
let axis = crate::document::OpenTypeBaselineAxis {
scripts: vec![dflt.clone(), latn.clone()],
};
assert_eq!(select_baseline_script(&axis, Some(*b"latn")), Some(&latn));
assert_eq!(select_baseline_script(&axis, Some(*b"hani")), Some(&dflt));
let no_default = crate::document::OpenTypeBaselineAxis {
scripts: vec![latn],
};
assert_eq!(select_baseline_script(&no_default, Some(*b"hani")), None);
}
}
pub(in crate::text) fn font_feature_family(font_family: &FontFamily) -> Option<String> {
match font_family {
FontFamily::SansSerif => Some("sans-serif".to_string()),
FontFamily::Serif => Some("serif".to_string()),
FontFamily::Monospace => Some("monospace".to_string()),
FontFamily::SystemUi => Some("system-ui".to_string()),
FontFamily::UiSerif => Some("ui-serif".to_string()),
FontFamily::UiSansSerif => Some("ui-sans-serif".to_string()),
FontFamily::UiMonospace => Some("ui-monospace".to_string()),
FontFamily::UiRounded => Some("ui-rounded".to_string()),
FontFamily::List(families) => families.first().and_then(font_feature_family),
FontFamily::Named(name) => Some(name.as_str().to_owned()),
}
}
pub(in crate::text) fn style_for_text_range<'a, T>(
ranges: &[(Range<usize>, &'a T)],
run_range: Range<usize>,
) -> Option<&'a T> {
ranges
.iter()
.find(|(range, _)| {
range.start <= run_range.start
&& (run_range.start < range.end || run_range.start == run_range.end)
})
.map(|(_, style)| *style)
}
pub(in crate::text) fn font_size_adjust_metric_ratio(
font: &DocumentFont,
metric: FontSizeAdjustMetric,
) -> Option<f32> {
let units_per_em = font.units_per_em.max(1) as f32;
let face = ttf_parser::Face::parse(&font.data, font.face_index).ok()?;
let value = match metric {
FontSizeAdjustMetric::ExHeight => face
.x_height()
.map(|height| height as f32)
.or_else(|| glyph_bbox_height(&face, 'x'))?,
FontSizeAdjustMetric::CapHeight => face
.capital_height()
.map(|height| height as f32)
.filter(|height| *height > 0.0)
.or_else(|| (font.cap_height > 0).then_some(font.cap_height as f32))
.or_else(|| glyph_bbox_height(&face, 'H'))?,
FontSizeAdjustMetric::ChWidth => glyph_advance_width(&face, '0')?,
FontSizeAdjustMetric::IcWidth => glyph_advance_width(&face, '水').unwrap_or(units_per_em),
FontSizeAdjustMetric::IcHeight => face
.glyph_index('水')
.and_then(|glyph| face.glyph_ver_advance(glyph))
.map(|advance| advance as f32)
.or_else(|| glyph_bbox_height(&face, '水'))
.unwrap_or(units_per_em),
};
(value.is_finite() && value > 0.0).then_some(value / units_per_em)
}
pub(in crate::text) fn glyph_advance_width(
face: &ttf_parser::Face<'_>,
character: char,
) -> Option<f32> {
face.glyph_index(character)
.and_then(|glyph| face.glyph_hor_advance(glyph))
.map(|advance| advance as f32)
.filter(|advance| *advance > 0.0)
}
pub(in crate::text) fn glyph_bbox_height(
face: &ttf_parser::Face<'_>,
character: char,
) -> Option<f32> {
face.glyph_index(character)
.and_then(|glyph| face.glyph_bounding_box(glyph))
.map(|bbox| (bbox.y_max - bbox.y_min).abs() as f32)
.filter(|height| *height > 0.0)
}