use super::{InlineFlowContent, InlineFlowPlan};
use crate::model::{
control::Control,
paragraph::Paragraph,
shape::{TextWrap, VertAlign},
};
use crate::renderer::{
float_placement::{ObjectPlacementFrame, ParagraphFloatFlow, ParagraphFloatPlacement},
height_measurer::MeasuredTable,
hwpunit_to_px,
layout_frame::{FrameExclusion, FrameExclusionPolicy},
px_to_hwpunit,
style_resolver::ResolvedStyleSet,
};
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FloatingShapePlacement {
pub control: usize,
pub placement: ParagraphFloatPlacement,
pub exclusion: Option<FrameExclusion>,
}
crate::storage::clone_fields!(FloatingShapePlacement, {control, placement, exclusion});
pub(super) fn supported(control: &Control) -> bool {
let Control::Shape(shape) = control else {
return false;
};
let c = shape.common();
!c.treat_as_char
&& c.width > 0
&& c.width <= i32::MAX as u32
&& c.height > 0
&& c.height <= i32::MAX as u32
&& matches!(c.vert_align, VertAlign::Top | VertAlign::Inside)
&& matches!(
c.text_wrap,
TextWrap::TopAndBottom | TextWrap::BehindText | TextWrap::InFrontOfText
)
&& matches!(
shape.as_ref(),
crate::model::shape::ShapeObject::Rectangle(_)
)
&& crate::renderer::composer::shape_caption_for_layout(shape).is_none()
&& shape
.drawing()
.and_then(|drawing| drawing.text_box.as_ref())
.is_none_or(|text| {
text.paragraphs.iter().all(|para| {
para.controls
.iter()
.all(crate::renderer::composer::control_is_width_neutral_marker)
})
})
}
pub(crate) fn resolve(
plan: InlineFlowPlan,
para: &Paragraph,
para_index: usize,
styles: &ResolvedStyleSet,
tables: &[MeasuredTable],
frame: &ObjectPlacementFrame<'_>,
preceding: &[FrameExclusion],
) -> Option<InlineFlowPlan> {
match resolve_with_storage(
plan,
para,
para_index,
styles,
tables,
frame,
preceding,
&crate::storage::Unbounded,
) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn resolve_with_storage<S: crate::storage::Storage>(
mut plan: InlineFlowPlan,
para: &Paragraph,
para_index: usize,
styles: &ResolvedStyleSet,
tables: &[MeasuredTable],
frame: &ObjectPlacementFrame<'_>,
preceding: &[FrameExclusion],
storage: &S,
) -> Result<Option<InlineFlowPlan>, S::Error> {
crate::storage::with_optional(|failure| {
if !para.controls.iter().any(supported) {
return Some(plan);
}
let mut placements = Vec::new();
let mut exclusions =
crate::storage::optional(storage.collect(preceding.iter().cloned()), failure)?;
for item in &plan.boxes {
let InlineFlowContent::FloatingShape { control } = item.content else {
continue;
};
let Control::Shape(shape) = ¶.controls[control] else {
return None;
};
let c = shape.common();
let m = &c.margin;
let width = hwpunit_to_px(
i32::try_from(i64::from(c.width) + i64::from(m.left) + i64::from(m.right)).ok()?,
frame.dpi,
);
let height = hwpunit_to_px(
i32::try_from(i64::from(c.height) + i64::from(m.top) + i64::from(m.bottom)).ok()?,
frame.dpi,
);
let anchored = ObjectPlacementFrame {
paragraph_y: item.y,
..*frame
};
let (left, top) = anchored.position(c, width, height);
if ![left, top, width, height].iter().all(|n| n.is_finite()) {
return None;
}
let exclusion = (c.text_wrap == TextWrap::TopAndBottom
&& left < frame.column.x + frame.column.width
&& left + width > frame.column.x)
.then(|| FrameExclusion {
horizontal: px_to_hwpunit(frame.column.x, frame.dpi)
..px_to_hwpunit(frame.column.x + frame.column.width, frame.dpi),
vertical: px_to_hwpunit(top, frame.dpi)..px_to_hwpunit(top + height, frame.dpi),
policy: FrameExclusionPolicy::BothSides,
});
if let Some(band) = &exclusion {
crate::storage::optional(storage.push(&mut exclusions, band.clone()), failure)?;
}
crate::storage::optional(
storage.push(
&mut placements,
FloatingShapePlacement {
control,
placement: ParagraphFloatPlacement {
flow: ParagraphFloatFlow::AbsolutePageFrame,
anchor_y: item.y,
table_left: Some(left + hwpunit_to_px(i32::from(m.left), frame.dpi)),
table_top: top + hwpunit_to_px(i32::from(m.top), frame.dpi),
occupied_bottom: top + height,
stored_host_origin: None,
stored_successor_line_origin: None,
},
exclusion,
},
),
failure,
)?;
}
if placements.is_empty() {
return Some(plan);
}
if exclusions.len() > preceding.len() {
plan = crate::storage::optional(
super::plan_with_storage(
para,
para_index,
styles,
tables,
frame,
&exclusions,
storage,
),
failure,
)??;
}
plan.floating_shapes = placements;
Some(plan)
})
}
pub(super) fn anchor(control: usize) -> super::InlineFlowBox {
super::InlineFlowBox {
content: InlineFlowContent::FloatingShape { control },
x: 0.0,
y: 0.0,
width: 0.0,
height: 0.0,
baseline: 0.0,
}
}
pub(super) fn relative_to(items: &mut [FloatingShapePlacement], y: f64) {
for item in items {
let p = &mut item.placement;
p.anchor_y -= y;
p.table_top -= y;
p.occupied_bottom -= y;
}
}