2016-02-03 14:24:10 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from helper import unittest, PillowTestCase
|
2017-06-13 19:02:43 +03:00
|
|
|
from PIL import Image, ImageDraw, ImageFont, features
|
2016-12-15 13:43:02 +03:00
|
|
|
|
2016-02-03 14:24:10 +03:00
|
|
|
|
|
|
|
FONT_SIZE = 20
|
|
|
|
FONT_PATH = "Tests/fonts/DejaVuSans.ttf"
|
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
@unittest.skipUnless(features.check('raqm'), "Raqm Library is not installed.")
|
|
|
|
class TestImagecomplextext(PillowTestCase):
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_english(self):
|
|
|
|
#smoke test, this should not fail
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'TEST', font=ttf, fill=500, direction='ltr')
|
|
|
|
|
|
|
|
|
|
|
|
def test_complex_text(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'اهلا عمان', font=ttf, fill=500)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_text.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_y_offset(self):
|
|
|
|
ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf", FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'العالم العربي', font=ttf, fill=500)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_y_offset.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-21 21:27:30 +03:00
|
|
|
self.assert_image_similar(im, target_img, 1.7)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_complex_unicode_text(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), u'السلام عليكم', font=ttf, fill=500)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_complex_unicode_text.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_text_direction_rtl(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'English عربي', font=ttf, fill=500, direction='rtl')
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_direction_rtl.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_text_direction_ltr(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'سلطنة عمان Oman', font=ttf, fill=500, direction='ltr')
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_direction_ltr.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_text_direction_rtl2(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'Oman سلطنة عمان', font=ttf, fill=500, direction='rtl')
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_direction_ltr.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_ligature_features(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'filling', font=ttf, fill=500, features=['-liga'])
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_ligature_features.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_kerning_features(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'TeToAV', font=ttf, fill=500, features=['-kern'])
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_kerning_features.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
def test_arabictext_features(self):
|
|
|
|
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
im = Image.new(mode='RGB', size=(300, 100))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.text((0, 0), 'اللغة العربية', font=ttf, fill=500, features=['-fina','-init','-medi'])
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
target = 'Tests/images/test_arabictext_features.png'
|
|
|
|
target_img = Image.open(target)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
2017-06-13 19:02:43 +03:00
|
|
|
self.assert_image_similar(im, target_img, .5)
|
2016-02-03 14:24:10 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|