Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent multiline spacing and tests, with extra space #1574

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ def text(self, xy, text, fill=None, font=None, anchor=None):
self.draw.draw_bitmap(xy, mask, ink)

def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
spacing=0, align="left"):
widths, heights = [], []
spacing=4, align="left"):
widths = []
max_width = 0
lines = self._multiline_split(text)
line_spacing = self.textsize('A', font=font)[1] + spacing
for line in lines:
line_width, line_height = self.textsize(line, font)
widths.append(line_width)
max_width = max(max_width, line_width)
heights.append(line_height)
left, top = xy
for idx, line in enumerate(lines):
if align == "left":
Expand All @@ -287,7 +287,7 @@ def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
else:
assert False, 'align must be "left", "center" or "right"'
self.text((left, top), line, fill, font, anchor)
top += heights[idx] + spacing
top += line_spacing
left = xy[0]

##
Expand All @@ -305,11 +305,11 @@ def multiline_textsize(self, text, font=None, spacing=0):
max_width = 0
height = 0
lines = self._multiline_split(text)
line_spacing = self.textsize('A', font=font)[1] + spacing
for line in lines:
line_width, line_height = self.textsize(line, font)
height += line_height + spacing
max_width = max(max_width, line_width)
return max_width, height
return max_width, len(lines)*line_spacing


##
Expand Down
Binary file modified Tests/images/multiline_text_spacing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions _imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,25 @@ font_getattr_descent(FontObject* self, void* closure)
return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender));
}

static PyObject*
font_getattr_height(FontObject* self, void* closure)
{
return PyInt_FromLong(PIXEL(self->face->size->metrics.height));
}

static PyObject*
font_getattr_x_ppem(FontObject* self, void* closure)
{
return PyInt_FromLong(self->face->size->metrics.x_ppem);
}

static PyObject*
font_getattr_y_ppem(FontObject* self, void* closure)
{
return PyInt_FromLong(self->face->size->metrics.y_ppem);
}


static PyObject*
font_getattr_glyphs(FontObject* self, void* closure)
{
Expand All @@ -503,6 +522,9 @@ static struct PyGetSetDef font_getsetters[] = {
{ "style", (getter) font_getattr_style },
{ "ascent", (getter) font_getattr_ascent },
{ "descent", (getter) font_getattr_descent },
{ "height", (getter) font_getattr_height },
{ "x_ppem", (getter) font_getattr_x_ppem },
{ "y_ppem", (getter) font_getattr_y_ppem },
{ "glyphs", (getter) font_getattr_glyphs },
{ NULL }
};
Expand Down