Skip to content

transform_path

Transforms SVG path data by a matrix. The result is written with absolute M, L, C, Q and Z only: relative commands are made absolute, H/V/S/T are expanded, and arcs become cubic curves (so any matrix works, skews included). Moving a path is the common case.

use helpers4::geometry::transform_path;

Cargo feature geometry (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features geometry

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.11", default-features = false, features = ["geometry"] }
pub fn transform_path(d: &str, matrix: Matrix, precision: usize) -> Result<String, ParsePathError>
ParameterTypeDescription
d&strThe path data.
matrixMatrixThe matrix, in the CSS/SVG layout.
precisionusizeDecimals kept per number; 6 is a good default.

Result<String, ParsePathError> — The transformed path data.

A ParsePathError if d is not valid path data.

use helpers4::geometry::{transform_path, Matrix};

let moved = transform_path("M0 0 h10 v10 z", Matrix::translate(5.0, 5.0), 6)?;
assert_eq!(moved, "M5 5 L15 5 L15 15 Z");

Why SVG path data could not be parsed.

use helpers4::geometry::ParsePathError;

#[non_exhaustive]
pub enum ParsePathError {
    /// The data does not start with a move (`M` or `m`).
    MissingMove,
    /// A letter that is not a path command.
    UnknownCommand {
        /// Byte offset of the letter in the input.
        index: usize,
    },
    /// A command is missing a number, or the text is not a number.
    ExpectedNumber {
        /// Byte offset where a number was expected.
        index: usize,
    },
    /// An arc flag is not `0` or `1`.
    ExpectedFlag {
        /// Byte offset where a flag was expected.
        index: usize,
    },
}

src/geometry/transform_path.rs