use super::render_tree::{BoundingBox, ImageNode, ShapeTransform};
use crate::model::image::Picture;
pub(crate) fn normalize_gif_crop(image: &mut ImageNode) {
if image
.original_size_hu
.is_some_and(|(width, height)| width > 0 && height > 0)
{
return;
}
let Some((left, top, right, bottom)) = image.crop else {
return;
};
if right <= left || bottom <= top {
return;
}
let Some(data) = image.data.as_deref() else {
return;
};
if !data.starts_with(b"GIF87a") && !data.starts_with(b"GIF89a") {
return;
}
let Some((width, height)) = super::svg::parse_image_dimensions(data) else {
return;
};
let Some((width, height)) = width
.checked_mul(75)
.zip(height.checked_mul(75))
.filter(|&(w, h)| w > 0 && h > 0 && w <= i32::MAX as u32 && h <= i32::MAX as u32)
else {
return;
};
let width_i = width as i32;
let height_i = height as i32;
image.crop = Some((
left.clamp(0, width_i),
top.clamp(0, height_i),
right.clamp(0, width_i),
bottom.clamp(0, height_i),
));
image.original_size_hu = Some((width, height));
}
#[derive(Debug, Clone, Copy, serde::Serialize)]
pub struct PictureSourceRect {
min_x: f64,
min_y: f64,
width: f64,
height: f64,
origin_x: f64,
origin_y: f64,
}
pub(crate) fn picture_source_rect(picture: &Picture) -> Option<PictureSourceRect> {
let shape = &picture.shape_attr;
if shape.group_level != 0
|| shape.original_width == 0
|| shape.original_height == 0
|| picture
.border_x
.iter()
.chain(&picture.border_y)
.all(|x| *x == 0)
{
return None;
}
let [x0, y0, x1, y1] = picture.border_x.map(f64::from);
let [x2, y2, x3, y3] = picture.border_y.map(f64::from);
let w = f64::from(shape.original_width);
let h = f64::from(shape.original_height);
let min_x = x0.min(x1).min(x2).min(x3);
let min_y = y0.min(y1).min(y2).min(y3);
let width = x0.max(x1).max(x2).max(x3) - min_x;
let height = y0.max(y1).max(y2).max(y3) - min_y;
if x0 == 0.0 && y0 == 0.0 && min_x == 0.0 && min_y == 0.0 && width == w && height == h {
return None;
}
Some(PictureSourceRect {
min_x: min_x / w,
min_y: min_y / h,
width: width / w,
height: height / h,
origin_x: x0 / w,
origin_y: y0 / h,
})
}
pub(crate) struct PicturePaintGeometry {
pub bitmap: BoundingBox,
pub viewport: BoundingBox,
pub clip: Option<[[f64; 2]; 4]>,
}
pub(crate) fn source_geometry(
base: BoundingBox,
transform: ShapeTransform,
source: Option<PictureSourceRect>,
) -> PicturePaintGeometry {
let Some(s) = source else {
return PicturePaintGeometry {
bitmap: base,
viewport: base,
clip: None,
};
};
let (sin, cos) = transform.rotation.to_radians().sin_cos();
let offset = |x: f64, y: f64| {
let x = x * base.width * if transform.horz_flip { -1.0 } else { 1.0 };
let y = y * base.height * if transform.vert_flip { -1.0 } else { 1.0 };
[cos * x - sin * y, sin * x + cos * y]
};
let w = base.width * s.width;
let h = base.height * s.height;
let [dx, dy] = offset(
s.min_x + s.origin_x + (s.width - 1.0) / 2.0,
s.min_y + s.origin_y + (s.height - 1.0) / 2.0,
);
let bitmap = BoundingBox::new(
base.x + (base.width - w) / 2.0 + dx,
base.y + (base.height - h) / 2.0 + dy,
w,
h,
);
let [vx, vy] = offset(
s.min_x + (s.width - 1.0) / 2.0,
s.min_y + (s.height - 1.0) / 2.0,
);
let cx = base.x + base.width / 2.0 + vx;
let cy = base.y + base.height / 2.0 + vy;
let viewport = BoundingBox::new(cx - w / 2.0, cy - h / 2.0, w, h);
let clip = [
[-w / 2.0, -h / 2.0],
[w / 2.0, -h / 2.0],
[w / 2.0, h / 2.0],
[-w / 2.0, h / 2.0],
]
.map(|[x, y]| [cx + cos * x - sin * y, cy + sin * x + cos * y]);
PicturePaintGeometry {
bitmap,
viewport,
clip: Some(clip),
}
}
pub(crate) fn picture_transform(picture: &Picture) -> ShapeTransform {
let shape = &picture.shape_attr;
let mut transform = crate::renderer::layout::extract_picture_transform(picture);
if shape.raw_rendering.is_empty() || shape.group_level != 0 {
return transform;
}
let u = [shape.render_sx, shape.render_c];
let v = [shape.render_b, shape.render_sy];
let (sin, cos) = transform.rotation.to_radians().sin_cos();
let frame_x = if (shape.current_width as i32) < 0 {
-1.0
} else {
1.0
};
let frame_y = if (shape.current_height as i32) < 0 {
-1.0
} else {
1.0
};
let along_u = (u[0] * cos + u[1] * sin) * frame_x;
let across_u = -u[0] * sin + u[1] * cos;
let along_v = (-v[0] * sin + v[1] * cos) * frame_y;
let across_v = v[0] * cos + v[1] * sin;
if ![along_u, across_u, along_v, across_v]
.iter()
.all(|x| x.is_finite())
|| along_u == 0.0
|| along_v == 0.0
|| across_u.abs() > along_u.abs() * 1e-6
|| across_v.abs() > along_v.abs() * 1e-6
{
return transform;
}
transform.horz_flip = along_u < 0.0;
transform.vert_flip = along_v < 0.0;
transform
}
pub(crate) fn picture_pre_rotation_ratio(picture: &Picture) -> Option<[f64; 2]> {
let shape = &picture.shape_attr;
let angle = shape.rotation_angle.rem_euclid(360);
let (w, h) = (
(shape.current_width as i32).unsigned_abs() as f64,
(shape.current_height as i32).unsigned_abs() as f64,
);
let (fw, fh) = (picture.common.width as f64, picture.common.height as f64);
if angle == 0 || w <= 0.0 || h <= 0.0 || fw <= 0.0 || fh <= 0.0 {
return None;
}
let (sin, cos) = (angle as f64).to_radians().sin_cos();
let expected_w = w * cos.abs() + h * sin.abs();
let expected_h = w * sin.abs() + h * cos.abs();
if (expected_w - fw).abs() > 1.0 || (expected_h - fh).abs() > 1.0 {
return None;
}
Some([w / fw, h / fh])
}
pub(crate) fn pre_rotation_bbox(
frame: &BoundingBox,
transform: ShapeTransform,
ratio: Option<[f64; 2]>,
) -> BoundingBox {
let Some([rx, ry]) = ratio else {
return transform.effective_image_bbox(frame);
};
let width = frame.width * rx;
let height = frame.height * ry;
BoundingBox::new(
frame.x + (frame.width - width) / 2.0,
frame.y + (frame.height - height) / 2.0,
width,
height,
)
}