Skip to content

SVG-native

momapy.rendering.svg_native

Classes for rendering in the SVG format

Classes:

Name Description
SVGElement

Class for SVG elements.

SVGNativeCompatRenderer

Renderer for SVG with compatibility mode filters.

SVGNativeRenderer

Renderer implementation for generating native SVG output.

SVGElement dataclass

SVGElement(name: str, value: Optional[str] = None, attributes: dict = dict(), elements: list[SVGElement] = list())

Bases: object

Class for SVG elements.

This class represents an SVG element with a name, optional text value, attributes, and child elements.

Attributes:

Name Type Description
name str

The tag name of the SVG element (e.g., 'svg', 'rect', 'path')

value Optional[str]

Optional text content of the element

attributes dict

Dictionary of attribute names and values

elements list[SVGElement]

List of child SVGElement instances

Example

element = SVGElement( ... name="rect", ... attributes={"x": "0", "y": "0", "width": "100", "height": "100"} ... ) print(element)

Methods:

Name Description
add_element

Add a sub-element to the SVG element.

to_string

Return the SVG string representing the element.

add_element

add_element(element)

Add a sub-element to the SVG element.

Parameters:

Name Type Description Default
element

The child SVGElement to add

required
Source code in src/momapy/rendering/svg_native.py
def add_element(self, element):
    """Add a sub-element to the SVG element.

    Args:
        element: The child SVGElement to add
    """
    self.elements.append(element)

to_string

to_string(indent: int = 0)

Return the SVG string representing the element.

Parameters:

Name Type Description Default
indent int

The indentation level (number of tabs)

0

Returns:

Type Description

The SVG markup as a string

Source code in src/momapy/rendering/svg_native.py
def to_string(self, indent: int = 0):
    """Return the SVG string representing the element.

    Args:
        indent: The indentation level (number of tabs)

    Returns:
        The SVG markup as a string
    """
    s_indent = "\t" * indent
    s_value = f"{s_indent}{self.value}\n" if self.value is not None else ""
    if self.attributes:
        l_s_attributes = []
        for attr_name, attr_value in self.attributes.items():
            s_attr_name = attr_name
            s_attr_value = f'"{attr_value}"'
            s_attribute = f"{s_attr_name}={s_attr_value}"
            l_s_attributes.append(s_attribute)
        s_attributes = f" {' '.join(l_s_attributes)}"
    else:
        s_attributes = ""
    if self.elements:
        s_elements = "\n".join(
            [child.to_string(indent + 1) for child in self.elements]
        )
        s_elements += "\n"
    else:
        s_elements = ""
    return f"{s_indent}<{self.name}{s_attributes}>\n{s_value}{s_elements}{s_indent}</{self.name}>"

SVGNativeCompatRenderer dataclass

SVGNativeCompatRenderer(svg: SVGElement, config: dict = dict(), _filter_elements: list[SVGElement] = list())

Bases: SVGNativeRenderer

Renderer for SVG with compatibility mode filters.

This renderer extends SVGNativeRenderer to provide compatibility with older SVG viewers by converting filters to a compatible format.

Example

renderer = SVGNativeCompatRenderer.from_file("output.svg", 800, 600, "svg") renderer.begin_session() renderer.render_layout_element(layout_element) renderer.end_session()

Methods:

Name Description
begin_session

Begin a rendering session.

end_session

End the rendering session and save the output.

from_file

Create an SVGNativeRenderer instance from a file path.

get_bolder_font_weight

Return the lightest font weight bolder than the given font weight

get_lighter_font_weight

Return the boldest font weight lighter than the given font weight

new_page

Create a new page in the output document.

render_drawing_element

Render a drawing element to the output.

render_layout_element

Render a layout element to the output.

render_map

Render a map to the output.

begin_session

begin_session()

Begin a rendering session.

This method initializes the rendering context. For SVGNativeRenderer, no explicit initialization is needed as the SVG element is created during instantiation.

Source code in src/momapy/rendering/svg_native.py
def begin_session(self):
    """Begin a rendering session.

    This method initializes the rendering context. For SVGNativeRenderer,
    no explicit initialization is needed as the SVG element is created
    during instantiation.
    """
    pass

end_session

end_session()

End the rendering session and save the output.

This method finalizes the SVG document, adds any filter definitions to the defs section, and writes the output to the file.

