@@ -702,8 +702,7 @@ def _prepare_multiline_text(
702
702
font_size : float | None ,
703
703
) -> tuple [
704
704
ImageFont .ImageFont | ImageFont .FreeTypeFont | ImageFont .TransposedFont ,
705
- str ,
706
- list [tuple [tuple [float , float ], AnyStr ]],
705
+ list [tuple [tuple [float , float ], str , AnyStr ]],
707
706
]:
708
707
if direction == "ttb" :
709
708
msg = "ttb direction is unsupported for multiline text"
@@ -753,13 +752,7 @@ def _prepare_multiline_text(
753
752
left = xy [0 ]
754
753
width_difference = max_width - widths [idx ]
755
754
756
- # first align left by anchor
757
- if anchor [0 ] == "m" :
758
- left -= width_difference / 2.0
759
- elif anchor [0 ] == "r" :
760
- left -= width_difference
761
-
762
- # then align by align parameter
755
+ # align by align parameter
763
756
if align in ("left" , "justify" ):
764
757
pass
765
758
elif align == "center" :
@@ -773,6 +766,12 @@ def _prepare_multiline_text(
773
766
if align == "justify" and width_difference != 0 and idx != len (lines ) - 1 :
774
767
words = line .split (" " if isinstance (text , str ) else b" " )
775
768
if len (words ) > 1 :
769
+ # align left by anchor
770
+ if anchor [0 ] == "m" :
771
+ left -= max_width / 2.0
772
+ elif anchor [0 ] == "r" :
773
+ left -= max_width
774
+
776
775
word_widths = [
777
776
self .textlength (
778
777
word ,
@@ -784,17 +783,23 @@ def _prepare_multiline_text(
784
783
)
785
784
for word in words
786
785
]
786
+ word_anchor = "l" + anchor [1 ]
787
787
width_difference = max_width - sum (word_widths )
788
788
for i , word in enumerate (words ):
789
- parts .append (((left , top ), word ))
789
+ parts .append (((left , top ), word_anchor , word ))
790
790
left += word_widths [i ] + width_difference / (len (words ) - 1 )
791
791
top += line_spacing
792
792
continue
793
793
794
- parts .append (((left , top ), line ))
794
+ # align left by anchor
795
+ if anchor [0 ] == "m" :
796
+ left -= width_difference / 2.0
797
+ elif anchor [0 ] == "r" :
798
+ left -= width_difference
799
+ parts .append (((left , top ), anchor , line ))
795
800
top += line_spacing
796
801
797
- return font , anchor , parts
802
+ return font , parts
798
803
799
804
def multiline_text (
800
805
self ,
@@ -819,7 +824,7 @@ def multiline_text(
819
824
* ,
820
825
font_size : float | None = None ,
821
826
) -> None :
822
- font , anchor , lines = self ._prepare_multiline_text (
827
+ font , lines = self ._prepare_multiline_text (
823
828
xy ,
824
829
text ,
825
830
font ,
@@ -834,7 +839,7 @@ def multiline_text(
834
839
font_size ,
835
840
)
836
841
837
- for xy , line in lines :
842
+ for xy , anchor , line in lines :
838
843
self .text (
839
844
xy ,
840
845
line ,
@@ -949,7 +954,7 @@ def multiline_textbbox(
949
954
* ,
950
955
font_size : float | None = None ,
951
956
) -> tuple [float , float , float , float ]:
952
- font , anchor , lines = self ._prepare_multiline_text (
957
+ font , lines = self ._prepare_multiline_text (
953
958
xy ,
954
959
text ,
955
960
font ,
@@ -966,7 +971,7 @@ def multiline_textbbox(
966
971
967
972
bbox : tuple [float , float , float , float ] | None = None
968
973
969
- for xy , line in lines :
974
+ for xy , anchor , line in lines :
970
975
bbox_line = self .textbbox (
971
976
xy ,
972
977
line ,
0 commit comments