Skip to content

Commit

Permalink
Fix last small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLemoine committed Feb 26, 2024
1 parent ad818c9 commit 5dfde4c
Show file tree
Hide file tree
Showing 36 changed files with 141 additions and 190 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Documentation on [docs.rs](https://docs.rs/dessin/0.8.2-pre/)

```rust
use dessin::prelude::*;
use dessin_svg::ToSVG;

#[derive(Default, Shape)]
struct MyShape {
Expand Down Expand Up @@ -58,7 +57,7 @@ fn main() {
MyShape(say_this = "Hello world"),
]);

let svg = dessin.to_svg().unwrap();
let svg = dessin_svg::to_string(&dessin).unwrap();
}

```
96 changes: 38 additions & 58 deletions dessin-svg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use nalgebra::{Scale2, Transform2};
use std::{
fmt::{self, Write},
io::Cursor,
ops::Deref,
};

#[derive(Debug)]
Expand Down Expand Up @@ -356,67 +355,48 @@ impl Exporter for SVGExporter {
}
}

pub struct SVG {
pub input_dessin: Shape,
pub options: SVGOptions,
}

impl SVG {
pub fn to_string_with_options(&self, options: SVGOptions) -> Result<String, SVGError> {
let (min_x, min_y, span_x, span_y) = match options.viewport {
ViewPort::ManualCentered { width, height } => {
(-width / 2., -height / 2., width, height)
}
ViewPort::ManualViewport {
x,
y,
width,
height,
} => (x - width / 2., y - height / 2., width, height),
ViewPort::AutoCentered => {
let bb = self.input_dessin.local_bounding_box().straigthen();

let mirror_bb = bb
.transform(&nalgebra::convert::<_, Transform2<f32>>(Scale2::new(
-1., -1.,
)))
.into_straight();

let overall_bb = bb.join(mirror_bb);

(
-overall_bb.width() / 2.,
-overall_bb.height() / 2.,
overall_bb.width(),
overall_bb.height(),
)
}
ViewPort::AutoBoundingBox => {
let bb = self.input_dessin.local_bounding_box().straigthen();

(bb.top_left().x, -bb.top_left().y, bb.width(), bb.height())
}
};
pub fn to_string_with_options(shape: &Shape, options: SVGOptions) -> Result<String, SVGError> {
let (min_x, min_y, span_x, span_y) = match options.viewport {
ViewPort::ManualCentered { width, height } => (-width / 2., -height / 2., width, height),
ViewPort::ManualViewport {
x,
y,
width,
height,
} => (x - width / 2., y - height / 2., width, height),
ViewPort::AutoCentered => {
let bb = shape.local_bounding_box().straigthen();

let mirror_bb = bb
.transform(&nalgebra::convert::<_, Transform2<f32>>(Scale2::new(
-1., -1.,
)))
.into_straight();

let overall_bb = bb.join(mirror_bb);

(
-overall_bb.width() / 2.,
-overall_bb.height() / 2.,
overall_bb.width(),
overall_bb.height(),
)
}
ViewPort::AutoBoundingBox => {
let bb = shape.local_bounding_box().straigthen();

let mut exporter = SVGExporter::new(min_x, min_y, span_x, span_y);
(bb.top_left().x, -bb.top_left().y, bb.width(), bb.height())
}
};

let parent_transform = nalgebra::convert(Scale2::new(1., -1.));
self.input_dessin
.write_into_exporter(&mut exporter, &parent_transform)?;
let mut exporter = SVGExporter::new(min_x, min_y, span_x, span_y);

Ok(exporter.finish())
}
let parent_transform = nalgebra::convert(Scale2::new(1., -1.));
shape.write_into_exporter(&mut exporter, &parent_transform)?;

pub fn to_string(&self) -> Result<String, SVGError> {
self.to_string_with_options(self.options.clone())
}
Ok(exporter.finish())
}

impl<T: Into<Shape>> From<T> for SVG {
fn from(value: T) -> Self {
SVG {
input_dessin: value.into(),
options: SVGOptions::default(),
}
}
pub fn to_string(shape: &Shape) -> Result<String, SVGError> {
to_string_with_options(shape, SVGOptions::default())
}
20 changes: 1 addition & 19 deletions examples/432technologies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ use dessin::{
};
use dessin_image::ToImage;
use dessin_pdf::ToPDF;
use dessin_svg::SVG;
use project_root::get_project_root;
use std::{
f32::consts::{FRAC_PI_4, FRAC_PI_8, PI},
fs,
};
use std::f32::consts::{FRAC_PI_4, FRAC_PI_8, PI};

const C: Color = rgb(0x3b, 0x54, 0x85);
fn c(a: u8) -> Color {
Expand Down Expand Up @@ -306,20 +302,6 @@ fn main() {

let path = get_project_root().unwrap().join("examples/out/");

// SVG
fs::write(
"./target/432.svg",
SVG::from(dessin.clone()).to_string().unwrap(),
)
.unwrap();

// // PDF
// fs::write(
// path.join("432technologies.pdf"),
// dessin.to_pdf().unwrap().save_to_bytes().unwrap(),
// )
// .unwrap();

// Image
dessin2!({ dessin }(scale = [5., 5.]))
.rasterize()
Expand Down
12 changes: 10 additions & 2 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use project_root::get_project_root;
fn main() {
let skip_animation = std::env::var("NO_ANIMATION") == Ok("1".to_string());

let path = get_project_root()
.unwrap()
.join("examples/out/animation.svg");

let test_img = dessin2!(polygons::Triangle!(fill = Color::BLUE) > (scale = [50., 50.]))
.rasterize()
.unwrap();
Expand All @@ -22,8 +26,12 @@ fn main() {
);

loop {
let final_image = SVG::from(frame.clone()).to_string().unwrap();
fs::write("test.svg", final_image).unwrap();
let final_image = to_string(&frame.clone()).unwrap();
fs::write(&path, final_image).unwrap();

if skip_animation {
break;
}

std::thread::sleep(Duration::from_millis(100));
let mut t = triangle.write().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions examples/any_triangle_with_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs;

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand Down Expand Up @@ -36,7 +35,7 @@ fn main() {
get_project_root()
.unwrap()
.join("examples/out/any_triangle.svg"),
SVG::from(triangle).to_string().unwrap(),
dessin_svg::to_string(&triangle).unwrap(),
)
.unwrap();
}
3 changes: 1 addition & 2 deletions examples/any_triangle_without_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs;

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand Down Expand Up @@ -38,7 +37,7 @@ fn main() {
get_project_root()
.unwrap()
.join("examples/out/any_triangle.svg"),
SVG::from(triangle).to_string().unwrap(),
dessin_svg::to_string(&triangle.into()).unwrap(),
)
.unwrap();
}
3 changes: 1 addition & 2 deletions examples/arc_with_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{f32::consts::PI, fs};

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand All @@ -23,7 +22,7 @@ fn main() {
// prints in svg version
fs::write(
get_project_root().unwrap().join("examples/out/arc.svg"),
SVG::from(arc).to_string().unwrap(),
dessin_svg::to_string(&arc).unwrap(),
)
.unwrap();
}
3 changes: 1 addition & 2 deletions examples/arc_without_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{f32::consts::PI, fs};

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand All @@ -26,7 +25,7 @@ fn main() {
// prints in svg version
fs::write(
get_project_root().unwrap().join("examples/out/arc.svg"),
SVG::from(arc).to_string().unwrap(),
dessin_svg::to_string(&arc.into()).unwrap(),
)
.unwrap();
}
8 changes: 5 additions & 3 deletions examples/basque_cross_with_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{f32::consts::PI, fs};

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
let basque_cross: Shape = dessin2!([
Expand Down Expand Up @@ -47,8 +47,10 @@ fn main() {

// prints in svg version
fs::write(
"./out/basque_cross.svg",
SVG::from(basque_cross).to_string().unwrap(),
get_project_root()
.unwrap()
.join("examples/out/basque_cross.svg"),
dessin_svg::to_string(&basque_cross).unwrap(),
)
.unwrap();
}
11 changes: 6 additions & 5 deletions examples/basque_cross_without_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{f32::consts::PI, fs};

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;
use std::{f32::consts::PI, fs};

fn main() {
let circle_point = Circle::default().with_radius(0.01);
Expand Down Expand Up @@ -75,8 +74,10 @@ fn main() {

// prints in svg version with Shape::from(...) -> Shape::Group(group) because of the group
fs::write(
"./out/basque_cross.svg",
SVG::from(Shape::Group(group)).to_string().unwrap(),
get_project_root()
.unwrap()
.join("examples/out/basque_cross.svg"),
dessin_svg::to_string(&Shape::Group(group)).unwrap(),
)
.unwrap();
}
3 changes: 1 addition & 2 deletions examples/blue_triangle_with_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use dessin::{
nalgebra::{Rotation2, Scale2},
prelude::{polygons::Triangle, *},
};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand All @@ -31,7 +30,7 @@ fn main() {
get_project_root()
.unwrap()
.join("examples/out/blue_triangle.svg"),
SVG::from(triangle).to_string().unwrap(),
dessin_svg::to_string(&triangle).unwrap(),
)
.unwrap();
}
3 changes: 1 addition & 2 deletions examples/blue_triangle_without_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use dessin::{
nalgebra::{Rotation2, Scale2},
prelude::{polygons::Triangle, *},
};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand Down Expand Up @@ -32,7 +31,7 @@ fn main() {
get_project_root()
.unwrap()
.join("examples/out/blue_triangle.svg"),
SVG::from(triangle).to_string().unwrap(),
dessin_svg::to_string(&triangle.into()).unwrap(),
)
.unwrap();
}
34 changes: 16 additions & 18 deletions examples/diamond_with_macro.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs;

use dessin::{nalgebra::Rotation2, prelude::*};
use dessin_svg::SVG;
use project_root::get_project_root;

fn main() {
Expand All @@ -14,34 +13,33 @@ fn main() {

Diamond: #(

// chooses a width of 4 for following the x axis
width={4.}
// chooses a width of 4 for following the x axis
width={4.}

// chooses a size of 5 between the origin and the diamond top apex following the y axis
height_top={5.}
// chooses a size of 5 between the origin and the diamond top apex following the y axis
height_top={5.}

// chooses a size of 3 between the origin and the diamond bottom apex following the y axis
height_bottom={3.}
// chooses a size of 3 between the origin and the diamond bottom apex following the y axis
height_bottom={3.}

// paints the inside of the diamond in diamond color
fill={rgb(185,242,255)}
// paints the inside of the diamond in diamond color
fill={rgb(185,242,255)}

// creates a black margin with a width of 0.1 (0.05 outside and the same inside the diamond)
stroke={Stroke::Full { color: rgb(0, 0, 0), width: 0.1}}
// creates a black margin with a width of 0.1 (0.05 outside and the same inside the diamond)
stroke={Stroke::Full { color: rgb(0, 0, 0), width: 0.1}}

// chooses a rotation of -10 radians in the trigonometric direction
rotate={Rotation2::new(-10_f32.to_radians())}

// moves of 15 following the x axis and 5 following the y axis
translate={[15.,5.]}
),
// chooses a rotation of -10 radians in the trigonometric direction
rotate={Rotation2::new(-10_f32.to_radians())}

// moves of 15 following the x axis and 5 following the y axis
translate={[15.,5.]}
),
]);

// prints in svg version
fs::write(
get_project_root().unwrap().join("examples/out/diamond.svg"),
SVG::from(diamond).to_string().unwrap(),
dessin_svg::to_string(&diamond).unwrap(),
)
.unwrap();
}
Loading

0 comments on commit 5dfde4c

Please sign in to comment.