deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
315 stars 80 forks source link

Fix stranger grey star in label of the graphic library Plotly #250

Closed vinithius2 closed 4 years ago

vinithius2 commented 4 years ago

I want to remove this little star, the problem is with the svg2rlg library and not with Plotly, I did a little test, follow the small script and the results:.

Result using the svg2rlg library to convert SVG to ReportLab: enter image description here

Plotly Result: enter image description here

import os
import pathlib
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg
from kaleido.scopes.plotly import PlotlyScope
import plotly.graph_objects as go

def pie():
    scope = PlotlyScope()
    labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
    values = [4500, 2500, 1053, 500]
    fig = go.Figure(data=[go.Pie(labels=labels, values=values)])

    with open(f"{str(pathlib.Path().absolute())}/test.svg", "wb") as f:
        f.write(scope.transform(fig, format="svg"))

    image = os.path.join(str(pathlib.Path().absolute()), 'test.svg')
    svg = svg2rlg(image)
    renderPDF.drawToFile(svg, "file.pdf")

if __name__ == "__main__":
    pie()

Was install...

Topic open in community: https://community.plotly.com/t/how-do-i-remove-this-star-from-the-legend/43994/2 Stackoverflow: https://stackoverflow.com/questions/63493551/remove-star-from-svg2rlg-with-reportlab

claudep commented 4 years ago

Could you please provide the .svg intermediate file?

vinithius2 commented 4 years ago

svg_and_pdf.zip

@claudep follow this zip file, github not accept svg file.

claudep commented 4 years ago

Thanks. I identified the issue being this rect: <rect class="scrollbar" rx="20" ry="3" width="0" height="0" style="fill: rgb(128, 139, 164); fill-opacity: 1;" x="0" y="0"/> where the rx/ry values are greater than height and width. The specs say: If rx is greater than half of the width of the rectangle, then the browser will consider the value for rx as half of the width of the rectangle.

The other issue being the border rect is displayed even with stroke-width: 0px;.

claudep commented 4 years ago

@replabrobin, sorry I don't find the new issue location for reportlab. There is a remaining issue here that a rect is stroked even with a strokeWidth to 0. Minimal example:

<svg class="main-svg" xmlns="http://www.w3.org/2000/svg" width="700" height="500" style="" viewBox="0 0 700 500">
    <rect class="bg" shape-rendering="crispEdges" style="stroke: rgb(0, 0, 0); fill: rgb(255, 255, 255); fill-opacity: 1; stroke-width: 0;" width="135" height="86" x="10" y="10"/>
</svg>

which gives a <reportlab.graphics.shapes.Rect object> with the following properties:

{'fillColor': Color(1,1,1,1), 'fillOpacity': 1.0, 'strokeColor': Color(0,0,0,1), 'strokeWidth': 0.0, 'strokeLineCap': 0, 'strokeLineJoin': 0, 'strokeMiterLimit': 0, 'strokeDashArray': None, 'strokeOpacity': 1.0, 'x': 10.0, 'y': 10.0, 'width': 135.0, 'height': 86.0, 'rx': 0.0, 'ry': 0.0, '_fillRule': 1}

Do you have any idea why this rectangle is drawn by ReportLab even with strokeWidth to 0?

replabrobin commented 4 years ago

Hi Claude, the strokeWidth==0 case is special in PDF which is why we have it still drawing. Andy Robinson told me that a stroke width of 0 is used by PDF renderers to draw the thinnest possible line in the medium they render to.

Quoting from the PDF 1.7 spec

"A line width of 0 denotes the thinnest line that can be rendered at device resolu- tion: 1 device pixel wide. However, some devices cannot reproduce 1-pixel lines, and on high-resolution devices, they are nearly invisible. Since the results of ren- dering such zero-width lines are device-dependent, their use is not recommend- ed."

To eliminate a stroke in PDF we need to set the stroke colour to None.

claudep commented 4 years ago

That's a pretty precious information to have. Thanks a lot! I think we can rather easily workaround that issue, now.