Skip to content

Commit 390b34c

Browse files
authored
Merge pull request #4664 from nulano/ft-getsize-mode
2 parents 291d1d9 + 2dd9324 commit 390b34c

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

Tests/images/text_mono.gif

1.52 KB
Loading

Tests/test_imagefont.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .helper import (
1313
assert_image_equal,
14+
assert_image_equal_tofile,
1415
assert_image_similar,
1516
assert_image_similar_tofile,
1617
is_pypy,
@@ -736,3 +737,19 @@ def test_variation_set_by_axes(self):
736737
@skip_unless_feature("raqm")
737738
class TestImageFont_RaqmLayout(TestImageFont):
738739
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
740+
741+
742+
def test_render_mono_size():
743+
# issue 4177
744+
745+
if distutils.version.StrictVersion(ImageFont.core.freetype2_version) < "2.4":
746+
pytest.skip("Different metrics")
747+
748+
im = Image.new("P", (100, 30), "white")
749+
draw = ImageDraw.Draw(im)
750+
ttf = ImageFont.truetype(
751+
"Tests/fonts/DejaVuSans.ttf", 18, layout_engine=ImageFont.LAYOUT_BASIC
752+
)
753+
754+
draw.text((10, 10), "r" * 10, "black", ttf)
755+
assert_image_equal_tofile(im, "Tests/images/text_mono.gif")

src/PIL/ImageFont.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def getsize(
259259
260260
:return: (width, height)
261261
"""
262-
size, offset = self.font.getsize(text, direction, features, language)
262+
size, offset = self.font.getsize(text, False, direction, features, language)
263263
return (
264264
size[0] + stroke_width * 2 + offset[0],
265265
size[1] + stroke_width * 2 + offset[1],
@@ -468,7 +468,9 @@ def getmask2(
468468
:py:mod:`PIL.Image.core` interface module, and the text offset, the
469469
gap between the starting coordinate and the first marking
470470
"""
471-
size, offset = self.font.getsize(text, direction, features, language)
471+
size, offset = self.font.getsize(
472+
text, mode == "1", direction, features, language
473+
)
472474
size = size[0] + stroke_width * 2, size[1] + stroke_width * 2
473475
im = fill("L", size, 0)
474476
self.font.render(

src/_imagingft.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ font_getsize(FontObject* self, PyObject* args)
609609
FT_Face face;
610610
int xoffset, yoffset;
611611
int horizontal_dir;
612+
int mask = 0;
613+
int load_flags;
612614
const char *dir = NULL;
613615
const char *lang = NULL;
614616
size_t i, count;
@@ -618,11 +620,11 @@ font_getsize(FontObject* self, PyObject* args)
618620
/* calculate size and bearing for a given string */
619621

620622
PyObject* string;
621-
if (!PyArg_ParseTuple(args, "O|zOz:getsize", &string, &dir, &features, &lang)) {
623+
if (!PyArg_ParseTuple(args, "O|izOz:getsize", &string, &mask, &dir, &features, &lang)) {
622624
return NULL;
623625
}
624626

625-
count = text_layout(string, self, dir, features, lang, &glyph_info, 0);
627+
count = text_layout(string, self, dir, features, lang, &glyph_info, mask);
626628
if (PyErr_Occurred()) {
627629
return NULL;
628630
}
@@ -641,7 +643,11 @@ font_getsize(FontObject* self, PyObject* args)
641643
/* Note: bitmap fonts within ttf fonts do not work, see #891/pr#960
642644
* Yifu Yu<root@jackyyf.com>, 2014-10-15
643645
*/
644-
error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP);
646+
load_flags = FT_LOAD_NO_BITMAP;
647+
if (mask) {
648+
load_flags |= FT_LOAD_TARGET_MONO;
649+
}
650+
error = FT_Load_Glyph(face, index, load_flags);
645651
if (error) {
646652
return geterror(error);
647653
}

0 commit comments

Comments
 (0)