Source code in src/momapy/rendering/svg_native.py
def end_session(self):
    """End the rendering session and save the output.

    This method finalizes the SVG document, adds any filter definitions
    to the defs section, and writes the output to the file.
    """
    if self._filter_elements:
        defs = SVGElement(name="defs", elements=self._filter_elements)
        self.svg.add_element(defs)
    if self.config.get("output_file") is not None:
        with open(self.config["output_file"], "w", encoding="utf-8") as f:
            f.write(str(self.svg))

from_file classmethod

from_file(output_file, width, height, format_, config=None)

Create an SVGNativeRenderer instance from a file path.

Parameters:

Name Type Description Default
output_file

The output file path

required
width

The width of the SVG canvas

required
height

The height of the SVG canvas

required
format_

The output format (must be "svg")

required
config

Optional configuration dictionary

None

Returns:

Type Description

A new SVGNativeRenderer instance

Raises:

Type Description
ValueError

If the format is not supported

Example

renderer = SVGNativeRenderer.from_file("output.svg", 800, 600, "svg")

Source code in src/momapy/rendering/svg_native.py
@classmethod
def from_file(cls, output_file, width, height, format_, config=None):
    """Create an SVGNativeRenderer instance from a file path.

    Args:
        output_file: The output file path
        width: The width of the SVG canvas
        height: The height of the SVG canvas
        format_: The output format (must be "svg")
        config: Optional configuration dictionary

    Returns:
        A new SVGNativeRenderer instance

    Raises:
        ValueError: If the format is not supported

    Example:
        >>> renderer = SVGNativeRenderer.from_file("output.svg", 800, 600, "svg")
    """
    if format_ not in cls.supported_formats:
        raise ValueError(f"Unsupported format: {format_}")
    if config is None:
        config = {}
    config["output_file"] = output_file
    config["width"] = width
    config["height"] = height
    config["format"] = format_
    svg = SVGElement(
        name="svg",
        attributes={
            "xmlns": "http://www.w3.org/2000/svg",
            "viewBox": f"0 0 {width} {height}",
        },
    )
    return cls(svg=svg, config=config)

get_bolder_font_weight classmethod

get_bolder_font_weight(font_weight: FontWeight | float) -> float

Return the lightest font weight bolder than the given font weight

Source code in src/momapy/rendering/core.py
@classmethod
def get_bolder_font_weight(
    cls, font_weight: momapy.drawing.FontWeight | float
) -> float:
    """Return the lightest font weight bolder than the given font weight"""
    if isinstance(font_weight, momapy.drawing.FontWeight):
        font_weight = cls.font_weight_value_mapping.get(font_weight)
        if font_weight is None:
            raise ValueError(
                f"font weight must be a float, {momapy.drawing.FontWeight.NORMAL}, or {momapy.drawing.FontWeight.BOLD}"
            )
    if font_weight < 400:
        new_font_weight = 400
    elif font_weight < 600:
        new_font_weight = 700
    else:
        new_font_weight = 900
    return new_font_weight

get_lighter_font_weight classmethod

get_lighter_font_weight(font_weight: FontWeight | float) -> float

Return the boldest font weight lighter than the given font weight

Source code in src/momapy/rendering/core.py
@classmethod
def get_lighter_font_weight(
    cls, font_weight: momapy.drawing.FontWeight | float
) -> float:
    """Return the boldest font weight lighter than the given font weight"""
    if isinstance(font_weight, momapy.drawing.FontWeight):
        font_weight = cls.font_weight_value_mapping.get(font_weight)
        if font_weight is None:
            raise ValueError(
                f"font weight must be a float, {momapy.drawing.FontWeight.NORMAL}, or {momapy.drawing.FontWeight.BOLD}"
            )
    if font_weight > 700:
        new_font_weight = 700
    elif font_weight > 500:
        new_font_weight = 400
    else:
        new_font_weight = 100
    return new_font_weight

new_page

new_page(width, height)

Create a new page in the output document.

Parameters:

Name Type Description Default
width

The width of the new page

required
height

The height of the new page

required
Note

SVG format does not support multiple pages. This method is a no-op.

Source code in src/momapy/rendering/svg_native.py
def new_page(self, width, height):
    """Create a new page in the output document.

    Args:
        width: The width of the new page
        height: The height of the new page

    Note:
        SVG format does not support multiple pages. This method is a no-op.
    """
    pass

render_drawing_element

render_drawing_element(drawing_element)

Render a drawing element to the output.

Parameters:

Name Type Description Default
drawing_element

