Restored charsets dictionary

This commit is contained in:
Andrew Murray 2024-01-22 22:08:45 +11:00
parent a09e056a4c
commit 2521ec4732

View File

@ -15,22 +15,22 @@ from .helper import (
fontname = "Tests/fonts/ter-x20b.pcf"
charsets: dict[str, tuple[int, str, str]] = {
"iso8859-1": (
223,
"hello, world",
"Tests/images/test_draw_pbm_ter_en_target.png",
),
"iso8859-2": (
223,
"witaj świecie",
"Tests/images/test_draw_pbm_ter_pl_target.png",
),
"cp1250": (
250,
"witaj świecie",
"Tests/images/test_draw_pbm_ter_pl_target.png",
),
charsets: dict[str, dict[str, int | str]] = {
"iso8859-1": {
"glyph_count": 223,
"message": "hello, world",
"image1": "Tests/images/test_draw_pbm_ter_en_target.png",
},
"iso8859-2": {
"glyph_count": 223,
"message": "witaj świecie",
"image1": "Tests/images/test_draw_pbm_ter_pl_target.png",
},
"cp1250": {
"glyph_count": 250,
"message": "witaj świecie",
"image1": "Tests/images/test_draw_pbm_ter_pl_target.png",
},
}
@ -44,7 +44,7 @@ def save_font(
font = PcfFontFile.PcfFontFile(test_file, encoding)
assert isinstance(font, FontFile.FontFile)
# check the number of characters in the font
assert len([_f for _f in font.glyph if _f]) == charsets[encoding][0]
assert len([_f for _f in font.glyph if _f]) == charsets[encoding]["glyph_count"]
tempname = str(tmp_path / "temp.pil")
@ -81,9 +81,14 @@ def test_draw(
font = ImageFont.load(tempname)
im = Image.new("L", (150, 30), "white")
draw = ImageDraw.Draw(im)
message = charsets[encoding][1].encode(encoding)
draw.text((0, 0), message, "black", font=font)
assert_image_similar_tofile(im, charsets[encoding][2], 0)
message = charsets[encoding]["message"]
assert isinstance(message, str)
draw.text((0, 0), message.encode(encoding), "black", font=font)
expected_path = charsets[encoding]["image1"]
assert isinstance(expected_path, str)
assert_image_similar_tofile(im, expected_path, 0)
@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
@ -99,8 +104,10 @@ def test_textsize(
assert dy == 20
assert dx in (0, 10)
assert font.getlength(bytearray([i])) == dx
message = charsets[encoding][1].encode(encoding)
for i in range(len(message)):
msg = message[: i + 1]
message = charsets[encoding]["message"]
assert isinstance(message, str)
message_bytes = message.encode(encoding)
for i in range(len(message_bytes)):
msg = message_bytes[: i + 1]
assert font.getlength(msg) == len(msg) * 10
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)