Skip to content

Commit 2f75ee7

Browse files
committed
Merge pull request #1574 from hugovk/wiredfool-multiline_text-plus4
Consistent multiline spacing and tests, with extra space
2 parents 7563915 + 731d0b1 commit 2f75ee7

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

PIL/ImageDraw.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@ def text(self, xy, text, fill=None, font=None, anchor=None):
267267
self.draw.draw_bitmap(xy, mask, ink)
268268

269269
def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
270-
spacing=0, align="left"):
271-
widths, heights = [], []
270+
spacing=4, align="left"):
271+
widths = []
272272
max_width = 0
273273
lines = self._multiline_split(text)
274+
line_spacing = self.textsize('A', font=font)[1] + spacing
274275
for line in lines:
275276
line_width, line_height = self.textsize(line, font)
276277
widths.append(line_width)
277278
max_width = max(max_width, line_width)
278-
heights.append(line_height)
279279
left, top = xy
280280
for idx, line in enumerate(lines):
281281
if align == "left":
@@ -287,7 +287,7 @@ def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
287287
else:
288288
assert False, 'align must be "left", "center" or "right"'
289289
self.text((left, top), line, fill, font, anchor)
290-
top += heights[idx] + spacing
290+
top += line_spacing
291291
left = xy[0]
292292

293293
##
@@ -305,11 +305,11 @@ def multiline_textsize(self, text, font=None, spacing=0):
305305
max_width = 0
306306
height = 0
307307
lines = self._multiline_split(text)
308+
line_spacing = self.textsize('A', font=font)[1] + spacing
308309
for line in lines:
309310
line_width, line_height = self.textsize(line, font)
310-
height += line_height + spacing
311311
max_width = max(max_width, line_width)
312-
return max_width, height
312+
return max_width, len(lines)*line_spacing
313313

314314

315315
##
-4 Bytes
Loading

_imagingft.c

+22
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,25 @@ font_getattr_descent(FontObject* self, void* closure)
492492
return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender));
493493
}
494494

495+
static PyObject*
496+
font_getattr_height(FontObject* self, void* closure)
497+
{
498+
return PyInt_FromLong(PIXEL(self->face->size->metrics.height));
499+
}
500+
501+
static PyObject*
502+
font_getattr_x_ppem(FontObject* self, void* closure)
503+
{
504+
return PyInt_FromLong(self->face->size->metrics.x_ppem);
505+
}
506+
507+
static PyObject*
508+
font_getattr_y_ppem(FontObject* self, void* closure)
509+
{
510+
return PyInt_FromLong(self->face->size->metrics.y_ppem);
511+
}
512+
513+
495514
static PyObject*
496515
font_getattr_glyphs(FontObject* self, void* closure)
497516
{
@@ -503,6 +522,9 @@ static struct PyGetSetDef font_getsetters[] = {
503522
{ "style", (getter) font_getattr_style },
504523
{ "ascent", (getter) font_getattr_ascent },
505524
{ "descent", (getter) font_getattr_descent },
525+
{ "height", (getter) font_getattr_height },
526+
{ "x_ppem", (getter) font_getattr_x_ppem },
527+
{ "y_ppem", (getter) font_getattr_y_ppem },
506528
{ "glyphs", (getter) font_getattr_glyphs },
507529
{ NULL }
508530
};

0 commit comments

Comments
 (0)