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 |
|---|
Examples:
element = SVGElement(
name="rect",
attributes={"x": "0", "y": "0", "width": "100", "height": "100"}
)
print(element)
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 a sub-element to the SVG element. |
to_string |
Return the SVG string representing the element. |
add_element
add_element(element: SVGElement) -> None
Add a sub-element to the SVG element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
SVGElement
|
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 |
|---|---|
str
|
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.
Examples:
renderer = SVGNativeCompatRenderer.from_file("output.svg", 800, 600, "svg")
renderer.begin_session()
renderer.render_layout_element(layout_element)
renderer.end_session()
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 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
from_file(output_file: str | PathLike, width: float, height: float, format_: Literal['svg'], config: dict | None = None) -> Self
Create an SVGNativeRenderer instance from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file
|
str | PathLike
|
The output file path |
required |
width
|
float
|
The width of the SVG canvas |
required |
height
|
float
|
The height of the SVG canvas |
required |
format_
|
Literal['svg']
|
The output format (must be "svg") |
required |
config
|
dict | None
|
Optional configuration dictionary |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
A new SVGNativeRenderer instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
Examples:
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
|
float
|
The width of the new page |
required |
height
|
float
|
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_drawing_element(drawing_element: DrawingElement) -> None
Render a drawing element to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drawing_element
|
DrawingElement
|
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_layout_element(layout_element: LayoutElement) -> None
Render a layout element to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout_element
|
LayoutElement
|
The layout element to render |
required |
Source code in src/momapy/rendering/svg_native.py
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 |
|---|
Examples:
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()
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 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
from_file(output_file: str | PathLike, width: float, height: float, format_: Literal['svg'], config: dict | None = None) -> Self
Create an SVGNativeRenderer instance from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_file
|
str | PathLike
|
The output file path |
required |
width
|
float
|
The width of the SVG canvas |
required |
height
|
float
|
The height of the SVG canvas |
required |
format_
|
Literal['svg']
|
The output format (must be "svg") |
required |
config
|
dict | None
|
Optional configuration dictionary |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
A new SVGNativeRenderer instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
Examples:
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
|
float
|
The width of the new page |
required |
height
|
float
|
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_drawing_element(drawing_element: DrawingElement) -> None
Render a drawing element to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drawing_element
|
DrawingElement
|
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_layout_element(layout_element: LayoutElement) -> None
Render a layout element to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout_element
|
LayoutElement
|
The layout element to render |
required |