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

Drawing.append_def() Generates Use objects in <defs> #110

Open
g-braeunlich opened this issue Oct 17, 2023 · 1 comment
Open

Drawing.append_def() Generates Use objects in <defs> #110

g-braeunlich opened this issue Oct 17, 2023 · 1 comment

Comments

@g-braeunlich
Copy link

I am trying to use drawsvg.Raw to load some svg snippets into <defs> manually and use the defs later in the drawing.
I am facing the following issue:

Example

import drawsvg

d = drawsvg.Drawing(10.0, 10.0)
d.append_def(drawsvg.Circle(0.0, 0.0, 1.0, id="A"))
print(d.as_svg())

Expected output

...
<defs>
<circle cx="0.0" cy="0.0" r="1.0" id="A" />
</defs>
...

Actual output

...
<defs>
<use xlink:href="#A" />
</defs>
...

My workaround:

class DefContainer(drawsvg.DrawingDef):
    def __init__(self, element):
        self.element = element

    def write_svg_element(
        self, id_map, is_duplicate, output_file, lcontext, dry_run, force_dup,
    ):
        self.element.write_svg_element(
            id_map, lambda _: False, output_file, lcontext, dry_run, force_dup
        )

d.append_def(DefContainer(drawsvg.Circle(0.0, 0.0, 1.0, id="A")))

But I guess Drawing.append_def should be changed so the above output which does not make sense cannot happen in the first place.

@cduck
Copy link
Owner

cduck commented Oct 18, 2023

Thanks for the report. That definitely looks like a bug.

My guess is that the SVG generation code (the part that replaces multiple uses of an element with a single def and references) doesn't consider the case where you append to defs but don't use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants