mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-12-03 16:24:24 +03:00
Test ImageFont.ImageFont, in case freetype2 is not supported
This commit is contained in:
parent
e36e67081a
commit
b3d9bd9950
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from PIL import Image, ImageDraw, ImageFont, ImageText
|
from PIL import Image, ImageDraw, ImageFont, ImageText, features
|
||||||
|
|
||||||
from .helper import assert_image_similar_tofile, skip_unless_feature
|
from .helper import assert_image_similar_tofile, skip_unless_feature
|
||||||
|
|
||||||
|
|
@ -20,42 +20,69 @@ def layout_engine(request: pytest.FixtureRequest) -> ImageFont.Layout:
|
||||||
return request.param
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(
|
||||||
def font(layout_engine: ImageFont.Layout) -> ImageFont.FreeTypeFont:
|
scope="module",
|
||||||
return ImageFont.truetype(FONT_PATH, 20, layout_engine=layout_engine)
|
params=[
|
||||||
|
None,
|
||||||
|
pytest.param(ImageFont.Layout.BASIC, marks=skip_unless_feature("freetype2")),
|
||||||
|
pytest.param(ImageFont.Layout.RAQM, marks=skip_unless_feature("raqm")),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def font(
|
||||||
|
request: pytest.FixtureRequest,
|
||||||
|
) -> ImageFont.ImageFont | ImageFont.FreeTypeFont:
|
||||||
|
layout_engine = request.param
|
||||||
|
if layout_engine is None:
|
||||||
|
return ImageFont.load_default_imagefont()
|
||||||
|
else:
|
||||||
|
return ImageFont.truetype(FONT_PATH, 20, layout_engine=layout_engine)
|
||||||
|
|
||||||
|
|
||||||
def test_get_length(font: ImageFont.FreeTypeFont) -> None:
|
def test_get_length(font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> None:
|
||||||
assert ImageText.Text("A", font).get_length() == 12
|
factor = 1 if isinstance(font, ImageFont.ImageFont) else 2
|
||||||
assert ImageText.Text("AB", font).get_length() == 24
|
assert ImageText.Text("A", font).get_length() == 6 * factor
|
||||||
assert ImageText.Text("M", font).get_length() == 12
|
assert ImageText.Text("AB", font).get_length() == 12 * factor
|
||||||
assert ImageText.Text("y", font).get_length() == 12
|
assert ImageText.Text("M", font).get_length() == 6 * factor
|
||||||
assert ImageText.Text("a", font).get_length() == 12
|
assert ImageText.Text("y", font).get_length() == 6 * factor
|
||||||
|
assert ImageText.Text("a", font).get_length() == 6 * factor
|
||||||
|
|
||||||
text = ImageText.Text("\n", font)
|
text = ImageText.Text("\n", font)
|
||||||
with pytest.raises(ValueError, match="can't measure length of multiline text"):
|
with pytest.raises(ValueError, match="can't measure length of multiline text"):
|
||||||
text.get_length()
|
text.get_length()
|
||||||
|
|
||||||
|
|
||||||
def test_get_bbox(font: ImageFont.FreeTypeFont) -> None:
|
@pytest.mark.parametrize(
|
||||||
assert ImageText.Text("A", font).get_bbox() == (0, 4, 12, 16)
|
"text, expected",
|
||||||
assert ImageText.Text("AB", font).get_bbox() == (0, 4, 24, 16)
|
(
|
||||||
assert ImageText.Text("M", font).get_bbox() == (0, 4, 12, 16)
|
("A", (0, 4, 12, 16)),
|
||||||
assert ImageText.Text("y", font).get_bbox() == (0, 7, 12, 20)
|
("AB", (0, 4, 24, 16)),
|
||||||
assert ImageText.Text("a", font).get_bbox() == (0, 7, 12, 16)
|
("M", (0, 4, 12, 16)),
|
||||||
|
("y", (0, 7, 12, 20)),
|
||||||
|
("a", (0, 7, 12, 16)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_get_bbox(
|
||||||
|
font: ImageFont.ImageFont | ImageFont.FreeTypeFont,
|
||||||
|
text: str,
|
||||||
|
expected: tuple[int, int, int, int],
|
||||||
|
) -> None:
|
||||||
|
if isinstance(font, ImageFont.ImageFont):
|
||||||
|
expected = (0, 0, expected[2] // 2, 11)
|
||||||
|
assert ImageText.Text(text, font).get_bbox() == expected
|
||||||
|
|
||||||
|
|
||||||
def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
|
def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
|
||||||
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
|
if features.check_module("freetype2"):
|
||||||
text = ImageText.Text("Hello World!", font)
|
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
|
||||||
text.embed_color()
|
text = ImageText.Text("Hello World!", font)
|
||||||
assert text.get_length() == 288
|
text.embed_color()
|
||||||
|
assert text.get_length() == 288
|
||||||
|
|
||||||
im = Image.new("RGB", (300, 64), "white")
|
im = Image.new("RGB", (300, 64), "white")
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
draw.text((10, 10), text, "#fa6")
|
draw.text((10, 10), text, "#fa6")
|
||||||
|
|
||||||
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
|
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
|
||||||
|
|
||||||
text = ImageText.Text("", mode="1")
|
text = ImageText.Text("", mode="1")
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user