@@ -708,92 +708,94 @@ def _prepare_multiline_text(
708
708
str ,
709
709
list [tuple [tuple [float , float ], AnyStr ]],
710
710
]:
711
- if direction == "ttb" :
712
- msg = "ttb direction is unsupported for multiline text"
713
- raise ValueError (msg )
714
-
715
711
if anchor is None :
716
- anchor = "la"
712
+ anchor = "lt" if direction == "ttb" else " la"
717
713
elif len (anchor ) != 2 :
718
714
msg = "anchor must be a 2 character string"
719
715
raise ValueError (msg )
720
- elif anchor [1 ] in "tb" :
716
+ elif anchor [1 ] in "tb" and direction != "ttb" :
721
717
msg = "anchor not supported for multiline text"
722
718
raise ValueError (msg )
723
719
724
720
if font is None :
725
721
font = self ._getfont (font_size )
726
722
727
- widths = []
728
- max_width : float = 0
729
723
lines = text .split ("\n " if isinstance (text , str ) else b"\n " )
730
724
line_spacing = (
731
725
self .textbbox ((0 , 0 ), "A" , font , stroke_width = stroke_width )[3 ]
732
726
+ stroke_width
733
727
+ spacing
734
728
)
735
729
736
- for line in lines :
737
- line_width = self .textlength (
738
- line ,
739
- font ,
740
- direction = direction ,
741
- features = features ,
742
- language = language ,
743
- embedded_color = embedded_color ,
744
- )
745
- widths .append (line_width )
746
- max_width = max (max_width , line_width )
747
-
748
730
top = xy [1 ]
749
- if anchor [1 ] == "m" :
750
- top -= (len (lines ) - 1 ) * line_spacing / 2.0
751
- elif anchor [1 ] == "d" :
752
- top -= (len (lines ) - 1 ) * line_spacing
753
-
754
731
parts = []
755
- for idx , line in enumerate ( lines ) :
732
+ if direction == "ttb" :
756
733
left = xy [0 ]
757
- width_difference = max_width - widths [idx ]
758
-
759
- # first align left by anchor
760
- if anchor [0 ] == "m" :
761
- left -= width_difference / 2.0
762
- elif anchor [0 ] == "r" :
763
- left -= width_difference
764
-
765
- # then align by align parameter
766
- if align in ("left" , "justify" ):
767
- pass
768
- elif align == "center" :
769
- left += width_difference / 2.0
770
- elif align == "right" :
771
- left += width_difference
772
- else :
773
- msg = 'align must be "left", "center", "right" or "justify"'
774
- raise ValueError (msg )
775
-
776
- if align == "justify" and width_difference != 0 :
777
- words = line .split (" " if isinstance (text , str ) else b" " )
778
- word_widths = [
779
- self .textlength (
780
- word ,
781
- font ,
782
- direction = direction ,
783
- features = features ,
784
- language = language ,
785
- embedded_color = embedded_color ,
786
- )
787
- for word in words
788
- ]
789
- width_difference = max_width - sum (word_widths )
790
- for i , word in enumerate (words ):
791
- parts .append (((left , top ), word ))
792
- left += word_widths [i ] + width_difference / (len (words ) - 1 )
793
- else :
734
+ for line in lines :
794
735
parts .append (((left , top ), line ))
736
+ left += line_spacing
737
+ else :
738
+ widths = []
739
+ max_width : float = 0
740
+ for line in lines :
741
+ line_width = self .textlength (
742
+ line ,
743
+ font ,
744
+ direction = direction ,
745
+ features = features ,
746
+ language = language ,
747
+ embedded_color = embedded_color ,
748
+ )
749
+ widths .append (line_width )
750
+ max_width = max (max_width , line_width )
751
+
752
+ if anchor [1 ] == "m" :
753
+ top -= (len (lines ) - 1 ) * line_spacing / 2.0
754
+ elif anchor [1 ] == "d" :
755
+ top -= (len (lines ) - 1 ) * line_spacing
756
+
757
+ for idx , line in enumerate (lines ):
758
+ left = xy [0 ]
759
+ width_difference = max_width - widths [idx ]
760
+
761
+ # first align left by anchor
762
+ if anchor [0 ] == "m" :
763
+ left -= width_difference / 2.0
764
+ elif anchor [0 ] == "r" :
765
+ left -= width_difference
766
+
767
+ # then align by align parameter
768
+ if align in ("left" , "justify" ):
769
+ pass
770
+ elif align == "center" :
771
+ left += width_difference / 2.0
772
+ elif align == "right" :
773
+ left += width_difference
774
+ else :
775
+ msg = 'align must be "left", "center", "right" or "justify"'
776
+ raise ValueError (msg )
777
+
778
+ if align == "justify" and width_difference != 0 :
779
+ words = line .split (" " if isinstance (text , str ) else b" " )
780
+ word_widths = [
781
+ self .textlength (
782
+ word ,
783
+ font ,
784
+ direction = direction ,
785
+ features = features ,
786
+ language = language ,
787
+ embedded_color = embedded_color ,
788
+ )
789
+ for word in words
790
+ ]
791
+ width_difference = max_width - sum (word_widths )
792
+ for i , word in enumerate (words ):
793
+ parts .append (((left , top ), word ))
794
+ left += word_widths [i ] + width_difference / (len (words ) - 1 )
795
+ else :
796
+ parts .append (((left , top ), line ))
795
797
796
- top += line_spacing
798
+ top += line_spacing
797
799
798
800
return font , anchor , parts
799
801
0 commit comments