@@ -83,6 +83,14 @@ def check_format(format): # noqa A002
8383 return fmt
8484
8585
86+ def add_text_in_group (dwg , elem , x , y , text , ** kwargs ):
87+ """
88+ Add the text to the elem within a group. This allows text rotations to work smoothly
89+ """
90+ grp = elem .add (dwg .g (transform = f"translate({ x } , { y } )" ))
91+ grp .add (dwg .text (text , ** kwargs ))
92+
93+
8694def draw_tree (
8795 tree ,
8896 width = None ,
@@ -260,14 +268,15 @@ def __init__(
260268 for x , genome_coord in ticks :
261269 delta = 5
262270 axis .add (dwg .line ((x , y - delta ), (x , y + delta ), stroke = "black" ))
263- axis .add (
264- dwg .text (
265- f"{ genome_coord :.2f} " ,
266- (x , y + 20 ),
267- font_size = 14 ,
268- text_anchor = "middle" ,
269- font_weight = "bold" ,
270- )
271+ add_text_in_group (
272+ dwg ,
273+ axis ,
274+ x ,
275+ y + 20 ,
276+ f"{ genome_coord :.2f} " ,
277+ font_size = 14 ,
278+ text_anchor = "middle" ,
279+ font_weight = "bold" ,
271280 )
272281
273282
@@ -339,7 +348,7 @@ def __init__(
339348 # We need to offset the rectangle so that it's centred
340349 self .mutation_attrs [m ] = {
341350 "size" : (6 , 6 ),
342- "transform" : "translate(-3, -3)" ,
351+ "transform" : "translate(-3 -3)" ,
343352 }
344353 if mutation_attrs is not None and m in mutation_attrs :
345354 self .mutation_attrs [m ].update (mutation_attrs [m ])
@@ -367,7 +376,9 @@ def setup_drawing(self):
367376 self .symbols = self .root_group .add (dwg .g (class_ = "symbols" ))
368377 self .nodes = self .symbols .add (dwg .g (class_ = "nodes" ))
369378 self .mutations = self .symbols .add (dwg .g (class_ = "mutations" , fill = "red" ))
370- self .labels = self .root_group .add (dwg .g (class_ = "labels" , font_size = 14 ))
379+ self .labels = self .root_group .add (
380+ dwg .g (class_ = "labels" , font_size = 14 , dominant_baseline = "middle" )
381+ )
371382 self .node_labels = self .labels .add (dwg .g (class_ = "nodes" ))
372383 self .mutation_labels = self .labels .add (
373384 dwg .g (class_ = "mutations" , font_style = "italic" )
@@ -497,12 +508,13 @@ def draw(self):
497508 labels = self .left_labels
498509 # TODO get rid of these manual positioning tweaks and add them
499510 # as offsets the user can access via a transform or something.
500- labels .add (
501- dwg .text (
502- insert = (pu [0 ] + dx , pu [1 ] + dy ),
503- class_ = node_class ,
504- ** self .node_label_attrs [u ],
505- )
511+ add_text_in_group (
512+ dwg ,
513+ labels ,
514+ pu [0 ] + dx ,
515+ pu [1 ] + dy ,
516+ class_ = node_class ,
517+ ** self .node_label_attrs [u ],
506518 )
507519 v = tree .parent (u )
508520 if v != NULL :
@@ -540,12 +552,13 @@ def draw(self):
540552 # TODO get rid of these manual positioning tweaks and add them
541553 # as offsets the user can access via a transform or something.
542554 dy = 4
543- labels .add (
544- dwg .text (
545- insert = (x + dx , y + dy ),
546- class_ = mutation_class ,
547- ** self .mutation_label_attrs [mutation .id ],
548- )
555+ add_text_in_group (
556+ dwg ,
557+ labels ,
558+ x + dx ,
559+ y + dy ,
560+ class_ = mutation_class ,
561+ ** self .mutation_label_attrs [mutation .id ],
549562 )
550563 y -= delta
551564
0 commit comments