use super::{
line_breaking, utf16_range_to_text_range, ComposedParagraph, ResolvedStyleSet, SpaceMetric,
};
use crate::model::paragraph::{LineSeg, Paragraph};
use crate::renderer::layout_frame::{LayoutFrame, ParagraphBox};
pub(crate) fn projects_current_marker_geometry(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
) -> bool {
stored_rows_match_paragraph_box(para, box_)
&& crate::renderer::layout::list_marker_geometry(para, styles)
.is_some_and(|marker| marker.marker_visible)
}
pub(crate) fn project_current_marker_height_rows(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_current_marker_height_rows_with_storage(
para,
box_,
styles,
dpi,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn project_current_marker_height_rows_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
crate::storage::with_optional(|failure| {
if !projects_current_marker_geometry(para, box_, styles)
|| !crate::renderer::cell_paragraph_has_only_text_metadata(para)
|| para.text.chars().any(char::is_control)
|| para.stored_text_partition_is_dirty()
|| super::stored_row_fragments::crosses_physical_frame(para)
|| para
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
let marker = crate::renderer::layout::list_marker_geometry(para, styles)?;
let minimum_height = (marker.line_font_size_px * 7200.0 / dpi).round() as i32;
let first = para.line_segs.first()?;
if first.line_height >= minimum_height {
return None;
}
let mut frame = box_.frame(first.vertical_pos);
let rows = crate::storage::optional(
line_breaking::layout_paragraph_in_frame_with_storage(
para, &mut frame, styles, dpi, storage,
),
failure,
)??;
if rows
.first()
.is_some_and(|row| row.line_height >= minimum_height)
{
Some((
rows,
crate::storage::optional(
frame.try_project_space_metrics_since(0, storage),
failure,
)?,
))
} else {
None
}
})
}
pub(super) fn stored_rows_match_paragraph_box(para: &Paragraph, box_: &ParagraphBox) -> bool {
let extent = box_.effective();
let stored_right = extent.end
- extent
.end
.rem_euclid(crate::renderer::layout_frame::COLUMN_WIDTH_QUANTUM_HWP);
box_.is_usable()
&& !para.line_segs.is_empty()
&& para.line_segs.iter().all(|line| {
line.is_first_segment()
&& line.is_last_segment()
&& line.column_start == extent.start
&& (line.segment_width == extent.end - extent.start
|| line.segment_width == stored_right - extent.start)
})
}
pub(crate) fn project_current_body_box_rows(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_current_body_box_rows_with_storage(
para,
box_,
styles,
dpi,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn project_current_body_box_rows_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
crate::storage::with_optional(|failure| {
let style = styles.para_styles.get(para.para_shape_id as usize)?;
let extent = box_.effective();
if style.head_type != crate::model::style::HeadType::None
|| !box_.is_usable()
|| extent.start <= 0
|| para.line_segs.len() < 2
|| !crate::renderer::cell_paragraph_has_only_text_metadata(para)
|| para.text.chars().any(char::is_control)
|| !para.line_segs.iter().all(|line| {
line.is_first_segment()
&& line.is_last_segment()
&& line.column_start < extent.start
&& line.segment_width == extent.end - extent.start
})
|| para.line_segs.windows(2).any(|pair| {
pair[1].vertical_pos <= pair[0].vertical_pos
|| pair[1].is_first_line_of_page()
|| pair[1].is_first_line_of_column()
})
{
return None;
}
crate::storage::optional(
project_stored_rows_with_storage(
para,
&box_.frame(para.line_segs[0].vertical_pos),
styles,
dpi,
false,
true,
storage,
),
failure,
)?
})
}
pub(crate) fn project_current_source_program_rows(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_current_source_program_rows_with_storage(
para,
box_,
styles,
dpi,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn project_current_source_program_rows_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
box_: &ParagraphBox,
styles: &ResolvedStyleSet,
dpi: f64,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
crate::storage::with_optional(|failure| {
if styles.source_program_projection_slots.is_empty() {
return None;
}
if !stored_rows_match_paragraph_box(para, box_)
|| !crate::renderer::cell_paragraph_has_only_text_metadata(para)
|| para.text.chars().any(char::is_control)
{
return None;
}
let composed = crate::storage::optional(
super::compose_paragraph_in_context_with_storage(para, styles, storage),
failure,
)?;
let mut fallbacks = crate::storage::optional(storage.vec(0), failure)?;
for run in composed.lines.iter().flat_map(|line| &line.runs) {
if run.text.chars().all(char::is_whitespace) {
let original = styles.char_styles.get(run.char_style_id as usize)?;
let primary = super::super::style_resolver::primary_font_name(
original.font_family_for_lang(run.lang_index),
);
if !original.font_metric_trusted_for_lang(run.lang_index)
&& crate::renderer::layout::verified_source_projection_face(
primary,
original.bold,
original.italic,
)
&& !crate::renderer::layout::verified_source_projection_text(
primary,
original.bold,
original.italic,
&run.text,
)
{
return None;
}
continue;
}
let fallback = styles
.source_program_projection_slots
.get(&(run.char_style_id, run.lang_index))?;
if run.inserted_control_text {
return None;
}
let original = styles.char_styles.get(run.char_style_id as usize)?;
if !original.font_metric_trusted_for_lang(run.lang_index)
&& !crate::renderer::layout::verified_source_projection_text(
super::super::style_resolver::primary_font_name(
original.font_family_for_lang(run.lang_index),
),
original.bold,
original.italic,
&run.text,
)
{
return None;
}
crate::storage::optional(
storage.push(&mut fallbacks, (run.char_style_id, fallback)),
failure,
)?;
}
if fallbacks.is_empty() {
return None;
}
let mut fallback_styles =
crate::storage::optional(crate::storage::try_clone(styles, storage), failure)?;
fallback_styles.source_program_projection_slots = Default::default();
for (id, fallback) in fallbacks {
*fallback_styles.char_styles.get_mut(id as usize)? =
crate::storage::optional(crate::storage::try_clone(fallback, storage), failure)?;
}
let first = para.line_segs.first()?.vertical_pos;
let frame = box_.frame(first);
let fallback_rows = crate::storage::optional(
line_breaking::layout_paragraph_in_frame_with_storage(
para,
&mut crate::storage::optional(crate::storage::try_clone(&frame, storage), failure)?,
&fallback_styles,
dpi,
storage,
),
failure,
)??;
if fallback_rows.len() != para.line_segs.len()
|| fallback_rows
.iter()
.enumerate()
.any(|(index, row)| row.text_start != para.line_seg_text_start(index))
{
return None;
}
crate::storage::optional(
project_stored_rows_with_storage(para, &frame, styles, dpi, false, true, storage),
failure,
)?
})
}
pub(super) fn has_unmarked_final_word_rewind(para: &Paragraph) -> bool {
match has_unmarked_final_word_rewind_with_storage(para, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn has_unmarked_final_word_rewind_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
storage: &S,
) -> Result<bool, S::Error> {
Ok({
let Some((last, prefix)) = para.line_segs.split_last() else {
return Ok(false);
};
let Some(previous) = prefix.last() else {
return Ok(false);
};
let chars: Vec<_> = storage.collect(para.text.chars())?;
let (start, _) = utf16_range_to_text_range(
¶.char_offsets,
para.line_seg_text_start(para.line_segs.len() - 1),
u32::MAX,
chars.len(),
);
start > 0
&& start < chars.len()
&& chars[start - 1].is_alphanumeric()
&& chars[start].is_alphanumeric()
&& !last.is_first_line_of_page()
&& !last.is_first_line_of_column()
&& last.vertical_pos >= 0
&& last.vertical_pos < previous.vertical_pos
&& last.column_start == previous.column_start
&& last.segment_width > 0
&& last.segment_width == previous.segment_width
&& !prefix.windows(2).any(|pair| {
pair[1].is_first_line_of_page()
|| pair[1].is_first_line_of_column()
|| pair[1].vertical_pos < pair[0].vertical_pos
})
})
}
pub(super) fn regenerated_spaces_change_metrics(
composed: &ComposedParagraph,
styles: &ResolvedStyleSet,
metric: SpaceMetric,
) -> bool {
match regenerated_spaces_change_metrics_with_storage(
composed,
styles,
metric,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn regenerated_spaces_change_metrics_with_storage<S: crate::storage::Storage>(
composed: &ComposedParagraph,
styles: &ResolvedStyleSet,
metric: SpaceMetric,
storage: &S,
) -> Result<bool, S::Error> {
Ok({
for run in composed.lines.iter().flat_map(|line| &line.runs) {
if !run.text.contains(' ') {
continue;
}
let stored = run.try_text_style(styles, storage)?;
let mut regenerated = crate::storage::try_clone(&stored, storage)?;
metric.apply_to_style(&mut regenerated);
let advance = |style: &crate::renderer::TextStyle| {
crate::renderer::layout::estimate_text_width_unrounded(" ", style)
};
if (advance(&stored) - advance(®enerated)).abs() > f64::EPSILON {
return Ok(true);
}
}
false
})
}
pub(super) fn can_rejoin_stored_word_tail(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
) -> bool {
match can_rejoin_stored_word_tail_with_storage(
para,
frame,
styles,
dpi,
supported_inline_picture,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn can_rejoin_stored_word_tail_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
storage: &S,
) -> Result<bool, S::Error> {
Ok({
if para.line_segs.len() < 2 {
return Ok(false);
}
let chars: Vec<_> = storage.collect(para.text.chars())?;
let (start, _) = utf16_range_to_text_range(
¶.char_offsets,
para.line_seg_text_start(para.line_segs.len() - 1),
u32::MAX,
chars.len(),
);
if start == 0
|| start >= chars.len()
|| !chars[start - 1].is_alphanumeric()
|| !chars[start].is_alphanumeric()
{
return Ok(false);
}
project_stored_soft_wraps_with_storage(
para,
frame,
styles,
dpi,
supported_inline_picture,
storage,
)?
.is_some()
})
}
pub(crate) fn project_stored_soft_wraps(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_stored_soft_wraps_with_storage(
para,
frame,
styles,
dpi,
supported_inline_picture,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn project_stored_soft_wraps_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
project_stored_rows_with_storage(
para,
frame,
styles,
dpi,
supported_inline_picture,
false,
storage,
)
}
fn project_stored_rows(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
refresh_physical_cache: bool,
) -> Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)> {
match project_stored_rows_with_storage(
para,
frame,
styles,
dpi,
supported_inline_picture,
refresh_physical_cache,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
fn project_stored_rows_with_storage<S: crate::storage::Storage>(
para: &Paragraph,
frame: &LayoutFrame,
styles: &ResolvedStyleSet,
dpi: f64,
supported_inline_picture: bool,
refresh_physical_cache: bool,
storage: &S,
) -> Result<Option<(Vec<LineSeg>, Vec<(u32, SpaceMetric)>)>, S::Error> {
crate::storage::with_optional(|failure| {
if let Some(projected) = crate::storage::optional(
super::stored_marker_frames::project_with_storage(para, frame, styles, dpi, storage),
failure,
)? {
return Some(projected);
}
if para.line_segs.len() < 2
|| (!para.controls.is_empty()
&& !supported_inline_picture
&& !(refresh_physical_cache
&& crate::renderer::cell_paragraph_has_only_text_metadata(para)))
|| para.text.contains('\n')
|| para.stored_text_partition_is_dirty()
|| (!refresh_physical_cache
&& super::stored_row_fragments::crosses_physical_frame(para)
&& !crate::storage::optional(
has_unmarked_final_word_rewind_with_storage(para, storage),
failure,
)?)
|| frame.models_exclusions()
|| para
.line_segs
.iter()
.any(|line| line.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0)
{
return None;
}
let last = para.line_segs.last()?;
let previous = ¶.line_segs[para.line_segs.len() - 2];
if !refresh_physical_cache
&& (last.is_first_line_of_page()
|| last.is_first_line_of_column()
|| last.vertical_pos == previous.vertical_pos
|| (last.vertical_pos < previous.vertical_pos
&& (last.vertical_pos < 0
|| last.column_start != previous.column_start
|| last.segment_width <= 0
|| last.segment_width != previous.segment_width)))
{
return None;
}
let count = para.text.chars().count();
let (last_start, _) = utf16_range_to_text_range(
¶.char_offsets,
para.line_seg_text_start(para.line_segs.len() - 1),
u32::MAX,
count,
);
if last_start == 0
|| last_start >= count
|| !para
.text
.chars()
.skip(last_start)
.any(|ch| !ch.is_whitespace())
{
return None;
}
let mut probe =
crate::storage::optional(crate::storage::try_clone(frame, storage), failure)?;
let rows = crate::storage::optional(
line_breaking::layout_paragraph_in_frame_with_storage(
para, &mut probe, styles, dpi, storage,
),
failure,
)??;
if refresh_physical_cache {
let cached = crate::storage::optional(
super::compose_paragraph_in_context_with_storage(para, styles, storage),
failure,
)?;
let chars = crate::storage::optional(storage.collect(para.text.chars()), failure)?;
let source_index = |position| {
utf16_range_to_text_range(¶.char_offsets, position, u32::MAX, chars.len()).0
};
let fresh_starts = crate::storage::optional(
storage.collect(rows.iter().map(|line| source_index(line.text_start))),
failure,
)?;
let cached_content_boundary_moves = (1..para.line_segs.len()).any(|index| {
let start = source_index(para.line_seg_text_start(index));
let next_ink = chars
.iter()
.enumerate()
.skip(start)
.find(|(_, ch)| !ch.is_whitespace())
.map(|(index, _)| index);
start > 0
&& next_ink.is_some_and(|next| {
fresh_starts.partition_point(|&fresh| fresh <= next) < index + 1
})
&& !cached
.lines
.get(index - 1)
.is_some_and(|line| line.has_line_break)
});
return if cached_content_boundary_moves {
Some((
rows,
crate::storage::optional(
probe.try_project_space_metrics_since(0, storage),
failure,
)?,
))
} else {
None
};
}
if rows.len() < para.line_segs.len()
|| (rows.len() == para.line_segs.len()
&& crate::storage::optional(
has_unmarked_final_word_rewind_with_storage(para, storage),
failure,
)?)
{
Some((
rows,
crate::storage::optional(
probe.try_project_space_metrics_since(0, storage),
failure,
)?,
))
} else {
None
}
})
}