forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 344
Add basic <symbol> support in SVG #1819
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
Open
Theo1335
wants to merge
15
commits into
py-pdf:master
Choose a base branch
from
Theo1335:svg-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−2
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f1402d6
Automatically format test SVG <symbol>
Theo1335 716b040
Merge branch 'master' into svg-feature
Theo1335 12c4120
erge branch 'master' into svg-feature
Theo1335 a74e2eb
Merge branch 'master' into svg-feature
Theo1335 08c997f
add basic <symbol> and tests
Theo1335 1d70e3e
Merge branch 'master' into svg-feature
Theo1335 4b5ede7
Update changelog for SVG <symbol> support
Theo1335 2da065f
Merge branch 'master' into svg-feature
Theo1335 97b9aa4
scale adjustment for symbols
Theo1335 0440c4e
Merge branch 'master' into svg-feature
Theo1335 84aac5a
Merge branch 'master' into svg-feature
andersonhc 47d508a
Merge branch 'master' into svg-feature
andersonhc 23402a1
Fix <symbol> scaling using transforms instead of setattr
Theo1335 8e6411b
Merge branch 'master' into svg-feature
Theo1335 660b10e
Merge branch 'master' into svg-feature
Theo1335 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1473,6 +1473,8 @@ def handle_defs(self, defs: "Element") -> None: | |||||||
| for child in defs: | ||||||||
| if child.tag in xmlns_lookup("svg", "g"): | ||||||||
| self.build_group(child) | ||||||||
| elif child.tag in xmlns_lookup("svg", "symbol"): | ||||||||
| self.build_symbol(child) | ||||||||
| elif child.tag in xmlns_lookup("svg", "a"): | ||||||||
| # <a> tags aren't supported but we need to recurse into them to | ||||||||
| # render nested elements. | ||||||||
|
|
@@ -1506,6 +1508,20 @@ def handle_defs(self, defs: "Element") -> None: | |||||||
| without_ns(child.tag), | ||||||||
| ) | ||||||||
|
|
||||||||
| @force_nodocument | ||||||||
| def build_symbol(self, symbol: "Element") -> GraphicsContext: | ||||||||
| """Parse <symbol> as reusable content, not rendered directly.""" | ||||||||
| group = self.build_group(symbol) | ||||||||
| viewbox = symbol.attrib.get("viewBox") | ||||||||
| if viewbox: | ||||||||
| parts = viewbox.replace(",", " ").split() | ||||||||
| if len(parts) >= 4: | ||||||||
| vx, vy, vw, vh = (float(p) for p in parts[:4]) | ||||||||
| group.transform = Transform.scaling( | ||||||||
| x=1 / vw, y=1 / vh | ||||||||
| ) @ Transform.translation(x=-vx, y=-vy) | ||||||||
| return group | ||||||||
|
|
||||||||
| # this assumes xrefs only reference already-defined ids. | ||||||||
| # I don't know if this is required by the SVG spec. | ||||||||
| @force_nodocument | ||||||||
|
|
@@ -1525,7 +1541,8 @@ def build_xref(self, xref: "Element") -> GraphicsContext: | |||||||
| raise ValueError(f"use {xref} doesn't contain known xref attribute") | ||||||||
|
|
||||||||
| try: | ||||||||
| pdf_group.add_item(self.cross_references[ref]) | ||||||||
| target = self.cross_references[ref] | ||||||||
| pdf_group.add_item(target) | ||||||||
|
Comment on lines
+1544
to
+1545
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think there's the need for the extra step here |
||||||||
| except KeyError: | ||||||||
| raise ValueError( | ||||||||
| f"use {xref} references nonexistent ref id {ref}" | ||||||||
|
|
@@ -1536,7 +1553,20 @@ def build_xref(self, xref: "Element") -> GraphicsContext: | |||||||
| # > The x and y properties define an additional transformation translate(x,y) | ||||||||
| x, y = float(xref.attrib.get("x", 0)), float(xref.attrib.get("y", 0)) | ||||||||
| pdf_group.transform = Transform.translation(x=x, y=y) | ||||||||
| # Note that we currently do not support "width" & "height" in <use> | ||||||||
| # Note that we currently do not support "width" & "height" with % in <use> | ||||||||
|
|
||||||||
| if "width" in xref.attrib or "height" in xref.attrib: | ||||||||
| w_str = xref.attrib.get("width", "") | ||||||||
| h_str = xref.attrib.get("height", "") | ||||||||
| if "%" not in w_str and "%" not in h_str: | ||||||||
| w = float(w_str) if w_str else 1 | ||||||||
| h = float(h_str) if h_str else 1 | ||||||||
| if pdf_group.transform is None: | ||||||||
| pdf_group.transform = Transform.scaling(x=w, y=h) | ||||||||
| else: | ||||||||
| pdf_group.transform = ( | ||||||||
| Transform.scaling(x=w, y=h) @ pdf_group.transform | ||||||||
| ) | ||||||||
|
|
||||||||
| return pdf_group | ||||||||
|
|
||||||||
|
|
@@ -1567,6 +1597,8 @@ def build_group( | |||||||
| elif child.tag in xmlns_lookup("svg", "style"): | ||||||||
| # Stylesheets already parsed globally. | ||||||||
| continue | ||||||||
| elif child.tag in xmlns_lookup("svg", "symbol"): | ||||||||
| self.build_symbol(child) | ||||||||
| elif child.tag in xmlns_lookup("svg", "g"): | ||||||||
| pdf_group.add_item(self.build_group(child, None, merged_style), False) | ||||||||
| elif child.tag in xmlns_lookup("svg", "a"): | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -246,6 +246,20 @@ def test_missing_xref(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(ValueError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fpdf.svg.SVGObject(svg_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_svg_symbol(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svg_data = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '<svg xmlns="http://www.w3.org/2000/svg" ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'xmlns:xlink="http://www.w3.org/1999/xlink">' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "<defs>" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '<symbol id="rond" width="10" height="10" viewBox="0 0 2 2"><circle cx="1" cy="1" r="1" fill="red"/></symbol>' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "</defs>" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '<use href="#rond" x="10" y="10" width="40" height="40"/>' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "</svg>" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svg = fpdf.svg.SVGObject(svg_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert svg is not None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "#rond" in svg.cross_references | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+249
to
+262
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_svg_conversion_no_transparency(self, tmp_path): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| svg = fpdf.svg.SVGObject.from_file(parameters.svgfile("SVG_logo.svg")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.