Skip to content

Commit

Permalink
Eager conversion of Mm->Dot
Browse files Browse the repository at this point in the history
  • Loading branch information
staticintlucas committed May 17, 2024
1 parent 049e568 commit ef15f40
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 99 deletions.
24 changes: 9 additions & 15 deletions keyset-drawing/src/imp/key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use geom::{
Angle, Circle, Dot, ExtRect, ExtVec, Length, Path, Point, Rect, RoundRect, Size, ToPath,
Vector, DOT_PER_MM, DOT_PER_UNIT,
Vector, DOT_PER_UNIT,
};
use profile::Profile;

Expand Down Expand Up @@ -75,15 +75,15 @@ pub fn homing(key: &key::Key, options: &Options<'_>) -> Option<KeyPath> {
key::Homing::Scoop => None,
key::Homing::Bar => Some(
Rect::from_center_and_size(
center + Size::new(0.0, (profile.homing.bar.y_offset * DOT_PER_MM).get()),
profile.homing.bar.size * DOT_PER_MM,
center + Size::new(0.0, profile.homing.bar.y_offset.get()),
profile.homing.bar.size,
)
.to_path(),
),
key::Homing::Bump => Some(
Circle::from_center_and_diameter(
center + Size::new(0.0, (profile.homing.bump.y_offset * DOT_PER_MM).get()),
profile.homing.bump.diameter * DOT_PER_MM,
center + Size::new(0.0, profile.homing.bump.y_offset.get()),
profile.homing.bump.diameter,
)
.to_path(),
),
Expand Down Expand Up @@ -353,12 +353,9 @@ mod tests {
assert_is_close!(path.outline.unwrap().width, options.outline_width);
let expected = Rect::from_center_and_size(
options.profile.top_with_size(Size::splat(1.0)).center(),
options.profile.homing.bar.size * DOT_PER_MM,
options.profile.homing.bar.size,
)
.translate(Vector::new(
0.0,
(options.profile.homing.bar.y_offset * DOT_PER_MM).get(),
));
.translate(Vector::new(0.0, options.profile.homing.bar.y_offset.get()));
assert_is_close!(bounds, expected);

// Bump
Expand All @@ -378,12 +375,9 @@ mod tests {
assert_is_close!(path.outline.unwrap().width, options.outline_width);
let expected = Rect::from_center_and_size(
options.profile.top_with_size(Size::splat(1.0)).center(),
Size::splat(options.profile.homing.bump.diameter.get()) * DOT_PER_MM,
Size::splat(options.profile.homing.bump.diameter.get()),
)
.translate(Vector::new(
0.0,
(options.profile.homing.bump.y_offset * DOT_PER_MM).get(),
));
.translate(Vector::new(0.0, options.profile.homing.bump.y_offset.get()));
assert_is_close!(bounds, expected);

// Non-homing key
Expand Down
6 changes: 3 additions & 3 deletions keyset-drawing/src/imp/legend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use font::{Font, Glyph};
use geom::{Dot, Path, Point, Rect, Scale, ToTransform, Vector};
use geom::{Dot, Path, Point, Rect, ToTransform, Vector};
use log::warn;
use profile::Profile;

Expand Down Expand Up @@ -30,15 +30,15 @@ pub fn draw(

let height = profile.text_height.get(legend.size_idx);
// Scale to correct height & flip y-axis
let transform = Scale::new(height / font.cap_height().get())
let transform = (height / font.cap_height())
.to_transform()
.then_scale(1.0, -1.0);
let text_path = Path::from_slice(&char_paths) * transform;

// Calculate legend bounds. For x this is based on actual size while for y we use the base line
// and text height so each character (especially symbols) are still aligned across keys
let bounds = Rect::new(
Point::new(text_path.bounds.min.x, -height),
Point::new(text_path.bounds.min.x, -height.get()),
Point::new(text_path.bounds.max.x, 0.0),
);

Expand Down
6 changes: 5 additions & 1 deletion keyset-drawing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl<'a> Options<'a> {

#[cfg(test)]
mod tests {
use geom::{Mm, DOT_PER_MM};
use isclose::assert_is_close;
use profile::Profile;

Expand All @@ -186,7 +187,10 @@ mod tests {
.show_keys(false)
.show_margin(true);

assert_is_close!(options.profile.typ.depth(), 1.0);
assert_is_close!(
options.profile.typ.depth(),
Length::<Mm>::new(1.0) * DOT_PER_MM
);
assert_eq!(options.font.num_glyphs(), 3); // .notdef, A, V
assert_is_close!(options.scale, 2.0);
}
Expand Down
8 changes: 4 additions & 4 deletions keyset-drawing/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ mod tests {
<path d="M25 90c0 -35.89851 29.10149 -65.00001 64.99999 -65.00001l820 0c35.89851 0 65 29.10149 65 65l0 820c0 35.89851 -29.1015 65 -65 65l-820 0c-35.89851 0 -64.99999 -29.1015 -64.99999 -65.00001z" fill="#cccccc" stroke="#aeaeae" stroke-width="10"/>
<path d="M170 120c0 -35.89851 29.10149 -65.00001 64.99999 -65.00001l530 0c35.89851 0 65 29.10149 65 65l0 605c0 35.89851 -29.1015 65 -65 65l-530 0c-35.89851 0 -64.99999 -29.1015 -64.99999 -65.00001z" fill="#cccccc" stroke="#aeaeae" stroke-width="10"/>
<path d="M220 105l560 0l0 635l-560 0z" fill="none" stroke="#ff0000" stroke-width="5"/>
<path d="M220 299.44446l0 -194.44446l126.36166 0l0 194.44446l-126.36166 -0zM235.52287 270.30502l37.03704 -68.0828l-37.03704 -68.0828l0 136.16559zM244.23747 120.52287l38.94336 69.98912l38.94336 -69.98912l-77.88672 -0zM330.8388 134.13943l-37.03704 68.0828l37.03704 68.0828l0 -136.16559zM322.1242 283.92157l-38.94336 -69.98912l-38.94336 69.98912l77.88672 0z" fill="#000000" stroke="none"/>
<path d="M653.63837 299.44446l0 -194.44446l126.36166 0l0 194.44446l-126.36166 -0zM669.1612 270.30502l37.03704 -68.0828l-37.03704 -68.0828l0 136.16559zM677.87573 120.52287l38.94336 69.98912l38.94336 -69.98912l-77.88672 -0zM764.4771 134.13943l-37.03704 68.0828l37.03704 68.0828l0 -136.16559zM755.76245 283.92157l-38.94336 -69.98912l-38.94336 69.98912l77.88672 0z" fill="#000000" stroke="none"/>
<path d="M220 740l0 -194.44446l126.36166 0l0 194.44446l-126.36166 -0zM235.52287 710.86053l37.03704 -68.0828l-37.03704 -68.0828l0 136.16559zM244.23747 561.0784l38.94336 69.98912l38.94336 -69.98912l-77.88672 -0zM330.8388 574.69495l-37.03704 68.0828l37.03704 68.0828l0 -136.16559zM322.1242 724.4771l-38.94336 -69.98912l-38.94336 69.98912l77.88672 0z" fill="#000000" stroke="none"/>
<path d="M653.63837 740l0 -194.44446l126.36166 0l0 194.44446l-126.36166 -0zM669.1612 710.86053l37.03704 -68.0828l-37.03704 -68.0828l0 136.16559zM677.87573 561.0784l38.94336 69.98912l38.94336 -69.98912l-77.88672 -0zM764.4771 574.69495l-37.03704 68.0828l37.03704 68.0828l0 -136.16559zM755.76245 724.4771l-38.94336 -69.98912l-38.94336 69.98912l77.88672 0z" fill="#000000" stroke="none"/>
<path d="M220 299.44446l0 -194.44444l126.36166 0l0 194.44444l-126.36166 -0zM235.52287 270.30502l37.03704 -68.08279l-37.03704 -68.08279l0 136.16557zM244.23747 120.52289l38.94336 69.98911l38.94336 -69.98911l-77.88671 -0zM330.83878 134.13945l-37.03704 68.08279l37.03704 68.08279l0 -136.16557zM322.12418 283.92157l-38.94336 -69.98911l-38.94336 69.98911l77.88671 0z" fill="#000000" stroke="none"/>
<path d="M653.6383 299.44446l0 -194.44444l126.36166 0l0 194.44444l-126.36166 -0zM669.16113 270.30502l37.03704 -68.08279l-37.03704 -68.08279l0 136.16557zM677.87573 120.52289l38.94336 69.98911l38.94336 -69.98911l-77.88671 -0zM764.47705 134.13945l-37.03704 68.08279l37.03704 68.08279l0 -136.16557zM755.76245 283.92157l-38.94336 -69.98911l-38.94336 69.98911l77.88671 0z" fill="#000000" stroke="none"/>
<path d="M220 740l0 -194.44444l126.36166 0l0 194.44444l-126.36166 -0zM235.52287 710.86053l37.03704 -68.08279l-37.03704 -68.08279l0 136.16557zM244.23747 561.0784l38.94336 69.98911l38.94336 -69.98911l-77.88671 -0zM330.83878 574.695l-37.03704 68.08279l37.03704 68.08279l0 -136.16557zM322.12418 724.4771l-38.94336 -69.98911l-38.94336 69.98911l77.88671 0z" fill="#000000" stroke="none"/>
<path d="M653.6383 740l0 -194.44444l126.36166 0l0 194.44444l-126.36166 -0zM669.16113 710.86053l37.03704 -68.08279l-37.03704 -68.08279l0 136.16557zM677.87573 561.0784l38.94336 69.98911l38.94336 -69.98911l-77.88671 -0zM764.47705 574.695l-37.03704 68.08279l37.03704 68.08279l0 -136.16557zM755.76245 724.4771l-38.94336 -69.98911l-38.94336 69.98911l77.88671 0z" fill="#000000" stroke="none"/>
</g>
</svg>"##
)
Expand Down
56 changes: 45 additions & 11 deletions keyset-profile/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@ use super::{BarProps, BumpProps, Profile, TopSurface};

pub use error::{Error, Result};

impl<'de> Deserialize<'de> for Type {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(serde::Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
enum RawType {
Cylindrical {
depth: f32,
},
Spherical {
depth: f32,
},
#[serde(alias = "chiclet")]
Flat,
}

RawType::deserialize(deserializer).map(|typ| {
// Convert to Length
match typ {
RawType::Cylindrical { depth } => Self::Cylindrical {
depth: Length::<Mm>::new(depth) * DOT_PER_MM,
},
RawType::Spherical { depth } => Self::Spherical {
depth: Length::<Mm>::new(depth) * DOT_PER_MM,
},
RawType::Flat => Self::Flat,
}
})
}
}

impl<'de> Deserialize<'de> for ScoopProps {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
Expand All @@ -28,7 +61,7 @@ impl<'de> Deserialize<'de> for ScoopProps {
RawScoopProps::deserialize(deserializer).map(|props| {
// Convert to Length
Self {
depth: Length::<Mm>::new(props.depth),
depth: Length::<Mm>::new(props.depth) * DOT_PER_MM,
}
})
}
Expand All @@ -50,8 +83,8 @@ impl<'de> Deserialize<'de> for BarProps {
RawBarProps::deserialize(deserializer).map(|props| {
// Convert to Length
Self {
size: Size::<Mm>::new(props.width, props.height),
y_offset: Length::<Mm>::new(props.y_offset),
size: Size::<Mm>::new(props.width, props.height) * DOT_PER_MM,
y_offset: Length::<Mm>::new(props.y_offset) * DOT_PER_MM,
}
})
}
Expand All @@ -72,8 +105,8 @@ impl<'de> Deserialize<'de> for BumpProps {
RawBumpProps::deserialize(deserializer).map(|props| {
// Convert to Length
Self {
diameter: Length::<Mm>::new(props.diameter),
y_offset: Length::<Mm>::new(props.y_offset),
diameter: Length::<Mm>::new(props.diameter) * DOT_PER_MM,
y_offset: Length::<Mm>::new(props.y_offset) * DOT_PER_MM,
}
})
}
Expand Down Expand Up @@ -192,6 +225,7 @@ impl<'de> Deserialize<'de> for Profile {
.legend
.into_iter()
.map(|(i, props)| {
let height = Length::<Dot>::new(props.size);
let Rect {
min: props_min,
max: props_max,
Expand All @@ -200,9 +234,9 @@ impl<'de> Deserialize<'de> for Profile {
min: raw_min,
max: raw_max,
} = raw_data.top.rect();
let offsets =
let offset =
SideOffsets::from_vectors_inner(props_min - raw_min, props_max - raw_max);
((i, props.size), (i, offsets))
((i, height), (i, offset))
})
.unzip();

Expand All @@ -228,16 +262,16 @@ mod tests {
let bar_props: BarProps =
toml::from_str("width = 3.85\nheight = 0.4\ny-offset = 5.05").unwrap();

assert_is_close!(bar_props.size, Size::<Mm>::new(3.85, 0.4));
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(5.05));
assert_is_close!(bar_props.size, Size::<Mm>::new(3.85, 0.4) * DOT_PER_MM);
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(5.05) * DOT_PER_MM);
}

#[test]
fn deserialize_bump_props() {
let bar_props: BumpProps = toml::from_str("diameter = 0.4\ny-offset = -0.2").unwrap();

assert_is_close!(bar_props.diameter, Length::<Mm>::new(0.4));
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(-0.2));
assert_is_close!(bar_props.diameter, Length::<Mm>::new(0.4) * DOT_PER_MM);
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(-0.2) * DOT_PER_MM);
}

#[test]
Expand Down
Loading

0 comments on commit ef15f40

Please sign in to comment.