Node Types API¶
This page provides detailed documentation for all node type constructors, properties, and methods.
Rect¶
An axis-aligned rectangle with top-left origin (y-axis points down).
from latticesvg import Rect
r = Rect(x=10, y=20, width=100, height=50)
print(r.right) # 110.0
print(r.bottom) # 70.0
r2 = r.copy()
| Property | Type | Description |
|---|---|---|
x |
float |
Top-left x coordinate (default 0.0) |
y |
float |
Top-left y coordinate (default 0.0) |
width |
float |
Width (default 0.0) |
height |
float |
Height (default 0.0) |
right |
float |
Read-only, equals x + width |
bottom |
float |
Read-only, equals y + height |
| Method | Returns | Description |
|---|---|---|
copy() |
Rect |
Returns a copy |
LayoutConstraints¶
Constraints passed from parent to child during layout.
from latticesvg import LayoutConstraints
c = LayoutConstraints(available_width=800, available_height=600)
| Property | Type | Default | Description |
|---|---|---|---|
available_width |
float \| None |
None |
Available width |
available_height |
float \| None |
None |
Available height |
Node (Base Class)¶
Abstract base for all layoutable elements. Subclasses must implement measure() and layout().
from latticesvg import Node
node = Node(style={"width": 100, "height": 50, "background": "#f0f0f0"})
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
style |
dict[str, Any] \| None |
None |
CSS style properties dict |
parent |
Node \| None |
None |
Parent node (usually set automatically by add()) |
Properties¶
| Property | Type | Description |
|---|---|---|
style |
ComputedStyle |
Computed style object |
parent |
Node \| None |
Parent node |
children |
list[Node] |
Child node list |
border_box |
Rect |
Border box rect after layout |
padding_box |
Rect |
Padding box rect after layout |
content_box |
Rect |
Content box rect after layout |
placement |
PlacementHint |
Grid placement info |
Methods¶
add(child, *, row=None, col=None, row_span=1, col_span=1, area=None)¶
Append a child node and optionally set grid placement.
| Parameter | Type | Description |
|---|---|---|
child |
Node |
Child node to add |
row |
int \| None |
Row start position (1-based CSS Grid line number) |
col |
int \| None |
Column start position (1-based CSS Grid line number) |
row_span |
int |
Number of rows to span (default 1) |
col_span |
int |
Number of columns to span (default 1) |
area |
str \| None |
Named grid area |
Returns: The added child node (enables chaining)
measure(constraints) → (min_w, max_w, intrinsic_h)¶
Returns the node's minimum content width, maximum content width, and intrinsic height. Called by the grid solver.
layout(constraints)¶
Computes border_box, padding_box, and content_box. Must be implemented by subclasses.
GridContainer¶
A CSS Grid layout container node that arranges children using grid layout.
from latticesvg import GridContainer
grid = GridContainer(style={
"grid-template-columns": "200px 1fr",
"grid-template-rows": "auto auto",
"gap": 10,
"width": 600,
"padding": 20,
})
Constructor Parameters¶
Inherits from Node, accepts style and parent. display is automatically set to "grid".
Methods¶
layout(constraints=None, available_width=None, available_height=None)¶
Runs the full layout computation. As a root node, you can pass the available width directly:
If available_width is not provided, the width style property is used, or defaults to 800.
measure(constraints) → (min_w, max_w, intrinsic_h)¶
Used for nested grid size calculations. Results are cached.
TextNode¶
Text node supporting automatic line wrapping, rich text markup, and vertical typesetting.
from latticesvg import TextNode
# Plain text
text = TextNode("Hello, World!", style={"font-size": 24, "color": "#333"})
# HTML rich text
rich = TextNode("<b>Bold</b> and <i>italic</i>", markup="html")
# Markdown rich text
md = TextNode("**Bold** and *italic*", markup="markdown")
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
str |
required | Text content |
style |
dict \| None |
None |
Style properties |
parent |
Node \| None |
None |
Parent node |
markup |
str |
"none" |
Markup mode: "none", "html", "markdown" |
Properties¶
| Property | Type | Description |
|---|---|---|
text |
str |
Raw text content |
markup |
str |
Markup mode |
lines |
list[Line] |
Lines after layout (plain text mode) |
Key Style Properties¶
TextNode responds to these style properties (see CSS Properties Reference):
- Font:
font-family,font-size,font-weight,font-style - Typography:
text-align,line-height,letter-spacing,word-spacing - Wrapping:
white-space,overflow-wrap,hyphens,lang - Overflow:
overflow,text-overflow - Vertical:
writing-mode,text-orientation,text-combine-upright
ImageNode¶
Image node supporting multiple image sources and object-fit scaling.
from latticesvg import ImageNode
# File path
img = ImageNode("photo.png", style={"width": 200, "height": 150})
# URL
img = ImageNode("https://example.com/image.png")
# bytes
img = ImageNode(raw_bytes, object_fit="contain")
# PIL Image
from PIL import Image
img = ImageNode(Image.open("photo.png"), object_fit="cover")
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
src |
str \| bytes \| PIL.Image |
required | Image source (path/URL/bytes/PIL) |
style |
dict \| None |
None |
Style properties |
parent |
Node \| None |
None |
Parent node |
object_fit |
str \| None |
None |
Scaling mode |
object-fit Modes¶
| Value | Description |
|---|---|
"fill" |
Stretch to fill (default, may distort) |
"contain" |
Scale proportionally, fully contained |
"cover" |
Scale proportionally, fully covering |
"none" |
Original size, no scaling |
Properties¶
| Property | Type | Description |
|---|---|---|
intrinsic_width |
float |
Image intrinsic width (read-only, lazily loaded) |
intrinsic_height |
float |
Image intrinsic height (read-only, lazily loaded) |
SVGNode¶
A node for embedding external SVG content.
from latticesvg import SVGNode
# SVG string
svg = SVGNode('<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40"/></svg>')
# File
svg = SVGNode("icon.svg", is_file=True)
# URL
svg = SVGNode("https://example.com/icon.svg")
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
svg |
str |
required | SVG content (string/path/URL) |
style |
dict \| None |
None |
Style properties (keyword-only) |
parent |
Node \| None |
None |
Parent node (keyword-only) |
is_file |
bool |
False |
Whether to treat as file path (keyword-only) |
Intrinsic size is parsed from the viewBox or width/height attributes.
MplNode¶
Matplotlib figure embedding node.
import matplotlib.pyplot as plt
from latticesvg import MplNode
fig, ax = plt.subplots(figsize=(4, 3))
ax.plot([1, 2, 3], [1, 4, 9])
node = MplNode(fig, style={"padding": 10})
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
figure |
matplotlib.figure.Figure |
required | Matplotlib figure object |
style |
dict \| None |
None |
Style properties |
parent |
Node \| None |
None |
Parent node |
Important Notes
- All Matplotlib customization should be done before creating the node
- The figure is automatically resized during layout to match allocated space
- SVG coordinates use 72 DPI uniformly
MathNode¶
LaTeX formula rendering node based on QuickJax (MathJax v4).
from latticesvg import MathNode
# display mode (standalone formula)
formula = MathNode(r"E = mc^2", style={"font-size": 24})
# inline mode
inline = MathNode(r"\alpha + \beta", display=False)
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
latex |
str |
required | LaTeX math expression |
style |
dict \| None |
None |
Style properties (keyword-only) |
backend |
str \| None |
None |
Backend name, None uses default (keyword-only) |
display |
bool |
True |
Whether to use display mode (keyword-only) |
parent |
Node \| None |
None |
Parent node (keyword-only) |
Properties¶
| Property | Type | Description |
|---|---|---|
latex |
str |
LaTeX source |
display |
bool |
display/inline mode |
scale_x |
float |
Horizontal scale factor after layout |
scale_y |
float |
Vertical scale factor after layout |
Auto-generated API Docs¶
nodes ¶
Node ¶
Abstract base for all layoutable elements.
Subclasses must implement :meth:measure and :meth:layout.
Source code in src/latticesvg/nodes/base.py
add ¶
add(child: 'Node', *, row: Optional[int] = None, col: Optional[int] = None, row_span: int = 1, col_span: int = 1, area: Optional[str] = None) -> 'Node'
Append child to this node and optionally set grid placement.
row and col use 1-based line numbers consistent with CSS Grid.
area places the child in a named grid area defined by
grid-template-areas on the container.
Source code in src/latticesvg/nodes/base.py
measure ¶
Return (min_content_width, max_content_width, intrinsic_height).
Called by the grid solver to determine how much space this node needs. Default implementation returns zero sizes.
Source code in src/latticesvg/nodes/base.py
layout ¶
Compute border_box, padding_box, and content_box.
Must be implemented by subclasses.
Rect
dataclass
¶
An axis-aligned rectangle (top-left origin, y-axis points down).
LayoutConstraints
dataclass
¶
LayoutConstraints(available_width: Optional[float] = None, available_height: Optional[float] = None)
Constraints passed from parent to child during layout.
GridContainer ¶
Bases: Node
A container node that arranges its children using CSS Grid layout.
Delegates the heavy-lifting (track sizing, placement, alignment)
to :class:~latticesvg.layout.grid_solver.GridSolver.
Source code in src/latticesvg/nodes/grid.py
layout ¶
layout(constraints: Optional[LayoutConstraints] = None, available_width: Optional[float] = None, available_height: Optional[float] = None) -> None
Run the full layout pass for this container and all descendants.
Can be called directly as grid.layout(available_width=800).
Source code in src/latticesvg/nodes/grid.py
TextNode ¶
TextNode(text: str, style: Optional[Dict[str, Any]] = None, parent: Optional[Node] = None, markup: str = 'none')
Bases: Node
A node that displays text content.
The text is measured using FreeType (or Pillow fallback) and
automatically wrapped according to the white-space property.
When markup is "html" or "markdown", inline tags/syntax
are parsed to produce styled spans (bold, italic, colour, …).
Source code in src/latticesvg/nodes/text.py
measure ¶
Return (min_content_width, max_content_width, intrinsic_height).
For vertical/sideways writing modes the axes are swapped internally so that the grid solver always receives values in the physical (horizontal/vertical) coordinate system.
The result is cached because it depends only on text content and style properties, not on constraints.
Source code in src/latticesvg/nodes/text.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
ImageNode ¶
ImageNode(src: Union[str, bytes, Any], style: Optional[Dict[str, Any]] = None, parent: Optional[Node] = None, object_fit: Optional[str] = None)
Bases: Node
Node that displays a raster image (PNG, JPEG, etc.).
The image's intrinsic size is read lazily via Pillow. The
object-fit property controls how the image is scaled within the
allocated space.
| PARAMETER | DESCRIPTION |
|---|---|
src
|
Image source: - File path (str) - URL starting with 'http://' or 'https://' (str) - Raw image bytes (bytes) - PIL Image object
TYPE:
|
style
|
Style properties
TYPE:
|
parent
|
Parent node
TYPE:
|
object_fit
|
Object-fit mode ('fill', 'contain', 'cover', 'none')
TYPE:
|
Source code in src/latticesvg/nodes/image.py
get_base64 ¶
Return the image as a base64-encoded data URI.
Source code in src/latticesvg/nodes/image.py
MplNode ¶
MplNode(figure: Any, style: Optional[Dict[str, Any]] = None, parent: Optional[Node] = None, auto_mpl_font: bool = True, tight_layout: bool = True)
Bases: Node
Node that embeds a Matplotlib figure as an SVG fragment.
The figure is rendered to an in-memory SVG buffer during the render phase, so all Matplotlib customisation should be done before creating this node.
| PARAMETER | DESCRIPTION |
|---|---|
figure
|
A
TYPE:
|
style
|
Optional CSS-like style dictionary.
TYPE:
|
parent
|
Optional parent node.
TYPE:
|
auto_mpl_font
|
When
TYPE:
|
tight_layout
|
When
TYPE:
|
Source code in src/latticesvg/nodes/mpl.py
get_svg_fragment ¶
Export the figure to an SVG string (cached).
When auto_mpl_font is enabled the inherited font-family
CSS property is translated into matplotlib rcParams so that
text inside the figure uses matching fonts. svg.fonttype is
always set to "path" so that glyphs are converted to vector
paths for cross-platform consistency.
Source code in src/latticesvg/nodes/mpl.py
SVGNode ¶
SVGNode(svg: str, *, style: Optional[Dict[str, Any]] = None, parent: Optional[Node] = None, is_file: bool = False)
Bases: Node
Node that embeds raw SVG content (from a string or file).
The SVG's viewBox or width/height attributes are used to
determine its intrinsic size. During rendering the content is scaled
to fit the allocated space.
| PARAMETER | DESCRIPTION |
|---|---|
svg
|
SVG source: - File path (when is_file=True) - URL starting with 'http://' or 'https://' - Raw SVG string
TYPE:
|
style
|
Style properties
TYPE:
|
parent
|
Parent node
TYPE:
|
is_file
|
If True, treat svg as a file path. Default False.
TYPE:
|
Source code in src/latticesvg/nodes/svg.py
get_inner_svg ¶
Return SVG content with the outer <svg> wrapper stripped.
This is used for embedding inside another SVG document.
Presentation attributes (fill, stroke, etc.) from the outer
<svg> element are preserved by wrapping inner content in
a <g> that carries those attributes forward.
Source code in src/latticesvg/nodes/svg.py
MathNode ¶
MathNode(latex: str, *, style: Optional[Dict[str, Any]] = None, backend: Optional[str] = None, display: bool = True, parent: Optional[Node] = None)
Bases: Node
Node that renders a LaTeX math expression to SVG.
By default the QuickJax backend (in-process MathJax v4) is used.
A different backend can be selected per-node or globally via
:func:latticesvg.math.set_default_backend.
Usage::
formula = MathNode(r"E = mc^2", style={"font-size": "20px"})