drawsvg
drawsvg copied to clipboard
Drawing.append_def() Generates Use objects in <defs>
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.
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.
Fixed by #134. Published version 2.4.0.