use super::*;
pub(super) fn shape_logical_joining_graph_runs(
runs: &mut [InlineParagraphRun],
font_system: &mut FontSystem,
tab_metric_style: &ComputedStyle,
) {
let mut index = 0;
while index < runs.len() {
let InlineLineItem::Fragment(_) = &runs[index].item else {
index += 1;
continue;
};
let mut fragment_indices = vec![index];
index += 1;
while let Some(run) = runs.get(index) {
match &run.item {
InlineLineItem::Fragment(right) => {
let InlineLineItem::Fragment(left) =
&runs[*fragment_indices.last().expect("first graph fragment")].item
else {
unreachable!("graph fragment indices name fragments");
};
if graph_fragments_have_nonjoining_tracking_boundary(left, right) {
break;
}
if !can_shape_inline_fragments_together(left, right) {
break;
}
fragment_indices.push(index);
index += 1;
}
InlineLineItem::Atom(atom) if graph_atom_is_transparent_to_shaping(atom) => {
index += 1;
}
InlineLineItem::Float(_) => {
index += 1;
}
InlineLineItem::Atom(_) => break,
}
}
let needs_source_shape = fragment_indices.len() == 1
&& matches!(
&runs[fragment_indices[0]].item,
InlineLineItem::Fragment(fragment) if {
let mut opportunities = Vec::new();
font_system.collect_cached_measured_break_opportunities(
fragment.text(),
TextBreakPolicy::from(fragment.style()),
&mut opportunities,
);
opportunities.into_iter().any(|position| {
position > 0 && position < fragment.text().len()
})
}
);
if fragment_indices.len() < 2 && !needs_source_shape {
continue;
}
if fragment_indices.len() == 1 {
let fragment_index = fragment_indices[0];
let Some(source) = runs[fragment_index].shaped.clone() else {
continue;
};
let text_len = match &runs[fragment_index].item {
InlineLineItem::Fragment(fragment) => fragment.text().len(),
InlineLineItem::Atom(_) | InlineLineItem::Float(_) => {
unreachable!("graph fragment indices name fragments")
}
};
let Some(selection) = SourceShapedSelection::from_source(source, 0..text_len) else {
continue;
};
let selected = selection.selected_rc();
let InlineLineItem::Fragment(fragment) = &mut runs[fragment_index].item else {
unreachable!("graph fragment indices name fragments")
};
runs[fragment_index].width = selected.advance_width();
runs[fragment_index].shaped = Some(selected);
fragment.set_source_shaped_selection(Some(selection));
continue;
}
let mut spans = Vec::with_capacity(fragment_indices.len());
let mut span_styles = Vec::with_capacity(fragment_indices.len());
let mut text = String::new();
let mut ranges = Vec::with_capacity(fragment_indices.len());
let mut line_height = None;
for &fragment_index in &fragment_indices {
let InlineLineItem::Fragment(fragment) = &runs[fragment_index].item else {
unreachable!("graph fragment indices name fragments");
};
line_height.get_or_insert(fragment.style().line_height);
let start = text.len();
text.push_str(fragment.text());
ranges.push(start..text.len());
spans.push(StyledTextSpan {
text: fragment.text(),
style: fragment.style(),
});
span_styles.push(Rc::clone(&fragment.data.style));
}
let Some(shaped) = font_system
.shape_untracked_styled_inline_fragments_with_style_identities(
&spans,
text,
line_height.expect("graph shaping group has a fragment"),
tab_metric_style,
&span_styles,
)
else {
continue;
};
let shaped = Rc::new(shaped);
let boundary_source = Rc::new(BoundaryShapedSource {
shaped: Rc::clone(&shaped),
});
for (&fragment_index, range) in fragment_indices.iter().zip(ranges) {
let InlineLineItem::Fragment(fragment) = &mut runs[fragment_index].item else {
unreachable!("graph fragment indices name fragments")
};
fragment.set_boundary_shaped_source(Rc::clone(&boundary_source), range.clone());
let Some(mut selection) =
SourceShapedSelection::from_source(Rc::clone(&shaped), range.clone())
else {
continue;
};
let slice = Some(selection.selected().clone());
let width = slice
.as_ref()
.map(ShapedInlineLine::advance_width)
.unwrap_or(0.0);
if let Some(slice) = slice.as_ref() {
selection.replace_selected(slice.clone());
}
runs[fragment_index].width = width;
runs[fragment_index].shaped = slice.map(Rc::new);
fragment.set_source_shaped_selection(Some(selection));
}
}
}
fn graph_fragments_have_nonjoining_tracking_boundary(
left: &InlineFragment,
right: &InlineFragment,
) -> bool {
let (Some(left_scope), Some(right_scope)) = (left.tracking_scope(), right.tracking_scope())
else {
return false;
};
InlineBoundaryAdvance::between(
UsedLetterSpacing::new(left_scope.letter_spacing()),
UsedLetterSpacing::new(right_scope.letter_spacing()),
)
.points()
!= 0.0
&& crate::text::inter_character_gap_allowed_between_text(left.text(), right.text())
}
pub(super) fn graph_atom_is_transparent_to_shaping(atom: &InlineAtom) -> bool {
matches!(atom.content(), InlineAtomContent::InlineEdge(InlineEdgeRole::BoxEdge(edge))
if edge.advance == 0.0
&& edge.paint_extent == 0.0
&& !inline_box_edge_fragment_breaks_shaping(atom.style(), *edge)
&& !inline_box_bidi_isolation_breaks_shaping(atom.style()))
}
pub(in crate::layout) fn push_text_graph_runs(
font_system: &mut FontSystem,
runs: &mut Vec<InlineParagraphRun>,
word: &InlineWord,
text: &str,
tracking_scope: Rc<InlineTrackingScope>,
) {
if text.is_empty() {
return;
}
let break_text = if word.style.hyphens == Hyphens::Auto
&& matches!(
word.style.automatic_discretionary_hyphenation_policy(),
DiscretionaryHyphenationPolicy::Ordinary
) {
Cow::Borrowed(text)
} else {
text_with_hyphenation_controls(text, &word.style)
};
let text = break_text.as_ref();
if text.is_empty() {
return;
}
let source_run = Rc::new(());
if word.source == InlineTextSource::BidiControl {
push_text_graph_run_segment(
font_system,
runs,
word,
text,
word.hanging_edges,
tracking_scope,
source_run,
);
return;
}
if tracking_scope.letter_spacing().points() == 0.0 {
push_text_graph_run_segment(
font_system,
runs,
word,
text,
word.hanging_edges,
tracking_scope,
source_run,
);
return;
}
for range in CursiveProtectedUnitRanges::new(text) {
let mut hanging_edges = word.hanging_edges;
hanging_edges.blocks_start &= range.start == 0;
hanging_edges.blocks_end &= range.end == text.len();
push_text_graph_run_segment(
font_system,
runs,
word,
&text[range],
hanging_edges,
Rc::clone(&tracking_scope),
Rc::clone(&source_run),
);
}
}
pub(in crate::layout) fn push_text_graph_run_segment(
font_system: &mut FontSystem,
runs: &mut Vec<InlineParagraphRun>,
word: &InlineWord,
text: &str,
hanging_edges: InlineHangingEdges,
tracking_scope: Rc<InlineTrackingScope>,
source_run: Rc<()>,
) {
if text.is_empty() {
return;
}
let mut fragment = InlineFragment::new_shared_style(
text,
Rc::clone(&word.style),
word.baseline_shift,
word.link_target.clone(),
word.mergeable,
word.source,
false,
hanging_edges,
Rc::clone(&word.ancestor_inline_decorations),
)
.with_visual_offset(word.visual_offset)
.with_excluded_positioning_geometry_source(word.excluded_positioning_geometry_source)
.with_source_run(source_run)
.with_tracking_scope(tracking_scope);
if word.style.content.is_generated() && word.style.display.is_flex() {
fragment.set_force_inline_background_paint(true);
}
let shaped = (word.source != InlineTextSource::BidiControl)
.then(|| {
font_system.shape_untracked_inline_line_with_style_identity(
text,
&word.style,
word.style.line_height,
)
})
.flatten();
let width = shaped
.as_ref()
.map(ShapedInlineLine::advance_width)
.unwrap_or(0.0);
let shaped = shaped.map(Rc::new);
runs.push(InlineParagraphRun {
item: InlineLineItem::Fragment(fragment),
width,
shaped,
});
}
pub(super) fn run_tracking_scope<'a>(
run: &'a InlineParagraphRun,
fallback: &ComputedStyle,
) -> &'a Rc<InlineTrackingScope> {
match &run.item {
InlineLineItem::Fragment(fragment) => fragment
.tracking_scope()
.expect("graph fragments retain inline tracking scope"),
InlineLineItem::Atom(atom) => atom
.tracking_scope()
.expect("graph atoms retain inline tracking scope"),
InlineLineItem::Float(_) => {
let _ = fallback;
unreachable!("first-letter graph fragments do not inherit float scope")
}
}
}
pub(in crate::layout) fn inline_run_has_nonzero_tracking(run: &InlineParagraphRun) -> bool {
match &run.item {
InlineLineItem::Fragment(fragment) => fragment
.tracking_scope()
.is_some_and(|scope| scope.letter_spacing().points() != 0.0),
InlineLineItem::Atom(atom) => atom
.tracking_scope()
.is_some_and(|scope| scope.letter_spacing().points() != 0.0),
InlineLineItem::Float(_) => false,
}
}