feat: optimized SVG export writer (fixes #236)#317
Merged
spe-ciellt merged 1 commit intogerbv:developfrom Mar 5, 2026
Merged
Conversation
SVG export via Cairo's SVG surface produces bloated files because it emits one <path> per Gerber primitive with repeated inline styles and 6-digit coordinate precision. A board with 112k primitives produces a 30MB SVG that SVGO can reduce to 1.8MB. Add a custom SVG writer (export-svg.c) that walks gerbv's data structures directly, producing dramatically smaller output: - CSS class deduplication: unique fill/stroke styles emitted once in <style>, referenced via class="c0". Typical board has <20 unique styles vs 112k inline repetitions. - Reduced precision: 3 decimal places with trailing zero stripping (0.00035mm resolution at 72 DPI, well below visual threshold). - SVG-native elements: <circle>, <rect>, <line>, <polygon> instead of generic <path> where applicable. - <g> grouping with inherited styles for layer/netstate transforms. - All 8 macro primitive types (circle, outline, polygon, moire, thermal, line20/21/22). - Clear polarity workaround using background-color fill, matching the existing vector export fix in draw.c. - Full support for Inkscape layer mode (--svg-layers). The new writer is the default for SVG export. The previous Cairo-based path is available via --svg-cairo CLI flag or the "Use Cairo SVG (legacy)" checkbox in the GUI export dialog. Measured size reduction: 42-69% on test files vs Cairo SVG output. Larger boards with more repeated apertures will see greater reduction. Fixes gerbv#236
This was referenced Mar 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a custom SVG writer (
export-svg.c) that walks gerbv's data structures directly, bypassing Cairo's SVG surface entirely. This produces dramatically smaller SVG output files.Root cause of #236: Cairo's SVG surface emits one
<path>per Gerber primitive with repeated inline styles and 6-digit coordinate precision. A board with 112,000 primitives = 112,000<path>elements with duplicate style attributes, producing ~30MB files.Key optimizations
<style>, referenced viaclass="c0". A typical board has <20 unique styles vs 112k inline repetitions.<circle>,<rect>,<line>,<polygon>instead of generic<path>where applicable.<g>grouping — Nested groups for layer/netstate transforms carry shared attributes.What's covered
draw.c)--svg-layers)--svg-cairoCLI flag or GUI checkboxMeasured results
Larger boards with many repeated aperture flashes (the #236 scenario) will see the greatest reduction, potentially 85-93% as noted in the issue.
Files changed
src/export-svg.csrc/export-svg.hsrc/export-image.csrc/gerbv.huse_cairo_svgflag togerbv_project_tsrc/main.c--svg-cairoCLI flag for legacy outputsrc/callbacks.csrc/CMakeLists.txtexport-svg.cto library sourcesTest plan
--svg-cairoproduces identical output to the previous code path--svg-layers)Requesting feedback on the approach and testing with real-world files that exercise the edge cases.
Fixes #236