Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tests/fonts/GreatVibes-Regular.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/fonts/LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/aref
ter-x20b.pcf, from http://terminus-font.sourceforge.net/
BungeeColor-Regular_colr_Windows.ttf, from https://github.com/djrrb/bungee
OpenSans.woff2, from https://fonts.googleapis.com/css?family=Open+Sans
GreatVibes-Regular.ttf, from https://fonts.google.com/specimen/Great+Vibes

All of the above fonts are published under the SIL Open Font License (OFL) v1.1 (http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL), which allows you to copy, modify, and redistribute them if you need to.

Expand Down
Binary file added Tests/images/use_max_line_height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion Tests/test_imagetext.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from __future__ import annotations

import sysconfig

import pytest

from PIL import Image, ImageDraw, ImageFont, ImageText, features

from .helper import assert_image_similar_tofile, skip_unless_feature
from .helper import (
assert_image_equal_tofile,
assert_image_similar_tofile,
skip_unless_feature,
)

FONT_PATH = "Tests/fonts/FreeMono.ttf"

Expand Down Expand Up @@ -71,6 +77,21 @@ def test_get_bbox(
assert ImageText.Text(text, font).get_bbox() == expected


def test_use_max_line_height() -> None:
font = ImageFont.truetype("Tests/fonts/GreatVibes-Regular.ttf", 120)
text = ImageText.Text("adjust\nYellow", font)
text.use_max_line_height()

im = Image.new("RGB", (309, 306))
draw = ImageDraw.Draw(im)
draw.text((0, 0), text, "#ff0")
expected = "Tests/images/use_max_line_height.png"
if sysconfig.get_platform() in ("win32", "win-amd64"):
assert_image_similar_tofile(im, expected, 12.28)
else:
assert_image_equal_tofile(im, expected)


def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
if features.check_module("freetype2"):
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
Expand Down
24 changes: 19 additions & 5 deletions src/PIL/ImageText.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,24 @@ def __init__(
self.language = language

self.embedded_color = False
self.line_height: float | None = None

self.stroke_width: float = 0
self.stroke_fill: _Ink | None = None

def use_max_line_height(self) -> None:
"""
Use the maximum line height from the text.
"""
self.line_height = self.font.getbbox(
self.text,
self._get_fontmode(),
None,
self.features,
self.language,
self.stroke_width,
)[3]

def embed_color(self) -> None:
"""
Use embedded color glyphs (COLR, CBDT, SBIX).
Expand Down Expand Up @@ -351,18 +365,18 @@ def _split(
raise ValueError(msg)

fontmode = self._get_fontmode()
line_spacing = (
self.font.getbbox(
if self.line_height is not None:
line_height = self.line_height
else:
line_height = self.font.getbbox(
"A",
fontmode,
None,
self.features,
self.language,
self.stroke_width,
)[3]
+ self.stroke_width
+ self.spacing
)
line_spacing = line_height + self.stroke_width + self.spacing

top = xy[1]
parts = []
Expand Down
Loading