Core Concepts¶
Architecture Overview¶
The LatticeSVG rendering pipeline has three stages:
Style Parsing Layout Solving SVG Rendering
┌──────────┐ ┌──────────┐ ┌──────────┐
│ CSS dicts │ → │ GridSolver│ → │ Renderer │ → SVG / PNG
│ → computed│ │ → box model│ │ → drawsvg │
└──────────┘ └──────────┘ └──────────┘
Node Tree¶
LatticeSVG uses a node tree to describe document structure. Every node is a subclass of Node:
GridContainer— Container node, arranges children using CSS GridTextNode— Leaf node, displays text contentImageNode— Leaf node, embeds raster imagesSVGNode— Leaf node, embeds SVG contentMplNode— Leaf node, embeds Matplotlib figuresMathNode— Leaf node, renders LaTeX formulas
# Node tree example
page = GridContainer(style={...}) # root container
├── TextNode("Title") # child 1
├── GridContainer(style={...}) # nested container
│ ├── ImageNode("photo.png") # grandchild
│ └── TextNode("Caption")
└── TextNode("Footer") # child 3
Style System¶
Declarative Styles¶
All styles are passed as Python dicts with CSS-compatible property names:
style = {
"width": "400px",
"padding": "16px",
"font-size": "14px",
"color": "#333333",
"background-color": "#ffffff",
"grid-template-columns": ["1fr", "1fr"],
}
Style Inheritance¶
Consistent with CSS, some properties inherit from parent nodes (e.g., color, font-size, font-family), while box model properties (e.g., padding, margin) do not.
ComputedStyle¶
Each node holds a ComputedStyle object responsible for:
- Parsing raw values — Converting
"16px"to16.0 - Expanding shorthands — Expanding
"padding": "10px 20px"to four directions - Inheritance — Inheritable properties taken from parent
- Defaults — Unspecified properties use registry defaults
Layout Algorithm¶
CSS Grid Solving¶
GridSolver implements the complete CSS Grid Level 1 layout algorithm:
- Track template parsing — Parse
grid-template-columns/grid-template-rows - Item placement — Position items via
row/col/areaor auto-placement - Track sizing — Handle fixed, percentage,
fr,auto,min-content,max-content,minmax() - Alignment — Apply
justify-items,align-items,justify-self,align-self - Box model — Compute
border-box,padding-box,content-boxfor each node
Box Model¶
After layout, each node has three rectangles (Rect):
┌─────────────────────────── border-box ──┐
│ border │
│ ┌──────────────────── padding-box ──┐ │
│ │ padding │ │
│ │ ┌──────────── content-box ────┐ │ │
│ │ │ │ │ │
│ │ │ content area │ │ │
│ │ │ │ │ │
│ │ └─────────────────────────────┘ │ │
│ └───────────────────────────────────┘ │
└─────────────────────────────────────────┘
The default box-sizing is border-box, consistent with modern CSS practice.
Rendering Pipeline¶
Renderer traverses the laid-out node tree, generating SVG elements for each node:
- Background — Solid colors, linear gradients, radial gradients
- Borders — Independent color/width/style per side, dashed and dotted support
- Border radius — Independent radius per corner
- Content — Text glyphs, embedded images, SVG fragments, math formulas
- Visual effects — Shadows, opacity, transforms, filters, clip-path
Output formats:
| Method | Output | Notes |
|---|---|---|
render(node, path) |
.svg file |
Also returns Drawing object |
render_to_drawing(node) |
Drawing object |
In-memory SVG |
render_to_string(node) |
SVG string | For HTML embedding |
render_png(node, path) |
.png file |
Requires cairosvg |
Text Engine¶
LatticeSVG's text engine is built on FreeType:
- Precise measurement — Glyph-level width, height, baseline offset
- Automatic line breaking — Greedy algorithm for text wrapping
- CJK support — Character-level break opportunities for CJK scripts
- Rich text — HTML / Markdown markup →
TextSpan→ multi-style composition - Vertical text —
writing-mode: vertical-rlsupport - Font fallback — Multi-family font chain lookup
- Font embedding — WOFF2 subsetting and embedding in SVG
Next Steps¶
- 📐 Grid Layout Tutorial — Practice CSS Grid layout patterns
- 📖 CSS Properties Reference — Browse all 63 supported properties
- 🔧 API Reference — Complete class and method documentation