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

Errors returned while transforming paths or converting matrices.

pub type Error {
  DegenerateArcTransform
  InvalidMatrix
  Core(svg_path.Error)
}

Constructors

  • DegenerateArcTransform

    An arc collapsed under the transform and cannot be represented as an arc.

  • InvalidMatrix

    A matrix contains non-finite values.

  • An error from the core path model.

Matrix

opaque

An opaque SVG affine transform matrix.

The stored values correspond to SVG’s matrix(a b c d e f) form: x' = a*x + c*y + e, y' = b*x + d*y + f.

pub opaque type Matrix

Values

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 identity() -> Matrix

The identity transform.

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 point(
  point: vec2.Vec2(Float),
  by transform: Matrix,
) -> vec2.Vec2(Float)

Transform a point by a matrix.

pub fn rotate(degrees degrees: Float) -> Matrix

Create a rotation matrix from an angle in degrees.

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: vec2.Vec2(Float),
  degrees degrees: Float,
) -> vec2.Vec2(Float)

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(factor factor: Float) -> Matrix

Create a uniform scale matrix.

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: vec2.Vec2(Float),
  factor factor: Float,
) -> vec2.Vec2(Float)

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(x x: Float, y y: Float) -> Matrix

Create a non-uniform scale matrix.

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: vec2.Vec2(Float),
  x x: Float,
  y y: Float,
) -> vec2.Vec2(Float)

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_gracefully or segment_gracefully2 to convert collapsed arcs into line-based representations when possible.

pub fn segment_gracefully(
  input: svg_path.Segment,
  by transform: Matrix,
) -> Result(svg_path.Segment, Error)

Transform a segment, converting collapsed arcs into lines when possible.

This returns a single segment. If a collapsed arc needs multiple line segments to preserve its motion, use segment_gracefully2.

pub fn segment_gracefully2(
  input: svg_path.Segment,
  by transform: Matrix,
) -> Result(svg_path.Subpath, Error)

Transform a segment, returning a subpath for graceful collapsed arc handling.

This can represent collapsed arcs as multiple line segments when needed.

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: vec2.Vec2(Float),
  degrees degrees: Float,
) -> vec2.Vec2(Float)

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: vec2.Vec2(Float),
  degrees degrees: Float,
) -> vec2.Vec2(Float)

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_gracefully(
  subpath: svg_path.Subpath,
  by transform: Matrix,
) -> Result(svg_path.Subpath, Error)

Transform a subpath, gracefully converting collapsed arcs when possible.

This uses the core path wiggle helpers to preserve continuity after collapsed arcs are converted to line-based subpaths.

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(x x: Float, y y: Float) -> Matrix

Create a translation matrix.

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: vec2.Vec2(Float),
  x x: Float,
  y y: Float,
) -> vec2.Vec2(Float)

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.

Search Document