use super::{ComposedLine, ComposedParagraph, ResolvedStyleSet};
use crate::model::{control::Control, paragraph::Paragraph};
use crate::renderer::{hwpunit_to_px, px_to_hwpunit, DEFAULT_DPI};
pub(super) fn refresh_equation_rows(
composed: &mut ComposedParagraph,
para: &Paragraph,
styles: &ResolvedStyleSet,
) {
refresh_rows(composed, para, styles, false);
}
pub(super) fn try_refresh_equation_rows<S: crate::storage::Storage>(
composed: &mut ComposedParagraph,
para: &Paragraph,
styles: &ResolvedStyleSet,
storage: &S,
) -> Result<(), S::Error> {
try_refresh_rows(composed, para, styles, false, storage)
}
fn refresh_rows(
composed: &mut ComposedParagraph,
para: &Paragraph,
styles: &ResolvedStyleSet,
regenerated: bool,
) {
match try_refresh_rows(
composed,
para,
styles,
regenerated,
&crate::storage::Unbounded,
) {
Ok(()) => (),
Err(never) => match never {},
}
}
fn try_refresh_rows<S: crate::storage::Storage>(
composed: &mut ComposedParagraph,
para: &Paragraph,
styles: &ResolvedStyleSet,
regenerated: bool,
storage: &S,
) -> Result<(), S::Error> {
try_refresh_single_tac_host_row(composed, para, styles, storage)?;
let positions = para.try_control_text_positions(storage)?;
let dpi = styles.resolved_dpi.unwrap_or(DEFAULT_DPI);
let tac_offsets_px: Vec<_> = storage.collect(
composed
.tac_controls
.iter()
.map(|(position, width, control)| (*position, hwpunit_to_px(*width, dpi), *control)),
)?;
let equation_owners =
crate::renderer::equation_tac_flow::uses_equation_only_flow(para, composed).then(|| {
crate::renderer::equation_tac_flow::equation_only_tac_line_assignment(
para,
composed,
&tac_offsets_px,
)
.into_iter()
.zip(&composed.tac_controls)
.map(|(line, (_, _, control))| (*control, line))
.collect::<Vec<_>>()
});
let line_count = composed.lines.len();
for line_index in 0..composed.lines.len() {
let following_box_height =
following_reserved_box_height(para, &positions, composed, line_index);
let stored_pair = para
.line_segs
.get(line_index)
.zip(para.line_segs.get(line_index + 1));
let end = composed
.lines
.get(line_index + 1)
.map_or(para.text.chars().count(), |line| line.char_start);
let line = &mut composed.lines[line_index];
let mut old_ascent = 0_i32;
let mut old_logical_height = 0_i32;
let mut new_logical_height = 0_i32;
let mut new_ascent = 0_i32;
let mut new_descent = 0_i32;
let mut spacing_object_height = 0_i32;
let mut changed = false;
let mut has_equation = false;
let mut has_form = false;
let mut all_current_simple_frames = true;
let mut supported = true;
for (control_index, (control, position)) in para.controls.iter().zip(&positions).enumerate()
{
let belongs_to_line = if let Some(owners) = &equation_owners {
owners.contains(&(control_index, line_index))
} else {
*position >= line.char_start
&& (*position < end || (*position == end && line_index + 1 == line_count))
};
if !belongs_to_line {
continue;
}
match control {
Control::Equation(equation) if equation.common.treat_as_char => {
has_equation = true;
let old_height = equation.common.height as i32;
all_current_simple_frames &=
para.line_segs.get(line_index).is_some_and(|source| {
source.line_height > old_height
&& crate::renderer::equation::has_current_simple_math_frame(
equation,
)
});
let old_baseline =
old_height.saturating_mul(i32::from(equation.baseline)) / 100;
let (_, height, baseline) =
crate::renderer::equation::flow_metrics_hwp(equation);
let height = height as i32;
old_ascent = old_ascent.max(old_baseline);
old_logical_height = old_logical_height.max(old_height);
new_logical_height = new_logical_height.max(height);
new_ascent = new_ascent.max(baseline);
new_descent = new_descent.max(height - baseline);
if equation.common.affect_line_spacing {
spacing_object_height = spacing_object_height.max(height);
}
changed |= height != old_height || baseline != old_baseline;
}
Control::Form(form) if form.common.treat_as_char => {
has_form = true;
let height = form.height as i32;
let baseline = super::line_breaking::baseline_distance_hwp(height);
old_ascent = old_ascent.max(baseline);
old_logical_height = old_logical_height.max(height);
new_logical_height = new_logical_height.max(height);
new_ascent = new_ascent.max(baseline);
new_descent = new_descent.max(height - baseline);
if form.common.affect_line_spacing {
spacing_object_height = spacing_object_height.max(height);
}
}
Control::Picture(_) | Control::Table(_) | Control::Shape(_) => supported = false,
_ => {}
}
}
if !supported {
continue;
}
if has_equation
&& new_logical_height <= 0
&& !line.runs.iter().any(|run| !run.text.trim().is_empty())
{
let generated = para.line_segs.get(line_index).is_some_and(|source| {
source.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
});
if generated || regenerated || para.line_segs.is_empty() {
let fs = line
.runs
.iter()
.filter_map(|run| styles.char_styles.get(run.char_style_id as usize))
.map(|style| style.font_size)
.reduce(f64::max)
.or_else(|| {
para.char_shapes
.first()
.and_then(|shape| styles.char_styles.get(shape.char_shape_id as usize))
.map(|style| style.font_size)
});
if let Some(fs) = fs.filter(|value| *value > 0.0) {
let ps = styles.para_styles.get(para.para_shape_id as usize);
let frame = super::frame_metrics_for_line(
fs,
fs,
ps.map_or(crate::model::style::LineSpacingType::Percent, |style| {
style.line_spacing_type
}),
ps.map_or(160.0, |style| style.line_spacing),
dpi,
);
line.line_height = frame.line_height;
line.baseline_distance = frame.baseline_distance;
line.line_spacing = frame.line_spacing;
}
}
continue;
}
let text_height = line
.runs
.iter()
.filter(|run| !run.text.trim().is_empty())
.filter_map(|run| styles.char_styles.get(run.char_style_id as usize))
.map(|style| px_to_hwpunit(style.font_size, dpi))
.max()
.unwrap_or(0);
let stored_text_strut_height = para
.line_segs
.get(line_index)
.filter(|source| {
!para.stored_text_partition_is_dirty()
&& line_count == para.line_segs.len()
&& source.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY
== 0
&& source.text_height > 0
&& source.baseline_distance
== (f64::from(source.text_height) * 0.85).round() as i32
&& para
.char_shape_id_at(line.char_start)
.and_then(|id| styles.char_styles.get(id as usize))
.is_some_and(|style| {
px_to_hwpunit(style.font_size, dpi) == source.text_height
})
})
.map_or(0, |source| source.text_height);
let text_height = text_height.max(stored_text_strut_height);
let text_ascent = (f64::from(text_height) * 0.85).round() as i32;
let text_descent = text_height - text_ascent;
if !has_equation && !has_form && text_height == 0 {
continue;
}
old_ascent = old_ascent.max(text_ascent);
old_logical_height = old_logical_height.max(text_height);
new_logical_height = new_logical_height.max(text_height);
new_ascent = new_ascent.max(text_ascent);
new_descent = new_descent.max(text_descent);
let current_simple_row = has_equation && !has_form && all_current_simple_frames;
let following_inline_box_was_reserved = stored_pair.is_some_and(|(source, next)| {
use crate::model::paragraph::LineSeg;
!para.stored_text_partition_is_dirty()
&& line_count == para.line_segs.len()
&& source.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& next.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& ((source.text_height == old_logical_height
&& (source.baseline_distance - old_ascent).abs() <= 1)
|| (current_simple_row && source.text_height > old_logical_height))
&& source.line_height > old_logical_height
&& following_box_height == Some(source.line_height)
&& source.vertical_pos >= 0
&& (source
.vertical_pos
.checked_add(source.text_height)
.and_then(|end| end.checked_add(source.line_spacing))
== Some(next.vertical_pos)
|| next.vertical_pos < source.vertical_pos)
});
let generated_row = para.line_segs.get(line_index).is_some_and(|line| {
line.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
});
let owned_inline_row = (has_equation || has_form)
&& (generated_row || regenerated || para.line_segs.is_empty());
let stale_current_math_row = current_simple_row
&& !para.stored_text_partition_is_dirty()
&& line_count == para.line_segs.len()
&& para.line_segs.get(line_index).is_some_and(|source| {
source.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0
&& source.line_height == source.text_height
&& source.line_height > old_logical_height
});
if (changed
&& line.line_height == old_logical_height
&& (line.baseline_distance - old_ascent).abs() <= 1)
|| following_inline_box_was_reserved
|| owned_inline_row
|| stale_current_math_row
{
let occupied_height = new_ascent + new_descent;
let leading = if owned_inline_row {
let font_size = line
.runs
.iter()
.filter(|run| !run.text.is_empty())
.filter_map(|run| styles.char_styles.get(run.char_style_id as usize))
.map(|style| style.font_size)
.reduce(f64::max)
.or_else(|| {
para.char_shapes
.first()
.and_then(|shape| styles.char_styles.get(shape.char_shape_id as usize))
.map(|style| style.font_size)
})
.unwrap_or(12.0);
let ps = styles.para_styles.get(para.para_shape_id as usize);
super::frame_metrics_for_line(
font_size.max(hwpunit_to_px(spacing_object_height, dpi)),
font_size,
ps.map_or(crate::model::style::LineSpacingType::Percent, |style| {
style.line_spacing_type
}),
ps.map_or(160.0, |style| style.line_spacing),
dpi,
)
.line_spacing
} else {
line.line_spacing
};
line.line_spacing =
leading.saturating_add(new_logical_height.saturating_sub(occupied_height));
line.line_height = occupied_height;
line.baseline_distance = new_ascent;
debug_assert!(hwpunit_to_px(line.line_height, DEFAULT_DPI) > 0.0);
}
}
Ok(())
}
fn following_reserved_box_height(
para: &Paragraph,
positions: &[usize],
composed: &ComposedParagraph,
index: usize,
) -> Option<i32> {
let source = para.line_segs.get(index)?;
if source.line_height <= source.text_height || composed.lines.len() != para.line_segs.len() {
return None;
}
for next_index in index + 1..para.line_segs.len() {
let previous = ¶.line_segs[next_index - 1];
let next = ¶.line_segs[next_index];
if next.tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY != 0
|| next.vertical_pos < 0
|| !(previous
.vertical_pos
.checked_add(previous.text_height)
.and_then(|end| end.checked_add(previous.line_spacing))
== Some(next.vertical_pos)
|| next.vertical_pos < previous.vertical_pos)
{
return None;
}
if next.text_height == source.line_height {
let line = composed.lines.get(next_index)?;
return source_inline_box_height(
para,
positions,
line,
composed
.lines
.get(next_index + 1)
.map(|line| line.char_start),
)
.filter(|height| *height == source.line_height);
}
if next.line_height != source.line_height || next.text_height != source.text_height {
return None;
}
}
None
}
fn source_inline_box_height(
para: &Paragraph,
positions: &[usize],
line: &ComposedLine,
next_start: Option<usize>,
) -> Option<i32> {
let end = next_start.unwrap_or_else(|| para.text.chars().count());
para.controls
.iter()
.zip(positions)
.filter(|(_, position)| {
**position >= line.char_start
&& (**position < end || (next_start.is_none() && **position == end))
})
.filter_map(|(control, _)| match control {
Control::Equation(value) if value.common.treat_as_char => {
Some(value.common.height as i32)
}
Control::Picture(value) if value.common.treat_as_char => {
Some(value.common.height as i32)
}
_ => None,
})
.max()
.filter(|height| *height > 0)
}
fn try_refresh_single_tac_host_row<S: crate::storage::Storage>(
composed: &mut ComposedParagraph,
para: &Paragraph,
styles: &ResolvedStyleSet,
storage: &S,
) -> Result<(), S::Error> {
let form_height = match para.controls.as_slice() {
[Control::Equation(e)] if e.common.treat_as_char => 0,
[Control::Form(f)] if f.common.treat_as_char => f.height as i32,
_ => return Ok(()),
};
let generated_form = form_height > 0
&& composed.lines.len() == 1
&& para.line_segs.len() == 1
&& para.line_segs[0].tag & crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY
!= 0;
if ((!composed.lines.is_empty() || !para.line_segs.is_empty()) && !generated_form)
|| para
.text
.chars()
.any(|c| !c.is_whitespace() && !c.is_control() && c != '\u{fffc}')
{
return Ok(());
}
let fs = para
.char_shapes
.first()
.and_then(|s| styles.char_styles.get(s.char_shape_id as usize))
.map_or(12.0, |s| s.font_size);
let ps = styles.para_styles.get(para.para_shape_id as usize);
let metrics = super::frame_metrics_for_line(
fs,
fs,
ps.map_or(crate::model::style::LineSpacingType::Percent, |s| {
s.line_spacing_type
}),
ps.map_or(160.0, |s| s.line_spacing),
styles.resolved_dpi.unwrap_or(DEFAULT_DPI),
);
let line_height = metrics.line_height.max(form_height);
let baseline = metrics
.baseline_distance
.max(super::line_breaking::baseline_distance_hwp(form_height));
if generated_form {
let line = &mut composed.lines[0];
line.line_height = line_height;
line.baseline_distance = baseline;
line.line_spacing = metrics.line_spacing;
return Ok(());
}
storage.push(
&mut composed.lines,
ComposedLine {
runs: Vec::new(),
line_height,
baseline_distance: baseline,
segment_width: 0,
column_start: 0,
line_spacing: metrics.line_spacing,
has_line_break: false,
char_start: 0,
},
)?;
Ok(())
}
pub(super) fn publish_regenerated_rows(para: &mut Paragraph, styles: &ResolvedStyleSet) {
publish_equation_rows(para, styles);
super::line_breaking::publish_vertical_alignment_baselines(para, styles);
}
fn publish_equation_rows(para: &mut Paragraph, styles: &ResolvedStyleSet) {
let mut has_inline_equation = false;
let supported = para.controls.iter().all(|control| match control {
Control::Equation(equation) if equation.common.treat_as_char => {
has_inline_equation = true;
true
}
Control::SectionDef(_) | Control::ColumnDef(_) | Control::Bookmark(_) => true,
_ => false,
});
if !has_inline_equation || !supported {
return;
}
let mut composed = super::compose_paragraph(para);
refresh_rows(&mut composed, para, styles, true);
if composed.lines.len() != para.line_segs.len() {
return;
}
let mut rows = para.line_segs.clone();
let mut top = rows.first().map_or(0, |row| row.vertical_pos);
for (row, common) in rows.iter_mut().zip(composed.lines) {
row.vertical_pos = top;
row.line_height = common.line_height;
row.text_height = common.line_height;
row.baseline_distance = common.baseline_distance;
row.line_spacing = common.line_spacing;
row.tag |= crate::model::paragraph::LineSeg::TAG_IMPLEMENTATION_PROPERTY;
top = top
.saturating_add(row.line_height)
.saturating_add(row.line_spacing);
}
para.replace_line_segs(rows);
}