From da997bd5b619ca206c99765ae6aa0e1d2b58d0d8 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Tue, 15 Aug 2023 11:06:55 -0700 Subject: [PATCH] Add single keys with variable units --- dyesub-tool/src/key/mod.rs | 38 +++++++++++++++++++++++--- dyesub-tool/src/key/oyayubi.rs | 10 +------ dyesub-tool/src/main.rs | 43 +++++++++++++++++++----------- dyesub-tool/templates/document.xml | 18 ++++++++----- 4 files changed, 74 insertions(+), 35 deletions(-) diff --git a/dyesub-tool/src/key/mod.rs b/dyesub-tool/src/key/mod.rs index 6a10d6b..d90bf4a 100644 --- a/dyesub-tool/src/key/mod.rs +++ b/dyesub-tool/src/key/mod.rs @@ -3,20 +3,52 @@ pub use oyayubi::OYAYUBI; use std::fmt::{Display, self}; -pub enum Key { +#[derive(Clone, Copy)] +pub enum Key<'a> { Oyayubi { latin: char, normal: char, shift: char, alt_shift: Option, + }, + Single { + text: &'a str, + u: f64, + }, +} + +pub const fn oyayubi<'a>(latin: char, normal: char, shift: char, alt_shift: Option) -> Key<'a> { + Key::Oyayubi { + latin, + normal, + shift, + alt_shift, + } +} + +pub const fn single<'a>(text: &'a str, u: f64) -> Key<'a> { + Key::Single { + text, + u, + } +} + +impl<'a> Key<'a> { + pub fn width_mod(&self) -> f64 { + use Key::*; + match self { + Oyayubi { .. } => 1.0, + Single { u, .. } => *u, + } } } -impl Display for Key { +impl<'a> Display for Key<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use Key::*; write!(f, "{}", match self { - Oyayubi { latin, .. } => latin, + Oyayubi { latin, .. } => latin.to_string(), + Single { text, .. } => text.to_string(), }) } } \ No newline at end of file diff --git a/dyesub-tool/src/key/oyayubi.rs b/dyesub-tool/src/key/oyayubi.rs index 5bbac60..ffffea5 100644 --- a/dyesub-tool/src/key/oyayubi.rs +++ b/dyesub-tool/src/key/oyayubi.rs @@ -1,13 +1,5 @@ use super::Key; - -const fn k(latin: char, normal: char, shift: char, alt_shift: Option) -> Key { - Key::Oyayubi { - latin, - normal, - shift, - alt_shift, - } -} +use super::oyayubi as k; pub const OYAYUBI: [Key; 30] = [ k('Q', '。', 'ぁ', None), diff --git a/dyesub-tool/src/main.rs b/dyesub-tool/src/main.rs index 854d8f5..f054c96 100644 --- a/dyesub-tool/src/main.rs +++ b/dyesub-tool/src/main.rs @@ -7,13 +7,14 @@ use key::*; use askama::Template; use derive_more::From; use std::{fs::OpenOptions, io::Write}; +use lazy_static::lazy_static; #[derive(Template)] #[template(path = "document.xml")] struct DocumentTemplate<'a> { dimensions: &'a DocumentDimensions, box_size: SVGMeasure, - positions: Vec<(&'a Key, (SVGMeasure, SVGMeasure))>, + positions: Vec<(&'a Key<'a>, (SVGMeasure, SVGMeasure))>, } struct DocumentDimensions { @@ -27,24 +28,25 @@ enum Error { Template(askama::Error), } -fn positions( +fn positions<'a>( + keys: &'a Vec, size: SVGMeasure, padding: SVGMeasure, dimensions: &DocumentDimensions, -) -> Vec<(&Key, (SVGMeasure, SVGMeasure))> { +) -> Vec<(&'a Key<'a>, (SVGMeasure, SVGMeasure))> { let grid = size + padding; - let row_count = ((dimensions.width - padding * 2.0) / grid) as u32; - OYAYUBI - .iter() - .enumerate() - .map(|(i, key)| ( - key, - ( - grid * (i as u32 % row_count).into() + padding, - grid * (i as u32 / row_count).into() + padding, - ) - )) - .collect() + let mut x = padding; + let mut y = padding; + let mut positions = Vec::with_capacity(keys.len()); + for key in keys { + if x + grid > dimensions.width { + x = padding; + y = y + grid; + } + positions.push((key, (x, y))); + x = x + size * key.width_mod() + padding; + } + positions } fn main() -> Result<(), Error> { @@ -60,10 +62,19 @@ fn main() -> Result<(), Error> { let box_size = SVGMeasure::new(14.0, SVGUnit::Millimeter); let padding = SVGMeasure::new(2.5, SVGUnit::Millimeter); let document = DocumentTemplate { - positions: positions(box_size, padding, &dimensions), + positions: positions(&KEYS, box_size, padding, &dimensions), dimensions: &dimensions, box_size, }; write!(file, "{}", document.render()?)?; Ok(()) } + +lazy_static! { + static ref KEYS: Vec> = { + let mut keys = OYAYUBI.to_vec(); + keys.push(single("親指左", 3.0)); + keys.push(single("親指右", 3.0)); + keys + }; +} \ No newline at end of file diff --git a/dyesub-tool/templates/document.xml b/dyesub-tool/templates/document.xml index a905954..07e94ae 100644 --- a/dyesub-tool/templates/document.xml +++ b/dyesub-tool/templates/document.xml @@ -5,20 +5,24 @@ {% for (key, (x, y)) in positions %} - + {% let width = box_size * key.width_mod() %} + + + {% let dy = "0.125em" %} {% match key %} {% when crate::key::Key::Oyayubi with { latin, normal, shift, alt_shift } %} - - {% let dy = "0.125em" %} {% let font_size = "0.45cm" %} {% let latin_font_size = "0.4cm" %} {% let alt_shift_font_size = "0.325cm" %} - {{ latin }} - {{ shift }} - {{ normal }} + {{ latin }} + {{ shift }} + {{ normal }} {% if let Some(alt_shift) = alt_shift %} - {{ alt_shift }} + {{ alt_shift }} {% endif %} + {% when crate::key::Key::Single with { text, u } %} + {% let font_size = "0.5cm" %} + {{ text }} {% endmatch %} {% endfor %}