diff --git a/dyesub-tool/src/main.rs b/dyesub-tool/src/main.rs index 0a4c50d..adc69d7 100644 --- a/dyesub-tool/src/main.rs +++ b/dyesub-tool/src/main.rs @@ -8,6 +8,12 @@ use std::{fs::OpenOptions, io::Write}; #[derive(Template)] #[template(path = "document.xml")] struct DocumentTemplate { + dimensions: DocumentDimensions, + box_size: SVGMeasure, + positions: Vec<(SVGMeasure, SVGMeasure)>, +} + +struct DocumentDimensions { width: SVGMeasure, height: SVGMeasure, } @@ -18,16 +24,35 @@ enum Error { Template(askama::Error), } +fn positions(size: SVGMeasure, padding: SVGMeasure, dimensions: &DocumentDimensions, count: u32) -> Vec<(SVGMeasure, SVGMeasure)> { + let grid = size + padding; + let row_count = ((dimensions.width - padding * 2.0) / grid) as u32; + (0..count) + .map(|i| ( + grid * (i % row_count).into() + padding, + grid * (i / row_count).into() + padding, + )) + .collect() +} + fn main() -> Result<(), Error> { let mut file = OpenOptions::new() .write(true) .truncate(true) .create(true) .open("output.svg")?; - let document = DocumentTemplate { + let dimensions = DocumentDimensions { width: SVGMeasure::new(8.5, SVGUnit::Inch), height: SVGMeasure::new(11.0, SVGUnit::Inch), }; + let box_size = SVGMeasure::new(14.0, SVGUnit::Millimeter); + let boxes = 54; + let padding = SVGMeasure::new(5.0, SVGUnit::Millimeter); + let document = DocumentTemplate { + positions: positions(box_size, padding, &dimensions, boxes), + dimensions, + box_size, + }; write!(file, "{}", document.render()?)?; Ok(()) } diff --git a/dyesub-tool/src/svg/measure.rs b/dyesub-tool/src/svg/measure.rs index 3e23764..f9814e6 100644 --- a/dyesub-tool/src/svg/measure.rs +++ b/dyesub-tool/src/svg/measure.rs @@ -5,7 +5,7 @@ use std::num::ParseFloatError; use std::str::FromStr; use std::{ fmt::Display, - ops::{Add, Div, Mul, Sub}, + ops::{Add, Div, Mul, Sub, Rem}, }; use lazy_static::lazy_static; use regex::Regex; @@ -118,6 +118,14 @@ impl Mul for SVGMeasure { } } +impl Div for SVGMeasure { + type Output = f64; + + fn div(self, other: Self) -> Self::Output { + self.measure / other.to_unit(self.unit).measure + } +} + impl Div for SVGMeasure { type Output = Self; @@ -126,3 +134,12 @@ impl Div for SVGMeasure { self } } + +impl Rem for SVGMeasure { + type Output = Self; + + fn rem(mut self, other: Self) -> Self::Output { + self.measure %= other.to_unit(self.unit).measure; + self + } +} diff --git a/dyesub-tool/src/svg/unit.rs b/dyesub-tool/src/svg/unit.rs index 762a535..ddfe527 100644 --- a/dyesub-tool/src/svg/unit.rs +++ b/dyesub-tool/src/svg/unit.rs @@ -25,14 +25,14 @@ pub enum SVGUnit { } impl SVGUnit { - pub fn to_user_units(&self) -> f64 { + pub const fn to_user_units(&self) -> f64 { use SVGUnit::*; match self { Pixel => 1.0, Inch => 96.0, Centimeter => 37.795, Millimeter => 3.7795, - Point => 4.0 / 3.0, // 1.3333 + Point => 1.3333, Pica => 16.0, } } diff --git a/dyesub-tool/templates/document.xml b/dyesub-tool/templates/document.xml index d555166..2f330cd 100644 --- a/dyesub-tool/templates/document.xml +++ b/dyesub-tool/templates/document.xml @@ -1,5 +1,8 @@ + {% for (x, y) in positions %} + + {% endfor %}