mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Add tests for charset aware PcfFontFile
This commit is contained in:
parent
60997894d1
commit
422efd68c3
|
@ -4,6 +4,7 @@ NotoSansJP-Thin.otf, from https://www.google.com/get/noto/help/cjk/
|
|||
AdobeVFPrototype.ttf, from https://github.com/adobe-fonts/adobe-variable-font-prototype
|
||||
TINY5x3GX.ttf, from http://velvetyne.fr/fonts/tiny
|
||||
ArefRuqaa-Regular.ttf, from https://github.com/google/fonts/tree/master/ofl/arefruqaa
|
||||
ter-x20b.pcf, from http://terminus-font.sourceforge.net/
|
||||
|
||||
All of the above fonts are published under the SIL Open Font License (OFL) v1.1 (http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL), which allows you to copy, modify, and redistribute them if you need to.
|
||||
|
||||
|
|
BIN
Tests/fonts/ter-x20b-cp1250.pbm
Normal file
BIN
Tests/fonts/ter-x20b-cp1250.pbm
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
BIN
Tests/fonts/ter-x20b-cp1250.pil
Normal file
BIN
Tests/fonts/ter-x20b-cp1250.pil
Normal file
Binary file not shown.
BIN
Tests/fonts/ter-x20b-iso8859-1.pbm
Normal file
BIN
Tests/fonts/ter-x20b-iso8859-1.pbm
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
BIN
Tests/fonts/ter-x20b-iso8859-1.pil
Normal file
BIN
Tests/fonts/ter-x20b-iso8859-1.pil
Normal file
Binary file not shown.
BIN
Tests/fonts/ter-x20b-iso8859-2.pbm
Normal file
BIN
Tests/fonts/ter-x20b-iso8859-2.pbm
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
BIN
Tests/fonts/ter-x20b-iso8859-2.pil
Normal file
BIN
Tests/fonts/ter-x20b-iso8859-2.pil
Normal file
Binary file not shown.
BIN
Tests/fonts/ter-x20b.pcf
Normal file
BIN
Tests/fonts/ter-x20b.pcf
Normal file
Binary file not shown.
BIN
Tests/images/test_draw_pbm_ter_en_target.png
Normal file
BIN
Tests/images/test_draw_pbm_ter_en_target.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 253 B |
BIN
Tests/images/test_draw_pbm_ter_pl_target.png
Normal file
BIN
Tests/images/test_draw_pbm_ter_pl_target.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 284 B |
129
Tests/test_font_pcf_charsets.py
Normal file
129
Tests/test_font_pcf_charsets.py
Normal file
|
@ -0,0 +1,129 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
from .helper import PillowTestCase
|
||||
|
||||
from PIL import Image, FontFile, PcfFontFile
|
||||
from PIL import ImageFont, ImageDraw
|
||||
|
||||
# from PIL._util import py3
|
||||
|
||||
codecs = dir(Image.core)
|
||||
|
||||
fontname = "Tests/fonts/ter-x20b.pcf"
|
||||
|
||||
charsets = {
|
||||
"iso8859-1": {
|
||||
"glyph_count": 223,
|
||||
"message": u"hello, world",
|
||||
"image1": "Tests/images/test_draw_pbm_ter_en_target.png",
|
||||
},
|
||||
"iso8859-2": {
|
||||
"glyph_count": 223,
|
||||
"message": u"witaj świecie",
|
||||
"image1": "Tests/images/test_draw_pbm_ter_pl_target.png",
|
||||
},
|
||||
"cp1250": {
|
||||
"glyph_count": 250,
|
||||
"message": u"witaj świecie",
|
||||
"image1": "Tests/images/test_draw_pbm_ter_pl_target.png",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class TestFontPcf(PillowTestCase):
|
||||
def setUp(self):
|
||||
if "zip_encoder" not in codecs or "zip_decoder" not in codecs:
|
||||
self.skipTest("zlib support not available")
|
||||
|
||||
def save_font(self, encoding):
|
||||
with open(fontname, "rb") as test_file:
|
||||
font = PcfFontFile.PcfFontFile(test_file, encoding)
|
||||
self.assertIsInstance(font, FontFile.FontFile)
|
||||
# check the number of characters in the font
|
||||
self.assertEqual(
|
||||
len([_f for _f in font.glyph if _f]), charsets[encoding]["glyph_count"]
|
||||
)
|
||||
|
||||
tempname = self.tempfile("temp.pil")
|
||||
self.addCleanup(self.delete_tempfile, tempname[:-4] + ".pbm")
|
||||
font.save(tempname)
|
||||
|
||||
with Image.open(tempname.replace(".pil", ".pbm")) as loaded:
|
||||
with Image.open("Tests/fonts/ter-x20b-%s.pbm" % encoding) as target:
|
||||
self.assert_image_equal(loaded, target)
|
||||
|
||||
with open(tempname, "rb") as f_loaded:
|
||||
with open("Tests/fonts/ter-x20b-%s.pil" % encoding, "rb") as f_target:
|
||||
self.assertEqual(f_loaded.read(), f_target.read())
|
||||
return tempname
|
||||
|
||||
def _test_sanity(self, encoding):
|
||||
self.save_font(encoding)
|
||||
|
||||
def test_sanity_iso8859_1(self):
|
||||
self._test_sanity("iso8859-1")
|
||||
|
||||
def test_sanity_iso8859_2(self):
|
||||
self._test_sanity("iso8859-2")
|
||||
|
||||
def test_sanity_cp1250(self):
|
||||
self._test_sanity("cp1250")
|
||||
|
||||
# def test_invalid_file(self):
|
||||
# with open("Tests/images/flower.jpg", "rb") as fp:
|
||||
# self.assertRaises(SyntaxError, PcfFontFile.PcfFontFile, fp)
|
||||
|
||||
def _test_draw(self, encoding):
|
||||
tempname = self.save_font(encoding)
|
||||
font = ImageFont.load(tempname)
|
||||
im = Image.new("L", (150, 30), "white")
|
||||
draw = ImageDraw.Draw(im)
|
||||
message = charsets[encoding]["message"].encode(encoding)
|
||||
draw.text((0, 0), message, "black", font=font)
|
||||
with Image.open(charsets[encoding]["image1"]) as target:
|
||||
self.assert_image_similar(im, target, 0)
|
||||
|
||||
def test_draw_iso8859_1(self):
|
||||
self._test_draw("iso8859-1")
|
||||
|
||||
def test_draw_iso8859_2(self):
|
||||
self._test_draw("iso8859-2")
|
||||
|
||||
def test_draw_cp1250(self):
|
||||
self._test_draw("cp1250")
|
||||
|
||||
def _test_textsize(self, encoding):
|
||||
tempname = self.save_font(encoding)
|
||||
font = ImageFont.load(tempname)
|
||||
for i in range(255):
|
||||
(dx, dy) = font.getsize(bytearray([i]))
|
||||
self.assertEqual(dy, 20)
|
||||
self.assertIn(dx, (0, 10))
|
||||
message = charsets[encoding]["message"].encode(encoding)
|
||||
for l in range(len(message)):
|
||||
msg = message[: l + 1]
|
||||
self.assertEqual(font.getsize(msg), (len(msg) * 10, 20))
|
||||
|
||||
def test_textsize_iso8859_1(self):
|
||||
self._test_textsize("iso8859-1")
|
||||
|
||||
def test_textsize_iso8859_2(self):
|
||||
self._test_textsize("iso8859-2")
|
||||
|
||||
def test_textsize_cp1250(self):
|
||||
self._test_textsize("cp1250")
|
||||
|
||||
# def _test_high_characters(self, message, encoding):
|
||||
# tempname = self.save_font(encoding)
|
||||
# font = ImageFont.load(tempname)
|
||||
# im = Image.new("L", (750, 30), "white")
|
||||
# draw = ImageDraw.Draw(im)
|
||||
# draw.text((0, 0), message, "black", font=font)
|
||||
# with Image.open("Tests/images/high_ascii_chars.png") as target:
|
||||
# self.assert_image_similar(im, target, 0)
|
||||
#
|
||||
# def test_high_characters(self):
|
||||
# message = "".join(chr(i + 1) for i in range(140, 232))
|
||||
# self._test_high_characters(message)
|
||||
# # accept bytes instances in Py3.
|
||||
# if py3:
|
||||
# self._test_high_characters(message.encode("latin1"))
|
Loading…
Reference in New Issue
Block a user