Clean codebase by making render module

main
Elnu 1 year ago
parent 81fed4c8ac
commit 1e646e0ac0

@ -1,53 +1,10 @@
pub mod svg; pub mod svg;
use svg::*;
use askama::Template; mod render;
use derive_more::From;
use lazy_static::lazy_static;
use std::{fs::OpenOptions, io::Write};
#[derive(Template)] use render::{render_file, Result};
#[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),
}
fn main() -> Result<(), Error> { fn main() -> Result<()> {
let mut file = OpenOptions::new() render_file("layout.json", "output.svg")?;
.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()?)?;
Ok(()) Ok(())
} }

@ -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<SVGMeasure> {
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<T> = std::result::Result<T, Error>;
pub fn render_file<P, Q>(input_path: P, output_path: Q) -> Result<()>
where P: AsRef<Path>, Q: AsRef<Path> {
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<String> {
let document = DocumentTemplate {
dimensions: &LETTER_LANDSCAPE,
keyboard: serde_json::from_str(input)?,
};
Ok(document.render()?)
}

@ -4,8 +4,8 @@
<defs> <defs>
<style type="text/css"><![CDATA[@import url('https://fonts.googleapis.com/css2?family=M+PLUS+Rounded+1c:wght@700&display=swap');]]></style> <style type="text/css"><![CDATA[@import url('https://fonts.googleapis.com/css2?family=M+PLUS+Rounded+1c:wght@700&display=swap');]]></style>
</defs> </defs>
{% let u = crate::svg::SVGMeasure::new(14.0, crate::svg::SVGUnit::Millimeter) %} {% let u = SVGMeasure::new(14.0, SVGUnit::Millimeter) %}
{% let padding = crate::svg::SVGMeasure::new(2.5, crate::svg::SVGUnit::Millimeter) %} {% let padding = SVGMeasure::new(2.5, SVGUnit::Millimeter) %}
{% let dy = "0.125em" %} {% let dy = "0.125em" %}
{% for key in keyboard.keys %} {% for key in keyboard.keys %}
{% let x = u * key.x + padding * key.x + dimensions.margin %} {% let x = u * key.x + padding * key.x + dimensions.margin %}
@ -17,47 +17,47 @@
{# top-left #} {# top-left #}
{% if let Some(legend) = key.legends[0] %} {% if let Some(legend) = key.legends[0] %}
<text x="{{ width * 0.25 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.25 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# top-center #} {# top-center #}
{% if let Some(legend) = key.legends[1] %} {% if let Some(legend) = key.legends[1] %}
<text x="{{ width * 0.5 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.5 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# top-right #} {# top-right #}
{% if let Some(legend) = key.legends[2] %} {% if let Some(legend) = key.legends[2] %}
<text x="{{ width * 0.75 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.75 }}" y="{{ height * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# middle-left #} {# middle-left #}
{% if let Some(legend) = key.legends[3] %} {% if let Some(legend) = key.legends[3] %}
<text x="{{ width * 0.25 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.25 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# middle-center #} {# middle-center #}
{% if let Some(legend) = key.legends[4] %} {% if let Some(legend) = key.legends[4] %}
<text x="{{ width * 0.5 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.5 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# middle-right #} {# middle-right #}
{% if let Some(legend) = key.legends[5] %} {% if let Some(legend) = key.legends[5] %}
<text x="{{ width * 0.75 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.75 }}" y="{{ height * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# bottom-left #} {# bottom-left #}
{% if let Some(legend) = key.legends[6] %} {% if let Some(legend) = key.legends[6] %}
<text x="{{ width * 0.25 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.25 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# bottom-center #} {# bottom-center #}
{% if let Some(legend) = key.legends[7] %} {% if let Some(legend) = key.legends[7] %}
<text x="{{ width * 0.55 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.55 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# bottom-right #} {# bottom-right #}
{% if let Some(legend) = key.legends[8] %} {% if let Some(legend) = key.legends[8] %}
<text x="{{ width * 0.75 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ crate::kle_font_units(legend.size) }}" dy="{{ dy }}">{{ legend.text }}</text> <text x="{{ width * 0.75 }}" y="{{ height * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ legend.size|kle_font_units }}" dy="{{ dy }}">{{ legend.text }}</text>
{% endif %} {% endif %}
{# TODO 9-11 are side #} {# TODO 9-11 are side #}

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

Loading…
Cancel
Save