diff --git a/dyesub-tool/src/main.rs b/dyesub-tool/src/main.rs index e570512..be40312 100644 --- a/dyesub-tool/src/main.rs +++ b/dyesub-tool/src/main.rs @@ -1,53 +1,10 @@ pub mod svg; -use svg::*; -use askama::Template; -use derive_more::From; -use lazy_static::lazy_static; -use std::{fs::OpenOptions, io::Write}; +mod render; -#[derive(Template)] -#[template(path = "document.xml")] -struct DocumentTemplate<'a> { - dimensions: &'a DocumentDimensions, - keyboard: kle_serial::Keyboard, -} - -struct DocumentDimensions { - width: SVGMeasure, - height: SVGMeasure, - margin: SVGMeasure, -} - -fn kle_font_units(kle_font_units: &usize) -> SVGMeasure { - SVGMeasure::new((6 + kle_font_units * 2) as f64, SVGUnit::Pixel) -} - -lazy_static! { - static ref LETTER_LANDSCAPE: DocumentDimensions = DocumentDimensions { - width: SVGMeasure::new(11.0, SVGUnit::Inch), - height: SVGMeasure::new(8.5, SVGUnit::Inch), - margin: SVGMeasure::new(0.25, SVGUnit::Inch), - }; -} - -#[derive(From, Debug)] -enum Error { - Io(std::io::Error), - Template(askama::Error), - LayoutParse(serde_json::Error), -} +use render::{render_file, Result}; -fn main() -> Result<(), Error> { - let mut file = OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open("output.svg")?; - let document = DocumentTemplate { - dimensions: &LETTER_LANDSCAPE, - keyboard: serde_json::from_str(&std::fs::read_to_string("layout.json")?)?, - }; - write!(file, "{}", document.render()?)?; +fn main() -> Result<()> { + render_file("layout.json", "output.svg")?; Ok(()) } diff --git a/dyesub-tool/src/render.rs b/dyesub-tool/src/render.rs new file mode 100644 index 0000000..16ffbd7 --- /dev/null +++ b/dyesub-tool/src/render.rs @@ -0,0 +1,65 @@ +use std::path::Path; + +use askama::Template; +use derive_more::From; +use lazy_static::lazy_static; + +use crate::svg::{SVGMeasure, SVGUnit}; + +#[derive(Template)] +#[template(path = "document.xml")] +struct DocumentTemplate<'a> { + dimensions: &'a DocumentDimensions, + keyboard: kle_serial::Keyboard, +} + +struct DocumentDimensions { + width: SVGMeasure, + height: SVGMeasure, + margin: SVGMeasure, +} + +lazy_static! { + static ref LETTER_LANDSCAPE: DocumentDimensions = DocumentDimensions { + width: SVGMeasure::new(11.0, SVGUnit::Inch), + height: SVGMeasure::new(8.5, SVGUnit::Inch), + margin: SVGMeasure::new(0.25, SVGUnit::Inch), + }; +} + +mod filters { + use crate::svg::{SVGMeasure, SVGUnit}; + + pub fn kle_font_units(kle_font_units: &usize) -> askama::Result { + Ok(SVGMeasure::new((6 + kle_font_units * 2) as f64, SVGUnit::Pixel)) + } +} + +#[derive(From, Debug)] +pub enum Error { + Io(std::io::Error), + Template(askama::Error), + LayoutParse(serde_json::Error), +} + +pub type Result = std::result::Result; + +pub fn render_file(input_path: P, output_path: Q) -> Result<()> +where P: AsRef, Q: AsRef { + use std::{fs::{self, OpenOptions}, io::Write}; + let mut file = OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(output_path)?; + write!(file, "{}", render(&fs::read_to_string(input_path)?)?)?; + Ok(()) +} + +pub fn render(input: &str) -> Result { + let document = DocumentTemplate { + dimensions: &LETTER_LANDSCAPE, + keyboard: serde_json::from_str(input)?, + }; + Ok(document.render()?) +} \ No newline at end of file diff --git a/dyesub-tool/templates/document.xml b/dyesub-tool/templates/document.xml index be9847b..5995dcc 100644 --- a/dyesub-tool/templates/document.xml +++ b/dyesub-tool/templates/document.xml @@ -4,8 +4,8 @@ - {% let u = crate::svg::SVGMeasure::new(14.0, crate::svg::SVGUnit::Millimeter) %} - {% let padding = crate::svg::SVGMeasure::new(2.5, crate::svg::SVGUnit::Millimeter) %} + {% let u = SVGMeasure::new(14.0, SVGUnit::Millimeter) %} + {% let padding = SVGMeasure::new(2.5, SVGUnit::Millimeter) %} {% let dy = "0.125em" %} {% for key in keyboard.keys %} {% let x = u * key.x + padding * key.x + dimensions.margin %} @@ -17,47 +17,47 @@ {# top-left #} {% if let Some(legend) = key.legends[0] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# top-center #} {% if let Some(legend) = key.legends[1] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# top-right #} {% if let Some(legend) = key.legends[2] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# middle-left #} {% if let Some(legend) = key.legends[3] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# middle-center #} {% if let Some(legend) = key.legends[4] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# middle-right #} {% if let Some(legend) = key.legends[5] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# bottom-left #} {% if let Some(legend) = key.legends[6] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# bottom-center #} {% if let Some(legend) = key.legends[7] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# bottom-right #} {% if let Some(legend) = key.legends[8] %} - {{ legend.text }} + {{ legend.text }} {% endif %} {# TODO 9-11 are side #}