"""
``symbol`` and ``switch``.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from ...pathdata.data import BBox, PathData
from ...units.lengths import LengthLike
from .._behavior import (
CoreAttributes,
ExternalResourcesAttribute,
FitToViewBox,
GraphicsElement,
PathOperand,
PresentationAttributes,
children_bbox,
union_outline,
)
from ..core import Category, Child, Element, GraphicsContainer
# region generated by `python scripts/dev.py svgwriter signatures write`: names used by the signatures
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from ...._internal._typing import FillRule, LineCap, LineJoin, Opacity
from ....ops.typeface import Typeface
from ...attributes.style import StyleLike
from ...attributes.viewbox import PreserveAspectRatio
from ...color.color import Color
from ...color.paint import PaintValue
from ...transform.transform import TransformLike
from .._behavior import Pivot, ViewBoxLike, XmlSpace
from ..core import AttrValue
from ..text import TextDecoration
# endregion
__all__ = ["Switch", "Symbol"]
[docs]
class Symbol(
FitToViewBox,
ExternalResourcesAttribute,
PresentationAttributes,
CoreAttributes,
GraphicsContainer,
tag="symbol",
category=Category.STRUCTURAL,
):
"""``<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>'
"""
# region generated by `python scripts/dev.py svgwriter signatures write`
def __init__(
self,
*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,
) -> None:
"""Create a ``<symbol>`` element.
Parameters
----------
*args, view_box, preserve_aspect_ratio
See the class docstring.
fill, fill_opacity, fill_rule, stroke, stroke_width, stroke_opacity, stroke_linecap, stroke_linejoin, stroke_miterlimit, stroke_dasharray, stroke_dashoffset, opacity, display, visibility, color, clip_path, mask, filter, font_family, font_size, font_weight, text_anchor, alignment_baseline, baseline_shift, clip, clip_rule, color_interpolation, color_interpolation_filters, color_profile, color_rendering, direction, dominant_baseline, enable_background, flood_color, flood_opacity, font, font_size_adjust, font_stretch, font_style, font_variant, glyph_orientation_horizontal, glyph_orientation_vertical, image_rendering, kerning, letter_spacing, lighting_color, marker, marker_end, marker_mid, marker_start, overflow, pointer_events, shape_rendering, stop_color, stop_opacity, text_decoration, text_rendering, unicode_bidi, word_spacing, writing_mode, class_, style, external_resources_required, xml_base, xml_lang, xml_space, id, attrs
The attribute groups of :class:`~svgwriter.PresentationAttributes`, :class:`~svgwriter.StyleAttributes`, :class:`~svgwriter.ExternalResourcesAttribute`, :class:`~svgwriter.CoreAttributes`, :class:`~svgwriter.Element`. Defaults to None (unset).
**kwargs
Any other SVG attribute in Python spelling, including namespaced and open ones
(``inkscape_label``, ``data_*``).
"""
if view_box is not None:
kwargs["view_box"] = view_box
if preserve_aspect_ratio is not None:
kwargs["preserve_aspect_ratio"] = preserve_aspect_ratio
self._forward_common(
kwargs, id, attrs, xml_base, xml_lang, xml_space, None, None, None, external_resources_required, None,
None, None, None, None, None, class_, style, fill, fill_opacity, fill_rule, stroke, stroke_width,
stroke_opacity, stroke_linecap, stroke_linejoin, stroke_miterlimit, stroke_dasharray,
stroke_dashoffset, opacity, display, visibility, color, clip_path, mask, filter, font_family,
font_size, font_weight, text_anchor, alignment_baseline, baseline_shift, clip, clip_rule,
color_interpolation, color_interpolation_filters, color_profile, color_rendering, direction,
dominant_baseline, enable_background, flood_color, flood_opacity, font, font_size_adjust, font_stretch,
font_style, font_variant, glyph_orientation_horizontal, glyph_orientation_vertical, image_rendering,
kerning, letter_spacing, lighting_color, marker, marker_end, marker_mid, marker_start, overflow,
pointer_events, shape_rendering, stop_color, stop_opacity, text_decoration, text_rendering,
unicode_bidi, word_spacing, writing_mode
)
super().__init__(*args, **kwargs)
# endregion
[docs]
class Switch(PathOperand, GraphicsElement, GraphicsContainer, tag="switch", category=Category.CONTAINER):
"""``<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>'
"""
# region generated by `python scripts/dev.py svgwriter signatures write`
def __init__(
self,
*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,
) -> None:
"""Create a ``<switch>`` element.
Parameters
----------
*args
See the class docstring.
transform, anchor, fill, fill_opacity, fill_rule, stroke, stroke_width, stroke_opacity, stroke_linecap, stroke_linejoin, stroke_miterlimit, stroke_dasharray, stroke_dashoffset, opacity, display, visibility, color, clip_path, mask, filter, font_family, font_size, font_weight, text_anchor, alignment_baseline, baseline_shift, clip, clip_rule, color_interpolation, color_interpolation_filters, color_profile, color_rendering, direction, dominant_baseline, enable_background, flood_color, flood_opacity, font, font_size_adjust, font_stretch, font_style, font_variant, glyph_orientation_horizontal, glyph_orientation_vertical, image_rendering, kerning, letter_spacing, lighting_color, marker, marker_end, marker_mid, marker_start, overflow, pointer_events, shape_rendering, stop_color, stop_opacity, text_decoration, text_rendering, unicode_bidi, word_spacing, writing_mode, class_, style, required_features, required_extensions, system_language, external_resources_required, xml_base, xml_lang, xml_space, id, attrs
The attribute groups of :class:`~svgwriter.Transformable`, :class:`~svgwriter.PresentationAttributes`, :class:`~svgwriter.StyleAttributes`, :class:`~svgwriter.ConditionalAttributes`, :class:`~svgwriter.ExternalResourcesAttribute`, :class:`~svgwriter.CoreAttributes`, :class:`~svgwriter.Element`. Defaults to None (unset).
**kwargs
Any other SVG attribute in Python spelling, including namespaced and open ones
(``inkscape_label``, ``data_*``).
"""
self._forward_common(
kwargs, id, attrs, xml_base, xml_lang, xml_space, required_features, required_extensions,
system_language, external_resources_required, None, None, None, None, None, None, class_, style, fill,
fill_opacity, fill_rule, stroke, stroke_width, stroke_opacity, stroke_linecap, stroke_linejoin,
stroke_miterlimit, stroke_dasharray, stroke_dashoffset, opacity, display, visibility, color, clip_path,
mask, filter, font_family, font_size, font_weight, text_anchor, alignment_baseline, baseline_shift,
clip, clip_rule, color_interpolation, color_interpolation_filters, color_profile, color_rendering,
direction, dominant_baseline, enable_background, flood_color, flood_opacity, font, font_size_adjust,
font_stretch, font_style, font_variant, glyph_orientation_horizontal, glyph_orientation_vertical,
image_rendering, kerning, letter_spacing, lighting_color, marker, marker_end, marker_mid, marker_start,
overflow, pointer_events, shape_rendering, stop_color, stop_opacity, text_decoration, text_rendering,
unicode_bidi, word_spacing, writing_mode, transform, anchor
)
super().__init__(*args, **kwargs)
# endregion
[docs]
def local_bbox(self) -> BBox | None:
"""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)
"""
return children_bbox(self)
[docs]
def local_outline(self) -> PathData:
"""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)
"""
return union_outline(self)