use super::Drawing;
use crate::{
element::{JsError, codes},
geometry::{Points, Size},
};
pub(crate) const MAX_SIDE: u32 = 16_384;
pub(crate) const MAX_PIXELS: u64 = 64 * 1024 * 1024;
pub(crate) const MAX_PENDING: usize = 16;
pub(crate) const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ImageFormat {
#[default]
Png,
}
impl ImageFormat {
pub fn from_mime(mime: &str) -> Option<Self> {
match mime.trim().to_ascii_lowercase().as_str() {
"" | "image/png" => Some(Self::Png),
_ => None,
}
}
pub fn mime(self) -> &'static str {
match self {
Self::Png => "image/png",
}
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct RenderRequest {
pub drawing: Drawing,
pub size: Size<Points>,
pub scale: Option<f32>,
pub format: ImageFormat,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RenderFailure {
TooLarge(String),
Failed(String),
}
impl From<String> for RenderFailure {
fn from(message: String) -> Self {
Self::Failed(message)
}
}
impl From<&str> for RenderFailure {
fn from(message: &str) -> Self {
Self::Failed(message.into())
}
}
impl RenderFailure {
pub(crate) fn into_error(self) -> JsError {
match self {
Self::TooLarge(message) => error(codes::QUOTA_EXCEEDED, &message),
Self::Failed(message) => JsError::Error(message),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[non_exhaustive]
pub struct RenderOptions {
pub scale: Option<f32>,
pub format: ImageFormat,
}
pub(crate) fn pixel_size(size: Size<Points>, scale: f32) -> Result<(u32, u32), JsError> {
let (width, height) = (size.width.0, size.height.0);
for (name, value) in [("width", width), ("height", height), ("scale", scale)] {
if !value.is_finite() || value <= 0.0 {
return Err(JsError::Range(format!(
"the {name} of an offscreen canvas must be a positive number, got {value}"
)));
}
}
let side = |points: f32| (f64::from(points) * f64::from(scale)).ceil();
let (w, h) = (side(width), side(height));
if w > f64::from(MAX_SIDE) || h > f64::from(MAX_SIDE) || w * h > MAX_PIXELS as f64 {
return Err(error(
codes::QUOTA_EXCEEDED,
&format!(
"a {w}×{h} pixel image is over the limit ({MAX_SIDE} pixels a side, {MAX_PIXELS} pixels)"
),
));
}
Ok((w as u32, h as u32))
}
pub(crate) fn error(code: &str, message: &str) -> JsError {
JsError::Error(message.into()).with_code(code)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::px;
#[test]
fn pixel_size_rounds_up_and_checks_limits() {
assert_eq!(
pixel_size(Size::new(px(100.0), px(50.5)), 2.0).unwrap(),
(200, 101)
);
assert_eq!(
pixel_size(Size::new(px(0.3), px(0.3)), 1.0).unwrap(),
(1, 1)
);
let range = pixel_size(Size::new(px(0.0), px(10.0)), 1.0).unwrap_err();
assert!(matches!(range, JsError::Range(_)));
assert!(matches!(
pixel_size(Size::new(px(10.0), px(10.0)), f32::NAN).unwrap_err(),
JsError::Range(_)
));
let side = pixel_size(Size::new(px(8193.0), px(10.0)), 2.0).unwrap_err();
assert_eq!(side.code(), Some(codes::QUOTA_EXCEEDED));
let area = pixel_size(Size::new(px(10_000.0), px(10_000.0)), 1.0).unwrap_err();
assert_eq!(area.code(), Some(codes::QUOTA_EXCEEDED));
assert_eq!(
pixel_size(Size::new(px(8192.0), px(8192.0)), 1.0).unwrap(),
(8192, 8192)
);
}
#[test]
fn formats_by_mime() {
assert_eq!(ImageFormat::from_mime(""), Some(ImageFormat::Png));
assert_eq!(ImageFormat::from_mime("IMAGE/PNG"), Some(ImageFormat::Png));
assert_eq!(ImageFormat::from_mime("image/webp"), None);
}
}