generated from ElnuDev/rust-project
Basic grid output
This commit is contained in:
parent
dc636263b7
commit
3960d6e721
4 changed files with 50 additions and 5 deletions
|
@ -8,6 +8,12 @@ use std::{fs::OpenOptions, io::Write};
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "document.xml")]
|
#[template(path = "document.xml")]
|
||||||
struct DocumentTemplate {
|
struct DocumentTemplate {
|
||||||
|
dimensions: DocumentDimensions,
|
||||||
|
box_size: SVGMeasure,
|
||||||
|
positions: Vec<(SVGMeasure, SVGMeasure)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DocumentDimensions {
|
||||||
width: SVGMeasure,
|
width: SVGMeasure,
|
||||||
height: SVGMeasure,
|
height: SVGMeasure,
|
||||||
}
|
}
|
||||||
|
@ -18,16 +24,35 @@ enum Error {
|
||||||
Template(askama::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> {
|
fn main() -> Result<(), Error> {
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.truncate(true)
|
.truncate(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open("output.svg")?;
|
.open("output.svg")?;
|
||||||
let document = DocumentTemplate {
|
let dimensions = DocumentDimensions {
|
||||||
width: SVGMeasure::new(8.5, SVGUnit::Inch),
|
width: SVGMeasure::new(8.5, SVGUnit::Inch),
|
||||||
height: SVGMeasure::new(11.0, 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()?)?;
|
write!(file, "{}", document.render()?)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::num::ParseFloatError;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
ops::{Add, Div, Mul, Sub},
|
ops::{Add, Div, Mul, Sub, Rem},
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
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 {
|
impl Div<f64> for SVGMeasure {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
|
@ -126,3 +134,12 @@ impl Div<f64> for SVGMeasure {
|
||||||
self
|
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 {
|
impl SVGUnit {
|
||||||
pub fn to_user_units(&self) -> f64 {
|
pub const fn to_user_units(&self) -> f64 {
|
||||||
use SVGUnit::*;
|
use SVGUnit::*;
|
||||||
match self {
|
match self {
|
||||||
Pixel => 1.0,
|
Pixel => 1.0,
|
||||||
Inch => 96.0,
|
Inch => 96.0,
|
||||||
Centimeter => 37.795,
|
Centimeter => 37.795,
|
||||||
Millimeter => 3.7795,
|
Millimeter => 3.7795,
|
||||||
Point => 4.0 / 3.0, // 1.3333
|
Point => 1.3333,
|
||||||
Pica => 16.0,
|
Pica => 16.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
<svg version="1.1"
|
<svg version="1.1"
|
||||||
width="{{ width }}" height="{{ height }}"
|
width="{{ dimensions.width }}" height="{{ dimensions.height }}"
|
||||||
xmlns="http://www.w3.org/2000/svg">
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
<rect width="100%" height="100%" fill="gray" />
|
<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>
|
</svg>
|
||||||
|
|
Before Width: | Height: | Size: 164 B After Width: | Height: | Size: 330 B |
Loading…
Add table
Reference in a new issue