use std::sync::Arc;
use geometry::Point;
use mln_style::bitmap::PremultipliedImage;
use mln_style::error::Error;
use mln_style::image::{ImageContent, ImageStretches, StyleImage, TextFit};
use mln_style::size::Size;
use mln_style::string::to_string_double;
use serde_json::{Map, Value as JSValue};
use crate::exec::pool::Respond;
use crate::image::decoder;
use crate::log_target;
use crate::storage::Data;
#[expect(
clippy::too_many_arguments,
reason = "the sprite sheet, the image's rectangle, and the metadata of one sprite entry"
)]
#[must_use]
pub(crate) fn create_style_image(
id: &str,
image: &PremultipliedImage,
src_x: i32,
src_y: i32,
width: i32,
height: i32,
ratio: f64,
sdf: bool,
stretch_x: ImageStretches,
stretch_y: ImageStretches,
content: Option<ImageContent>,
text_fit_width: Option<TextFit>,
text_fit_height: Option<TextFit>,
) -> Option<StyleImage> {
if width <= 0
|| height <= 0
|| width > 1024
|| height > 1024
|| ratio <= 0.0
|| ratio > 10.0
|| src_x < 0
|| src_y < 0
|| i64::from(src_x) + i64::from(width) > i64::from(image.size.width)
|| i64::from(src_y) + i64::from(height) > i64::from(image.size.height)
{
tracing::error!(
target: log_target::SPRITE,
width,
height,
x = src_x,
y = src_y,
image_width = image.size.width,
image_height = image.size.height,
ratio = %to_string_double(ratio),
"Can't create an image with invalid metrics"
);
return None;
}
let size = Size::new(width as u32, height as u32);
let mut dst_image = PremultipliedImage::new(size);
PremultipliedImage::copy(image, &mut dst_image, Point::new(src_x as u32, src_y as u32), Point::new(0, 0), size)
.expect("sprite metrics were validated");
match StyleImage::new(
id.to_owned(),
dst_image,
ratio as f32,
sdf,
stretch_x,
stretch_y,
content,
text_fit_width,
text_fit_height,
) {
Ok(image) => Some(image),
Err(ex) => {
tracing::error!(target: log_target::SPRITE, error = %ex, "Can't create an image with invalid metadata");
None
}
}
}
fn warn(name: &str, message: std::fmt::Arguments<'_>) {
tracing::warn!(target: log_target::SPRITE, name, error = %message, "Invalid sprite image");
}
fn member<'a, T>(
entry: &'a Map<String, JSValue>,
name: &str,
property: &str,
must: &str,
read: impl FnOnce(&'a JSValue) -> Option<T>,
) -> Option<T> {
let value = read(entry.get(property)?);
if value.is_none() {
warn(name, format_args!("value of '{property}' must be {must}"));
}
value
}
fn integer(entry: &Map<String, JSValue>, name: &str, property: &str) -> u16 {
member(entry, name, property, "an integer between 0 and 65535", |value| {
value.as_u64().and_then(|value| u16::try_from(value).ok())
})
.unwrap_or(0)
}
fn stretches(entry: &Map<String, JSValue>, name: &str, property: &str) -> ImageStretches {
let Some(pairs) = member(entry, name, property, "an array", JSValue::as_array) else {
return ImageStretches::new();
};
pairs
.iter()
.filter_map(|pair| {
let stretch = match pair.as_array().map(Vec::as_slice) {
Some([start, end]) => start.as_f64().zip(end.as_f64()),
_ => None,
};
if stretch.is_none() {
warn(name, format_args!("members of '{property}' must be an array of two numbers"));
}
stretch.map(|(start, end)| (start as f32, end as f32))
})
.collect()
}
fn content(entry: &Map<String, JSValue>, name: &str, property: &str) -> Option<ImageContent> {
member(entry, name, property, "an array of four numbers", |value| {
let [left, top, right, bottom] = value.as_array()?.as_slice() else { return None };
let side = |value: &JSValue| value.as_f64().map(|value| value as f32);
Some(ImageContent { left: side(left)?, top: side(top)?, right: side(right)?, bottom: side(bottom)? })
})
}
fn text_fit(entry: &Map<String, JSValue>, name: &str, property: &str) -> Option<TextFit> {
let value = member(entry, name, property, "a string", JSValue::as_str)?;
let text_fit = match value {
"stretchOrShrink" => Some(TextFit::StretchOrShrink),
"stretchOnly" => Some(TextFit::StretchOnly),
"proportional" => Some(TextFit::Proportional),
_ => None,
};
if text_fit.is_none() {
warn(name, format_args!("value of '{property}' is an invalid value '{value}'"));
}
text_fit
}
#[expect(clippy::needless_pass_by_value, reason = "a job owns its inputs, as one sent to another thread does")]
pub fn load_sprite(id: String, image: Data, json: Data, respond: Respond<Result<Vec<Arc<StyleImage>>, Error>>) {
decoder::decode(&image, move |raster| respond(raster.and_then(|raster| parse_sprite(&id, &raster, &json))));
}
pub fn parse_sprite(id: &str, image: &PremultipliedImage, json: &[u8]) -> Result<Vec<Arc<StyleImage>>, Error> {
let doc: JSValue = serde_json::from_slice(json).map_err(|e| Error::failed(format!("Failed to parse JSON: {e}")))?;
let JSValue::Object(entries) = doc else {
return Err(Error::failed("Sprite JSON root must be an object"));
};
let mut images = Vec::with_capacity(entries.len());
for (name, value) in &entries {
let Some(entry) = value.as_object() else { continue };
let complete_name = if id == "default" { name.clone() } else { format!("{id}:{name}") };
let image = create_style_image(
&complete_name,
image,
i32::from(integer(entry, name, "x")),
i32::from(integer(entry, name, "y")),
i32::from(integer(entry, name, "width")),
i32::from(integer(entry, name, "height")),
member(entry, name, "pixelRatio", "a number", JSValue::as_f64).unwrap_or(1.0),
member(entry, name, "sdf", "a boolean", JSValue::as_bool).unwrap_or(false),
stretches(entry, name, "stretchX"),
stretches(entry, name, "stretchY"),
content(entry, name, "content"),
text_fit(entry, name, "textFitWidth"),
text_fit(entry, name, "textFitHeight"),
);
if let Some(image) = image {
images.push(Arc::new(image));
}
}
Ok(images)
}