Skip to content

Commit

Permalink
Add --flatten <TOL> command to flatten all curves with the provided…
Browse files Browse the repository at this point in the history
… tolerance (#63)
  • Loading branch information
abey79 authored Nov 18, 2023
1 parent 64ecaf8 commit 41d659e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
8 changes: 7 additions & 1 deletion crates/vsvg-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ pub(crate) fn command_list() -> HashMap<Id, CommandDesc<'static>> {
)
)
),
command_decl!(
arg!(--flatten <TOL> "Flatten all curves to line segments with the provided tolerance"),
f64,
|state, tol| state.document = state.document.flatten(*tol).into()
),
command_decl!(
arg!(--dlayer [X] "Set target layer for draw operations"),
LayerID,
Expand Down Expand Up @@ -271,7 +276,8 @@ pub(crate) fn command_list() -> HashMap<Id, CommandDesc<'static>> {
arg!(--stats "Print stats"),
bool,
|state, b| {
println!("{:?}", state.document.stats());
println!("Stats: {:?}", state.document.stats());
println!("Bounds: {:?}", state.document.bounds());
}
),
]
Expand Down
14 changes: 14 additions & 0 deletions crates/vsvg/src/document/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ impl Document {
}
}

impl From<FlattenedDocument> for Document {
fn from(flattened_document: FlattenedDocument) -> Self {
Self {
layers: flattened_document
.layers
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect(),

metadata: flattened_document.metadata,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/vsvg/src/document/flattened_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::BTreeMap;
#[derive(Default, Clone, Debug)]
pub struct FlattenedDocument {
pub layers: BTreeMap<LayerID, FlattenedLayer>,
metadata: DocumentMetadata,
pub(crate) metadata: DocumentMetadata,
}

impl FlattenedDocument {
Expand Down
2 changes: 1 addition & 1 deletion crates/vsvg/src/layer/flattened_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{FlattenedPath, Polyline, Transforms};
#[derive(Default, Clone, Debug)]
pub struct FlattenedLayer {
pub paths: Vec<FlattenedPath>,
metadata: LayerMetadata,
pub(crate) metadata: LayerMetadata,
}

impl FlattenedLayer {
Expand Down
9 changes: 9 additions & 0 deletions crates/vsvg/src/layer/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ impl Layer {
}
}

impl From<FlattenedLayer> for Layer {
fn from(flattened_layer: FlattenedLayer) -> Self {
Self {
paths: flattened_layer.paths.into_iter().map(Path::from).collect(),
metadata: flattened_layer.metadata,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/vsvg/src/path/flattened_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl PathDataTrait for Polyline {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FlattenedPath {
pub data: Polyline,
metadata: PathMetadata,
pub(crate) metadata: PathMetadata,
}

impl Transforms for FlattenedPath {
Expand Down
9 changes: 9 additions & 0 deletions crates/vsvg/src/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ impl<T: IntoBezPath> From<T> for Path {
}
}

impl From<FlattenedPath> for Path {
fn from(path: FlattenedPath) -> Self {
Self {
data: path.data.into_bezpath(),
metadata: path.metadata,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 41d659e

Please sign in to comment.