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

Svg path #265

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.vscode
15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ categories = ["graphics"]
features = ["mint", "schemars", "serde"]

[features]
default = ["std"]
default = ["std", "svg_path"]
std = []
serde = ["smallvec/serde", "arrayvec/serde", "serde1"]
svg_path = ["nom"]

[dependencies]
smallvec = "1.10.0"
anyhow = "1.0.70"
nom = { version = "7.1.3", optional = true }

[dependencies.arrayvec]
version = "0.7.1"
Expand All @@ -33,16 +40,20 @@ optional = true
version = "0.8.6"
optional = true

[dependencies.serde]
[dependencies.serde1]
version = "1.0.105"
package = "serde"
optional = true
default-features = false
features = ["derive"]

# This is used for research but not really needed; maybe refactor.
[dev-dependencies]
rand = "0.8.0"
snog = { git = "https://github.com/derekdreery/snog" }

[target.'cfg(target_arch="wasm32")'.dev-dependencies]
getrandom = { version = "0.2.0", features = ["js"] }

[patch.crates-io]
kurbo = { path = "." }
69 changes: 69 additions & 0 deletions examples/svg_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use kurbo::{
paths::svg::{self, OneVec},
Affine, Line, Point, Size,
};
use snog::{
peniko::{Color, Stroke},
App, RenderCtx,
};

fn main() {
let data = Data::new();
App::new_with_data(data).with_render(render).run()
}

// TODO both the lifetime of the RenderCtx and the ref to user data could be the same - nothing is
// gained by having one longer than the other.
fn render(data: &mut Data, mut ctx: RenderCtx<'_>) {
let Size { width, height } = ctx.screen().size();

let stroke = Stroke::new(0.005);
let scale = Affine::scale_non_uniform(width, height);
let brush = Color::WHITE;
ctx.stroke(&stroke, scale, &brush, None, &data.path);
}

struct Data {
path: svg::Path,
}

impl Data {
fn new() -> Self {
let path = svg::Path::try_from(vec![
svg::PathEl::MoveTo(OneVec::single(Point::new(0.1, 0.1))),
svg::PathEl::Bearing(std::f64::consts::FRAC_PI_2),
svg::PathEl::LineToRel(
OneVec::try_from(vec![Point::new(0.2, 0.0), Point::new(-0.1, -0.2)]).unwrap(),
),
svg::PathEl::VertRel(OneVec::single(-0.2)),
svg::PathEl::CubicToRel(OneVec::single(svg::CubicTo {
to: Point::new(0.2, 0.0),
ctrl1: Point::new(0.1, 0.1),
ctrl2: Point::new(0.1, -0.1),
})),
svg::PathEl::SmoothCubicToRel(
OneVec::try_from(vec![
svg::SmoothCubicTo {
to: Point::new(0.2, 0.0),
ctrl2: Point::new(0.1, -0.1),
},
svg::SmoothCubicTo {
to: Point::new(0.2, 0.0),
ctrl2: Point::new(0.1, -0.1),
},
])
.unwrap(),
),
svg::PathEl::QuadToRel(OneVec::single(svg::QuadTo {
to: Point::new(0.0, 0.1),
ctrl: Point::new(0.1, 0.1),
})),
svg::PathEl::SmoothQuadToRel(
OneVec::try_from(vec![Point::new(0.0, 0.1), Point::new(0.0, 0.1)]).unwrap(),
),
])
.unwrap();

Data { path }
}
}
25 changes: 25 additions & 0 deletions src/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ impl BezPath {
BezPath(v)
}

/// Create a BezPath with segments corresponding to the sequence of `PathSeg`s.
///
/// This constructor will insert [`PathEl::MoveTo`]s as needed so that drawing the returned
/// `BezPath` is equivalent to drawing each of the `PathSeg`s.
pub fn from_path_segments(segments: impl Iterator<Item = PathSeg>) -> BezPath {
let mut path_elements = Vec::new();
let mut current_pos = None;

for segment in segments {
let start = segment.start();
if Some(start) != current_pos {
path_elements.push(PathEl::MoveTo(start));
};
path_elements.push(match segment {
PathSeg::Line(l) => PathEl::LineTo(l.p1),
PathSeg::Quad(q) => PathEl::QuadTo(q.p1, q.p2),
PathSeg::Cubic(c) => PathEl::CurveTo(c.p1, c.p2, c.p3),
});

current_pos = Some(segment.end());
}

BezPath::from_vec(path_elements)
}

/// Removes the last [`PathEl`] from the path and returns it, or `None` if the path is empty.
pub fn pop(&mut self) -> Option<PathEl> {
self.0.pop()
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ mod line;
mod mindist;
pub mod offset;
mod param_curve;
#[cfg(any(feature = "svg_path"))]
pub mod paths;
mod point;
mod quadbez;
mod quadspline;
Expand All @@ -107,8 +109,8 @@ mod rounded_rect_radii;
mod shape;
pub mod simplify;
mod size;
#[cfg(feature = "std")]
mod svg;
//#[cfg(feature = "std")]
//mod svg;
mod translate_scale;
mod vec2;

Expand All @@ -130,7 +132,7 @@ pub use crate::rounded_rect::*;
pub use crate::rounded_rect_radii::*;
pub use crate::shape::*;
pub use crate::size::*;
#[cfg(feature = "std")]
pub use crate::svg::*;
//#[cfg(feature = "std")]
//pub use crate::svg::*;
pub use crate::translate_scale::*;
pub use crate::vec2::*;
8 changes: 8 additions & 0 deletions src/paths/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Different path representations, useful in different contexts.
//!
//! In `kurbo`, the canonical path representation is `BezPath`, which is always drawn with perfect
//! accuracy. However, sometimes it is useful to represent paths in different ways, for example
//! circular arcs are often used in architecture, and SVG defines a different path model to
//! `BezPath`, so they cannot be used interchangeably.
#[cfg(feature = "svg_path")]
pub mod svg;
Loading