PlottingSVG Library
Overview
PlottingSVG is a static, Qt-free library that converts CorePlotting::RenderableScene objects into publication-quality SVG documents. It is the SVG counterpart to PlottingOpenGL — both consume the same CorePlotting scene graph but produce different output: OpenGL draw calls vs. SVG strings.
Key Features
- Symmetric with PlottingOpenGL: Same input (
RenderableScene), different output. Widgets that build a scene for OpenGL can export to SVG without a second scene-building path. - No Qt dependency: Pure C++ string output via
<sstream>and<fstream>. Usable in headless or batch workflows. - Layered documents:
SVGSceneRenderergroups geometry into named<g>layers (rectangles,polylines,glyphs,decorations). - Decoration system: Canvas-space overlays (scalebar, axes) via the
SVGDecorationinterface. - Legacy flat export:
SVGExport.hpppreserves the DataViewer document layout (single element list, no named layers).
Motivation
SVG export previously lived in CorePlotting/Export/SVGPrimitives (removed). OpenGL rendering already had its own library (PlottingOpenGL); embedding SVG in CorePlotting created architectural asymmetry and forced each widget to duplicate document assembly.
PlottingSVG extracts SVG rendering into a dedicated library that any consumer can link.
Architecture
Position in the Stack
CorePlotting (data types, layout, scene building)
├── PlottingOpenGL (RenderableScene → OpenGL draw calls)
└── PlottingSVG (RenderableScene → SVG strings)
Both backends depend on CorePlotting but not on each other. Widgets link PlottingOpenGL for live rendering and optionally PlottingSVG for export.
Directory Structure
src/PlottingSVG/
├── CMakeLists.txt
├── SVGDocument.hpp / .cpp
├── SVGSceneRenderer.hpp / .cpp
├── SVGExport.hpp / .cpp # Legacy DataViewer flat document API
├── SVGUtils.hpp / .cpp
├── Renderers/
│ ├── SVGPolyLineRenderer.hpp / .cpp
│ ├── SVGGlyphRenderer.hpp / .cpp
│ └── SVGRectangleRenderer.hpp / .cpp
└── Decorations/
├── SVGDecoration.hpp
├── SVGAxisRenderer.hpp / .cpp
└── SVGScalebar.hpp / .cpp
Dependencies
PlottingSVG (STATIC)
├── CorePlotting (RenderableScene, batch types)
└── glm::glm (MVP matrix math)
Registered in src/CMakeLists.txt outside ENABLE_UI.
Export APIs
PlottingSVG exposes two paths. New widget integrations should use SVGSceneRenderer.
SVGSceneRenderer (preferred)
Main entry point for layered SVG export. Stores a non-owning pointer to the scene; the scene must remain alive through render(). At render() time the renderer reads scene.view_matrix and scene.projection_matrix and computes MVP = projection * view * batch.model_matrix per batch.
namespace PlottingSVG {
class SVGSceneRenderer {
public:
SVGSceneRenderer(); // default 1920×1080 canvas, white background
void setScene(CorePlotting::RenderableScene const & scene);
void setCanvasSize(int width, int height);
void setBackgroundColor(std::string const & hex_color);
void addDecoration(std::unique_ptr<SVGDecoration> decoration);
void clearDecorations();
[[nodiscard]] std::string render() const;
[[nodiscard]] bool renderToFile(std::filesystem::path const & path) const;
};
}Layer order: rectangles → polylines → glyphs → decorations (only when decoration output is non-empty). If no scene is set, the document still contains XML header, viewBox, and background.
Plot widgets typically copy live OpenGL view/projection matrices onto the cached scene before calling render(), and use the widget’s current pixel dimensions as canvas size.
SVGExport (legacy DataViewer)
Flat document assembly used by DataViewer_Widget/Rendering/SVGExporter:
| Symbol | Role |
|---|---|
SVGExportParams |
canvas_width, canvas_height, background_color |
ScalebarTimeRange |
Visible time window for scalebar pixel mapping |
renderSceneToSVG() |
Flatten batches to element strings (rectangles → polylines → glyphs) |
buildSVGDocument() |
Full SVG 1.1 document with fixed DataViewer <desc> |
createScalebarSVG() |
Bottom-right scalebar via default SVGScalebar |
This path does not use SVGDocument layer groups. See SVGExport.
SVGDocument
Lower-level XML assembler used by SVGSceneRenderer:
SVGDocument doc(width, height);
doc.setBackground("#FFFFFF");
doc.setDescription("…"); // default: "WhiskerToolbox Export"
doc.addElements("glyphs", elements);
return doc.build();Layers are emitted as <g id="layer_name"> groups in insertion order. Fragment strings are written verbatim — callers must supply XML-safe content.
Per-batch renderers
Stateless render() methods on each batch type:
| Renderer | Input | Output |
|---|---|---|
SVGRectangleRenderer |
RenderableRectangleBatch |
<rect> with fill/opacity |
SVGPolyLineRenderer |
RenderablePolyLineBatch |
<polyline> with per-segment stroke |
SVGGlyphRenderer |
RenderableGlyphBatch |
<circle>, <line>, <rect> per glyph type |
All use PlottingSVG::transformVertexToSVG() and colorToSVGHex(). Details: SVGUtils.
Decorations
Canvas-pixel-space overlays implementing SVGDecoration::render(width, height):
| Class | Purpose |
|---|---|
SVGScalebar |
Horizontal bar, end ticks, numeric label; corner/padding/style options |
SVGAxisRenderer |
Spine, uniform ticks, title; AxisPosition::Bottom or Left |
createScalebarSVG() delegates to SVGScalebar. Built-in decorations are available to any caller via SVGSceneRenderer::addDecoration(); plot widgets today export scene geometry only (no decoration overlays). See Decorations.
Coordinate Transform
transformVertexToSVG(vertex, mvp, canvas_width, canvas_height):
ndc = mvp * vertex- Perspective divide when
|ndc.w| > 1e-6 - Map to SVG pixels:
svg_x = width * (ndc.x + 1) / 2,svg_y = height * (1 - ndc.y) / 2
colorToSVGHex() clamps RGB to #RRGGBB (alpha not embedded — use SVG opacity attributes separately).
Widget Integration Pattern
- Cache a
RenderableSceneduring OpenGLrebuildScene()(not only as a local variable). - Implement
exportToSVG()on the OpenGL widget: sync matrices onto the scene, configureSVGSceneRenderer, return the SVG string. - Add Export SVG button on the properties widget (
exportSVGRequested()signal). - Implement
handleExportSVG()on the top-level widget:AppFileDialog→ write file. - Connect signal → slot in the widget registration file.
PlottingSVG::SVGSceneRenderer renderer;
_scene.view_matrix = _view_matrix;
_scene.projection_matrix = _projection_matrix;
renderer.setScene(_scene);
renderer.setCanvasSize(_widget_width, _widget_height);
renderer.setBackgroundColor(background_hex);
return QString::fromStdString(renderer.render());Consuming Widgets
| Widget | API | Notes |
|---|---|---|
| EventPlotWidget | SVGSceneRenderer |
Cached _scene; widget pixel canvas size |
| PSTHWidget | SVGSceneRenderer |
Bar and line render modes |
| HeatmapWidget | SVGSceneRenderer |
Cached heatmap scene |
| ScatterPlotWidget | SVGSceneRenderer |
Cached scatter scene |
| DataViewer_Widget | buildSVGDocument() + createScalebarSVG() |
Legacy flat path via SVGExporter |
Not yet integrated: LinePlotWidget, OnionSkinViewWidget, TemporalProjectionViewWidget, and SpectrogramWidget still lack SVG export. LinePlotWidget uses BatchLineRenderer rather than RenderableScene directly and needs a LineBatchData → RenderablePolyLineBatch conversion helper first.
Testing
Tests live in tests/PlottingSVG/:
| File | Coverage |
|---|---|
SVGUtils.test.cpp |
Coordinate transform, color conversion |
SVGDocument.test.cpp |
XML structure, layers, background |
SVGPolyLineRenderer.test.cpp |
Polyline styling and geometry |
SVGGlyphRenderer.test.cpp |
Glyph types (tick, circle, square, cross) |
SVGRectangleRenderer.test.cpp |
Rectangle fill and opacity |
SVGSceneRenderer.test.cpp |
Full scene layer ordering |
SVGScalebar.test.cpp |
Scalebar positioning and label |
SVGAxisRenderer.test.cpp |
Axis spine, ticks, title |
EventPlotExportIntegration.test.cpp |
End-to-end scene export smoke test |
ctest --preset linux-clang-release --output-on-failure -R "PlottingSVG"Tests use Catch2 and require no OpenGL context or Qt event loop.
See Also
- SVGExport — legacy DataViewer flat document API
- SVGUtils — coordinate and color helpers
- Decorations — scalebar and axis overlays
- CorePlotting Library — upstream scene graph
- Building Custom Plot Widgets — how widgets build
RenderableScene