Skip to content
Edit this page

ictdesigntools.draw.svg

SVG output, with the standard library only.

Shapes carry their Meta as data-id, data-kind and data-<key> attributes and their titles as <title> (hover text and an accessible name), so a page can make parts interactive with a small script and no framework. Components are written once in <defs> and placed with <use>. Nothing is styled inline, so the SVG works under a strict Content-Security-Policy.

to_svg

to_svg(
    drawing: Drawing,
    css_class: str = "",
    standalone: bool = False,
    theme: dict[str, Style] | None = None,
    id_prefix: str = "",
    classes: bool = False,
    class_prefix: str = "d-",
    interactive: bool = False,
) -> str

The drawing as an <svg> element.

standalone adds the namespace and a physical size for a file; id_prefix keeps component ids unique when a page holds several drawings; theme overrides named styles. classes also gives every shape with a named style the class <class_prefix><name>, so a page's stylesheet can restyle it (a dark theme, a highlight) over the colours written as attributes, which stay as the fallback. interactive marks the drawing for the optional script (interactive_script()): hover, select, keyboard focus and a draw:select event for parts that carry a Meta id.

Source code in src/ictdesigntools/draw/svg.py
def to_svg(drawing: Drawing, css_class: str = "", standalone: bool = False,
           theme: dict[str, Style] | None = None, id_prefix: str = "", classes: bool = False,
           class_prefix: str = "d-", interactive: bool = False) -> str:  # fmt: skip
    """The drawing as an ``<svg>`` element.

    ``standalone`` adds the namespace and a physical size for a file; ``id_prefix`` keeps
    component ids unique when a page holds several drawings; ``theme`` overrides named styles.
    ``classes`` also gives every shape with a named style the class ``<class_prefix><name>``, so
    a page's stylesheet can restyle it (a dark theme, a highlight) over the colours written as
    attributes, which stay as the fallback. ``interactive`` marks the drawing for the optional
    script (``interactive_script()``): hover, select, keyboard focus and a ``draw:select``
    event for parts that carry a ``Meta`` id."""
    x, y, w, h = drawing.view
    out = [
        f'<svg viewBox="{_f(x)} {_f(y)} {_f(w)} {_f(h)}" role="img"',
        f' class="{escape(css_class)}"' if css_class else "",
        " data-draw-interactive" if interactive else "",
        ' xmlns="http://www.w3.org/2000/svg"' if standalone else "",
        f' width="{_f(w)}{drawing.units}" height="{_f(h)}{drawing.units}"' if standalone else "",
        ' font-family="Helvetica, Arial, sans-serif">',
        f"<title>{escape(drawing.title)}</title>" if drawing.title else "",
        f"<desc>{escape(drawing.desc)}</desc>" if drawing.desc else "",
    ]
    r = _Renderer(drawing, theme, id_prefix, class_prefix if classes else None)
    components = drawing.components()
    if components:
        out.append("<defs>")
        for c in components:
            out.append(f'<g id="{r.component_id(c)}">')
            r.items(c.items, out)
            out.append("</g>")
        out.append("</defs>")
    r.items(drawing.items, out)
    out.append("</svg>")
    return "".join(out)

svg_file

svg_file(
    drawing: Drawing, theme: dict[str, Style] | None = None
) -> bytes

A standalone SVG file, at full size in the drawing's units.

Source code in src/ictdesigntools/draw/svg.py
def svg_file(drawing: Drawing, theme: dict[str, Style] | None = None) -> bytes:
    """A standalone SVG file, at full size in the drawing's units."""
    body = to_svg(drawing, standalone=True, theme=theme)
    return ('<?xml version="1.0" encoding="UTF-8"?>\n' + body + "\n").encode("utf-8")

meta_attrs

meta_attrs(meta: Meta) -> str

data- attributes for a part's meta.

Source code in src/ictdesigntools/draw/svg.py
def meta_attrs(meta: Meta) -> str:
    """``data-`` attributes for a part's meta."""
    if not meta:
        return ""
    attrs = []
    if meta.id:
        attrs.append(f" data-id={quoteattr(meta.id)}")
    if meta.kind:
        attrs.append(f" data-kind={quoteattr(meta.kind)}")
    for key, value in meta.data:
        name = _KEY.sub("-", key.lower()).strip("-")
        if name and name not in ("id", "kind"):
            attrs.append(f" data-{name}={quoteattr(value)}")
    return "".join(attrs)