Skip to content

Commit

Permalink
Merge pull request #333 from wiredfool/paddywwoof-truetype
Browse files Browse the repository at this point in the history
Vertical Alignment of ttf rendering
  • Loading branch information
aclark4life committed Sep 24, 2013
2 parents 9bb76ea + ae6eb87 commit dea45da
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions PIL/ImageColor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
5 changes: 4 additions & 1 deletion PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
Binary file added Tests/images/multiline_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_font_with_filelike():
#shared_bytes = _font_as_bytes()
#assert_no_exception(lambda: _render(shared_bytes))
#assert_exception(Exception, lambda: _render(shared_bytes))
_clean
_clean()

def test_font_with_open_file():
with open(font_path, 'rb') as f:
Expand Down Expand Up @@ -67,3 +67,21 @@ def test_render_equal():

assert_image_equal(img_path, img_filelike)
_clean()


def test_render_multiline():
im = Image.new(mode='RGB', size=(300,100))
ttf = ImageFont.truetype(font_path, font_size)
draw = ImageDraw.Draw(im)
line_spacing = draw.textsize('A', font=ttf)[1] + 8
lines = ['hey you', 'you are awesome', 'this looks awkward']
y = 0
for line in lines:
draw.text((0, y), line, font=ttf)
y += line_spacing

target = 'Tests/images/multiline_text.png'
target_img = Image.open(target)

assert_image_equal(im, target_img)

15 changes: 11 additions & 4 deletions _imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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++) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
);
}

Expand Down

0 comments on commit dea45da

Please sign in to comment.