The drawing element to render

required

This method converts the drawing element to an SVG element and adds it to the SVG document.

Source code in src/momapy/rendering/svg_native.py
def render_drawing_element(self, drawing_element):
    """Render a drawing element to the output.

    Args:
        drawing_element: The drawing element to render

    This method converts the drawing element to an SVG element
    and adds it to the SVG document.
    """
    element = self._make_drawing_element_element(drawing_element)
    self.svg.add_element(element)

render_layout_element

render_layout_element(layout_element)

Render a layout element to the output.

Parameters:

Name Type Description Default
layout_element

The layout element to render

required
Source code in src/momapy/rendering/svg_native.py
def render_layout_element(self, layout_element):
    """Render a layout element to the output.

    Args:
        layout_element: The layout element to render
    """
    drawing_elements = layout_element.drawing_elements()
    for drawing_element in drawing_elements:
        self.render_drawing_element(drawing_element)

render_map

render_map(map_)

Render a map to the output.

Parameters:

Name Type Description Default
map_

The map to render

required
Source code in src/momapy/rendering/svg_native.py
def render_map(self, map_):
    """Render a map to the output.

    Args:
        map_: The map to render
    """
    self.render_layout_element(map_.layout)

SVGNativeRenderer dataclass

SVGNativeRenderer(svg: SVGElement, config: dict = dict(), _filter_elements: list[SVGElement] = list())

Bases: Renderer

Renderer implementation for generating native SVG output.

This renderer creates SVG markup directly without external dependencies. It supports all standard SVG features including filters, transformations, and presentation attributes.

Attributes:

Name Type Description
svg SVGElement

The root SVGElement that will contain all rendered content

Example

from momapy.meta.nodes import Rectangle import momapy.geometry

Create a layout element to render

node = Rectangle( ... position=momapy.geometry.Point(100, 100), ... width=200, ... height=100 ... )

Create renderer and render the element

renderer = SVGNativeRenderer.from_file("output.svg", 800, 600, "svg") renderer.begin_session() renderer.render_layout_element(node) renderer.end_session()

Methods:

Name Description
begin_session

Begin a rendering session.

end_session

End the rendering session and save the output.

from_file

Create an SVGNativeRenderer instance from a file path.

get_bolder_font_weight

Return the lightest font weight bolder than the given font weight

get_lighter_font_weight

Return the boldest font weight lighter than the given font weight

new_page

Create a new page in the output document.

render_drawing_element

Render a drawing element to the output.

render_layout_element

Render a layout element to the output.

render_map

Render a map to the output.

begin_session

begin_session()

Begin a rendering session.

This method initializes the rendering context. For SVGNativeRenderer, no explicit initialization is needed as the SVG element is created during instantiation.

Source code in src/momapy/rendering/svg_native.py
def begin_session(self):
    """Begin a rendering session.

    This method initializes the rendering context. For SVGNativeRenderer,
    no explicit initialization is needed as the SVG element is created
    during instantiation.
    """
    pass

end_session

end_session()

End the rendering session and save the output.

This method finalizes the SVG document, adds any filter definitions to the defs section, and writes the output to the file.

Source code in src/momapy/rendering/svg_native.py
def end_session(self):
    """End the rendering session and save the output.

    This method finalizes the SVG document, adds any filter definitions
    to the defs section, and writes the output to the file.
    """
    if self._filter_elements:
        defs = SVGElement(name="defs", elements=self._filter_elements)
        self.svg.add_element(defs)
    if self.config.get("output_file") is not None:
        with open(self.config["output_file"], "w", encoding="utf-8") as f:
            f.write(str(self.svg))

from_file classmethod

from_file(output_file, width, height, format_, config=None)

Create an SVGNativeRenderer instance from a file path.

Parameters:

Name Type Description Default
output_file

The output file path

required
width

The width of the SVG canvas

required
height

The height of the SVG canvas

required
format_

The output format (must be "svg")

required
config

Optional configuration dictionary

None

Returns:

Type Description

A new SVGNativeRenderer instance

Raises:

Type Description
ValueError

If the format is not supported

Example

renderer = SVGNativeRenderer.from_file("output.svg", 800, 600, "svg")

