mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	Do not justify a single word
This commit is contained in:
		
							parent
							
								
									3d4119521c
								
							
						
					
					
						commit
						cccc07269a
					
				
							
								
								
									
										
											BIN
										
									
								
								Tests/images/multiline_text_justify_single_word.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Tests/images/multiline_text_justify_single_word.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 2.4 KiB  | 
| 
						 | 
					@ -267,6 +267,18 @@ def test_render_multiline_text_align(
 | 
				
			||||||
    assert_image_similar_tofile(im, f"Tests/images/multiline_text{ext}.png", 0.01)
 | 
					    assert_image_similar_tofile(im, f"Tests/images/multiline_text{ext}.png", 0.01)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_render_multiline_text_align_justify_single_word(
 | 
				
			||||||
 | 
					    font: ImageFont.FreeTypeFont,
 | 
				
			||||||
 | 
					) -> None:
 | 
				
			||||||
 | 
					    im = Image.new("RGB", (185, 65))
 | 
				
			||||||
 | 
					    draw = ImageDraw.Draw(im)
 | 
				
			||||||
 | 
					    draw.multiline_text(
 | 
				
			||||||
 | 
					        (0, 0), "hey you\nyou are awesome\nthis", font=font, align="justify"
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_single_word.png")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:
 | 
					def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:
 | 
				
			||||||
    im = Image.new(mode="RGB", size=(300, 100))
 | 
					    im = Image.new(mode="RGB", size=(300, 100))
 | 
				
			||||||
    draw = ImageDraw.Draw(im)
 | 
					    draw = ImageDraw.Draw(im)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -772,24 +772,26 @@ class ImageDraw:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if align == "justify" and width_difference != 0:
 | 
					            if align == "justify" and width_difference != 0:
 | 
				
			||||||
                words = line.split(" " if isinstance(text, str) else b" ")
 | 
					                words = line.split(" " if isinstance(text, str) else b" ")
 | 
				
			||||||
                word_widths = [
 | 
					                if len(words) > 1:
 | 
				
			||||||
                    self.textlength(
 | 
					                    word_widths = [
 | 
				
			||||||
                        word,
 | 
					                        self.textlength(
 | 
				
			||||||
                        font,
 | 
					                            word,
 | 
				
			||||||
                        direction=direction,
 | 
					                            font,
 | 
				
			||||||
                        features=features,
 | 
					                            direction=direction,
 | 
				
			||||||
                        language=language,
 | 
					                            features=features,
 | 
				
			||||||
                        embedded_color=embedded_color,
 | 
					                            language=language,
 | 
				
			||||||
                    )
 | 
					                            embedded_color=embedded_color,
 | 
				
			||||||
                    for word in words
 | 
					                        )
 | 
				
			||||||
                ]
 | 
					                        for word in words
 | 
				
			||||||
                width_difference = max_width - sum(word_widths)
 | 
					                    ]
 | 
				
			||||||
                for i, word in enumerate(words):
 | 
					                    width_difference = max_width - sum(word_widths)
 | 
				
			||||||
                    parts.append(((left, top), word))
 | 
					                    for i, word in enumerate(words):
 | 
				
			||||||
                    left += word_widths[i] + width_difference / (len(words) - 1)
 | 
					                        parts.append(((left, top), word))
 | 
				
			||||||
            else:
 | 
					                        left += word_widths[i] + width_difference / (len(words) - 1)
 | 
				
			||||||
                parts.append(((left, top), line))
 | 
					                    top += line_spacing
 | 
				
			||||||
 | 
					                    continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            parts.append(((left, top), line))
 | 
				
			||||||
            top += line_spacing
 | 
					            top += line_spacing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return font, anchor, parts
 | 
					        return font, anchor, parts
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user