1
from __future__ import annotations
9
from pathlib import Path
10
from typing import Any, BinaryIO
13
from packaging.version import parse as parse_version
15
from PIL import Image, ImageDraw, ImageFont, features
16
from PIL._typing import StrOrBytesPath
20
assert_image_equal_tofile,
21
assert_image_similar_tofile,
24
skip_unless_feature_version,
27
FONT_PATH = "Tests/fonts/FreeMono.ttf"
30
TEST_TEXT = "hey you\nyou are awesome\nthis looks awkward"
33
pytestmark = skip_unless_feature("freetype2")
36
def test_sanity() -> None:
37
version = features.version_module("freetype2")
38
assert version is not None
39
assert re.search(r"\d+\.\d+\.\d+$", version)
45
pytest.param(ImageFont.Layout.BASIC),
46
pytest.param(ImageFont.Layout.RAQM, marks=skip_unless_feature("raqm")),
49
def layout_engine(request: pytest.FixtureRequest) -> ImageFont.Layout:
53
@pytest.fixture(scope="module")
54
def font(layout_engine: ImageFont.Layout) -> ImageFont.FreeTypeFont:
55
return ImageFont.truetype(FONT_PATH, FONT_SIZE, layout_engine=layout_engine)
58
def test_font_properties(font: ImageFont.FreeTypeFont) -> None:
59
assert font.path == FONT_PATH
60
assert font.size == FONT_SIZE
62
font_copy = font.font_variant()
63
assert font_copy.path == FONT_PATH
64
assert font_copy.size == FONT_SIZE
66
font_copy = font.font_variant(size=FONT_SIZE + 1)
67
assert font_copy.size == FONT_SIZE + 1
69
second_font_path = "Tests/fonts/DejaVuSans/DejaVuSans.ttf"
70
font_copy = font.font_variant(font=second_font_path)
71
assert font_copy.path == second_font_path
75
font: StrOrBytesPath | BinaryIO, layout_engine: ImageFont.Layout
78
ttf = ImageFont.truetype(font, FONT_SIZE, layout_engine=layout_engine)
81
img = Image.new("RGB", (256, 64), "white")
82
d = ImageDraw.Draw(img)
83
d.text((10, 10), txt, font=ttf, fill="black")
88
@pytest.mark.parametrize("font", (FONT_PATH, Path(FONT_PATH)))
89
def test_font_with_name(layout_engine: ImageFont.Layout, font: str | Path) -> None:
90
_render(font, layout_engine)
93
def test_font_with_filelike(layout_engine: ImageFont.Layout) -> None:
94
def _font_as_bytes() -> BytesIO:
95
with open(FONT_PATH, "rb") as f:
96
font_bytes = BytesIO(f.read())
99
ttf = ImageFont.truetype(_font_as_bytes(), FONT_SIZE, layout_engine=layout_engine)
100
ttf_copy = ttf.font_variant()
101
assert ttf_copy.font_bytes == ttf.font_bytes
103
_render(_font_as_bytes(), layout_engine)
111
def test_font_with_open_file(layout_engine: ImageFont.Layout) -> None:
112
with open(FONT_PATH, "rb") as f:
113
_render(f, layout_engine)
116
def test_render_equal(layout_engine: ImageFont.Layout) -> None:
117
img_path = _render(FONT_PATH, layout_engine)
118
with open(FONT_PATH, "rb") as f:
119
font_filelike = BytesIO(f.read())
120
img_filelike = _render(font_filelike, layout_engine)
122
assert_image_equal(img_path, img_filelike)
125
def test_non_ascii_path(tmp_path: Path, layout_engine: ImageFont.Layout) -> None:
126
tempfile = str(tmp_path / ("temp_" + chr(128) + ".ttf"))
128
shutil.copy(FONT_PATH, tempfile)
129
except UnicodeEncodeError:
130
pytest.skip("Non-ASCII path could not be created")
132
ImageFont.truetype(tempfile, FONT_SIZE, layout_engine=layout_engine)
135
def test_transparent_background(font: ImageFont.FreeTypeFont) -> None:
136
im = Image.new(mode="RGBA", size=(300, 100))
137
draw = ImageDraw.Draw(im)
140
draw.text((10, 10), txt, font=font)
142
target = "Tests/images/transparent_background_text.png"
143
assert_image_similar_tofile(im, target, 4.09)
145
target = "Tests/images/transparent_background_text_L.png"
146
assert_image_similar_tofile(im.convert("L"), target, 0.01)
149
def test_I16(font: ImageFont.FreeTypeFont) -> None:
150
im = Image.new(mode="I;16", size=(300, 100))
151
draw = ImageDraw.Draw(im)
154
draw.text((10, 10), txt, fill=0xFFFE, font=font)
156
assert im.getpixel((12, 14)) == 0xFFFE
158
target = "Tests/images/transparent_background_text_L.png"
159
assert_image_similar_tofile(im.convert("L"), target, 0.01)
162
def test_textbbox_equal(font: ImageFont.FreeTypeFont) -> None:
163
im = Image.new(mode="RGB", size=(300, 100))
164
draw = ImageDraw.Draw(im)
167
bbox = draw.textbbox((10, 10), txt, font)
168
draw.text((10, 10), txt, font=font)
171
assert_image_similar_tofile(im, "Tests/images/rectangle_surrounding_text.png", 2.5)
174
@pytest.mark.parametrize(
175
"text, mode, fontname, size, length_basic, length_raqm",
178
("text", "L", "FreeMono.ttf", 15, 36, 36),
179
("text", "1", "FreeMono.ttf", 15, 36, 36),
181
("rrr", "L", "DejaVuSans/DejaVuSans.ttf", 18, 21, 22.21875),
182
("rrr", "1", "DejaVuSans/DejaVuSans.ttf", 18, 24, 22.21875),
185
("ill", "L", "OpenSansCondensed-LightItalic.ttf", 63, 33, 31.984375),
186
("ill", "1", "OpenSansCondensed-LightItalic.ttf", 63, 33, 31.984375),
194
layout_engine: ImageFont.Layout,
198
f = ImageFont.truetype("Tests/fonts/" + fontname, size, layout_engine=layout_engine)
200
im = Image.new(mode, (1, 1), 0)
201
d = ImageDraw.Draw(im)
203
if layout_engine == ImageFont.Layout.BASIC:
204
length = d.textlength(text, f)
205
assert length == length_basic
208
length = d.textlength(text, f, features=["-kern"])
209
assert length == length_raqm
212
def test_float_size(layout_engine: ImageFont.Layout) -> None:
214
for size in (48, 48.5, 49):
215
f = ImageFont.truetype(
216
"Tests/fonts/NotoSans-Regular.ttf", size, layout_engine=layout_engine
218
lengths.append(f.getlength("text"))
219
assert lengths[0] != lengths[1] != lengths[2]
222
def test_render_multiline(font: ImageFont.FreeTypeFont) -> None:
223
im = Image.new(mode="RGB", size=(300, 100))
224
draw = ImageDraw.Draw(im)
225
line_spacing = font.getbbox("A")[3] + 4
226
lines = TEST_TEXT.split("\n")
229
draw.text((0, y), line, font=font)
235
assert_image_similar_tofile(im, "Tests/images/multiline_text.png", 6.2)
238
def test_render_multiline_text(font: ImageFont.FreeTypeFont) -> None:
241
im = Image.new(mode="RGB", size=(300, 100))
242
draw = ImageDraw.Draw(im)
243
draw.text((0, 0), TEST_TEXT, font=font)
245
assert_image_similar_tofile(im, "Tests/images/multiline_text.png", 0.01)
250
(0, 0), TEST_TEXT, fill=None, font=font, anchor=None, spacing=4, align="left"
252
draw.text((0, 0), TEST_TEXT, None, font, None, 4, "left")
255
@pytest.mark.parametrize(
256
"align, ext", (("left", ""), ("center", "_center"), ("right", "_right"))
258
def test_render_multiline_text_align(
259
font: ImageFont.FreeTypeFont, align: str, ext: str
261
im = Image.new(mode="RGB", size=(300, 100))
262
draw = ImageDraw.Draw(im)
263
draw.multiline_text((0, 0), TEST_TEXT, font=font, align=align)
265
assert_image_similar_tofile(im, f"Tests/images/multiline_text{ext}.png", 0.01)
268
def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:
269
im = Image.new(mode="RGB", size=(300, 100))
270
draw = ImageDraw.Draw(im)
273
with pytest.raises(ValueError):
274
draw.multiline_text((0, 0), TEST_TEXT, font=font, align="unknown")
277
def test_draw_align(font: ImageFont.FreeTypeFont) -> None:
278
im = Image.new("RGB", (300, 100), "white")
279
draw = ImageDraw.Draw(im)
281
draw.text((100, 40), line, (0, 0, 0), font=font, align="left")
284
def test_multiline_bbox(font: ImageFont.FreeTypeFont) -> None:
285
im = Image.new(mode="RGB", size=(300, 100))
286
draw = ImageDraw.Draw(im)
289
assert draw.textbbox((0, 0), TEST_TEXT, font=font) == draw.multiline_textbbox(
290
(0, 0), TEST_TEXT, font=font
295
assert font.getbbox("A") == draw.multiline_textbbox((0, 0), "A", font=font)
299
draw.textbbox((0, 0), TEST_TEXT, font=font, spacing=4)
302
def test_multiline_width(font: ImageFont.FreeTypeFont) -> None:
303
im = Image.new(mode="RGB", size=(300, 100))
304
draw = ImageDraw.Draw(im)
307
draw.textbbox((0, 0), "longest line", font=font)[2]
308
== draw.multiline_textbbox((0, 0), "longest line\nline", font=font)[2]
312
def test_multiline_spacing(font: ImageFont.FreeTypeFont) -> None:
313
im = Image.new(mode="RGB", size=(300, 100))
314
draw = ImageDraw.Draw(im)
315
draw.multiline_text((0, 0), TEST_TEXT, font=font, spacing=10)
317
assert_image_similar_tofile(im, "Tests/images/multiline_text_spacing.png", 2.5)
320
@pytest.mark.parametrize(
321
"orientation", (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270)
323
def test_rotated_transposed_font(
324
font: ImageFont.FreeTypeFont, orientation: Image.Transpose
326
img_gray = Image.new("L", (100, 100))
327
draw = ImageDraw.Draw(img_gray)
330
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
334
bbox_a = draw.textbbox((10, 10), word)
337
draw.font = transposed_font
338
bbox_b = draw.textbbox((20, 20), word)
342
bbox_a[2] - bbox_a[0],
343
bbox_a[3] - bbox_a[1],
345
bbox_b[3] - bbox_b[1],
346
bbox_b[2] - bbox_b[0],
350
assert bbox_b[:2] == (20, 20)
353
with pytest.raises(ValueError):
354
draw.textlength(word)
357
@pytest.mark.parametrize(
361
Image.Transpose.ROTATE_180,
362
Image.Transpose.FLIP_LEFT_RIGHT,
363
Image.Transpose.FLIP_TOP_BOTTOM,
366
def test_unrotated_transposed_font(
367
font: ImageFont.FreeTypeFont, orientation: Image.Transpose
369
img_gray = Image.new("L", (100, 100))
370
draw = ImageDraw.Draw(img_gray)
373
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
377
bbox_a = draw.textbbox((10, 10), word)
378
length_a = draw.textlength(word)
381
draw.font = transposed_font
382
bbox_b = draw.textbbox((20, 20), word)
383
length_b = draw.textlength(word)
387
bbox_a[2] - bbox_a[0],
388
bbox_a[3] - bbox_a[1],
390
bbox_b[2] - bbox_b[0],
391
bbox_b[3] - bbox_b[1],
395
assert bbox_b[:2] == (20, 20)
397
assert length_a == length_b
400
@pytest.mark.parametrize(
401
"orientation", (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270)
403
def test_rotated_transposed_font_get_mask(
404
font: ImageFont.FreeTypeFont, orientation: Image.Transpose
408
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
411
mask = transposed_font.getmask(text)
414
assert mask.size == (13, 108)
417
@pytest.mark.parametrize(
421
Image.Transpose.ROTATE_180,
422
Image.Transpose.FLIP_LEFT_RIGHT,
423
Image.Transpose.FLIP_TOP_BOTTOM,
426
def test_unrotated_transposed_font_get_mask(
427
font: ImageFont.FreeTypeFont, orientation: Image.Transpose
431
transposed_font = ImageFont.TransposedFont(font, orientation=orientation)
434
mask = transposed_font.getmask(text)
437
assert mask.size == (108, 13)
440
def test_free_type_font_get_name(font: ImageFont.FreeTypeFont) -> None:
441
assert ("FreeMono", "Regular") == font.getname()
444
def test_free_type_font_get_metrics(font: ImageFont.FreeTypeFont) -> None:
445
ascent, descent = font.getmetrics()
447
assert isinstance(ascent, int)
448
assert isinstance(descent, int)
449
assert (ascent, descent) == (16, 4)
452
def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
457
mask = font.getmask(text)
460
assert mask.size == (108, 13)
463
def test_load_path_not_found() -> None:
465
filename = "somefilenamethatdoesntexist.ttf"
468
with pytest.raises(OSError):
469
ImageFont.load_path(filename)
470
with pytest.raises(OSError):
471
ImageFont.truetype(filename)
474
def test_load_non_font_bytes() -> None:
475
with open("Tests/images/hopper.jpg", "rb") as f:
476
with pytest.raises(OSError):
477
ImageFont.truetype(f)
480
def test_default_font() -> None:
482
txt = "This is a default font using FreeType support."
483
im = Image.new(mode="RGB", size=(300, 100))
484
draw = ImageDraw.Draw(im)
487
default_font = ImageFont.load_default()
488
draw.text((10, 10), txt, font=default_font)
490
larger_default_font = ImageFont.load_default(size=14)
491
draw.text((10, 60), txt, font=larger_default_font)
494
assert_image_equal_tofile(im, "Tests/images/default_font_freetype.png")
497
@pytest.mark.parametrize("mode", ("", "1", "RGBA"))
498
def test_getbbox(font: ImageFont.FreeTypeFont, mode: str) -> None:
499
assert (0, 4, 12, 16) == font.getbbox("A", mode)
502
def test_getbbox_empty(font: ImageFont.FreeTypeFont) -> None:
504
assert (0, 0, 0, 0) == font.getbbox("")
507
def test_render_empty(font: ImageFont.FreeTypeFont) -> None:
509
im = Image.new(mode="RGB", size=(300, 100))
511
draw = ImageDraw.Draw(im)
513
draw.text((10, 10), "", font=font)
514
assert_image_equal(im, target)
517
def test_unicode_extended(layout_engine: ImageFont.Layout) -> None:
519
text = "A\u278A\U0001F12B"
520
target = "Tests/images/unicode_extended.png"
522
ttf = ImageFont.truetype(
523
"Tests/fonts/NotoSansSymbols-Regular.ttf",
525
layout_engine=layout_engine,
527
img = Image.new("RGB", (100, 60))
528
d = ImageDraw.Draw(img)
529
d.text((10, 10), text, font=ttf)
532
assert_image_similar_tofile(img, target, 6.2)
535
@pytest.mark.parametrize(
536
"platform, font_directory",
537
(("linux", "/usr/local/share/fonts"), ("darwin", "/System/Library/Fonts")),
539
@pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
541
monkeypatch: pytest.MonkeyPatch, platform: str, font_directory: str
543
def _test_fake_loading_font(path_to_fake: str, fontname: str) -> None:
545
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
546
with monkeypatch.context() as m:
547
m.setattr(ImageFont, "_FreeTypeFont", free_type_font, raising=False)
550
filepath: str, size: int, index: int, encoding: str, *args: Any
551
) -> ImageFont.FreeTypeFont:
552
_freeTypeFont = getattr(ImageFont, "_FreeTypeFont")
553
if filepath == path_to_fake:
554
return _freeTypeFont(FONT_PATH, size, index, encoding, *args)
555
return _freeTypeFont(filepath, size, index, encoding, *args)
557
m.setattr(ImageFont, "FreeTypeFont", loadable_font)
558
font = ImageFont.truetype(fontname)
560
name = font.getname()
561
assert ("FreeMono", "Regular") == name
565
monkeypatch.setattr(sys, "platform", platform)
566
if platform == "linux":
567
monkeypatch.setenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
568
monkeypatch.setenv("XDG_DATA_DIRS", "/usr/share/:/usr/local/share/")
570
def fake_walker(path: str) -> list[tuple[str, list[str], list[str]]]:
571
if path == font_directory:
576
["Arial.ttf", "Single.otf", "Duplicate.otf", "Duplicate.ttf"],
579
return [(path, [], ["some_random_font.ttf"])]
581
monkeypatch.setattr(os, "walk", fake_walker)
584
_test_fake_loading_font(font_directory + "/Arial.ttf", "Arial.ttf")
585
_test_fake_loading_font(font_directory + "/Arial.ttf", "Arial")
588
_test_fake_loading_font(font_directory + "/Single.otf", "Single")
591
_test_fake_loading_font(font_directory + "/Duplicate.ttf", "Duplicate")
594
def test_imagefont_getters(font: ImageFont.FreeTypeFont) -> None:
595
assert font.getmetrics() == (16, 4)
596
assert font.font.ascent == 16
597
assert font.font.descent == 4
598
assert font.font.height == 20
599
assert font.font.x_ppem == 20
600
assert font.font.y_ppem == 20
601
assert font.font.glyphs == 4177
602
assert font.getbbox("A") == (0, 4, 12, 16)
603
assert font.getbbox("AB") == (0, 4, 24, 16)
604
assert font.getbbox("M") == (0, 4, 12, 16)
605
assert font.getbbox("y") == (0, 7, 12, 20)
606
assert font.getbbox("a") == (0, 7, 12, 16)
607
assert font.getlength("A") == 12
608
assert font.getlength("AB") == 24
609
assert font.getlength("M") == 12
610
assert font.getlength("y") == 12
611
assert font.getlength("a") == 12
614
@pytest.mark.parametrize("stroke_width", (0, 2))
615
def test_getsize_stroke(font: ImageFont.FreeTypeFont, stroke_width: int) -> None:
616
assert font.getbbox("A", stroke_width=stroke_width) == (
624
def test_complex_font_settings() -> None:
625
t = ImageFont.truetype(FONT_PATH, FONT_SIZE, layout_engine=ImageFont.Layout.BASIC)
626
with pytest.raises(KeyError):
627
t.getmask("абвг", direction="rtl")
628
with pytest.raises(KeyError):
629
t.getmask("абвг", features=["-kern"])
630
with pytest.raises(KeyError):
631
t.getmask("абвг", language="sr")
634
def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
635
version = features.version_module("freetype2")
636
assert version is not None
637
freetype = parse_version(version)
638
if freetype < parse_version("2.9.1"):
639
with pytest.raises(NotImplementedError):
640
font.get_variation_names()
641
with pytest.raises(NotImplementedError):
642
font.get_variation_axes()
645
with pytest.raises(OSError):
646
font.get_variation_names()
647
with pytest.raises(OSError):
648
font.get_variation_axes()
650
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf")
651
assert font.get_variation_names(), [
658
b"Black Medium Contrast",
659
b"Black High Contrast",
662
assert font.get_variation_axes() == [
663
{"name": b"Weight", "minimum": 200, "maximum": 900, "default": 389},
664
{"name": b"Contrast", "minimum": 0, "maximum": 100, "default": 0},
667
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf")
668
assert font.get_variation_names() == [
686
assert font.get_variation_axes() == [
687
{"name": b"Size", "minimum": 0, "maximum": 300, "default": 0}
691
def _check_text(font: ImageFont.FreeTypeFont, path: str, epsilon: float) -> None:
692
im = Image.new("RGB", (100, 75), "white")
693
d = ImageDraw.Draw(im)
694
d.text((10, 10), "Text", font=font, fill="black")
697
assert_image_similar_tofile(im, path, epsilon)
698
except AssertionError:
700
path = path.replace("_adobe", "_adobe_older_harfbuzz")
701
assert_image_similar_tofile(im, path, epsilon)
706
def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None:
707
version = features.version_module("freetype2")
708
assert version is not None
709
freetype = parse_version(version)
710
if freetype < parse_version("2.9.1"):
711
with pytest.raises(NotImplementedError):
712
font.set_variation_by_name("Bold")
715
with pytest.raises(OSError):
716
font.set_variation_by_name("Bold")
718
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
719
_check_text(font, "Tests/images/variation_adobe.png", 11)
720
for name in ("Bold", b"Bold"):
721
font.set_variation_by_name(name)
722
assert font.getname()[1] == "Bold"
723
_check_text(font, "Tests/images/variation_adobe_name.png", 16)
725
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
726
_check_text(font, "Tests/images/variation_tiny.png", 40)
727
for name in ("200", b"200"):
728
font.set_variation_by_name(name)
729
assert font.getname()[1] == "200"
730
_check_text(font, "Tests/images/variation_tiny_name.png", 40)
733
def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None:
734
version = features.version_module("freetype2")
735
assert version is not None
736
freetype = parse_version(version)
737
if freetype < parse_version("2.9.1"):
738
with pytest.raises(NotImplementedError):
739
font.set_variation_by_axes([100])
742
with pytest.raises(OSError):
743
font.set_variation_by_axes([500, 50])
745
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
746
font.set_variation_by_axes([500, 50])
747
_check_text(font, "Tests/images/variation_adobe_axes.png", 11.05)
749
font = ImageFont.truetype("Tests/fonts/TINY5x3GX.ttf", 36)
750
font.set_variation_by_axes([100])
751
_check_text(font, "Tests/images/variation_tiny_axes.png", 32.5)
754
@pytest.mark.parametrize(
768
ids=("ls", "ms", "rs", "ma", "mt", "mm", "mb", "md"),
771
layout_engine: ImageFont.Layout, anchor: str, left: int, top: int
773
name, text = "quick", "Quick"
774
path = f"Tests/images/test_anchor_{name}_{anchor}.png"
776
if layout_engine == ImageFont.Layout.RAQM:
777
width, height = (129, 44)
779
width, height = (128, 44)
781
bbox_expected = (left, top, left + width, top + height)
783
f = ImageFont.truetype(
784
"Tests/fonts/NotoSans-Regular.ttf", 48, layout_engine=layout_engine
787
im = Image.new("RGB", (200, 200), "white")
788
d = ImageDraw.Draw(im)
789
d.line(((0, 100), (200, 100)), "gray")
790
d.line(((100, 0), (100, 200)), "gray")
791
d.text((100, 100), text, fill="black", anchor=anchor, font=f)
793
assert d.textbbox((0, 0), text, f, anchor=anchor) == bbox_expected
795
assert_image_similar_tofile(im, path, 7)
798
@pytest.mark.parametrize(
817
def test_anchor_multiline(
818
layout_engine: ImageFont.Layout, anchor: str, align: str
820
target = f"Tests/images/test_anchor_multiline_{anchor}_{align}.png"
821
text = "a\nlong\ntext sample"
823
f = ImageFont.truetype(
824
"Tests/fonts/NotoSans-Regular.ttf", 48, layout_engine=layout_engine
828
im = Image.new("RGB", (600, 400), "white")
829
d = ImageDraw.Draw(im)
830
d.line(((0, 200), (600, 200)), "gray")
831
d.line(((300, 0), (300, 400)), "gray")
832
d.multiline_text((300, 200), text, fill="black", anchor=anchor, font=f, align=align)
834
assert_image_similar_tofile(im, target, 4)
837
def test_anchor_invalid(font: ImageFont.FreeTypeFont) -> None:
838
im = Image.new("RGB", (100, 100), "white")
839
d = ImageDraw.Draw(im)
842
for anchor in ["", "l", "a", "lax", "sa", "xa", "lx"]:
843
with pytest.raises(ValueError):
844
font.getmask2("hello", anchor=anchor)
845
with pytest.raises(ValueError):
846
font.getbbox("hello", anchor=anchor)
847
with pytest.raises(ValueError):
848
d.text((0, 0), "hello", anchor=anchor)
849
with pytest.raises(ValueError):
850
d.textbbox((0, 0), "hello", anchor=anchor)
851
with pytest.raises(ValueError):
852
d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
853
with pytest.raises(ValueError):
854
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor)
855
for anchor in ["lt", "lb"]:
856
with pytest.raises(ValueError):
857
d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
858
with pytest.raises(ValueError):
859
d.multiline_textbbox((0, 0), "foo\nbar", anchor=anchor)
862
@pytest.mark.parametrize("bpp", (1, 2, 4, 8))
863
def test_bitmap_font(layout_engine: ImageFont.Layout, bpp: int) -> None:
865
layout_name = ["basic", "raqm"][layout_engine]
866
target = f"Tests/images/bitmap_font_{bpp}_{layout_name}.png"
867
font = ImageFont.truetype(
868
f"Tests/fonts/DejaVuSans/DejaVuSans-24-{bpp}-stripped.ttf",
870
layout_engine=layout_engine,
873
im = Image.new("RGB", (160, 35), "white")
874
draw = ImageDraw.Draw(im)
875
draw.text((2, 2), text, "black", font)
877
assert_image_equal_tofile(im, target)
880
def test_bitmap_font_stroke(layout_engine: ImageFont.Layout) -> None:
882
layout_name = ["basic", "raqm"][layout_engine]
883
target = f"Tests/images/bitmap_font_stroke_{layout_name}.png"
884
font = ImageFont.truetype(
885
"Tests/fonts/DejaVuSans/DejaVuSans-24-8-stripped.ttf",
887
layout_engine=layout_engine,
890
im = Image.new("RGB", (160, 35), "white")
891
draw = ImageDraw.Draw(im)
892
draw.text((2, 2), text, "black", font, stroke_width=2, stroke_fill="red")
894
assert_image_similar_tofile(im, target, 0.03)
897
@pytest.mark.parametrize("embedded_color", (False, True))
898
def test_bitmap_blend(layout_engine: ImageFont.Layout, embedded_color: bool) -> None:
899
font = ImageFont.truetype(
900
"Tests/fonts/EBDTTestFont.ttf", size=64, layout_engine=layout_engine
903
im = Image.new("RGBA", (128, 96), "white")
904
d = ImageDraw.Draw(im)
905
d.text((16, 16), "AA", font=font, fill="#8E2F52", embedded_color=embedded_color)
907
assert_image_equal_tofile(im, "Tests/images/bitmap_font_blend.png")
910
def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
912
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
915
im = Image.new("RGB", (300, 64), "white")
916
d = ImageDraw.Draw(im)
917
d.text((10, 10), txt, font=ttf, fill="#fa6", embedded_color=True)
919
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
922
@pytest.mark.parametrize("fontmode", ("1", "L", "RGBA"))
923
def test_float_coord(layout_engine: ImageFont.Layout, fontmode: str) -> None:
925
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
927
im = Image.new("RGB", (300, 64), "white")
928
d = ImageDraw.Draw(im)
932
embedded_color = fontmode == "RGBA"
933
d.text((9.5, 9.5), txt, font=ttf, fill="#fa6", embedded_color=embedded_color)
935
assert_image_similar_tofile(im, "Tests/images/text_float_coord.png", 3.9)
936
except AssertionError:
937
if fontmode == "1" and layout_engine == ImageFont.Layout.BASIC:
938
assert_image_similar_tofile(
939
im, "Tests/images/text_float_coord_1_alt.png", 1
945
def test_cbdt(layout_engine: ImageFont.Layout) -> None:
947
font = ImageFont.truetype(
948
"Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
951
im = Image.new("RGB", (128, 96), "white")
952
d = ImageDraw.Draw(im)
954
d.text((16, 16), "AB", font=font, embedded_color=True)
956
assert_image_equal_tofile(im, "Tests/images/cbdt.png")
958
assert str(e) in ("unimplemented feature", "unknown file format")
959
pytest.skip("freetype compiled without libpng or CBDT support")
962
def test_cbdt_mask(layout_engine: ImageFont.Layout) -> None:
964
font = ImageFont.truetype(
965
"Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
968
im = Image.new("RGB", (128, 96), "white")
969
d = ImageDraw.Draw(im)
971
d.text((16, 16), "AB", "green", font=font)
973
assert_image_equal_tofile(im, "Tests/images/cbdt_mask.png")
975
assert str(e) in ("unimplemented feature", "unknown file format")
976
pytest.skip("freetype compiled without libpng or CBDT support")
979
def test_sbix(layout_engine: ImageFont.Layout) -> None:
981
font = ImageFont.truetype(
982
"Tests/fonts/chromacheck-sbix.woff", size=300, layout_engine=layout_engine
985
im = Image.new("RGB", (400, 400), "white")
986
d = ImageDraw.Draw(im)
988
d.text((50, 50), "\uE901", font=font, embedded_color=True)
990
assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix.png", 1)
992
assert str(e) in ("unimplemented feature", "unknown file format")
993
pytest.skip("freetype compiled without libpng or SBIX support")
996
def test_sbix_mask(layout_engine: ImageFont.Layout) -> None:
998
font = ImageFont.truetype(
999
"Tests/fonts/chromacheck-sbix.woff", size=300, layout_engine=layout_engine
1002
im = Image.new("RGB", (400, 400), "white")
1003
d = ImageDraw.Draw(im)
1005
d.text((50, 50), "\uE901", (100, 0, 0), font=font)
1007
assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix_mask.png", 1)
1008
except OSError as e:
1009
assert str(e) in ("unimplemented feature", "unknown file format")
1010
pytest.skip("freetype compiled without libpng or SBIX support")
1013
@skip_unless_feature_version("freetype2", "2.10.0")
1014
def test_colr(layout_engine: ImageFont.Layout) -> None:
1015
font = ImageFont.truetype(
1016
"Tests/fonts/BungeeColor-Regular_colr_Windows.ttf",
1018
layout_engine=layout_engine,
1021
im = Image.new("RGB", (300, 75), "white")
1022
d = ImageDraw.Draw(im)
1024
d.text((15, 5), "Bungee", font=font, embedded_color=True)
1026
assert_image_similar_tofile(im, "Tests/images/colr_bungee.png", 21)
1029
@skip_unless_feature_version("freetype2", "2.10.0")
1030
def test_colr_mask(layout_engine: ImageFont.Layout) -> None:
1031
font = ImageFont.truetype(
1032
"Tests/fonts/BungeeColor-Regular_colr_Windows.ttf",
1034
layout_engine=layout_engine,
1037
im = Image.new("RGB", (300, 75), "white")
1038
d = ImageDraw.Draw(im)
1040
d.text((15, 5), "Bungee", "black", font=font)
1042
assert_image_similar_tofile(im, "Tests/images/colr_bungee_mask.png", 22)
1045
def test_woff2(layout_engine: ImageFont.Layout) -> None:
1047
font = ImageFont.truetype(
1048
"Tests/fonts/OpenSans.woff2",
1050
layout_engine=layout_engine,
1052
except OSError as e:
1053
assert str(e) in ("unimplemented feature", "unknown file format")
1054
pytest.skip("FreeType compiled without brotli or WOFF2 support")
1056
im = Image.new("RGB", (350, 100), "white")
1057
d = ImageDraw.Draw(im)
1059
d.text((15, 5), "OpenSans", "black", font=font)
1061
assert_image_similar_tofile(im, "Tests/images/test_woff2.png", 5)
1064
def test_render_mono_size() -> None:
1067
im = Image.new("P", (100, 30), "white")
1068
draw = ImageDraw.Draw(im)
1069
ttf = ImageFont.truetype(
1070
"Tests/fonts/DejaVuSans/DejaVuSans.ttf",
1072
layout_engine=ImageFont.Layout.BASIC,
1075
draw.text((10, 10), "r" * 10, "black", ttf)
1076
assert_image_equal_tofile(im, "Tests/images/text_mono.gif")
1079
def test_too_many_characters(font: ImageFont.FreeTypeFont) -> None:
1080
with pytest.raises(ValueError):
1081
font.getlength("A" * 1_000_001)
1082
with pytest.raises(ValueError):
1083
font.getbbox("A" * 1_000_001)
1084
with pytest.raises(ValueError):
1085
font.getmask2("A" * 1_000_001)
1087
transposed_font = ImageFont.TransposedFont(font)
1088
with pytest.raises(ValueError):
1089
transposed_font.getlength("A" * 1_000_001)
1091
imagefont = ImageFont.ImageFont()
1092
with pytest.raises(ValueError):
1093
imagefont.getlength("A" * 1_000_001)
1094
with pytest.raises(ValueError):
1095
imagefont.getbbox("A" * 1_000_001)
1096
with pytest.raises(ValueError):
1097
imagefont.getmask("A" * 1_000_001)
1100
def test_bytes(font: ImageFont.FreeTypeFont) -> None:
1101
assert font.getlength(b"test") == font.getlength("test")
1103
assert font.getbbox(b"test") == font.getbbox("test")
1106
Image.Image()._new(font.getmask(b"test")),
1107
Image.Image()._new(font.getmask("test")),
1111
Image.Image()._new(font.getmask2(b"test")[0]),
1112
Image.Image()._new(font.getmask2("test")[0]),
1114
assert font.getmask2(b"test")[1] == font.getmask2("test")[1]
1117
@pytest.mark.parametrize(
1120
"Tests/fonts/oom-e8e927ba6c0d38274a37c1567560eb33baf74627.ttf",
1121
"Tests/fonts/oom-4da0210eb7081b0bf15bf16cc4c52ce02c1e1bbc.ttf",
1124
def test_oom(test_file: str) -> None:
1125
with open(test_file, "rb") as f:
1126
font = ImageFont.truetype(BytesIO(f.read()))
1127
with pytest.raises(Image.DecompressionBombError):
1128
font.getmask("Test Text")
1131
def test_raqm_missing_warning(monkeypatch: pytest.MonkeyPatch) -> None:
1132
monkeypatch.setattr(ImageFont.core, "HAVE_RAQM", False)
1133
with pytest.warns(UserWarning) as record:
1134
font = ImageFont.truetype(
1135
FONT_PATH, FONT_SIZE, layout_engine=ImageFont.Layout.RAQM
1137
assert font.layout_engine == ImageFont.Layout.BASIC
1138
assert str(record[-1].message) == (
1139
"Raqm layout was requested, but Raqm is not available. "
1140
"Falling back to basic layout."
1144
@pytest.mark.parametrize("size", [-1, 0])
1145
def test_invalid_truetype_sizes_raise_valueerror(
1146
layout_engine: ImageFont.Layout, size: int
1148
with pytest.raises(ValueError):
1149
ImageFont.truetype(FONT_PATH, size, layout_engine=layout_engine)