-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
326 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Structure to hold the state for the [`Draw`] API between commands. | ||
|
||
use kurbo::Affine; | ||
use vsvg::{ | ||
Draw, IntoBezPathTolerance, Layer, Path, PathMetadata, PathTrait, Transforms, DEFAULT_TOLERANCE, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct DrawState { | ||
pub transform: Affine, | ||
pub metadata: PathMetadata, | ||
|
||
/// used to convert shapes to Béziers | ||
pub tolerance: f64, | ||
} | ||
|
||
impl Default for DrawState { | ||
fn default() -> Self { | ||
Self { | ||
transform: Affine::default(), | ||
metadata: PathMetadata::default(), | ||
tolerance: DEFAULT_TOLERANCE, | ||
} | ||
} | ||
} | ||
|
||
impl Transforms for DrawState { | ||
fn transform(&mut self, affine: &Affine) -> &mut Self { | ||
self.transform = *affine * self.transform; | ||
self | ||
} | ||
} | ||
|
||
pub struct LayerDrawer<'layer, 'state> { | ||
pub(crate) state: &'state DrawState, | ||
pub(crate) layer: &'layer mut Layer, | ||
} | ||
|
||
impl<'layer, 'state> Draw for LayerDrawer<'layer, 'state> { | ||
fn add_path<T: IntoBezPathTolerance>(&mut self, path: T) -> &mut Self { | ||
let mut path: Path = Path::from_tolerance(path, self.state.tolerance); | ||
*path.metadata_mut() = self.state.metadata.clone(); | ||
path.apply_transform(self.state.transform); | ||
self.layer.paths.push(path); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod cli; | ||
mod commands; | ||
mod draw_state; | ||
|
||
use crate::commands::command_list; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "vsvg-sketch" | ||
version = "0.1.0-alpha.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
vsvg = { path = "../vsvg", features = ["geo"] } | ||
vsvg-viewer = { path = "../vsvg-viewer", optional = true } | ||
kurbo.workspace = true | ||
anyhow.workspace = true | ||
|
||
[dev-dependencies] | ||
|
||
[features] | ||
default = ["viewer"] | ||
viewer = ["dep:vsvg-viewer"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use vsvg_sketch::prelude::*; | ||
|
||
fn main() -> Result { | ||
Sketch::with_page_size(PageSize::A5) | ||
.scale(Units::CM) | ||
.translate(7.0, 6.0) | ||
.circle(0.0, 0.0, 2.5) | ||
.translate(1.0, 4.0) | ||
.rotate_deg(45.0) | ||
.rect(0., 0., 4.0, 1.0) | ||
.show()? | ||
.save("output.svg")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod prelude; | ||
pub mod sketch; | ||
|
||
pub type Result = anyhow::Result<()>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub use crate::{sketch::Sketch, Result}; | ||
pub use vsvg::{Draw, PageSize, Transforms, Units}; | ||
pub use vsvg_viewer::show; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use kurbo::Affine; | ||
use vsvg::path::IntoBezPathTolerance; | ||
use vsvg::{ | ||
Document, DocumentTrait, Draw, LayerID, PageSize, Path, PathMetadata, Transforms, | ||
DEFAULT_TOLERANCE, | ||
}; | ||
|
||
pub struct Sketch { | ||
document: Document, | ||
transform: Affine, | ||
target_layer: LayerID, | ||
tolerance: f64, | ||
path_metadata: PathMetadata, | ||
} | ||
|
||
impl Default for Sketch { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
impl Sketch { | ||
pub fn new() -> Self { | ||
Self::with_document(Document::default()) | ||
} | ||
|
||
pub fn with_page_size(page_size: PageSize) -> Self { | ||
Self::with_document(Document::new_with_page_size(page_size)) | ||
} | ||
|
||
pub fn with_document(mut document: Document) -> Self { | ||
let target_layer = 0; | ||
document.ensure_exists(target_layer); | ||
|
||
Self { | ||
document, | ||
tolerance: DEFAULT_TOLERANCE, | ||
transform: Affine::default(), | ||
target_layer, | ||
path_metadata: PathMetadata::default(), | ||
} | ||
} | ||
|
||
pub fn set_layer(&mut self, layer_id: LayerID) -> &mut Self { | ||
self.document.ensure_exists(layer_id); | ||
self.target_layer = layer_id; | ||
self | ||
} | ||
|
||
pub fn document(&self) -> &Document { | ||
&self.document | ||
} | ||
|
||
pub fn show(&mut self) -> anyhow::Result<&mut Self> { | ||
vsvg_viewer::show(self.document())?; | ||
Ok(self) | ||
} | ||
|
||
pub fn save(&mut self, path: impl AsRef<std::path::Path>) -> anyhow::Result<&mut Self> { | ||
let file = std::io::BufWriter::new(std::fs::File::create(path)?); | ||
self.document.to_svg(file)?; | ||
Ok(self) | ||
} | ||
} | ||
|
||
impl Transforms for Sketch { | ||
fn transform(&mut self, affine: &Affine) -> &mut Self { | ||
self.transform *= *affine; | ||
self | ||
} | ||
} | ||
|
||
impl Draw for Sketch { | ||
fn add_path<T: IntoBezPathTolerance>(&mut self, path: T) -> &mut Self { | ||
let mut path: Path = | ||
Path::from_tolerance_metadata(path, self.tolerance, self.path_metadata.clone()); | ||
|
||
path.apply_transform(self.transform); | ||
|
||
self.document.push_path(self.target_layer, path); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.