PNG export stops when an SVG icon uses a valid SVG feature that the built-in renderer does not accept
FYI this was researched with Claude and I am posting it myself after verifying it.
Summary
In D2 v0.9.0, PNG, PDF, PPTX and GIF exports use the built-in renderer. This renderer imports each SVG icon with d2svgimport. If an icon contains one element, attribute or value that the importer does not accept, the full export stops with an error. The SVG export of the same diagram is correct.
The rejected icons are valid SVG. Browsers show them correctly. Before v0.9.0, PNG export used a browser. Many icons from common icon sets use these features.
Environment
- d2 v0.9.0 (
d5a5174), Linux amd64.
Steps to reproduce
Save this file as
icon.svg:<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10" width="10" height="10"> <defs> <linearGradient id="g"> <stop offset="0" stop-color="red"/> <stop offset="1" stop-color="blue"/> </linearGradient> </defs> <path d="M0 0H10V10H0Z" fill="url(#g)"/> </svg>Save this file as
x.d2:x: {shape: image; icon: ./icon.svg}Run
d2 x.d2 x.svg. The export is correct.Run
d2 x.d2 x.png.
Actual result
err: failed to compile x.d2: scene: shape "x" SVG asset: d2svgimport: ./icon.svg: element <linearGradient> is missing required attribute "x1"d2 writes no PNG file.
Expected result
d2 writes x.png. The gradient uses the initial values from the SVG specification: gradientUnits="objectBoundingBox", x1="0%", y1="0%", x2="100%", y2="0%".
Other valid SVG that stops the export
Each row changes one thing in the SVG above. The SVG export and browsers show each file correctly.
| SVG content | Error from d2 v0.9.0 |
|---|---|
gradientUnits="objectBoundingBox" and x1, y1, x2, y2 on <linearGradient> |
element <linearGradient> supports only gradientUnits="userSpaceOnUse" |
<linearGradient> outside <defs> |
element <linearGradient> must be a direct child of <defs> |
<radialGradient> |
unsupported paint server <radialGradient> |
<mask> |
unknown element <mask> |
<filter> |
unknown element <filter> |
enable-background on <path> |
element <path> has unsupported attribute "enable-background" |
style="isolation:isolate" |
element <path> has unsupported style property "isolation" |
style="mix-blend-mode:normal" on <g> |
element <g> has unsupported style property "mix-blend-mode" |
An empty <dc:title> in <metadata> (Inkscape writes this) |
element <title> uses unsupported namespace or metadata name |
version="1.2" on the root <svg> |
root <svg> has unsupported version |
<clipPath> without an id |
element <clipPath> must declare an id |
A space after base64, in a data URI |
element <image> has malformed canonical base64 data |
| Embedded rasters with more than 128 × 128 pixels in total | embedded raster decoded bytes exceed limit 65536 |
One fill-rule="evenodd" path with 1200 edges, at the default image size |
d2raster: even-odd clip work 265420800 exceeds limit 250000000 |
The even-odd limit applies to the full export. Thus, the result also changes with the icon size, --scale and the other icons in the diagram.
Related: style.multiple on shape: image
x: {shape: image; icon: ./icon-that-imports.svg; style.multiple: true}PNG export stops with shape "x" uses unsupported multiple for shape type image. The SVG export ignores multiple for image shapes. The layout keeps 10 px of space for it.
See attached zip file for extra SVG files not exportable to PNG under v0.9.0
Source: terrastruct/d2