Renderer API¶
The Renderer class converts a laid-out node tree to SVG (or PNG) output.
Basic Usage¶
from latticesvg import GridContainer, TextNode, Renderer
grid = GridContainer(style={"width": 400, "padding": 20, "background": "white"})
grid.add(TextNode("Hello!", style={"font-size": 24}))
renderer = Renderer()
# Output SVG file
renderer.render(grid, "output.svg")
# Output PNG file (requires cairosvg)
renderer.render_png(grid, "output.png", scale=2)
# Get drawsvg.Drawing object
drawing = renderer.render_to_drawing(grid)
# Get SVG string
svg_string = renderer.render_to_string(grid)
Constructor¶
No parameters. Creates an internal drawsvg.Drawing instance.
Methods¶
render(node, output_path, *, embed_fonts=False)¶
Renders a node and its descendants to an SVG file.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
Node |
required | Root node |
output_path |
str |
required | Output path for SVG file |
embed_fonts |
bool |
False |
Whether to embed subsetted WOFF2 fonts |
Returns: drawsvg.Drawing — available for further use
render_to_drawing(node, *, embed_fonts=False)¶
Renders a node to a drawsvg.Drawing object without writing any file.
Automatic Layout
This method automatically runs node.layout(), so callers don't need to call it manually (calling it beforehand is harmless).
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
Node |
required | Root node |
embed_fonts |
bool |
False |
Whether to embed subsetted WOFF2 fonts |
Returns: drawsvg.Drawing
render_to_string(node, *, embed_fonts=False)¶
Renders a node to an SVG string (no file I/O).
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
Node |
required | Root node |
embed_fonts |
bool |
False |
Whether to embed subsetted WOFF2 fonts |
Returns: str — SVG XML string
render_png(node, output_path, scale=1.0, *, embed_fonts=False)¶
Renders to SVG first, then converts to PNG via cairosvg.
| Parameter | Type | Default | Description |
|---|---|---|---|
node |
Node |
required | Root node |
output_path |
str |
required | Output path for PNG file |
scale |
float |
1.0 |
Scale factor (2.0 outputs 2x resolution) |
embed_fonts |
bool |
False |
Whether to embed fonts for accurate glyphs |
Dependency Required
PNG output requires cairosvg:
Font Embedding¶
When embed_fonts=True, the Renderer will:
- Traverse the node tree to collect all used fonts and characters
- Extract required glyphs from font files (subsetting)
- Package subsetted fonts as WOFF2 format
- Insert
@font-facerules into the SVG
This makes the generated SVG fully self-contained, independent of fonts installed on the viewing device.
Properties¶
| Property | Type | Description |
|---|---|---|
drawing |
drawsvg.Drawing \| None |
The Drawing object from the most recent render |
Auto-generated API Docs¶
Renderer ¶
Render a laid-out node tree to SVG using drawsvg.
Usage::
renderer = Renderer()
renderer.render(root_node, "output.svg")
renderer.render_png(root_node, "output.png", scale=2)
Source code in src/latticesvg/render/renderer.py
render ¶
Render node and its descendants to an SVG file.
| PARAMETER | DESCRIPTION |
|---|---|
embed_fonts
|
If True, subset and embed all used fonts as WOFF2
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Drawing
|
The drawing instance for further use. |
Source code in src/latticesvg/render/renderer.py
render_to_drawing ¶
Render node to a drawsvg.Drawing without writing any file.
A full layout() pass is performed automatically before
rendering, so callers do not need to call node.layout()
explicitly. (Calling it beforehand is harmless — it will simply
be executed again to guarantee up-to-date geometry.)
| PARAMETER | DESCRIPTION |
|---|---|
embed_fonts
|
If True, subset and embed all used fonts as WOFF2
TYPE:
|
Source code in src/latticesvg/render/renderer.py
render_to_string ¶
Render node to an SVG string (no file I/O).
render_png ¶
render_png(node: 'Node', output_path: str, scale: float = 1.0, *, embed_fonts: bool = False) -> None
Render to SVG, then convert to PNG via cairosvg.
| PARAMETER | DESCRIPTION |
|---|---|
embed_fonts
|
If True, embed subsetted fonts into the intermediate SVG before rasterising. This ensures cairosvg uses the exact same glyphs that were measured during layout.
TYPE:
|