Closed donkirkby closed 2 years ago
I tried the other shapes in Chrome 97.0.4692.71, Firefox 95.0.1, and the PyCharm 2021.3.2 SVG viewer to see which ones were visible with a thick stroke at size zero.
Circle doesn't appear in any.
<circle fill="#0000ff" stroke="#000000" stroke-width="20"
cx="150" cy="100" r="0"/>
Ellipse doesn't appear in any.
<ellipse fill="#0000ff" stroke="#000000" stroke-width="10"
cx="150" cy="100" rx="0" ry="20"/>
Polygon appears in Chrome and Firefox, not in PyCharm.
<polygon stroke="#000000" stroke-linejoin="round" stroke-linecap="round" stroke-width="20"
points="150,100 150,100 150,100"/>
Path appears in all three.
<path fill="#0000ff" stroke="#000000" stroke-linecap="round" stroke-width="20"
d="M 150,100 h 0"/>
There are lots of possible commands for a path, so it may be a pain to deal with. Here's another example that appears in all three.
<path fill="#0000ff" stroke="#000000" stroke-linecap="round" stroke-width="20"
d="M 10,90 C 10,90 10,90 10,90"/>
Rectangle has the opposite problem! It doesn't appear in SVG viewers, but it does appear when svglib converts it to a PNG.
<rect fill="#0000ff" stroke="#000000" stroke-linecap="round" stroke-width="20"
x="150" y="100" width="0" height="40"/>
So in summary, I plan to make a pull request that handles line, polyline, and polygon. Path and rectangle are just confusing, so I'll leave them out for now.
Thanks for the detailed report. Could you please also evaluate what would be a possible fix in reportlab itself?
I'm pretty sure the fix would be in _renderPM.c
, but I haven't tracked it down yet. If I do track it down, do you know where the reportlab project tracks issues? I couldn't find anything beyond a mailing list.
The reportlab mailing list is the way to report issues and suggest patches.
Thanks for publishing svglib, it's been really helpful in several of my projects.
The Problem
I know this sounds odd, but drawing a line of length zero with rounded end caps is an easy way to draw a dot in an SVG file. Unfortunately, the dot disappears when I convert it to PNG with svglib.
The same problem occurs with a polyline of length zero. I haven't tested it, but there might be similar problems with other shapes of size zero.
Example
Here's a minimal example:
When I open the SVG file in a viewer, I see the dot. In the PNG file, I see nothing.
To see the same problem with a polyline, replace the line element with this:
Workaround
If you change the points in the line just enough that they're not equal, then they'll stop getting filtered out. If the change is small enough, the pixels will look the same as the SVG version.
In the example, change the line's x1 value like this:
Analysis
You could argue that this is a bug in ReportLab, although I can't find any spec on what should happen to lines of length 0. Since svglib is trying to make image files that match the SVG appearance, I suggest you work around ReportLab's behaviour.
I think the best place for a workaround is in
Svg2RlgShapeConverter.convertLine()
andSvg2RlgShapeConverter.convertPolyline()
, as well as the conversion functions for any other shapes with the same problem. I suggest checking whether all the points are identical, and then nudging the first one if they are.I'll try to put together a pull request with this workaround. Let me know if you don't think the fix belongs in svglib, or if you'd prefer a different approach.