A pure-Python library for reading and converting SVG files to ReportLab Graphics drawings
Utility functions for parsing, normalizing, and converting SVG path data and mathematical operations for arc and bezier curve processing. These functions handle the complex mathematics required for accurate SVG path rendering.
Parse and normalize SVG path data strings into structured format for processing.
def normalise_svg_path(attr):
"""
Normalize SVG path data by adding operation codes.
Converts SVG path strings into structured list format with explicit
operation codes for multi-argument parameters and fixes consecutive
M/m operators to M/m + L/l sequences.
Parameters:
- attr: str - SVG path data string (e.g., "M 10 20, M 20 20, L 30 40, Z")
Returns:
list: Normalized path operations and coordinates
Example: ['M', [10, 20], 'L', [20, 20], 'L', [30, 40], 'Z', []]
"""Usage Example:
from svglib.utils import normalise_svg_path
# Normalize complex path data
path_data = "M 10 20, M 20 20, L 30 40, 40 40, Z"
normalized = normalise_svg_path(path_data)
# Returns: ['M', [10, 20], 'L', [20, 20], 'L', [30, 40], 'L', [40, 40], 'Z', []]
# Handle path with curves
curve_path = "M 0 0 C 10 10, 20 10, 30 0"
normalized_curve = normalise_svg_path(curve_path)Split string values into float arrays with appropriate SVG path operations.
def split_floats(op, min_num, value):
"""
Split string of numbers into float list with SVG path operations.
Converts coordinate strings to float arrays and inserts appropriate
operation codes for multi-coordinate sequences.
Parameters:
- op: str - SVG path operation ('m', 'M', 'l', 'L', etc.)
- min_num: int - Minimum number of coordinates per operation
- value: str - String containing coordinate values
Returns:
list: Operation codes and coordinate arrays
Example: ['m', [10.0, 20.0], 'l', [30.0, 40.0]]
"""Parse SVG arc command values with proper flag and coordinate handling.
def split_arc_values(op, value):
"""
Parse SVG arc command values.
Handles the complex arc parameter format with rx, ry, rotation,
large-arc-flag, sweep-flag, and endpoint coordinates.
Parameters:
- op: str - Arc operation ('A' or 'a')
- value: str - Arc parameter string
Returns:
list: Arc operations with parsed parameters
"""Convert quadratic Bezier curves to cubic Bezier format for ReportLab compatibility.
def convert_quadratic_to_cubic_path(q0, q1, q2):
"""
Convert quadratic Bezier curve to cubic Bezier curve.
ReportLab only supports cubic Bezier curves, so this function converts
SVG quadratic curves (Q commands) to cubic format (C commands).
Parameters:
- q0: tuple - Start point (x, y)
- q1: tuple - Control point (x, y)
- q2: tuple - End point (x, y)
Returns:
tuple: Four control points for cubic curve (c0, c1, c2, c3)
"""Usage Example:
from svglib.utils import convert_quadratic_to_cubic_path
# Convert quadratic to cubic
start = (0, 0)
control = (50, 100)
end = (100, 0)
cubic_points = convert_quadratic_to_cubic_path(start, control, end)
c0, c1, c2, c3 = cubic_points
# c0 = (0, 0), c1 ≈ (33.33, 66.67), c2 ≈ (66.67, 66.67), c3 = (100, 0)Calculate angles and relationships between vectors for arc processing.
def vector_angle(u, v):
"""
Calculate angle between two vectors.
Used in arc processing to determine sweep angles and orientations.
Parameters:
- u: tuple - First vector (x, y)
- v: tuple - Second vector (x, y)
Returns:
float: Angle between vectors in degrees
"""Convert between different arc parameter representations used in SVG.
def end_point_to_center_parameters(x1, y1, x2, y2, fA, fS, rx, ry, phi=0):
"""
Convert SVG arc endpoint parameters to center parameters.
SVG uses endpoint parameterization for arcs, but mathematical processing
often requires center parameterization. This function performs the conversion.
Parameters:
- x1, y1: float - Start point coordinates
- x2, y2: float - End point coordinates
- fA: int - Large arc flag (0 or 1)
- fS: int - Sweep flag (0 or 1)
- rx, ry: float - Ellipse radii
- phi: float - Rotation angle in degrees
Returns:
tuple: (cx, cy, start_angle, sweep_angle) - Center and angle parameters
"""Generate Bezier curve approximations for elliptical arcs.
def bezier_arc_from_centre(cx, cy, rx, ry, start_ang=0, extent=90):
"""
Generate Bezier curves approximating an elliptical arc from center parameters.
Converts arc specifications to series of cubic Bezier curves that
ReportLab can render accurately.
Parameters:
- cx, cy: float - Arc center coordinates
- rx, ry: float - Ellipse radii
- start_ang: float - Start angle in degrees
- extent: float - Arc extent in degrees
Returns:
list: List of cubic Bezier curve control points
"""
def bezier_arc_from_end_points(x1, y1, rx, ry, phi, fA, fS, x2, y2):
"""
Generate Bezier curves for SVG arc from endpoint parameters.
High-level function that combines endpoint-to-center conversion with
Bezier arc generation for direct SVG arc processing.
Parameters:
- x1, y1: float - Start point coordinates
- rx, ry: float - Ellipse radii
- phi: float - Rotation angle in degrees
- fA: int - Large arc flag (0 or 1)
- fS: int - Sweep flag (0 or 1)
- x2, y2: float - End point coordinates
Returns:
list: List of cubic Bezier curve control points
"""Usage Example:
from svglib.utils import bezier_arc_from_centre, bezier_arc_from_end_points
# Generate arc from center parameters
curves = bezier_arc_from_centre(
cx=50, cy=50, # Center
rx=30, ry=20, # Radii
start_ang=0, extent=90 # Quarter circle
)
# Generate arc from SVG endpoint format
curves = bezier_arc_from_end_points(
x1=0, y1=50, # Start point
rx=30, ry=20, # Radii
phi=0, # No rotation
fA=0, fS=1, # Small arc, positive sweep
x2=50, y2=0 # End point
)The module uses various mathematical operations from Python's math module:
acos, cos, sin - Trigonometric functions for arc calculationsdegrees, radians - Angle unit conversionssqrt, hypot - Distance and magnitude calculationscopysign, fabs - Sign and absolute value operationsceil - Rounding operationsThese are used internally by the arc and curve processing functions to perform accurate geometric calculations required for SVG path rendering.
Install with Tessl CLI
npx tessl i tessl/pypi-svglib