use super::{element_max_x, element_max_y, Node, Rect, SVGPlayer};
use crate::wmf::imports::BTreeMap;
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct DeviceClipBounds {
left: f32,
top: f32,
right: f32,
bottom: f32,
}
impl DeviceClipBounds {
fn intersect(self, other: Self) -> Self {
let left = self.left.max(other.left);
let top = self.top.max(other.top);
Self {
left,
top,
right: self.right.min(other.right).max(left),
bottom: self.bottom.min(other.bottom).max(top),
}
}
fn is_empty(self) -> bool {
self.left >= self.right || self.top >= self.bottom
}
}
impl SVGPlayer {
pub(super) fn install_device_clip(&mut self, record: usize, rect: &Rect, intersect: bool) {
let (x1, y1) = self
.context_current
.window
.to_device(f32::from(rect.left), f32::from(rect.top));
let (x2, y2) = self
.context_current
.window
.to_device(f32::from(rect.right), f32::from(rect.bottom));
let mut bounds = DeviceClipBounds {
left: x1.min(x2),
top: y1.min(y2),
right: x1.max(x2),
bottom: y1.max(y2),
};
if intersect {
if let Some(previous) = self
.current_clip_id
.as_ref()
.and_then(|id| self.clip_bounds.get(id))
{
bounds = previous.intersect(bounds);
}
}
if self
.current_clip_id
.as_ref()
.and_then(|id| self.clip_bounds.get(id))
== Some(&bounds)
{
return;
}
let id = format!("clip{record}");
let rectangle = Node::new("rect")
.set("x", bounds.left)
.set("y", bounds.top)
.set("width", bounds.right - bounds.left)
.set("height", bounds.bottom - bounds.top);
self.definitions.push(
Node::new("clipPath")
.set("id", &id)
.set("clipPathUnits", "userSpaceOnUse")
.add(rectangle),
);
self.clip_bounds.insert(id.clone(), bounds);
self.current_clip_id = Some(id);
}
}
pub(super) fn visible_element_maxima(
node: &Node,
clips: &BTreeMap<String, DeviceClipBounds>,
) -> (Option<i32>, Option<i32>) {
let x = element_max_x(node);
let y = element_max_y(node);
let clip = node
.shared_clip_reference()
.and_then(|reference| reference.strip_prefix("url(#"))
.and_then(|reference| reference.strip_suffix(')'))
.and_then(|id| clips.get(id));
let Some(clip) = clip else {
return (x, y);
};
let left = node.attr("x").and_then(|x| x.parse::<f32>().ok());
let top = node.attr("y").and_then(|y| y.parse::<f32>().ok());
if clip.is_empty()
|| x.is_some_and(|right| right as f32 <= clip.left)
|| y.is_some_and(|bottom| bottom as f32 <= clip.top)
|| left.is_some_and(|left| left >= clip.right)
|| top.is_some_and(|top| top >= clip.bottom)
{
return (None, None);
}
(
x.map(|right| right.min(clip.right.ceil() as i32)),
y.map(|bottom| bottom.min(clip.bottom.ceil() as i32)),
)
}