Skip to content

Commit

Permalink
Adding support for <path> elements in SVG <clipPath> elements - fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Dec 29, 2023
1 parent fffa96f commit 52205c0
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,8 @@ in order to get warned about deprecated features used in your code.
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.

## [2.7.8] - Not released yet
### Added
* support for `<path>` elements in SVG `<clipPath>` elements
### Fixed
* When adding a link on a table cell, an extra link was added erroneously on the left. Moreover, now `FPDF._disable_writing()` properly disable link writing.

Expand Down
20 changes: 17 additions & 3 deletions fpdf/svg.py
Expand Up @@ -958,16 +958,30 @@ def build_path(self, path):
@force_nodocument
def build_shape(self, shape):
"""Convert an SVG shape tag into a PDF path object. Necessary to make xref (because ShapeBuilder doesn't have access to this object.)"""
shape_path = getattr(ShapeBuilder, shape_tags[shape.tag])(shape)
shape_builder = getattr(ShapeBuilder, shape_tags[shape.tag])
shape_path = shape_builder(shape)
self.apply_clipping_path(shape_path, shape)
self.update_xref(shape.attrib.get("id"), shape_path)
return shape_path

@force_nodocument
def build_clipping_path(self, shape, clip_id):
clipping_path_shape = getattr(ShapeBuilder, shape_tags[shape.tag])(shape, True)
if shape.tag in shape_tags:
shape_builder = getattr(ShapeBuilder, shape_tags[shape.tag])
clipping_path_shape = shape_builder(shape, True)
elif shape.tag in xmlns_lookup("svg", "path"):
clipping_path_shape = PaintedPath()
apply_styles(clipping_path_shape, shape)
svg_path = shape.attrib.get("d")
if svg_path is not None:
svg_path_converter(clipping_path_shape, svg_path)
else:
LOGGER.warning(
"Ignoring unsupported <clipPath> child tag: <%s> (contributions are welcome to add support for it)",
shape.tag,
)
return
self.update_xref(clip_id, clipping_path_shape)
return clipping_path_shape

@force_nodocument
def apply_clipping_path(self, stylable, svg_element):
Expand Down
Binary file added test/svg/generated_pdf/issue-1076.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions test/svg/parameters.py
Expand Up @@ -775,6 +775,10 @@ def Gs(**kwargs):
svgfile("use-image-def.svg"),
id="Use xlink:href to insert an <image> from <defs>",
),
pytest.param(
svgfile("issue-1076.svg"),
id="<clipPath> containing a <path>",
),
)

svg_path_edge_cases = (
Expand Down
79 changes: 79 additions & 0 deletions test/svg/svg_sources/issue-1076.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 52205c0

Please sign in to comment.