use super::{line_breaking, projects_current_marker_geometry, ResolvedStyleSet, SpaceMetric};
use crate::model::paragraph::{LineSeg, Paragraph};
use crate::renderer::layout_frame::{LayoutFrame, ParagraphBox};
pub(super) fn project(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_with_storage(para, frame, styles, dpi, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn project_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
Ok({
if para.line_segs.len() < 2
|| super::stored_word_tail::has_unmarked_final_word_rewind_with_storage(para,storage)?
|| !para.controls.is_empty()
|| para.text.contains('\n')
|| para.stored_text_partition_is_dirty()
|| frame.models_exclusions()
|| para
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return Ok(None);
}
let Some(last_frame_start) = para
.line_segs
.windows(2)
.enumerate()
.filter(|(_, pair)| {
pair[1].is_first_line_of_page()
|| pair[1].is_first_line_of_column()
|| pair[1].vertical_pos < pair[0].vertical_pos
})
.map(|(index, _)| index + 1)
.next_back()
else {
return Ok(None);
};
let box_ = ParagraphBox::content(frame.horizontal.clone());
if !projects_current_marker_geometry(para, &box_, styles) {
return Ok(None);
}
let mut probe = crate::storage::try_clone(frame, storage)?;
let Some(mut rows) = line_breaking::layout_paragraph_in_frame_with_storage(
para, &mut probe, styles, dpi, storage,
)?
else {
return Ok(None);
};
let trace = std::env::var_os("RHWP_DIAG_MARKER_FRAMES").is_some();
if trace {
eprintln!(
"MARKER_FRAMES style={} source_rows={} projected_rows={} last_frame_start={}",
para.para_shape_id,
para.line_segs.len(),
rows.len(),
last_frame_start,
);
}
if rows.len() <= last_frame_start || rows.len() >= para.line_segs.len() {
return Ok(None);
}
let physical_flags = LineSeg::TAG_FIRST_LINE_OF_PAGE | LineSeg::TAG_FIRST_LINE_OF_COLUMN;
for (row, source) in rows.iter_mut().zip(¶.line_segs) {
if row.line_height != source.line_height || row.line_spacing != source.line_spacing {
if trace {
eprintln!("MARKER_FRAMES metric_mismatch new={row:?} source={source:?}");
}
return Ok(None);
}
row.vertical_pos = source.vertical_pos;
row.tag = (row.tag & !physical_flags) | (source.tag & physical_flags);
}
Some((rows, probe.try_project_space_metrics_since(0, storage)?))
})
}