use super::ComposedParagraph;
use crate::model::paragraph::{LineSeg, Paragraph, SpaceMetric};
use std::borrow::Cow;
#[derive(Debug, Clone)]
pub(crate) struct FrameGeometry {
pub(super) line_segs: Vec<LineSeg>,
pub(super) space_metrics: Vec<(u32, SpaceMetric)>,
pub(super) inline_table_heights: Vec<(usize, u32)>,
pub(super) stored_partition: bool,
}
impl ComposedParagraph {
pub(crate) fn frame_owns_multiple_inline_tables(&self) -> bool {
self.frame_geometry
.as_ref()
.is_some_and(|frame| !frame.stored_partition && frame.inline_table_heights.len() > 1)
}
pub(crate) fn control_line_indices(&self, para: &Paragraph) -> Option<Vec<usize>> {
if let Some(frame) = self
.frame_geometry
.as_ref()
.filter(|frame| !frame.stored_partition)
{
(0..para.controls.len())
.map(|index| {
super::control_slot_line_index_with_starts(
para,
index,
frame.line_segs.iter().map(|segment| segment.text_start),
)
})
.collect()
} else if crate::renderer::para_has_no_stored_line_segs(para) {
(0..para.controls.len())
.map(|index| {
self.inline_controls
.iter()
.find(|owner| owner.control_index == index)
.filter(|owner| owner.line_index < self.lines.len())
.map(|owner| owner.line_index)
})
.collect()
} else {
crate::renderer::float_placement::stored_control_line_indices(para)
}
}
pub(crate) fn inline_table_height_hu(&self, index: usize) -> Option<u32> {
self.frame_geometry
.as_ref()?
.inline_table_heights
.iter()
.find_map(|&(owner, height)| (owner == index).then_some(height))
}
pub(crate) fn frame_owns_inline_table(&self, index: usize) -> bool {
self.frame_geometry.as_ref().is_some_and(|frame| {
!frame.stored_partition
&& frame
.inline_table_heights
.iter()
.any(|&(owner, _)| owner == index)
})
}
pub(crate) fn paragraph_with_frame_geometry<'a>(
&self,
source: &'a Paragraph,
) -> Cow<'a, Paragraph> {
match self.paragraph_with_frame_geometry_with_storage(source, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn paragraph_with_frame_geometry_with_storage<'a, S: crate::storage::Storage>(
&self,
source: &'a Paragraph,
storage: &S,
) -> Result<Cow<'a, Paragraph>, S::Error> {
Ok({
match &self.frame_geometry {
Some(frame) => {
let mut paragraph = storage.clone_paragraph(source)?;
let lines = crate::storage::try_clone(&frame.line_segs, storage)?;
let spaces = crate::storage::try_clone(&frame.space_metrics, storage)?;
if frame.stored_partition {
paragraph.line_segs = lines;
paragraph.layout_space_metrics = spaces;
paragraph.source_line_seg_vertical_pos = None;
} else {
paragraph.replace_line_segs_with_space_metrics(lines, spaces);
}
for &(index, height) in &frame.inline_table_heights {
if let Some(crate::model::control::Control::Table(table)) =
paragraph.controls.get_mut(index)
{
table.common.height = height;
}
}
Cow::Owned(paragraph)
}
None => Cow::Borrowed(source),
}
})
}
}