use crate::cell::utils::{column_index_from_string, get_column_letter};
use crate::exceptions::{Error, Result};
use crate::styles::colors::Color;
use crate::units::{emu_to_pixels, pixels_to_emu, short_color};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Shadow {
pub visible: bool,
pub blur_radius: i64,
pub distance: i64,
pub direction: i64,
pub alignment: String,
pub color: Color,
pub alpha: i64,
}
impl Default for Shadow {
fn default() -> Self {
Shadow {
visible: false,
blur_radius: 6,
distance: 2,
direction: 0,
alignment: Shadow::SHADOW_BOTTOM_RIGHT.to_string(),
color: Color::new(Color::BLACK),
alpha: 50,
}
}
}
impl Shadow {
pub const SHADOW_BOTTOM: &'static str = "b";
pub const SHADOW_BOTTOM_LEFT: &'static str = "bl";
pub const SHADOW_BOTTOM_RIGHT: &'static str = "br";
pub const SHADOW_CENTER: &'static str = "ctr";
pub const SHADOW_LEFT: &'static str = "l";
pub const SHADOW_TOP: &'static str = "t";
pub const SHADOW_TOP_LEFT: &'static str = "tl";
pub const SHADOW_TOP_RIGHT: &'static str = "tr";
pub fn new() -> Self {
Shadow::default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AnchorType {
#[default]
Absolute,
OneCell,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Drawing {
pub name: String,
pub description: String,
pub coordinates: ((i64, i64), (i64, i64)),
pub left: i64,
pub top: i64,
width: i64,
height: i64,
pub resize_proportional: bool,
pub rotation: i64,
pub anchor_type: AnchorType,
pub anchor_col: Option<i64>,
pub anchor_row: Option<i64>,
}
impl Default for Drawing {
fn default() -> Self {
Drawing {
name: String::new(),
description: String::new(),
coordinates: ((1, 2), (16, 8)),
left: 0,
top: 0,
width: emu_to_pixels(200_000),
height: emu_to_pixels(1_828_800),
resize_proportional: false,
rotation: 0,
anchor_type: AnchorType::Absolute,
anchor_col: None,
anchor_row: None,
}
}
}
impl Drawing {
pub fn new() -> Self {
Drawing::default()
}
pub fn width(&self) -> i64 {
self.width
}
pub fn set_width(&mut self, width: i64) {
if self.resize_proportional && width != 0 {
let ratio = self.height as f64 / self.width as f64;
self.height = (ratio * width as f64).round() as i64;
}
self.width = width;
}
pub fn height(&self) -> i64 {
self.height
}
pub fn set_height(&mut self, height: i64) {
if self.resize_proportional && height != 0 {
let ratio = self.width as f64 / self.height as f64;
self.width = (ratio * height as f64).round() as i64;
}
self.height = height;
}
pub fn set_dimension(&mut self, width: i64, height: i64) {
if !self.resize_proportional || width == 0 || height == 0 {
return;
}
let x_ratio = width as f64 / self.width as f64;
let y_ratio = height as f64 / self.height as f64;
if x_ratio * (self.height as f64) < height as f64 {
self.height = (x_ratio * self.height as f64).ceil() as i64;
self.width = width;
} else {
self.width = (y_ratio * self.width as f64).ceil() as i64;
self.height = height;
}
}
pub fn emu_dimensions(&self) -> (i64, i64, i64, i64) {
(
pixels_to_emu(self.left as f64),
pixels_to_emu(self.top as f64),
pixels_to_emu(self.width as f64),
pixels_to_emu(self.height as f64),
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Shape {
pub axis_coordinates: ((f64, f64), (f64, f64)),
pub coordinates: (f64, f64, f64, f64),
pub text: Option<String>,
pub scheme: String,
pub style: String,
border_color: String,
color: String,
text_color: String,
border_width: i64,
}
impl Default for Shape {
fn default() -> Self {
Shape {
axis_coordinates: ((0.0, 0.0), (1.0, 1.0)),
coordinates: (0.0, 0.0, 1.0, 1.0),
text: None,
scheme: "accent1".to_string(),
style: Shape::RECT.to_string(),
border_color: short_color(Color::BLACK),
color: short_color(Color::WHITE),
text_color: short_color(Color::BLACK),
border_width: 0,
}
}
}
impl Shape {
pub const MARGIN_LEFT: i64 = 6 + 13 + 1;
pub const MARGIN_BOTTOM: i64 = 17 + 11;
pub const FONT_WIDTH: i64 = 7;
pub const FONT_HEIGHT: i64 = 8;
pub const RECT: &'static str = "rect";
pub const ROUND_RECT: &'static str = "roundRect";
pub fn new() -> Self {
Shape::default()
}
pub fn with_text(text: impl Into<String>) -> Self {
Shape {
text: Some(text.into()),
..Shape::default()
}
}
pub fn border_color(&self) -> &str {
&self.border_color
}
pub fn set_border_color(&mut self, color: &str) {
self.border_color = short_color(color);
}
pub fn color(&self) -> &str {
&self.color
}
pub fn set_color(&mut self, color: &str) {
self.color = short_color(color);
}
pub fn text_color(&self) -> &str {
&self.text_color
}
pub fn set_text_color(&mut self, color: &str) {
self.text_color = short_color(color);
}
pub fn border_width(&self) -> i64 {
self.border_width
}
pub fn set_border_width(&mut self, width: i64) {
self.border_width = width;
}
pub fn set_axis_coordinates(&mut self, coordinates: ((f64, f64), (f64, f64))) {
self.axis_coordinates = coordinates;
}
pub fn norm_pct(pct: f64) -> f64 {
pct.clamp(0.0, 1.0)
}
}
pub fn bounding_box(bw: i64, bh: i64, w: i64, h: i64) -> (i64, i64) {
let mut new_width = w;
let mut new_height = h;
if bw != 0 && new_width > bw {
new_width = bw;
new_height = (new_width as f64 / (w as f64 / h as f64)).round() as i64;
}
if bh != 0 && new_height > bh {
new_height = bh;
new_width = (new_height as f64 * (w as f64 / h as f64)).round() as i64;
}
(new_width, new_height)
}
#[derive(Debug, Clone, PartialEq)]
pub struct Image {
pub data: Vec<u8>,
pub format: String,
pub size: (u32, u32),
pub no_change_aspect: bool,
pub no_change_arrowheads: bool,
pub drawing: Drawing,
}
impl Image {
pub fn from_png(data: Vec<u8>) -> Result<Image> {
let size = read_png_dimensions(&data)?;
Ok(Image {
data,
format: "png".to_string(),
size,
no_change_aspect: true,
no_change_arrowheads: true,
drawing: Drawing::new(),
})
}
pub fn new(data: Vec<u8>, format: &str, size: (u32, u32)) -> Self {
Image {
data,
format: format.to_string(),
size,
no_change_aspect: true,
no_change_arrowheads: true,
drawing: Drawing::new(),
}
}
pub fn extension(&self) -> String {
self.format.to_lowercase()
}
pub fn fit_to(&mut self, size: (Option<i64>, Option<i64>)) {
let (new_width, new_height) = bounding_box(
size.0.unwrap_or(0),
size.1.unwrap_or(0),
self.size.0 as i64,
self.size.1 as i64,
);
self.drawing.set_width(new_width);
self.drawing.set_height(new_height);
}
pub fn anchor_absolute(
&mut self,
cell: (&str, u32),
cell_left: i64,
cell_top: i64,
point_pos: &PointResolver,
) -> Result<(String, u32)> {
self.drawing.anchor_type = AnchorType::Absolute;
self.drawing.left = cell_left;
self.drawing.top = cell_top;
self.drawing.anchor_col = Some(column_index_from_string(cell.0)? as i64 - 1);
self.drawing.anchor_row = Some(cell.1 as i64 - 1);
Ok(point_pos(
self.drawing.top + self.drawing.height(),
self.drawing.left + self.drawing.width(),
))
}
pub fn anchor_one_cell(&mut self, column: &str, row: u32) -> Result<()> {
self.drawing.anchor_type = AnchorType::OneCell;
self.drawing.anchor_col = Some(column_index_from_string(column)? as i64 - 1);
self.drawing.anchor_row = Some(row as i64 - 1);
Ok(())
}
}
pub type PointResolver = dyn Fn(i64, i64) -> (String, u32);
pub fn read_png_dimensions(data: &[u8]) -> Result<(u32, u32)> {
const SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
if data.len() < 24 || data[..8] != SIGNATURE {
return Err(Error::Value(
"Image data is not a valid PNG file".to_string(),
));
}
let width = u32::from_be_bytes([data[16], data[17], data[18], data[19]]);
let height = u32::from_be_bytes([data[20], data[21], data[22], data[23]]);
if width == 0 || height == 0 {
return Err(Error::Value("PNG has zero dimensions".to_string()));
}
Ok((width, height))
}
pub fn column_letters(index: u32) -> String {
get_column_letter(index).unwrap_or_default()
}
pub fn column_index(letters: &str) -> Result<u32> {
column_index_from_string(letters)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShapeStyle {
pub style: String,
pub color: String,
pub border_color: String,
pub border_width: i64,
pub text_color: String,
}
impl Shape {
pub fn style(&self) -> ShapeStyle {
ShapeStyle {
style: self.style.clone(),
color: self.color.clone(),
border_color: self.border_color.clone(),
border_width: self.border_width,
text_color: self.text_color.clone(),
}
}
}
pub fn validate_anchor_column(column: &str) -> Result<u32> {
column_index_from_string(column)
.map_err(|_| Error::ColumnStringIndex(format!("{column} is not a valid column name")))
}
pub const DEFAULT_ANCHOR_COLUMN: &str = "A";
pub fn loose_column_index(letters: &str) -> u32 {
letters
.chars()
.filter(|c| c.is_ascii_alphabetic())
.fold(0u32, |acc, c| {
acc * 26 + (c.to_ascii_uppercase() as u32 - 'A' as u32 + 1)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drawing_defaults_match_python() {
let drawing = Drawing::new();
assert_eq!(drawing.width(), emu_to_pixels(200_000));
assert_eq!(drawing.height(), emu_to_pixels(1_828_800));
assert_eq!(drawing.coordinates, ((1, 2), (16, 8)));
assert_eq!(drawing.anchor_type, AnchorType::Absolute);
}
#[test]
fn emu_dimensions_convert_pixels() {
let mut drawing = Drawing::new();
drawing.left = 10;
drawing.top = 400;
drawing.set_width(800);
drawing.set_height(400);
let (x, y, w, h) = drawing.emu_dimensions();
assert_eq!(x, pixels_to_emu(10.0));
assert_eq!(y, pixels_to_emu(400.0));
assert_eq!(w, pixels_to_emu(800.0));
assert_eq!(h, pixels_to_emu(400.0));
}
#[test]
fn proportional_resize_keeps_ratio() {
let mut drawing = Drawing::new();
drawing.set_width(1000);
drawing.set_height(500);
drawing.resize_proportional = true;
drawing.set_width(2000);
assert_eq!(drawing.width(), 2000);
assert_eq!(drawing.height(), 1000);
drawing.set_height(500);
assert_eq!(drawing.width(), 1000);
assert_eq!(drawing.height(), 500);
}
#[test]
fn set_dimension_only_applies_when_proportional() {
let mut drawing = Drawing::new();
drawing.set_width(100);
drawing.set_height(100);
drawing.set_dimension(50, 25);
assert_eq!(drawing.width(), 100);
assert_eq!(drawing.height(), 100);
drawing.resize_proportional = true;
drawing.set_dimension(50, 25);
assert!(drawing.width() <= 50);
assert!(drawing.height() <= 25);
}
#[test]
fn colours_are_stored_in_short_form() {
let mut shape = Shape::new();
shape.set_color("FFFF0000");
shape.set_border_color("FF00FF00");
shape.set_text_color("FF0000FF");
assert_eq!(shape.color(), "FF0000");
assert_eq!(shape.border_color(), "00FF00");
assert_eq!(shape.text_color(), "0000FF");
}
#[test]
fn norm_pct_clamps() {
assert_eq!(Shape::norm_pct(-0.5), 0.0);
assert_eq!(Shape::norm_pct(0.25), 0.25);
assert_eq!(Shape::norm_pct(1.5), 1.0);
}
#[test]
fn bounding_box_preserves_ratio() {
assert_eq!(bounding_box(100, 100, 200, 100), (100, 50));
assert_eq!(bounding_box(0, 50, 200, 100), (100, 50));
assert_eq!(bounding_box(1000, 1000, 200, 100), (200, 100));
}
#[test]
fn shadow_defaults() {
let shadow = Shadow::new();
assert!(!shadow.visible);
assert_eq!(shadow.alignment, "br");
assert_eq!(shadow.alpha, 50);
}
fn minimal_png(width: u32, height: u32) -> Vec<u8> {
let mut png = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
png.extend_from_slice(&13u32.to_be_bytes());
png.extend_from_slice(b"IHDR");
png.extend_from_slice(&width.to_be_bytes());
png.extend_from_slice(&height.to_be_bytes());
png.extend_from_slice(&[8, 6, 0, 0, 0]);
png.extend_from_slice(&[0u8; 4]);
png
}
#[test]
fn png_dimensions_are_read_from_the_header() {
let png = minimal_png(64, 32);
assert_eq!(read_png_dimensions(&png).unwrap(), (64, 32));
assert!(read_png_dimensions(&minimal_png(0, 32)).is_err());
assert!(read_png_dimensions(b"not a png").is_err());
}
#[test]
fn image_preserves_bytes_and_fits() {
let png = minimal_png(200, 100);
let mut image = Image::from_png(png.clone()).unwrap();
assert_eq!(image.size, (200, 100));
assert_eq!(image.extension(), "png");
assert_eq!(image.data, png);
image.fit_to((Some(50), None));
assert_eq!(image.drawing.width(), 50);
assert_eq!(image.drawing.height(), 25);
}
#[test]
fn one_cell_anchor_records_zero_based_position() {
let mut image = Image::new(vec![], "png", (10, 10));
image.anchor_one_cell("C", 4).unwrap();
assert_eq!(image.drawing.anchor_type, AnchorType::OneCell);
assert_eq!(image.drawing.anchor_col, Some(2));
assert_eq!(image.drawing.anchor_row, Some(3));
assert!(image.anchor_one_cell("!!", 1).is_err());
}
#[test]
fn column_helpers() {
assert_eq!(column_letters(1), "A");
assert_eq!(column_index("AA").unwrap(), 27);
assert_eq!(loose_column_index("AA"), 27);
assert_eq!(validate_anchor_column("B").unwrap(), 2);
}
}