Pillow

Форк
0
/
test_imagefontpil.py 
84 строки · 2.5 Кб
1
from __future__ import annotations
2

3
import struct
4
from io import BytesIO
5

6
import pytest
7

8
from PIL import Image, ImageDraw, ImageFont, _util, features
9

10
from .helper import assert_image_equal_tofile
11

12
fonts = [ImageFont.load_default_imagefont()]
13
if not features.check_module("freetype2"):
14
    default_font = ImageFont.load_default()
15
    if isinstance(default_font, ImageFont.ImageFont):
16
        fonts.append(default_font)
17

18

19
@pytest.mark.parametrize("font", fonts)
20
def test_default_font(font: ImageFont.ImageFont) -> None:
21
    # Arrange
22
    txt = 'This is a "better than nothing" default font.'
23
    im = Image.new(mode="RGB", size=(300, 100))
24
    draw = ImageDraw.Draw(im)
25

26
    # Act
27
    draw.text((10, 10), txt, font=font)
28

29
    # Assert
30
    assert_image_equal_tofile(im, "Tests/images/default_font.png")
31

32

33
def test_without_freetype() -> None:
34
    original_core = ImageFont.core
35
    if features.check_module("freetype2"):
36
        ImageFont.core = _util.DeferredError(ImportError("Disabled for testing"))
37
    try:
38
        with pytest.raises(ImportError):
39
            ImageFont.truetype("Tests/fonts/FreeMono.ttf")
40

41
        assert isinstance(ImageFont.load_default(), ImageFont.ImageFont)
42

43
        with pytest.raises(ImportError):
44
            ImageFont.load_default(size=14)
45
    finally:
46
        ImageFont.core = original_core
47

48

49
@pytest.mark.parametrize("font", fonts)
50
def test_unicode(font: ImageFont.ImageFont) -> None:
51
    # should not segfault, should return UnicodeDecodeError
52
    # issue #2826
53
    with pytest.raises(UnicodeEncodeError):
54
        font.getbbox("’")
55

56

57
@pytest.mark.parametrize("font", fonts)
58
def test_textbbox(font: ImageFont.ImageFont) -> None:
59
    im = Image.new("RGB", (200, 200))
60
    d = ImageDraw.Draw(im)
61
    assert d.textlength("test", font=font) == 24
62
    assert d.textbbox((0, 0), "test", font=font) == (0, 0, 24, 11)
63

64

65
def test_decompression_bomb() -> None:
66
    glyph = struct.pack(">hhhhhhhhhh", 1, 0, 0, 0, 256, 256, 0, 0, 256, 256)
67
    fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)
68

69
    font = ImageFont.ImageFont()
70
    font._load_pilfont_data(fp, Image.new("L", (256, 256)))
71
    with pytest.raises(Image.DecompressionBombError):
72
        font.getmask("A" * 1_000_000)
73

74

75
@pytest.mark.timeout(4)
76
def test_oom() -> None:
77
    glyph = struct.pack(
78
        ">hhhhhhhhhh", 1, 0, -32767, -32767, 32767, 32767, -32767, -32767, 32767, 32767
79
    )
80
    fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)
81

82
    font = ImageFont.ImageFont()
83
    font._load_pilfont_data(fp, Image.new("L", (1, 1)))
84
    font.getmask("A" * 1_000_000)
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.