Skia
momapy.rendering.skia
Class for rendering with Skia
Classes:
| Name | Description |
|---|---|
SkiaRenderer |
Renderer implementation using the Skia graphics library. |
SkiaRenderer
dataclass
SkiaRenderer(_current_state: dict = dict(), _states: list[dict] = list(), *, canvas: Canvas, _config: dict = dict(), _skia_typefaces: dict = dict(), _skia_fonts: dict = dict())
Bases: StatefulRenderer
Renderer implementation using the Skia graphics library.
This renderer supports multiple output formats including PDF, SVG, PNG, JPEG, and WebP. It provides hardware-accelerated rendering capabilities and advanced features like filters and effects.
Attributes:
| Name | Type | Description |
|---|---|---|
canvas |
Canvas
|
The Skia canvas used for rendering |
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 = SkiaRenderer.from_file("output.pdf", 800, 600, "pdf") 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 a SkiaRenderer 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 to the output. |
restore |
Set the current state to the last saved state |
save |
Save the current state |
self_restore |
Restore the Skia canvas state. |
self_save |
Save the Skia canvas 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 |
begin_session
Begin a rendering session.
This method initializes the rendering context. For SkiaRenderer, no explicit initialization is needed beyond the canvas setup.
end_session
End the rendering session and save the output.
This method finalizes the rendering, flushes the canvas, and saves the output to the file. The specific actions depend on the output format: - PDF: Ends the page and closes the document - PNG/JPEG/WebP: Takes a snapshot and saves the image - SVG: Flushes the stream
Source code in src/momapy/rendering/skia.py
from_file
classmethod
from_file(file_path: str | PathLike, width: float, height: float, format_: Literal['pdf', 'svg', 'png', 'jpeg', 'webp'] = 'pdf') -> Self
Create a SkiaRenderer 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_
|
Literal['pdf', 'svg', 'png', 'jpeg', 'webp']
|
The output format (pdf, svg, png, jpeg, or webp) |
'pdf'
|
Returns:
| Type | Description |
|---|---|
Self
|
A new SkiaRenderer instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
Example
renderer = SkiaRenderer.from_file("output.pdf", 800, 600, "pdf")
Source code in src/momapy/rendering/skia.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
|
The width of the new page |
required | |
height
|
The height of the new page |
required |
Note
Only PDF format supports multiple pages. Other formats will ignore this call.
Source code in src/momapy/rendering/skia.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 handles filters, transformations, and delegates to the appropriate rendering method based on the drawing element type.
Source code in src/momapy/rendering/skia.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/skia.py
render_map
Render a map to the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_
|
The map to render |
required |
restore
Set the current state to the last saved state
save
self_restore
Restore the Skia canvas state.
This method restores the Skia canvas to the state saved by the most recent call to self_save().
self_save
Save the Skia canvas state.
This method saves the current state of the Skia canvas, including transformations and clipping regions.
set_current_state
set_current_state_from_drawing_element
set_current_state_from_drawing_element(drawing_element: DrawingElement)
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