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

Class for SVG native compat renderers

SVGNativeRenderer

Class for SVG native renderers

SVGElement dataclass

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

Bases: object

Class for SVG elements

Parameters:

Name Type Description Default
name str
required
value str | None
None
attributes dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
elements list[str]

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

<dynamic>

Methods:

Name Description
add_element

Add an sub-element to the SVG element

to_string

Return the SVG string representing the element

add_element

add_element(element)

Add an sub-element to the SVG element

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

to_string

to_string(indent: int = 0)

Return the SVG string representing the element

Source code in src/momapy/rendering/svg_native.py
def to_string(self, indent: int = 0):
    """Return the SVG string representing the element"""
    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

Class for SVG native compat renderers

Parameters:

Name Type Description Default
svg SVGElement
required
config dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>

Methods:

Name Description
begin_session

Begin a session

end_session

End a session

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

Make a new page

render_drawing_element

Render a drawing element

render_layout_element

Render a layout element

render_map

Render a map

begin_session

begin_session()

Begin a session

Source code in src/momapy/rendering/svg_native.py
def begin_session(self):
    pass

end_session

end_session()

End a session

Source code in src/momapy/rendering/svg_native.py
def end_session(self):
    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") as f:
            f.write(str(self.svg))

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)

Make a new page

Source code in src/momapy/rendering/svg_native.py
def new_page(self, width, height):
    pass

render_drawing_element

render_drawing_element(drawing_element)

Render a drawing element

Source code in src/momapy/rendering/svg_native.py
def render_drawing_element(self, drawing_element):
    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

Source code in src/momapy/rendering/svg_native.py
def render_layout_element(self, layout_element):
    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

Source code in src/momapy/rendering/svg_native.py
def render_map(self, map_):
    self.render_layout_element(map_.layout)

SVGNativeRenderer dataclass

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

Bases: Renderer

Class for SVG native renderers

Parameters:

Name Type Description Default
svg SVGElement
required
config dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>

Methods:

Name Description
begin_session

Begin a session

end_session

End a session

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

Make a new page

render_drawing_element

Render a drawing element

render_layout_element

Render a layout element

render_map

Render a map

begin_session

begin_session()

Begin a session

Source code in src/momapy/rendering/svg_native.py
def begin_session(self):
    pass

end_session

end_session()

End a session

Source code in src/momapy/rendering/svg_native.py
def end_session(self):
    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") as f:
            f.write(str(self.svg))

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)

Make a new page

Source code in src/momapy/rendering/svg_native.py
def new_page(self, width, height):
    pass

render_drawing_element

render_drawing_element(drawing_element)

Render a drawing element

Source code in src/momapy/rendering/svg_native.py
def render_drawing_element(self, drawing_element):
    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

Source code in src/momapy/rendering/svg_native.py
def render_layout_element(self, layout_element):
    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

Source code in src/momapy/rendering/svg_native.py
def render_map(self, map_):
    self.render_layout_element(map_.layout)