svg_path/transform
Transform paths and path segments with 2D affine matrices.
Matrices use SVG’s six-value affine form matrix(a b c d e f) and are
applied to column vectors. Use chain(first:, then:) when thinking in
transform application order, and multiply(left:, right:) when thinking in
algebraic matrix multiplication order.
Types
An anchor point on a bounding box.
Top means the visual top edge, box.min.y; Bottom means the visual
bottom edge, box.max.y.
pub type Anchor {
TopLeft
TopCenter
TopRight
CenterLeft
Center
CenterRight
BottomLeft
BottomCenter
BottomRight
}
Constructors
-
TopLeft -
TopCenter -
TopRight -
CenterLeft -
Center -
CenterRight -
BottomLeft -
BottomCenter -
BottomRight
Errors returned while transforming paths or converting matrices.
pub type Error {
DegenerateArcTransform
InvalidMatrix
InvalidTolerance(tolerance: Float)
AffineError(error: affine.Error)
CorrespondenceOutsideTolerance(
mapped: svg_path.Point,
target: svg_path.Point,
tolerance: Float,
)
PathError(error: svg_path.Error)
}
Constructors
-
DegenerateArcTransformAn arc collapsed under the transform and cannot be represented as an arc.
-
InvalidMatrixA matrix contains non-finite values.
-
InvalidTolerance(tolerance: Float)The requested correspondence tolerance must be nonnegative.
-
AffineError(error: affine.Error)Affine correspondence construction failed.
-
CorrespondenceOutsideTolerance( mapped: svg_path.Point, target: svg_path.Point, tolerance: Float, )A constructed matrix failed its point correspondence check.
-
PathError(error: svg_path.Error)An error from the core path model.
Values
pub fn about_point(
transform transform: Matrix,
point point: svg_path.Point,
) -> Matrix
Create a matrix that applies a transform about a point.
pub fn bounding_box(
box: svg_path.BoundingBox,
by transform: Matrix,
) -> Result(svg_path.BoundingBox, Error)
Transform a bounding box by a matrix.
The result is the smallest axis-aligned bounding box containing the transformed corners of the input box.
pub fn chain(first first: Matrix, then second: Matrix) -> Matrix
Chain two transforms in application order.
chain(first: a, then: b) creates a matrix that applies a first and b
second. In algebraic matrix order, this is b * a.
pub fn from_tuple(
values: #(Float, Float, Float, Float, Float, Float),
) -> Matrix
Create an affine matrix from SVG’s six matrix values as a tuple.
pub fn matrix(
a a: Float,
b b: Float,
c c: Float,
d d: Float,
e e: Float,
f f: Float,
) -> Matrix
Create an affine matrix from SVG’s six matrix values.
pub fn multiply(left left: Matrix, right right: Matrix) -> Matrix
Multiply two matrices in algebraic order.
multiply(left: a, right: b) returns a * b.
pub fn path(
path: svg_path.Path,
by transform: Matrix,
) -> Result(svg_path.Path, Error)
Transform every subpath in a path by a matrix.
pub fn path_about_anchor(
input: svg_path.Path,
by transform: Matrix,
anchor anchor: Anchor,
) -> Result(svg_path.Path, Error)
Transform a path about an anchor on its bounding box.
pub fn path_about_point(
input: svg_path.Path,
by transform: Matrix,
point point: svg_path.Point,
) -> Result(svg_path.Path, Error)
Transform a path about a point.
pub fn path_with_arc_collapse(
path: svg_path.Path,
by transform: Matrix,
) -> Result(svg_path.Path, Error)
Transform every subpath in a path, replacing collapsed arcs with lines.
This is the path-level counterpart of subpath_with_arc_collapse.
pub fn point(
point: svg_path.Point,
by transform: Matrix,
) -> svg_path.Point
Transform a point by a matrix.
pub fn point_pair_similarity(
source_start source_start: svg_path.Point,
source_end source_end: svg_path.Point,
target_start target_start: svg_path.Point,
target_end target_end: svg_path.Point,
tolerance tolerance: Float,
) -> Result(Matrix, Error)
Find a translation, rotation, and uniform scale mapping one point pair to another.
The returned matrix maps source_start within tolerance of target_start
and source_end within tolerance of target_end. Errors distinguish an
invalid tolerance, affine construction failure, and a failed correspondence.
pub fn point_triple_map(
source_a source_a: svg_path.Point,
source_b source_b: svg_path.Point,
source_c source_c: svg_path.Point,
target_a target_a: svg_path.Point,
target_b target_b: svg_path.Point,
target_c target_c: svg_path.Point,
tolerance tolerance: Float,
) -> Result(Matrix, Error)
Find an affine transform mapping one point triple to another.
The returned matrix maps source_a, source_b, and source_c within
tolerance of target_a, target_b, and target_c. Errors distinguish an
invalid tolerance, affine construction failure, and a failed correspondence.
pub fn rotate(degrees degrees: Float) -> Matrix
Create a rotation matrix from an angle in degrees. Positive angles rotate visually clockwise in SVG coordinates (y down).
pub fn rotate_path(
input: svg_path.Path,
degrees degrees: Float,
) -> Result(svg_path.Path, Error)
Rotate a path around the origin.
pub fn rotate_point(
input: svg_path.Point,
degrees degrees: Float,
) -> svg_path.Point
Rotate a point around the origin.
pub fn rotate_segment(
input: svg_path.Segment,
degrees degrees: Float,
) -> Result(svg_path.Segment, Error)
Rotate a segment around the origin.
pub fn rotate_subpath(
input: svg_path.Subpath,
degrees degrees: Float,
) -> Result(svg_path.Subpath, Error)
Rotate a subpath around the origin.
pub fn scale_path(
input: svg_path.Path,
factor factor: Float,
) -> Result(svg_path.Path, Error)
Scale a path uniformly.
pub fn scale_point(
input: svg_path.Point,
factor factor: Float,
) -> svg_path.Point
Scale a point uniformly.
pub fn scale_segment(
input: svg_path.Segment,
factor factor: Float,
) -> Result(svg_path.Segment, Error)
Scale a segment uniformly.
pub fn scale_subpath(
input: svg_path.Subpath,
factor factor: Float,
) -> Result(svg_path.Subpath, Error)
Scale a subpath uniformly.
pub fn scale_xy_path(
input: svg_path.Path,
x x: Float,
y y: Float,
) -> Result(svg_path.Path, Error)
Scale a path non-uniformly.
pub fn scale_xy_point(
input: svg_path.Point,
x x: Float,
y y: Float,
) -> svg_path.Point
Scale a point non-uniformly.
pub fn scale_xy_segment(
input: svg_path.Segment,
x x: Float,
y y: Float,
) -> Result(svg_path.Segment, Error)
Scale a segment non-uniformly.
pub fn scale_xy_subpath(
input: svg_path.Subpath,
x x: Float,
y y: Float,
) -> Result(svg_path.Subpath, Error)
Scale a subpath non-uniformly.
pub fn segment(
segment: svg_path.Segment,
by transform: Matrix,
) -> Result(svg_path.Segment, Error)
Transform a segment by a matrix.
Degenerate arc transforms return DegenerateArcTransform; use
segment_with_arc_collapse or segment_to_subpath_with_arc_collapse to convert
collapsed arcs into line-based representations when possible.
pub fn segment_about_anchor(
input: svg_path.Segment,
by transform: Matrix,
anchor anchor: Anchor,
) -> Result(svg_path.Segment, Error)
Transform a segment about an anchor on its bounding box.
pub fn segment_about_point(
input: svg_path.Segment,
by transform: Matrix,
point point: svg_path.Point,
) -> Result(svg_path.Segment, Error)
Transform a segment about a point.
pub fn segment_to_subpath_with_arc_collapse(
input: svg_path.Segment,
by transform: Matrix,
) -> Result(svg_path.Subpath, Error)
Transform a segment, representing collapsed arcs with line segments.
This can represent collapsed arcs as multiple line segments when needed. Matrix validation, source arc conversion, and subpath construction errors still propagate.
pub fn segment_with_arc_collapse(
input: svg_path.Segment,
by transform: Matrix,
) -> Result(svg_path.Segment, Error)
Transform a segment, allowing a collapsed arc to become one line segment.
This returns a single segment. If a collapsed arc needs multiple line
segments to preserve its motion, use segment_to_subpath_with_arc_collapse.
Such a multi-line collapse returns DegenerateArcTransform here. Errors
from matrix validation or source arc conversion are not suppressed.
pub fn skew_x(degrees degrees: Float) -> Matrix
Create an x-axis skew matrix from an angle in degrees.
pub fn skew_x_path(
input: svg_path.Path,
degrees degrees: Float,
) -> Result(svg_path.Path, Error)
Skew a path along the x axis.
pub fn skew_x_point(
input: svg_path.Point,
degrees degrees: Float,
) -> svg_path.Point
Skew a point along the x axis.
pub fn skew_x_segment(
input: svg_path.Segment,
degrees degrees: Float,
) -> Result(svg_path.Segment, Error)
Skew a segment along the x axis.
pub fn skew_x_subpath(
input: svg_path.Subpath,
degrees degrees: Float,
) -> Result(svg_path.Subpath, Error)
Skew a subpath along the x axis.
pub fn skew_y(degrees degrees: Float) -> Matrix
Create a y-axis skew matrix from an angle in degrees.
pub fn skew_y_path(
input: svg_path.Path,
degrees degrees: Float,
) -> Result(svg_path.Path, Error)
Skew a path along the y axis.
pub fn skew_y_point(
input: svg_path.Point,
degrees degrees: Float,
) -> svg_path.Point
Skew a point along the y axis.
pub fn skew_y_segment(
input: svg_path.Segment,
degrees degrees: Float,
) -> Result(svg_path.Segment, Error)
Skew a segment along the y axis.
pub fn skew_y_subpath(
input: svg_path.Subpath,
degrees degrees: Float,
) -> Result(svg_path.Subpath, Error)
Skew a subpath along the y axis.
pub fn subpath(
subpath: svg_path.Subpath,
by transform: Matrix,
) -> Result(svg_path.Subpath, Error)
Transform a subpath by a matrix.
Closed subpaths remain semantically closed when the transformed endpoints can be reconciled.
pub fn subpath_about_anchor(
input: svg_path.Subpath,
by transform: Matrix,
anchor anchor: Anchor,
) -> Result(svg_path.Subpath, Error)
Transform a subpath about an anchor on its bounding box.
pub fn subpath_about_point(
input: svg_path.Subpath,
by transform: Matrix,
point point: svg_path.Point,
) -> Result(svg_path.Subpath, Error)
Transform a subpath about a point.
pub fn subpath_with_arc_collapse(
subpath: svg_path.Subpath,
by transform: Matrix,
) -> Result(svg_path.Subpath, Error)
Transform a subpath, replacing collapsed arcs with line segments.
Collapsed arcs retain their directly transformed endpoints so neighboring segments remain continuous. Reconstruction uses strict endpoint matching; only closing a semantically closed subpath has a final wiggle fallback. Matrix validation, source arc conversion, and reconstruction errors still propagate; allowing arc collapse does not guarantee success.
pub fn to_tuple(
transform: Matrix,
) -> #(Float, Float, Float, Float, Float, Float)
Return SVG’s six matrix values as #(a, b, c, d, e, f).
pub fn translate_path(
input: svg_path.Path,
x x: Float,
y y: Float,
) -> Result(svg_path.Path, Error)
Translate a path.
pub fn translate_point(
input: svg_path.Point,
x x: Float,
y y: Float,
) -> svg_path.Point
Translate a point.
pub fn translate_segment(
input: svg_path.Segment,
x x: Float,
y y: Float,
) -> Result(svg_path.Segment, Error)
Translate a segment.
pub fn translate_subpath(
input: svg_path.Subpath,
x x: Float,
y y: Float,
) -> Result(svg_path.Subpath, Error)
Translate a subpath.