Elements

Element classes. Every class also has a lowercase alias (svgwriter.circle is svgwriter.Circle).

Each class lists its own members and links to its bases: moving, rotating, aligning and the center/top/… attributes are on Transformable, which every shape, group, image and text inherits.

Base classes

class Element(*children: Element | str, id: str | None = None, attrs: Mapping[str, Any] | None = None, **extra: Any)[source]

Bases: Construction, AttributeAccess, TreeEditing, Queryable, Displayable, Composable

Base class for every SVG element.

Attributes are kept in attributes under their SVG names; keyword arguments use Python spelling (stroke_width, class_, xlink_href) and are translated by svg_name().

Parameters:
  • *children (Element or str) – Child elements and text nodes.

  • id (str, optional) – The element id. Defaults to None.

  • attrs (mapping, optional) – Extra attributes given by their SVG names. Defaults to None.

  • **extra – Any other attribute, Python spelling.

Raises:

UnknownKeywordError – If a keyword is not a known SVG attribute.

See also

svgwriter.model.elements.Container

Elements that hold children.

Examples

>>> import svgwriter as svg
>>> el = svg.rect(0, 0, 10, 10, stroke_width=2)
>>> el.tag, el.attributes["stroke-width"]
('rect', 2)
What the code above draws
tag: ClassVar[str] = ''
arranges_children: ClassVar[bool] = False

True for layout groups, which place their children before any child is measured.

namespace: ClassVar[str | None] = None
category: ClassVar[Category] = 13
content: ClassVar[ContentModel] = 2
property id: str | None

The element id, or None.

Assigning validates the name as an XML name; assigning None removes it.

Raises:

ValueError – On assignment, if the value is not a valid id.

Examples

>>> import svgwriter as svg
>>> el = svg.rect(0, 0, 1, 1, id="a")
>>> el.id
'a'
What the code above draws
property animate: Any

el.animate.fade_in(1).spin(2, begin=1).

Returns:

Motion – Chainable presets that add SMIL animation children.

Examples

>>> import svgwriter as svg
>>> _ = svg.rect(0, 0, 10, 10).animate.fade_in()
Type:

Animation presets for this element

property interact: Any

button.interact.button(); add your own with @svg.preset.interaction.

Returns:

Interactions – The presets; each call applies one and returns this element.

Examples

>>> import svgwriter as svg
>>> _ = svg.rect(0, 0, 10, 10).interact.highlight(color="crimson")
What the code above draws
Type:

Interaction presets for this element

wireframe(*, color: str = '#0ea5e9', width: float | None = None, bounds: bool = False) → Any[source]

A copy drawn as thin outlines, to see the geometry under the paint (for debugging).

Parameters:
  • color (str, optional) – Outline colour. Defaults to sky blue.

  • width (float, optional) – Outline width in user units. Defaults to None: a hairline scaled to the drawing.

  • bounds (bool, optional) – Also draw each shape’s bounding box, dashed. Defaults to False.

Returns:

Element – The wireframe; this one is unchanged.

See also

svgwriter.ops.wireframe

The function behind it.

Examples

>>> import svgwriter as svg
>>> svg.circle(5, 5, 4, fill="red").wireframe().style["fill"]
'none'
property home: Element

a detached copy to start each frame from.

A behavior that sets the state from home (element.matrix = element.home.matrix, then a relative move) computes every frame afresh, so the motion never drifts and frames can be drawn in any order.

Returns:

Element – The copy Scene.attach took.

Raises:

ValueError – If no behavior has been attached to this element.

See also

svgwriter.library.preset.behavior

Write a behavior.

Examples

>>> import svgwriter as svg
>>> class Hop(svg.Scene):
...     width, height, duration = 40, 40, 1.0
...     def build(self, doc):
...         self.dot = doc.circle(20, 30, 4).animate.hover(height=10).element
>>> scene = Hop()
>>> scene.dot.cy = 31
>>> float(scene.dot.home.cy)
30.0
Type:

The element as it was when its first behavior was attached

computed(name: str, default: object = None) → object[source]

The value SVG would use for property name (SVG spelling or Python spelling).

Looks at the inline style, matching <style> rules, the presentation attribute, then ancestors for inherited properties.

Parameters:
  • name (str) – Property name, in SVG (font-size) or Python (font_size) spelling.

  • default (object, optional) – Returned when the property is not set anywhere. Defaults to None.

Returns:

object – The property’s value, written as SVG text when it is not a plain number, or default.

Examples

>>> import svgwriter as svg
>>> g = svg.group(fill="red", font_size=30)
>>> t = g.add(svg.text("hi", style="font-size: 12px"))
>>> t.computed("fill"), t.computed("font_size"), t.computed("opacity", 1)
('red', '12px', 1)
What the code above draws
clone(linked: Literal[False] = False) → E[source]
clone(linked: Literal[True]) → Use

A deep, detached copy; or with linked, a <use> that renders this element.

Parameters:

linked (bool) – False copies the whole subtree (pixels included), so edits never reach the original. True returns a <use> referencing this element (giving it an id), so edits to the original show through every linked clone.

Returns:

Element – A copy of the same class, or a Use.

Notes

A linked clone needs the original in the same document: placed anywhere, or unplaced, in which case it moves into <defs> when the <use> joins the document. copy.copy and copy.deepcopy both behave like clone().

Examples

>>> import svgwriter as svg
>>> star = svg.circle(0, 0, 5, id="dot")
>>> star.clone().id, star.clone(linked=True).tag
('dot', 'use')
What the code above draws
append(child: C) → C

Append a child element or text node and return it.

An element that already has a parent is moved. Elements referenced by the child but not yet placed are collected into the root <defs>.

Parameters:

child (Element or str) – The child to append.

Returns:

Element or str – The appended child.

Raises:

TypeError – If the content model forbids the child, or it is not an element or str.

See also

insert

Add a child at a given position.

Examples

>>> import svgwriter as svg
>>> g = svg.group()
>>> g.append(svg.circle(0, 0, 1)).tag
'circle'
What the code above draws
clear() → None

Remove and detach every child.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> g.clear()
>>> len(g)
0
copy(linked: bool = False) → Element

Same as clone().

discard(name: str) → None

Remove an attribute if it is set.

Parameters:

name (str) – The attribute name, Python or SVG spelling.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> r.discard("width")
>>> r.has("width")
False
What the code above draws
ensure_id() → ElementId

Return the id, assigning a generated one if needed.

Returns:

str – The existing or newly generated id.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").ensure_id()
'a'
extend(children: Iterable[Child]) → None

Append every child in children.

Parameters:

children (iterable of Element or str) – The children to append, in order.

Examples

>>> import svgwriter as svg
>>> g = svg.group()
>>> g.extend([svg.circle(0, 0, 1), svg.circle(1, 1, 1)])
>>> len(g)
2
What the code above draws
find(match: type[Element] | str) → Element | None

First descendant (or self) matching a tag or class.

Parameters:

match (str or type) – A tag name or an element class.

Returns:

Element or None – The first match in document order, or None.

See also

find_all

Every match.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> g.find("circle").tag, g.find("rect")
('circle', None)
What the code above draws
find_all(match: type[Element] | str) → list[Element]

Every descendant (and self) matching a tag or class.

Parameters:

match (str or type) – A tag name or an element class.

Returns:

list of Element – The matches in document order.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1), svg.circle(1, 1, 1))
>>> len(g.find_all("circle"))
2
What the code above draws
classmethod from_attributes(attributes: Mapping[str, str]) → E

Build an instance from raw SVG attributes, bypassing __init__.

Parameters:

attributes (mapping of str to str) – Attributes by SVG name, stored as given.

Returns:

Element – A new instance of this class.

Examples

>>> import svgwriter as svg
>>> svg.Rect.from_attributes({"width": "5"}).attributes
{'width': '5'}
get(name: str, default: object = None) → object

Return an attribute value, or default if it is not set.

A presentation property declared in the inline style is read from there, as it overrides the attribute.

Parameters:
  • name (str) – The attribute name, Python or SVG spelling.

  • default (object, optional) – Returned when the attribute is absent. Defaults to None.

Returns:

object – The stored value or the default.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1, stroke_width=2)
>>> r.get("stroke_width"), r.get("fill", "none")
(2, 'none')
>>> svg.rect(0, 0, 1, 1, style="fill:blue").get("fill")
'blue'
What the code above draws
get_element_by_id(element_id: str) → Element | None

Find the element with the given id in this subtree.

Parameters:

element_id (str) – The id to look for.

Returns:

Element or None – The matching element, or None.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1, id="dot"))
>>> g.get_element_by_id("dot").tag
'circle'
What the code above draws
has(name: str) → bool

Report whether an attribute is set.

Parameters:

name (str) – The attribute name, Python or SVG spelling.

Returns:

bool – True if the attribute is present.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1).has("width")
True
insert(index: int, child: C) → C

Insert a child at index and return it.

Parameters:
  • index (int) – Position among the children.

  • child (Element or str) – The child to insert.

Returns:

Element or str – The inserted child.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> _ = g.insert(0, svg.rect(0, 0, 1, 1))
>>> [c.tag for c in g]
['rect', 'circle']
What the code above draws
property iri: IRI

#id pointing at this element, assigning an id if needed.

Use it where SVG expects a plain IRI (xlink:href of <use>, <mpath>, …); passing the element itself does the same. An element’s own xlink:href is el.href.

See also

url

The url(#id) form.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").iri.svg_value()
'#a'
iter(tag: str | None = None, *, max_depth: float = inf) → Iterator[Element]

Depth-first walk over this element and its descendants.

Parameters:
  • tag (str, optional) – Yield only elements with this tag. Defaults to None (all).

  • max_depth (float, optional) – Deepest level to descend to; 0 is this element only. Defaults to infinity.

Yields:

Element – Elements in document order, starting with this one.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1), svg.rect(0, 0, 1, 1))
>>> [e.tag for e in g.iter()]
['g', 'circle', 'rect']
What the code above draws
property label: str | None

A readable name for the element, stored as inkscape:label, or None.

Inkscape shows it in its Layers and Objects panels, and doc["name"] finds an element by label when no id matches. Assigning None removes it.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> r.label = "Logo"
>>> r.label, r.attributes["inkscape:label"]
('Logo', 'Logo')
What the code above draws
classmethod lookup(tag: str) → type[Element] | None

Return the registered class for tag.

Parameters:

tag (str) – An SVG tag name.

Returns:

type[Element] or None – The class registered for the tag, or None if there is none.

Examples

>>> import svgwriter as svg
>>> svg.Element.lookup("rect").__name__
'Rect'
classmethod many(**columns: Any) → list[E]

Build many elements at once from columns that broadcast like numpy.

Each keyword is an attribute in Python spelling; a scalar applies to every element and an array gives one value per element. Colors may be one color, a list of colors or an (n, 3|4) array of 0-1 floats.

Parameters:

**columns – Attribute name to scalar or sequence.

Returns:

list of Element – The new elements, ready for doc.group(*elements) or doc.add_all.

Raises:

ValueError – If the columns do not broadcast to one length.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> dots = svg.Circle.many(cx=np.arange(3) * 10, cy=5, r=2, fill=["red", "green", "blue"])
>>> [str(d) for d in dots][1]
'<circle cx="10" cy="5" r="2" fill="green"/>'
classmethod registered(category: Category | None = None) → dict[str, type[Element]]

Registered element classes, optionally filtered by category.

Parameters:

category (Category, optional) – Keep only classes of this category. Defaults to None (all).

Returns:

dict[str, type[Element]] – Registered classes keyed by tag.

Examples

>>> import svgwriter as svg
>>> "circle" in svg.Element.registered()
True
remove(child: Child) → None

Remove a child and detach it from this element.

Parameters:

child (Element or str) – The child to remove.

Raises:

ValueError – If the child is not present.

Examples

>>> import svgwriter as svg
>>> c = svg.circle(0, 0, 1)
>>> g = svg.group(c)
>>> g.remove(c)
>>> len(g)
0
What the code above draws
root() → Element

Return the topmost ancestor, or this element if it has no parent.

Returns:

Element – The root of the tree this element belongs to.

Examples

>>> import svgwriter as svg
>>> c = svg.circle(0, 0, 1)
>>> g = svg.group(c)
>>> c.root() is g
True
What the code above draws
save(target: AnyPath | TextSink, **options: Any) → None

Write to a path or text stream.

Parameters:
  • target (path or text stream) – Where to write the SVG markup.

  • **options – Serialization options, as for to_string().

See also

to_string

Serialize to a string.

Examples

>>> import io
>>> import svgwriter as svg
>>> buffer = io.StringIO()
>>> svg.circle(0, 0, 1).save(buffer, xml_declaration=False, pretty=False)
>>> buffer.getvalue()
'<circle cx="0" cy="0" r="1"/>'
set(name: str, value: AttrValue) → None

Set one attribute; None removes it.

Attributes in other namespaces use Clark notation: "{uri}name". Colors with alpha are split into the color and its *-opacity attribute, and elements are replaced by references to them. A presentation property the inline style already declares (as Inkscape writes them) is changed there, where it takes effect.

Parameters:
  • name (str) – The attribute name, Python or SVG spelling.

  • value (object) – The new value; None removes the attribute.

Raises:
  • TypeError – If a length, angle or ~svgwriter.Quantity has the wrong dimension for the attribute (width given an area).

  • ValueError – If the attribute is unsupported, a color string is not a valid color or paint, or a color with alpha is given to an attribute without an opacity counterpart.

See also

update

Set several attributes at once.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> _ = r.set("stroke_width", 3)
>>> r.attributes["stroke-width"]
3
>>> styled = svg.rect(0, 0, 1, 1, style="fill:#ff0000")
>>> styled.fill = "blue"
>>> styled.attributes["style"], styled.has("fill")
(Style({'fill': 'blue'}), False)
What the code above draws What the code above draws
property text_content: str

All character data below this element, in document order (the DOM textContent).

Assigning replaces every child with the single string.

Examples

>>> import svgwriter as svg
>>> t = svg.text("Hello world")
>>> t[6:].fill = "red"
>>> t.text_content
'Hello world'
>>> title = svg.Title("Old")
>>> title.text_content = "New"
>>> str(title)
'<title>New</title>'
What the code above draws
to_string(**options: Any) → str

Serialize to an SVG string; see svgwriter.formats.io.SerializeOptions.

Parameters:

**options – Serialization options such as pretty or xml_declaration.

Returns:

str – The SVG markup.

See also

save

Write to a file or stream.

Examples

>>> import svgwriter as svg
>>> svg.circle(0, 0, 1).to_string(xml_declaration=False, pretty=False)
'<circle cx="0" cy="0" r="1"/>'
update(attributes: Mapping[str, AttrValue] | None = None, /, **kwargs: AttrValue) → None

Set several attributes; None values are skipped.

Parameters:
  • attributes (mapping, optional) – Attributes by name. Defaults to None.

  • **kwargs – More attributes, Python spelling.

See also

set

Set a single attribute.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> _ = r.update(fill="red", stroke=None)
>>> r.get("fill")
'red'
What the code above draws
property url: FuncIRI

url(#id) pointing at this element, assigning an id if needed.

Use it where SVG expects a functional IRI (fill, clip-path, filter); passing the element itself does the same.

See also

iri

The plain #id form.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").url.svg_value()
'url(#a)'
class Transformable(*args: Element | str, transform: Transform | TransformOp | Matrix | str | None = None, anchor: tuple[float, float] | Point | str | None = None, **kwargs: Any)[source]

Bases: Placement, TransformMethods, Element

An element placed by a transform.

The element’s own geometry attributes (cx, x, d, …) are local coordinates and stay as written; every move goes through t. Rotation, scaling and skewing pivot on anchor.

Parameters:
  • t (Transform, Matrix or str, optional) – Initial transform (transform= is accepted as an alias). Defaults to None.

  • anchor ((float, float), optional) – Pivot in local coordinates. Defaults to the bounding-box centre.

See also

svgwriter.model.elements._behavior.Convertible

Conversions and clipping on top of this class.

Notes

t is one affine matrix. Every method multiplies into it (before it in the parent frame, after it with local=True) instead of appending to a list; output is the shortest equivalent transform.

Examples

>>> import svgwriter as svg
>>> c = svg.circle(50, 50, 10).translate(10, 0).rotate(90)
>>> str(c.matrix)
'rotate(90 55 55)'
>>> c.position
Point(x=60.0, y=50.0)
What the code above draws
transform_attribute: ClassVar[str] = 'transform'
property anchor: Point

Pivot in local coordinates; the bounding-box centre unless set.

Assign a point, or a name read on the element’s own box each time, so it follows edits: "center", "top", "bottom", "left", "right", "top_left", "top_right", "bottom_left", "bottom_right" (and the other BBox.point() names), "origin" for local (0, 0), or, on text, "baseline" for the start of its first baseline. Assign None to go back to the centre. The same names work as about= in every transform method and animation preset.

Raises:

ValueError – On assigning an unknown name; the message lists the valid ones.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 10, 4)
>>> r.anchor
Point(x=5.0, y=2.0)
>>> r.anchor = "bottom"
>>> r.anchor
Point(x=5.0, y=4.0)
What the code above draws
property matrix: Matrix

The transform as an affine Matrix; assign to replace it.

Written to SVG as the shortest equivalent transform text.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1).translate(2, 3).matrix.apply(0, 0)
Point(x=2.0, y=3.0)
align(target: Any, at: str = 'center', *, to: str | None = None, offset: PointLike = (0.0, 0.0)) → E

Move the element so its point at meets the point to of target.

Parameters:
  • target (Element, BBox or (float, float)) – What to align with; an element’s bounds() are used.

  • at (str, optional) – Named point of this element’s bounds. Defaults to "center".

  • to (str, optional) – Named point of the target. Defaults to at, so a.align(b, "left") lines up left edges and a.align(b, "top", to="bottom") stacks a under b.

  • offset ((float, float), optional) – Extra shift after aligning, for gaps. Defaults to (0, 0).

Returns:

Transformable – This element.

Raises:

TypeError – If either size is unknown or target is not an element, box or point.

See also

place

Move to a coordinate.

BBox.point

The point names.

Examples

>>> import svgwriter as svg
>>> base = svg.rect(0, 0, 40, 10)
>>> label = svg.rect(0, 0, 10, 4).align(base, "top", to="bottom", offset=(0, 2))
>>> label.bounds()
BBox(x=15.0, y=12.0, width=10.0, height=4.0)
What the code above draws What the code above draws
append(child: C) → C

Append a child element or text node and return it.

An element that already has a parent is moved. Elements referenced by the child but not yet placed are collected into the root <defs>.

Parameters:

child (Element or str) – The child to append.

Returns:

Element or str – The appended child.

Raises:

TypeError – If the content model forbids the child, or it is not an element or str.

See also

insert

Add a child at a given position.

Examples

>>> import svgwriter as svg
>>> g = svg.group()
>>> g.append(svg.circle(0, 0, 1)).tag
'circle'
What the code above draws
apply(transform: TransformLike, *, local: bool = False) → E

Compose an arbitrary transform (no pivot), in the parent frame unless local.

Parameters:
  • transform (Transform, Matrix or str) – The transform to compose.

  • local (bool, optional) – Apply in the element’s own frame. Defaults to False.

Returns:

Transformable – This element.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1).apply("translate(4 5)").position
Point(x=4.5, y=5.5)
property bottom

Largest y of bounds(); assign to move the bottom edge there.

property bottom_center

Middle of the bottom edge of bounds(); assign to move it there.

property bottom_left

Bottom-left corner of bounds(); assign to move it there.

property bottom_right

Bottom-right corner of bounds(); assign to move it there.

bounds() → BBox | None

Bounding box in parent coordinates (after t).

Returns:

BBox or None – The box enclosing the transformed local box, or None when unknown.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 2, 2).translate(5, 5).bounds()
BBox(x=5.0, y=5.0, width=2.0, height=2.0)
property center

Centre of bounds(); assign a point (a.center = b.center) to move it there.

property center_left

Middle of the left edge of bounds(); assign to move it there.

property center_right

Middle of the right edge of bounds(); assign to move it there.

clear() → None

Remove and detach every child.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> g.clear()
>>> len(g)
0
discard(name: str) → None

Remove an attribute if it is set.

Parameters:

name (str) – The attribute name, Python or SVG spelling.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> r.discard("width")
>>> r.has("width")
False
What the code above draws
ensure_id() → ElementId

Return the id, assigning a generated one if needed.

Returns:

str – The existing or newly generated id.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").ensure_id()
'a'
extend(children: Iterable[Child]) → None

Append every child in children.

Parameters:

children (iterable of Element or str) – The children to append, in order.

Examples

