Add single keys with variable units

main
Elnu 1 year ago
parent f5e043c981
commit da997bd5b6

@ -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<char>,
},
Single {
text: &'a str,
u: f64,
},
}
pub const fn oyayubi<'a>(latin: char, normal: char, shift: char, alt_shift: Option<char>) -> 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(),
})
}
}

@ -1,13 +1,5 @@
use super::Key;
const fn k(latin: char, normal: char, shift: char, alt_shift: Option<char>) -> Key {
Key::Oyayubi {
latin,
normal,
shift,
alt_shift,
}
}
use super::oyayubi as k;
pub const OYAYUBI: [Key; 30] = [
k('Q', '。', 'ぁ', None),

@ -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<Key>,
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<Key<'static>> = {
let mut keys = OYAYUBI.to_vec();
keys.push(single("親指左", 3.0));
keys.push(single("親指右", 3.0));
keys
};
}

@ -5,20 +5,24 @@
<style type="text/css"><![CDATA[@import url('https://fonts.googleapis.com/css2?family=M+PLUS+Rounded+1c:wght@700&display=swap');]]></style>
</defs>
{% for (key, (x, y)) in positions %}
<g x="{{ x }}" y="{{ y }}" transform="translate({{ x.to_user_units() }},{{ y.to_user_units() }})" width="{{ box_size }}" height="{{ box_size }}">
{% let width = box_size * key.width_mod() %}
<g x="{{ x }}" y="{{ y }}" transform="translate({{ x.to_user_units() }},{{ y.to_user_units() }})" width="{{ width }}" height="{{ box_size }}">
<rect width="{{ width }}" height="{{ box_size }}" fill="none" stroke="#cccccc" stroke-width="1pt" rx="2mm" />
{% let dy = "0.125em" %}
{% match key %}
{% when crate::key::Key::Oyayubi with { latin, normal, shift, alt_shift } %}
<rect width="{{ box_size }}" height="{{ box_size }}" fill="none" stroke="#cccccc" stroke-width="1pt" rx="2mm" />
{% let dy = "0.125em" %}
{% let font_size = "0.45cm" %}
{% let latin_font_size = "0.4cm" %}
{% let alt_shift_font_size = "0.325cm" %}
<text x="{{ box_size * 0.25 }}" y="{{ box_size * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ latin_font_size }}" dy="{{ dy }}">{{ latin }}</text>
<text x="{{ box_size * 0.75 }}" y="{{ box_size * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ font_size }}" dy="{{ dy }}">{{ shift }}</text>
<text x="{{ box_size * 0.75 }}" y="{{ box_size * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ font_size }}" dy="{{ dy }}">{{ normal }}</text>
<text x="{{ width * 0.25 }}" y="{{ box_size * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ latin_font_size }}" dy="{{ dy }}">{{ latin }}</text>
<text x="{{ width * 0.75 }}" y="{{ box_size * 0.25 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ font_size }}" dy="{{ dy }}">{{ shift }}</text>
<text x="{{ width * 0.75 }}" y="{{ box_size * 0.75 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ font_size }}" dy="{{ dy }}">{{ normal }}</text>
{% if let Some(alt_shift) = alt_shift %}
<text x="{{ box_size / 2.0 }}" y="{{ box_size / 2.0 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ alt_shift_font_size }}" dy="{{ dy }}">{{ alt_shift }}</text>
<text x="{{ width / 2.0 }}" y="{{ box_size / 2.0 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ alt_shift_font_size }}" dy="{{ dy }}">{{ alt_shift }}</text>
{% endif %}
{% when crate::key::Key::Single with { text, u } %}
{% let font_size = "0.5cm" %}
<text x="{{ width * 0.5 }}" y="{{ box_size * 0.5 }}" dominant-baseline="middle" text-anchor="middle" style="font-family: 'M PLUS Rounded 1c'" font-size="{{ font_size }}" dy="{{ dy }}">{{ text }}</text>
{% endmatch %}
</g>
{% endfor %}

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Loading…
Cancel
Save