SVG-native - momapy

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`

    48
    49
    50
    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`

    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    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`

    156
    157
    def begin_session(self):
        pass

#### `` end_session

    end_session()

End a session

Source code in `src/momapy/rendering/svg_native.py`

    159
    160
    161
    162
    163
    164
    165
    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`

    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    @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`

    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    @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`

    167
    168
    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`

    178
    179
    180
    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`

    173
    174
    175
    176
    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`

    170
    171
    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`

    156
    157
    def begin_session(self):
        pass

#### `` end_session

    end_session()

End a session

Source code in `src/momapy/rendering/svg_native.py`

    159
    160
    161
    162
    163
    164
    165
    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`

    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    @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`

    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    @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`

    167
    168
    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`

    178
    179
    180
    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`

    173
    174
    175
    176
    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`

    170
    171
    def render_map(self, map_):
        self.render_layout_element(map_.layout)