Skip to content

Commit 38992f6

Browse files
Merge pull request #1 from nulano/fix-alpha-for-overlapping-glyphs
Add tests for glyph alpha blending
2 parents 29ca3fc + 0cef9f2 commit 38992f6

11 files changed

+26
-12
lines changed

Tests/fonts/CBDTTestFont.ttf

1.5 KB
Binary file not shown.

Tests/fonts/EBDTTestFont.ttf

5.05 KB
Binary file not shown.

Tests/fonts/LICENSE.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
NotoNastaliqUrdu-Regular.ttf and NotoSansSymbols-Regular.ttf, from https://github.com/googlei18n/noto-fonts
33
NotoSans-Regular.ttf, from https://www.google.com/get/noto/
44
NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/
5-
NotoColorEmoji.ttf, from https://github.com/googlefonts/noto-emoji
65
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype
76
TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny
87
ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa
@@ -25,3 +24,5 @@ FreeMono.ttf is licensed under GPLv3.
2524
10x20-ISO8859-1.pcf, from https://packages.ubuntu.com/xenial/xfonts-base
2625

2726
"Public domain font. Share and enjoy."
27+
28+
CBDTTestFont.ttf and EBDTTestFont.ttf from https://github.com/nulano/font-tests are public domain.

Tests/fonts/NotoColorEmoji.ttf

-5.09 MB
Binary file not shown.

Tests/images/bitmap_font_blend.png

387 Bytes
Loading

Tests/images/cbdt.png

348 Bytes
Loading

Tests/images/cbdt_mask.png

367 Bytes
Loading

Tests/images/cbdt_notocoloremoji.png

-3.62 KB
Binary file not shown.
-3.54 KB
Binary file not shown.

Tests/test_imagefont.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,19 @@ def test_bitmap_font_stroke(layout_engine):
856856
assert_image_similar_tofile(im, target, 0.03)
857857

858858

859+
@pytest.mark.parametrize("embedded_color", (False, True))
860+
def test_bitmap_blend(layout_engine, embedded_color):
861+
font = ImageFont.truetype(
862+
"Tests/fonts/EBDTTestFont.ttf", size=64, layout_engine=layout_engine
863+
)
864+
865+
im = Image.new("RGBA", (128, 96), "white")
866+
d = ImageDraw.Draw(im)
867+
d.text((16, 16), "AA", font=font, embedded_color=embedded_color, fill="#8E2F52")
868+
869+
assert_image_equal_tofile(im, "Tests/images/bitmap_font_blend.png")
870+
871+
859872
def test_standard_embedded_color(layout_engine):
860873
txt = "Hello World!"
861874
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
@@ -894,15 +907,15 @@ def test_float_coord(layout_engine, fontmode):
894907
def test_cbdt(layout_engine):
895908
try:
896909
font = ImageFont.truetype(
897-
"Tests/fonts/NotoColorEmoji.ttf", size=109, layout_engine=layout_engine
910+
"Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
898911
)
899912

900-
im = Image.new("RGB", (150, 150), "white")
913+
im = Image.new("RGB", (128, 96), "white")
901914
d = ImageDraw.Draw(im)
902915

903-
d.text((10, 10), "\U0001f469", font=font, embedded_color=True)
916+
d.text((16, 16), "AB", font=font, embedded_color=True)
904917

905-
assert_image_similar_tofile(im, "Tests/images/cbdt_notocoloremoji.png", 6.2)
918+
assert_image_equal_tofile(im, "Tests/images/cbdt.png")
906919
except OSError as e: # pragma: no cover
907920
assert str(e) in ("unimplemented feature", "unknown file format")
908921
pytest.skip("freetype compiled without libpng or CBDT support")
@@ -911,17 +924,15 @@ def test_cbdt(layout_engine):
911924
def test_cbdt_mask(layout_engine):
912925
try:
913926
font = ImageFont.truetype(
914-
"Tests/fonts/NotoColorEmoji.ttf", size=109, layout_engine=layout_engine
927+
"Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
915928
)
916929

917-
im = Image.new("RGB", (150, 150), "white")
930+
im = Image.new("RGB", (128, 96), "white")
918931
d = ImageDraw.Draw(im)
919932

920-
d.text((10, 10), "\U0001f469", "black", font=font)
933+
d.text((16, 16), "AB", "green", font=font)
921934

922-
assert_image_similar_tofile(
923-
im, "Tests/images/cbdt_notocoloremoji_mask.png", 6.2
924-
)
935+
assert_image_equal_tofile(im, "Tests/images/cbdt_mask.png")
925936
except OSError as e: # pragma: no cover
926937
assert str(e) in ("unimplemented feature", "unknown file format")
927938
pytest.skip("freetype compiled without libpng or CBDT support")

src/PIL/ImageDraw.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import math
3434
import numbers
35+
import struct
3536

3637
from . import Image, ImageColor
3738

@@ -542,7 +543,8 @@ def draw_text(ink, stroke_width=0, stroke_offset=None):
542543
# font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
543544
# extract mask and set text alpha
544545
color, mask = mask, mask.getband(3)
545-
color.fillband(3, (ink >> 24) & 0xFF)
546+
ink_alpha = struct.pack("=i", ink)[3]
547+
color.fillband(3, ink_alpha)
546548
x, y = coord
547549
self.im.paste(color, (x, y, x + mask.size[0], y + mask.size[1]), mask)
548550
else:

0 commit comments

Comments
 (0)