rendering
pleat.rendering ¶
Renderers for half-edge graphs.
Provides :class:CairoRenderer (raster/vector output via pycairo) and
:class:SvgwriteRenderer (vector output via svgwrite), plus small polygon
helpers (:func:inset_corner, :func:inset_poly) and color utilities.
Rendering ¶
An in-memory rendered picture of a graph: display it, save it, or read its bytes.
Holds both an SVG (vector) and a PNG (raster) snapshot baked at render time.
In Jupyter it auto-displays as inline SVG via the rich-display protocol; in a
script show() opens a matplotlib window; headless it is a graceful no-op.
Store the rendered bytes and pixel dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg_bytes
|
bytes
|
The SVG document as raw bytes. |
required |
png_bytes
|
bytes
|
The PNG image as raw bytes. |
required |
width
|
int
|
Pixel width of the raster snapshot. |
required |
height
|
int
|
Pixel height of the raster snapshot. |
required |
Source code in pleat/rendering.py
save ¶
Write the rendering to disk.
path with no extension writes both path.svg and path.png; a
.svg or .png extension writes just that format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination path; extension selects the format(s). |
required |
Source code in pleat/rendering.py
show ¶
Display the rendering: inline in Jupyter, a matplotlib window in scripts.
In a headless context (no GUI backend, not Jupyter) this is a no-op.
Source code in pleat/rendering.py
CairoRenderer ¶
CairoRenderer(
width: int | None = None,
height: int | None = None,
line_width: float | str = "auto",
vertex_radius: float | None = None,
scale: float | str = "auto",
face_inset: float | None = None,
position_key: str = "pos",
curve_position_key: str = "curve_pos",
face_color_by: object = None,
edge_color_by: object = None,
vertex_color_by: object = None,
face_cmap: object = None,
edge_cmap: object = None,
vertex_cmap: object = None,
)
Render half-edge graphs to PNG / SVG via pycairo.
Faces, edges, and vertices are rendered in that order so edges and
vertex markers sit on top of face fills. Per-element color_key and
line_width attributes override the defaults; missing colour keys fall
back to a translucent blue (faces) or grey (edges).
Configure a renderer. Construction does no I/O; call render_graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int | None
|
Output width in pixels (defaults to height, or 512). |
None
|
height
|
int | None
|
Output height in pixels (defaults to width, or 512). |
None
|
line_width
|
float | str
|
Stroke width in graph units; |
'auto'
|
vertex_radius
|
float | None
|
Marker radius for vertices (defaults to line_width). |
None
|
scale
|
float | str
|
Drawing scale; |
'auto'
|
face_inset
|
float | None
|
Distance to inset face fills from the edges (defaults to line_width). |
None
|
position_key
|
str
|
Vertex attribute holding the 2D position. |
'pos'
|
curve_position_key
|
str
|
Half-edge attribute holding curved-fold polylines
(overrides the straight |
'curve_pos'
|
face_color_by
|
object
|
Auto-colour faces by a preset name ( |
None
|
edge_color_by
|
object
|
Same idea for edges; presets are |
None
|
vertex_color_by
|
object
|
Same idea for vertices; preset is |
None
|
face_cmap
|
object
|
Matplotlib colormap name, |
None
|
edge_cmap
|
object
|
Same for edges; defaults to
:data: |
None
|
vertex_cmap
|
object
|
Same for vertices; defaults to
:data: |
None
|
Source code in pleat/rendering.py
render_face ¶
Draw a single face's filled polygon, honouring face[color_key].
Source code in pleat/rendering.py
set_source_color ¶
Set the current cairo source colour from an RGB/RGBA tuple, ndarray, or #RRGGBB string.
Source code in pleat/rendering.py
render_edge ¶
Draw a single half-edge as a line or a tapered ribbon.
If both endpoints carry a line_width attribute, the edge is drawn
as a filled ribbon of varying width; otherwise it is a stroked polyline.
The curve_pos attribute, if present, replaces the straight segment with a polyline.
Source code in pleat/rendering.py
render_vertex ¶
Draw a vertex as a small disc, picking colour from vertex[color_key] or attribute flags.
Source code in pleat/rendering.py
autoscale ¶
Pick an isotropic scale to fit graph in the surface (origin-centred).
Source code in pleat/rendering.py
autocenterscale ¶
Pick scale and translation to fit graph in the surface, centred on its bounding box.
Source code in pleat/rendering.py
auto_line_width
staticmethod
¶
Default line width for graph: min(min_edge / 2, mean_edge / 10).
Source code in pleat/rendering.py
render_graph ¶
render_graph(
graph: "HalfEdgeGraph",
render_vertices: bool = True,
render_faces: bool = True,
render_edges: bool = True,
for_cutting: bool = False,
) -> "Rendering"
Render the whole graph (faces, edges, vertices) onto the cairo surface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
'HalfEdgeGraph'
|
A :class: |
required |
render_vertices
|
bool
|
Whether to draw vertex markers. |
True
|
render_faces
|
bool
|
Whether to draw face fills. |
True
|
render_edges
|
bool
|
Whether to draw edges. |
True
|
for_cutting
|
bool
|
Reorder edges to minimise pen-up moves on a plotter.
Currently raises :class: |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
'Rendering'
|
class: |
Source code in pleat/rendering.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
SvgwriteRenderer ¶
Vector renderer for cutting plotters: minimises pen-up moves between strokes.
Edges are emitted as a few long polylines rather than many short segments,
so a plotter can complete the cut without unnecessary travel. Faces and
vertices are not implemented; only for_cutting=True is supported.
Source code in pleat/rendering.py
create_drawing ¶
Initialise the underlying svgwrite.Drawing based on the bounding box of pts.
Source code in pleat/rendering.py
render_graph ¶
render_graph(
filename: str,
graph,
render_vertices: bool = False,
render_faces: bool = False,
render_edges: bool = True,
for_cutting: bool = True,
height: float = 30,
unit=svgwrite.cm,
render_interior_and_borders: bool = True,
extra_render_keys: tuple[str, ...] = ("drawing_edge",),
) -> None
Write a plotter-ready SVG of graph's edges to filename.
Also writes companion files <name>_borders.svg and
<name>_interior.svg (when render_interior_and_borders is True),
plus one extra SVG per attribute key in extra_render_keys that
marks half-edges with a truthy attribute of that key.
Source code in pleat/rendering.py
inset_corner ¶
Inset the corner a -> b -> c inwards by dist.
Returns a point inside the angle at b such that its distance to both
edges of the angle equals dist. If the corner is straight (v == w),
returns b unchanged.
Source code in pleat/rendering.py
inset_poly ¶
Inset a polygon's vertices inwards by dist (see :func:inset_corner).
multi_show ¶
multi_show(
graphs: Iterable,
titles: list[str] | None = None,
ncols: int | None = None,
figsize: tuple[float, float] | None = None,
suptitle: str | None = None,
cell_size: float = 4.0,
per_subplot_kwargs: list[dict] | None = None,
**show_kwargs: object
) -> None
Render multiple half-edge graphs side-by-side in a matplotlib grid.
Each graph is rendered to in-memory PNG bytes via G.render(...) and then
displayed as an image in a matplotlib subplot. This is a thin convenience
helper for tutorials and notebook galleries — it does not return the
renderer or its surfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graphs
|
Iterable
|
Iterable of half-edge graphs. |
required |
titles
|
list[str] | None
|
Optional list of titles, one per graph. |
None
|
ncols
|
int | None
|
Number of columns (defaults to |
None
|
figsize
|
tuple[float, float] | None
|
Matplotlib figure size; defaults to |
None
|
suptitle
|
str | None
|
Optional figure-level title. |
None
|
cell_size
|
float
|
Per-cell size in matplotlib inches. |
4.0
|
per_subplot_kwargs
|
list[dict] | None
|
Optional list of per-subplot kwargs dicts (one per graph)
that override |
None
|
**show_kwargs
|
object
|
Forwarded verbatim to each |
{}
|
Source code in pleat/rendering.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |