Source code for svgwriter.model.elements.text._text

"""
``text`` and the decoration flags.
"""

from __future__ import annotations

import enum
from typing import TYPE_CHECKING, Any, ClassVar

from ...._internal._constants import DEFAULT_PRECISION
from ...units.lengths import LengthLike
from .._behavior import (
    GraphicsElement,
    PathOperand,
)
from ..core import Category, Child, ContentModel, Element
from ._textaccess import TextAccess
from ._textbase import _TextLength, _TextPositioning

# 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, LengthAdjust, 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 ._textbase import Coordinates
# endregion

__all__ = ["Text", "TextDecoration"]


[docs] class TextDecoration(enum.Flag): """The ``text-decoration`` keywords, combinable with ``|``. Pass a member (or a combination) as the ``decoration`` argument of :class:`Text`, :class:`TSpan` or :class:`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 = enum.auto() OVERLINE = enum.auto() LINE_THROUGH = enum.auto() BLINK = enum.auto()
[docs] def svg_value(self, precision: int = DEFAULT_PRECISION) -> str: """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' """ words = [m.name.lower().replace("_", "-") for m in TextDecoration if m.value and m in self] # type: ignore[union-attr] return " ".join(words) or "none"
[docs] class Text(TextAccess, PathOperand, GraphicsElement, _TextLength, _TextPositioning, tag="text", category=Category.TEXT_CONTENT, content=ContentModel.MIXED): """``<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, y, dx, 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 :class:`Character` for character *i* (``len(text)`` counts characters); :meth:`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( {"x", "y", "dx", "dy", "rotate", "textLength", "lengthAdjust", "font-family", "font-size", "font-weight", "font-style", "text-anchor"} ) # region generated by `python scripts/dev.py svgwriter signatures write` def __init__( self, *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, ) -> None: """Create a ``<text>`` element. Parameters ---------- *args, text_length, length_adjust, x, y, dx, dy, rotate, decoration 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_*``). """ if text_length is not None: kwargs["text_length"] = text_length if length_adjust is not None: kwargs["length_adjust"] = length_adjust if x is not None: kwargs["x"] = x if y is not None: kwargs["y"] = y if dx is not None: kwargs["dx"] = dx if dy is not None: kwargs["dy"] = dy if rotate is not None: kwargs["rotate"] = rotate if decoration is not None: kwargs["decoration"] = decoration 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