예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)
예제 #5
0
    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)
예제 #6
0
    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
예제 #7
0
    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))
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
 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')
예제 #11
0
 def test_leak(self):
     ttype = ImageFont.truetype('Tests/fonts/FreeMono.ttf', 20)
     self._test_font(ttype)
예제 #12
0
 def get_font(self):
     return ImageFont.truetype(FONT_PATH,
                               FONT_SIZE,
                               layout_engine=self.LAYOUT_ENGINE)
예제 #13
0
 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())