Pillow/Tests/test_imagefontctl.py

196 lines
7.2 KiB
Python
Raw Normal View History

import unittest
from PIL import Image, ImageDraw, ImageFont, features
2016-12-15 13:43:02 +03:00
from .helper import PillowTestCase
2016-02-03 14:24:10 +03:00
FONT_SIZE = 20
FONT_PATH = "Tests/fonts/DejaVuSans.ttf"
2018-03-03 12:54:00 +03:00
2019-06-13 18:54:46 +03:00
@unittest.skipUnless(features.check("raqm"), "Raqm Library is not installed.")
class TestImagecomplextext(PillowTestCase):
def test_english(self):
2018-06-15 12:40:04 +03:00
# smoke test, this should not fail
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "TEST", font=ttf, fill=500, direction="ltr")
2018-01-27 09:07:24 +03:00
def test_complex_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "اهلا عمان", font=ttf, fill=500)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_text.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
def test_y_offset(self):
2019-06-13 18:54:46 +03:00
ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf", FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "العالم العربي", font=ttf, fill=500)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_y_offset.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 1.7)
2016-02-03 14:24:10 +03:00
def test_complex_unicode_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "السلام عليكم", font=ttf, fill=500)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_complex_unicode_text.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
ttf = ImageFont.truetype("Tests/fonts/KhmerOSBattambang-Regular.ttf", FONT_SIZE)
2019-04-11 08:36:33 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
2019-04-11 08:36:33 +03:00
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "លោកុប្បត្តិ", font=ttf, fill=500)
2019-04-11 08:36:33 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_complex_unicode_text2.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 2.3)
2019-04-11 08:36:33 +03:00
def test_text_direction_rtl(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "English عربي", font=ttf, fill=500, direction="rtl")
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_direction_rtl.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
def test_text_direction_ltr(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "سلطنة عمان Oman", font=ttf, fill=500, direction="ltr")
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_direction_ltr.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
def test_text_direction_rtl2(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "Oman سلطنة عمان", font=ttf, fill=500, direction="rtl")
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_direction_ltr.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
2019-05-31 13:31:41 +03:00
def test_text_direction_ttb(self):
ttf = ImageFont.truetype("Tests/fonts/NotoSansJP-Regular.otf", FONT_SIZE)
2019-06-19 09:30:10 +03:00
im = Image.new(mode="RGB", size=(100, 300))
2019-05-31 13:31:41 +03:00
draw = ImageDraw.Draw(im)
try:
2019-06-19 09:30:10 +03:00
draw.text((0, 0), "English あい", font=ttf, fill=500, direction="ttb")
2019-05-31 13:31:41 +03:00
except ValueError as ex:
2019-05-28 13:23:15 +03:00
if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction":
2019-06-19 09:30:10 +03:00
self.skipTest("libraqm 0.7 or greater not available")
2019-05-31 13:31:41 +03:00
2019-06-19 09:30:10 +03:00
target = "Tests/images/test_direction_ttb.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 1.15)
2019-05-31 13:31:41 +03:00
2019-07-28 23:40:03 +03:00
def test_text_direction_ttb_stroke(self):
ttf = ImageFont.truetype("Tests/fonts/NotoSansJP-Regular.otf", 50)
im = Image.new(mode="RGB", size=(100, 300))
draw = ImageDraw.Draw(im)
try:
draw.text(
(25, 25),
"あい",
font=ttf,
fill=500,
direction="ttb",
stroke_width=2,
stroke_fill="#0f0",
)
except ValueError as ex:
if str(ex) == "libraqm 0.7 or greater required for 'ttb' direction":
self.skipTest("libraqm 0.7 or greater not available")
target = "Tests/images/test_direction_ttb_stroke.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 12.4)
2019-07-28 23:40:03 +03:00
def test_ligature_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "filling", font=ttf, fill=500, features=["-liga"])
target = "Tests/images/test_ligature_features.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
liga_size = ttf.getsize("fi", features=["-liga"])
2018-03-06 11:53:07 +03:00
self.assertEqual(liga_size, (13, 19))
2018-01-27 09:07:24 +03:00
def test_kerning_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "TeToAV", font=ttf, fill=500, features=["-kern"])
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_kerning_features.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
2016-02-03 14:24:10 +03:00
def test_arabictext_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2016-02-03 14:24:10 +03:00
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text(
(0, 0),
"اللغة العربية",
font=ttf,
fill=500,
features=["-fina", "-init", "-medi"],
)
target = "Tests/images/test_arabictext_features.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_x_max_and_y_offset(self):
ttf = ImageFont.truetype("Tests/fonts/ArefRuqaa-Regular.ttf", 40)
2019-06-22 07:47:56 +03:00
im = Image.new(mode="RGB", size=(50, 100))
draw = ImageDraw.Draw(im)
2019-06-22 07:47:56 +03:00
draw.text((0, 0), "لح", font=ttf, fill=500)
2019-06-22 07:47:56 +03:00
target = "Tests/images/test_x_max_and_y_offset.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_language(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
2019-06-13 18:54:46 +03:00
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
2019-06-13 18:54:46 +03:00
draw.text((0, 0), "абвг", font=ttf, fill=500, language="sr")
2019-03-06 07:38:05 +03:00
2019-06-13 18:54:46 +03:00
target = "Tests/images/test_language.png"
2019-11-25 23:03:23 +03:00
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)