move bitmap font tests to test_imagefont
Before Width: | Height: | Size: 481 B After Width: | Height: | Size: 481 B |
BIN
Tests/images/bitmap_font_1_raqm.png
Normal file
After Width: | Height: | Size: 480 B |
Before Width: | Height: | Size: 661 B After Width: | Height: | Size: 661 B |
BIN
Tests/images/bitmap_font_2_raqm.png
Normal file
After Width: | Height: | Size: 658 B |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
BIN
Tests/images/bitmap_font_4_raqm.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
BIN
Tests/images/bitmap_font_8_raqm.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
|
@ -840,6 +840,24 @@ class TestImageFont:
|
|||
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
|
||||
)
|
||||
|
||||
@skip_unless_feature("freetype2")
|
||||
@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
|
||||
def test_bitmap_font(self, bpp):
|
||||
text = "Bitmap Font"
|
||||
layout_name = ["basic", "raqm"][self.LAYOUT_ENGINE]
|
||||
target = f"Tests/images/bitmap_font_{bpp}_{layout_name}.png"
|
||||
font = ImageFont.truetype(
|
||||
f"Tests/fonts/DejaVuSans-24-{bpp}-stripped.ttf",
|
||||
24,
|
||||
layout_engine=self.LAYOUT_ENGINE,
|
||||
)
|
||||
|
||||
im = Image.new("RGB", (160, 35), "white")
|
||||
draw = ImageDraw.Draw(im)
|
||||
draw.text((2, 2), text, "black", font)
|
||||
|
||||
assert_image_equal_tofile(im, target)
|
||||
|
||||
def test_standard_embedded_color(self):
|
||||
txt = "Hello World!"
|
||||
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=self.LAYOUT_ENGINE)
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
import pytest
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from .helper import assert_image_equal_tofile, assert_image_similar, skip_unless_feature
|
||||
|
||||
image_font_installed = True
|
||||
try:
|
||||
ImageFont.core.getfont
|
||||
except ImportError:
|
||||
image_font_installed = False
|
||||
|
||||
|
||||
@pytest.mark.skipif(not image_font_installed, reason="Image font not installed")
|
||||
def test_similar():
|
||||
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)
|
||||
draw_outline.fontmode = "1" # disable anti-aliasing to match bitmap font
|
||||
|
||||
# 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,
|
||||
)
|
||||
assert_image_similar(im_bitmap, im_outline, 4)
|
||||
|
||||
|
||||
@skip_unless_feature("freetype2")
|
||||
@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
|
||||
def test_bitmap_font(bpp):
|
||||
text = "Bitmap Font"
|
||||
target = f"Tests/images/bitmap_font_{bpp}.png"
|
||||
font = ImageFont.truetype(
|
||||
f"Tests/fonts/DejaVuSans-24-{bpp}-stripped.ttf",
|
||||
24,
|
||||
layout_engine=ImageFont.LAYOUT_BASIC,
|
||||
)
|
||||
|
||||
im = Image.new("RGB", (160, 35), "white")
|
||||
draw = ImageDraw.Draw(im)
|
||||
draw.text((2, 2), text, "black", font)
|
||||
|
||||
assert_image_equal_tofile(im, target)
|