Merge pull request #1 from nulano/fix-alpha-for-overlapping-glyphs

Add tests for glyph alpha blending
This commit is contained in:
Zach Nagengast 2023-11-27 08:50:41 -08:00 committed by GitHub
commit 38992f6810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 26 additions and 12 deletions

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,6 @@
NotoNastaliqUrdu-Regular.ttf and NotoSansSymbols-Regular.ttf, from https://github.com/googlei18n/noto-fonts NotoNastaliqUrdu-Regular.ttf and NotoSansSymbols-Regular.ttf, from https://github.com/googlei18n/noto-fonts
NotoSans-Regular.ttf, from https://www.google.com/get/noto/ NotoSans-Regular.ttf, from https://www.google.com/get/noto/
NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/ NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/
NotoColorEmoji.ttf, from https://github.com/googlefonts/noto-emoji
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype
TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny
ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa
@ -25,3 +24,5 @@ FreeMono.ttf is licensed under GPLv3.
10x20-ISO8859-1.pcf, from https://packages.ubuntu.com/xenial/xfonts-base 10x20-ISO8859-1.pcf, from https://packages.ubuntu.com/xenial/xfonts-base
"Public domain font. Share and enjoy." "Public domain font. Share and enjoy."
CBDTTestFont.ttf and EBDTTestFont.ttf from https://github.com/nulano/font-tests are public domain.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

BIN
Tests/images/cbdt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 B

BIN
Tests/images/cbdt_mask.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

View File

@ -856,6 +856,19 @@ def test_bitmap_font_stroke(layout_engine):
assert_image_similar_tofile(im, target, 0.03) assert_image_similar_tofile(im, target, 0.03)
@pytest.mark.parametrize("embedded_color", (False, True))
def test_bitmap_blend(layout_engine, embedded_color):
font = ImageFont.truetype(
"Tests/fonts/EBDTTestFont.ttf", size=64, layout_engine=layout_engine
)
im = Image.new("RGBA", (128, 96), "white")
d = ImageDraw.Draw(im)
d.text((16, 16), "AA", font=font, embedded_color=embedded_color, fill="#8E2F52")
assert_image_equal_tofile(im, "Tests/images/bitmap_font_blend.png")
def test_standard_embedded_color(layout_engine): def test_standard_embedded_color(layout_engine):
txt = "Hello World!" txt = "Hello World!"
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine) ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
@ -894,15 +907,15 @@ def test_float_coord(layout_engine, fontmode):
def test_cbdt(layout_engine): def test_cbdt(layout_engine):
try: try:
font = ImageFont.truetype( font = ImageFont.truetype(
"Tests/fonts/NotoColorEmoji.ttf", size=109, layout_engine=layout_engine "Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
) )
im = Image.new("RGB", (150, 150), "white") im = Image.new("RGB", (128, 96), "white")
d = ImageDraw.Draw(im) d = ImageDraw.Draw(im)
d.text((10, 10), "\U0001f469", font=font, embedded_color=True) d.text((16, 16), "AB", font=font, embedded_color=True)
assert_image_similar_tofile(im, "Tests/images/cbdt_notocoloremoji.png", 6.2) assert_image_equal_tofile(im, "Tests/images/cbdt.png")
except OSError as e: # pragma: no cover except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format") assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or CBDT support") pytest.skip("freetype compiled without libpng or CBDT support")
@ -911,17 +924,15 @@ def test_cbdt(layout_engine):
def test_cbdt_mask(layout_engine): def test_cbdt_mask(layout_engine):
try: try:
font = ImageFont.truetype( font = ImageFont.truetype(
"Tests/fonts/NotoColorEmoji.ttf", size=109, layout_engine=layout_engine "Tests/fonts/CBDTTestFont.ttf", size=64, layout_engine=layout_engine
) )
im = Image.new("RGB", (150, 150), "white") im = Image.new("RGB", (128, 96), "white")
d = ImageDraw.Draw(im) d = ImageDraw.Draw(im)
d.text((10, 10), "\U0001f469", "black", font=font) d.text((16, 16), "AB", "green", font=font)
assert_image_similar_tofile( assert_image_equal_tofile(im, "Tests/images/cbdt_mask.png")
im, "Tests/images/cbdt_notocoloremoji_mask.png", 6.2
)
except OSError as e: # pragma: no cover except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format") assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or CBDT support") pytest.skip("freetype compiled without libpng or CBDT support")

View File

@ -32,6 +32,7 @@
import math import math
import numbers import numbers
import struct
from . import Image, ImageColor from . import Image, ImageColor
@ -542,7 +543,8 @@ class ImageDraw:
# font.getmask2(mode="RGBA") returns color in RGB bands and mask in A # font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
# extract mask and set text alpha # extract mask and set text alpha
color, mask = mask, mask.getband(3) color, mask = mask, mask.getband(3)
color.fillband(3, (ink >> 24) & 0xFF) ink_alpha = struct.pack("=i", ink)[3]
color.fillband(3, ink_alpha)
x, y = coord x, y = coord
self.im.paste(color, (x, y, x + mask.size[0], y + mask.size[1]), mask) self.im.paste(color, (x, y, x + mask.size[0], y + mask.size[1]), mask)
else: else: