def test_similar(self): text = 'EmbeddedBitmap' font_outline = ImageFont.truetype(font='Tests/fonts/DejaVuSans.ttf', size=24) font_bitmap = ImageFont.truetype( font='Tests/fonts/DejaVuSans-bitmap.ttf', size=24) size_outline = font_outline.getsize(text) size_bitmap = font_bitmap.getsize(text) size_final = (max(size_outline[0], size_bitmap[0]), max(size_outline[1], size_bitmap[1])) im_bitmap = Image.new('RGB', size_final, (255, 255, 255)) im_outline = im_bitmap.copy() draw_bitmap = ImageDraw.Draw(im_bitmap) draw_outline = ImageDraw.Draw(im_outline) # 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. draw_bitmap.text((0, size_final[1] - size_bitmap[1]), text, fill=(0, 0, 0), font=font_bitmap) draw_outline.text((0, size_final[1] - size_outline[1]), text, fill=(0, 0, 0), font=font_outline) self.assert_image_similar(im_bitmap, im_outline, 20)
def loadable_font(filepath, size, index, encoding, *args, **kwargs): if filepath == path_to_fake: return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding, *args, **kwargs) return ImageFont._FreeTypeFont(filepath, size, index, encoding, *args, **kwargs)
def _test_high_characters(self, message): tempname = self.save_font() font = ImageFont.load(tempname) im = Image.new("L", (750, 30), "white") draw = ImageDraw.Draw(im) draw.text((0, 0), message, "black", font=font) with Image.open('Tests/images/high_ascii_chars.png') as target: self.assert_image_similar(im, target, 0)
def test_draw(self): tempname = self.save_font() font = ImageFont.load(tempname) im = Image.new("L", (130, 30), "white") draw = ImageDraw.Draw(im) draw.text((0, 0), message, 'black', font=font) with Image.open('Tests/images/test_draw_pbm_target.png') as target: self.assert_image_similar(im, target, 0)
def test_textsize(self): tempname = self.save_font() font = ImageFont.load(tempname) for i in range(255): (dx, dy) = font.getsize(chr(i)) self.assertEqual(dy, 20) self.assertIn(dx, (0, 10)) for l in range(len(message)): msg = message[:l + 1] self.assertEqual(font.getsize(msg), (len(msg) * 10, 20))
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_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), 'السلام عليكم', 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_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, 1.7)
def _render(self, font): txt = "Hello World!" ttf = ImageFont.truetype(font, FONT_SIZE, layout_engine=self.LAYOUT_ENGINE) ttf.getsize(txt) img = Image.new("RGB", (256, 64), "white") d = ImageDraw.Draw(img) d.text((10, 10), txt, font=ttf, fill='black') return img
def test_unrotated_transposed_font_get_mask(self): # Arrange text = "mask this" font = self.get_font() orientation = None transposed_font = ImageFont.TransposedFont(font, orientation=orientation) # Act mask = transposed_font.getmask(text) # Assert self.assertEqual(mask.size, (108, 13))
def test_rotated_transposed_font_get_mask(self): # Arrange text = "mask this" font = self.get_font() orientation = Image.ROTATE_90 transposed_font = ImageFont.TransposedFont(font, orientation=orientation) # Act mask = transposed_font.getmask(text) # Assert self.assertEqual(mask.size, (13, 108))
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) liga_size = ttf.getsize('fi', features=['-liga']) self.assertEqual(liga_size, (13, 19))
def test_default_font(self): # Arrange txt = 'This is a "better than nothing" default font.' im = Image.new(mode='RGB', size=(300, 100)) draw = ImageDraw.Draw(im) target = 'Tests/images/default_font.png' target_img = Image.open(target) # Act default_font = ImageFont.load_default() draw.text((10, 10), txt, font=default_font) # Assert self.assert_image_equal(im, target_img)
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)
def _test_fake_loading_font(self, path_to_fake, fontname): # Make a copy of FreeTypeFont so we can patch the original free_type_font = copy.deepcopy(ImageFont.FreeTypeFont) with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font): def loadable_font(filepath, size, index, encoding, *args, **kwargs): if filepath == path_to_fake: return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding, *args, **kwargs) return ImageFont._FreeTypeFont(filepath, size, index, encoding, *args, **kwargs) with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font): font = ImageFont.truetype(fontname) # Make sure it's loaded name = font.getname() self.assertEqual(('FreeMono', 'Regular'), name)
def test_unrotated_transposed_font(self): img_grey = Image.new("L", (100, 100)) draw = ImageDraw.Draw(img_grey) word = "testing" font = self.get_font() orientation = None transposed_font = ImageFont.TransposedFont(font, orientation=orientation) # Original font draw.font = font box_size_a = draw.textsize(word) # Rotated font draw.font = transposed_font box_size_b = draw.textsize(word) # Check boxes a and b are same size self.assertEqual(box_size_a, box_size_b)
def test_rotated_transposed_font(self): img_grey = Image.new("L", (100, 100)) draw = ImageDraw.Draw(img_grey) word = "testing" font = self.get_font() orientation = Image.ROTATE_90 transposed_font = ImageFont.TransposedFont(font, orientation=orientation) # Original font draw.font = font box_size_a = draw.textsize(word) # Rotated font draw.font = transposed_font box_size_b = draw.textsize(word) # Check (w,h) of box a is (h,w) of box b self.assertEqual(box_size_a[0], box_size_b[1]) self.assertEqual(box_size_a[1], box_size_b[0])
def test_leak(self): ttype = ImageFont.truetype('Tests/fonts/FreeMono.ttf', 20) self._test_font(ttype)
def test_unicode_pilfont(self): # should not segfault, should return UnicodeDecodeError # issue #2826 font = ImageFont.load_default() with self.assertRaises(UnicodeEncodeError): font.getsize(u"’")
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 get_font(self): return ImageFont.truetype(FONT_PATH, FONT_SIZE, layout_engine=self.LAYOUT_ENGINE)
def test_font_with_filelike(self): ImageFont.truetype(self._font_as_bytes(), FONT_SIZE, layout_engine=self.LAYOUT_ENGINE) self._render(self._font_as_bytes())
def test_leak(self): default_font = ImageFont.load_default() self._test_font(default_font)