Source code in src/momapy/rendering/svg_native.py
@classmethod
def from_file(cls, output_file, width, height, format_, config=None):
    """Create an SVGNativeRenderer instance from a file path.

    Args:
        output_file: The output file path
        width: The width of the SVG canvas
        height: The height of the SVG canvas
        format_: The output format (must be "svg")
        config: Optional configuration dictionary

    Returns:
        A new SVGNativeRenderer instance

    Raises:
        ValueError: If the format is not supported

    Example:
        >>> renderer = SVGNativeRenderer.from_file("output.svg", 800, 600, "svg")
    """
    if format_ not in cls.supported_formats:
        raise ValueError(f"Unsupported format: {format_}")
    if config is None:
        config = {}
    config["output_file"] = output_file
    config["width"] = width
    config["height"] = height
    config["format"] = format_
    svg = SVGElement(
        name="svg",
        attributes={
            "xmlns": "http://www.w3.org/2000/svg",
            "viewBox": f"0 0 {width} {height}",
        },
    )
    return cls(svg=svg, config=config)

get_bolder_font_weight classmethod

get_bolder_font_weight(font_weight: FontWeight | float) -> float

Return the lightest font weight bolder than the given font weight

Source code in src/momapy/rendering/core.py
@classmethod
def get_bolder_font_weight(
    cls, font_weight: momapy.drawing.FontWeight | float
) -> float:
    """Return the lightest font weight bolder than the given font weight"""
    if isinstance(font_weight, momapy.drawing.FontWeight):
        font_weight = cls.font_weight_value_mapping.get(font_weight)
        if font_weight is None:
            raise ValueError(
                f"font weight must be a float, {momapy.drawing.FontWeight.NORMAL}, or {momapy.drawing.FontWeight.BOLD}"
            )
    if font_weight < 400:
        new_font_weight = 400
    elif font_weight < 600:
        new_font_weight = 700
    else:
        new_font_weight = 900
    return new_font_weight

get_lighter_font_weight classmethod

get_lighter_font_weight(font_weight: FontWeight | float) -> float

Return the boldest font weight lighter than the given font weight

Source code in src/momapy/rendering/core.py
@classmethod
def get_lighter_font_weight(
    cls, font_weight: momapy.drawing.FontWeight | float
) -> float:
    """Return the boldest font weight lighter than the given font weight"""
    if isinstance(font_weight, momapy.drawing.FontWeight):
        font_weight = cls.font_weight_value_mapping.get(font_weight)
        if font_weight is None:
            raise ValueError(
                f"font weight must be a float, {momapy.drawing.FontWeight.NORMAL}, or {momapy.drawing.FontWeight.BOLD}"
            )
    if font_weight > 700:
        new_font_weight = 700
    elif font_weight > 500:
        new_font_weight = 400
    else:
        new_font_weight = 100
    return new_font_weight

new_page

new_page(width, height)

Create a new page in the output document.

Parameters:

Name Type Description Default
width

The width of the new page

required
height

The height of the new page

required
Note

SVG format does not support multiple pages. This method is a no-op.

Source code in src/momapy/rendering/svg_native.py
def new_page(self, width, height):
    """Create a new page in the output document.

    Args:
        width: The width of the new page
        height: The height of the new page

    Note:
        SVG format does not support multiple pages. This method is a no-op.
    """
    pass

render_drawing_element

render_drawing_element(drawing_element)

Render a drawing element to the output.

Parameters:

Name Type Description Default
drawing_element

The drawing element to render

required

This method converts the drawing element to an SVG element and adds it to the SVG document.

Source code in src/momapy/rendering/svg_native.py
def render_drawing_element(self, drawing_element):
    """Render a drawing element to the output.

    Args:
        drawing_element: The drawing element to render

    This method converts the drawing element to an SVG element
    and adds it to the SVG document.
    """
    element = self._make_drawing_element_element(drawing_element)
    self.svg.add_element(element)

render_layout_element

render_layout_element(layout_element)

Render a layout element to the output.

Parameters:

Name Type Description Default
layout_element

The layout element to render

required
Source code in src/momapy/rendering/svg_native.py
def render_layout_element(self, layout_element):
    """Render a layout element to the output.

    Args:
        layout_element: The layout element to render
    """
    drawing_elements = layout_element.drawing_elements()
    for drawing_element in drawing_elements:
        self.render_drawing_element(drawing_element)

render_map

render_map(map_)

Render a map to the output.

Parameters:

Name Type Description Default
map_

The map to render

required
Source code in src/momapy/rendering/svg_native.py
def render_map(self, map_):
    """Render a map to the output.

    Args:
        map_: The map to render
    """
    self.render_layout_element(map_.layout)