use crate::style::{StyleError, box_values::unsupported, component::Component, grammar::Keyword};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize)]
pub(crate) enum ObjectFit {
#[default]
Fill,
Contain,
Cover,
None,
ScaleDown,
}
impl ObjectFit {
pub(crate) fn from_component(value: &Component) -> Result<Self, StyleError> {
match value.keyword() {
Some(Keyword::Fill) => Ok(Self::Fill),
Some(Keyword::Contain) => Ok(Self::Contain),
Some(Keyword::Cover) => Ok(Self::Cover),
Some(Keyword::None) => Ok(Self::None),
Some(Keyword::ScaleDown) => Ok(Self::ScaleDown),
_ => Err(unsupported(value)),
}
}
pub(crate) fn size(self, intrinsic: [f32; 2], bounds: [f32; 2]) -> [f32; 2] {
let [width, height] = intrinsic;
let scale = match self {
Self::Fill => return bounds,
Self::None => 1.0,
Self::Contain => (bounds[0] / width).min(bounds[1] / height),
Self::Cover => (bounds[0] / width).max(bounds[1] / height),
Self::ScaleDown => (bounds[0] / width).min(bounds[1] / height).min(1.0),
};
[width * scale, height * scale]
}
}
const SOURCE_SLOT: usize = 0;
const FROM_IMAGE_SLOT: usize = 0;
const RESOLUTION_SLOT: usize = 1;
const INITIAL_DPPX: f32 = 1.0;
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize)]
pub(crate) struct ImageResolution {
pub from_image: bool,
pub dppx: Option<f32>,
}
impl Default for ImageResolution {
fn default() -> Self {
Self {
from_image: false,
dppx: Some(INITIAL_DPPX),
}
}
}
impl ImageResolution {
pub(crate) fn from_component(
value: &Component,
relative: &crate::style::units::Relative<'_>,
) -> Result<Self, StyleError> {
let source = value.slot(SOURCE_SLOT).ok_or_else(|| unsupported(value))?;
let dppx = source
.slot(RESOLUTION_SLOT)
.map(|resolution| crate::style::values::resolution(resolution, relative))
.transpose()?;
if dppx.is_some_and(|dppx| dppx <= 0.0) {
return Err(StyleError::Invalid(format!(
"non-positive image-resolution: {value}"
)));
}
Ok(Self {
from_image: source.slot(FROM_IMAGE_SLOT).is_some(),
dppx,
})
}
pub(crate) fn density(self, recorded: Option<[f32; 2]>) -> [f32; 2] {
match recorded.filter(|_| self.from_image) {
Some(recorded) => recorded,
None => [self.dppx.unwrap_or(INITIAL_DPPX); 2],
}
}
}
const ANGLE_SLOT: usize = 0;
const FLIP_SLOT: usize = 1;
const QUARTER_TURN_DEG: f32 = 90.0;
const QUARTER_TURNS: f32 = 4.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize)]
pub(crate) enum ImageOrientation {
#[default]
FromImage,
None,
Turned {
turns: u8,
flip: bool,
},
}
impl ImageOrientation {
pub(crate) fn from_component(
value: &Component,
relative: &crate::style::units::Relative<'_>,
) -> Result<Self, StyleError> {
match value.keyword() {
Some(Keyword::FromImage) => return Ok(Self::FromImage),
Some(Keyword::None) => return Ok(Self::None),
_ => {}
}
let degrees = value
.slot(ANGLE_SLOT)
.map(|angle| crate::style::values::angle(angle, relative))
.transpose()?
.unwrap_or_default();
let turns = (degrees / QUARTER_TURN_DEG + 0.5)
.floor()
.rem_euclid(QUARTER_TURNS) as u8;
Ok(Self::Turned {
turns,
flip: value.slot(FLIP_SLOT).is_some(),
})
}
}