2023-12-21 14:13:31 +03:00
|
|
|
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
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import FontFile, Image, ImageDraw, ImageFont, PcfFontFile
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal_tofile,
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile,
|
2021-02-21 14:15:56 +03:00
|
|
|
skip_unless_feature,
|
|
|
|
)
|
2013-04-21 12:30:59 +04:00
|
|
|
|
2017-11-02 14:11:38 +03:00
|
|
|
fontname = "Tests/fonts/10x20-ISO8859-1.pcf"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
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:
|
2021-02-21 14:15:56 +03:00
|
|
|
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:
|
2022-06-27 07:03:20 +03:00
|
|
|
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)
|
2021-02-21 14:22:29 +03:00
|
|
|
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
|
2020-06-06 12:42:24 +03:00
|
|
|
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)
|
2021-02-21 14:22:29 +03:00
|
|
|
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"))
|