use crate::model::image::Picture;
pub(crate) fn inline_picture_size_hu(picture: &Picture) -> (i32, i32) {
crate::renderer::stored_picture_metrics::stored_picture_size_hu(picture)
}
pub(crate) fn inline_picture_occupied_size_hu(picture: &Picture) -> (i32, i32) {
let (width, height) = inline_picture_size_hu(picture);
let margin = &picture.common.margin;
(
width
.saturating_add(i32::from(margin.left))
.saturating_add(i32::from(margin.right)),
height
.saturating_add(i32::from(margin.top))
.saturating_add(i32::from(margin.bottom)),
)
}
pub(crate) fn inline_picture_ascent_ratio(vertical_alignment: u8) -> f64 {
match vertical_alignment {
1 => 0.0,
2 => 0.5,
3 => 1.0,
_ => 0.85,
}
}
pub(super) fn refresh_picture_rows_with_storage<S: crate::storage::Storage>(
composed: &mut super::ComposedParagraph,
para: &crate::model::paragraph::Paragraph,
styles: &crate::renderer::style_resolver::ResolvedStyleSet,
storage: &S,
) -> Result<(), S::Error> {
use crate::model::control::Control;
use crate::renderer::{hwpunit_to_px, px_to_hwpunit, DEFAULT_DPI};
if !para.line_segs.is_empty() {
return Ok(());
}
let Some(style) = styles.para_styles.get(para.para_shape_id as usize) else {
return Ok(());
};
if style.vertical_alignment != 0
|| !para.controls.iter().all(|control| match control {
Control::Picture(picture) => picture.common.treat_as_char && picture.caption.is_none(),
Control::Footnote(_) | Control::Endnote(_) => true,
other => super::control_is_width_neutral_marker(other),
})
{
return Ok(());
}
let dpi = styles.resolved_dpi.unwrap_or(DEFAULT_DPI);
let count = composed.lines.len();
for index in 0..count {
let end = composed
.lines
.get(index + 1)
.map_or(para.text.chars().count(), |line| line.char_start);
let line = &mut composed.lines[index];
let pictures: Vec<_> = storage.collect(
composed
.tac_controls
.iter()
.filter(|(position, _, _)| {
*position >= line.char_start
&& (*position < end || (*position == end && index + 1 == count))
})
.filter_map(|(_, _, control)| match para.controls.get(*control)? {
Control::Picture(picture) => Some(picture),
_ => None,
}),
)?;
if pictures.is_empty() {
continue;
}
let font = crate::renderer::composed_line_max_font_size(line, para, styles);
if !font.is_finite() || font <= 0.0 {
continue;
}
let text_height = px_to_hwpunit(font, dpi);
let mut ascent = (f64::from(text_height) * 0.85).round() as i32;
let mut descent = text_height - ascent;
let mut spacing_height = text_height;
for picture in pictures {
let margin = &picture.common.margin;
let height = inline_picture_size_hu(picture)
.1
.saturating_add(i32::from(margin.top))
.saturating_add(i32::from(margin.bottom));
let baseline = (f64::from(height)
* inline_picture_ascent_ratio(style.vertical_alignment))
.round() as i32;
ascent = ascent.max(baseline);
descent = descent.max(height - baseline);
if picture.common.affect_line_spacing {
spacing_height = spacing_height.max(height);
}
}
if matches!(
style.line_spacing_type,
crate::model::style::LineSpacingType::Fixed
| crate::model::style::LineSpacingType::Minimum
) {
spacing_height = ascent + descent;
}
let metrics = super::frame_metrics_for_line(
hwpunit_to_px(spacing_height, dpi),
font,
style.line_spacing_type,
style.line_spacing,
dpi,
);
line.line_height = ascent + descent;
line.baseline_distance = ascent;
line.line_spacing = metrics.line_spacing;
}
Ok(())
}