use std::str::FromStr;
use crate::error::{ImageError, Result, invalid};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Alignment {
Top,
TopRight,
Right,
BottomRight,
Bottom,
BottomLeft,
Left,
#[default]
TopLeft,
Center,
}
impl Alignment {
pub fn create(identifier: &str) -> Result<Alignment> {
use Alignment::*;
let alignment = match identifier.to_lowercase().as_str() {
"top" | "top-center" | "top_center" | "topcenter" | "center-top" | "center_top" | "centertop"
| "top-middle" | "top_middle" | "topmiddle" | "middle-top" | "middle_top" | "middletop" => Top,
"top-right" | "top_right" | "topright" | "right-top" | "right_top" | "righttop" => TopRight,
"right" | "right-center" | "right_center" | "rightcenter" | "center-right" | "center_right"
| "centerright" | "right-middle" | "right_middle" | "rightmiddle" | "middle-right" | "middle_right"
| "middleright" => Right,
"bottom-right" | "bottom_right" | "bottomright" | "right-bottom" | "right_bottom" | "rightbottom" => {
BottomRight
}
"bottom" | "bottom-center" | "bottom_center" | "bottomcenter" | "center-bottom" | "center_bottom"
| "centerbottom" | "bottom-middle" | "bottom_middle" | "bottommiddle" | "middle-bottom"
| "middle_bottom" | "middlebottom" => Bottom,
"bottom-left" | "bottom_left" | "bottomleft" | "left-bottom" | "left_bottom" | "leftbottom" => BottomLeft,
"left" | "left-center" | "left_center" | "leftcenter" | "center-left" | "center_left" | "centerleft"
| "left-middle" | "left_middle" | "leftmiddle" | "middle-left" | "middle_left" | "middleleft" => Left,
"top-left" | "top_left" | "topleft" | "left-top" | "left_top" | "lefttop" => TopLeft,
"center" | "middle" | "center-center" | "center_center" | "centercenter" | "center-middle"
| "center_middle" | "centermiddle" | "middle-center" | "middle_center" | "middlecenter" => Center,
_ => {
return Err(invalid(format!(
"Unable to create Intervention\\Image\\Alignment from \"{identifier}\""
)));
}
};
Ok(alignment)
}
pub fn value(self) -> &'static str {
match self {
Alignment::Top => "top",
Alignment::TopRight => "top-right",
Alignment::Right => "right",
Alignment::BottomRight => "bottom-right",
Alignment::Bottom => "bottom",
Alignment::BottomLeft => "bottom-left",
Alignment::Left => "left",
Alignment::TopLeft => "top-left",
Alignment::Center => "center",
}
}
}
impl FromStr for Alignment {
type Err = ImageError;
fn from_str(identifier: &str) -> Result<Self> {
Alignment::create(identifier)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Direction {
#[default]
Horizontal,
Vertical,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Fraction {
Full,
Half,
Third,
TwoThirds,
Quarter,
ThreeQuarter,
OneAndAHalf,
Double,
Triple,
}
impl Fraction {
pub fn multiplier(self) -> f64 {
match self {
Fraction::Full => 1.0,
Fraction::Half => 0.5,
Fraction::Third => 0.333_333_333_3,
Fraction::TwoThirds => 0.666_666_666_7,
Fraction::Quarter => 0.25,
Fraction::ThreeQuarter => 0.75,
Fraction::OneAndAHalf => 1.5,
Fraction::Double => 2.0,
Fraction::Triple => 3.0,
}
}
pub fn of(self, value: f64) -> f64 {
let scaled = value * self.multiplier();
(scaled * 1e9).round() / 1e9
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Dimension {
#[default]
Auto,
Pixels(i64),
Fraction(Fraction),
}
impl From<i32> for Dimension {
fn from(value: i32) -> Self {
Dimension::Pixels(i64::from(value))
}
}
impl From<u32> for Dimension {
fn from(value: u32) -> Self {
Dimension::Pixels(i64::from(value))
}
}
impl From<Fraction> for Dimension {
fn from(value: Fraction) -> Self {
Dimension::Fraction(value)
}
}
impl From<Option<u32>> for Dimension {
fn from(value: Option<u32>) -> Self {
value.map_or(Dimension::Auto, Into::into)
}
}
impl Dimension {
pub(crate) fn resolve(self, current: u32) -> Option<i64> {
match self {
Dimension::Auto => None,
Dimension::Pixels(pixels) => Some(pixels),
Dimension::Fraction(fraction) => Some(fraction.of(f64::from(current)).round() as i64),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Point {
pub x: i64,
pub y: i64,
}
impl Point {
pub fn new(x: i64, y: i64) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Size {
width: i64,
height: i64,
pivot: Point,
}
fn php_round_half(value: i64) -> i64 {
(value as f64 / 2.0).round() as i64
}
impl Size {
pub fn new(width: i64, height: i64) -> Result<Size> {
if width < 0 {
return Err(invalid(
"Width of Intervention\\Image\\Size must be greater than or equal to 0",
));
}
if height < 0 {
return Err(invalid(
"Height of Intervention\\Image\\Size must be greater than or equal to 0",
));
}
Ok(Size {
width,
height,
pivot: Point::default(),
})
}
pub(crate) fn of(width: u32, height: u32) -> Size {
Size {
width: i64::from(width),
height: i64::from(height),
pivot: Point::default(),
}
}
pub fn width(&self) -> u32 {
self.width.max(0) as u32
}
pub fn height(&self) -> u32 {
self.height.max(0) as u32
}
pub fn pivot(&self) -> Point {
self.pivot
}
pub fn aspect_ratio(&self) -> f64 {
self.width as f64 / self.height as f64
}
pub fn is_landscape(&self) -> bool {
self.width > self.height
}
pub fn is_portrait(&self) -> bool {
self.width < self.height
}
pub fn fits_within(&self, other: &Size) -> bool {
self.width <= other.width && self.height <= other.height
}
pub fn move_pivot(&mut self, alignment: Alignment, x: i64, y: i64) -> &mut Self {
let (w, h) = (self.width, self.height);
self.pivot = match alignment {
Alignment::Top => Point::new(php_round_half(w) + x, y),
Alignment::TopRight => Point::new(w - x, y),
Alignment::Left => Point::new(x, php_round_half(h) + y),
Alignment::Right => Point::new(w - x, php_round_half(h) + y),
Alignment::BottomLeft => Point::new(x, h - y),
Alignment::Bottom => Point::new(php_round_half(w) + x, h - y),
Alignment::BottomRight => Point::new(w - x, h - y),
Alignment::Center => Point::new(php_round_half(w) + x, php_round_half(h) + y),
Alignment::TopLeft => Point::new(x, y),
};
self
}
pub fn align_pivot_to(&mut self, size: &Size, alignment: Alignment) -> &mut Self {
let mut reference = Size {
width: size.width,
height: size.height,
pivot: Point::default(),
};
reference.move_pivot(alignment, 0, 0);
self.move_pivot(alignment, 0, 0);
self.pivot = reference.offset_to(self);
self
}
pub fn offset_to(&self, size: &Size) -> Point {
Point::new(self.pivot.x - size.pivot.x, self.pivot.y - size.pivot.y)
}
}
pub(crate) struct Resizer {
width: Option<i64>,
height: Option<i64>,
}
impl Resizer {
pub(crate) fn new(width: Option<i64>, height: Option<i64>) -> Result<Resizer> {
if width.is_some_and(|width| width < 1) {
return Err(invalid("Width must be greater than or equal to 1"));
}
if height.is_some_and(|height| height < 1) {
return Err(invalid("Height must be greater than or equal to 1"));
}
Ok(Resizer { width, height })
}
fn target(&self) -> Result<Size> {
match (self.width, self.height) {
(Some(width), Some(height)) => Size::new(width, height),
_ => Err(invalid("Target size needs width and height")),
}
}
fn proportional_width(&self, size: &Size) -> i64 {
match self.height {
None => size.width,
Some(height) => ((height as f64 * size.aspect_ratio()).round() as i64).max(1),
}
}
fn proportional_height(&self, size: &Size) -> i64 {
match self.width {
None => size.height,
Some(width) => ((width as f64 / size.aspect_ratio()).round() as i64).max(1),
}
}
pub(crate) fn resize(&self, size: &Size) -> Size {
Size::of(0, 0).with(self.width.unwrap_or(size.width), self.height.unwrap_or(size.height))
}
pub(crate) fn resize_down(&self, size: &Size) -> Size {
Size::of(0, 0).with(
self.width.map_or(size.width, |width| width.min(size.width)),
self.height.map_or(size.height, |height| height.min(size.height)),
)
}
pub(crate) fn scale(&self, size: &Size) -> Size {
let (w, h) = match (self.width, self.height) {
(Some(width), Some(height)) => (
self.proportional_width(size).min(width),
self.proportional_height(size).min(height),
),
(Some(width), None) => (width, self.proportional_height(size)),
(None, Some(height)) => (self.proportional_width(size), height),
(None, None) => (size.width, size.height),
};
Size::of(0, 0).with(w, h)
}
pub(crate) fn scale_down(&self, size: &Size) -> Size {
let (w, h) = match (self.width, self.height) {
(Some(width), Some(height)) => (
self.proportional_width(size).min(width).min(size.width),
self.proportional_height(size).min(height).min(size.height),
),
(Some(width), None) => (width.min(size.width), self.proportional_height(size).min(size.height)),
(None, Some(height)) => (self.proportional_width(size).min(size.width), height.min(size.height)),
(None, None) => (size.width, size.height),
};
Size::of(0, 0).with(w, h)
}
pub(crate) fn contain(&self, size: &Size) -> Result<Size> {
let target = self.target()?;
let mut resized = Size::of(0, 0).with(target.width, self.proportional_height(size));
if !resized.fits_within(&target) {
resized = resized.with(self.proportional_width(size), target.height);
}
Ok(resized)
}
pub(crate) fn contain_down(&self, size: &Size) -> Result<Size> {
let target = self.target()?;
let mut resized = Size::of(0, 0).with(
size.width.min(target.width),
size.height.min(self.proportional_height(size)),
);
if !resized.fits_within(&target) {
resized = resized.with(
size.width.min(self.proportional_width(size)),
size.height.min(target.height),
);
}
Ok(resized)
}
}
impl Size {
fn with(mut self, width: i64, height: i64) -> Size {
self.width = width;
self.height = height;
self
}
}