diff --git a/Tests/fonts/ArefRuqaa-Regular.ttf b/Tests/fonts/ArefRuqaa-Regular.ttf new file mode 100644 index 00000000000..940cb58f4dc Binary files /dev/null and b/Tests/fonts/ArefRuqaa-Regular.ttf differ diff --git a/Tests/fonts/LICENSE.txt b/Tests/fonts/LICENSE.txt index ee9daee592c..55283afeee3 100644 --- a/Tests/fonts/LICENSE.txt +++ b/Tests/fonts/LICENSE.txt @@ -1,9 +1,8 @@ -NotoNastaliqUrdu-Regular.ttf: +NotoNastaliqUrdu-Regular.ttf (from https://github.com/googlei18n/noto-fonts) +ArefRuqaa-Regular.ttf (from https://github.com/google/fonts/tree/master/ofl/arefruqaa) -(from https://github.com/googlei18n/noto-fonts) - -All Noto 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. +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. 10x20-ISO8859-1.pcf diff --git a/Tests/images/test_x_max_and_y_offset.png b/Tests/images/test_x_max_and_y_offset.png new file mode 100644 index 00000000000..f8bec3e95e5 Binary files /dev/null and b/Tests/images/test_x_max_and_y_offset.png differ diff --git a/Tests/test_imagefontctl.py b/Tests/test_imagefontctl.py index 3c498332c68..b3e5a34cde7 100644 --- a/Tests/test_imagefontctl.py +++ b/Tests/test_imagefontctl.py @@ -143,6 +143,18 @@ def test_arabictext_features(self): self.assert_image_similar(im, target_img, .5) + def test_x_max_and_y_offset(self): + ttf = ImageFont.truetype("Tests/fonts/ArefRuqaa-Regular.ttf", 40) + + im = Image.new(mode='RGB', size=(50, 100)) + draw = ImageDraw.Draw(im) + draw.text((0, 0), 'لح', font=ttf, fill=500) + + target = 'Tests/images/test_x_max_and_y_offset.png' + target_img = Image.open(target) + + self.assert_image_similar(im, target_img, .5) + def test_language(self): ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE) diff --git a/src/_imagingft.c b/src/_imagingft.c index 512caeb4b13..ae7ebe6d649 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -604,7 +604,7 @@ text_layout(PyObject* string, FontObject* self, const char* dir, PyObject *featu static PyObject* font_getsize(FontObject* self, PyObject* args) { - int i, x, y_max, y_min; + int i, x, x_max, y_max, y_min; FT_Face face; int xoffset, yoffset; const char *dir = NULL; @@ -621,7 +621,7 @@ font_getsize(FontObject* self, PyObject* args) face = NULL; xoffset = yoffset = 0; - y_max = y_min = 0; + x_max = y_max = y_min = 0; count = text_layout(string, self, dir, features, lang, &glyph_info, 0); if (PyErr_Occurred()) { @@ -630,7 +630,7 @@ font_getsize(FontObject* self, PyObject* args) for (x = i = 0; i < count; i++) { - int index, error; + int index, error, offset, x_advanced; FT_BBox bbox; FT_Glyph glyph; face = self->face; @@ -649,15 +649,14 @@ font_getsize(FontObject* self, PyObject* args) x += glyph_info[i].x_advance; - if (i == count - 1) - { - int offset; - offset = glyph_info[i].x_advance - - face->glyph->metrics.width - - face->glyph->metrics.horiBearingX; - if (offset < 0) - x -= offset; - } + x_advanced = x; + offset = glyph_info[i].x_advance - + face->glyph->metrics.width - + face->glyph->metrics.horiBearingX; + if (offset < 0) + x_advanced -= offset; + if (x_advanced > x_max) + x_max = x_advanced; FT_Get_Glyph(face->glyph, &glyph); FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_SUBPIXELS, &bbox); @@ -684,7 +683,7 @@ font_getsize(FontObject* self, PyObject* args) /* left bearing */ if (xoffset < 0) - x -= xoffset; + x_max -= xoffset; else xoffset = 0; /* difference between the font ascender and the distance of @@ -694,7 +693,7 @@ font_getsize(FontObject* self, PyObject* args) return Py_BuildValue( "(ii)(ii)", - PIXEL(x), PIXEL(y_max - y_min), + PIXEL(x_max), PIXEL(y_max - y_min), PIXEL(xoffset), yoffset ); }