svg_path/transform/serialize

SVG transform attribute serializer.

This module serializes affine matrices as SVG transform attribute strings. It prefers readable transform functions such as translate, scale, rotate, and skew. Simple forms are recognized directly; general matrices use affine.decompose in Translation × Rotation × ShearX × Scale order. Rounded factors are checked by parsing the actual output. If decomposition or the formatting check fails, ordinary serialization falls back to matrix(a b c d e f). Output is deterministic, not necessarily shortest, and does not preserve construction history or provide animation continuity.

Types

Failures from strict, ordered transform serialization.

pub type Error {
  ConflictingMatrixOption
  FactorizationFailed(reason: affine.DecompositionError)
  InsufficientPrecision
}

Constructors

  • ConflictingMatrixOption

    An explicit order conflicts with force_matrix: True.

  • FactorizationFailed(reason: affine.DecompositionError)

    The affine solver could not produce the requested factors.

  • InsufficientPrecision

    Rounded SVG factors could not reconstruct the input within the documented formatting budget, or could not be parsed as finite SVG transforms.

Options for SVG transform serialization.

pub type Options {
  Options(
    decimal_places: option.Option(Int),
    fixed_decimals: Bool,
    force_matrix: Bool,
  )
}

Constructors

  • Options(
      decimal_places: option.Option(Int),
      fixed_decimals: Bool,
      force_matrix: Bool,
    )

Values

pub fn decimal_options(decimal_places: Int) -> Options

Create options that round numbers to the given number of decimal places.

Trailing zeroes are stripped. Negative decimal places are clamped to zero.

pub fn default_options() -> Options

Default transform serialization options.

Defaults to up to 5 decimal places, stripped trailing zeroes, and readable transform functions when possible.

pub fn fixed_decimal_options(decimal_places: Int) -> Options

Create options that round numbers and keep exactly the given number of decimal places.

Negative decimal places are clamped to zero.

pub fn force_matrix(options: Options) -> Options

Force serialization as matrix(a b c d e f).

This disables the nicer translate, scale, rotate, and skew representations.

pub fn to_string(transform: transform.Matrix) -> String

Serialize a transform matrix with default options.

pub fn to_string_in_order(
  transform: transform.Matrix,
  order order: affine.DecompositionOrder,
  options options: Options,
) -> Result(String, Error)

Serialize in an explicitly requested algebraic order (rightmost acts first). Identity factors and verified negligible arithmetic noise may be omitted. Never substitutes another order or matrix notation. force_matrix: True is an error, not an override.

The generated text is parsed and compared with the input. For each linear column and the translation vector, max-norm error must be at most max(1, input vector norm) * max(1e-12, 10^-decimal_places). With decimal_places: None, the budget is 1e-12 instead. Negative decimal places mean zero. This is a formatting budget, not exact equivalence or the affine solver’s tighter relative reconstruction guarantee.

pub fn to_string_with(
  transform transform: transform.Matrix,
  options options: Options,
) -> String

Serialize a transform matrix with custom options. Uses the formatting budget documented on to_string_in_order. Matrix fallback still rounds coefficients according to these options; neither form promises exact equivalence. Pure skewY retains its direct representation.

✨ Search Document