Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrects padding #88

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions dessin/src/contrib/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, PartialEq, Shape)]
pub struct Padding<T> {
#[shape(into)]
pub shape: T,

pub padding_left: f32,
pub padding_right: f32,
pub padding_top: f32,
pub padding_bottom: f32,
}

impl<T> Default for Padding<T>
where
T: Default,
{
fn default() -> Self {
Padding {
shape: T::default(),
padding_left: 0.,
padding_right: 0.,
padding_top: 0.,
padding_bottom: 0.,
}
}
}

impl<T> Padding<T> {
/// Wrap a [`Shape`] with Padding
#[inline]
Expand Down Expand Up @@ -141,3 +158,74 @@ impl<T: ShapeOp> ShapeOp for Padding<T> {
self.shape.global_transform(parent_transform)
}
}

// creates 2 tests to check that the code works
#[cfg(test)]
mod tests {
use crate::prelude::*;

#[test]
fn similar_op_1() {
let rectangle_1 = dessin2!(Rectangle!(width = 3., height = 2., translate = [1., 0.]));

let base_1 = dessin2!(Padding<Style<Rectangle>>(
shape = rectangle_1,
padding_left = 1.5,
padding_right = 1.,
padding_top = 0.8,
padding_bottom = 1.,
));

let rectangle_2 = dessin2!(Rectangle!(scale = [5.5, 3.8], translate = [0.75, -0.1]));

let base_2 = dessin2!(Padding<Style<Rectangle>>(
shape = rectangle_2,
padding_left = 0.,
padding_right = 0.,
padding_top = 0.,
padding_bottom = 0.,
));

let base_1 = Shape::from(base_1);
let base_2 = Shape::from(base_2);

assert_eq!(
base_1.local_bounding_box().straigthen(),
base_2.local_bounding_box().straigthen()
);
}

#[test]
fn similar_op_2() {
let test_1 = dessin2!([
Circle!(radius = 1., translate = [0.5, 0.]),
Rectangle!(width = 1., height = 0.4, translate = [2., 0.])
]);

let base_1 = dessin2!(Padding<Shape>(
shape = test_1,
padding_left = 1.5,
padding_right = 1.,
padding_top = 0.8,
padding_bottom = 1.,
));

let rectangle = dessin2!(Rectangle!(scale = [5.5, 3.8], translate = [0.75, -0.1]));

let base_2 = dessin2!(Padding<Style<Rectangle>>(
shape = rectangle,
padding_left = 0.,
padding_right = 0.,
padding_top = 0.,
padding_bottom = 0.,
));

let base_1 = Shape::from(base_1);
let base_2 = Shape::from(base_2);

assert_eq!(
base_1.local_bounding_box().straigthen(),
base_2.local_bounding_box().straigthen()
);
}
}
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ path = "./line_with_macro.rs"
[[example]]
name = "line_without_macro"
path = "./line_without_macro.rs"

[[example]]
name = "purple_padding"
path = "./purple_padding.rs"
1 change: 1 addition & 0 deletions examples/out/padding.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions examples/purple_padding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use dessin::prelude::*;
use project_root::get_project_root;
use std::fs;

fn main() {
let rectangle_1 = dessin2!(Rectangle!(
width = 3.,
height = 2.,
translate = [1., 0.],
fill = rgb(255, 0, 0)
));

let base = dessin2!(Padding<Shape>( // here, we can replace 'Shape' with 'Rectangle' but in case we want to use the
// Padding to a multiple geometric form, using Shape become a must
shape = rectangle_1.clone(),
padding_left = 1.5,
padding_right = 1.,
padding_top = 0.8,
padding_bottom = 1.,

));

let rectangle_2 = dessin2!(Rectangle!(
width = 5.5,
height = 3.8,
stroke = Stroke::Full {
color: rgb(0, 150, 0),
width: 0.1
},
translate = [0.75, -0.1]
));

let base = Shape::from(base);
let rectangle_1 = Shape::from(rectangle_1);
let rectangle_2 = Shape::from(rectangle_2);

// creates a group
let mut group = Group::default();

group.shapes = vec![];

group.shapes.push(base);
group.shapes.push(rectangle_1);
group.shapes.push(rectangle_2);

// prints in svg version
fs::write(
get_project_root().unwrap().join("examples/out/padding.svg"),
dessin_svg::to_string(&Shape::Group(group)).unwrap(),
)
.unwrap();
}
Loading