>>> import svgwriter as svg
>>> g = svg.group()
>>> g.extend([svg.circle(0, 0, 1), svg.circle(1, 1, 1)])
>>> len(g)
2
What the code above draws
find(match: type[Element] | str) → Element | None

First descendant (or self) matching a tag or class.

Parameters:

match (str or type) – A tag name or an element class.

Returns:

Element or None – The first match in document order, or None.

See also

find_all

Every match.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> g.find("circle").tag, g.find("rect")
('circle', None)
What the code above draws
find_all(match: type[Element] | str) → list[Element]

Every descendant (and self) matching a tag or class.

Parameters:

match (str or type) – A tag name or an element class.

Returns:

list of Element – The matches in document order.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1), svg.circle(1, 1, 1))
>>> len(g.find_all("circle"))
2
What the code above draws
flip_x(*, about: Pivot | None = None, local: bool = False) → E

Mirror left-right (across the vertical line through the anchor) and return self.

Parameters:
  • about (str or (float, float), optional) – Point on the mirror line, as for rotate(). Defaults to the anchor.

  • local (bool, optional) – Compose in the element’s own frame. Defaults to False.

Returns:

Transformable – This element.

See also

flip_y

Mirror top-bottom.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 10, 10).flip_x(about=(0, 0)).bounds()
BBox(x=-10.0, y=0.0, width=10.0, height=10.0)
flip_y(*, about: Pivot | None = None, local: bool = False) → E

Mirror top-bottom (across the horizontal line through the anchor) and return self.

Parameters:
  • about (str or (float, float), optional) – Point on the mirror line, as for rotate(). Defaults to the anchor.

  • local (bool, optional) – Compose in the element’s own frame. Defaults to False.

Returns:

Transformable – This element.

See also

flip_x

Mirror left-right.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 10, 10).flip_y(about=(0, 0)).bounds()
BBox(x=0.0, y=-10.0, width=10.0, height=10.0)
classmethod from_attributes(attributes: Mapping[str, str]) → E

Build an instance from raw SVG attributes, bypassing __init__.

Parameters:

attributes (mapping of str to str) – Attributes by SVG name, stored as given.

Returns:

Element – A new instance of this class.

Examples

>>> import svgwriter as svg
>>> svg.Rect.from_attributes({"width": "5"}).attributes
{'width': '5'}
get(name: str, default: object = None) → object

Return an attribute value, or default if it is not set.

A presentation property declared in the inline style is read from there, as it overrides the attribute.

Parameters:
  • name (str) – The attribute name, Python or SVG spelling.

  • default (object, optional) – Returned when the attribute is absent. Defaults to None.

Returns:

object – The stored value or the default.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1, stroke_width=2)
>>> r.get("stroke_width"), r.get("fill", "none")
(2, 'none')
>>> svg.rect(0, 0, 1, 1, style="fill:blue").get("fill")
'blue'
What the code above draws
get_element_by_id(element_id: str) → Element | None

Find the element with the given id in this subtree.

Parameters:

element_id (str) – The id to look for.

Returns:

Element or None – The matching element, or None.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1, id="dot"))
>>> g.get_element_by_id("dot").tag
'circle'
What the code above draws
has(name: str) → bool

Report whether an attribute is set.

Parameters:

name (str) – The attribute name, Python or SVG spelling.

Returns:

bool – True if the attribute is present.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1).has("width")
True
property id: str | None

The element id, or None.

Assigning validates the name as an XML name; assigning None removes it.

Raises:

ValueError – On assignment, if the value is not a valid id.

Examples

>>> import svgwriter as svg
>>> el = svg.rect(0, 0, 1, 1, id="a")
>>> el.id
'a'
What the code above draws
insert(index: int, child: C) → C

Insert a child at index and return it.

Parameters:
  • index (int) – Position among the children.

  • child (Element or str) – The child to insert.

Returns:

Element or str – The inserted child.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1))
>>> _ = g.insert(0, svg.rect(0, 0, 1, 1))
>>> [c.tag for c in g]
['rect', 'circle']
What the code above draws
property iri: IRI

#id pointing at this element, assigning an id if needed.

Use it where SVG expects a plain IRI (xlink:href of <use>, <mpath>, …); passing the element itself does the same. An element’s own xlink:href is el.href.

See also

url

The url(#id) form.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").iri.svg_value()
'#a'
iter(tag: str | None = None, *, max_depth: float = inf) → Iterator[Element]

Depth-first walk over this element and its descendants.

Parameters:
  • tag (str, optional) – Yield only elements with this tag. Defaults to None (all).

  • max_depth (float, optional) – Deepest level to descend to; 0 is this element only. Defaults to infinity.

Yields:

Element – Elements in document order, starting with this one.

Examples

>>> import svgwriter as svg
>>> g = svg.group(svg.circle(0, 0, 1), svg.rect(0, 0, 1, 1))
>>> [e.tag for e in g.iter()]
['g', 'circle', 'rect']
What the code above draws
property label: str | None

A readable name for the element, stored as inkscape:label, or None.

Inkscape shows it in its Layers and Objects panels, and doc["name"] finds an element by label when no id matches. Assigning None removes it.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> r.label = "Logo"
>>> r.label, r.attributes["inkscape:label"]
('Logo', 'Logo')
What the code above draws
property left

Smallest x of bounds(); assign to move the element so its left edge is there.

classmethod lookup(tag: str) → type[Element] | None

Return the registered class for tag.

Parameters:

tag (str) – An SVG tag name.

Returns:

type[Element] or None – The class registered for the tag, or None if there is none.

Examples

>>> import svgwriter as svg
>>> svg.Element.lookup("rect").__name__
'Rect'
classmethod many(**columns: Any) → list[E]

Build many elements at once from columns that broadcast like numpy.

Each keyword is an attribute in Python spelling; a scalar applies to every element and an array gives one value per element. Colors may be one color, a list of colors or an (n, 3|4) array of 0-1 floats.

Parameters:

**columns – Attribute name to scalar or sequence.

Returns:

list of Element – The new elements, ready for doc.group(*elements) or doc.add_all.

Raises:

ValueError – If the columns do not broadcast to one length.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> dots = svg.Circle.many(cx=np.arange(3) * 10, cy=5, r=2, fill=["red", "green", "blue"])
>>> [str(d) for d in dots][1]
'<circle cx="10" cy="5" r="2" fill="green"/>'
place(x: float, y: float, *, at: str | None = None) → E

Move the element so its anchor, or the named point of its bounds, lands on (x, y).

Parameters:
  • x (float) – Target position in parent coordinates.

  • y (float) – Target position in parent coordinates.

  • at (str, optional) – A named point of bounds() ("top_left", "center", "bottom", …; see BBox.point()). Defaults to None, which uses the anchor.

Returns:

Transformable – This element.

Raises:
  • ValueError – If at is not a known point name.

  • TypeError – If at is given and the element’s size is unknown.

See also

align

Place relative to another element.

Examples

>>> import svgwriter as svg
>>> svg.circle(0, 0, 1).place(10, 20).position
Point(x=10.0, y=20.0)
>>> svg.rect(0, 0, 4, 2).place(10, 10, at="bottom_right").bounds()
BBox(x=6.0, y=8.0, width=4.0, height=2.0)
property position: Point

The anchor in parent coordinates; assign to move the element there.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 10, 10)
>>> r.position = (20, 20)
>>> r.position
Point(x=20.0, y=20.0)
What the code above draws
classmethod registered(category: Category | None = None) → dict[str, type[Element]]

Registered element classes, optionally filtered by category.

Parameters:

category (Category, optional) – Keep only classes of this category. Defaults to None (all).

Returns:

dict[str, type[Element]] – Registered classes keyed by tag.

Examples

>>> import svgwriter as svg
>>> "circle" in svg.Element.registered()
True
remove(child: Child) → None

Remove a child and detach it from this element.

Parameters:

child (Element or str) – The child to remove.

Raises:

ValueError – If the child is not present.

Examples

>>> import svgwriter as svg
>>> c = svg.circle(0, 0, 1)
>>> g = svg.group(c)
>>> g.remove(c)
>>> len(g)
0
What the code above draws
reset() → E

Drop the transform and return self.

Returns:

Transformable – This element.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1).translate(3, 3).reset().matrix.is_identity
True
property right

Largest x of bounds(); assign to move the right edge there.

root() → Element

Return the topmost ancestor, or this element if it has no parent.

Returns:

Element – The root of the tree this element belongs to.

Examples

>>> import svgwriter as svg
>>> c = svg.circle(0, 0, 1)
>>> g = svg.group(c)
>>> c.root() is g
True
What the code above draws
rotate(degrees: float | Angle, *, about: Pivot | None = None, local: bool = False) → E

Rotate by degrees (clockwise on screen) and return self.

Parameters:
  • degrees (float or Angle) – Angle (numbers are degrees); positive turns from +x towards +y.

  • about (str or (float, float), optional) – Pivot: a point name (see anchor), read on the element’s own box, or a point in parent coordinates (local ones with local). Defaults to the anchor.

  • local (bool, optional) – Compose in the element’s own frame. Defaults to False.

Returns:

Transformable – This element.

Raises:

TypeError – If degrees is a length or another non-angle quantity.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 10, 10).rotate(90).bounds()
BBox(x=0.0, y=0.0, width=10.0, height=10.0)
>>> svg.rect(0, 0, 10, 10).rotate(90, about="top_left").bounds()
BBox(x=-10.0, y=0.0, width=10.0, height=10.0)
save(target: AnyPath | TextSink, **options: Any) → None

Write to a path or text stream.

Parameters:
  • target (path or text stream) – Where to write the SVG markup.

  • **options – Serialization options, as for to_string().

See also

to_string

Serialize to a string.

Examples

>>> import io
>>> import svgwriter as svg
>>> buffer = io.StringIO()
>>> svg.circle(0, 0, 1).save(buffer, xml_declaration=False, pretty=False)
>>> buffer.getvalue()
'<circle cx="0" cy="0" r="1"/>'
scale(sx: float, sy: float | None = None, *, about: Pivot | None = None, local: bool = False) → E

Scale by sx and sy (default sx) about the anchor and return self.

Parameters:
  • sx (float) – Factors along x and y; negative values mirror. sy defaults to sx.

  • sy (float) – Factors along x and y; negative values mirror. sy defaults to sx.

  • about (str or (float, float), optional) – Fixed point, as for rotate(). Defaults to the anchor.

  • local (bool, optional) – Scale along the element’s own axes instead of the parent’s. Defaults to False.

Returns:

Transformable – This element.

Examples

>>> import svgwriter as svg
>>> svg.circle(50, 50, 10).scale(2).bounds()
BBox(x=30.0, y=30.0, width=40.0, height=40.0)
set(name: str, value: AttrValue) → None

Set one attribute; None removes it.

Attributes in other namespaces use Clark notation: "{uri}name". Colors with alpha are split into the color and its *-opacity attribute, and elements are replaced by references to them. A presentation property the inline style already declares (as Inkscape writes them) is changed there, where it takes effect.

Parameters:
  • name (str) – The attribute name, Python or SVG spelling.

  • value (object) – The new value; None removes the attribute.

Raises:
  • TypeError – If a length, angle or ~svgwriter.Quantity has the wrong dimension for the attribute (width given an area).

  • ValueError – If the attribute is unsupported, a color string is not a valid color or paint, or a color with alpha is given to an attribute without an opacity counterpart.

See also

update

Set several attributes at once.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> _ = r.set("stroke_width", 3)
>>> r.attributes["stroke-width"]
3
>>> styled = svg.rect(0, 0, 1, 1, style="fill:#ff0000")
>>> styled.fill = "blue"
>>> styled.attributes["style"], styled.has("fill")
(Style({'fill': 'blue'}), False)
What the code above draws What the code above draws
skew(x_degrees: float = 0.0, y_degrees: float = 0.0, *, about: Pivot | None = None, local: bool = False) → E

Skew along x then y by angles in degrees about the anchor and return self.

Parameters:
  • x_degrees (float, optional) – Skew angles along x and y. Defaults to 0.

  • y_degrees (float, optional) – Skew angles along x and y. Defaults to 0.

  • about (str or (float, float), optional) – Fixed point, as for rotate(). Defaults to the anchor.

  • local (bool, optional) – Compose in the element’s own frame. Defaults to False.

Returns:

Transformable – This element.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 10, 10).skew(45).bounds()
BBox(x=-5.0, y=0.0, width=20.0, height=10.0)
property text_content: str

All character data below this element, in document order (the DOM textContent).

Assigning replaces every child with the single string.

Examples

>>> import svgwriter as svg
>>> t = svg.text("Hello world")
>>> t[6:].fill = "red"
>>> t.text_content
'Hello world'
>>> title = svg.Title("Old")
>>> title.text_content = "New"
>>> str(title)
'<title>New</title>'
What the code above draws
to_string(**options: Any) → str

Serialize to an SVG string; see svgwriter.formats.io.SerializeOptions.

Parameters:

**options – Serialization options such as pretty or xml_declaration.

Returns:

str – The SVG markup.

See also

save

Write to a file or stream.

Examples

>>> import svgwriter as svg
>>> svg.circle(0, 0, 1).to_string(xml_declaration=False, pretty=False)
'<circle cx="0" cy="0" r="1"/>'
property top

Smallest y of bounds(); assign to move the top edge there.

property top_center

Middle of the top edge of bounds(); assign to move it there.

property top_left

Top-left corner of bounds(); assign to move it there.

property top_right

Top-right corner of bounds(); assign to move it there.

translate(dx: float, dy: float = 0.0, *, local: bool = False) → E

Move by (dx, dy) and return self.

The offset is added to the transform, in the parent frame unless local.

Parameters:
  • dx (float) – Offset in user units. dy defaults to 0.

  • dy (float) – Offset in user units. dy defaults to 0.

  • local (bool, optional) – Measure along the element’s own (rotated, scaled) axes instead of the parent’s. Defaults to False.

Returns:

Transformable – This element.

See also

place

Move the anchor to an absolute position.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 2, 2).rotate(90).translate(10, 0).position
Point(x=11.0, y=1.0)
update(attributes: Mapping[str, AttrValue] | None = None, /, **kwargs: AttrValue) → None

Set several attributes; None values are skipped.

Parameters:
  • attributes (mapping, optional) – Attributes by name. Defaults to None.

  • **kwargs – More attributes, Python spelling.

See also

set

Set a single attribute.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1)
>>> _ = r.update(fill="red", stroke=None)
>>> r.get("fill")
'red'
What the code above draws
property url: FuncIRI

url(#id) pointing at this element, assigning an id if needed.

Use it where SVG expects a functional IRI (fill, clip-path, filter); passing the element itself does the same.

See also

iri

The plain #id form.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 1, 1, id="a").url.svg_value()
'url(#a)'
attributes: dict[str, Any]
children: list[Element | str]
parent: Element | None
property transform: Matrix

Same as t, under the SVG attribute’s name.

Examples

>>> import svgwriter as svg
>>> r = svg.rect(0, 0, 1, 1).translate(2, 3)
>>> r.transform == r.matrix
True
What the code above draws
local_bbox() → BBox | None[source]

Bounding box in local coordinates (before t), if known.

Returns:

BBox or None – The box, or None when the element has no known extent. Subclasses override this.

Examples

>>> import svgwriter as svg
>>> svg.rect(1, 2, 3, 4).local_bbox()
BBox(x=1.0, y=2.0, width=3.0, height=4.0)
class Convertible(*args: Element | str, transform: Transform | TransformOp | Matrix | str | None = None, anchor: tuple[float, float] | Point | str | None = None, **kwargs: Any)[source]

Bases: Transformable

Conversions (to_path, to_group, rasterize) and clip.

Every conversion returns a new element or array and leaves this one unchanged. A conversion that cannot be done raises TypeError naming the element and the alternatives.

See also

svgwriter.model.elements.Transformable

Transforms and anchors.

Examples

>>> import svgwriter as svg
>>> box = svg.rect(0, 0, 10, 5, fill="red")
>>> type(box.to_path()).__name__
'Path'
>>> box.rasterize().shape
(5, 10, 4)
What the code above draws
to_path(**kwargs: Any) → Path[source]

An equivalent <path>.

Shapes override this to return their outline; the base implementation is for elements that have none.

Parameters:

**kwargs – Options understood by the overriding implementations.

Returns:

Path – A new <path> element.

Raises:

TypeError – When the element has no vector outline (an <image>, for instance).

Examples

>>> import svgwriter as svg
>>> svg.circle(5, 5, 2).to_path().tag
'path'
to_group(**kwargs: Any) → Group[source]

A <g> holding a copy of this element; keywords become group attributes.

A group returns a copy of itself.

Parameters:

**kwargs – Attributes set on the group.

Returns:

Group – A new <g>.

Examples

>>> import svgwriter as svg
>>> g = svg.circle(5, 5, 2).to_group(opacity=0.5)
>>> g.tag, len(g)
('g', 1)
What the code above draws
to_image(*, scale: float = 1.0, padding: float = 0.0, background: ColorLike | None = None) → Image[source]

Rasterize into an <image> placed over this element’s bounds.

Parameters:
  • scale (float, optional) – Pixels per user unit. Defaults to 1.0.

  • padding (float, optional) – Extra margin in user units. Defaults to 0.0.

  • background (color, optional) – Painted under the element. Defaults to None (transparent).

Returns:

Image – Embedded pixels positioned where the element was drawn.

Examples

>>> import svgwriter as svg
>>> img = svg.circle(10, 10, 5, fill="red").to_image(scale=2)
>>> img.shape, (img.x, img.y, img.width)
((20, 20), (5.0, 5.0, 10.0))
What the code above draws
clip(*shapes: Element, units: str | None = None) → E[source]

Clip to the union of shapes (or a ClipPath) and return self.

Shapes are in this element’s local coordinates, so the clip moves with the element. The <clipPath> goes into <defs> when the element is in a document.

Parameters:
  • *shapes (Element) – Shapes, text or <use>; a single ClipPath is used as is.

  • units ({"userSpaceOnUse", "objectBoundingBox"}, optional) – clipPathUnits for a new clip path. Defaults to None.

Returns:

Convertible – This element.

Raises:

TypeError – If no shape is given.

See also

unclip

Remove the clip.

Notes

to_path() (or flatten_clip()) bakes the clip into one path with a boolean intersection.

Examples

>>> import svgwriter as svg
>>> art = svg.rect(0, 0, 100, 100, fill="teal").clip(svg.circle(50, 50, 40))
>>> art.clip_region.tag
'clipPath'
What the code above draws
unclip() → E[source]

Remove the clip and return self.

Returns:

Convertible – This element.

Examples

>>> import svgwriter as svg
>>> art = svg.rect(0, 0, 9, 9).clip(svg.circle(4, 4, 3)).unclip()
>>> art.clip_region is None
True
What the code above draws
property clip_region: ClipPath | None

The <clipPath> this element is clipped by, if it can be found.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 9, 9).clip_region is None
True
render_bounds() → BBox | None[source]

bounds() grown by half the stroke width.

Returns:

BBox or None – The box including the stroke, or None when bounds() is unknown.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 10, 10, stroke="black", stroke_width=2).render_bounds()
BBox(x=-1.0, y=-1.0, width=12.0, height=12.0)
rasterize(*, scale: float = 1.0, pixels: tuple[int, int] | int | None = None, padding: float = 0.0, background: ColorLike | None = None) → np.ndarray[source]

Render this element alone into an RGBA array.

The canvas is the element’s render_bounds() in parent coordinates, plus padding on every side.

Parameters:
  • scale (float, optional) – Pixels per user unit. Defaults to 1.0.

  • pixels ((int, int) or int, optional) – Exact output (columns, rows), or one int for a square; the element is fitted and centred, and scale is ignored. Defaults to None.

  • padding (float, optional) – Extra margin in user units. Defaults to 0.0.

  • background (color, optional) – Painted under the element. Defaults to None (transparent).

Returns:

numpy.ndarray – float32 array, shape (rows, columns, 4), straight RGBA in 0..1.

Raises:

TypeError – When the element’s size is unknown (an <image> without width and height, for instance).

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 8, 4, fill="red").rasterize(scale=2).shape
(8, 16, 4)
class PathOperand(*children: Element | str, id: str | None = None, attrs: Mapping[str, Any] | None = None, **extra: Any)[source]

Bases: BooleanOps, Measurement, AlongPath, Element

An element with an outline.

Adds to_path(), exact measurement, walking the outline (at(), sample(), between(), round_corners(), …) and boolean operators:

  • a | b: union

  • a & b: intersection

  • a - b: difference

  • a ^ b: exclusive or

Every operator returns a new Path carrying the left operand’s presentation attributes.

See also

svgwriter.model.elements._behavior.Convertible

Conversions and clipping.

Examples

