Skip to content
Closed
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ python:
- nightly

dist: trusty

install:
- "travis_retry sudo add-apt-repository -y ppa:as-bahanta/raqm"
- "travis_retry sudo apt-get update"
- "travis_retry sudo apt-get -qq install libfreetype6-dev liblcms2-dev python-qt4 ghostscript libffi-dev libjpeg-turbo-progs cmake imagemagick"
- "travis_retry sudo apt-get -qq install libfreetype6-dev libharfbuzz-dev libfribidi-dev libraqm-dev liblcms2-dev python-qt4 ghostscript libffi-dev libjpeg-turbo-progs cmake imagemagick"
- "travis_retry pip install cffi"
- "travis_retry pip install nose"
- "travis_retry pip install check-manifest"
Expand Down
9 changes: 4 additions & 5 deletions PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,24 @@ def text(self, xy, text, fill=None, font=None, anchor=None,
if self._multiline_check(text):
return self.multiline_text(xy, text, fill, font, anchor,
*args, **kwargs)

ink, fill = self._getink(fill)
if font is None:
font = self.getfont()
if ink is None:
ink = fill
if ink is not None:
try:
mask, offset = font.getmask2(text, self.fontmode)
mask, offset = font.getmask2(text, self.fontmode, *args, **kwargs)
xy = xy[0] + offset[0], xy[1] + offset[1]
except AttributeError:
try:
mask = font.getmask(text, self.fontmode)
mask = font.getmask(text, self.fontmode, *args, **kwargs)
except TypeError:
mask = font.getmask(text)
self.draw.draw_bitmap(xy, mask, ink)

def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
spacing=4, align="left"):
spacing=4, align="left", direction=None, features=None):
widths = []
max_width = 0
lines = self._multiline_split(text)
Expand All @@ -259,7 +258,7 @@ def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
left += (max_width - widths[idx])
else:
assert False, 'align must be "left", "center" or "right"'
self.text((left, top), line, fill, font, anchor)
self.text((left, top), line, fill, font, anchor, direction=direction, features=features)
top += line_spacing
left = xy[0]

Expand Down
14 changes: 7 additions & 7 deletions PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@ def getname(self):
def getmetrics(self):
return self.font.ascent, self.font.descent

def getsize(self, text):
size, offset = self.font.getsize(text)
def getsize(self, text, direction=None, features=None):
size, offset = self.font.getsize(text, direction, features)
return (size[0] + offset[0], size[1] + offset[1])

def getoffset(self, text):
return self.font.getsize(text)[1]

def getmask(self, text, mode=""):
return self.getmask2(text, mode)[0]
def getmask(self, text, mode="", direction=None, features=None):
return self.getmask2(text, mode, direction=direction, features=features)[0]

def getmask2(self, text, mode="", fill=Image.core.fill):
size, offset = self.font.getsize(text)
def getmask2(self, text, mode="", fill=Image.core.fill, direction=None, features=None):
size, offset = self.font.getsize(text, direction, features)
im = fill("L", size, 0)
self.font.render(text, im.id, mode == "1")
self.font.render(text, im.id, mode == "1", direction, features)
return im, offset

def font_variant(self, font=None, size=None, index=None, encoding=None):
Expand Down
1 change: 1 addition & 0 deletions PIL/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"pil": "PIL._imaging",
"tkinter": "PIL._imagingtk",
"freetype2": "PIL._imagingft",
"raqm": "PIL._imagingft",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to trigger on raqm being available when it's not compiled in but freetype is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I delete it, all builds fails the error is

Traceback (most recent call last):
  File "selftest.py", line 184, in <module>
    supported = features.check_module(name)
  File "build\bdist.win-amd64\egg\PIL\features.py", line 15, in check_module
ValueError: Unknown module raqm

"littlecms2": "PIL._imagingcms",
"webp": "PIL._webp",
"transp_webp": ("WEBP", "WebPDecoderBuggyAlpha")
Expand Down
Binary file added Tests/fonts/NotoNastaliqUrdu-Regular.ttf
Binary file not shown.
Binary file added Tests/images/test_Nastalifont_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_arabictext_features.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_complex_unicode_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_direction_ltr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_direction_rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_kerning_features.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_ligature_features.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/test_y_offset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 141 additions & 0 deletions Tests/test_imagefontctl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import ImageDraw, ImageFont

#check if raqm installed
have_raqm = ImageFont.core.have_raqm

FONT_SIZE = 20
FONT_PATH = "Tests/fonts/DejaVuSans.ttf"

try:
from PIL import ImageFont

ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a way to check if raqm is installed. As it stands, if there is an error that triggers an import error on ImageFont, then this whole set of tests are going to get skipped.

There are version flags for several of the libraries, that would be something to add to _imagingft.c, potentially for all three libraries, but at least raqm.

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'TEST', font=ttf, fill=500, direction='ltr')
@unittest.skipIf(not have_raqm, "Raqm Library is not installed !")
class TestImagecomplextext(PillowTestCase):
def test_complex_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'اهلا عمان', font=ttf, fill=500)

target = 'Tests/images/test_text.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_y_offset(self):
ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf", FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'العالم العربي', font=ttf, fill=500)

target = 'Tests/images/test_y_offset.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_complex_unicode_text(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), u'السلام عليكم', font=ttf, fill=500)

target = 'Tests/images/test_complex_unicode_text.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_text_direction_rtl(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'English عربي', font=ttf, fill=500, direction='rtl')

target = 'Tests/images/test_direction_rtl.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_text_direction_ltr(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'سلطنة عمان Oman', font=ttf, fill=500, direction='ltr')

target = 'Tests/images/test_direction_ltr.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_text_direction_rtl2(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'Oman سلطنة عمان', font=ttf, fill=500, direction='rtl')

target = 'Tests/images/test_direction_ltr.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_ligature_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'filling', font=ttf, fill=500, features=['-liga'])

target = 'Tests/images/test_ligature_features.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_kerning_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'TeToAV', font=ttf, fill=500, features=['-kern'])

target = 'Tests/images/test_kerning_features.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

def test_arabictext_features(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

im = Image.new(mode='RGB', size=(300, 100))
draw = ImageDraw.Draw(im)
draw.text((0, 0), 'اللغة العربية', font=ttf, fill=500, features=['-fina','-init','-medi'])

target = 'Tests/images/test_arabictext_features.png'
target_img = Image.open(target)

self.assert_image_similar(im, target_img, .5)

except ImportError:
class TestImagecomplextext(PillowTestCase):
def test_skip(self):
self.skipTest("ImportError")
except KeyError:
class TestImagecomplextext(PillowTestCase):
def test_skip(self):
self.skipTest("KeyError")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a more descriptive skip message (because it could be ImportError rather than KeyError).


if __name__ == '__main__':
unittest.main()

# End of file
Loading