Boilerplate

This commit is contained in:
Elnu 2023-08-13 22:14:23 -07:00
parent b7b441a933
commit 0ad5b4df3b
10 changed files with 392 additions and 7 deletions

View file

@ -3,6 +3,6 @@ name = "dyesub-tool"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
askama = "0.12.0"
derive_more = "0.99.17"

View file

@ -1,3 +1,33 @@
fn main() {
println!("Hello, world!");
pub mod svg;
use svg::*;
use std::{fs::OpenOptions, io::Write};
use askama::Template;
use derive_more::From;
#[derive(Template)]
#[template(path = "document.xml")]
struct DocumentTemplate {
width: SVGMeasure,
height: SVGMeasure,
}
#[derive(From, Debug)]
enum Error {
Io(std::io::Error),
Template(askama::Error),
}
fn main() -> Result<(), Error> {
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open("output.svg")?;
let document = DocumentTemplate {
width: SVGMeasure::new(8.5, SVGUnit::Inch),
height: SVGMeasure::new(11.0,SVGUnit::Inch),
};
write!(file, "{}", document.render()?)?;
Ok(())
}

View file

@ -0,0 +1,52 @@
use super::SVGUnit;
use std::fmt;
use std::{ops::{Add, Sub, Mul, Div}, fmt::Display};
pub struct SVGMeasure {
pub measure: f64,
pub unit: SVGUnit,
}
impl SVGMeasure {
pub fn new(measure: f64, unit: SVGUnit) -> Self {
Self { measure, unit }
}
}
impl Display for SVGMeasure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.measure, self.unit)
}
}
impl Add for SVGMeasure {
type Output = Self;
fn add(self, other: SVGMeasure) -> SVGMeasure {
SVGMeasure::new(self.measure + other.measure, self.unit)
}
}
impl Sub for SVGMeasure {
type Output = Self;
fn sub(self, other: SVGMeasure) -> SVGMeasure {
SVGMeasure::new(self.measure - other.measure, self.unit)
}
}
impl Mul<f64> for SVGMeasure {
type Output = Self;
fn mul(self, scalar: f64) -> SVGMeasure {
SVGMeasure::new(self.measure * scalar, self.unit)
}
}
impl Div<f64> for SVGMeasure {
type Output = Self;
fn div(self, scalar: f64) -> SVGMeasure {
SVGMeasure::new(self.measure / scalar, self.unit)
}
}

View file

@ -0,0 +1,5 @@
mod unit;
pub use unit::SVGUnit;
mod measure;
pub use measure::SVGMeasure;

View file

@ -0,0 +1,55 @@
use std::fmt::{self, Display};
/// https://oreillymedia.github.io/Using_SVG/guide/units.html
pub enum SVGUnit {
/// Pixel units, directly equivalent to SVG user units.
Pixel,
/// Inches.
Inch,
/// Centimeters.
Centimeter,
/// Millimeters.
Millimeter,
/// Points.
Point,
// Picas.
Pica,
/// Em units. Equivalent to the computed font-size in effect for an element.
Em,
/// Ex units. Equivalent to the height of a lower-case letter in the font (and font-size) in effect for an element. If the font doesnt include lower-case letters, or doesnt include the metadata about the ex-height, then 1ex = 0.5em.
Ex,
/// Character units. Equivalent to the width of the `0` (zero) character in the font and font-size in effect for an element.
Character,
/// Root-em units. The font size of the root element in the document, regardless of any settings for the current element.
Rem,
/// Viewport width units. 1% of the viewport width.
ViewportWidth,
/// Viewport height units. 1% of the viewport height.
ViewportHeight,
/// Viewport minimum units. 1% of the viewport width or height, whichever is smaller.
ViewportMinimum,
/// Viewport maximum units. 1% of the viewport width or height, whichever is larger.
ViewportMaximum,
}
impl Display for SVGUnit {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use SVGUnit::*;
write!(f, "{}", match &self {
Pixel => "px",
Inch => "in",
Centimeter => "cm",
Millimeter => "mm",
Point => "pt",
Pica => "pc",
Em => "em",
Ex => "ex",
Character => "ch",
Rem => "rem",
ViewportWidth => "vw",
ViewportHeight => "vh",
ViewportMinimum => "vmin",
ViewportMaximum => "vmax",
})
}
}

View file

@ -0,0 +1,5 @@
<svg version="1.1"
width="{{ width }}" height="{{ height }}"
xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="gray" />
</svg>

After

Width:  |  Height:  |  Size: 164 B