Cairo
momapy.rendering.cairo
Class for rendering with Cairo.
Note
SVG filter effects (e.g. drop shadow, Gaussian blur) are not supported
by this backend. Cairo has no native filter primitives, so any
filter attribute on drawing elements is ignored. Use the skia or
svg-native backend if you need filter effects.
Classes:
| Name | Description |
|---|---|
CairoRenderer |
Renderer implementation using the Cairo graphics library. |
CairoRenderer
dataclass
CairoRenderer(_current_state: dict[str, Any] = dict(), _states: list[dict[str, Any]] = list(), *, context: Context, _config: dict[str, Any] = dict(), _pango_font_descriptions: dict[tuple, Any] = dict())
Bases: StatefulRenderer, SupportsFileOutput
Renderer implementation using the Cairo graphics library.
This renderer supports multiple output formats including PDF, SVG, PNG, and PostScript. It uses Pango for text rendering.
SVG filter effects (drop shadow, Gaussian blur, etc.) are not supported:
Cairo has no native filter primitives, so the filter attribute on
drawing elements is ignored.
Attributes:
| Name | Type | Description |
|---|---|---|
context |
Context
|
The Cairo context used for rendering |
Examples:
from momapy.meta.nodes import Rectangle
from momapy.geometry import Point
# Create a layout element to render
node = Rectangle(
position=Point(100.0, 100.0),
width=200.0,
height=100.0
)
# Create renderer and render the element
renderer = CairoRenderer.from_file("output.pdf", 800, 600, "pdf")
renderer.begin_session()
renderer.render_layout_element(node)
renderer.end_session()
Methods:
| Name | Description |
|---|---|
__post_init__ |
Initialize the renderer's current state after initialization. |
begin_session |
Begin a rendering session. |
end_session |
End the rendering session and save the output. |
from_file |
Create a CairoRenderer instance from a file path. |
get_bolder_font_weight |
Return the lightest font weight bolder than the given font weight. |
get_current_state |
Return the current state. |
get_current_value |
Return the current value for an attribute. |
get_initial_value |
Return the initial value for an attribute. |
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. |
restore |
Set the current state to the last saved state. |
save |
Save the current state. |
self_restore |
Restore the Cairo context state. |
self_save |
Save the Cairo context state. |
set_current_state |
Set the current state to the given state. |
set_current_state_from_drawing_element |
Set the current state to a state given by a drawing element. |
set_current_value |
Set the current value for an attribute. |
__post_init__
begin_session
Begin a rendering session.
This method initializes the rendering context. For CairoRenderer, no explicit initialization is needed beyond the context setup.
default_format
class-attribute
The format used when from_file is called with format_=None.
Subclasses set this to one of their :attr:supported_formats.
end_session
End the rendering session and save the output.
This method finalizes the rendering and saves the output to the file. For PNG format, it writes the image data. For other formats, it finishes and flushes the surface.
Source code in src/momapy/rendering/cairo.py
from_file
classmethod
from_file(file_path: str | PathLike, width: float, height: float, format_: str | None = None) -> Self
Create a CairoRenderer instance from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | PathLike
|
The output file path |
required |
width
|
float
|
The width of the canvas |
required |
height
|
float
|
The height of the canvas |
required |
format_
|
str | None
|
The output format (pdf, svg, png, or ps). |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
A new CairoRenderer instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
Examples:
Source code in src/momapy/rendering/cairo.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_current_state
get_current_value
get_initial_value
Return the initial value for an attribute.
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
Only PDF and PostScript formats support multiple pages. Other formats will ignore this call.
Source code in src/momapy/rendering/cairo.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 handles transformations and delegates to the appropriate rendering method based on the drawing element type.
Source code in src/momapy/rendering/cairo.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/cairo.py
render_map
render_map(map_: Map) -> None
Render a map.
This is a convenience method, not part of the abstract
contract: the default implementation renders the map's layout via
:meth:render_layout_element, which is what every built-in backend
needs. Subclasses may override it if a backend requires
map-specific handling, but they are not obliged to. The file
pipeline (:func:render_map/:func:render_maps) does not call this
method; it renders each page through :meth:render_layout_element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_
|
Map
|
The map to render. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the map has no layout to render. |
Source code in src/momapy/rendering/core.py
restore
Set the current state to the last saved state.
Source code in src/momapy/rendering/core.py
save
self_restore
Restore the Cairo context state.
This method restores the Cairo context to the state saved by the most recent call to self_save().
self_save
Save the Cairo context state.
This method saves the current state of the Cairo context, including transformations, clipping regions, and drawing parameters.
set_current_state
Set the current state to the given state.
set_current_state_from_drawing_element
set_current_state_from_drawing_element(drawing_element: DrawingElement) -> None
Set the current state to a state given by a drawing element.
Source code in src/momapy/rendering/core.py
set_current_value
Set the current value for an attribute.