Skip to content

Commit 47ef0d5

Browse files
committed
Change when preamble is inserted
Put a placeholder in, then actually place it on `draw`. This allows the preamble to be (re)set after init but before actually drawing, which can be useful for complex composite drawings.
1 parent dea4e37 commit 47ef0d5

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

python/tskit/drawing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def tostring(self):
134134

135135

136136
class Drawing:
137-
def __init__(self, size=None, preamble=None, **kwargs):
137+
def __init__(self, size=None, **kwargs):
138138
kwargs = {
139139
"version": "1.1",
140140
"xmlns": "http://www.w3.org/2000/svg",
@@ -148,8 +148,7 @@ def __init__(self, size=None, preamble=None, **kwargs):
148148
kwargs["height"] = size[1]
149149

150150
self.root = Element("svg", **kwargs)
151-
if preamble is not None:
152-
self.root.add(preamble)
151+
self.root.add("") # First root elem is a blank preamble
153152
self.defs = Element("defs")
154153
self.root.add(self.defs)
155154

@@ -881,8 +880,9 @@ def __init__(
881880
root_svg_attributes = {}
882881
if canvas_size is None:
883882
canvas_size = size
884-
dwg = Drawing(size=canvas_size, preamble=None, **root_svg_attributes)
883+
dwg = Drawing(size=canvas_size, **root_svg_attributes)
885884

885+
self.preamble = preamble
886886
self.image_size = size
887887
self.plotbox = Plotbox(size)
888888
self.root_groups = {}
@@ -893,6 +893,8 @@ def __init__(
893893
self.drawing = dwg
894894

895895
def draw(self, path=None):
896+
if self.preamble is not None:
897+
self.drawing.root.children[0] = self.preamble
896898
output = self.drawing.tostring()
897899
if path is not None:
898900
# TODO remove the 'pretty' when we are done debugging this.

0 commit comments

Comments
 (0)