Source code for svgwriter.model.elements.structure._link

"""
Elements that reference others: ``use`` and ``a``.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any, ClassVar

from ...pathdata.data import BBox, PathData
from ...units.lengths import LengthLike
from .._behavior import (
    GraphicsElement,
    PathOperand,
    Positioned,
    XLinkAttributes,
    children_bbox,
    union_outline,
)
from ..core import Category, Child, Element, GraphicsContainer
from ._use import UseGeometry

# 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 ...color.color import Color
    from ...color.paint import PaintValue
    from ...transform.transform import TransformLike
    from .._behavior import Pivot, XmlSpace
    from ..core import AttrValue
    from ..text import TextDecoration
# endregion

__all__ = ["A", "Use"]


[docs] class Use( UseGeometry, PathOperand, GraphicsElement, XLinkAttributes, Positioned, tag="use", category=Category.STRUCTURAL, ): """``<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, 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"/>' """ # region generated by `python scripts/dev.py svgwriter signatures write` def __init__( self, 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, ) -> None: """Create a ``<use>`` element. Parameters ---------- href, x, y, width, height 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, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, 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.XLinkAttributes`, :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_*``). """ if width is not None: kwargs["width"] = width if height is not None: kwargs["height"] = height self._forward_common( kwargs, id, attrs, xml_base, xml_lang, xml_space, required_features, required_extensions, system_language, external_resources_required, None, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, 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 ) # endregion super().__init__(href=href, x=x, y=y, **kwargs) geometry_attributes: ClassVar[frozenset[str]] = frozenset({"x", "y", "width", "height", "xlink:href"})
[docs] class A(PathOperand, GraphicsElement, XLinkAttributes, GraphicsContainer, tag="a", category=Category.CONTAINER): """``<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>' """ # region generated by `python scripts/dev.py svgwriter signatures write` def __init__( self, *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, ) -> None: """Create a ``<a>`` element. Parameters ---------- *children, href, target 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, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, 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.XLinkAttributes`, :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, xlink_title, xlink_show, xlink_actuate, xlink_role, xlink_arcrole, 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 ) # endregion super().__init__(*children, href=href, **kwargs) self._assign(target=target)
[docs] def local_bbox(self) -> BBox | None: """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) """ return children_bbox(self)
[docs] def local_outline(self) -> PathData: """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) """ return union_outline(self)