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