>>> import svgwriter as svg
>>> a, b = svg.rect(0, 0, 10, 10), svg.rect(5, 0, 10, 10)
>>> (a | b).exact_bbox().width, (a & b).exact_bbox().width
(15.0, 5.0)
What the code above draws What the code above draws
geometry_attributes: ClassVar[frozenset[str]] = frozenset({})

Attributes describing the geometry; not copied by to_path().

local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
outline() → PathData[source]

Outline in parent coordinates: clipped by clip-path, then t applied.

Returns:

PathData – The outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).translate(1, 1).outline())
'M1 1 L5 1 L5 4 L1 4 Z'
to_path(**kwargs: Any) → Path[source]

An equivalent <path> with t and any clip baked in.

Presentation attributes are copied; keywords override them.

Parameters:

**kwargs – Attributes set on the new path, overriding copied ones.

Returns:

Path – A new <path> element.

Examples

>>> import svgwriter as svg
>>> svg.rect(0, 0, 4, 3, fill="red").to_path().get("fill")
'red'
flatten_clip() → Path[source]

The visible area as one <path>: to_path() with the clip applied.

Returns:

Path – A new <path> without clipping.

Examples

>>> import svgwriter as svg
>>> art = svg.rect(0, 0, 10, 10).clip(svg.rect(0, 0, 5, 5))
>>> art.flatten_clip().exact_bbox().width
5.0
What the code above draws
expand_pattern() → Element[source]

Replace a pattern fill by copies of its tile, clipped to this outline (plain vector graphics).

Useful for targets without pattern support (plotters, cutters, some editors). The stroke, if any, is kept on top.

Returns:

Group – A <g> with this element’s transform holding the clipped tiles and an unfilled copy for the stroke.

Raises:

ValueError – If fill is not a <pattern>.

See also

svgwriter.Pattern.tiles

The tiles on their own.

Examples

>>> import svgwriter as svg
>>> dots = svg.Pattern(svg.circle(2, 2, 1), width=4, height=4, pattern_units="userSpaceOnUse")
>>> expanded = svg.rect(0, 0, 8, 8, fill=dots, stroke="black").expand_pattern()
>>> [e.tag for e in expanded], len(expanded[0][0])
(['g', 'rect'], 4)
What the code above draws
transformed(transform: TransformLike) → Path[source]

A <path> with t and then transform baked into its coordinates.

Parameters:

transform (Transform, Matrix or str) – The transform applied after t.

Returns:

Path – A new <path> with no transform attribute.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 1, 1).transformed("translate(2 0)").d)
'M2 0 L3 0 L3 1 L2 1 Z'

Shapes

Basic shapes and <path>.

Modules

_base Shape and length helpers _basic rect, circle, ellipse, line _poly polyline, polygon _path path

