mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 16:07:30 +03:00 
			
		
		
		
	Added "justify" align for multiline text
This commit is contained in:
		
							parent
							
								
									7093de46a7
								
							
						
					
					
						commit
						10eaff8ac7
					
				
							
								
								
									
										
											BIN
										
									
								
								Tests/images/multiline_text_justify.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Tests/images/multiline_text_justify.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 3.2 KiB | 
|  | @ -254,7 +254,8 @@ def test_render_multiline_text(font: ImageFont.FreeTypeFont) -> None: | |||
| 
 | ||||
| 
 | ||||
| @pytest.mark.parametrize( | ||||
|     "align, ext", (("left", ""), ("center", "_center"), ("right", "_right")) | ||||
|     "align, ext", | ||||
|     (("left", ""), ("center", "_center"), ("right", "_right"), ("justify", "_justify")), | ||||
| ) | ||||
| def test_render_multiline_text_align( | ||||
|     font: ImageFont.FreeTypeFont, align: str, ext: str | ||||
|  |  | |||
|  | @ -387,8 +387,9 @@ Methods | |||
|                     the number of pixels between lines. | ||||
|     :param align: If the text is passed on to | ||||
|                   :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_text`, | ||||
|                   ``"left"``, ``"center"`` or ``"right"``. Determines the relative alignment of lines. | ||||
|                   Use the ``anchor`` parameter to specify the alignment to ``xy``. | ||||
|                   ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines | ||||
|                   the relative alignment of lines. Use the ``anchor`` parameter to | ||||
|                   specify the alignment to ``xy``. | ||||
|     :param direction: Direction of the text. It can be ``"rtl"`` (right to | ||||
|                       left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom). | ||||
|                       Requires libraqm. | ||||
|  | @ -455,8 +456,9 @@ Methods | |||
|                               of Pillow, but implemented only in version 8.0.0. | ||||
| 
 | ||||
|     :param spacing: The number of pixels between lines. | ||||
|     :param align: ``"left"``, ``"center"`` or ``"right"``. Determines the relative alignment of lines. | ||||
|                   Use the ``anchor`` parameter to specify the alignment to ``xy``. | ||||
|     :param align: ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines | ||||
|                   the relative alignment of lines. Use the ``anchor`` parameter to | ||||
|                   specify the alignment to ``xy``. | ||||
|     :param direction: Direction of the text. It can be ``"rtl"`` (right to | ||||
|                       left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom). | ||||
|                       Requires libraqm. | ||||
|  | @ -599,8 +601,9 @@ Methods | |||
|                     the number of pixels between lines. | ||||
|     :param align: If the text is passed on to | ||||
|                   :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_textbbox`, | ||||
|                   ``"left"``, ``"center"`` or ``"right"``. Determines the relative alignment of lines. | ||||
|                   Use the ``anchor`` parameter to specify the alignment to ``xy``. | ||||
|                   ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines | ||||
|                   the relative alignment of lines. Use the ``anchor`` parameter to | ||||
|                   specify the alignment to ``xy``. | ||||
|     :param direction: Direction of the text. It can be ``"rtl"`` (right to | ||||
|                       left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom). | ||||
|                       Requires libraqm. | ||||
|  | @ -650,8 +653,9 @@ Methods | |||
|                    vertical text. See :ref:`text-anchors` for details. | ||||
|                    This parameter is ignored for non-TrueType fonts. | ||||
|     :param spacing: The number of pixels between lines. | ||||
|     :param align: ``"left"``, ``"center"`` or ``"right"``. Determines the relative alignment of lines. | ||||
|                   Use the ``anchor`` parameter to specify the alignment to ``xy``. | ||||
|     :param align: ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines | ||||
|                   the relative alignment of lines. Use the ``anchor`` parameter to | ||||
|                   specify the alignment to ``xy``. | ||||
|     :param direction: Direction of the text. It can be ``"rtl"`` (right to | ||||
|                       left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom). | ||||
|                       Requires libraqm. | ||||
|  |  | |||
|  | @ -761,17 +761,35 @@ class ImageDraw: | |||
|                 left -= width_difference | ||||
| 
 | ||||
|             # then align by align parameter | ||||
|             if align == "left": | ||||
|             if align in ("left", "justify"): | ||||
|                 pass | ||||
|             elif align == "center": | ||||
|                 left += width_difference / 2.0 | ||||
|             elif align == "right": | ||||
|                 left += width_difference | ||||
|             else: | ||||
|                 msg = 'align must be "left", "center" or "right"' | ||||
|                 msg = 'align must be "left", "center", "right" or "justify"' | ||||
|                 raise ValueError(msg) | ||||
| 
 | ||||
|             parts.append(((left, top), line)) | ||||
|             if align == "justify" and width_difference != 0: | ||||
|                 words = line.split(" " if isinstance(text, str) else b" ") | ||||
|                 word_widths = [ | ||||
|                     self.textlength( | ||||
|                         word, | ||||
|                         font, | ||||
|                         direction=direction, | ||||
|                         features=features, | ||||
|                         language=language, | ||||
|                         embedded_color=embedded_color, | ||||
|                     ) | ||||
|                     for word in words | ||||
|                 ] | ||||
|                 width_difference = max_width - sum(word_widths) | ||||
|                 for i, word in enumerate(words): | ||||
|                     parts.append(((left, top), word)) | ||||
|                     left += word_widths[i] + width_difference / (len(words) - 1) | ||||
|             else: | ||||
|                 parts.append(((left, top), line)) | ||||
| 
 | ||||
|             top += line_spacing | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user