Basic grid output

main
Elnu 1 year ago
parent dc636263b7
commit 3960d6e721

@ -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(())
}

@ -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<f64> 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<f64> for SVGMeasure {
type Output = Self;
@ -126,3 +134,12 @@ impl Div<f64> 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
}
}

@ -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,
}
}

@ -1,5 +1,8 @@
<svg version="1.1"
width="{{ width }}" height="{{ height }}"
width="{{ dimensions.width }}" height="{{ dimensions.height }}"
xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="gray" />
{% for (x, y) in positions %}
<rect x="{{ x }}" y="{{ y }}" width="{{ box_size }}" height="{{ box_size }}" fill="white" />
{% endfor %}
</svg>

Before

Width:  |  Height:  |  Size: 164 B

After

Width:  |  Height:  |  Size: 330 B

Loading…
Cancel
Save