class Circle(cx: LengthLike = 0, cy: LengthLike = 0, r: LengthLike | None = None, *, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Shape

<circle>: a circle from its center and radius.

Parameters:
  • cx (length, optional) – Center. Defaults to 0.

  • cy (length, optional) – Center. Defaults to 0.

  • r (length) – Radius; required, by position or as r= (svg.circle(r=10) keeps the center at 0, 0).

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Raises:

TypeError – If r is not given (circle needs r).

See also

Ellipse

Different horizontal and vertical radii.

Examples

>>> import svgwriter as svg
>>> str(svg.Circle(5, 5, 3))
'<circle cx="5" cy="5" r="3"/>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'cx', 'cy', 'r'})

Attributes describing the geometry; not copied by to_path().

cx

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

cy

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

r

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

classmethod from_bbox(box: BBox, **kwargs: Any) → Self[source]

Create the largest circle centered in a bounding box.

Parameters:
  • box (BBox) – The area to fit.

  • **kwargs

  • Presentation

  • spelling. (core and other SVG attributes in Python)

Returns:

Circle – The new circle.

Examples

>>> import svgwriter as svg
>>> str(svg.Circle.from_bbox(svg.BBox(0, 0, 10, 20)))
'<circle cx="5" cy="10" r="5"/>'
property area: float

Enclosed area in square user units.

Examples

>>> import svgwriter as svg
>>> round(svg.Circle(0, 0, 1).area, 4)
3.1416
property bbox: BBox

Geometric bounding box in user units.

Examples

>>> import svgwriter as svg
>>> svg.Circle(0, 0, 2).bbox
BBox(x=-2.0, y=-2.0, width=4.0, height=4.0)
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'circle'
class Ellipse(cx: LengthLike = 0, cy: LengthLike = 0, rx: LengthLike | None = None, ry: LengthLike | None = None, *, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Shape

<ellipse>: an ellipse from its center and two radii.

Parameters:
  • cx (length, optional) – Center. Defaults to 0.

  • cy (length, optional) – Center. Defaults to 0.

  • rx (length) – Horizontal and vertical radii; required, by position or as rx= and ry=.

  • ry (length) – Horizontal and vertical radii; required, by position or as rx= and ry=.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Raises:

TypeError – If rx or ry is not given (ellipse needs rx).

See also

Circle

Equal radii.

Examples

>>> import svgwriter as svg
>>> str(svg.Ellipse(5, 5, 3, 2))
'<ellipse cx="5" cy="5" rx="3" ry="2"/>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'cx', 'cy', 'rx', 'ry'})

Attributes describing the geometry; not copied by to_path().

cx

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

cy

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

rx

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

ry

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

classmethod from_bbox(box: BBox, **kwargs: Any) → Self[source]

Create the ellipse inscribed in a bounding box.

Parameters:
  • box (BBox) – The area to fill.

  • **kwargs

  • Presentation

  • spelling. (core and other SVG attributes in Python)

Returns:

Ellipse – The new ellipse.

Examples

>>> import svgwriter as svg
>>> str(svg.Ellipse.from_bbox(svg.BBox(0, 0, 10, 20)))
'<ellipse cx="5" cy="10" rx="5" ry="10"/>'
property bbox: BBox

Geometric bounding box in user units.

Examples

>>> import svgwriter as svg
>>> svg.Ellipse(5, 5, 3, 2).bbox
BBox(x=2.0, y=3.0, width=6.0, height=4.0)
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'ellipse'
class Line(x1: LengthLike = 0, y1: LengthLike = 0, x2: LengthLike = 0, y2: LengthLike = 0, *, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Shape

<line>: a straight segment between two points.

Parameters:
  • x1 (length, optional) – Start point. Defaults to 0.

  • y1 (length, optional) – Start point. Defaults to 0.

  • x2 (length, optional) – End point. Defaults to 0.

  • y2 (length, optional) – End point. Defaults to 0.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Polyline

Several connected segments.

Examples

>>> import svgwriter as svg
>>> str(svg.Line(0, 0, 3, 4))
'<line x1="0" y1="0" x2="3" y2="4"/>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'x1', 'x2', 'y1', 'y2'})

Attributes describing the geometry; not copied by to_path().

x1

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

y1

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

x2

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

y2

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

property length: float

Distance between the end points in user units.

Examples

>>> import svgwriter as svg
>>> svg.Line(0, 0, 3, 4).length
5.0
property bbox: BBox

Geometric bounding box in user units.

Examples

>>> import svgwriter as svg
>>> svg.Line(0, 0, 3, 4).bbox
BBox(x=0.0, y=0.0, width=3.0, height=4.0)
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'line'
class Path(d: PathDataLike | PathBuilder = '', *, path_length: float | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PathCommands, Shape

<path>: an arbitrary outline described by path data.

Drawing methods append commands to d and return the path, so paths can be built fluently, and paths combine with the operators of PathOperand.

Parameters:
  • d (str, PathData, PathBuilder or iterable of commands, optional) – The path data. Defaults to an empty path.

  • path_length (float, optional) – Author’s computed total length. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Polyline

Straight segments from a point array.

svgwriter.PathBuilder

Build path data without an element.

Notes

p1 + p2 appends p2’s subpaths to a copy of p1; p += p2 does it in place, as do |=, &=, -= and ^= for the boolean operations.

Drawing methods append to d and return the path, so a path can be built fluently: path().move_to(0, 0).line_to(10, 0).close().

Examples

>>> import svgwriter as svg
>>> str(svg.Path().move_to(0, 0).line_to(10, 0).arc_to(5, 5, 10, 10).close().d)
'M0 0 L10 0 A5 5 0 0 1 10 10 Z'
>>> a = svg.Path("M0 0 H10", stroke="red")
>>> str(a + svg.Path("M0 5 H10"))
'<path d="M0 0 H10 M0 5 H10" stroke="red"/>'
What the code above draws
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'d', 'pathLength'})

Attributes describing the geometry; not copied by to_path().

path_length

Expose one SVG attribute as a Python attribute.

Parameters:
  • name (str, optional) – SVG attribute name; derived from the Python name when omitted.

  • convert (callable, optional) – Applied to raw strings (for instance values read by the parser).

  • default (object, optional) – Returned when the attribute is not set. Defaults to None.

See also

LengthAttr

Attribute that reads lengths.

Examples

>>> import svgwriter as svg
>>> svg.Rect.width.key
'width'
>>> svg.Rect(width=3, height=1).width
3.0
property connects: Connection | None

The objects this path joins as an Inkscape connector, or None for a plain path.

Assign (start, end) to attach the path to two elements (inkscape:connection-start and -end; it is redrawn as a straight line between their centres), or None to detach it.

Examples

>>> import svgwriter as svg
>>> doc = svg.Document(100, 40)
>>> a, b = doc.circle(10, 20, 5), doc.circle(90, 20, 5)
>>> wire = doc.path(stroke="black")
>>> wire.connects = (a, b)
>>> wire.connects.start is a, str(wire.d)
(True, 'M10 20 L90 20')
What the code above draws
property d: PathData

The path data; assign a string, PathData or builder.

Examples

>>> import svgwriter as svg
>>> p = svg.Path("M0 0 L10 0")
>>> p.d = "M5 5 H20"
>>> str(p)
'<path d="M5 5 H20"/>'
What the code above draws
property nodes: ndarray

All end and control points as an (N, 2) array; assign to move them.

Assigning rewrites d in absolute form. Broadcasting works as in numpy: p.nodes = p.nodes * [1, -1] + 5.

Examples

>>> import svgwriter as svg
>>> p = svg.Path("M0 0 L10 0 L10 10")
>>> p.nodes = p.nodes + [1, 2]
>>> str(p.d)
'M1 2 L11 2 L11 12'
What the code above draws
classmethod from_points(points: Iterable[tuple[float, float]], *, closed: bool = False, **kwargs: Any) → Self[source]

Create a path through a sequence of points.

Parameters:
  • points (iterable of tuple of float) – Vertices (x, y); the first starts the path.

  • closed (bool, optional) – Close the path back to the first point. Defaults to False.

  • **kwargs

  • Presentation

  • spelling. (core and other SVG attributes in Python)

Returns:

Path – The new path.

See also

Polyline

The same outline as a <polyline>.

Examples

>>> import svgwriter as svg
>>> str(svg.Path.from_points([(0, 0), (5, 0), (5, 5)], closed=True))
'<path d="M0 0 L5 0 L5 5 Z"/>'
property bbox: BBox

Geometric bounding box of the path data; empty paths give a zero box.

Examples

>>> import svgwriter as svg
>>> svg.Path("M0 0 L10 5").bbox
BBox(x=0.0, y=0.0, width=10.0, height=5.0)
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'path'
class Polygon(points: PointsLike | str = (), *, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Polyline

<polygon>: a closed Polyline.

Takes the same parameters as Polyline; the last vertex joins the first.

Parameters:
  • points (array-like or str, optional) – Vertices as (N, 2) pairs or SVG points text. Defaults to no points.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Polyline

The open variant.

Examples

>>> import svgwriter as svg
>>> str(svg.Polygon([(0, 0), (4, 0), (4, 3)]))
'<polygon points="0,0 4,0 4,3"/>'
property area: float

Enclosed area from the shoelace formula, in square user units.

Examples

>>> import svgwriter as svg
>>> svg.Polygon([(0, 0), (4, 0), (4, 3)]).area
6.0
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'polygon'
class Polyline(points: PointsLike | str = (), *, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Shape

<polyline>: an open sequence of straight segments.

Parameters:
  • points (array-like or str, optional) – Vertices as (N, 2) pairs or SVG points text. Defaults to no points.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Polygon

The closed variant.

Path.from_points

The same outline as a <path>.

Examples

>>> import svgwriter as svg
>>> str(svg.Polyline([(0, 0), (1, 2)]))
'<polyline points="0,0 1,2"/>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'points'})

Attributes describing the geometry; not copied by to_path().

property points: ndarray

Vertices as a read-only (N, 2) float array.

Assign any (N, 2) array-like or SVG points text to replace them.

Examples

>>> import svgwriter as svg
>>> line = svg.Polyline("0,0 1,2")
>>> line.points.tolist()
[[0.0, 0.0], [1.0, 2.0]]
>>> line.points = [(5, 5), (6, 6)]
>>> str(line)
'<polyline points="5,5 6,6"/>'
What the code above draws
classmethod regular(cx: float, cy: float, radius: float, sides: int = 6, **kwargs: Any) → Self[source]

Create the vertices of a regular polygon.

The first vertex is at the top and the rest follow clockwise.

Parameters:
  • cx (float) – Center.

  • cy (float) – Center.

  • radius (float) – Distance from the center to each vertex.

  • sides (int, optional) – Number of vertices. Defaults to 6.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Returns:

Polyline – The new element; on Polygon it is closed.

Examples

>>> import svgwriter as svg
>>> str(svg.Polygon.regular(0, 0, 10, 4))
'<polygon points="0,-10 10,0 0,10 -10,0"/>'
property bbox: BBox

Geometric bounding box of the vertices; no points give a zero box.

Examples

>>> import svgwriter as svg
>>> svg.Polyline([(0, 0), (4, 3)]).bbox
BBox(x=0.0, y=0.0, width=4.0, height=3.0)
tag: ClassVar[str] = 'polyline'
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
class Rect(x: LengthLike = 0, y: LengthLike = 0, width: LengthLike | None = None, height: LengthLike | None = None, *, rx: LengthLike | None = None, ry: LengthLike | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: RectConstructors, Shape

<rect>: an axis-aligned rectangle with optional rounded corners.

Giving only one of rx and ry uses it for both, and radii are clamped to half the width and height, as SVG specifies.

Parameters:
  • x (length, optional) – Top-left corner. Defaults to 0.

  • y (length, optional) – Top-left corner. Defaults to 0.

  • width (length) – Size; required, by position or as width= and height=.

  • height (length) – Size; required, by position or as width= and height=.

  • rx (length, optional) – Corner radii. Defaults to None.

  • ry (length, optional) – Corner radii. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Raises:

TypeError – If width or height is not given (rect needs width).

See also

Circle

A circle from its center and radius.

Path

Arbitrary outlines.

Examples

>>> import svgwriter as svg
>>> str(svg.Rect(1, 2, 3, 4, rx=1))
'<rect x="1" y="2" width="3" height="4" rx="1"/>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'height', 'rx', 'ry', 'width', 'x', 'y'})

Attributes describing the geometry; not copied by to_path().

x

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

y

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

width

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

height

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

rx

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

ry

reads a float when unitless, a Length when it has a unit.

Takes the same parameters as Attr. Values that cannot be parsed as a length are returned unchanged.

See also

Attr

The general attribute descriptor.

Examples

>>> import svgwriter as svg
>>> svg.Rect(width=5, height=1).width, svg.parse('<rect width="2mm"/>').width
(5.0, Length(value=2.0, unit=<Unit.MM: 'mm'>))
Type:

A length attribute

property bbox: BBox

Geometric bounding box in user units.

Examples

>>> import svgwriter as svg
>>> svg.Rect(1, 2, 3, 4).bbox
BBox(x=1.0, y=2.0, width=3.0, height=4.0)
corner_radii() → tuple[float, float][source]

Return the effective corner radii.

A missing radius takes the other’s value, and each is clamped to half the corresponding side.

Returns:

tuple of float – The effective (rx, ry) in user units.

Examples

>>> import svgwriter as svg
>>> svg.Rect(0, 0, 10, 10, rx=20).corner_radii()
(5.0, 5.0)
local_outline() → PathData[source]

Outline in the element’s own coordinates (t and clipping ignored).

Returns:

PathData – The outline; subclasses override this.

Raises:

TypeError – If the element has no outline.

Examples

>>> import svgwriter as svg
>>> str(svg.rect(0, 0, 4, 3).local_outline())
'M0 0 H4 V3 H0 Z'
tag: ClassVar[str] = 'rect'
class Shape(*args: Element | str, transform: Transform | TransformOp | Matrix | str | None = None, anchor: tuple[float, float] | Point | str | None = None, **kwargs: Any)[source]

Bases: PathOperand, GraphicsElement, ABC

Base class for the basic shapes and paths.

Every shape converts to a <path> (to_path()) and supports the geometric operators of PathOperand.

Parameters:

**kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Path

The general shape, built from path data.

Examples

>>> import svgwriter as svg
>>> str(svg.Rect(0, 0, 10, 5).to_path())
'<path d="M0 0 H10 V5 H0 Z"/>'
abstract property bbox: BBox

Geometric bounding box in user units, ignoring stroke and transform.

Examples

>>> import svgwriter as svg
>>> svg.Circle(5, 5, 3).bbox
BBox(x=2.0, y=2.0, width=6.0, height=6.0)
local_bbox() → BBox[source]

Bounding box in local coordinates (before t), if known.

Returns:

BBox or None – The box, or None when the element has no known extent. Subclasses override this.

Examples

>>> import svgwriter as svg
>>> svg.rect(1, 2, 3, 4).local_bbox()
BBox(x=1.0, y=2.0, width=3.0, height=4.0)
category: ClassVar[Category] = 9

Structure and images

Structural, descriptive and external-content elements.

class A(*children: Child, href: str | Element | None = None, target: str | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PathOperand, GraphicsElement, XLinkAttributes, GraphicsContainer

<a>: a hyperlink around graphics.

Parameters:
  • *children (Element) – Initial content.

  • href (str or Element, optional) – The link target. Defaults to None.

  • target (str, optional) – Where to open the link, such as "_blank". Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.A(svg.Rect(0, 0, 5, 5), href="https://example.com"))
'<a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://example.com"><rect x="0" y="0" width="5" height="5"/></a>'
local_bbox() → BBox | None[source]

Box around the link’s children.

Returns:

BBox or None – The union of the children’s bounding boxes, or None when empty.

Examples

>>> import svgwriter as svg
>>> svg.A(svg.rect(0, 0, 5, 5), href="#x").local_bbox()
BBox(x=0.0, y=0.0, width=5.0, height=5.0)
local_outline() → PathData[source]

Union of the link’s children’s outlines.

Returns:

PathData – The combined outline of every child.

Examples

>>> import svgwriter as svg
>>> svg.A(svg.rect(0, 0, 5, 5), href="#x").local_outline().bbox
BBox(x=0.0, y=0.0, width=5.0, height=5.0)
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'a'
class ColorProfile(name: str, href: str | None = None, *, local: str | None = None, rendering_intent: str | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, CoreAttributes

<color-profile>: an ICC profile referenced by icc-color.

Parameters:
  • name (str) – Name used to refer to the profile from icc-color.

  • href (str, optional) – Location of the profile. Defaults to None.

  • local (str, optional) – Name of a locally installed profile. Defaults to None.

  • rendering_intent (str, optional) – Rendering intent, such as "perceptual". Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.ColorProfile("sRGB", "srgb.icc"))
'<color-profile xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="srgb.icc" name="sRGB"/>'
content: ClassVar[ContentModel] = 2
tag: ClassVar[str] = 'color-profile'
class Defs(*args: Child, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: GraphicsElement, GraphicsContainer

<defs>: referenced, never directly rendered, content.

Parameters:
  • *children (Element) – Initial content.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Use

Instantiate an element defined here.

Examples

>>> import svgwriter as svg
>>> str(svg.Defs(svg.Circle(5, 5, 3, id="dot")))
'<defs><circle id="dot" cx="5" cy="5" r="3"/></defs>'
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'defs'
class Desc(text: str = '', *, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Descriptive

<desc>: a long description.

Parameters:
  • text (str, optional) – The description text. Defaults to empty.

  • **kwargs – Style, core and other SVG attributes in Python spelling.

See also

Title

A short, tooltip-style description.

Examples

>>> import svgwriter as svg
>>> str(svg.Desc("A red square"))
'<desc>A red square</desc>'
tag: ClassVar[str] = 'desc'
class Group(*args: Child, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PathOperand, GraphicsElement, GraphicsContainer

<g>: groups elements to share attributes and a transform.

Parameters:
  • *children (Element) – Initial content.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Svg

A group that also establishes a viewport.

Examples

>>> import svgwriter as svg
>>> str(svg.Group(svg.Rect(0, 0, 5, 5), fill="red"))
'<g fill="red"><rect x="0" y="0" width="5" height="5"/></g>'
local_bbox() → BBox | None[source]

Box around the group’s children.

Returns:

BBox or None – The union of the children’s bounding boxes, or None when empty.

Examples

>>> import svgwriter as svg
>>> svg.Group(svg.rect(0, 0, 5, 5), svg.rect(10, 10, 5, 5)).local_bbox()
BBox(x=0.0, y=0.0, width=15.0, height=15.0)
local_outline() → PathData[source]

Union of the group’s children’s outlines.

Returns:

PathData – The combined outline of every child.

Examples

>>> import svgwriter as svg
>>> svg.Group(svg.rect(0, 0, 5, 5), svg.rect(10, 10, 5, 5)).local_outline().bbox
BBox(x=0.0, y=0.0, width=15.0, height=15.0)
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'g'
class Image(source: np.ndarray | str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, *, alpha: np.ndarray | float | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: GraphicsElement, XLinkAttributes, Positioned

<image>: embedded pixels (numpy) or a linked file.

Parameters:
  • source (array, str or None, optional) – A pixel array ((H, W), (H, W, 3) or (H, W, 4); floats in 0..1 or uint8) to embed, or a URL/path to link. Defaults to None.

  • x (length, optional) – Top-left corner. Defaults to None.

  • y (length, optional) – Top-left corner. Defaults to None.

  • width (length, optional) – Display size; defaults to the pixel size for embedded images.

  • height (length, optional) – Display size; defaults to the pixel size for embedded images.

  • alpha (array or float, optional) – Separate alpha, broadcastable to (H, W). Defaults to None.

  • preserve_aspect_ratio (PreserveAspectRatio or str, optional) – How the pixels fit the box. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Image.open

Embed an image file.

Pixels

The pixel container behind an embedded image.

Notes

color is float32 (H, W, C) (C is 1 for grayscale, else 3) and alpha is the scalar 1.0 when fully opaque, otherwise (H, W, 1), so color * alpha always broadcasts. Edit them in place or assign new arrays. Pixels are written as a PNG data: URI.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> img = svg.Image(np.zeros((2, 3, 3)), x=10)
>>> img.shape, img.width, img.alpha
((2, 3), 3.0, 1.0)
>>> img.color[..., 0] = 1.0
>>> img.rgba()[0, 0].tolist()
[1.0, 0.0, 0.0, 1.0]
What the code above draws
classmethod open(path: str | PathLike[str], **kwargs: Any) → Image[source]

Decode an image file into an embedded <image>.

Parameters:
  • path (str or os.PathLike) – A PNG, JPEG, WebP, GIF or BMP file.

  • **kwargs – Attributes for the image, as in the constructor.

Returns:

Image – An image holding the decoded pixels, sized to match them unless width and height are given.

Raises:

ValueError – If the file cannot be decoded.

Examples

>>> import os, tempfile
>>> import numpy as np
>>> import svgwriter as svg
>>> with tempfile.TemporaryDirectory() as folder:
...     name = os.path.join(folder, "pixel.png")
...     _ = open(name, "wb").write(svg.Image(np.zeros((2, 3, 3))).encode())
...     svg.Image.open(name).shape
(2, 3)
property pixels: Pixels | None

The embedded Pixels, or None for a linked image.

A data: URI read from a document is decoded on first use.

Examples

>>> import svgwriter as svg
>>> svg.Image("photo.png").pixels is None
True
property color: ndarray

Color channels, float32 (H, W, 1|3) in 0..1; editable in place.

Assign an array to replace them.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> img = svg.Image(np.zeros((1, 2, 3)))
>>> img.color = np.ones((1, 2, 3))
>>> float(img.color.min())
1.0
What the code above draws
property alpha: ndarray | float

1.0 when opaque, else float32 (H, W, 1).

Assign a scalar or an array broadcastable to (H, W) to replace it.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> img = svg.Image(np.zeros((2, 2, 3)))
>>> img.alpha = 0.5
>>> img.alpha.shape
(2, 2, 1)
What the code above draws
Type:

Alpha

property shape: tuple[int, int]

Pixel (rows, columns).

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> svg.Image(np.zeros((2, 3))).shape
(2, 3)
rgba() → ndarray[source]

Combine color and alpha into one array.

Returns:

numpy.ndarray – (H, W, 4) float32 straight (unpremultiplied) RGBA.

Raises:

TypeError – If the image links to a file rather than embedding pixels.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> svg.Image(np.zeros((1, 1, 3))).rgba().tolist()
[[[0.0, 0.0, 0.0, 1.0]]]
category: ClassVar[Category] = 7
content: ClassVar[ContentModel] = 2
encode(format: str = 'png', *, quality: int = 90) → bytes[source]

Encode the pixels as an image file.

Parameters:
  • format ({"png", "jpeg", "webp", "bmp", "gif", "ppm"}, optional) – Output format. Defaults to "png".

  • quality (int, optional) – Compression quality for lossy formats; ignored by BMP, GIF and PPM. Defaults to 90.

Returns:

bytes – The encoded file contents.

Raises:

TypeError – If the image links to a file rather than embedding pixels.

Examples

>>> import numpy as np
>>> import svgwriter as svg
>>> svg.Image(np.zeros((2, 2, 3))).encode("jpeg")[0]
255
tag: ClassVar[str] = 'image'
local_bbox() → BBox | None[source]

Box from the x, y, width and height attributes.

Returns:

BBox or None – The image rectangle, or None when it has no size.

Examples

>>> import svgwriter as svg
>>> svg.Image(width=4, height=3).local_bbox()
BBox(x=0.0, y=0.0, width=4.0, height=3.0)
>>> svg.Image().local_bbox() is None
True
class Pixels(color: ndarray | None, alpha: ndarray | float = 1.0, source: str | None = None)[source]

Bases: PixelCompositing, Drawable

Image data: color channels plus a broadcastable alpha.

color is (H, W, C) with C in {1, 3} and alpha broadcasts to it. Both are float32 in 0..1. A fully opaque image keeps alpha as the scalar 1.0. Written as a PNG data: URI.

Parameters:
  • color (numpy.ndarray or None) – float32 (H, W, 1|3) color channels; None when the pixels are still an undecoded data: URI.

  • alpha (numpy.ndarray or float, optional) – Opacity, 1.0 or an array broadcastable to color. Defaults to 1.0.

  • source (str, optional) – The data: URI to decode lazily when color is None. Defaults to None.

See also

Image

The <image> element holding pixels.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.zeros((2, 3, 3)))
Pixels(2x3, channels=3, opaque=True)
classmethod from_array(array: object, alpha: object = None) → Pixels[source]

Create pixels from an array, splitting off any embedded alpha channel.

Parameters:
  • array (array-like) – (H, W) or (H, W, 1|2|3|4) values, floats in 0..1 or uint8. Two and four channels carry alpha last.

  • alpha (array-like or float, optional) – Separate alpha broadcastable to (H, W). Defaults to None.

Returns:

Pixels – The color and alpha as float32 arrays.

Raises:
  • ValueError – If the shape is unsupported, float values leave 0..1, or alpha is given both in array and as alpha.

  • TypeError – If the array is not numeric.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> px = Pixels.from_array(np.zeros((1, 2, 4)))
>>> px.color.shape, px.alpha.shape
((1, 2, 3), (1, 2, 1))
classmethod decode(data: bytes, *, profile: bytes | str | None = None, intent: str | None = None) → Pixels[source]

Decode encoded image bytes into sRGB.

Embedded colour information (ICC profile, PNG gAMA/cHRM) is applied, so the pixels look as the file intends. ICC profiles go through LittleCMS.

Parameters:
  • data (bytes) – PNG, JPEG, WebP, GIF, BMP or PNM file contents.

  • profile (bytes or str, optional) – The color-profile of the image: ICC profile bytes that replace any embedded profile, "sRGB" to read the stored values as sRGB, or "auto" (the default, also None) to use the embedded profile.

  • intent (str, optional) – SVG rendering-intent for a profile conversion, such as "relative-colorimetric". Defaults to perceptual.

Returns:

Pixels – The decoded pixels.

Raises:

ValueError – If the data is damaged or in an unknown format, or profile is an unknown keyword.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> png = Pixels.from_array(np.zeros((2, 2, 3))).encode()
>>> Pixels.decode(png)
Pixels(2x2, channels=3, opaque=True)
property color: ndarray

Color channels, float32 (H, W, 1|3) in 0..1; editable in place.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> pixels = Pixels.from_array(np.zeros((2, 3, 3)))
>>> pixels.color.shape
(2, 3, 3)
property alpha: ndarray | float

1.0 when opaque, else float32 (H, W, 1).

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.zeros((2, 3, 3))).alpha
1.0
>>> Pixels.from_array(np.zeros((2, 3, 3)), alpha=0.5).alpha.shape
(2, 3, 1)
Type:

Alpha

property shape: tuple[int, int]

Pixel (rows, columns).

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.zeros((2, 3, 3))).shape
(2, 3)
rgba() → ndarray[source]

Combine color and alpha into one array.

Returns:

numpy.ndarray – (H, W, 4) float32 straight (unpremultiplied) RGBA; grayscale is expanded to three channels.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.ones((1, 1))).rgba().tolist()
[[[1.0, 1.0, 1.0, 1.0]]]
clone() → Pixels[source]

Copy the pixel arrays.

Returns:

Pixels – An independent copy; editing it leaves this one unchanged.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> px = Pixels.from_array(np.zeros((1, 1, 3)))
>>> px.clone() == px
True
encode(format: str = 'png', *, quality: int = 90) → bytes[source]

Encode the pixels as an image file.

Parameters:
  • format ({"png", "jpeg", "webp", "bmp", "gif", "ppm"}, optional) – Output format. Defaults to "png".

  • quality (int, optional) – Compression quality for lossy formats; ignored by BMP, GIF and PPM. Defaults to 90.

Returns:

bytes – The encoded file contents.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.zeros((2, 2, 3))).encode()[1:4]
b'PNG'
svg_value(precision: int = 4) → str[source]

Serialize as a data: URI.

Parameters:

precision (int, optional) – Unused; present for the attribute-value protocol. Defaults to the library default.

Returns:

str – A base64 PNG data: URI, or the original URI if never decoded.

Examples

>>> import numpy as np
>>> from svgwriter.model.elements.structure import Pixels
>>> Pixels.from_array(np.zeros((1, 1, 3))).svg_value()[:30]
'data:image/png;base64,iVBORw0K'
class Metadata(*children: Child, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

<metadata>: arbitrary content, usually RDF in a foreign namespace.

Parameters:
  • *children (Element or str) – The content.

  • **kwargs – Core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.Metadata("licensed"))
'<metadata>licensed</metadata>'
category: ClassVar[Category] = 3
content: ClassVar[ContentModel] = 3
tag: ClassVar[str] = 'metadata'
class StyleElement(css: str | StyleSheet = '', *, type: str = 'text/css', media: str | None = None, title: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

<style>: an embedded style sheet.

Parameters:
  • css (str or StyleSheet, optional) – The style sheet text, or a StyleSheet to render. Defaults to empty.

  • type (str, optional) – Style language. Defaults to "text/css".

  • media (str, optional) – Media query the sheet applies to. Defaults to None.

  • title (str, optional) – Alternate style sheet title. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.StyleElement("circle { fill: red }"))
'<style type="text/css">circle { fill: red }</style>'
property css: str

The style sheet text; assign a string or a StyleSheet.

Examples

>>> import svgwriter as svg
>>> style = svg.StyleElement()
>>> style.css = "rect { fill: blue }"
>>> style.css
'rect { fill: blue }'
content: ClassVar[ContentModel] = 1
tag: ClassVar[str] = 'style'
class Svg(*children: Child, version: str | None = '1.1', base_profile: str | None = None, zoom_and_pan: str | None = None, content_style_type: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, view_box: ViewBoxLike | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Positioned, FitToViewBox, ExternalResourcesAttribute, PresentationAttributes, ConditionalAttributes, CoreAttributes, GraphicsContainer

<svg>: a document root or nested viewport.

Parameters:
  • *children (Element) – Initial content.

  • version (str, optional) – Written as the version attribute; None omits it. Defaults to the SVG version the library targets ("1.1").

  • base_profile (str, optional) – The remaining <svg>-specific attributes. Defaults to None.

  • zoom_and_pan (str, optional) – The remaining <svg>-specific attributes. Defaults to None.

  • content_style_type (str, optional) – The remaining <svg>-specific attributes. Defaults to None.

  • **kwargs – Position (x, y, width, height), view_box, presentation, conditional and core attributes in Python spelling.

See also

Group

Group content without establishing a viewport.

Symbol

A reusable viewport instantiated by <use>.

Examples

>>> import svgwriter as svg
>>> str(svg.Svg(svg.Circle(5, 5, 3), width=10, height=10))
'<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" version="1.1"><circle cx="5" cy="5" r="3"/></svg>'
classmethod with_size(width: float, height: float, **kwargs: Any) → Self[source]

Create a root with a size and a matching viewBox.

Parameters:
  • width (float) – Size in user units; the viewBox is 0 0 width height unless view_box is passed in kwargs.

  • height (float) – Size in user units; the viewBox is 0 0 width height unless view_box is passed in kwargs.

  • **kwargs

  • Presentation

  • spelling. (core and other SVG attributes in Python)

Returns:

Svg – The new root.

Examples

>>> import svgwriter as svg
>>> str(svg.Svg.with_size(100, 50))
'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50" viewBox="0 0 100 50" version="1.1"/>'
defs() → Defs[source]

Return the first <defs> child, creating one if needed.

A new <defs> is inserted before all other children.

Returns:

Defs – The existing or newly created <defs>.

Examples

>>> import svgwriter as svg
>>> doc = svg.Svg()
>>> doc.defs() is doc.defs()
True
>>> str(doc)
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><defs/></svg>'
What the code above draws
category: ClassVar[Category] = 10
tag: ClassVar[str] = 'svg'
class Switch(*args: Child, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PathOperand, GraphicsElement, GraphicsContainer

<switch>: renders the first child whose conditions pass.

Parameters:
  • *children (Element) – Initial content.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.Switch(svg.Rect(0, 0, 5, 5, required_extensions="x")))
'<switch><rect x="0" y="0" width="5" height="5" requiredExtensions="x"/></switch>'
local_bbox() → BBox | None[source]

Box around the switch’s children.

Returns:

BBox or None – The union of the children’s bounding boxes, or None when empty.

Examples

>>> import svgwriter as svg
>>> svg.Switch(svg.rect(0, 0, 5, 5)).local_bbox()
BBox(x=0.0, y=0.0, width=5.0, height=5.0)
local_outline() → PathData[source]

Union of the switch’s children’s outlines.

Returns:

PathData – The combined outline of every child.

Examples

>>> import svgwriter as svg
>>> svg.Switch(svg.rect(0, 0, 5, 5)).local_outline().bbox
BBox(x=0.0, y=0.0, width=5.0, height=5.0)
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'switch'
class Symbol(*args: Child, view_box: ViewBoxLike | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FitToViewBox, ExternalResourcesAttribute, PresentationAttributes, CoreAttributes, GraphicsContainer

<symbol>: a template instantiated by <use>.

Parameters:
  • *children (Element) – Initial content.

  • **kwargs – view_box, preserve_aspect_ratio, presentation and core attributes in Python spelling.

See also

Use

Instantiate the symbol.

Examples

>>> import svgwriter as svg
>>> str(svg.Symbol(svg.Circle(5, 5, 3), id="dot", view_box=(0, 0, 10, 10)))
'<symbol id="dot" viewBox="0 0 10 10"><circle cx="5" cy="5" r="3"/></symbol>'
category: ClassVar[Category] = 10
tag: ClassVar[str] = 'symbol'
class Title(text: str = '', *, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Descriptive

<title>: a short, tooltip-style description.

Parameters:
  • text (str, optional) – The title text. Defaults to empty.

  • **kwargs – Style, core and other SVG attributes in Python spelling.

See also

Desc

A longer description.

Examples

>>> import svgwriter as svg
>>> str(svg.Title("Square"))
'<title>Square</title>'
tag: ClassVar[str] = 'title'
class Use(href: str | Element | None = None, x: LengthLike | None = None, y: LengthLike | None = None, *, width: LengthLike | None = None, height: LengthLike | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: UseGeometry, PathOperand, GraphicsElement, XLinkAttributes, Positioned

<use>: renders a referenced element.

Parameters:
  • href (str or Element, optional) – The target as a reference string or the element itself, which gets an id if it has none. Defaults to None.

  • x (length, optional) – Translation applied to the instance. Defaults to None.

  • y (length, optional) – Translation applied to the instance. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Defs

Where referenced content usually lives.

Symbol

A viewport template to instantiate.

Examples

>>> import svgwriter as svg
>>> dot = svg.Circle(5, 5, 3, id="dot")
>>> str(svg.Use(dot, x=10))
'<use xmlns:xlink="http://www.w3.org/1999/xlink" x="10" xlink:href="#dot"/>'
What the code above draws
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'height', 'width', 'x', 'xlink:href', 'y'})

Attributes describing the geometry; not copied by to_path().

category: ClassVar[Category] = 10
tag: ClassVar[str] = 'use'
class View(*, view_target: str | None = None, zoom_and_pan: str | None = None, view_box: ViewBoxLike | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FitToViewBox, ExternalResourcesAttribute, CoreAttributes

<view>: a named view into the document.

Parameters:
  • view_target (str, optional) – Elements the view targets. Defaults to None.

  • zoom_and_pan (str, optional) – "magnify" or "disable". Defaults to None.

  • **kwargs – view_box, preserve_aspect_ratio, core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.View(id="zoomed", view_box=(0, 0, 50, 50)))
'<view id="zoomed" viewBox="0 0 50 50"/>'
content: ClassVar[ContentModel] = 0
tag: ClassVar[str] = 'view'

Text and fonts

Text content elements, live character handles and SVG fonts.

class AltGlyph(text: str = '', *, href: str | Element | None = None, glyph_ref: str | None = None, format: str | None = None, x: Coordinates | None = None, y: Coordinates | None = None, dx: Coordinates | None = None, dy: Coordinates | None = None, rotate: float | Sequence[float] | None = None, decoration: TextDecoration | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, PresentationAttributes, ConditionalAttributes, CoreAttributes, _TextPositioning

<altGlyph>: substitute glyphs for its character data.

Parameters:
  • text (str, optional) – The character data to substitute. Defaults to the empty string.

  • href (str or Element, optional) – An altGlyphDef or a glyph, or its IRI.

  • glyph_ref (str, optional) – Identifier of the glyph in the referenced font.

  • format (str, optional) – Glyph format of the font.

  • x (length or sequence of length, optional) – Absolute and relative positions.

  • y (length or sequence of length, optional) – Absolute and relative positions.

  • dx (length or sequence of length, optional) – Absolute and relative positions.

  • dy (length or sequence of length, optional) – Absolute and relative positions.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

AltGlyphDef

The definition href points at.

Examples

>>> str(AltGlyph("a", glyph_ref="g1"))
'<altGlyph glyphRef="g1">a</altGlyph>'
category: ClassVar[Category] = 11
content: ClassVar[ContentModel] = 1
tag: ClassVar[str] = 'altGlyph'
class AltGlyphDef(*args: Child, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes, Container[Element]

<altGlyphDef>: glyph substitutions for altGlyph.

Parameters:
  • *children (Element) – Either one sequence of GlyphRef or several AltGlyphItem.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

AltGlyph

The element that uses the definition.

Examples

>>> len(AltGlyphDef(AltGlyphItem(GlyphRef("#a"))))
1
category: ClassVar[Category] = 11
tag: ClassVar[str] = 'altGlyphDef'
class AltGlyphItem(*args: Child, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes, Container[GlyphRef]

<altGlyphItem>: one candidate set of glyph references.

Parameters:
  • *children (GlyphRef) – The glyph references of the candidate.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

AltGlyphDef

The container of candidates.

Examples

>>> len(AltGlyphItem(GlyphRef("#a"), GlyphRef("#b")))
2
category: ClassVar[Category] = 11
tag: ClassVar[str] = 'altGlyphItem'
class Character(text: Text, index: int, start: int | None = None, stop: int | None = None)[source]

Bases: _Characters

A live handle on character index of a <text> (text[i]).

Parameters:
  • text (Text) – The text the character belongs to.

  • index (int) – Zero-based index into Text.characters.

Notes

Moves and rotations edit the text’s dx/dy/rotate lists, so the result is still selectable SVG text. Scale and skew have no per-character form in SVG; use Text.glyphs() for outlines that can be transformed freely.

Examples

>>> import svgwriter as svg
>>> t = svg.text("abc", x=0, y=10)
>>> t[1].translate(0, -4).rotate(20)
Character('b', 1)
>>> t.dy, t.rotate_
((0.0, -4.0, 4.0), (0.0, 20.0, 0.0))
What the code above draws
property index: int

Zero-based index of the character in the text.

Examples

>>> import svgwriter as svg
>>> svg.text("abc", x=0, y=10)[2].index
2
property char: str

The character itself.

Examples

>>> import svgwriter as svg
>>> svg.text("abc", x=0, y=10)[1].char
'b'
property position: Point

Baseline origin of the glyph in the text’s local coordinates.

Examples

>>> import svgwriter as svg
>>> svg.text("abc", x=5, y=10)[0].position.y
10.0
property angle: float

Per-character rotation in degrees.

Examples

>>> import svgwriter as svg
>>> t = svg.text("abc", x=0, y=10)
>>> _ = t[1].rotate(20)
>>> t[1].angle
20.0
What the code above draws
class CharacterRange(text: Text, start: int, stop: int)[source]

Bases: _Characters

A live handle on characters [start, stop) of a <text> (text[a:b]).

Setting a presentation attribute on the range wraps it in a styled <tspan>.

Parameters:
  • text (Text) – The text the characters belong to.

  • start (int) – Index of the first character.

  • stop (int) – Index one past the last character.

Examples

>>> import svgwriter as svg
>>> t = svg.text("Hello world", x=0, y=20)
>>> t[6:11].fill = "red"
>>> str(t)
'<text x="0" y="20">Hello <tspan fill="red">world</tspan></text>'
What the code above draws
class CharacterSelection(ranges: Sequence[CharacterRange])[source]

Bases: object

Characters picked by a list, a mask or a stepped slice, as contiguous CharacterRange runs.

Setting a presentation attribute sets it on every run, so each run becomes a styled <tspan>.

Parameters:

ranges (sequence of CharacterRange) – The runs, in text order.

Examples

>>> import svgwriter as svg
>>> t = svg.text("Hello", x=0, y=20)
>>> t[[0, 1, 4]].fill = "red"
>>> str(t)
'<text x="0" y="20"><tspan fill="red">He</tspan>ll<tspan fill="red">o</tspan></text>'
>>> t[[True, False, True, False, True]].chars
'Hlo'
What the code above draws
fill: PaintValue
stroke: PaintValue
stroke_width: LengthLike
opacity: float
fill_opacity: float
stroke_opacity: float
font_family: str
font_size: LengthLike
font_weight: str | int
font_style: str
text_decoration: str
letter_spacing: LengthLike
baseline_shift: LengthLike | str
classmethod of(text: Text, positions: Sequence[int], bounds: Sequence[int]) → CharacterSelection[source]

The selection of character positions (cluster indices) of text.

property chars: str

The selected characters, in text order.

effect(name: str, /, **options: Any) → CharacterSelection[source]

Apply an effect from svg.effects to every run and return the selection.

ranges
class Font(*children: Child, horiz_adv_x: float = 0, horiz_origin_x: float | None = None, horiz_origin_y: float | None = None, vert_origin_x: float | None = None, vert_origin_y: float | None = None, vert_adv_y: float | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FontGlyphs, PresentationAttributes, ExternalResourcesAttribute, CoreAttributes, Container[Element]

<font>: an SVG font made of glyph outlines.

This is the SVG 1.1 element; to set text in TrueType/OpenType font files, use Typeface.

Parameters:
  • *children (Element) – Glyphs, kerning pairs and a font-face.

  • horiz_adv_x (float, optional) – Default horizontal advance of glyphs. Defaults to 0.

  • horiz_origin_x (float, optional) – Origin for horizontal text.

  • horiz_origin_y (float, optional) – Origin for horizontal text.

  • vert_origin_x (float, optional) – Origin for vertical text.

  • vert_origin_y (float, optional) – Origin for vertical text.

  • vert_adv_y (float, optional) – Default vertical advance of glyphs.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

FontFace

The font’s descriptors.

Glyph

One character outline.

Typeface

A font family of TrueType/OpenType faces.

Examples

>>> f = Font(horiz_adv_x=10)
>>> str(f)
'<font horiz-adv-x="10"/>'
category: ClassVar[Category] = 5
tag: ClassVar[str] = 'font'
class FontFace(*children: Child, font_family: str | None = None, font_style: str | None = None, font_variant: str | None = None, font_weight: str | None = None, font_stretch: str | None = None, font_size: str | None = None, unicode_range: str | None = None, units_per_em: float | None = None, ascent: float | None = None, descent: float | None = None, cap_height: float | None = None, x_height: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **descriptors: Any)[source]

Bases: CoreAttributes, Container[Element]

<font-face>: font descriptors, mirroring CSS @font-face.

Less common descriptors (panose_1, v_alphabetic, underline_position and so on) go through **descriptors.

Parameters:
  • *children (Element) – A font-face-src and similar.

  • font_family (str, optional) – The matching CSS descriptors.

  • font_style (str, optional) – The matching CSS descriptors.

  • font_variant (str, optional) – The matching CSS descriptors.

  • font_weight (str, optional) – The matching CSS descriptors.

  • font_stretch (str, optional) – The matching CSS descriptors.

  • font_size (str, optional) – The matching CSS descriptors.

  • unicode_range (str, optional) – Characters the face covers.

  • units_per_em (float, optional) – The em square in glyph units.

  • ascent (float, optional) – Vertical metrics in glyph units.

  • descent (float, optional) – Vertical metrics in glyph units.

  • cap_height (float, optional) – Vertical metrics in glyph units.

  • x_height (float, optional) – Vertical metrics in glyph units.

  • **descriptors – Further descriptors and core attributes in Python spelling.

See also

FontFaceSrc

Where the font data comes from.

Examples

>>> str(FontFace(font_family="X", units_per_em=1000))
'<font-face font-family="X" units-per-em="1000"/>'
category: ClassVar[Category] = 5
tag: ClassVar[str] = 'font-face'
class FontFaceFormat(string: str | None = None, *, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

<font-face-format>: the format of a font-face-uri.

Parameters:
  • string (str, optional) – The format hint, such as "svg".

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

FontFaceUri

The element it describes.

Examples

>>> str(FontFaceFormat("svg"))
'<font-face-format string="svg"/>'
category: ClassVar[Category] = 5
content: ClassVar[ContentModel] = 0
tag: ClassVar[str] = 'font-face-format'
class FontFaceName(name: str | None = None, *, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

<font-face-name>: a locally installed font by name.

Parameters:
  • name (str, optional) – The font’s name.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

FontFaceUri

An external font instead.

Examples

>>> str(FontFaceName("X"))
'<font-face-name name="X"/>'
category: ClassVar[Category] = 5
content: ClassVar[ContentModel] = 0
tag: ClassVar[str] = 'font-face-name'
class FontFaceSrc(*args: Child, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes, Container[Element]

<font-face-src>: where the font data comes from.

Parameters:
  • *children (Element) – FontFaceUri and FontFaceName entries, in order of preference.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

FontFace

The descriptor element that contains it.

Examples

>>> str(FontFaceSrc(FontFaceName("X")))
'<font-face-src><font-face-name name="X"/></font-face-src>'
category: ClassVar[Category] = 5
tag: ClassVar[str] = 'font-face-src'
class FontFaceUri(href: str | None = None, *formats: FontFaceFormat, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, CoreAttributes, Container[FontFaceFormat]

<font-face-uri>: an external font reference.

Parameters:
  • href (str, optional) – Location of the font.

  • *formats (FontFaceFormat) – Format hints.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

FontFaceName

A locally installed font instead.

Examples

>>> len(FontFaceUri("a.svg", FontFaceFormat("svg")))
1
category: ClassVar[Category] = 5
tag: ClassVar[str] = 'font-face-uri'
class Glyph(unicode: str | None = None, d: PathData | PathBuilder | str | None = None, *, glyph_name: str | None = None, orientation: str | None = None, arabic_form: str | None = None, lang: str | None = None, horiz_adv_x: float | None = None, vert_origin_x: float | None = None, vert_origin_y: float | None = None, vert_adv_y: float | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _GlyphBase

<glyph>: the outline for one character sequence.

Parameters:
  • unicode (str, optional) – The character sequence the glyph draws.

  • d (PathData or PathBuilder or str, optional) – The outline.

  • glyph_name (str, optional) – A name to refer to the glyph by.

  • orientation (str, optional) – "h" or "v" to restrict the glyph to one writing direction.

  • arabic_form (str, optional) – Contextual form for Arabic.

  • lang (str, optional) – Language the glyph applies to.

  • **kwargs – horiz_adv_x, vert_origin_x, vert_origin_y, vert_adv_y and other presentation, core and SVG attributes in Python spelling.

See also

MissingGlyph

The fallback glyph.

Examples

>>> str(Glyph("a", "M0 0L5 5", horiz_adv_x=6))
'<glyph d="M0 0 L5 5" horiz-adv-x="6" unicode="a"/>'
tag: ClassVar[str] = 'glyph'
class GlyphRef(href: str | Element | None = None, *, glyph_ref: str | None = None, format: str | None = None, x: float | None = None, y: float | None = None, dx: float | None = None, dy: float | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, PresentationAttributes, CoreAttributes

<glyphRef>: one glyph of an altGlyphDef.

Parameters:
  • href (str or Element, optional) – The glyph, or its IRI.

  • glyph_ref (str, optional) – Identifier of the glyph in the font.

  • format (str, optional) – Glyph format of the font.

  • x (float, optional) – Position and offset of the glyph.

  • y (float, optional) – Position and offset of the glyph.

  • dx (float, optional) – Position and offset of the glyph.

  • dy (float, optional) – Position and offset of the glyph.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

AltGlyphItem

Groups glyph references into one candidate set.

Examples

>>> GlyphRef("#g", x=1).get("x")
1
category: ClassVar[Category] = 11
content: ClassVar[ContentModel] = 0
tag: ClassVar[str] = 'glyphRef'
class HKern(k: float, *, u1: str | None = None, g1: str | None = None, u2: str | None = None, g2: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Kern

<hkern>: horizontal kerning pair.

Parameters:
  • k (float) – The adjustment applied between the pair.

  • u1 (str, optional) – Unicode characters or ranges of the first and second glyph.

  • u2 (str, optional) – Unicode characters or ranges of the first and second glyph.

  • g1 (str, optional) – Glyph names of the first and second glyph.

  • g2 (str, optional) – Glyph names of the first and second glyph.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

VKern

Vertical kerning.

Examples

>>> str(HKern(-3, u1="A", u2="V"))
'<hkern u1="A" u2="V" k="-3"/>'
tag: ClassVar[str] = 'hkern'
class MissingGlyph(*children: Child, d: PathData | PathBuilder | str | None = None, horiz_adv_x: float | None = None, vert_origin_x: float | None = None, vert_origin_y: float | None = None, vert_adv_y: float | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _GlyphBase

<missing-glyph>: drawn for characters without a glyph.

Parameters:
  • *children (Element) – Graphics, as an alternative to d.

  • d (PathData or PathBuilder or str, optional) – The outline.

  • horiz_adv_x (float, optional) – Horizontal advance.

  • vert_origin_x (float, optional) – Origin for vertical text.

  • vert_origin_y (float, optional) – Origin for vertical text.

  • vert_adv_y (float, optional) – Vertical advance.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Glyph

A glyph for a specific character.

Examples

>>> str(MissingGlyph(d="M0 0"))
'<missing-glyph d="M0 0"/>'
tag: ClassVar[str] = 'missing-glyph'
class TRef(href: str | Element | None = None, *, x: Coordinates | None = None, y: Coordinates | None = None, dx: Coordinates | None = None, dy: Coordinates | None = None, rotate: float | Sequence[float] | None = None, decoration: TextDecoration | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, PresentationAttributes, ConditionalAttributes, CoreAttributes, _TextPositioning

<tref>: the character data of a referenced element.

Parameters:
  • href (str or Element, optional) – The referenced element, or its IRI.

  • x (length or sequence of length, optional) – Absolute and relative positions.

  • y (length or sequence of length, optional) – Absolute and relative positions.

  • dx (length or sequence of length, optional) – Absolute and relative positions.

  • dy (length or sequence of length, optional) – Absolute and relative positions.

  • rotate (float or sequence of float, optional) – Per-character rotation in degrees.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

TSpan

A run with its own character data.

Examples

>>> TRef("#label", x=1).get("x")
1
category: ClassVar[Category] = 11
tag: ClassVar[str] = 'tref'
class TSpan(*args: Child, text_length: LengthLike | None = None, length_adjust: LengthAdjust | None = None, x: Coordinates | None = None, y: Coordinates | None = None, dx: Coordinates | None = None, dy: Coordinates | None = None, rotate: float | Sequence[float] | None = None, decoration: TextDecoration | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PresentationAttributes, ConditionalAttributes, CoreAttributes, _TextLength, _TextPositioning

<tspan>: a sub-run of text with its own position or style.

Parameters:
  • *args (str or Element) – Character data and nested spans.

  • x (length or sequence of length, optional) – Absolute and relative positions.

  • y (length or sequence of length, optional) – Absolute and relative positions.

  • dx (length or sequence of length, optional) – Absolute and relative positions.

  • dy (length or sequence of length, optional) – Absolute and relative positions.

  • rotate (float or sequence of float, optional) – Per-character rotation in degrees.

  • text_length (length, optional) – Target advance width of the run.

  • length_adjust ({"spacing", "spacingAndGlyphs"}, optional) – How text_length is met.

  • decoration (TextDecoration, optional) – Written as text-decoration.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Text.span

Append a span to a text.

Examples

>>> str(TSpan("a", dx=3, fill="red"))
'<tspan dx="3" fill="red">a</tspan>'
category: ClassVar[Category] = 11
content: ClassVar[ContentModel] = 3
tag: ClassVar[str] = 'tspan'
class Text(*args: Child, text_length: LengthLike | None = None, length_adjust: LengthAdjust | None = None, x: Coordinates | None = None, y: Coordinates | None = None, dx: Coordinates | None = None, dy: Coordinates | None = None, rotate: float | Sequence[float] | None = None, decoration: TextDecoration | str | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: TextAccess, PathOperand, GraphicsElement, _TextLength, _TextPositioning

<text>: a run of graphics text, laid out with the installed fonts.

The element holds character data and nested tspan, tref, textPath and altGlyph children. Characters can be addressed, moved and outlined individually.

Parameters:
  • *content (str or Element) – Text runs and tspan/tref/textPath/altGlyph children.

  • x (length or sequence of length, optional) – Absolute and relative positions; a list positions successive characters.

  • y (length or sequence of length, optional) – Absolute and relative positions; a list positions successive characters.

  • dx (length or sequence of length, optional) – Absolute and relative positions; a list positions successive characters.

  • dy (length or sequence of length, optional) – Absolute and relative positions; a list positions successive characters.

  • rotate (float or sequence of float, optional) – Per-character rotation in degrees.

  • text_length (length, optional) – Target advance width of the text.

  • length_adjust ({"spacing", "spacingAndGlyphs"}, optional) – How text_length is met.

  • decoration (TextDecoration, optional) – Written as text-decoration.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

TSpan

A styled or repositioned sub-run.

TextPath

Text laid out along a path.

Notes

Text takes part in boolean operations through its glyph outlines (Rect(0, 0, 100, 40) - Text("Hi", ...)). text[i] is a live Character for character i (len(text) counts characters); glyphs() returns one outline path per character. The method rotate() rotates the whole element; the per-character SVG rotate list is text.rotate_.

Examples

>>> str(Text("Hi", x=10, y=20, decoration=TextDecoration.UNDERLINE))
'<text x="10" y="20" text-decoration="underline">Hi</text>'
geometry_attributes: ClassVar[frozenset[str]] = frozenset({'dx', 'dy', 'font-family', 'font-size', 'font-style', 'font-weight', 'lengthAdjust', 'rotate', 'text-anchor', 'textLength', 'x', 'y'})

Attributes describing the geometry; not copied by to_path().

category: ClassVar[Category] = 11
content: ClassVar[ContentModel] = 3
tag: ClassVar[str] = 'text'
class TextDecoration(*values)[source]

Bases: Flag

The text-decoration keywords, combinable with |.

Pass a member (or a combination) as the decoration argument of Text, TSpan or AltGlyph.

See also

Text

The element that takes the decoration argument.

Examples

>>> d = TextDecoration.UNDERLINE | TextDecoration.LINE_THROUGH
>>> d.svg_value()
'underline line-through'
NONE = 0
UNDERLINE = 1
OVERLINE = 2
LINE_THROUGH = 4
svg_value(precision: int = 4) → str[source]

Format the flags as a text-decoration attribute value.

Parameters:

precision (int, optional) – Unused; accepted so every attribute value shares one signature. Defaults to the library’s default precision.

Returns:

str – The space-separated keywords, or "none" when no flag is set.

Examples

>>> TextDecoration.NONE.svg_value()
'none'
>>> TextDecoration.OVERLINE.svg_value()
'overline'
class TextPath(*content: Child, href: str | Element | None = None, start_offset: LengthLike | None = None, method: str | None = None, spacing: str | None = None, text_length: LengthLike | None = None, length_adjust: LengthAdjust | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, PresentationAttributes, ConditionalAttributes, CoreAttributes, _TextLength

<textPath>: text laid out along a path.

Parameters:
  • *content (str or Element) – Character data and spans.

  • href (str or Element, optional) – The path to follow, or its IRI.

  • start_offset (length, optional) – Distance along the path where the text starts.

  • method ({"align", "stretch"}, optional) – How glyphs are fitted to the path.

  • spacing ({"auto", "exact"}, optional) – How glyph spacing is computed.

  • text_length (length, optional) – Target advance width of the text.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Text

Text laid out on a straight baseline.

Examples

>>> TextPath("abc", href="#p", start_offset="50%").get("startOffset")
'50%'
category: ClassVar[Category] = 11
content: ClassVar[ContentModel] = 3
tag: ClassVar[str] = 'textPath'
class VKern(k: float, *, u1: str | None = None, g1: str | None = None, u2: str | None = None, g2: str | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Kern

<vkern>: vertical kerning pair.

Parameters:
  • k (float) – The adjustment applied between the pair.

  • u1 (str, optional) – Unicode characters or ranges of the first and second glyph.

  • u2 (str, optional) – Unicode characters or ranges of the first and second glyph.

  • g1 (str, optional) – Glyph names of the first and second glyph.

  • g2 (str, optional) – Glyph names of the first and second glyph.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

HKern

Horizontal kerning.

Examples

>>> str(VKern(2, u1="a", u2="b"))
'<vkern u1="a" u2="b" k="2"/>'
tag: ClassVar[str] = 'vkern'

Paint servers, clipping and markers

Paint servers, clipping and markers: gradients, stops, patterns, clipPath, mask and marker.

class ClipPath(*children: Child, clip_path_units: Units | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PathOperand, Transformable, PresentationAttributes, ConditionalAttributes, ExternalResourcesAttribute, CoreAttributes, GraphicsContainer

<clipPath>: shapes whose union is the clipping region.

Parameters:
  • *children (Element) – Shapes, text and use elements that define the region.

  • clip_path_units ({"userSpaceOnUse", "objectBoundingBox"}, optional) – Coordinate system of the content.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Mask

Soft, luminance-based masking.

Examples

>>> import svgwriter as svg
>>> str(ClipPath(svg.Rect(0, 0, 10, 10), id="c"))
'<clipPath id="c"><rect x="0" y="0" width="10" height="10"/></clipPath>'
local_bbox() → BBox | None[source]

Box around the clipping shapes.

Returns:

BBox or None – None when the clip path is empty.

Examples

>>> import svgwriter as svg
>>> ClipPath(svg.Rect(0, 0, 10, 20)).local_bbox().height
20.0
local_outline() → PathData[source]

Union of the children’s outlines.

Returns:

PathData – The clipping region as path data.

Examples

>>> import svgwriter as svg
>>> len(ClipPath(svg.Rect(0, 0, 10, 10)).local_outline()) > 0
True
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'clipPath'
class Gradient(*stops: Stop, gradient_units: Literal['userSpaceOnUse', 'objectBoundingBox'] | None = None, spread_method: Literal['pad', 'reflect', 'repeat'] | None = None, **kwargs: Any)[source]

Bases: GradientStops, Transformable, XLinkAttributes, ExternalResourcesAttribute, PresentationAttributes, CoreAttributes, Container[Stop]

Shared base of linear and radial gradients.

Holds the color stops and the attributes common to both kinds.

Parameters:
  • *stops (Stop) – Color stops.

  • gradient_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of the gradient vector. Defaults to None.

  • spread_method ({"pad", "reflect", "repeat"}, optional) – What happens beyond the ends of the vector. Defaults to None.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

LinearGradient

Gradient along a line.

RadialGradient

Gradient around a center.

Examples

>>> import svgwriter as svg
>>> grad = svg.LinearGradient(svg.Stop(0, "red"), svg.Stop(1, "blue"))
>>> isinstance(grad, svg.Gradient), len(grad)
(True, 2)
transform_attribute: ClassVar[str] = 'gradientTransform'
category: ClassVar[Category] = 6
class LinearGradient(*stops: Stop, x1: LengthLike | None = None, y1: LengthLike | None = None, x2: LengthLike | None = None, y2: LengthLike | None = None, gradient_units: Units | None = None, spread_method: SpreadMethod | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Gradient

<linearGradient>: a gradient along a line.

Parameters:
  • *stops (Stop) – Color stops.

  • x1 (length, optional) – The gradient vector.

  • y1 (length, optional) – The gradient vector.

  • x2 (length, optional) – The gradient vector.

  • y2 (length, optional) – The gradient vector.

  • gradient_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of the vector.

  • spread_method ({"pad", "reflect", "repeat"}, optional) – What happens beyond the ends of the vector.

  • **kwargs – transform, href and other presentation, core and SVG attributes in Python spelling.

See also

RadialGradient

Gradient around a center.

Examples

>>> str(LinearGradient(Stop(0, "red"), x2=1))
'<linearGradient x2="1"><stop offset="0" stop-color="red"/></linearGradient>'
local_bbox() → BBox | None[source]

Box spanned by the gradient vector.

Returns:

BBox or None – The unit box for objectBoundingBox units, otherwise the box around the vector’s end points.

Examples

>>> LinearGradient(x1=0, y1=0, x2=10, y2=5, gradient_units="userSpaceOnUse").local_bbox().width
10.0
classmethod horizontal(*colors: Color | str, **kwargs: Any) → Self[source]

Create an evenly spaced left-to-right gradient.

Parameters:
  • *colors (Color or str) – The stop colors.

  • **kwargs – Further attributes of the gradient.

Returns:

LinearGradient – A gradient from (0, 0) to (1, 0) in bounding-box units.

Examples

>>> LinearGradient.horizontal("red", "blue").get("x2")
1
classmethod vertical(*colors: Color | str, **kwargs: Any) → Self[source]

Create an evenly spaced top-to-bottom gradient.

Parameters:
  • *colors (Color or str) – The stop colors.

  • **kwargs – Further attributes of the gradient.

Returns:

LinearGradient – A gradient from (0, 0) to (0, 1) in bounding-box units.

Examples

>>> LinearGradient.vertical("red", "blue").get("y2")
1
tag: ClassVar[str] = 'linearGradient'
class Marker(*children: Child, ref_x: float = 0, ref_y: float = 0, marker_width: LengthLike | None = None, marker_height: LengthLike | None = None, marker_units: Literal['strokeWidth', 'userSpaceOnUse'] | None = None, orient: MarkerOrient | None = None, view_box: ViewBoxLike | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FitToViewBox, PresentationAttributes, ExternalResourcesAttribute, CoreAttributes, GraphicsContainer

<marker>: arrowheads and other symbols at path vertices.

Parameters:
  • *children (Element) – The marker graphics.

  • ref_x (float, optional) – The point aligned with the vertex. Defaults to 0.

  • ref_y (float, optional) – The point aligned with the vertex. Defaults to 0.

  • marker_width (length, optional) – Viewport size.

  • marker_height (length, optional) – Viewport size.

  • marker_units ({"strokeWidth", "userSpaceOnUse"}, optional) – Coordinate system of the size.

  • orient (float or "auto", optional) – Rotation; "auto" follows the path direction.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Path

A shape that can carry markers.

Examples

>>> import svgwriter as svg
>>> m = Marker(svg.Circle(0, 0, 2), ref_x=1, marker_width=4, marker_height=4, orient="auto")
>>> m.get("orient")
'auto'
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'marker'
class Mask(*children: Child, mask_units: Units | None = None, mask_content_units: Units | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Positioned, PresentationAttributes, ConditionalAttributes, ExternalResourcesAttribute, CoreAttributes, GraphicsContainer

<mask>: luminance of its content becomes the alpha mask.

Parameters:
  • *children (Element) – The graphics whose luminance defines the mask.

  • mask_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of x, y, width and height.

  • mask_content_units ({"userSpaceOnUse", "objectBoundingBox"}, optional) – Coordinate system of the content.

  • **kwargs – x, y, width, height and other presentation, core and SVG attributes in Python spelling.

See also

ClipPath

Hard-edged clipping region.

Examples

>>> import svgwriter as svg
>>> str(Mask(svg.Rect(0, 0, 10, 10), id="m", mask_units="userSpaceOnUse"))
'<mask id="m" maskUnits="userSpaceOnUse"><rect x="0" y="0" width="10" height="10"/></mask>'
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'mask'
class Pattern(*children: Child, pattern_units: Units | None = None, pattern_content_units: Units | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, view_box: ViewBoxLike | None = None, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PatternTiling, Transformable, Positioned, FitToViewBox, XLinkAttributes, ExternalResourcesAttribute, PresentationAttributes, ConditionalAttributes, CoreAttributes, GraphicsContainer

<pattern>: a tile of graphics used as paint.

Parameters:
  • *children (Element) – The graphics of one tile.

  • pattern_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of x, y, width and height.

  • pattern_content_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of the tile’s content.

  • **kwargs – x, y, width, height, view_box, transform and other presentation, core and SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> p = Pattern(svg.Circle(2, 2, 2), width=4, height=4, pattern_units="userSpaceOnUse")
>>> p.local_bbox().width
4.0
transform_attribute: ClassVar[str] = 'patternTransform'
local_bbox() → BBox | None[source]

Box of one tile from its x, y, width and height.

Returns:

BBox or None – The tile rectangle.

Examples

>>> Pattern(width=10, height=10, pattern_units="userSpaceOnUse").local_bbox().height
10.0
category: ClassVar[Category] = 12
tag: ClassVar[str] = 'pattern'
class RadialGradient(*stops: Stop, cx: LengthLike | None = None, cy: LengthLike | None = None, r: LengthLike | None = None, fx: LengthLike | None = None, fy: LengthLike | None = None, gradient_units: Units | None = None, spread_method: SpreadMethod | None = None, transform: TransformLike | None = None, anchor: Pivot | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: Gradient

<radialGradient>: a gradient around a center.

Parameters:
  • *stops (Stop) – Color stops.

  • cx (length, optional) – Center of the outer circle.

  • cy (length, optional) – Center of the outer circle.

  • r (length, optional) – Radius of the outer circle.

  • fx (length, optional) – Focal point.

  • fy (length, optional) – Focal point.

  • gradient_units ({"objectBoundingBox", "userSpaceOnUse"}, optional) – Coordinate system of the circle.

  • spread_method ({"pad", "reflect", "repeat"}, optional) – What happens beyond the circle.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

LinearGradient

Gradient along a line.

Examples

>>> str(RadialGradient(cx=0.5, cy=0.5, r=0.5))
'<radialGradient cx="0.5" cy="0.5" r="0.5"/>'
local_bbox() → BBox | None[source]

Box of the gradient’s outer circle.

Returns:

BBox or None – The unit box for objectBoundingBox units, otherwise the circle’s box.

Examples

>>> RadialGradient(cx=5, cy=5, r=2, gradient_units="userSpaceOnUse").local_bbox().width
4.0
tag: ClassVar[str] = 'radialGradient'
class Stop(offset: float | str = 0, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, *, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: PresentationAttributes, CoreAttributes

<stop>: one color stop of a gradient.

Parameters:
  • offset (float or str, optional) – Position along the gradient vector, 0 to 1 or a percentage. Defaults to 0.

  • stop_color (Color or str, optional) – The color at the offset.

  • stop_opacity (float, optional) – The opacity at the offset.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Gradient.add_stop

Create and append a stop in one call.

Examples

>>> str(Stop(0.5, "red", 0.5))
'<stop offset="0.5" stop-color="red" stop-opacity="0.5"/>'
category: ClassVar[Category] = 6
tag: ClassVar[str] = 'stop'

Filters

<filter> and every SVG 1.1 filter primitive.

class BlendMode(*values)[source]

Bases: Keyword

Blend modes of FeBlend.

Examples

>>> import svgwriter as svg
>>> svg.BlendMode.MULTIPLY.value
'multiply'
NORMAL = 'normal'
MULTIPLY = 'multiply'
SCREEN = 'screen'
DARKEN = 'darken'
LIGHTEN = 'lighten'
class Channel(*values)[source]

Bases: Keyword

Color channels selectable in FeDisplacementMap.

Examples

>>> import svgwriter as svg
>>> svg.Channel.RED.value
'R'
R = 'R'
G = 'G'
B = 'B'
A = 'A'
RED = 'R'
GREEN = 'G'
BLUE = 'B'
ALPHA = 'A'
class ColorMatrixType(*values)[source]

Bases: Keyword

Types of FeColorMatrix.

Examples

>>> import svgwriter as svg
>>> svg.ColorMatrixType.HUE_ROTATE.value
'hueRotate'
MATRIX = 'matrix'
SATURATE = 'saturate'
HUE_ROTATE = 'hueRotate'
LUMINANCE_TO_ALPHA = 'luminanceToAlpha'
class CompositeOperator(*values)[source]

Bases: Keyword

Operators of FeComposite.

Examples

>>> import svgwriter as svg
>>> svg.CompositeOperator.ATOP.value
'atop'
OVER = 'over'
IN = 'in'
OUT = 'out'
ATOP = 'atop'
XOR = 'xor'
ARITHMETIC = 'arithmetic'
class EdgeMode(*values)[source]

Bases: Keyword

Edge handling of FeConvolveMatrix.

Examples

>>> import svgwriter as svg
>>> svg.EdgeMode.WRAP.value
'wrap'
DUPLICATE = 'duplicate'
WRAP = 'wrap'
NONE = 'none'
class FeBlend(mode: BlendMode = BlendMode.NORMAL, *, in2: FilterInput | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: TwoInputs

<feBlend>: blend two inputs with a mode.

Parameters:
  • mode (BlendMode, optional) – How to blend. Defaults to BlendMode.NORMAL, which is omitted from the output.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeBlend(svg.BlendMode.MULTIPLY, in_="SourceGraphic", in2="shade"))
'<feBlend in="SourceGraphic" in2="shade" mode="multiply"/>'
tag: ClassVar[str] = 'feBlend'
class FeColorMatrix(type: ColorMatrixType = ColorMatrixType.MATRIX, values: Sequence[float] | float | None = None, *, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: ColorMatrixShortcuts, SingleInput

<feColorMatrix>: a 5x4 color transform or a shorthand.

Parameters:
  • type (ColorMatrixType, optional) – Kind of transform. Defaults to ColorMatrixType.MATRIX.

  • values (sequence of float or float, optional) – The 20 matrix entries for matrix, otherwise the single amount. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Raises:

ValueError – If a matrix is given other than 20 values.

See also

FeComponentTransfer

Per-channel remapping.

Examples

>>> import svgwriter as svg
>>> str(svg.FeColorMatrix.saturate(0))
'<feColorMatrix type="saturate" values="0"/>'
tag: ClassVar[str] = 'feColorMatrix'
class FeComponentTransfer(*functions: FeFunc, red: FeFunc | None = None, green: FeFunc | None = None, blue: FeFunc | None = None, alpha: FeFunc | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput, Container[FeFunc]

<feComponentTransfer>: per-channel remapping.

Parameters:
  • *functions (FeFunc) – Channel functions to add as children.

  • red (FeFunc, optional) – Functions for one channel each; they are converted to the matching feFuncR/G/B/A element. Default to None.

  • green (FeFunc, optional) – Functions for one channel each; they are converted to the matching feFuncR/G/B/A element. Default to None.

  • blue (FeFunc, optional) – Functions for one channel each; they are converted to the matching feFuncR/G/B/A element. Default to None.

  • alpha (FeFunc, optional) – Functions for one channel each; they are converted to the matching feFuncR/G/B/A element. Default to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

See also

FeColorMatrix

Mix channels with a matrix.

Examples

>>> import svgwriter as svg
>>> str(svg.FeComponentTransfer(red=svg.FeFuncR.linear(2)))
'<feComponentTransfer><feFuncR type="linear" slope="2" intercept="0"/></feComponentTransfer>'
tag: ClassVar[str] = 'feComponentTransfer'
class FeComposite(operator: CompositeOperator = CompositeOperator.OVER, *, k1: float | None = None, k2: float | None = None, k3: float | None = None, k4: float | None = None, in2: FilterInput | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: TwoInputs

<feComposite>: Porter-Duff compositing or arithmetic.

Parameters:
  • operator (CompositeOperator, optional) – The compositing operation. Defaults to CompositeOperator.OVER.

  • k1 (float, optional) – Coefficients of the arithmetic operator. Defaults to None.

  • k2 (float, optional) – Coefficients of the arithmetic operator. Defaults to None.

  • k3 (float, optional) – Coefficients of the arithmetic operator. Defaults to None.

  • k4 (float, optional) – Coefficients of the arithmetic operator. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeComposite(svg.CompositeOperator.IN))
'<feComposite operator="in"/>'
tag: ClassVar[str] = 'feComposite'
class FeConvolveMatrix(kernel_matrix: Sequence[float] = (), order: int | tuple[int, int] = 3, *, divisor: float | None = None, bias: float | None = None, target_x: int | None = None, target_y: int | None = None, edge_mode: EdgeMode | None = None, kernel_unit_length: float | tuple[float, float] | None = None, preserve_alpha: bool | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput

<feConvolveMatrix>: apply a convolution kernel.

Parameters:
  • kernel_matrix (sequence of float, optional) – The kernel, row by row. Defaults to empty.

  • order (int or tuple of int, optional) – Kernel size, or (columns, rows). Defaults to 3.

  • divisor (float, optional) – Normalisation divisor and added bias. Default to None.

  • bias (float, optional) – Normalisation divisor and added bias. Default to None.

  • target_x (int, optional) – Kernel cell aligned with the output pixel. Default to None.

  • target_y (int, optional) – Kernel cell aligned with the output pixel. Default to None.

  • edge_mode (EdgeMode, optional) – Handling of pixels beyond the edge. Defaults to None.

  • kernel_unit_length (float or tuple of float, optional) – Kernel scale. Defaults to None.

  • preserve_alpha (bool, optional) – Leave the alpha channel unconvolved. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Raises:

ValueError – If the kernel length does not match order.

Examples

>>> import svgwriter as svg
>>> str(svg.FeConvolveMatrix([0, 1, 0, 1, -4, 1, 0, 1, 0], divisor=1))
'<feConvolveMatrix order="3" kernelMatrix="0 1 0 1 -4 1 0 1 0" divisor="1"/>'
tag: ClassVar[str] = 'feConvolveMatrix'
class FeDiffuseLighting(*light: Child, diffuse_constant: float | None = None, surface_scale: float | None = None, kernel_unit_length: float | tuple[float, float] | None = None, lighting_color: Color | str | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Lighting

<feDiffuseLighting>: Lambertian lighting of the alpha bump map.

Parameters:
  • *light (LightSource) – The light source child.

  • diffuse_constant (float, optional) – Diffuse reflection constant. Defaults to None.

  • surface_scale (float, optional) – Height of the surface for full alpha. Defaults to None.

  • kernel_unit_length (float or tuple of float, optional) – Surface normal sampling distance. Defaults to None.

  • lighting_color (Color or str, optional) – Color of the light. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

See also

FeSpecularLighting

Specular highlights.

Examples

>>> import svgwriter as svg
>>> str(svg.FeDiffuseLighting(svg.FeDistantLight(45, 60), surface_scale=2))
'<feDiffuseLighting surfaceScale="2"><feDistantLight azimuth="45" elevation="60"/></feDiffuseLighting>'
tag: ClassVar[str] = 'feDiffuseLighting'
class FeDisplacementMap(scale: float = 0, x_channel_selector: Channel = Channel.A, y_channel_selector: Channel = Channel.A, *, in2: FilterInput | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: TwoInputs

<feDisplacementMap>: displace in by channels of in2.

Parameters:
  • scale (float, optional) – Displacement scale. Defaults to 0.

  • x_channel_selector (Channel, optional) – Channels of in2 driving each axis. Default to Channel.A.

  • y_channel_selector (Channel, optional) – Channels of in2 driving each axis. Default to Channel.A.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeDisplacementMap(5, svg.Channel.R, svg.Channel.G))
'<feDisplacementMap scale="5" xChannelSelector="R" yChannelSelector="G"/>'
tag: ClassVar[str] = 'feDisplacementMap'
class FeDistantLight(azimuth: float = 0, elevation: float = 0, *, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: LightSource

<feDistantLight>: light from infinitely far away.

Parameters:
  • azimuth (float, optional) – Direction angle in the xy plane, in degrees. Defaults to 0.

  • elevation (float, optional) – Angle above the xy plane, in degrees. Defaults to 0.

  • **kwargs – Core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeDistantLight(45, 60))
'<feDistantLight azimuth="45" elevation="60"/>'
property directional: bool

the direction is the same everywhere.

Returns:

bool – True.

Examples

>>> import svgwriter as svg
>>> svg.FeDistantLight(45, 60).directional
True
Type:

Always True

light_vector(x: float, y: float, z: float) → tuple[float, float, float][source]

The constant unit vector towards the light, independent of the point.

Parameters:
  • x (float) – The surface point; ignored, since the light is infinitely far away.

  • y (float) – The surface point; ignored, since the light is infinitely far away.

  • z (float) – The surface point; ignored, since the light is infinitely far away.

Returns:

tuple of float – Unit vector (x, y, z) pointing at the light.

Examples

>>> import svgwriter as svg
>>> svg.FeDistantLight(0, 0).light_vector(5, 5, 0)
(1.0, 0.0, 0.0)
tag: ClassVar[str] = 'feDistantLight'
class FeFlood(flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, *, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FilterPrimitive

<feFlood>: fill the sub-region with a color.

Parameters:
  • flood_color (Color or str, optional) – The fill color. Defaults to None.

  • flood_opacity (float, optional) – Opacity in 0..1. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeFlood("red", 0.5))
'<feFlood flood-color="red" flood-opacity="0.5"/>'
tag: ClassVar[str] = 'feFlood'
class FeFunc(type: TransferType = TransferType.IDENTITY, *, table_values: Sequence[float] | None = None, slope: float | None = None, intercept: float | None = None, amplitude: float | None = None, exponent: float | None = None, offset: float | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

Shared base of the four channel transfer functions.

Parameters:
  • type (TransferType, optional) – Kind of function. Defaults to TransferType.IDENTITY.

  • table_values (sequence of float, optional) – Lookup table for table and discrete. Defaults to None.

  • slope (float, optional) – Coefficients of linear. Default to None.

  • intercept (float, optional) – Coefficients of linear. Default to None.

  • amplitude (float, optional) – Coefficients of gamma. Default to None.

  • exponent (float, optional) – Coefficients of gamma. Default to None.

  • offset (float, optional) – Coefficients of gamma. Default to None.

  • **kwargs – Core and other SVG attributes in Python spelling.

See also

FeComponentTransfer

The primitive that holds these functions.

Examples

>>> import svgwriter as svg
>>> str(svg.FeFuncR.linear(2, 0.1))
'<feFuncR type="linear" slope="2" intercept="0.1"/>'
classmethod linear(slope: float, intercept: float = 0, **kwargs: Any) → Self[source]

Create a linear transfer function, slope * C + intercept.

Parameters:
  • slope (float) – Multiplier.

  • intercept (float, optional) – Added constant. Defaults to 0.

  • **kwargs – Core and other SVG attributes in Python spelling.

Returns:

FeFunc – The new function element.

Examples

>>> import svgwriter as svg
>>> str(svg.FeFuncA.linear(0.5))
'<feFuncA type="linear" slope="0.5" intercept="0"/>'
classmethod table(*values: float, **kwargs: Any) → Self[source]

Create a piecewise-linear transfer function from lookup values.

Parameters:
  • *values (float) – Table entries, evenly spaced over the input range.

  • **kwargs – Core and other SVG attributes in Python spelling.

Returns:

FeFunc – The new function element.

See also

evaluate

Apply the function to a value.

Examples

>>> import svgwriter as svg
>>> str(svg.FeFuncR.table(0, 1))
'<feFuncR type="table" tableValues="0 1"/>'
classmethod gamma(amplitude: float = 1, exponent: float = 1, offset: float = 0, **kwargs: Any) → Self[source]

Create a gamma transfer function, amplitude * C ** exponent + offset.

Parameters:
  • amplitude (float, optional) – Multiplier. Defaults to 1.

  • exponent (float, optional) – Power applied to the channel. Defaults to 1.

  • offset (float, optional) – Added constant. Defaults to 0.

  • **kwargs – Core and other SVG attributes in Python spelling.

Returns:

FeFunc – The new function element.

Examples

>>> import svgwriter as svg
>>> svg.FeFuncG.gamma(2, 3).evaluate(0.5)
0.25
evaluate(c: float) → float[source]

Apply this transfer function to a channel value.

Parameters:

c (float) – Channel value in 0..1.

Returns:

float – The transformed value.

Examples

>>> import svgwriter as svg
>>> svg.FeFuncR.linear(2, 0.1).evaluate(0.5)
1.1
content: ClassVar[ContentModel] = 2
class FeFuncA(type: TransferType = TransferType.IDENTITY, *, table_values: Sequence[float] | None = None, slope: float | None = None, intercept: float | None = None, amplitude: float | None = None, exponent: float | None = None, offset: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FeFunc

<feFuncA>: the transfer function of the alpha channel.

Takes the parameters of FeFunc.

Examples

>>> import svgwriter as svg
>>> svg.FeFuncA.tag
'feFuncA'
tag: ClassVar[str] = 'feFuncA'
class FeFuncB(type: TransferType = TransferType.IDENTITY, *, table_values: Sequence[float] | None = None, slope: float | None = None, intercept: float | None = None, amplitude: float | None = None, exponent: float | None = None, offset: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FeFunc

<feFuncB>: the transfer function of the blue channel.

Takes the parameters of FeFunc.

Examples

>>> import svgwriter as svg
>>> svg.FeFuncB.tag
'feFuncB'
tag: ClassVar[str] = 'feFuncB'
class FeFuncG(type: TransferType = TransferType.IDENTITY, *, table_values: Sequence[float] | None = None, slope: float | None = None, intercept: float | None = None, amplitude: float | None = None, exponent: float | None = None, offset: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FeFunc

<feFuncG>: the transfer function of the green channel.

Takes the parameters of FeFunc.

Examples

>>> import svgwriter as svg
>>> svg.FeFuncG.tag
'feFuncG'
tag: ClassVar[str] = 'feFuncG'
class FeFuncR(type: TransferType = TransferType.IDENTITY, *, table_values: Sequence[float] | None = None, slope: float | None = None, intercept: float | None = None, amplitude: float | None = None, exponent: float | None = None, offset: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FeFunc

<feFuncR>: the transfer function of the red channel.

Takes the parameters of FeFunc.

Examples

>>> import svgwriter as svg
>>> str(svg.FeFuncR.table(0, 1))
'<feFuncR type="table" tableValues="0 1"/>'
tag: ClassVar[str] = 'feFuncR'
class FeGaussianBlur(std_deviation: float | tuple[float, float] = 0, *, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput

<feGaussianBlur>: blur the input with a Gaussian.

Parameters:
  • std_deviation (float or tuple of float, optional) – Blur radius, or (x, y). Defaults to 0.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeGaussianBlur(std_deviation=2))
'<feGaussianBlur stdDeviation="2"/>'
tag: ClassVar[str] = 'feGaussianBlur'
class FeImage(href: str | None = None, *, preserve_aspect_ratio: PreserveAspectRatio | str | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, ExternalResourcesAttribute, FilterPrimitive

<feImage>: an external image or referenced element as input.

Parameters:
  • href (str, optional) – URL of the image or reference to an element. Defaults to None.

  • preserve_aspect_ratio (PreserveAspectRatio or str, optional) – How the image fits the sub-region. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeImage("texture.png"))
'<feImage xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="texture.png"/>'
tag: ClassVar[str] = 'feImage'
class FeMerge(*nodes: Child | FilterInput, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FilterPrimitive, Container[FeMergeNode]

<feMerge>: stack inputs with over, first at the bottom.

Parameters:
  • *nodes (FeMergeNode, FilterInput) – Layers, either nodes or anything accepted as an input, which is wrapped in a node.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeMerge(svg.FeOffset(id="shadow"), "SourceGraphic"))
'<feMerge><feMergeNode in="shadow-out"/><feMergeNode in="SourceGraphic"/></feMerge>'
tag: ClassVar[str] = 'feMerge'
class FeMergeNode(in_: FilterInput | None = None, *, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: CoreAttributes

<feMergeNode>: one layer of feMerge.

Parameters:
  • in (StandardInput, str or FilterPrimitive, optional) – The layer’s input. Defaults to None.

  • **kwargs – Core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeMergeNode("SourceGraphic"))
'<feMergeNode in="SourceGraphic"/>'
content: ClassVar[ContentModel] = 2
tag: ClassVar[str] = 'feMergeNode'
class FeMorphology(operator: MorphologyOperator = MorphologyOperator.ERODE, radius: float | tuple[float, float] = 0, *, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput

<feMorphology>: erode (thin) or dilate (fatten) the input.

Parameters:
  • operator (MorphologyOperator, optional) – erode or dilate. Defaults to MorphologyOperator.ERODE.

  • radius (float or tuple of float, optional) – Radius, or (x, y). Defaults to 0.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeMorphology(svg.MorphologyOperator.DILATE, 2))
'<feMorphology operator="dilate" radius="2"/>'
tag: ClassVar[str] = 'feMorphology'
class FeOffset(dx: float = 0, dy: float = 0, *, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput

<feOffset>: shift the input.

Parameters:
  • dx (float, optional) – Offset along each axis. Defaults to 0.

  • dy (float, optional) – Offset along each axis. Defaults to 0.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeOffset(dx=2, dy=3))
'<feOffset dx="2" dy="3"/>'
tag: ClassVar[str] = 'feOffset'
class FePointLight(x: float = 0, y: float = 0, z: float = 0, *, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: LightSource

<fePointLight>: an omnidirectional point light.

Parameters:
  • x (float, optional) – Light position. Defaults to 0.

  • y (float, optional) – Light position. Defaults to 0.

  • z (float, optional) – Light position. Defaults to 0.

  • **kwargs – Core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FePointLight(1, 2, 3))
'<fePointLight x="1" y="2" z="3"/>'
property directional: bool

the direction depends on the surface point.

Returns:

bool – False.

Examples

>>> import svgwriter as svg
>>> svg.FePointLight(1, 2, 3).directional
False
Type:

Always False

light_vector(x: float, y: float, z: float) → tuple[float, float, float][source]

The unit vector from the point towards the light position.

Parameters:
  • x (float) – The surface point.

  • y (float) – The surface point.

  • z (float) – The surface point.

Returns:

tuple of float – Unit vector (x, y, z) pointing at the light.

Examples

>>> import svgwriter as svg
>>> svg.FePointLight(0, 10, 0).light_vector(0, 0, 0)
(0.0, 1.0, 0.0)
tag: ClassVar[str] = 'fePointLight'
class FeSpecularLighting(*light: Child, specular_constant: float | None = None, specular_exponent: float | None = None, surface_scale: float | None = None, kernel_unit_length: float | tuple[float, float] | None = None, lighting_color: Color | str | None = None, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: _Lighting

<feSpecularLighting>: Phong specular highlights.

Parameters:
  • *light (LightSource) – The light source child.

  • specular_constant (float, optional) – Specular reflection constant. Defaults to None.

  • specular_exponent (float, optional) – Shininess exponent. Defaults to None.

  • surface_scale (float, optional) – Height of the surface for full alpha. Defaults to None.

  • kernel_unit_length (float or tuple of float, optional) – Surface normal sampling distance. Defaults to None.

  • lighting_color (Color or str, optional) – Color of the light. Defaults to None.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

See also

FeDiffuseLighting

Diffuse lighting.

Examples

>>> import svgwriter as svg
>>> str(svg.FeSpecularLighting(svg.FePointLight(1, 2, 3), specular_exponent=5))
'<feSpecularLighting specularExponent="5"><fePointLight x="1" y="2" z="3"/></feSpecularLighting>'
tag: ClassVar[str] = 'feSpecularLighting'
class FeSpotLight(x: float = 0, y: float = 0, z: float = 0, *, points_at_x: float = 0, points_at_y: float = 0, points_at_z: float = 0, specular_exponent: float | None = None, limiting_cone_angle: float | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FePointLight

<feSpotLight>: a point light aimed at pointsAt.

Parameters:
  • x (float, optional) – Light position. Defaults to 0.

  • y (float, optional) – Light position. Defaults to 0.

  • z (float, optional) – Light position. Defaults to 0.

  • points_at_x (float, optional) – The point the light aims at. Default to 0.

  • points_at_y (float, optional) – The point the light aims at. Default to 0.

  • points_at_z (float, optional) – The point the light aims at. Default to 0.

  • specular_exponent (float, optional) – Focus of the light beam. Defaults to None.

  • limiting_cone_angle (float, optional) – Half-angle of the cone in degrees. Defaults to None.

  • **kwargs – Core and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeSpotLight(1, 2, 3, points_at_x=4))
'<feSpotLight x="1" y="2" z="3" pointsAtX="4" pointsAtY="0" pointsAtZ="0"/>'
tag: ClassVar[str] = 'feSpotLight'
class FeTile(*args: Child, in_: FilterInput | None = None, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SingleInput

<feTile>: repeat the input’s sub-region.

Parameters:

**kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeTile(in_="cell"))
'<feTile in="cell"/>'
tag: ClassVar[str] = 'feTile'
class FeTurbulence(base_frequency: float | tuple[float, float] = 0, num_octaves: int = 1, *, seed: float | None = None, stitch_tiles: bool = False, type: TurbulenceType = TurbulenceType.TURBULENCE, result: str | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FilterPrimitive

<feTurbulence>: Perlin noise.

Parameters:
  • base_frequency (float or tuple of float, optional) – Noise frequency, or (x, y). Defaults to 0.

  • num_octaves (int, optional) – Number of noise layers. Defaults to 1.

  • seed (float, optional) – Random seed. Defaults to None.

  • stitch_tiles (bool, optional) – Stitch tile edges seamlessly. Defaults to False.

  • type (TurbulenceType, optional) – turbulence or fractalNoise. Defaults to TurbulenceType.TURBULENCE.

  • **kwargs – Sub-region (x, y, width, height), result, in_ and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeTurbulence(0.05, 2, seed=1))
'<feTurbulence baseFrequency="0.05" numOctaves="2" seed="1" type="turbulence"/>'
tag: ClassVar[str] = 'feTurbulence'
class Filter(*primitives: Child, filter_units: Units | None = None, primitive_units: Units | None = None, filter_res: float | tuple[float, float] | None = None, x: LengthLike | None = None, y: LengthLike | None = None, width: LengthLike | None = None, height: LengthLike | None = None, fill: PaintValue = None, fill_opacity: Opacity | None = None, fill_rule: FillRule | None = None, stroke: PaintValue = None, stroke_width: LengthLike | None = None, stroke_opacity: Opacity | None = None, stroke_linecap: LineCap | None = None, stroke_linejoin: LineJoin | None = None, stroke_miterlimit: float | None = None, stroke_dasharray: Sequence[float] | str | None = None, stroke_dashoffset: LengthLike | None = None, opacity: Opacity | None = None, display: str | None = None, visibility: str | None = None, color: str | None = None, clip_path: str | Element | None = None, mask: str | Element | None = None, filter: str | Element | None = None, font_family: str | None = None, font_size: LengthLike | None = None, font_weight: str | int | None = None, text_anchor: str | None = None, alignment_baseline: str | None = None, baseline_shift: LengthLike | str | None = None, clip: str | None = None, clip_rule: FillRule | None = None, color_interpolation: str | None = None, color_interpolation_filters: str | None = None, color_profile: str | None = None, color_rendering: str | None = None, direction: str | None = None, dominant_baseline: str | None = None, enable_background: str | None = None, flood_color: Color | str | None = None, flood_opacity: Opacity | None = None, font: Typeface | str | None = None, font_size_adjust: float | str | None = None, font_stretch: str | None = None, font_style: str | None = None, font_variant: str | None = None, glyph_orientation_horizontal: float | str | None = None, glyph_orientation_vertical: float | str | None = None, image_rendering: str | None = None, kerning: LengthLike | str | None = None, letter_spacing: LengthLike | str | None = None, lighting_color: Color | str | None = None, marker: str | Element | None = None, marker_end: str | Element | None = None, marker_mid: str | Element | None = None, marker_start: str | Element | None = None, overflow: str | None = None, pointer_events: str | None = None, shape_rendering: str | None = None, stop_color: Color | str | None = None, stop_opacity: Opacity | None = None, text_decoration: TextDecoration | str | None = None, text_rendering: str | None = None, unicode_bidi: str | None = None, word_spacing: LengthLike | str | None = None, writing_mode: str | None = None, class_: str | Sequence[str] | None = None, style: StyleLike | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, Positioned, PresentationAttributes, ExternalResourcesAttribute, CoreAttributes, Container[Element]

<filter>: a graph of filter primitives.

Parameters:
  • *primitives (FilterPrimitive) – The primitives, in evaluation order.

  • filter_units ({"userSpaceOnUse", "objectBoundingBox"}, optional) – Coordinate systems for the region and the primitives.

  • primitive_units ({"userSpaceOnUse", "objectBoundingBox"}, optional) – Coordinate systems for the region and the primitives.

  • filter_res (float or (float, float), optional) – Intermediate image resolution.

  • **kwargs – Region (x, y, width, height), href and presentation/core attributes in Python spelling.

See also

FilterPrimitive

Base of the elements a filter contains.

Examples

>>> import svgwriter as svg
>>> str(svg.Filter(svg.FeGaussianBlur(std_deviation=1), id="soft"))
'<filter id="soft"><feGaussianBlur stdDeviation="1"/></filter>'
collect() → Self[source]

Insert every detached primitive the filter’s primitives read, before its first reader.

Filter(merge) is then enough: the blur and offset that feed the merge come along in dependency order.

Returns:

Filter – This filter, for chaining.

Examples

>>> import svgwriter as svg
>>> blur = svg.FeGaussianBlur(3, in_="SourceAlpha")
>>> shadow = svg.FeOffset(2, 2, in_=blur)
>>> f = svg.Filter(svg.FeMerge(shadow, "SourceGraphic"))
>>> [p.tag for p in f.primitives]
['feGaussianBlur', 'feOffset', 'feMerge']
property primitives: list[FilterPrimitive]

The filter-primitive children in evaluation order.

Examples

>>> import svgwriter as svg
>>> svg.Filter(svg.FeOffset(), svg.FeTile()).primitives
[<FeOffset feOffset dx='0' dy='0'>, <FeTile feTile>]
category: ClassVar[Category] = 2
tag: ClassVar[str] = 'filter'
class FilterPrimitive(*args: Element | str, result: str | None = None, **kwargs: Any)[source]

Bases: Positioned, PresentationAttributes, CoreAttributes

Base of every fe* element: sub-region and result name.

Parameters:
  • *args (Element) – Initial children.

  • result (str, optional) – Name later primitives use to refer to this output. Defaults to None, which generates one on first access.

  • **kwargs – Sub-region (x, y, width, height), presentation, core and other SVG attributes in Python spelling.

See also

Filter

The container that holds primitives.

Examples

>>> import svgwriter as svg
>>> str(svg.FeOffset(result="shifted"))
'<feOffset result="shifted" dx="0" dy="0"/>'
property result: str

The result name, generated on first access if unset.

Examples

>>> import svgwriter as svg
>>> svg.FeOffset(id="shift").result
'shift-out'
sources() → list[FilterPrimitive][source]

The primitives this one reads, directly or through merge nodes, nearest first.

Returns:

list of FilterPrimitive – Every upstream primitive given as an input object (not by name).

Examples

>>> import svgwriter as svg
>>> blur = svg.FeGaussianBlur(2, in_="SourceAlpha")
>>> shifted = svg.FeOffset(2, 2, in_=blur)
>>> shifted.sources() == [blur]
True
category: ClassVar[Category] = 4
class LightSource(*args: Element | str, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, **kwargs: Any)[source]

Bases: CoreAttributes, ABC

Abstract light source; the child of a lighting primitive.

Parameters:

**kwargs – Core and other SVG attributes in Python spelling.

See also

FeDistantLight

Light from infinitely far away.

FePointLight

A point light.

Examples

>>> import svgwriter as svg
>>> svg.FeDistantLight(0, 0).directional
True
abstract property directional: bool

True when the light direction is the same everywhere.

Examples

>>> import svgwriter as svg
>>> svg.FePointLight().directional
False
abstractmethod light_vector(x: float, y: float, z: float) → tuple[float, float, float][source]

Compute the direction from a surface point towards the light.

Parameters:
  • x (float) – The surface point.

  • y (float) – The surface point.

  • z (float) – The surface point.

Returns:

tuple of float – Unit vector (x, y, z) pointing at the light.

Examples

>>> import svgwriter as svg
>>> svg.FePointLight(0, 0, 10).light_vector(0, 0, 0)
(0.0, 0.0, 1.0)
category: ClassVar[Category] = 8
content: ClassVar[ContentModel] = 2
class MorphologyOperator(*values)[source]

Bases: Keyword

Operators of FeMorphology.

Examples

>>> import svgwriter as svg
>>> svg.MorphologyOperator.DILATE.value
'dilate'
ERODE = 'erode'
DILATE = 'dilate'
class SingleInput(*args: Element | str, in_: StandardInput | str | FilterPrimitive | None = None, **kwargs: Any)[source]

Bases: FilterPrimitive

Primitives that read one input (in).

Parameters:
  • *args (Element) – Initial children.

  • in (StandardInput, str or FilterPrimitive, optional) – The input: a keyword, a result name or the primitive producing it. Defaults to None, meaning the previous primitive’s result.

  • **kwargs – result, sub-region and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeOffset(in_="SourceAlpha"))
'<feOffset in="SourceAlpha" dx="0" dy="0"/>'
class StandardInput(*values)[source]

Bases: Keyword

Keywords accepted by in and in2.

Examples

>>> import svgwriter as svg
>>> svg.StandardInput.SOURCE_ALPHA.value
'SourceAlpha'
SOURCE_GRAPHIC = 'SourceGraphic'
SOURCE_ALPHA = 'SourceAlpha'
BACKGROUND_IMAGE = 'BackgroundImage'
BACKGROUND_ALPHA = 'BackgroundAlpha'
FILL_PAINT = 'FillPaint'
STROKE_PAINT = 'StrokePaint'
class TransferType(*values)[source]

Bases: Keyword

Transfer function types of FeFunc.

Examples

>>> import svgwriter as svg
>>> svg.TransferType.GAMMA.value
'gamma'
IDENTITY = 'identity'
TABLE = 'table'
DISCRETE = 'discrete'
LINEAR = 'linear'
GAMMA = 'gamma'
class TurbulenceType(*values)[source]

Bases: Keyword

Noise types of FeTurbulence.

Examples

>>> import svgwriter as svg
>>> svg.TurbulenceType.FRACTAL_NOISE.value
'fractalNoise'
FRACTAL_NOISE = 'fractalNoise'
TURBULENCE = 'turbulence'
class TwoInputs(*args: Element | str, in2: StandardInput | str | FilterPrimitive | None = None, **kwargs: Any)[source]

Bases: SingleInput

Primitives that combine in and in2.

Parameters:
  • *args (Element) – Initial children.

  • in2 (StandardInput, str or FilterPrimitive, optional) – The second input. Defaults to None.

  • **kwargs – in_, result, sub-region and other SVG attributes in Python spelling.

Examples

>>> import svgwriter as svg
>>> str(svg.FeBlend(in_="SourceGraphic", in2="shade"))
'<feBlend in="SourceGraphic" in2="shade"/>'
resolve_input(value: StandardInput | str | FilterPrimitive | None) → StandardInput | str | None[source]

Turn a primitive into the name of its result.

Referring to a primitive by element lets its result name be generated on demand; keywords and strings pass through unchanged.

Parameters:

value (StandardInput, str, FilterPrimitive or None) – An input keyword, a result name or the primitive producing the input.

Returns:

StandardInput, str or None – The result name for a primitive, otherwise value itself.

Examples

>>> import svgwriter as svg
>>> svg.resolve_input(svg.FeOffset(id="shift"))
'shift-out'
>>> svg.resolve_input("SourceAlpha")
'SourceAlpha'

Animation

SMIL animation elements (animate, set, animateMotion, mpath, animateColor, animateTransform) and their timing curve, Ease.

class Accumulate(*values)[source]

Bases: Keyword

The accumulate attribute: whether repeats build on the previous end value.

See also

Additive

Controls combination with the underlying value.

Examples

>>> Accumulate.NONE.value
'none'
NONE = 'none'
SUM = 'sum'
class Additive(*values)[source]

Bases: Keyword

The additive attribute: replace the underlying value or add to it.

See also

Accumulate

Repeat-iteration counterpart.

Examples

>>> Additive.SUM.value
'sum'
REPLACE = 'replace'
SUM = 'sum'
class Animate(*args: Child, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, values: Sequence[Any] | str | None = None, from_: Any = None, to: Any = None, by: Any = None, calc_mode: CalcMode | None = None, key_times: Sequence[float] | str | None = None, key_splines: Sequence[tuple[float, float, float, float]] | str | None = None, additive: Additive | None = None, accumulate: Accumulate | None = None, ease: EaseLike | None = None, attribute_name: str | None = None, attribute_type: Literal['CSS', 'XML', 'auto'] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: FadeShortcut, AnimationElement, _ValueAnimation, _AttributeTarget

<animate>: interpolate one attribute over time.

Parameters:
  • attribute_name (str, optional) – The attribute to animate.

  • attribute_type ({"CSS", "XML", "auto"}, optional) – Namespace of the attribute.

  • values (sequence or str, optional) – The values to pass through.

  • from (object, optional) – Start value (written from).

  • to (object, optional) – End value.

  • by (object, optional) – Relative change.

  • calc_mode (CalcMode, optional) – Interpolation mode.

  • key_times (sequence of float or str, optional) – Times of each value, 0 to 1.

  • key_splines (sequence of 4-tuples or str, optional) – Bezier control points for spline mode.

  • additive (Additive, optional) – Combination with the underlying value.

  • accumulate (Accumulate, optional) – Whether repeats accumulate.

  • ease (str, tuple, callable or Ease, optional) – Timing curve over the whole timeline (see Ease); sets calcMode, keyTimes and keySplines (or samples values), so leave calc_mode unset or "linear"/"discrete".

  • **kwargs – Timing (begin, dur, repeat_count and so on, see AnimationElement), core and other SVG attributes.

See also

Set

Assign a value instead of interpolating.

AnimateTransform

Animate the transform attribute.

Examples

>>> str(Animate.fade(1, 0, dur=2))
'<animate attributeName="opacity" from="1" to="0" dur="2s"/>'
>>> str(Animate(attribute_name="x", from_=0, to=90, dur=1, ease="ease-in-out"))
'<animate attributeName="x" from="0" to="90" calcMode="spline" keyTimes="0;1" keySplines="0.42 0 0.58 1" dur="1s"/>'
tag: ClassVar[str] = 'animate'
class AnimateColor(*args: Child, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, values: Sequence[Any] | str | None = None, from_: Any = None, to: Any = None, by: Any = None, calc_mode: CalcMode | None = None, key_times: Sequence[float] | str | None = None, key_splines: Sequence[tuple[float, float, float, float]] | str | None = None, additive: Additive | None = None, accumulate: Accumulate | None = None, ease: EaseLike | None = None, attribute_name: str | None = None, attribute_type: Literal['CSS', 'XML', 'auto'] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: AnimationElement, _ValueAnimation, _AttributeTarget

<animateColor>: color interpolation (deprecated in later SVG).

Takes the same parameters as Animate.

See also

Animate

Preferred, as it also interpolates colors.

Examples

>>> str(AnimateColor(attribute_name="fill", from_="red", to="blue"))
'<animateColor attributeName="fill" from="red" to="blue"/>'
tag: ClassVar[str] = 'animateColor'
class AnimateMotion(*children: Child, path: PathData | PathBuilder | str | None = None, key_points: Sequence[float] | str | None = None, rotate: float | Literal['auto', 'auto-reverse'] | None = None, origin: str | None = None, ease: EaseLike | None = None, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, values: Sequence[Any] | str | None = None, from_: Any = None, to: Any = None, by: Any = None, calc_mode: CalcMode | None = None, key_times: Sequence[float] | str | None = None, key_splines: Sequence[tuple[float, float, float, float]] | str | None = None, additive: Additive | None = None, accumulate: Accumulate | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: AnimationElement, _ValueAnimation

<animateMotion>: move an element along a path.

Parameters:
  • *children (MPath) – An optional mpath naming the path by reference.

  • path (PathData or PathBuilder or str, optional) – The motion path.

  • key_points (sequence of float or str, optional) – Progress along the path at each key_times entry.

  • rotate (float or {"auto", "auto-reverse"}, optional) – Orientation along the path.

  • origin (str, optional) – The motion origin.

  • ease (str, tuple, callable or Ease, optional) – Timing curve along the path (or through values); see Ease. Along a path it is written with keyPoints.

  • **kwargs – Timing (see AnimationElement), values attributes, core and other SVG attributes.

See also

MPath

Reference a path instead of inlining it.

Examples

>>> str(AnimateMotion(path="M0 0L10 10", dur=2, rotate="auto"))
'<animateMotion dur="2s" path="M0 0 L10 10" rotate="auto"/>'
content: ClassVar[ContentModel] = 2
tag: ClassVar[str] = 'animateMotion'
class AnimateTransform(type: TransformType = TransformType.TRANSLATE, *, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, values: Sequence[Any] | str | None = None, from_: Any = None, to: Any = None, by: Any = None, calc_mode: CalcMode | None = None, key_times: Sequence[float] | str | None = None, key_splines: Sequence[tuple[float, float, float, float]] | str | None = None, additive: Additive | None = None, accumulate: Accumulate | None = None, ease: EaseLike | None = None, attribute_name: str | None = None, attribute_type: Literal['CSS', 'XML', 'auto'] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: SpinShortcut, AnimationElement, _ValueAnimation, _AttributeTarget

<animateTransform>: animate the transform attribute.

Parameters:
  • type (TransformType, optional) – Kind of transform. Defaults to TransformType.TRANSLATE.

  • **kwargs – Everything Animate takes; attribute_name defaults to "transform".

See also

Animate

Animate any other attribute.

Examples

>>> AnimateTransform(TransformType.SCALE, from_=1, to=2, dur=1).get("type")
<TransformType.SCALE: 'scale'>
tag: ClassVar[str] = 'animateTransform'
class AnimationElement(*args: Element | str, begin: str | float | None = None, dur: str | float | None = None, end: str | float | None = None, min: str | float | None = None, max: str | float | None = None, restart: Restart = Restart.ALWAYS, repeat_count: float | Literal['indefinite'] | None = None, repeat_dur: str | float | None = None, fill: AnimationFill | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, ConditionalAttributes, ExternalResourcesAttribute, CoreAttributes

Timing attributes shared by every animation element.

Numeric clock values are written in seconds; float("inf") for repeat_count is written as indefinite.

Parameters:
  • *args (Element) – Child elements.

  • begin (float or str, optional) – Clock values: seconds as a number or any SMIL timing string.

  • dur (float or str, optional) – Clock values: seconds as a number or any SMIL timing string.

  • end (float or str, optional) – Clock values: seconds as a number or any SMIL timing string.

  • min (float or str, optional) – Clock values: seconds as a number or any SMIL timing string.

  • max (float or str, optional) – Clock values: seconds as a number or any SMIL timing string.

  • restart (Restart, optional) – When the animation may restart. Defaults to Restart.ALWAYS, which is not written.

  • repeat_count (float or "indefinite", optional) – Number of repeats.

  • repeat_dur (float or str, optional) – Total duration of the repeats.

  • fill (AnimationFill, optional) – State after the animation ends.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

Animate

Interpolate an attribute.

Set

Assign a value.

Examples

>>> str(Set("fill", "red", begin=1, dur=2))
'<set attributeName="fill" begin="1s" dur="2s" to="red"/>'
category: ClassVar[Category] = 1
class AnimationFill(*values)[source]

Bases: Keyword

The animation fill attribute: the state after the active duration (not paint).

See also

AnimationElement

Takes fill as an argument.

Examples

>>> AnimationFill.FREEZE.value
'freeze'
FREEZE = 'freeze'
REMOVE = 'remove'
class CalcMode(*values)[source]

Bases: Keyword

The calcMode attribute: how values are interpolated.

See also

Animate

Takes calc_mode as an argument.

Examples

>>> CalcMode.SPLINE.value
'spline'
DISCRETE = 'discrete'
LINEAR = 'linear'
PACED = 'paced'
SPLINE = 'spline'
class Ease(curve: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] = 'linear')[source]

Bases: object

A timing curve: how an animation’s progress runs over its duration.

Every animation element and every element.animate preset takes one as ease=, in any of the forms below, and the curve applies to the whole timeline: with several values the motion through all of them follows the one curve (keyTimes are moved accordingly). Bézier curves are written as SMIL calcMode="spline" with exact keySplines; steps as calcMode="discrete"; a Python function is sampled into values with keyTimes at 24 to 60 evenly spaced times, more the more the curve bends, and played linearly between them.

Parameters:

curve (str, tuple, callable or Ease, optional) –

One of

  • a name: "linear" (the default), "ease", "ease-in", "ease-out", "ease-in-out" (the standard cubic-bézier curves), "step-start" or "step-end";

  • (x1, y1, x2, y2): a cubic bézier from (0, 0) to (1, 1) with 0 <= x1, x2 <= 1; y beyond 0..1 overshoots (and is then sampled, since SMIL splines stay inside the unit square);

  • ("steps", n) or ("steps", n, "start"): n equal jumps, at the end (default) or the start of each interval;

  • a function from time 0..1 to progress, normally 0..1 but free to overshoot (f(0) should be 0 and f(1) 1);

  • another Ease.

Raises:
  • ValueError – For an unknown name, a bézier x outside 0..1 or a step count below 1.

  • TypeError – For anything else.

See also

Animate

Takes ease.

Examples

>>> import svgwriter as svg
>>> svg.Ease("ease-in")(0.5) < 0.5
True
>>> slide = svg.Animate(attribute_name="x", from_=0, to=100, dur=1, ease="ease-out")
>>> slide.get("keySplines"), slide.get("calcMode")
('0 0 0.58 1', 'spline')
>>> svg.Ease(("steps", 4))(0.6)
0.5
>>> svg.Ease(lambda t: t * t)(0.5)
0.25

Notes

Progress at time x along a bézier is found by solving x(s) = x for the curve parameter with Newton’s method, falling back to bisection, the same way the SMIL evaluator behind doc.at plays keySplines.

property linear: bool

Whether the curve is the identity, so it changes nothing.

time_of(progress: float) → float[source]

The earliest time (0..1) at which the curve reaches progress.

Parameters:

progress (float) – Target progress.

Returns:

float – The time, or 1.0 when the curve never gets there.

Examples

>>> round(Ease("ease-in").time_of(0.5), 3)
0.657
smil(values: Sequence[str], key_times: Sequence[float] | None = None, *, discrete: bool = False) → dict[str, Any][source]

SMIL timing attributes that play values along this curve over the whole timeline.

Parameters:
  • values (sequence of str) – The animation’s values.

  • key_times (sequence of float, optional) – Their times, evenly spread when omitted.

  • discrete (bool, optional) – Whether the animation jumps between values (calcMode="discrete"). Defaults to False.

Returns:

dict – Some of values, key_times, key_splines and calc_mode, in Python spelling; empty for a linear curve.

Examples

>>> Ease("ease-in").smil(["0", "10", "20"])["key_times"]
[0.0, 0.6575, 1.0]
class MPath(href: str | Element | None = None, *, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: XLinkAttributes, ExternalResourcesAttribute, CoreAttributes

<mpath>: the motion path of animateMotion by reference.

Parameters:
  • href (str or Element, optional) – The path element, or its IRI.

  • **kwargs – Presentation, core and other SVG attributes in Python spelling.

See also

AnimateMotion

The element that contains it.

Examples

>>> import svgwriter as svg
>>> p = svg.Path("M0 0L10 10", id="track")
>>> 'href="#track"' in str(MPath(p))
True
category: ClassVar[Category] = 1
tag: ClassVar[str] = 'mpath'
class Restart(*values)[source]

Bases: Keyword

The restart attribute: when an animation may begin again.

See also

AnimationElement

Takes restart as an argument.

Examples

>>> Restart.NEVER.value
'never'
ALWAYS = 'always'
WHEN_NOT_ACTIVE = 'whenNotActive'
NEVER = 'never'
class Set(attribute_name: str | None = None, to: Any = None, *, begin: Clock | None = None, dur: Clock | None = None, end: Clock | None = None, min: Clock | None = None, max: Clock | None = None, restart: Restart | None = None, repeat_count: RepeatCount | None = None, repeat_dur: Clock | None = None, fill: AnimationFill | None = None, attribute_type: Literal['CSS', 'XML', 'auto'] | None = None, href: str | Element | None = None, xlink_title: str | None = None, xlink_show: str | None = None, xlink_actuate: str | None = None, xlink_role: str | None = None, xlink_arcrole: str | None = None, required_features: str | Sequence[str] | None = None, required_extensions: str | Sequence[str] | None = None, system_language: str | Sequence[str] | None = None, external_resources_required: bool | None = None, xml_base: str | None = None, xml_lang: str | None = None, xml_space: XmlSpace | None = None, id: str | None = None, attrs: Mapping[str, AttrValue] | None = None, **kwargs: Any)[source]

Bases: AnimationElement, _AttributeTarget

<set>: assign a value for a duration.

Parameters:
  • attribute_name (str, optional) – The attribute to set.

  • to (object, optional) – The value to assign.

  • **kwargs – Timing (see AnimationElement), core and other SVG attributes.

See also

Animate

Interpolate instead of assigning.

Examples

>>> str(Set("visibility", "hidden", begin=2))
'<set attributeName="visibility" begin="2s" to="hidden"/>'
tag: ClassVar[str] = 'set'
class TransformType(*values)[source]

Bases: Keyword

The type of an animateTransform.

See also

AnimateTransform

Takes the type as its first argument.

Examples

>>> TransformType.SKEW_X.value
'skewX'
TRANSLATE = 'translate'
SCALE = 'scale'
ROTATE = 'rotate'
SKEW_X = 'skewX'
SKEW_Y = 'skewY'

Animation presets

element.animate returns a Motion; its methods add SMIL children and chain. @svg.preset.animation adds presets of your own as more methods, and Motion.apply runs any animation function with typed options (see Preset functions).

class Motion(element: Element)[source]

Bases: TransformPresets

Animation presets for one element; every method adds SMIL children and returns self to chain.

Reach it as element.animate. The SMIL plays in browsers, and svgwriter.Document.at(), video export and animated GIF/APNG/WebP play it too. Each preset takes dur (seconds), begin (seconds), repeat (a count, or "indefinite"), freeze (keep the end state; default True) and ease, a timing curve over the whole preset (a name such as "ease-in-out", a bézier 4-tuple, ("steps", n) or a function; see Ease). show and hide act at one instant, so they take only the moment. Presets added with @svg.preset.animation are methods too, and so are behaviors (@svg.preset.behavior), which attach to the scene owning the element.

Parameters:

element (Element) – What to animate.

Examples

>>> import svgwriter as svg
>>> dot = svg.circle(10, 10, 5, fill="red")
>>> _ = dot.animate.fade_in(0.5).spin(2, begin=0.5)
>>> [child.tag for child in dot.children]
['animate', 'animateTransform']
>>> _ = dot.animate.move_to(80, 10, ease="ease-in-out")
>>> dot.children[-1].get("keySplines")
'0.42 0 0.58 1'
What the code above draws
element
on(event: str, source: Element | None = None) → Triggered[source]

The presets started by a pointer event instead of the clock: el.animate.on("click").pulse().

Each preset’s begin becomes a delay after the event. SVG in browsers and viewers plays the animation when the event happens, and can replay it; still images, PDF and video show the resting state.

Parameters:
  • event (str) – "click", "hover", "leave", "press", "release", "focus" or "blur".

  • source (Element, optional) – The element the event happens on, such as a button that animates something else. Defaults to this element.

Returns:

Triggered – The presets, each returning the same object to chain.

Raises:

ValueError – If event is not a known event name.

Examples

>>> import svgwriter as svg
>>> doc = svg.Document(width=80, height=30)
>>> button = doc.rect(0, 0, 30, 20, id="go")
>>> ball = doc.circle(50, 10, 5)
>>> _ = ball.animate.on("click", button).move_to(70, 10, begin=0.2)
>>> str(ball.children[-1].get("begin"))
'go.click+0.2s'
What the code above draws
apply(function: ~collections.abc.Callable[[~typing.Concatenate[~svgwriter.model.elements._motion.Motion, ~P]], ~svgwriter.model.elements._motion.Motion | None], *args: ~typing.~P, **kwargs: ~typing.~P) → Motion[source]

Run an animation function on this element: function(self, *args, **kwargs).

Typed with the function’s own parameters, so editors complete and check its options. The function need not be registered; @svg.preset.animation also makes it element.animate.<name>.

Parameters:
  • function (callable) – function(motion, *, option=default, ...) returning the motion or None.

  • *args – Passed on to function.

  • **kwargs – Passed on to function.

Returns:

Motion – This motion, to chain further presets.

Examples

>>> import svgwriter as svg
>>> def lift(motion: svg.Motion, *, height: float = 8) -> svg.Motion:
...     return motion.move_by(0, -height)
>>> dot = svg.circle(0, 0, 2)
>>> dot.animate.apply(lift, height=3).fade_in().element is dot
True
What the code above draws
tween(attribute: str, start: Any, end: Any, dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Change any attribute from start to end (numbers, lengths, colors, lists).

show(at: float = 0.0) → Motion[source]

Hidden until at seconds, then shown at once.

hide(at: float = 0.0) → Motion[source]

Shown until at seconds, then hidden at once.

fade_in(dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Opacity from 0 to 1.

fade_out(dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Opacity from 1 to 0.

draw(dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Draw the stroke on from its start, as if by a pen (dash offset from full length to 0).

create(dur: float = 1.0, *, begin: float = 0.0, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Draw the outline on, then fade the fill in; a group creates every child at once.

A shape without a stroke is drawn with its fill color. Every contour starts drawing together, at the same pen speed. ease shapes the drawing and the fade each.

uncreate(dur: float = 1.0, *, begin: float = 0.0, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

The reverse of create(): fade the fill out, then draw the outline off.

write(dur: float = 1.0, *, begin: float = 0.0, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Create a <text> character by character (a group child by child), each starting before the last ends.

ease spaces the starts along the curve and shapes each character’s drawing.

wipe(side: str = 'left', dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Reveal the element from one side (left, right, top, bottom) with a growing clip.

typewriter(dur: float = 1.0, *, begin: float = 0.0, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Show a <text> one character at a time over dur seconds (ease spaces the characters).

follow(path: Any, dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, rotate: bool = True, freeze: bool = True, ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Travel along path (an element or path data), turning with it when rotate.

Switch fully off and on again, by default forever.

color_cycle(colors: list[Any], dur: float = 1.0, *, attribute: str = 'fill', begin: float = 0.0, repeat: float | str | None = 'indefinite', ease: Ease | str | tuple[float, float, float, float] | tuple[Any, ...] | Callable[[float], float] | None = None) → Motion[source]

Blend through colors (and back to the first), by default forever.

morph(target: Element, dur: float = 1.0, *, begin: float = 0.0, repeat: float | str | None = None, freeze: bool = True, ease: EaseLike | None = None) → Motion[source]

Turn this <path>’s outline into target’s, matching their segments first.

Both outlines are brought to the same commands and segment count, so the d attribute can be interpolated; the path’s own d is rewritten to the matched start shape, which looks the same.

Parameters:
  • target (Element) – Any shape with an outline (<rect>, <circle>, <path>…).

  • dur (float, optional) – Seconds the morph takes. Defaults to 1.0.

  • begin (float, optional) – Seconds before it starts. Defaults to 0.0.

  • repeat (float or str, optional) – repeatCount: a number or "indefinite". Defaults to None (once).

  • freeze (bool, optional) – Keep the target shape after the end. Defaults to True.

  • ease (str or callable, optional) – Easing name or curve. Defaults to None (linear).

Returns:

Motion – This object, for chaining.

Raises:

TypeError – If the element is not a <path>; convert it with to_path() first.

See also

svgwriter.interpolate

The segment matching and blending used here.

Examples

>>> import svgwriter as svg
>>> shape = svg.rect(0, 0, 20, 20).to_path()
>>> _ = shape.animate.morph(svg.circle(10, 10, 10), 2)
>>> anim = shape.find(svg.Animate)
>>> anim.get("attributeName"), anim.get("dur")
('d', '2s')
>>> svg.circle(0, 0, 1).animate.morph(shape)
Traceback (most recent call last):
...
TypeError: morph animates a <path>'s d; convert first with element.to_path()
stagger(preset: str, step: float = 0.2, **options: Any) → Motion[source]

Apply preset (a method name) to each child element, each starting step seconds after the last.