svg_path_editor.path_operations#

Functions#

reverse_path(svg[, subpath_of_item])

Reverse the drawing direction of a path or sub-path.

optimize_relative_absolute(svg)

Optimize the relative/absolute representation of a path.

optimize_path(svg, *[, remove_useless_commands, ...])

Optimize the representation of an SVG path.

Module Contents#

svg_path_editor.path_operations.reverse_path(svg, subpath_of_item=None)[source]#

Reverse the drawing direction of a path or sub-path.

Parameters:
  • svg (SvgPath) – Input path.

  • subpath_of_item (int | None) – Index of an item within the sub-path to reverse, or None to reverse the entire path.

Returns:

A new path with the selected segment reversed. Geometry is preserved, but command types and relative/absolute representation may change.

Return type:

SvgPath

svg_path_editor.path_operations.optimize_relative_absolute(svg)[source]#

Optimize the relative/absolute representation of a path.

Each command is toggled between relative and absolute form, and the representation that yields a shorter minified path string is kept.

Parameters:

svg (SvgPath) – Input path.

Returns:

A new path with possibly changed relative/absolute commands. Geometry is preserved; only representation changes.

Return type:

SvgPath

svg_path_editor.path_operations.optimize_path(svg, *, remove_useless_commands=False, remove_orphan_dots=False, use_shorthands=False, use_horizontal_and_vertical_lines=False, use_relative_absolute=False, use_reverse=False, use_close_path=False)[source]#

Optimize the representation of an SVG path.

The function can apply several optional passes that can be enabled using the parameters.

Parameters:
  • svg (SvgPath) – Input path.

  • remove_useless_commands (bool) – Remove redundant M/Z commands and degenerate L/H/V segments.

  • remove_orphan_dots (bool) – Remove empty closed subpaths (M immediately followed by Z).

  • use_shorthands (bool) – Convert eligible C/Q segments to S/T where possible.

  • use_horizontal_and_vertical_lines (bool) – Replace L with H or V where possible.

  • use_relative_absolute (bool) – Choose between relative and absolute commands per segment to minimize size.

  • use_reverse (bool) – Reverse the path direction if that yields a shorter minified representation. This can affect stroked paths.

  • use_close_path (bool) – Convert final line segments that return to start into Z. This can affect stroked paths.

Returns:

A new, possibly shorter, but geometrically equivalent path.

Return type:

SvgPath