inkext.geomsvg¶
Methods for converting SVG shape elements to geometry objects.
- inkext.geomsvg.convert_circle(element: TElement) tuple[geom2d.Arc, geom2d.Arc, geom2d.Arc, geom2d.Arc] ¶
Convert an SVG circle shape element to four circular arc segments.
- Parameters:
element – An SVG ‘circle’ element of the form: <circle r=’RX’ cx=’X’ cy=’Y’/>
- Returns:
A counter-clockwise wound list of four circular geom2d.Arc segments.
- inkext.geomsvg.convert_ellipse(element: TElement) geom2d.Ellipse ¶
Convert an SVG ellipse shape element to a geom2d.Ellipse.
- Parameters:
element – An SVG ‘ellipse’ element of the form: <ellipse rx=’RX’ ry=’RY’ cx=’X’ cy=’Y’/>
- Returns:
A geom2d.Ellipse.
- inkext.geomsvg.convert_line(element: TElement) list[geom2d.Line] ¶
Convert an SVG line shape element to a geom2d.Line.
- Parameters:
element – An SVG ‘line’ element of the form: <line x1=’X1’ y1=’Y1’ x2=’X2’ y2=’Y2/>
- Returns:
geom2d.Line((x1, y1), (x2, y2))
- Return type:
A line segment
- inkext.geomsvg.convert_poly(element: TElement, close: bool = False) list[geom2d.Line] ¶
Convert SVG polyline or polygon element to list of line segments.
- Parameters:
element – An SVG ‘polyline’ element of the form: <polyline points=’x1,y1 x2,y2 x3,y3 […]’/>
close – If True, close polygon if not already closed. Default is False.
- Returns:
A list of geom2d.Line segments.
- inkext.geomsvg.convert_rect(element: TElement) list[geom2d.Line] ¶
Convert an SVG rect shape element to four geom2d.Line segments.
- Parameters:
element – An SVG ‘rect’ element of the form <rect x=’X’ y=’Y’ width=’W’ height=’H’/>
- Returns:
A clockwise wound polygon as a list of four geom2d.Line segments.
- inkext.geomsvg.parse_path_geom(path_data: str, ellipse_to_bezier: bool = True) list[Sequence[TGeom]] ¶
Parse SVG path data and convert to geometry objects.
- Parameters:
path_data – The d attribute value of an SVG path element.
ellipse_to_bezier – Convert elliptical arcs to bezier curves if True. Default is False.
- Returns:
A list of zero or more subpaths. A subpath being a list of zero or more Line, Arc, EllipticalArc, or CubicBezier objects.
- inkext.geomsvg.path_to_polyline(path: Iterable[TPathGeom]) Iterator[geom2d.P] ¶
Convert a path to a polyline (sequence of points).
Only the endpoints of Arc and CubicBezier segments are used.
- Parameters:
path – An iterable of path segments.
- Returns:
A polypath as an iterable of points (
geom2d.P
).
- inkext.geomsvg.path_to_polypath(path: Iterable[TPathGeom]) Iterator[geom2d.Line] ¶
Convert a path to a polypath (sequence of Lines).
Arc and CubicBezier segments are converted to simple Line segments using the end points.
- Parameters:
path – An iterable of path segments.
- Returns:
A polypath as an iterable of Line segments.
- inkext.geomsvg.svg_element_to_geometry(element: TElement, element_transform: TMatrix | None = None, parent_transform: TMatrix | None = None, ellipse_to_bezier: bool = True) Sequence[Sequence[TGeom]] ¶
Convert the SVG shape element to subpath.
Creates a list of one or more sub-paths consisting of a list of Line, Arc, and/or CubicBezier segments, and applies node/parent transforms. The coordinates of the segments will be absolute with respect to the parent container.
- Parameters:
element – An SVG Element shape node.
element_transform – An optional transform to apply to the element. Default is None.
parent_transform – An optional parent transform to apply to the element. Default is None.
ellipse_to_bezier – Convert ellipses and elliptical arcs to bezier curves if True. Default is True.
- Returns:
A list of zero or more sub-paths. A sub-path being a list of zero or more Line, Arc, Ellipse, EllipticalArc, or CubicBezier objects.
- inkext.geomsvg.svg_to_geometry(svg_elements: Iterable[tuple[TElement, TMatrix | None]], parent_transform: TMatrix | None = None, ellipse_to_bezier: bool = True) list[Sequence[TGeom]] ¶
Convert the SVG shape elements to geometry objects.
Converts SVG shape elements to Line, Arc, and/or CubicBezier segments, and applies node/parent transforms. The coordinates of the segments will be absolute with respect to the parent container.
- Parameters:
svg_elements – An iterable collection of 2-tuples consisting of SVG Element node and transform matrix.
parent_transform – An optional parent transform to apply to all nodes. Default is None.
ellipse_to_bezier – Convert ellipses and elliptical arcs to bezier curves if True. Default is True.
- Returns:
A list of paths, where a path is a list of one or more segments made of Line, Arc, or CubicBezier objects, and optionally EllipticalArc, Ellipse if ellipse_to_bezier is False.