2014-12-29 21:09:37 +03:00
|
|
|
|
import copy
|
2019-07-06 23:40:53 +03:00
|
|
|
|
import os
|
2019-03-22 13:14:39 +03:00
|
|
|
|
import re
|
2019-04-07 17:27:16 +03:00
|
|
|
|
import shutil
|
2020-03-29 02:40:46 +03:00
|
|
|
|
import sys
|
2019-07-06 23:40:53 +03:00
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
|
import pytest
|
2020-07-16 14:57:39 +03:00
|
|
|
|
from packaging.version import parse as parse_version
|
2020-08-07 13:28:33 +03:00
|
|
|
|
|
2019-10-12 16:29:10 +03:00
|
|
|
|
from PIL import Image, ImageDraw, ImageFont, features
|
2019-07-06 23:40:53 +03:00
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
|
from .helper import (
|
|
|
|
|
assert_image_equal,
|
2020-06-01 20:21:40 +03:00
|
|
|
|
assert_image_equal_tofile,
|
2020-01-30 17:56:07 +03:00
|
|
|
|
assert_image_similar_tofile,
|
|
|
|
|
is_win32,
|
2020-02-18 01:03:32 +03:00
|
|
|
|
skip_unless_feature,
|
2020-10-12 00:26:11 +03:00
|
|
|
|
skip_unless_feature_version,
|
2020-01-30 17:56:07 +03:00
|
|
|
|
)
|
2013-04-25 23:25:06 +04:00
|
|
|
|
|
2014-07-05 01:04:19 +04:00
|
|
|
|
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
|
|
|
|
FONT_SIZE = 20
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
2015-06-18 10:51:33 +03:00
|
|
|
|
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
|
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
|
pytestmark = skip_unless_feature("freetype2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestImageFont:
|
2017-06-13 19:31:29 +03:00
|
|
|
|
LAYOUT_ENGINE = ImageFont.LAYOUT_BASIC
|
|
|
|
|
|
|
|
|
|
def get_font(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
return ImageFont.truetype(
|
|
|
|
|
FONT_PATH, FONT_SIZE, layout_engine=self.LAYOUT_ENGINE
|
|
|
|
|
)
|
2017-07-15 10:12:33 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_sanity(self):
|
2019-10-12 16:29:10 +03:00
|
|
|
|
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("freetype2"))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_font_properties(self):
|
|
|
|
|
ttf = self.get_font()
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf.path == FONT_PATH
|
|
|
|
|
assert ttf.size == FONT_SIZE
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
ttf_copy = ttf.font_variant()
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf_copy.path == FONT_PATH
|
|
|
|
|
assert ttf_copy.size == FONT_SIZE
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
|
ttf_copy = ttf.font_variant(size=FONT_SIZE + 1)
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf_copy.size == FONT_SIZE + 1
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2021-01-21 13:33:35 +03:00
|
|
|
|
second_font_path = "Tests/fonts/DejaVuSans/DejaVuSans.ttf"
|
2017-06-13 19:31:29 +03:00
|
|
|
|
ttf_copy = ttf.font_variant(font=second_font_path)
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf_copy.path == second_font_path
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_font_with_name(self):
|
|
|
|
|
self.get_font()
|
|
|
|
|
self._render(FONT_PATH)
|
|
|
|
|
|
|
|
|
|
def _font_as_bytes(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
with open(FONT_PATH, "rb") as f:
|
2017-06-13 19:31:29 +03:00
|
|
|
|
font_bytes = BytesIO(f.read())
|
|
|
|
|
return font_bytes
|
|
|
|
|
|
|
|
|
|
def test_font_with_filelike(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
ImageFont.truetype(
|
|
|
|
|
self._font_as_bytes(), FONT_SIZE, layout_engine=self.LAYOUT_ENGINE
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
self._render(self._font_as_bytes())
|
|
|
|
|
# Usage note: making two fonts from the same buffer fails.
|
|
|
|
|
# shared_bytes = self._font_as_bytes()
|
|
|
|
|
# self._render(shared_bytes)
|
2020-02-22 16:06:21 +03:00
|
|
|
|
# with pytest.raises(Exception):
|
|
|
|
|
# _render(shared_bytes)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_font_with_open_file(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
with open(FONT_PATH, "rb") as f:
|
2017-06-13 19:31:29 +03:00
|
|
|
|
self._render(f)
|
|
|
|
|
|
2020-09-13 06:53:58 +03:00
|
|
|
|
def test_non_ascii_path(self, tmp_path):
|
2020-03-28 04:51:28 +03:00
|
|
|
|
tempfile = str(tmp_path / ("temp_" + chr(128) + ".ttf"))
|
2019-04-07 17:27:16 +03:00
|
|
|
|
try:
|
2020-03-28 04:51:28 +03:00
|
|
|
|
shutil.copy(FONT_PATH, tempfile)
|
2019-04-07 17:27:16 +03:00
|
|
|
|
except UnicodeEncodeError:
|
2020-09-13 06:53:58 +03:00
|
|
|
|
pytest.skip("Non-ASCII path could not be created")
|
2019-04-07 17:27:16 +03:00
|
|
|
|
|
|
|
|
|
ImageFont.truetype(tempfile, FONT_SIZE)
|
|
|
|
|
|
2019-04-26 15:14:15 +03:00
|
|
|
|
def test_unavailable_layout_engine(self):
|
|
|
|
|
have_raqm = ImageFont.core.HAVE_RAQM
|
|
|
|
|
ImageFont.core.HAVE_RAQM = False
|
|
|
|
|
|
|
|
|
|
try:
|
2019-06-13 18:54:46 +03:00
|
|
|
|
ttf = ImageFont.truetype(
|
|
|
|
|
FONT_PATH, FONT_SIZE, layout_engine=ImageFont.LAYOUT_RAQM
|
|
|
|
|
)
|
2019-04-26 15:14:15 +03:00
|
|
|
|
finally:
|
|
|
|
|
ImageFont.core.HAVE_RAQM = have_raqm
|
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf.layout_engine == ImageFont.LAYOUT_BASIC
|
2019-04-26 15:14:15 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def _render(self, font):
|
|
|
|
|
txt = "Hello World!"
|
2019-06-13 18:54:46 +03:00
|
|
|
|
ttf = ImageFont.truetype(font, FONT_SIZE, layout_engine=self.LAYOUT_ENGINE)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
ttf.getsize(txt)
|
|
|
|
|
|
|
|
|
|
img = Image.new("RGB", (256, 64), "white")
|
|
|
|
|
d = ImageDraw.Draw(img)
|
2019-06-13 18:54:46 +03:00
|
|
|
|
d.text((10, 10), txt, font=ttf, fill="black")
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
return img
|
|
|
|
|
|
|
|
|
|
def test_render_equal(self):
|
|
|
|
|
img_path = self._render(FONT_PATH)
|
2019-06-13 18:54:46 +03:00
|
|
|
|
with open(FONT_PATH, "rb") as f:
|
2017-06-13 19:31:29 +03:00
|
|
|
|
font_filelike = BytesIO(f.read())
|
|
|
|
|
img_filelike = self._render(font_filelike)
|
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
|
assert_image_equal(img_path, img_filelike)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-04-19 13:56:17 +03:00
|
|
|
|
def test_transparent_background(self):
|
|
|
|
|
im = Image.new(mode="RGBA", size=(300, 100))
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
|
|
|
|
|
txt = "Hello World!"
|
|
|
|
|
draw.text((10, 10), txt, font=ttf)
|
|
|
|
|
|
|
|
|
|
target = "Tests/images/transparent_background_text.png"
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, target, 4.09)
|
2020-04-19 13:56:17 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_textsize_equal(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
|
|
|
|
|
txt = "Hello World!"
|
|
|
|
|
size = draw.textsize(txt, ttf)
|
|
|
|
|
draw.text((10, 10), txt, font=ttf)
|
|
|
|
|
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
# Epsilon ~.5 fails with FreeType 2.7
|
|
|
|
|
assert_image_similar_tofile(
|
|
|
|
|
im, "Tests/images/rectangle_surrounding_text.png", 2.5
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-06-20 13:54:53 +03:00
|
|
|
|
@pytest.mark.parametrize(
|
2020-12-11 23:06:24 +03:00
|
|
|
|
"text, mode, font, size, length_basic, length_raqm",
|
2020-06-20 13:54:53 +03:00
|
|
|
|
(
|
|
|
|
|
# basic test
|
2020-12-11 23:06:24 +03:00
|
|
|
|
("text", "L", "FreeMono.ttf", 15, 36, 36),
|
|
|
|
|
("text", "1", "FreeMono.ttf", 15, 36, 36),
|
2020-06-20 13:54:53 +03:00
|
|
|
|
# issue 4177
|
2021-01-21 13:33:35 +03:00
|
|
|
|
("rrr", "L", "DejaVuSans/DejaVuSans.ttf", 18, 21, 22.21875),
|
|
|
|
|
("rrr", "1", "DejaVuSans/DejaVuSans.ttf", 18, 24, 22.21875),
|
2020-06-20 13:54:53 +03:00
|
|
|
|
# test 'l' not including extra margin
|
|
|
|
|
# using exact value 2047 / 64 for raqm, checked with debugger
|
2020-12-11 23:06:24 +03:00
|
|
|
|
("ill", "L", "OpenSansCondensed-LightItalic.ttf", 63, 33, 31.984375),
|
|
|
|
|
("ill", "1", "OpenSansCondensed-LightItalic.ttf", 63, 33, 31.984375),
|
2020-06-20 13:54:53 +03:00
|
|
|
|
),
|
|
|
|
|
)
|
2020-12-11 23:06:24 +03:00
|
|
|
|
def test_getlength(self, text, mode, font, size, length_basic, length_raqm):
|
2020-06-20 13:54:53 +03:00
|
|
|
|
f = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/" + font, size, layout_engine=self.LAYOUT_ENGINE
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-08 00:43:29 +03:00
|
|
|
|
im = Image.new(mode, (1, 1), 0)
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-06-20 13:54:53 +03:00
|
|
|
|
if self.LAYOUT_ENGINE == ImageFont.LAYOUT_BASIC:
|
2020-10-08 00:43:29 +03:00
|
|
|
|
length = d.textlength(text, f)
|
2020-12-11 23:06:24 +03:00
|
|
|
|
assert length == length_basic
|
2020-06-20 13:54:53 +03:00
|
|
|
|
else:
|
|
|
|
|
# disable kerning, kerning metrics changed
|
2020-10-08 00:43:29 +03:00
|
|
|
|
length = d.textlength(text, f, features=["-kern"])
|
2020-06-20 13:54:53 +03:00
|
|
|
|
assert length == length_raqm
|
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_render_multiline(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = self.get_font()
|
2019-06-13 18:54:46 +03:00
|
|
|
|
line_spacing = draw.textsize("A", font=ttf)[1] + 4
|
2017-06-13 19:31:29 +03:00
|
|
|
|
lines = TEST_TEXT.split("\n")
|
|
|
|
|
y = 0
|
|
|
|
|
for line in lines:
|
|
|
|
|
draw.text((0, y), line, font=ttf)
|
|
|
|
|
y += line_spacing
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
# some versions of freetype have different horizontal spacing.
|
|
|
|
|
# setting a tight epsilon, I'm showing the original test failure
|
|
|
|
|
# at epsilon = ~38.
|
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/multiline_text.png", 6.2)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_render_multiline_text(self):
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Test that text() correctly connects to multiline_text()
|
|
|
|
|
# and that align defaults to left
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
draw.text((0, 0), TEST_TEXT, font=ttf)
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
# Epsilon ~.5 fails with FreeType 2.7
|
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/multiline_text.png", 6.2)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
# Test that text() can pass on additional arguments
|
|
|
|
|
# to multiline_text()
|
2019-06-13 18:54:46 +03:00
|
|
|
|
draw.text(
|
|
|
|
|
(0, 0), TEST_TEXT, fill=None, font=ttf, anchor=None, spacing=4, align="left"
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw.text((0, 0), TEST_TEXT, None, ttf, None, 4, "left")
|
|
|
|
|
|
|
|
|
|
# Test align center and right
|
2019-06-13 18:54:46 +03:00
|
|
|
|
for align, ext in {"center": "_center", "right": "_right"}.items():
|
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2015-06-18 10:51:33 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align=align)
|
2015-06-18 10:51:33 +03:00
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
# Epsilon ~.5 fails with FreeType 2.7
|
|
|
|
|
assert_image_similar_tofile(
|
|
|
|
|
im, "Tests/images/multiline_text" + ext + ".png", 6.2
|
|
|
|
|
)
|
2015-06-18 10:51:33 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_unknown_align(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Act/Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align="unknown")
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2017-07-23 23:56:02 +03:00
|
|
|
|
def test_draw_align(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new("RGB", (300, 100), "white")
|
2017-07-23 23:56:02 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
line = "some text"
|
2019-06-13 18:54:46 +03:00
|
|
|
|
draw.text((100, 40), line, (0, 0, 0), font=ttf, align="left")
|
2017-07-23 23:56:02 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_multiline_size(self):
|
|
|
|
|
ttf = self.get_font()
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
|
|
# Test that textsize() correctly connects to multiline_textsize()
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert draw.textsize(TEST_TEXT, font=ttf) == draw.multiline_textsize(
|
|
|
|
|
TEST_TEXT, font=ttf
|
2019-06-13 18:54:46 +03:00
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2018-04-24 17:55:17 +03:00
|
|
|
|
# Test that multiline_textsize corresponds to ImageFont.textsize()
|
|
|
|
|
# for single line text
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ttf.getsize("A") == draw.multiline_textsize("A", font=ttf)
|
2018-07-01 20:55:53 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
# Test that textsize() can pass on additional arguments
|
|
|
|
|
# to multiline_textsize()
|
|
|
|
|
draw.textsize(TEST_TEXT, font=ttf, spacing=4)
|
|
|
|
|
draw.textsize(TEST_TEXT, ttf, 4)
|
|
|
|
|
|
|
|
|
|
def test_multiline_width(self):
|
|
|
|
|
ttf = self.get_font()
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert (
|
|
|
|
|
draw.textsize("longest line", font=ttf)[0]
|
|
|
|
|
== draw.multiline_textsize("longest line\nline", font=ttf)[0]
|
2019-06-13 18:54:46 +03:00
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_multiline_spacing(self):
|
|
|
|
|
ttf = self.get_font()
|
|
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
# Epsilon ~.5 fails with FreeType 2.7
|
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/multiline_text_spacing.png", 6.2)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_rotated_transposed_font(self):
|
|
|
|
|
img_grey = Image.new("L", (100, 100))
|
|
|
|
|
draw = ImageDraw.Draw(img_grey)
|
|
|
|
|
word = "testing"
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
|
|
|
|
orientation = Image.ROTATE_90
|
2019-06-13 18:54:46 +03:00
|
|
|
|
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
# Original font
|
|
|
|
|
draw.font = font
|
|
|
|
|
box_size_a = draw.textsize(word)
|
|
|
|
|
|
|
|
|
|
# Rotated font
|
|
|
|
|
draw.font = transposed_font
|
|
|
|
|
box_size_b = draw.textsize(word)
|
|
|
|
|
|
|
|
|
|
# Check (w,h) of box a is (h,w) of box b
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert box_size_a[0] == box_size_b[1]
|
|
|
|
|
assert box_size_a[1] == box_size_b[0]
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_unrotated_transposed_font(self):
|
|
|
|
|
img_grey = Image.new("L", (100, 100))
|
|
|
|
|
draw = ImageDraw.Draw(img_grey)
|
|
|
|
|
word = "testing"
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
|
|
|
|
orientation = None
|
2019-06-13 18:54:46 +03:00
|
|
|
|
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
# Original font
|
|
|
|
|
draw.font = font
|
|
|
|
|
box_size_a = draw.textsize(word)
|
|
|
|
|
|
|
|
|
|
# Rotated font
|
|
|
|
|
draw.font = transposed_font
|
|
|
|
|
box_size_b = draw.textsize(word)
|
|
|
|
|
|
|
|
|
|
# Check boxes a and b are same size
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert box_size_a == box_size_b
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_rotated_transposed_font_get_mask(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
text = "mask this"
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
orientation = Image.ROTATE_90
|
2019-06-13 18:54:46 +03:00
|
|
|
|
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
mask = transposed_font.getmask(text)
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-11-25 10:38:13 +03:00
|
|
|
|
assert mask.size == (13, 108)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_unrotated_transposed_font_get_mask(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
text = "mask this"
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
orientation = None
|
2019-06-13 18:54:46 +03:00
|
|
|
|
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
mask = transposed_font.getmask(text)
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-11-25 10:38:13 +03:00
|
|
|
|
assert mask.size == (108, 13)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_free_type_font_get_name(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
name = font.getname()
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert ("FreeMono", "Regular") == name
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_free_type_font_get_metrics(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
ascent, descent = font.getmetrics()
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert isinstance(ascent, int)
|
|
|
|
|
assert isinstance(descent, int)
|
|
|
|
|
assert (ascent, descent) == (16, 4) # too exact check?
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_free_type_font_get_offset(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
text = "offset this"
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
offset = font.getoffset(text)
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert offset == (0, 3)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_free_type_font_get_mask(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
text = "mask this"
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
mask = font.getmask(text)
|
|
|
|
|
|
|
|
|
|
# Assert
|
2020-11-25 10:38:13 +03:00
|
|
|
|
assert mask.size == (108, 13)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_load_path_not_found(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
filename = "somefilenamethatdoesntexist.ttf"
|
|
|
|
|
|
|
|
|
|
# Act/Assert
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
ImageFont.load_path(filename)
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
ImageFont.truetype(filename)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2019-06-23 04:53:01 +03:00
|
|
|
|
def test_load_non_font_bytes(self):
|
|
|
|
|
with open("Tests/images/hopper.jpg", "rb") as f:
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
ImageFont.truetype(f)
|
2019-06-23 04:53:01 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
def test_default_font(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
txt = 'This is a "better than nothing" default font.'
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-06-13 19:31:29 +03:00
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
|
# Act
|
|
|
|
|
default_font = ImageFont.load_default()
|
|
|
|
|
draw.text((10, 10), txt, font=default_font)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
|
# Assert
|
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/default_font.png")
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2017-07-14 13:57:07 +03:00
|
|
|
|
def test_getsize_empty(self):
|
2017-08-31 18:56:06 +03:00
|
|
|
|
# issue #2614
|
2017-07-14 13:57:07 +03:00
|
|
|
|
font = self.get_font()
|
2017-07-23 23:56:02 +03:00
|
|
|
|
# should not crash.
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert (0, 0) == font.getsize("")
|
2017-07-14 13:57:07 +03:00
|
|
|
|
|
2017-08-31 18:56:06 +03:00
|
|
|
|
def test_render_empty(self):
|
|
|
|
|
# issue 2666
|
|
|
|
|
font = self.get_font()
|
2019-06-13 18:54:46 +03:00
|
|
|
|
im = Image.new(mode="RGB", size=(300, 100))
|
2017-08-31 18:56:06 +03:00
|
|
|
|
target = im.copy()
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-06-15 12:40:04 +03:00
|
|
|
|
# should not crash here.
|
2019-06-13 18:54:46 +03:00
|
|
|
|
draw.text((10, 10), "", font=font)
|
2020-01-30 17:56:07 +03:00
|
|
|
|
assert_image_equal(im, target)
|
2018-01-12 22:26:42 +03:00
|
|
|
|
|
2017-11-02 23:46:17 +03:00
|
|
|
|
def test_unicode_pilfont(self):
|
|
|
|
|
# should not segfault, should return UnicodeDecodeError
|
|
|
|
|
# issue #2826
|
|
|
|
|
font = ImageFont.load_default()
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(UnicodeEncodeError):
|
2019-09-30 17:56:31 +03:00
|
|
|
|
font.getsize("’")
|
2017-11-02 23:46:17 +03:00
|
|
|
|
|
2019-04-08 15:49:49 +03:00
|
|
|
|
def test_unicode_extended(self):
|
2019-04-08 08:05:30 +03:00
|
|
|
|
# issue #3777
|
2019-09-30 17:56:31 +03:00
|
|
|
|
text = "A\u278A\U0001F12B"
|
2019-04-08 15:49:49 +03:00
|
|
|
|
target = "Tests/images/unicode_extended.png"
|
|
|
|
|
|
2019-06-25 15:20:57 +03:00
|
|
|
|
ttf = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/NotoSansSymbols-Regular.ttf",
|
|
|
|
|
FONT_SIZE,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
2019-04-08 08:05:30 +03:00
|
|
|
|
img = Image.new("RGB", (100, 60))
|
|
|
|
|
d = ImageDraw.Draw(img)
|
|
|
|
|
d.text((10, 10), text, font=ttf)
|
2019-04-08 15:49:49 +03:00
|
|
|
|
|
2020-09-02 00:29:06 +03:00
|
|
|
|
# fails with 14.7
|
|
|
|
|
assert_image_similar_tofile(img, target, 6.2)
|
2019-04-08 08:05:30 +03:00
|
|
|
|
|
2020-03-29 02:40:46 +03:00
|
|
|
|
def _test_fake_loading_font(self, monkeypatch, path_to_fake, fontname):
|
2017-06-13 19:31:29 +03:00
|
|
|
|
# Make a copy of FreeTypeFont so we can patch the original
|
|
|
|
|
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
|
2020-03-29 02:40:46 +03:00
|
|
|
|
with monkeypatch.context() as m:
|
|
|
|
|
m.setattr(ImageFont, "_FreeTypeFont", free_type_font, raising=False)
|
2019-06-13 18:54:46 +03:00
|
|
|
|
|
|
|
|
|
def loadable_font(filepath, size, index, encoding, *args, **kwargs):
|
2017-06-13 19:31:29 +03:00
|
|
|
|
if filepath == path_to_fake:
|
2019-06-13 18:54:46 +03:00
|
|
|
|
return ImageFont._FreeTypeFont(
|
|
|
|
|
FONT_PATH, size, index, encoding, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
return ImageFont._FreeTypeFont(
|
|
|
|
|
filepath, size, index, encoding, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
|
2020-03-29 02:40:46 +03:00
|
|
|
|
m.setattr(ImageFont, "FreeTypeFont", loadable_font)
|
|
|
|
|
font = ImageFont.truetype(fontname)
|
|
|
|
|
# Make sure it's loaded
|
|
|
|
|
name = font.getname()
|
|
|
|
|
assert ("FreeMono", "Regular") == name
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-03-22 23:05:40 +03:00
|
|
|
|
@pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
|
2020-03-29 02:40:46 +03:00
|
|
|
|
def test_find_linux_font(self, monkeypatch):
|
2017-06-13 19:31:29 +03:00
|
|
|
|
# A lot of mocking here - this is more for hitting code and
|
|
|
|
|
# catching syntax like errors
|
2019-06-13 18:54:46 +03:00
|
|
|
|
font_directory = "/usr/local/share/fonts"
|
2020-03-29 02:40:46 +03:00
|
|
|
|
monkeypatch.setattr(sys, "platform", "linux")
|
|
|
|
|
monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/")
|
|
|
|
|
|
|
|
|
|
def fake_walker(path):
|
|
|
|
|
if path == font_directory:
|
|
|
|
|
return [
|
|
|
|
|
(
|
|
|
|
|
path,
|
|
|
|
|
[],
|
|
|
|
|
["Arial.ttf", "Single.otf", "Duplicate.otf", "Duplicate.ttf"],
|
2019-06-13 18:54:46 +03:00
|
|
|
|
)
|
2020-03-29 02:40:46 +03:00
|
|
|
|
]
|
|
|
|
|
return [(path, [], ["some_random_font.ttf"])]
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(os, "walk", fake_walker)
|
|
|
|
|
# Test that the font loads both with and without the
|
|
|
|
|
# extension
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Arial.ttf", "Arial.ttf"
|
|
|
|
|
)
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Arial.ttf", "Arial"
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-03-29 02:40:46 +03:00
|
|
|
|
# Test that non-ttf fonts can be found without the
|
|
|
|
|
# extension
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Single.otf", "Single"
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-03-29 02:40:46 +03:00
|
|
|
|
# Test that ttf fonts are preferred if the extension is
|
|
|
|
|
# not specified
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Duplicate.ttf", "Duplicate"
|
|
|
|
|
)
|
2014-12-29 21:09:37 +03:00
|
|
|
|
|
2020-03-22 23:05:40 +03:00
|
|
|
|
@pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
|
2020-03-29 02:40:46 +03:00
|
|
|
|
def test_find_macos_font(self, monkeypatch):
|
2017-06-13 19:31:29 +03:00
|
|
|
|
# Like the linux test, more cover hitting code rather than testing
|
|
|
|
|
# correctness.
|
2019-06-13 18:54:46 +03:00
|
|
|
|
font_directory = "/System/Library/Fonts"
|
2020-03-29 02:40:46 +03:00
|
|
|
|
monkeypatch.setattr(sys, "platform", "darwin")
|
|
|
|
|
|
|
|
|
|
def fake_walker(path):
|
|
|
|
|
if path == font_directory:
|
|
|
|
|
return [
|
|
|
|
|
(
|
|
|
|
|
path,
|
|
|
|
|
[],
|
|
|
|
|
["Arial.ttf", "Single.otf", "Duplicate.otf", "Duplicate.ttf"],
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
return [(path, [], ["some_random_font.ttf"])]
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(os, "walk", fake_walker)
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Arial.ttf", "Arial.ttf"
|
|
|
|
|
)
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Arial.ttf", "Arial"
|
|
|
|
|
)
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Single.otf", "Single"
|
|
|
|
|
)
|
|
|
|
|
self._test_fake_loading_font(
|
|
|
|
|
monkeypatch, font_directory + "/Duplicate.ttf", "Duplicate"
|
|
|
|
|
)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
|
|
|
|
def test_imagefont_getters(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
t = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Act / Assert
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert t.getmetrics() == (16, 4)
|
|
|
|
|
assert t.font.ascent == 16
|
|
|
|
|
assert t.font.descent == 4
|
|
|
|
|
assert t.font.height == 20
|
|
|
|
|
assert t.font.x_ppem == 20
|
|
|
|
|
assert t.font.y_ppem == 20
|
|
|
|
|
assert t.font.glyphs == 4177
|
|
|
|
|
assert t.getsize("A") == (12, 16)
|
|
|
|
|
assert t.getsize("AB") == (24, 16)
|
2020-11-25 10:38:13 +03:00
|
|
|
|
assert t.getsize("M") == (12, 16)
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert t.getsize("y") == (12, 20)
|
|
|
|
|
assert t.getsize("a") == (12, 16)
|
|
|
|
|
assert t.getsize_multiline("A") == (12, 16)
|
|
|
|
|
assert t.getsize_multiline("AB") == (24, 16)
|
|
|
|
|
assert t.getsize_multiline("a") == (12, 16)
|
|
|
|
|
assert t.getsize_multiline("ABC\n") == (36, 36)
|
|
|
|
|
assert t.getsize_multiline("ABC\nA") == (36, 36)
|
|
|
|
|
assert t.getsize_multiline("ABC\nAaaa") == (48, 36)
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2019-07-28 23:40:03 +03:00
|
|
|
|
def test_getsize_stroke(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
t = self.get_font()
|
|
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
|
for stroke_width in [0, 2]:
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert t.getsize("A", stroke_width=stroke_width) == (
|
|
|
|
|
12 + stroke_width * 2,
|
|
|
|
|
16 + stroke_width * 2,
|
2019-07-28 23:40:03 +03:00
|
|
|
|
)
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert t.getsize_multiline("ABC\nAaaa", stroke_width=stroke_width) == (
|
|
|
|
|
48 + stroke_width * 2,
|
|
|
|
|
36 + stroke_width * 4,
|
2019-07-28 23:40:03 +03:00
|
|
|
|
)
|
|
|
|
|
|
2019-03-07 04:20:12 +03:00
|
|
|
|
def test_complex_font_settings(self):
|
|
|
|
|
# Arrange
|
|
|
|
|
t = self.get_font()
|
|
|
|
|
# Act / Assert
|
|
|
|
|
if t.layout_engine == ImageFont.LAYOUT_BASIC:
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
|
t.getmask("абвг", direction="rtl")
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
|
t.getmask("абвг", features=["-kern"])
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
|
t.getmask("абвг", language="sr")
|
2019-03-07 04:20:12 +03:00
|
|
|
|
|
2019-06-12 13:27:11 +03:00
|
|
|
|
def test_variation_get(self):
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
2020-07-16 14:57:39 +03:00
|
|
|
|
freetype = parse_version(features.version_module("freetype2"))
|
|
|
|
|
if freetype < parse_version("2.9.1"):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
font.get_variation_names()
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
font.get_variation_axes()
|
2019-06-12 13:27:11 +03:00
|
|
|
|
return
|
|
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
font.get_variation_names()
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
font.get_variation_axes()
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf")
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert font.get_variation_names(), [
|
|
|
|
|
b"ExtraLight",
|
|
|
|
|
b"Light",
|
|
|
|
|
b"Regular",
|
|
|
|
|
b"Semibold",
|
|
|
|
|
b"Bold",
|
|
|
|
|
b"Black",
|
|
|
|
|
b"Black Medium Contrast",
|
|
|
|
|
b"Black High Contrast",
|
|
|
|
|
b"Default",
|
|
|
|
|
]
|
|
|
|
|
assert font.get_variation_axes() == [
|
|
|
|
|
{"name": b"Weight", "minimum": 200, "maximum": 900, "default": 389},
|
|
|
|
|
{"name": b"Contrast", "minimum": 0, "maximum": 100, "default": 0},
|
|
|
|
|
]
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf")
|
2020-02-22 16:06:21 +03:00
|
|
|
|
assert font.get_variation_names() == [
|
|
|
|
|
b"20",
|
|
|
|
|
b"40",
|
|
|
|
|
b"60",
|
|
|
|
|
b"80",
|
|
|
|
|
b"100",
|
|
|
|
|
b"120",
|
|
|
|
|
b"140",
|
|
|
|
|
b"160",
|
|
|
|
|
b"180",
|
|
|
|
|
b"200",
|
|
|
|
|
b"220",
|
|
|
|
|
b"240",
|
|
|
|
|
b"260",
|
|
|
|
|
b"280",
|
|
|
|
|
b"300",
|
|
|
|
|
b"Regular",
|
|
|
|
|
]
|
|
|
|
|
assert font.get_variation_axes() == [
|
|
|
|
|
{"name": b"Size", "minimum": 0, "maximum": 300, "default": 0}
|
|
|
|
|
]
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
2020-06-14 15:23:19 +03:00
|
|
|
|
def _check_text(self, font, path, epsilon):
|
|
|
|
|
im = Image.new("RGB", (100, 75), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
d.text((10, 10), "Text", font=font, fill="black")
|
|
|
|
|
|
|
|
|
|
try:
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, path, epsilon)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
except AssertionError:
|
|
|
|
|
if "_adobe" in path:
|
|
|
|
|
path = path.replace("_adobe", "_adobe_older_harfbuzz")
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, path, epsilon)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
2019-06-12 13:27:11 +03:00
|
|
|
|
def test_variation_set_by_name(self):
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
2020-07-16 14:57:39 +03:00
|
|
|
|
freetype = parse_version(features.version_module("freetype2"))
|
|
|
|
|
if freetype < parse_version("2.9.1"):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
font.set_variation_by_name("Bold")
|
2019-06-12 13:27:11 +03:00
|
|
|
|
return
|
|
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
font.set_variation_by_name("Bold")
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_adobe.png", 11)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
for name in ["Bold", b"Bold"]:
|
|
|
|
|
font.set_variation_by_name(name)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_adobe_name.png", 11)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_tiny.png", 40)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
for name in ["200", b"200"]:
|
|
|
|
|
font.set_variation_by_name(name)
|
2020-06-14 15:23:19 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_tiny_name.png", 40)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
def test_variation_set_by_axes(self):
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
|
2020-07-16 14:57:39 +03:00
|
|
|
|
freetype = parse_version(features.version_module("freetype2"))
|
|
|
|
|
if freetype < parse_version("2.9.1"):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
font.set_variation_by_axes([100])
|
2019-06-12 13:27:11 +03:00
|
|
|
|
return
|
|
|
|
|
|
2020-04-07 09:58:21 +03:00
|
|
|
|
with pytest.raises(OSError):
|
2020-02-22 16:06:21 +03:00
|
|
|
|
font.set_variation_by_axes([500, 50])
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
|
|
|
|
|
font.set_variation_by_axes([500, 50])
|
2020-09-05 08:21:40 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_adobe_axes.png", 11.05)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
|
|
|
|
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
|
|
|
|
|
font.set_variation_by_axes([100])
|
2020-06-14 15:23:19 +03:00
|
|
|
|
self._check_text(font, "Tests/images/variation_tiny_axes.png", 32.5)
|
2019-06-12 13:27:11 +03:00
|
|
|
|
|
2020-04-22 04:02:08 +03:00
|
|
|
|
@pytest.mark.parametrize(
|
2020-10-12 18:08:16 +03:00
|
|
|
|
"anchor, left, left_old, top",
|
2020-04-22 04:02:08 +03:00
|
|
|
|
(
|
|
|
|
|
# test horizontal anchors
|
2020-06-20 13:54:53 +03:00
|
|
|
|
("ls", 0, 0, -36),
|
|
|
|
|
("ms", -64, -65, -36),
|
|
|
|
|
("rs", -128, -129, -36),
|
2020-04-22 04:02:08 +03:00
|
|
|
|
# test vertical anchors
|
2020-06-20 13:54:53 +03:00
|
|
|
|
("ma", -64, -65, 16),
|
|
|
|
|
("mt", -64, -65, 0),
|
|
|
|
|
("mm", -64, -65, -17),
|
|
|
|
|
("mb", -64, -65, -44),
|
|
|
|
|
("md", -64, -65, -51),
|
2020-04-22 04:02:08 +03:00
|
|
|
|
),
|
2020-06-20 13:54:53 +03:00
|
|
|
|
ids=("ls", "ms", "rs", "ma", "mt", "mm", "mb", "md"),
|
2020-04-22 04:02:08 +03:00
|
|
|
|
)
|
2020-06-20 13:54:53 +03:00
|
|
|
|
def test_anchor(self, anchor, left, left_old, top):
|
2020-04-22 04:02:08 +03:00
|
|
|
|
name, text = "quick", "Quick"
|
2020-09-23 00:19:04 +03:00
|
|
|
|
path = f"Tests/images/test_anchor_{name}_{anchor}.png"
|
2020-06-20 13:54:53 +03:00
|
|
|
|
|
|
|
|
|
freetype = parse_version(features.version_module("freetype2"))
|
|
|
|
|
if freetype < parse_version("2.4"):
|
|
|
|
|
width, height = (129, 44)
|
|
|
|
|
left = left_old
|
|
|
|
|
elif self.LAYOUT_ENGINE == ImageFont.LAYOUT_RAQM:
|
|
|
|
|
width, height = (129, 44)
|
|
|
|
|
else:
|
|
|
|
|
width, height = (128, 44)
|
|
|
|
|
|
2020-10-08 00:43:29 +03:00
|
|
|
|
bbox_expected = (left, top, left + width, top + height)
|
|
|
|
|
|
2020-04-22 04:02:08 +03:00
|
|
|
|
f = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/NotoSans-Regular.ttf", 48, layout_engine=self.LAYOUT_ENGINE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (200, 200), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
d.line(((0, 100), (200, 100)), "gray")
|
|
|
|
|
d.line(((100, 0), (100, 200)), "gray")
|
|
|
|
|
d.text((100, 100), text, fill="black", anchor=anchor, font=f)
|
|
|
|
|
|
2020-10-08 00:43:29 +03:00
|
|
|
|
assert d.textbbox((0, 0), text, f, anchor=anchor) == bbox_expected
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, path, 7)
|
2020-04-22 04:02:08 +03:00
|
|
|
|
|
2020-06-20 13:54:53 +03:00
|
|
|
|
@pytest.mark.parametrize(
|
2020-10-12 18:08:16 +03:00
|
|
|
|
"anchor, align",
|
2020-06-20 13:54:53 +03:00
|
|
|
|
(
|
|
|
|
|
# test horizontal anchors
|
|
|
|
|
("lm", "left"),
|
|
|
|
|
("lm", "center"),
|
|
|
|
|
("lm", "right"),
|
|
|
|
|
("mm", "left"),
|
|
|
|
|
("mm", "center"),
|
|
|
|
|
("mm", "right"),
|
|
|
|
|
("rm", "left"),
|
|
|
|
|
("rm", "center"),
|
|
|
|
|
("rm", "right"),
|
|
|
|
|
# test vertical anchors
|
|
|
|
|
("ma", "center"),
|
|
|
|
|
# ("mm", "center"), # duplicate
|
|
|
|
|
("md", "center"),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
def test_anchor_multiline(self, anchor, align):
|
2020-09-23 00:19:04 +03:00
|
|
|
|
target = f"Tests/images/test_anchor_multiline_{anchor}_{align}.png"
|
2020-06-20 13:54:53 +03:00
|
|
|
|
text = "a\nlong\ntext sample"
|
|
|
|
|
|
|
|
|
|
f = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/NotoSans-Regular.ttf", 48, layout_engine=self.LAYOUT_ENGINE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# test render
|
|
|
|
|
im = Image.new("RGB", (600, 400), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
d.line(((0, 200), (600, 200)), "gray")
|
|
|
|
|
d.line(((300, 0), (300, 400)), "gray")
|
|
|
|
|
d.multiline_text(
|
|
|
|
|
(300, 200), text, fill="black", anchor=anchor, font=f, align=align
|
|
|
|
|
)
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, target, 4)
|
2020-06-20 13:54:53 +03:00
|
|
|
|
|
2020-04-22 04:02:08 +03:00
|
|
|
|
def test_anchor_invalid(self):
|
|
|
|
|
font = self.get_font()
|
|
|
|
|
im = Image.new("RGB", (100, 100), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
d.font = font
|
|
|
|
|
|
|
|
|
|
for anchor in ["", "l", "a", "lax", "sa", "xa", "lx"]:
|
|
|
|
|
pytest.raises(ValueError, lambda: font.getmask2("hello", anchor=anchor))
|
2020-06-20 13:54:53 +03:00
|
|
|
|
pytest.raises(ValueError, lambda: font.getbbox("hello", anchor=anchor))
|
2020-04-22 04:02:08 +03:00
|
|
|
|
pytest.raises(ValueError, lambda: d.text((0, 0), "hello", anchor=anchor))
|
2020-10-08 00:43:29 +03:00
|
|
|
|
pytest.raises(
|
|
|
|
|
ValueError, lambda: d.textbbox((0, 0), "hello", anchor=anchor)
|
|
|
|
|
)
|
2020-04-22 04:02:08 +03:00
|
|
|
|
pytest.raises(
|
|
|
|
|
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
|
|
|
|
|
)
|
2020-10-08 00:43:29 +03:00
|
|
|
|
pytest.raises(
|
|
|
|
|
ValueError,
|
|
|
|
|
lambda: d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor),
|
|
|
|
|
)
|
2020-04-22 04:02:08 +03:00
|
|
|
|
for anchor in ["lt", "lb"]:
|
|
|
|
|
pytest.raises(
|
|
|
|
|
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
|
|
|
|
|
)
|
2020-10-08 00:43:29 +03:00
|
|
|
|
pytest.raises(
|
|
|
|
|
ValueError,
|
|
|
|
|
lambda: d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor),
|
|
|
|
|
)
|
2020-04-22 04:02:08 +03:00
|
|
|
|
|
2020-10-11 23:45:10 +03:00
|
|
|
|
@skip_unless_feature("freetype2")
|
|
|
|
|
@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
|
|
|
|
|
def test_bitmap_font(self, bpp):
|
|
|
|
|
text = "Bitmap Font"
|
|
|
|
|
layout_name = ["basic", "raqm"][self.LAYOUT_ENGINE]
|
|
|
|
|
target = f"Tests/images/bitmap_font_{bpp}_{layout_name}.png"
|
|
|
|
|
font = ImageFont.truetype(
|
2021-01-21 13:33:35 +03:00
|
|
|
|
f"Tests/fonts/DejaVuSans/DejaVuSans-24-{bpp}-stripped.ttf",
|
2020-10-11 23:45:10 +03:00
|
|
|
|
24,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (160, 35), "white")
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
draw.text((2, 2), text, "black", font)
|
|
|
|
|
|
|
|
|
|
assert_image_equal_tofile(im, target)
|
|
|
|
|
|
2020-03-29 16:57:10 +03:00
|
|
|
|
def test_standard_embedded_color(self):
|
|
|
|
|
txt = "Hello World!"
|
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=self.LAYOUT_ENGINE)
|
|
|
|
|
ttf.getsize(txt)
|
|
|
|
|
|
2020-10-11 23:25:16 +03:00
|
|
|
|
im = Image.new("RGB", (300, 64), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
d.text((10, 10), txt, font=ttf, fill="#fa6", embedded_color=True)
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 6.2)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2020-10-12 00:26:11 +03:00
|
|
|
|
@skip_unless_feature_version("freetype2", "2.5.0")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
def test_cbdt(self):
|
|
|
|
|
try:
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/NotoColorEmoji.ttf",
|
|
|
|
|
size=109,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (150, 150), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-12-30 06:48:01 +03:00
|
|
|
|
d.text((10, 10), "\U0001f469", font=font, embedded_color=True)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/cbdt_notocoloremoji.png", 6.2)
|
2020-12-30 06:48:01 +03:00
|
|
|
|
except IOError as e: # pragma: no cover
|
2020-10-11 23:25:16 +03:00
|
|
|
|
assert str(e) in ("unimplemented feature", "unknown file format")
|
2020-12-30 06:48:01 +03:00
|
|
|
|
pytest.skip("freetype compiled without libpng or CBDT support")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2020-10-12 00:26:11 +03:00
|
|
|
|
@skip_unless_feature_version("freetype2", "2.5.0")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
def test_cbdt_mask(self):
|
|
|
|
|
try:
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/NotoColorEmoji.ttf",
|
|
|
|
|
size=109,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (150, 150), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-10-07 15:22:27 +03:00
|
|
|
|
d.text((10, 10), "\U0001f469", "black", font=font)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(
|
|
|
|
|
im, "Tests/images/cbdt_notocoloremoji_mask.png", 6.2
|
|
|
|
|
)
|
2020-12-30 06:48:01 +03:00
|
|
|
|
except IOError as e: # pragma: no cover
|
2020-12-30 05:27:28 +03:00
|
|
|
|
assert str(e) in ("unimplemented feature", "unknown file format")
|
2020-12-30 06:48:01 +03:00
|
|
|
|
pytest.skip("freetype compiled without libpng or CBDT support")
|
2020-12-30 05:27:28 +03:00
|
|
|
|
|
|
|
|
|
@skip_unless_feature_version("freetype2", "2.5.1")
|
|
|
|
|
def test_sbix(self):
|
|
|
|
|
try:
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/chromacheck-sbix.woff",
|
|
|
|
|
size=300,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (400, 400), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-12-30 06:48:01 +03:00
|
|
|
|
d.text((50, 50), "\uE901", font=font, embedded_color=True)
|
2020-12-30 05:27:28 +03:00
|
|
|
|
|
2021-02-22 04:14:49 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix.png", 1)
|
2020-12-30 06:48:01 +03:00
|
|
|
|
except IOError as e: # pragma: no cover
|
2020-12-30 05:27:28 +03:00
|
|
|
|
assert str(e) in ("unimplemented feature", "unknown file format")
|
2020-12-30 06:48:01 +03:00
|
|
|
|
pytest.skip("freetype compiled without libpng or SBIX support")
|
2020-12-30 05:27:28 +03:00
|
|
|
|
|
|
|
|
|
@skip_unless_feature_version("freetype2", "2.5.1")
|
|
|
|
|
def test_sbix_mask(self):
|
|
|
|
|
try:
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/chromacheck-sbix.woff",
|
|
|
|
|
size=300,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (400, 400), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
|
|
d.text((50, 50), "\uE901", (100, 0, 0), font=font)
|
|
|
|
|
|
2021-02-22 04:14:49 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix_mask.png", 1)
|
2020-12-30 06:48:01 +03:00
|
|
|
|
except IOError as e: # pragma: no cover
|
2020-10-11 23:25:16 +03:00
|
|
|
|
assert str(e) in ("unimplemented feature", "unknown file format")
|
2020-12-30 06:48:01 +03:00
|
|
|
|
pytest.skip("freetype compiled without libpng or SBIX support")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2020-10-12 00:26:11 +03:00
|
|
|
|
@skip_unless_feature_version("freetype2", "2.10.0")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
def test_colr(self):
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/BungeeColor-Regular_colr_Windows.ttf",
|
|
|
|
|
size=64,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (300, 75), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
2020-12-30 06:48:01 +03:00
|
|
|
|
d.text((15, 5), "Bungee", font=font, embedded_color=True)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/colr_bungee.png", 21)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2020-10-12 00:26:11 +03:00
|
|
|
|
@skip_unless_feature_version("freetype2", "2.10.0")
|
2020-03-29 16:57:10 +03:00
|
|
|
|
def test_colr_mask(self):
|
|
|
|
|
font = ImageFont.truetype(
|
|
|
|
|
"Tests/fonts/BungeeColor-Regular_colr_Windows.ttf",
|
|
|
|
|
size=64,
|
|
|
|
|
layout_engine=self.LAYOUT_ENGINE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
im = Image.new("RGB", (300, 75), "white")
|
|
|
|
|
d = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
|
|
d.text((15, 5), "Bungee", "black", font=font)
|
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
|
assert_image_similar_tofile(im, "Tests/images/colr_bungee_mask.png", 22)
|
2020-03-29 16:57:10 +03:00
|
|
|
|
|
2017-06-13 19:31:29 +03:00
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
|
@skip_unless_feature("raqm")
|
2017-06-13 19:31:29 +03:00
|
|
|
|
class TestImageFont_RaqmLayout(TestImageFont):
|
|
|
|
|
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
|
2020-06-01 20:21:40 +03:00
|
|
|
|
|
|
|
|
|
|
2020-10-12 00:26:11 +03:00
|
|
|
|
@skip_unless_feature_version("freetype2", "2.4", "Different metrics")
|
2020-06-01 20:21:40 +03:00
|
|
|
|
def test_render_mono_size():
|
|
|
|
|
# issue 4177
|
|
|
|
|
|
|
|
|
|
im = Image.new("P", (100, 30), "white")
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
ttf = ImageFont.truetype(
|
2021-01-21 13:33:35 +03:00
|
|
|
|
"Tests/fonts/DejaVuSans/DejaVuSans.ttf",
|
|
|
|
|
18,
|
|
|
|
|
layout_engine=ImageFont.LAYOUT_BASIC,
|
2020-06-01 20:21:40 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
draw.text((10, 10), "r" * 10, "black", ttf)
|
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/text_mono.gif")
|
2020-12-16 19:21:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_freetype_deprecation(monkeypatch):
|
|
|
|
|
# Arrange: mock features.version_module to return fake FreeType version
|
|
|
|
|
def fake_version_module(module):
|
|
|
|
|
return "2.7"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(features, "version_module", fake_version_module)
|
|
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
|
ImageFont.truetype(FONT_PATH, FONT_SIZE)
|