Merge pull request #1574 from hugovk/wiredfool-multiline_text-plus4

Consistent multiline spacing and tests, with extra space
This commit is contained in:
wiredfool 2015-12-07 13:41:15 +00:00
commit 2f75ee7e9f
3 changed files with 28 additions and 6 deletions

View File

@ -267,15 +267,15 @@ class ImageDraw(object):
self.draw.draw_bitmap(xy, mask, ink) self.draw.draw_bitmap(xy, mask, ink)
def multiline_text(self, xy, text, fill=None, font=None, anchor=None, def multiline_text(self, xy, text, fill=None, font=None, anchor=None,
spacing=0, align="left"): spacing=4, align="left"):
widths, heights = [], [] widths = []
max_width = 0 max_width = 0
lines = self._multiline_split(text) lines = self._multiline_split(text)
line_spacing = self.textsize('A', font=font)[1] + spacing
for line in lines: for line in lines:
line_width, line_height = self.textsize(line, font) line_width, line_height = self.textsize(line, font)
widths.append(line_width) widths.append(line_width)
max_width = max(max_width, line_width) max_width = max(max_width, line_width)
heights.append(line_height)
left, top = xy left, top = xy
for idx, line in enumerate(lines): for idx, line in enumerate(lines):
if align == "left": if align == "left":
@ -287,7 +287,7 @@ class ImageDraw(object):
else: else:
assert False, 'align must be "left", "center" or "right"' assert False, 'align must be "left", "center" or "right"'
self.text((left, top), line, fill, font, anchor) self.text((left, top), line, fill, font, anchor)
top += heights[idx] + spacing top += line_spacing
left = xy[0] left = xy[0]
## ##
@ -305,11 +305,11 @@ class ImageDraw(object):
max_width = 0 max_width = 0
height = 0 height = 0
lines = self._multiline_split(text) lines = self._multiline_split(text)
line_spacing = self.textsize('A', font=font)[1] + spacing
for line in lines: for line in lines:
line_width, line_height = self.textsize(line, font) line_width, line_height = self.textsize(line, font)
height += line_height + spacing
max_width = max(max_width, line_width) max_width = max(max_width, line_width)
return max_width, height return max_width, len(lines)*line_spacing
## ##

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -492,6 +492,25 @@ font_getattr_descent(FontObject* self, void* closure)
return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender)); 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* static PyObject*
font_getattr_glyphs(FontObject* self, void* closure) font_getattr_glyphs(FontObject* self, void* closure)
{ {
@ -503,6 +522,9 @@ static struct PyGetSetDef font_getsetters[] = {
{ "style", (getter) font_getattr_style }, { "style", (getter) font_getattr_style },
{ "ascent", (getter) font_getattr_ascent }, { "ascent", (getter) font_getattr_ascent },
{ "descent", (getter) font_getattr_descent }, { "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 }, { "glyphs", (getter) font_getattr_glyphs },
{ NULL } { NULL }
}; };