Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use fillMode directly instead of _fillRule and reportlab monkeypatch #381

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ docs/_build
env/
.workon
dist/
tests/samples/misc/*-svglib.pdf
tests/samples/misc/*.pdf
tests/samples/others/*.pdf
tests/samples/wikipedia/flags
tests/samples/wikipedia/symbols
tests/samples/W3C_SVG_12_TinyTestSuite.tar.gz
Expand Down
44 changes: 5 additions & 39 deletions svglib/svglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tempfile
import shlex
import shutil
import sys
from collections import defaultdict, namedtuple

from reportlab.pdfbase.pdfmetrics import stringWidth
Expand Down Expand Up @@ -1320,7 +1321,7 @@ def applyStyleOnShape(self, shape, node, only_explicit=False):
mappingN = (
(["fill"], "fillColor", "convertColor", ["black"]),
(["fill-opacity"], "fillOpacity", "convertOpacity", [1]),
(["fill-rule"], "_fillRule", "convertFillRule", ["nonzero"]),
(["fill-rule"], "fillMode", "convertFillRule", ["nonzero"]),
(["stroke"], "strokeColor", "convertColor", ["none"]),
(["stroke-width"], "strokeWidth", "convertLength", ["1"]),
(["stroke-opacity"], "strokeOpacity", "convertOpacity", [1]),
Expand Down Expand Up @@ -1378,7 +1379,9 @@ def applyStyleOnShape(self, shape, node, only_explicit=False):
meth = getattr(ac, func)
setattr(shape, rlgAttr, meth(*svgAttrValues))
except (AttributeError, KeyError, ValueError):
logger.debug("Exception during applyStyleOnShape")
logger.debug("applyStyleOnShape setattr({},{!r},{}(*{!r})) caused {} exception".format(
shape.__class__.__name__,rlgAttr,meth.__name__,svgAttrValues,
sys.exc_info()[0].__class__.__name__))
if getattr(shape, 'fillOpacity', None) is not None and shape.fillColor:
shape.fillColor.alpha = shape.fillOpacity
if getattr(shape, 'strokeWidth', None) == 0:
Expand Down Expand Up @@ -1507,40 +1510,3 @@ def copy_shape_properties(source_shape, dest_shape):
setattr(dest_shape, prop, val)
except AttributeError:
pass


def monkeypatch_reportlab():
"""
https://bitbucket.org/rptlab/reportlab/issues/95/
ReportLab always use 'Even-Odd' filling mode for paths, this patch forces
RL to honor the path fill rule mode (possibly 'Non-Zero Winding') instead.
"""
from reportlab.pdfgen.canvas import Canvas
from reportlab.graphics import shapes

original_renderPath = shapes._renderPath

def patchedRenderPath(path, drawFuncs, **kwargs):
# Patched method to transfer fillRule from Path to PDFPathObject
# Get back from bound method to instance
try:
drawFuncs[0].__self__.fillMode = path._fillRule
except AttributeError:
pass
return original_renderPath(path, drawFuncs, **kwargs)
shapes._renderPath = patchedRenderPath

original_drawPath = Canvas.drawPath

def patchedDrawPath(self, path, **kwargs):
current = self._fillMode
if hasattr(path, 'fillMode'):
self._fillMode = path.fillMode
else:
self._fillMode = FILL_NON_ZERO
original_drawPath(self, path, **kwargs)
self._fillMode = current
Canvas.drawPath = patchedDrawPath


monkeypatch_reportlab()
42 changes: 42 additions & 0 deletions tests/samples/misc/fill-rule-evenodd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions tests/samples/misc/fill-rule-nonzero.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def test_fillrule(self):
node = minimal_svg_node('<polygon fill-rule="evenodd"/>')
poly = Polygon()
converter.applyStyleOnShape(poly, node)
assert poly._fillRule == FILL_EVEN_ODD
assert poly.fillMode == FILL_EVEN_ODD

def test_stroke(self):
converter = svglib.Svg2RlgShapeConverter(None)
Expand Down