2014-11-07 14:21:12 +03:00
|
|
|
from helper import unittest, PillowTestCase
|
|
|
|
from PIL import Image, ImageFont, ImageDraw
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2015-09-08 18:00:00 +03:00
|
|
|
image_font_installed = True
|
|
|
|
try:
|
|
|
|
ImageFont.core.getfont
|
|
|
|
except ImportError:
|
|
|
|
image_font_installed = False
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(not image_font_installed, "image font not installed")
|
2014-11-07 14:21:12 +03:00
|
|
|
class TestImageFontBitmap(PillowTestCase):
|
|
|
|
def test_similar(self):
|
|
|
|
text = 'EmbeddedBitmap'
|
2015-07-03 09:22:56 +03:00
|
|
|
font_outline = ImageFont.truetype(
|
2015-12-30 23:27:27 +03:00
|
|
|
font='Tests/fonts/DejaVuSans.ttf', size=24)
|
2015-07-03 09:22:56 +03:00
|
|
|
font_bitmap = ImageFont.truetype(
|
2015-12-30 23:27:27 +03:00
|
|
|
font='Tests/fonts/DejaVuSans-bitmap.ttf', size=24)
|
|
|
|
size_outline = font_outline.getsize(text)
|
|
|
|
size_bitmap = font_bitmap.getsize(text)
|
2018-06-24 15:32:25 +03:00
|
|
|
size_final = (max(size_outline[0], size_bitmap[0]),
|
|
|
|
max(size_outline[1], size_bitmap[1]))
|
2014-11-07 14:21:12 +03:00
|
|
|
im_bitmap = Image.new('RGB', size_final, (255, 255, 255))
|
|
|
|
im_outline = im_bitmap.copy()
|
2015-12-30 23:27:27 +03:00
|
|
|
draw_bitmap = ImageDraw.Draw(im_bitmap)
|
|
|
|
draw_outline = ImageDraw.Draw(im_outline)
|
2015-01-08 08:28:49 +03:00
|
|
|
|
2015-07-03 09:22:56 +03:00
|
|
|
# Metrics are different on the bitmap and ttf fonts,
|
|
|
|
# more so on some platforms and versions of freetype than others.
|
|
|
|
# Mac has a 1px difference, linux doesn't.
|
2015-04-24 02:26:52 +03:00
|
|
|
draw_bitmap.text((0, size_final[1] - size_bitmap[1]),
|
2015-01-08 08:28:49 +03:00
|
|
|
text, fill=(0, 0, 0), font=font_bitmap)
|
2015-04-24 02:26:52 +03:00
|
|
|
draw_outline.text((0, size_final[1] - size_outline[1]),
|
2015-01-08 08:28:49 +03:00
|
|
|
text, fill=(0, 0, 0), font=font_outline)
|
|
|
|
self.assert_image_similar(im_bitmap, im_outline, 20)
|
2014-11-07 14:21:12 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2014-11-07 17:13:58 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|