use super::*;
#[derive(Debug, Clone, Default)]
pub(in crate::layout) struct BidiLineScopeContinuations {
pub(in crate::layout) prefix_parent_context: String,
pub(in crate::layout) prefix: String,
pub(in crate::layout) suffix: String,
pub(in crate::layout) trailing_line_edge_context: String,
pub(in crate::layout) suffix_parent_context: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct BidiControlScope {
start: char,
end: char,
is_isolate: bool,
}
impl InlineOpportunityGraph {
fn clone_scopes_before(
&self,
position: InlineGraphPosition,
) -> Vec<InlineFragmentContinuation> {
let mut scopes = Vec::<Option<InlineFragmentContinuation>>::new();
for run in self.runs.iter().take(position.run_index) {
let InlineLineItem::Atom(atom) = &run.item else {
continue;
};
let InlineAtomContent::InlineEdge(InlineEdgeRole::BoxEdge(edge)) = atom.content()
else {
continue;
};
if edge.is_positioning_marker() {
continue;
}
match edge.logical_edge {
InlineLogicalEdge::Start => {
scopes.push(InlineFragmentContinuation::from_source_start(atom));
}
InlineLogicalEdge::End => {
scopes.pop();
}
}
}
scopes.into_iter().flatten().collect()
}
pub(super) fn insert_clone_continuation_edges(
&self,
range: InlineGraphRange,
items: &mut Vec<MeasuredInlineItem>,
) {
let leading = self.clone_scopes_before(range.start);
let trailing = self.clone_scopes_before(range.end);
if leading.is_empty() && trailing.is_empty() {
return;
}
let mut continued = Vec::with_capacity(leading.len() + items.len() + trailing.len());
continued.extend(
leading
.iter()
.map(InlineFragmentContinuation::start_measured_item),
);
continued.append(items);
continued.extend(
trailing
.iter()
.rev()
.map(InlineFragmentContinuation::end_measured_item),
);
mark_clone_continuation_fragment_edges(&mut continued, leading.len(), trailing.len());
*items = continued;
}
pub(in crate::layout) fn bidi_scope_continuations_for_range(
&self,
range: InlineGraphRange,
) -> BidiLineScopeContinuations {
let scopes_before_start = self.bidi_control_scopes_before(range.start);
let scopes_before_end = self.bidi_control_scopes_before(range.end);
BidiLineScopeContinuations {
prefix_parent_context: scopes_before_start
.iter()
.any(|scope| scope.is_isolate)
.then(|| self.parent_direction_before(range.start))
.flatten()
.map(bidi_prefix_parent_context_control)
.unwrap_or_default()
.to_owned(),
prefix: scopes_before_start
.iter()
.map(|scope| scope.start)
.collect(),
suffix: scopes_before_end
.iter()
.rev()
.map(|scope| scope.end)
.collect(),
trailing_line_edge_context: String::new(),
suffix_parent_context: scopes_before_end
.iter()
.any(|scope| scope.is_isolate)
.then(|| {
self.parent_direction_after(range.end)
.or_else(|| self.parent_direction_before(range.end))
})
.flatten()
.map(bidi_suffix_parent_context_control)
.unwrap_or_default()
.to_owned(),
}
}
fn parent_direction_before(&self, position: InlineGraphPosition) -> Option<Direction> {
let mut scopes = Vec::new();
let mut direction = None;
for (run_index, run) in self.runs.iter().enumerate() {
if run_index > position.run_index {
break;
}
let InlineLineItem::Fragment(fragment) = &run.item else {
continue;
};
let end = if run_index == position.run_index {
position.byte_offset.min(fragment.text().len())
} else {
fragment.text().len()
};
let Some(text) = fragment.text().get(..end) else {
continue;
};
if scopes.is_empty()
&& let Some(found) = plaintext_direction_for_text(text)
{
direction = Some(found);
}
update_bidi_control_scope_stack(&mut scopes, text);
}
direction
}
fn parent_direction_after(&self, position: InlineGraphPosition) -> Option<Direction> {
let mut scopes = self.bidi_control_scopes_before(position);
for (run_index, run) in self.runs.iter().enumerate().skip(position.run_index) {
let InlineLineItem::Fragment(fragment) = &run.item else {
continue;
};
let start = if run_index == position.run_index {
position.byte_offset.min(fragment.text().len())
} else {
0
};
let Some(text) = fragment.text().get(start..) else {
continue;
};
if scopes.is_empty()
&& let Some(direction) = plaintext_direction_for_text(text)
{
return Some(direction);
}
update_bidi_control_scope_stack(&mut scopes, text);
}
None
}
fn bidi_control_scopes_before(&self, position: InlineGraphPosition) -> Vec<BidiControlScope> {
let mut scopes = Vec::new();
for (run_index, run) in self.runs.iter().enumerate() {
if run_index > position.run_index {
break;
}
let InlineLineItem::Fragment(fragment) = &run.item else {
continue;
};
let end = if run_index == position.run_index {
position.byte_offset.min(fragment.text().len())
} else {
fragment.text().len()
};
if let Some(text) = fragment.text().get(..end) {
update_bidi_control_scope_stack(&mut scopes, text);
}
}
scopes
}
}
fn update_bidi_control_scope_stack(scopes: &mut Vec<BidiControlScope>, text: &str) {
for character in text.chars() {
let Some(scope) = bidi_control_scope_start(character) else {
match character {
'\u{202c}' => {
if scopes.last().is_some_and(|scope| !scope.is_isolate) {
scopes.pop();
}
}
'\u{2069}' => {
if let Some(isolate_index) = scopes.iter().rposition(|scope| scope.is_isolate) {
scopes.truncate(isolate_index);
}
}
_ => {}
}
continue;
};
scopes.push(scope);
}
}
fn bidi_control_scope_start(character: char) -> Option<BidiControlScope> {
let (end, is_isolate) = match character {
'\u{202a}' | '\u{202b}' | '\u{202d}' | '\u{202e}' => ('\u{202c}', false),
'\u{2066}' | '\u{2067}' | '\u{2068}' => ('\u{2069}', true),
_ => return None,
};
Some(BidiControlScope {
start: character,
end,
is_isolate,
})
}
fn bidi_prefix_parent_context_control(direction: Direction) -> &'static str {
match direction {
Direction::Ltr => "\u{200e}",
Direction::Rtl => "\u{200f}",
}
}
fn bidi_suffix_parent_context_control(direction: Direction) -> &'static str {
match direction {
Direction::Ltr => "\u{200e}",
Direction::Rtl => "\u{200f}",
}
}