svg_path/serialize

SVG path data serializer.

This module turns paths, subpaths, and segments into SVG d attribute strings. The default output favors readable canonical forms; options can be used for relative commands, smaller whitespace, and omitted repeated command letters.

Types

Formatting for digits to the left of the decimal point.

pub type LeftDecimalOptions {
  Succinct
  AutoLeftPadding
  LeftPadding(Int)
}

Constructors

  • Succinct

    Do not pad numbers on the left.

  • AutoLeftPadding

    Pre-scan the serialized value and choose the smallest shared left width that aligns its numbers.

  • LeftPadding(Int)

    Pad numbers to the given left width.

    The width includes a leading minus sign for negative numbers.

Newline placement for serialized path data.

pub type Newlines {
  OneLine
  AtSubpaths
  AtSegments
}

Constructors

  • OneLine

    Keep serialized path data on one line.

  • AtSubpaths

    Put each subpath on its own line.

  • AtSegments

    Put each segment on its own line.

Options for SVG path data serialization.

pub type Options {
  Options(
    left_decimals: LeftDecimalOptions,
    right_decimals: RightDecimalOptions,
    relative: Bool,
    minimize_whitespace: Bool,
    repeat_commands: Bool,
    newlines: Newlines,
  )
}

Constructors

  • Options(
      left_decimals: LeftDecimalOptions,
      right_decimals: RightDecimalOptions,
      relative: Bool,
      minimize_whitespace: Bool,
      repeat_commands: Bool,
      newlines: Newlines,
    )

    Arguments

    left_decimals

    Formatting for digits to the left of the decimal point.

    right_decimals

    Formatting for digits to the right of the decimal point.

    relative

    Whether to emit relative commands instead of absolute commands.

    minimize_whitespace

    Whether to remove optional spaces between command letters and arguments.

    repeat_commands

    Whether to repeat command letters when SVG syntax allows them to be omitted.

    newlines

    Where to insert newlines in the serialized path data.

Formatting for digits to the right of the decimal point.

pub type RightDecimalOptions {
  System
  AtMost(Int)
  Fixed(Int)
}

Constructors

  • System

    Use the system float formatter, stripped of purely trailing decimal zeroes.

  • AtMost(Int)

    Use at most this many decimal places, stripping trailing zeroes.

  • Fixed(Int)

    Use exactly this many decimal places.

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 serialization options.

Defaults to readable absolute commands, up to 5 decimal places, repeated command letters, and normal whitespace.

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 minimize_whitespace(options: Options) -> Options

Remove optional spaces between command letters and their arguments.

pub fn path(path: svg_path.Path) -> String

Serialize a path with default options.

pub fn path_with_options(
  path path: svg_path.Path,
  options options: Options,
) -> String

Serialize a path with custom options.

pub fn relative_decimal_options(decimal_places: Int) -> Options

Create relative serialization options with decimal rounding.

pub fn relative_fixed_decimal_options(
  decimal_places: Int,
) -> Options

Create relative serialization options with fixed decimal formatting.

pub fn relative_options() -> Options

Create options that serialize with relative commands.

pub fn repeat_commands(
  options: Options,
  repeat_commands: Bool,
) -> Options

Configure whether repeated command letters should be emitted.

SVG allows some commands to omit the command letter when the same command repeats. Pass False for smaller, less verbose output.

pub fn segment(segment: svg_path.Segment) -> String

Serialize a segment with default options.

pub fn segment_with_options(
  segment segment: svg_path.Segment,
  options options: Options,
) -> String

Serialize a segment with custom options.

pub fn subpath(subpath: svg_path.Subpath) -> String

Serialize a subpath with default options.

pub fn subpath_with_options(
  subpath subpath: svg_path.Subpath,
  options options: Options,
) -> String

Serialize a subpath with custom options.

pub fn with_left_decimals(
  options options: Options,
  left_decimals left_decimals: LeftDecimalOptions,
) -> Options

Set left-side decimal formatting for serialization options.

pub fn with_left_padding(
  options options: Options,
  left_padding left_padding: LeftDecimalOptions,
) -> Options

Set left-side number padding for serialization options.

pub fn with_newlines(
  options options: Options,
  newlines newlines: Newlines,
) -> Options

Configure where newlines should be inserted in serialized path data.

pub fn with_right_decimals(
  options options: Options,
  right_decimals right_decimals: RightDecimalOptions,
) -> Options

Set right-side decimal formatting for serialization options.

Search Document