From 93b4321665e379b21381e82e9089ff7024b3ee7c Mon Sep 17 00:00:00 2001 From: Micah Chambers Date: Fri, 20 May 2016 06:19:00 +0000 Subject: [PATCH] fixed tests, and memory leak --- PIL/ImageFont.py | 3 ++- Tests/test_imagefont.py | 14 ++++++++------ _imagingft.c | 9 +++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index b0c4c5092..e991d2f6d 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -180,7 +180,8 @@ class FreeTypeFont(object): Return a string containing all the characters with matching glyphs in the font """ - return ''.join(unichr(code) for code in self.font.getglyphs()) + codes = self.font.getglyphs() + return ''.join(unichr(code) for code in codes) ## # Wrapper that creates a transposed font from any existing font diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 6e93fe893..777d4bb27 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -52,9 +52,10 @@ try: ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) self.assertEqual(ttf.path, FONT_PATH) self.assertEqual(ttf.size, FONT_SIZE) - self.assertTrue(ttf.hasglyph('a')) - self.assertFalse(ttf.hasglyph('\ueeee')) - self.assertEqual(ttf.getglyphs(), '....') + self.assertTrue(ttf.hasglyphs('a')) + self.assertFalse(ttf.hasglyphs(u'a\ueeee')) + self.assertEqual(len(ttf.getglyphs()), 4160) + self.assertIn('a', ttf.getglyphs()) ttf_copy = ttf.font_variant() self.assertEqual(ttf_copy.path, FONT_PATH) @@ -66,9 +67,10 @@ try: second_font_path = "Tests/fonts/DejaVuSans.ttf" ttf_copy = ttf.font_variant(font=second_font_path) self.assertEqual(ttf_copy.path, second_font_path) - self.assertTrue(ttf_copy.hasglyph('a')) - self.assertFalse(ttf_copy.hasglyph('\ueeee')) - self.assertEqual(ttf_copy.getglyphs(), '....') + self.assertTrue(ttf_copy.hasglyphs('ae')) + self.assertFalse(ttf_copy.hasglyphs(u'a\ueeee')) + self.assertEqual(len(ttf.getglyphs()), 4160) + self.assertIn('a', ttf.getglyphs()) def test_font_with_name(self): ImageFont.truetype(FONT_PATH, FONT_SIZE) diff --git a/_imagingft.c b/_imagingft.c index ca64ef18f..e277e1671 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -352,14 +352,15 @@ font_hasglyphs(FontObject* self, PyObject* args) static PyObject* font_getglyphs(FontObject* self) { - int ii = 0; FT_UInt gindex = 0; - PyObject* characters = PyList_New(self->face->num_glyphs); + PyObject* characters = PyList_New(0); + if(!characters) + return PyErr_NoMemory(); FT_ULong charcode = 0; charcode = FT_Get_First_Char(self->face, &gindex); - for(ii = 0; gindex != 0 && ii < self->face->num_glyphs; ii++) { - PyList_SetItem(characters, ii, PyLong_FromUnsignedLong(charcode)); + while( gindex != 0 ) { + PyList_Append(characters, PyLong_FromUnsignedLong(charcode)); charcode = FT_Get_Next_Char(self->face, charcode, &gindex); }