A crate for vector path operations.
This crate provides a complete toolkit for working with vector paths, including path construction, transformation, manipulation, and rendering.
The crate's design philosophy treats paths as data streams, where all operations
are implemented as type transformations on these streams. For example, converting
curves to straight lines is a transformation from Iterator<Item = PathCmd> to
Iterator<Item = LinearCmd>. This approach enables flexible composition of
operations - you can chain dash transformations, stroke operations, and linearization
in any order, with the sequence determining the processing pipeline.
Features
std: Enables the standard library for enhanced functionalitylibm: Provides mathematical functions via the libm library
Constructing Paths
Path construction is built around the PathCore trait, which serves as the fundamental
interface for path building operations. The trait defines six essential methods:
current: Retrieves the current endpoint of the pathmove_to: Moves to a specified point without drawingline_to: Draws a straight line to the specified pointquadratic_to: Draws a quadratic Bézier curve to the specified pointcubic_to: Draws a cubic Bézier curve to the specified pointclose: Closes the current path segment
Building upon PathCore, several extension traits provide enhanced functionality:
PathRelative: Enables relative path construction operationsPathExtend: Advanced curve operations including arcs and connectionsPathGeometry: Basic geometric shapes like circles, ellipses, and rectanglesPathCanvas: Web Canvas-compatible methods such as arc and arcToPathSvg: SVG-compatible path operations including arc commands
Any type implementing PathCore automatically gains these extension traits,
allowing for seamless path construction using the full feature set.
Additionally, SVG path format support is provided through the SvgCore trait,
enabling parsing of SVG path data strings.
use *;
// Basic path construction
let mut path = default;
path.move_to;
path.line_to;
path.cubic_to;
path.close;
// Using geometric shapes
let mut path = default;
path.add_rect; // Rectangle
path.add_circle; // Circle
path.add_ellipse; // Ellipse
path.add_round_rect;
path.add_pie;
// Parse SVG path string
let mut path = default;
path.svg_path_str.unwrap;
Manipulating Paths
Path manipulation is centered around iterator-based operations. The crate defines two primary path command structures:
PathCmd: Complete path commands supporting all operations including lines, quadratic and cubic Bézier curves, and path closureLinearCmd: Simplified commands containing only straight lines and closure operations
The crate provides iterator extensions for advanced operations:
PathCmdFlow: ExtendsIterator<Item = PathCmd>with curve operations including linearization, dashing, and rasterizationLinearCmdFlow: ExtendsIterator<Item = LinearCmd>primarily for tessellation operations
Conversion between command types is straightforward: Iterator<Item = PathCmd> can be
converted to Iterator<Item = LinearCmd> using the linearize method.
use *;
// Create a path with curves
let mut path = default;
path.add_circle;
// Linearize curves to straight lines
path.iter.linearize;
// Apply transformations
path.iter
.translate
.rotate
.scale;
// Apply dash pattern
let dash = new.unwrap;
path.iter.dash;
// Compute bounding box
let mut bounds = default;
path.iter.bounds;
// Calculate path length
path.iter.length;
// Convert to SVG string
path.iter.svgstr;
Rasterizing Paths
Paths can be converted to grayscale images through the rasterization process.
First rasterize the path into a VecStorage container, then render to a Mask
bitmap using fill rules (non-zero or even-odd).
To stroke a path, first apply the stroke method on PathCmdFlow followed by
the rastive method, then render the storage to a mask.
use *;
// Create a path
let mut path = default;
path.add_circle;
// Rasterize to storage
let mut storage = new;
path.iter.rastive;
// Render to mask using non-zero fill rule
let mut mask = new;
storage.non_zero;
// Stroke and rasterize
let mut stroker = new;
path.iter.stroke.rastive;
storage.even_odd;
Tessellating Paths
The crate implements two distinct tessellation algorithms:
- Traper: Utilizes scanline algorithms optimized for fill operations
- Striper: Constructs triangle strips along paths, ideal for stroke operations
The Mesh type provides a convenient container for storing tessellation results.
use *;
// Create a path
let mut path = default;
path.add_circle;
// Fill using Non-Zero rule
let mut mesh = default;
path.iter
.linearize
.iter
.non_zero;
// Fill using Even-Odd rule
let mut mesh = default;
path.iter
.linearize
.iter
.even_odd;
Complete Example
use *;
License
This project is licensed under the MIT license. See the LICENSE file for complete licensing details.