mod autospace;
mod bidi;
mod breaking;
mod combine;
mod decoration;
mod ellipsis;
mod emphasis;
pub(crate) mod features;
mod hanging;
mod hyphenation;
mod initial_letter;
mod leaders;
mod line_boxes;
mod paragraph;
mod ruby;
mod shaping;
mod skip_ink;
mod spacing;
mod spacing_trim;
mod tabs;
mod tailoring;
#[cfg(test)]
mod tests;
mod whitespace;
use crate::document::Paragraph;
use crate::fonts::{
FontBook, FontError, FontId, FontSelection, FontSynthesis, FontVariation, GlyphMatch,
};
use crate::resources::ResourceResolver;
use crate::style::{Style, TextAlign, WhiteSpace};
use bidi::resolve as paragraph_bidi;
use rustybuzz::Face;
use serde::Serialize;
use shaping::MissingGlyphs;
use std::ops::Range;
use std::{
cell::{Cell, RefCell},
collections::HashMap,
sync::Arc,
};
use unicode_linebreak::BreakOpportunity;
use unicode_script::{Script, UnicodeScript};
use unicode_segmentation::UnicodeSegmentation;
pub(crate) use initial_letter::{
Surrounding as LetterSurrounding, font_scale as initial_letter_scale,
};
pub(crate) use line_boxes::{AtomicFragment, AtomicMetrics, BoxFragment, BoxMetrics, LinkFragment};
const SYNTHETIC_BOLD_OFFSET_EM: f32 = 0.02;
pub(crate) const MAX_GLYPH_MATCH_WORK: usize = 100_000_000;
use line_boxes::{Piece, Slot};
pub(crate) use paragraph::{ParagraphClamp, PreparedParagraph, TextPosition, trim_line};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub(crate) enum GlyphOrientation {
#[default]
Horizontal,
UprightRl,
UprightLr,
UprightSidewaysLr,
ReflectedSideways,
}
impl GlyphOrientation {
pub(crate) fn matrix(self) -> [f32; 4] {
match self {
Self::Horizontal => [1.0, 0.0, 0.0, 1.0],
Self::UprightRl => [0.0, -1.0, 1.0, 0.0],
Self::UprightLr => [0.0, 1.0, 1.0, 0.0],
Self::UprightSidewaysLr => [0.0, 1.0, -1.0, 0.0],
Self::ReflectedSideways => [1.0, 0.0, 0.0, -1.0],
}
}
pub(crate) fn font_vector(self, [x, y]: [f32; 2]) -> [f32; 2] {
let [a, b, c, d] = self.matrix();
[a * x + b * y, c * x + d * y]
}
}
#[derive(Clone, Debug, Serialize)]
pub struct GlyphRun {
pub text: String,
#[serde(skip)]
pub source_range: Range<usize>,
#[serde(skip)]
pub source_span: usize,
pub rtl: bool,
pub(crate) orientation: GlyphOrientation,
pub(crate) inline_scale: f32,
pub(crate) combined: bool,
#[serde(skip)]
bidi_scoped: bool,
#[serde(skip)]
leader: bool,
pub x: f32,
pub width: f32,
pub baseline_shift: f32,
pub font_size: f32,
#[serde(skip)]
metric_size: f32,
pub font_id: FontId,
pub synthesis: FontSynthesis,
pub oblique_skew: f32,
pub variations: Vec<FontVariation>,
#[serde(skip)]
pub palette: Option<Arc<[crate::graphics::Color]>>,
pub color: crate::graphics::Color,
pub(crate) stroke: Option<TextStroke>,
pub(crate) marks: Vec<GlyphRun>,
pub(crate) mark_shapes: Vec<MarkShape>,
#[serde(skip)]
pub(crate) shadows: Arc<[crate::style::TextShadow]>,
#[serde(skip)]
pub white_space: WhiteSpace,
#[serde(skip)]
leading: Option<f32>,
#[serde(skip)]
ink_bounds: Option<[f32; 2]>,
#[serde(skip)]
ink_sides: [f32; 2],
#[serde(skip)]
metrics: crate::fonts::FaceMetrics,
#[serde(skip)]
glyphs: Vec<PositionedGlyph>,
#[serde(skip)]
removed_spaces: Vec<usize>,
}
#[derive(Clone, Debug, Serialize)]
pub(crate) struct MarkShape {
pub contours: Vec<crate::graphics::Contour>,
pub color: crate::graphics::Color,
}
impl MarkShape {
pub(crate) fn shape(&self) -> crate::graphics::Shape {
crate::graphics::Shape::Path {
contours: self.contours.clone(),
even_odd: true,
clip: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub(crate) struct TextStroke {
pub width: f32,
pub color: crate::graphics::Color,
}
impl TextStroke {
pub(crate) fn of(style: &Style) -> Option<Self> {
if style.text_paint.stroke_width > 0.0 {
return Some(Self {
width: style.text_paint.stroke_width,
color: style.text_paint.stroke_color.used,
});
}
let color = style.svg.stroke_color.used;
(color.alpha > 0.0 && style.svg.text_stroke_width > 0.0).then_some(Self {
width: style.svg.text_stroke_width,
color,
})
}
}
fn script_ranges(text: &str, range: Range<usize>) -> Vec<Range<usize>> {
let mut ranges = Vec::new();
let mut start = range.start;
let mut current = Script::Common;
for (offset, ch) in text[range.clone()].char_indices() {
let script = ch.script();
if matches!(script, Script::Common | Script::Inherited) {
continue;
}
if current != Script::Common && script != current {
ranges.push(start..range.start + offset);
start = range.start + offset;
}
current = script;
}
ranges.push(start..range.end);
ranges
}
fn shape_line(
context: &TextContext<'_>,
text: &str,
bidi: &bidi::ResolvedParagraph<'_>,
range: Range<usize>,
context_range: Range<usize>,
paragraph: &Paragraph,
has_rtl: bool,
) -> Result<Vec<Piece>, TextError> {
if range.is_empty() {
return Ok(Vec::new());
}
let start = range.start;
let text = &text[range.clone()];
let combined = combine::groups(paragraph);
let emitted = RefCell::new(std::collections::HashSet::new());
let shape_range = |range: Range<usize>, rtl: bool| -> Result<Vec<Piece>, TextError> {
let absolute = (start + range.start)..(start + range.end);
let first = paragraph
.spans()
.partition_point(|s| s.range.end <= absolute.start);
let mut pieces: Vec<_> = paragraph.spans()[first..]
.iter()
.enumerate()
.take_while(|(_, s)| s.range.start < absolute.end)
.map(|(index, s)| {
(
s.range.start.max(absolute.start)..s.range.end.min(absolute.end),
s,
first + index,
)
})
.collect();
if rtl {
pieces.reverse();
}
let mut result = Vec::new();
for (r, piece, span) in pieces {
if let Some(index) = piece.atomic {
result.push(Piece::Atomic {
index,
offset: piece.range.start,
owner: piece.inline_box,
rtl,
});
continue;
}
let mut boundaries = vec![r.start, r.end];
for group in &combined {
boundaries.extend(
[group.range.start, group.range.end]
.into_iter()
.filter(|offset| r.start < *offset && *offset < r.end),
);
}
boundaries.sort_unstable();
boundaries.dedup();
let mut ranges: Vec<_> = boundaries.windows(2).map(|b| b[0]..b[1]).collect();
if rtl {
ranges.reverse();
}
for r in ranges {
if let Some(group) = combined.iter().find(|group| group.range.contains(&r.start)) {
if emitted.borrow_mut().insert(group.range.start) {
result.extend(
combine::shape(context, paragraph, group)?
.into_iter()
.map(|run| Piece::Text(Box::new(run))),
);
}
continue;
}
let (style, source) = (&piece.style, piece.range.clone());
let shaping_context =
source.start.max(context_range.start)..source.end.min(context_range.end);
let mut runs = tabs::shape(
context,
¶graph.text()[shaping_context.clone()],
(r.start - shaping_context.start)..(r.end - shaping_context.start),
rtl,
style,
)?;
for run in &mut runs {
run.bidi_scoped = bidi.has_controls();
run.source_range.start += shaping_context.start;
run.source_range.end += shaping_context.start;
run.source_span = span;
run.leader = piece.leader;
}
result.extend(runs.into_iter().map(|run| Piece::Text(Box::new(run))));
}
}
Ok(result)
};
if !has_rtl {
let mut result = Vec::new();
for range in script_ranges(text, 0..text.len()) {
result.extend(shape_range(range, false)?);
}
return Ok(result);
}
let mut result = Vec::new();
for (run, rtl) in bidi.visual_runs(range) {
let mut scripts = script_ranges(text, run.start - start..run.end - start);
if rtl {
scripts.reverse();
}
for script in scripts {
result.extend(shape_range(script, rtl)?);
}
}
Ok(result)
}
#[derive(Clone)]
pub(crate) struct Line {
pub phantom: bool,
pub wrapped: bool,
pub indent: f32,
pub text: String,
pub range: Range<usize>,
pub width: f32,
pub wrap_width: f32,
pub runs: Vec<GlyphRun>,
pub boxes: Vec<BoxFragment>,
pub links: Vec<LinkFragment>,
pub atomics: Vec<AtomicFragment>,
pub annotations: Vec<AtomicFragment>,
ruby: ruby::RubyLine,
pub height: f32,
pub ascent: f32,
pub ink_bounds: Option<[f32; 2]>,
pub align: TextAlign,
pub decorations: Vec<crate::graphics::DecorationPaint>,
order: Vec<Slot>,
present: Vec<usize>,
ellipsis: Option<usize>,
hangable: hanging::Hangable,
hanging: [f32; 2],
pub first_line_styles: bool,
}
pub(crate) use crate::style::IntrinsicWidths;
#[derive(Clone, Debug)]
pub struct PositionedGlyph {
pub id: u32,
pub text_range: Range<usize>,
pub x_advance: f32,
pub x_spacing: f32,
pub x_offset: f32,
pub y_offset: f32,
pub y_advance: f32,
}
impl PositionedGlyph {
pub fn advance(&self, font_size: f32) -> f32 {
self.x_advance * font_size + self.x_spacing
}
pub fn normalized_advance(&self, font_size: f32) -> Option<f32> {
let advance = self.x_advance + self.x_spacing / font_size;
advance.is_finite().then_some(advance)
}
}
impl GlyphRun {
pub fn has_bidi_scopes(&self) -> bool {
self.bidi_scoped
}
pub(crate) fn opaque(mut self) -> Self {
const OPAQUE: f32 = 1.0;
self.color.alpha = OPAQUE;
if let Some(stroke) = &mut self.stroke {
stroke.color.alpha = OPAQUE;
}
if let Some(palette) = &self.palette {
self.palette = Some(
palette
.iter()
.map(|color| crate::graphics::Color {
alpha: OPAQUE,
..*color
})
.collect(),
);
}
for shape in &mut self.mark_shapes {
shape.color.alpha = OPAQUE;
}
self.marks = self.marks.into_iter().map(Self::opaque).collect();
self
}
pub(crate) fn copies(&self, [x, y]: [f32; 2]) -> Vec<[f32; 2]> {
let pen = [x + self.x, y + self.baseline_shift];
let mut copies = vec![pen];
if self.synthesis.bold {
let [a, b, _, _] = self.orientation.matrix();
let bold = self.font_size * SYNTHETIC_BOLD_OFFSET_EM * self.inline_scale;
copies.push([pen[0] + a * bold, pen[1] + b * bold]);
}
copies
}
pub(crate) fn shear(&self, baseline: f32) -> Option<[f32; 6]> {
self.synthesis.oblique.then(|| {
[
1.0,
0.0,
-self.oblique_skew,
1.0,
self.oblique_skew * baseline,
0.0,
]
})
}
pub(crate) fn glyph_origins(&self, [x, y]: [f32; 2]) -> Vec<(&PositionedGlyph, [f32; 2])> {
let mut pen = x;
self.glyphs
.iter()
.map(|glyph| {
let origin = [
pen + glyph.x_offset * self.font_size,
y + glyph.y_offset * self.font_size,
];
pen += glyph.advance(self.font_size);
(glyph, origin)
})
.collect()
}
pub(crate) fn ink_rect(&self, [x, y]: [f32; 2]) -> Option<crate::graphics::Rect> {
let glyphs = self.synthesized_ink(
[
self.x + (self.ink_sides[0] * self.font_size).min(0.0),
self.x + self.width + (self.ink_sides[1] * self.font_size).max(0.0),
],
self.ink_bounds?,
[x, y],
);
Some(
self.marks
.iter()
.filter_map(|mark| mark.ink_rect([x, y]))
.chain(self.mark_shapes.iter().filter_map(|mark| {
crate::paint::shape_bounds(&mark.shape()).map(|rect| crate::graphics::Rect {
x: rect.x + x,
y: rect.y + y,
..rect
})
}))
.fold(glyphs, crate::graphics::Rect::union),
)
}
fn synthesized_ink(
&self,
[left, right]: [f32; 2],
[top, bottom]: [f32; 2],
[x, y]: [f32; 2],
) -> crate::graphics::Rect {
let shear = if self.synthesis.oblique {
self.oblique_skew.abs() * top.abs().max(bottom.abs())
} else {
0.0
};
let bold = if self.synthesis.bold {
self.font_size * SYNTHETIC_BOLD_OFFSET_EM
} else {
0.0
};
let stroke = self.stroke.map_or(0.0, |stroke| stroke.width / 2.0);
let left = x + left - shear - stroke;
let right = x + right + shear + bold + stroke;
let top = y + self.baseline_shift + top - stroke;
let bottom = y + self.baseline_shift + bottom + stroke;
crate::graphics::Rect {
x: left,
y: top,
width: right - left,
height: bottom - top,
}
}
pub(crate) fn with_marks(&self) -> impl Iterator<Item = &Self> {
std::iter::once(self).chain(&self.marks)
}
pub(crate) fn set_math_glyphs(
&mut self,
mut glyphs: Vec<PositionedGlyph>,
width: f32,
face: &ttf_parser::Face<'_>,
) {
for glyph in &mut glyphs {
glyph.text_range = 0..self.text.len();
}
self.glyphs = glyphs;
self.width = width;
let bounds = self
.outline_rects(face)
.reduce(crate::graphics::Rect::union);
self.ink_bounds = bounds.map(|rect| [rect.y, rect.y + rect.height]);
self.ink_sides = bounds
.filter(|_| self.font_size > 0.0)
.map_or([0.0; 2], |rect| {
[
(rect.x - self.x) / self.font_size,
(rect.x + rect.width - self.x - self.width) / self.font_size,
]
});
}
fn outline_rects<'r>(
&'r self,
face: &'r ttf_parser::Face<'_>,
) -> impl Iterator<Item = crate::graphics::Rect> + 'r {
let scale = self.font_size / f32::from(face.units_per_em());
self.glyph_origins([self.x, 0.0])
.into_iter()
.filter_map(move |(glyph, [x, y])| {
let outline = u16::try_from(glyph.id)
.ok()
.and_then(|id| face.glyph_bounding_box(ttf_parser::GlyphId(id)))?;
Some(crate::graphics::Rect {
x: x + f32::from(outline.x_min) * scale,
y: y - f32::from(outline.y_max) * scale,
width: (f32::from(outline.x_max) - f32::from(outline.x_min)) * scale,
height: (f32::from(outline.y_max) - f32::from(outline.y_min)) * scale,
})
})
}
pub fn glyphs(&self) -> &[PositionedGlyph] {
&self.glyphs
}
pub(crate) fn advance(&self, index: usize) -> f32 {
let glyph = &self.glyphs[index];
glyph.advance(self.font_size)
}
fn x_at(&self, offset: usize) -> f32 {
let before: f32 = (0..self.glyphs.len())
.filter(|&index| self.source_range.start + self.glyphs[index].text_range.start < offset)
.map(|index| self.advance(index))
.sum();
if self.rtl {
self.width - before
} else {
before
}
}
}
impl Line {
pub(crate) fn refresh_ink_bounds(&mut self) {
self.ink_bounds = self
.runs
.iter()
.flat_map(GlyphRun::with_marks)
.filter_map(|run| {
run.ink_bounds
.map(|ink| ink.map(|y| y + run.baseline_shift))
})
.reduce(|[a, b], [c, d]| [a.min(c), b.max(d)]);
}
pub(crate) fn alignment_offset(&self, available: f32, rtl: bool) -> f32 {
let start = if rtl { 0.0 } else { self.indent };
let [left, right] = self.hanging;
start
+ self
.align
.offset(available - self.indent, self.width - left - right, rtl)
- left
}
pub(crate) fn x_at(&self, paragraph: &Paragraph, offset: usize, rtl: bool) -> f32 {
let offset = if self.first_line_styles {
paragraph
.first_line_offsets()
.to_copy(offset)
.expect("every source character boundary has a first-line offset")
} else {
offset
};
if let Some(run) = self
.runs
.iter()
.find(|run| run.source_range.contains(&offset))
{
return run.x + run.x_at(offset);
}
if let Some(atomic) = self.atomics.iter().find(|atomic| atomic.offset == offset) {
return if rtl {
atomic.x + atomic.width
} else {
atomic.x
};
}
let at_start = self
.runs
.iter()
.map(|run| run.source_range.start)
.chain(self.atomics.iter().map(|atomic| atomic.offset))
.min()
.is_none_or(|start| offset <= start);
if at_start == rtl { self.width } else { 0.0 }
}
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum TextError {
#[error("{0}")]
Font(#[from] FontError),
#[error("computed text style has no font candidates")]
NoFontCandidates,
#[error("missing glyph in font: {text:?}")]
MissingGlyph { text: String },
#[error("glyph matching work exceeds {limit}")]
GlyphMatchingLimit { limit: usize },
#[error("bidirectional control expansion work exceeds {limit} bytes")]
BidiExpansionLimit { limit: usize },
#[error("non-finite text geometry")]
NonFiniteGeometry,
#[error("small-caps casing exceeds the inline text expansion limit")]
CaseExpansionLimit,
#[error("leader expansion exceeds {limit} text bytes")]
LeaderExpansionLimit { limit: usize },
#[error(
"paragraph has {expected} atomic inlines and ruby annotations but {actual} were measured"
)]
AtomicMetrics { expected: usize, actual: usize },
#[error("text continuation belongs to another paragraph")]
ParagraphMismatch,
#[error("atomic inline {index} on a ::first-line inherits first-line styles into its contents")]
FirstLineAtomic { index: usize },
}
fn shaping_language(face: &Face<'_>, style: &Style) -> Option<rustybuzz::Language> {
const PRIVATE_SYSTEM_SUBTAG: &str = "x-hbot";
const PADDING: char = ' ';
let content = || style.language.parse().ok();
let Some(tag) = style
.font_features
.language_override
.as_deref()
.and_then(crate::style::language_system_tag)
else {
return content();
};
let name = std::str::from_utf8(&tag)
.expect("language system tags are ASCII")
.trim_end_matches(PADDING);
if !shaping::has_language_system(face, tag)
|| !name
.bytes()
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit())
{
return content();
}
let source = if style.language.is_empty() {
format!("{PRIVATE_SYSTEM_SUBTAG}{name}")
} else {
format!("{}-{PRIVATE_SYSTEM_SUBTAG}{name}", style.language)
};
source.parse().ok()
}
fn implies_presentation(style: &Style, text: &str) -> bool {
let mut chars = text.chars().peekable();
while let Some(ch) = chars.next() {
if shaping::implied_presentation(style.font_features.emoji, ch, chars.peek().copied())
.is_some()
{
return true;
}
}
false
}
struct ShapingInstance<'a> {
face: Face<'a>,
variations: Arc<[FontVariation]>,
synthesis: FontSynthesis,
oblique_skew: f32,
scale: f32,
}
impl ShapingInstance<'_> {
fn used_size(&self, style: &Style) -> f32 {
style.size * self.scale
}
}
type FaceKey = (
FontId,
FontSelection,
crate::fonts::FontAdjustments,
Option<u32>,
);
type FaceCache<'a> = HashMap<FaceKey, Option<Arc<ShapingInstance<'a>>>>;
pub(crate) struct TextContext<'a> {
fonts: &'a FontBook,
resolver: &'a dyn ResourceResolver,
faces: RefCell<FaceCache<'a>>,
optical_axes: RefCell<HashMap<FontId, bool>>,
matching_work: Cell<Option<usize>>,
bidi_work: Cell<Option<usize>>,
}
struct IntrinsicSource<'p, 'c, 'f> {
paragraph: &'p Paragraph,
bidi: bidi::ResolvedParagraph<'p>,
automatic: Vec<usize>,
opportunities: Vec<(usize, BreakOpportunity)>,
ruby: Option<ruby::RubyLayout<'p, 'c, 'f>>,
}
impl<'p, 'c, 'f> IntrinsicSource<'p, 'c, 'f> {
fn new(
context: &'c TextContext<'f>,
paragraph: &'p Paragraph,
source: &'p Paragraph,
) -> Result<Self, TextError> {
let automatic = hyphenation::automatic(paragraph);
Ok(Self {
paragraph,
bidi: paragraph_bidi(paragraph, &context.bidi_work)?,
opportunities: whitespace::opportunities(paragraph, &automatic),
automatic,
ruby: ruby::RubyLayout::of(context, paragraph, source),
})
}
}
impl<'a> TextContext<'a> {
pub(crate) fn fonts(&self) -> &'a FontBook {
self.fonts
}
pub(crate) fn resolver(&self) -> &'a dyn ResourceResolver {
self.resolver
}
pub(crate) fn intrinsic_widths(
&self,
paragraph: &Paragraph,
atomics: &[IntrinsicWidths],
) -> Result<IntrinsicWidths, TextError> {
let expected = paragraph.measured_blocks().len();
if atomics.len() != expected {
return Err(TextError::AtomicMetrics {
expected,
actual: atomics.len(),
});
}
let text = paragraph.text();
let base = IntrinsicSource::new(self, paragraph, paragraph)?;
let first_line = paragraph
.first_line()
.map(|variant| IntrinsicSource::new(self, variant, paragraph))
.transpose()?;
let opportunities = &base.opportunities;
let base_autospace = autospace::Autospace::of(self, paragraph);
let first_line_autospace = first_line
.as_ref()
.and_then(|source| autospace::Autospace::of(self, source.paragraph));
let minimum: Vec<f32> = atomics.iter().map(|widths| widths.min).collect();
let maximum: Vec<f32> = atomics.iter().map(|widths| widths.max).collect();
let measure = |range: Range<usize>, atomic_widths: &[f32], min_content: bool| {
let (source, autospace, range) = match first_line.as_ref().filter(|_| range.start == 0)
{
Some(source) => (
source,
first_line_autospace.as_ref(),
0..paragraph.first_line_offsets().to_copy_after(range.end),
),
None => (&base, base_autospace.as_ref(), range),
};
let spacers = line_boxes::Spacers {
autospace,
ruby: source.ruby.as_ref(),
};
let paragraph = source.paragraph;
let text = paragraph.text();
let context_range = breaking::context_range(&source.opportunities, range.clone());
let visible = whitespace::visible(paragraph, range.clone());
let mut pieces = shape_line(
self,
text,
&source.bidi,
visible.clone(),
context_range,
paragraph,
source.bidi.has_rtl(),
)?;
if visible.end == range.end {
hyphenation::decorate(
self,
paragraph,
&source.automatic,
&source.bidi,
range.end,
&mut pieces,
)?;
}
let indent = paragraph::indent_at(base.paragraph, opportunities, range.start, 0.0);
let mut arranged = line_boxes::arrange(
paragraph,
pieces,
range.clone(),
atomic_widths,
0.0,
spacers,
)?;
let end_trim = spacing_trim::trim(
self,
paragraph,
&mut arranged,
paragraph::starts_block_line(base.paragraph, opportunities, range.start),
)?;
if min_content && let Some(end_trim) = end_trim {
end_trim.apply(&mut arranged);
}
tabs::resolve(self, paragraph, &mut arranged, indent)?;
let hangable = hanging::hangable(
paragraph,
range.clone(),
visible.clone(),
&arranged.runs,
&arranged.order,
);
let width = if min_content {
whitespace::wrapping_width(
paragraph,
visible.end,
&arranged.runs,
&arranged.order,
arranged.width,
)
} else {
arranged.width
} + indent
- hangable.intrinsic(min_content);
if !width.is_finite() {
return Err(TextError::NonFiniteGeometry);
}
Ok(width)
};
if text.is_empty() {
let width = measure(0..0, &maximum, false)?;
return Ok(IntrinsicWidths {
min: width,
max: width,
});
}
let mut widths = IntrinsicWidths::default();
let mut line_start = 0;
let mut word_start = 0;
let emergency = whitespace::emergency_opportunities(paragraph, true);
let mut emergency = emergency.into_iter().peekable();
for (end, opportunity) in opportunities.iter().copied() {
while emergency.peek().is_some_and(|&offset| offset < end) {
let offset = emergency.next().unwrap();
widths.min = widths.min.max(measure(word_start..offset, &minimum, true)?);
word_start = offset;
}
if emergency.peek() == Some(&end) {
emergency.next();
}
widths.min = widths.min.max(measure(word_start..end, &minimum, true)?);
word_start = end;
if opportunity == BreakOpportunity::Mandatory {
widths.max = widths.max.max(measure(line_start..end, &maximum, false)?);
line_start = end;
}
}
widths.max = widths.max.max(widths.min);
Ok(widths)
}
pub(crate) fn new(fonts: &'a FontBook, resolver: &'a dyn ResourceResolver) -> Self {
Self {
fonts,
resolver,
faces: RefCell::new(HashMap::new()),
optical_axes: RefCell::new(HashMap::new()),
matching_work: Cell::new(Some(MAX_GLYPH_MATCH_WORK)),
bidi_work: Cell::new(Some(crate::document::MAX_INLINE_TEXT_BYTES)),
}
}
fn face(
&self,
id: FontId,
style: &Style,
) -> Result<Option<Arc<ShapingInstance<'a>>>, TextError> {
let selection = style.font_selection();
let adjustments = &style.font_adjustments;
let sized = adjustments.size_adjust.is_some()
|| (adjustments.optical_sizing && self.has_optical_axis(id)?);
let key = (
id,
selection,
adjustments.clone(),
sized.then(|| style.size.to_bits()),
);
if let Some(face) = self.faces.borrow().get(&key) {
return Ok(face.clone());
}
let face = self
.fonts
.sized_instance(id, selection, adjustments, style.size, self.resolver)?
.map(|(instance, used)| {
let (synthesis, oblique_skew) = instance.synthesis(selection);
Arc::new(ShapingInstance {
face: Face::from_face(instance.face),
variations: instance.variations,
synthesis,
oblique_skew,
scale: if style.size == 0.0 {
1.0
} else {
used / style.size
},
})
});
self.faces.borrow_mut().insert(key, face.clone());
Ok(face)
}
fn has_optical_axis(&self, id: FontId) -> Result<bool, TextError> {
if let Some(optical) = self.optical_axes.borrow().get(&id) {
return Ok(*optical);
}
let optical = self.fonts.has_optical_axis(id, self.resolver)?;
self.optical_axes.borrow_mut().insert(id, optical);
Ok(optical)
}
fn primary(&self, style: &Style) -> Result<FontId, TextError> {
for &id in style.font_candidates() {
if self.face(id, style)?.is_some() {
return Ok(id);
}
}
Err(TextError::NoFontCandidates)
}
fn shape_run(
&self,
id: FontId,
paragraph: &str,
range: Range<usize>,
rtl: bool,
style: &Style,
missing: MissingGlyphs,
) -> Result<Vec<GlyphRun>, TextError> {
let instance = self.face(id, style)?.ok_or(TextError::NoFontCandidates)?;
let face = &instance.face;
let narrow_space_advance = if style.size > 0.0
&& paragraph[range.clone()].contains(shaping::NARROW_NO_BREAK_SPACE)
{
let normal_space = match crate::fonts::advance(face, ' ') {
Some(width) => width,
None => self
.shape(" ", 0..1, false, style, MissingGlyphs::Substitute)?
.iter()
.filter(|run| run.font_size > 0.0)
.map(|run| run.width / run.font_size)
.sum(),
};
Some(normal_space * shaping::NARROW_SPACE_FACTOR)
} else {
None
};
let entry = self.fonts.face(id).ok_or(FontError::UnknownFont(id))?;
let family = &entry.descriptor().family;
let alternates =
features::alternate_features(&style.font_features.alternates, |kind, name| {
self.fonts
.feature_value(style.font_namespace(), family, kind, name)
});
let palette = entry.data().and_then(|data| {
self.fonts.palette(
style.font_namespace(),
&style.font_features.palette,
family,
data,
)
});
let language = shaping_language(face, style);
shaping::shape_runs(
shaping::ShapingFont {
face,
font_id: id,
synthesis: instance.synthesis,
oblique_skew: instance.oblique_skew,
variations: &instance.variations,
narrow_space_advance,
missing,
size: instance.used_size(style),
features: features::FaceFeatures {
descriptor: &entry.settings().features,
alternates: &alternates,
..Default::default()
},
language: language.as_ref(),
palette: palette.as_ref(),
},
paragraph,
range,
rtl,
style,
)
}
fn spend_matching_work(&self, units: usize) -> Result<(), TextError> {
let remaining = self
.matching_work
.get()
.and_then(|work| work.checked_sub(units.max(1)));
self.matching_work.set(remaining);
if remaining.is_none() {
return Err(TextError::GlyphMatchingLimit {
limit: MAX_GLYPH_MATCH_WORK,
});
}
Ok(())
}
fn matching_font(
&self,
paragraph: &str,
range: Range<usize>,
rtl: bool,
style: &Style,
named: Option<&[FontId]>,
presentation: bool,
) -> Result<Option<FontId>, TextError> {
let candidates = if paragraph[range.clone()].chars().any(shaping::private_use) {
named.expect("private-use candidates were resolved for the run")
} else {
style.font_candidates()
};
let matched = self.fonts.find_glyph(candidates, |id| {
self.spend_matching_work(
range
.len()
.saturating_mul(paragraph[range.clone()].chars().count()),
)?;
let Some(instance) = self.face(id, style)? else {
return Ok(GlyphMatch::Unavailable);
};
if !shaping::supports_variations(
&instance.face,
¶graph[range.clone()],
presentation.then_some(style.font_features.emoji),
) {
return Ok(GlyphMatch::Missing);
}
match self.shape_run(
id,
paragraph,
range.clone(),
rtl,
style,
MissingGlyphs::Reject,
) {
Ok(_) => Ok(GlyphMatch::Found(())),
Err(TextError::MissingGlyph { .. }) => Ok(GlyphMatch::Missing),
Err(error) => Err(error),
}
})?;
Ok(matched.map(|(id, ())| id))
}
fn presented_font(
&self,
paragraph: &str,
range: Range<usize>,
rtl: bool,
style: &Style,
named: Option<&[FontId]>,
) -> Result<Option<FontId>, TextError> {
let presented = self.matching_font(paragraph, range.clone(), rtl, style, named, true)?;
if presented.is_some() || !implies_presentation(style, ¶graph[range.clone()]) {
return Ok(presented);
}
self.matching_font(paragraph, range, rtl, style, named, false)
}
fn shape(
&self,
paragraph: &str,
range: Range<usize>,
rtl: bool,
style: &Style,
missing: MissingGlyphs,
) -> Result<Vec<GlyphRun>, TextError> {
if self.matching_work.get().is_none() {
return Err(TextError::GlyphMatchingLimit {
limit: MAX_GLYPH_MATCH_WORK,
});
}
let text = ¶graph[range.clone()];
let mut has_private = false;
let mut has_variation = false;
if !text.is_ascii() {
for cluster in text.graphemes(true) {
let mut scalars = 0;
for ch in cluster.chars() {
scalars += 1;
has_private |= shaping::private_use(ch);
has_variation |= shaping::variation_selector(ch);
}
if scalars > 1 {
self.spend_matching_work(cluster.len().saturating_mul(scalars))?;
}
}
}
has_variation |= implies_presentation(style, text);
let primary = self.primary(style)?;
let named = has_private.then(|| style.named_font_candidates(self.fonts));
if named.is_none() && !has_variation {
match self.shape_run(
primary,
paragraph,
range.clone(),
rtl,
style,
MissingGlyphs::Reject,
) {
Ok(runs) => return Ok(runs),
Err(TextError::MissingGlyph { .. }) => {}
Err(error) => return Err(error),
}
}
let symbol_font = self.fonts.missing_glyph_font();
let mut pieces: Vec<(Range<usize>, FontId, MissingGlyphs)> = Vec::new();
let mut append = |range: Range<usize>, id, policy| {
if let Some((last, previous, last_policy)) = pieces.last_mut()
&& *previous == id
&& *last_policy == policy
{
last.end = range.end;
} else {
pieces.push((range, id, policy));
}
};
for (offset, grapheme) in paragraph[range.clone()].grapheme_indices(true) {
let cluster = range.start + offset..range.start + offset + grapheme.len();
if let Some(id) =
self.presented_font(paragraph, cluster.clone(), rtl, style, named.as_deref())?
{
append(cluster, id, MissingGlyphs::Reject);
continue;
}
if missing == MissingGlyphs::Reject {
return Err(TextError::MissingGlyph {
text: grapheme.into(),
});
}
if grapheme.chars().nth(1).is_none() {
append(
cluster,
symbol_font.ok_or(TextError::NoFontCandidates)?,
MissingGlyphs::Substitute,
);
continue;
}
let mut remainder = cluster.start;
for (offset, _) in grapheme
.char_indices()
.rev()
.filter(|(offset, _)| *offset > 0)
{
let prefix = cluster.start..cluster.start + offset;
if let Some(id) =
self.presented_font(paragraph, prefix.clone(), rtl, style, named.as_deref())?
{
remainder = prefix.end;
append(prefix, id, MissingGlyphs::Reject);
break;
}
}
for (offset, ch) in paragraph[remainder..cluster.end].char_indices() {
let part = remainder + offset..remainder + offset + ch.len_utf8();
if let Some(id) =
self.presented_font(paragraph, part.clone(), rtl, style, named.as_deref())?
{
append(part, id, MissingGlyphs::Reject);
} else {
append(
part,
symbol_font.ok_or(TextError::NoFontCandidates)?,
MissingGlyphs::Substitute,
);
}
}
}
if rtl {
pieces.reverse();
}
let mut runs = Vec::new();
for (range, id, policy) in pieces {
runs.extend(self.shape_run(id, paragraph, range, rtl, style, policy)?);
}
Ok(runs)
}
pub(crate) fn layout_paragraph(
&self,
paragraph: &Paragraph,
width: f32,
atomics: &[AtomicMetrics],
) -> Result<Vec<Line>, TextError> {
let prepared = self.prepare_paragraph(paragraph);
prepared.lines(prepared.start(), width, atomics)?.collect()
}
pub(crate) fn outline_bounds(
&self,
paragraph: &Paragraph,
line: &Line,
) -> Result<Option<crate::graphics::Rect>, TextError> {
let mut bounds: Option<crate::graphics::Rect> = None;
for run in &line.runs {
let style = ¶graph.spans()[run.source_span].style;
let Some(instance) = self.face(run.font_id, style)? else {
continue;
};
for rect in run.outline_rects(&instance.face) {
let ink = run.synthesized_ink(
[rect.x, rect.x + rect.width],
[rect.y, rect.y + rect.height],
[0.0, 0.0],
);
bounds = Some(bounds.map_or(ink, |bounds| bounds.union(ink)));
}
}
Ok(bounds)
}
pub(crate) fn place_line_emphasis(
&self,
paragraph: &Paragraph,
line: &mut Line,
) -> Result<(), TextError> {
emphasis::place(self, paragraph, line)
}
pub(crate) fn finish_line_paint(
&self,
paragraph: &Paragraph,
line: &mut Line,
) -> Result<(), TextError> {
line.refresh_ink_bounds();
line.links = line_boxes::links(paragraph, line);
line.decorations = decoration::paint(self, paragraph, line)?;
Ok(())
}
pub(crate) fn prepare_paragraph<'p, 'context>(
&'context self,
paragraph: &'p Paragraph,
) -> PreparedParagraph<'p, 'context, 'a> {
PreparedParagraph::new(self, paragraph)
}
}