Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy method font_variant() and accessible properties to truetype() #1123

Merged
merged 2 commits into from Mar 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions PIL/ImageFont.py
Expand Up @@ -133,6 +133,11 @@ def __init__(self, font=None, size=10, index=0, encoding="", file=None):
DeprecationWarning)
font = file

self.path = font
self.size = size
self.index = index
self.encoding = encoding

if isPath(font):
self.font = core.getfont(font, size, index, encoding)
else:
Expand Down Expand Up @@ -162,6 +167,21 @@ def getmask2(self, text, mode="", fill=Image.core.fill):
self.font.render(text, im.id, mode == "1")
return im, offset

def font_variant(self, font=None, size=None, index=None, encoding=None):
"""
Create a copy of this FreeTypeFont object,
using any specified arguments to override the settings.

Parameters are identical to the parameters used to initialize this object,
minus the deprecated 'file' argument.

:return: A FreeTypeFont object.
"""
return FreeTypeFont(font = self.path if font == None else font,
size = self.size if size == None else size,
index = self.index if index == None else index,
encoding = self.encoding if encoding == None else encoding)

##
# Wrapper that creates a transposed font from any existing font
# object.
Expand Down
16 changes: 16 additions & 0 deletions Tests/test_imagefont.py
Expand Up @@ -44,6 +44,22 @@ def test_sanity(self):
self.assertRegexpMatches(
ImageFont.core.freetype2_version, "\d+\.\d+\.\d+$")

def test_font_properties(self):
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
self.assertEqual(ttf.path, FONT_PATH)
self.assertEqual(ttf.size, FONT_SIZE)

ttf_copy = ttf.font_variant()
self.assertEqual(ttf_copy.path, FONT_PATH)
self.assertEqual(ttf_copy.size, FONT_SIZE)

ttf_copy = ttf.font_variant(size=FONT_SIZE+1)
self.assertEqual(ttf_copy.size, FONT_SIZE+1)

second_font_path = "Tests/fonts/DejaVuSans.ttf"
ttf_copy = ttf.font_variant(font=second_font_path)
self.assertEqual(ttf_copy.path, second_font_path)

def test_font_with_name(self):
ImageFont.truetype(FONT_PATH, FONT_SIZE)
self._render(FONT_PATH)
Expand Down