Pillow/Tests/test_font_pcf.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2020-03-27 11:50:51 +03:00
import os
2024-01-23 13:42:36 +03:00
from pathlib import Path
2020-03-28 04:51:28 +03:00
import pytest
from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile
from .helper import (
assert_image_equal_tofile,
assert_image_similar_tofile,
skip_unless_feature,
)
fontname = "Tests/fonts/10x20-ISO8859-1.pcf"
2014-06-10 13:10:47 +04:00
message = "hello, world"
2020-03-27 11:50:51 +03:00
pytestmark = skip_unless_feature("zlib")
2020-03-27 12:30:00 +03:00
2024-01-23 13:42:36 +03:00
def save_font(request: pytest.FixtureRequest, tmp_path: Path) -> str:
2020-03-27 11:50:51 +03:00
with open(fontname, "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file)
assert isinstance(font, FontFile.FontFile)
# check the number of characters in the font
assert len([_f for _f in font.glyph if _f]) == 223
tempname = str(tmp_path / "temp.pil")
2024-01-22 10:42:43 +03:00
def delete_tempfile() -> None:
2020-03-27 11:50:51 +03:00
try:
os.remove(tempname[:-4] + ".pbm")
except OSError:
pass # report?
2020-03-27 12:30:00 +03:00
2020-03-27 11:50:51 +03:00
request.addfinalizer(delete_tempfile)
font.save(tempname)
with Image.open(tempname.replace(".pil", ".pbm")) as loaded:
assert_image_equal_tofile(loaded, "Tests/fonts/10x20.pbm")
2020-03-27 11:50:51 +03:00
with open(tempname, "rb") as f_loaded:
with open("Tests/fonts/10x20.pil", "rb") as f_target:
assert f_loaded.read() == f_target.read()
return tempname
2020-03-27 12:30:00 +03:00
2024-01-23 13:42:36 +03:00
def test_sanity(request: pytest.FixtureRequest, tmp_path: Path) -> None:
2020-03-27 11:50:51 +03:00
save_font(request, tmp_path)
2020-03-27 12:30:00 +03:00
2024-01-22 10:42:43 +03:00
def test_less_than_256_characters() -> None:
with open("Tests/fonts/10x20-ISO8859-1-fewer-characters.pcf", "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file)
assert isinstance(font, FontFile.FontFile)
# check the number of characters in the font
assert len([_f for _f in font.glyph if _f]) == 127
2024-01-22 10:42:43 +03:00
def test_invalid_file() -> None:
2020-03-27 11:50:51 +03:00
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
PcfFontFile.PcfFontFile(fp)
2020-03-27 12:30:00 +03:00
2024-01-23 13:42:36 +03:00
def test_draw(request: pytest.FixtureRequest, tmp_path: Path) -> None:
2020-03-27 11:50:51 +03:00
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
im = Image.new("L", (130, 30), "white")
draw = ImageDraw.Draw(im)
draw.text((0, 0), message, "black", font=font)
assert_image_similar_tofile(im, "Tests/images/test_draw_pbm_target.png", 0)
2020-03-27 11:50:51 +03:00
2020-03-27 12:30:00 +03:00
2024-01-23 13:42:36 +03:00
def test_textsize(request: pytest.FixtureRequest, tmp_path: Path) -> None:
2020-03-27 11:50:51 +03:00
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
for i in range(255):
2022-05-25 20:23:37 +03:00
(ox, oy, dx, dy) = font.getbbox(chr(i))
assert ox == 0
assert oy == 0
2020-03-27 11:50:51 +03:00
assert dy == 20
assert dx in (0, 10)
2022-05-25 20:23:37 +03:00
assert font.getlength(chr(i)) == dx
for i in range(len(message)):
msg = message[: i + 1]
2022-05-25 20:23:37 +03:00
assert font.getlength(msg) == len(msg) * 10
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)
2020-03-27 11:50:51 +03:00
2020-03-27 12:30:00 +03:00
2024-01-22 10:42:43 +03:00
def _test_high_characters(
2024-01-23 13:42:36 +03:00
request: pytest.FixtureRequest, tmp_path: Path, message: str | bytes
2024-01-22 10:42:43 +03:00
) -> None:
2020-03-27 11:50:51 +03:00
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
im = Image.new("L", (750, 30), "white")
draw = ImageDraw.Draw(im)
draw.text((0, 0), message, "black", font=font)
assert_image_similar_tofile(im, "Tests/images/high_ascii_chars.png", 0)
2020-03-27 11:50:51 +03:00
2020-03-27 12:30:00 +03:00
2024-01-23 13:42:36 +03:00
def test_high_characters(request: pytest.FixtureRequest, tmp_path: Path) -> None:
2020-03-27 11:50:51 +03:00
message = "".join(chr(i + 1) for i in range(140, 232))
_test_high_characters(request, tmp_path, message)
# accept bytes instances.
_test_high_characters(request, tmp_path, message.encode("latin1"))