From 25b39de5b700e5e14727b431645196628547b950 Mon Sep 17 00:00:00 2001 From: paddywwoof Date: Thu, 19 Sep 2013 15:30:08 +0100 Subject: [PATCH 1/2] ImageColor defaults to alpha = 255 in only rgb specified for RGBA mode --- PIL/ImageColor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PIL/ImageColor.py b/PIL/ImageColor.py index 2a190fc66ca..86f221adca2 100644 --- a/PIL/ImageColor.py +++ b/PIL/ImageColor.py @@ -102,6 +102,8 @@ def getcolor(color, mode): if mode == "RGB": return color if mode == "RGBA": + if len(color) == 3: + color = (color + (255,)) r, g, b, a = color return r, g, b, a if Image.getmodebase(mode) == "L": From 02855a86c07afadf380c018152197f6b6dbae5c6 Mon Sep 17 00:00:00 2001 From: paddywwoof Date: Fri, 20 Sep 2013 14:02:40 +0100 Subject: [PATCH 2/2] hopefully this uses a reasonable y offset --- PIL/ImageFont.py | 5 ++++- _imagingft.c | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 2cb9c41d21d..826b37d5c38 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -157,6 +157,9 @@ def getmetrics(self): def getsize(self, text): return self.font.getsize(text)[0] + def getoffset(self, text): + return self.font.getsize(text)[1] + def getmask(self, text, mode=""): return self.getmask2(text, mode)[0] @@ -183,7 +186,7 @@ def __init__(self, font, orientation=None): self.orientation = orientation # any 'transpose' argument, or None def getsize(self, text): - w, h = self.font.getsize(text) + w, h = self.font.getsize(text)[0] if self.orientation in (Image.ROTATE_90, Image.ROTATE_270): return h, w return w, h diff --git a/_imagingft.c b/_imagingft.c index 9dcc8318395..47d50bdca20 100644 --- a/_imagingft.c +++ b/_imagingft.c @@ -183,7 +183,7 @@ font_getsize(FontObject* self, PyObject* args) int i, x, y_max, y_min; FT_ULong ch; FT_Face face; - int xoffset; + int xoffset, yoffset; FT_Bool kerning = FT_HAS_KERNING(self->face); FT_UInt last_index = 0; @@ -203,7 +203,7 @@ font_getsize(FontObject* self, PyObject* args) } face = NULL; - xoffset = 0; + xoffset = yoffset = 0; y_max = y_min = 0; for (x = i = 0; font_getchar(string, i, &ch); i++) { @@ -231,7 +231,11 @@ font_getsize(FontObject* self, PyObject* args) y_max = bbox.yMax; if (bbox.yMin < y_min) y_min = bbox.yMin; - + + /* find max distance of baseline from top */ + if (face->glyph->metrics.horiBearingY > yoffset) + yoffset = face->glyph->metrics.horiBearingY; + last_index = index; } @@ -248,12 +252,15 @@ font_getsize(FontObject* self, PyObject* args) face->glyph->metrics.horiBearingX; if (offset < 0) x -= offset; + /* difference between the font ascender and the distance of + * the baseline from the top */ + yoffset = PIXEL(self->face->size->metrics.ascender - yoffset); } return Py_BuildValue( "(ii)(ii)", PIXEL(x), PIXEL(y_max - y_min), - PIXEL(xoffset), 0 + PIXEL(xoffset), yoffset ); }