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 a sub-element to the SVG element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
The child SVGElement to add |
required |
to_string
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
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 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
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
from_file
classmethod
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
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
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
new_page
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
render_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
render_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
render_map
Render a map to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_
|
The map to render |
required |
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 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
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
from_file
classmethod
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
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
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
new_page
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
render_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
render_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
render_map
Render a map to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_
|
The map to render |
required |