[Image-SIG] Fix for left bearing of first character in a string

Ned Batchelder ned at nedbatchelder.com
Sun Feb 3 01:14:40 CET 2008


In PIL 1.1.6, ImageDraw.text will position the string incorrectly if the 
first character has a negative left bearing.  To see the problem, show a 
string like "///" in an italic font.  The first slash will be clipped at 
the left, and the string will be mis-positioned.

This patch fixes it:

--- C:\src\PIL\Imaging-1.1.6\_imagingft.c       2006-12-03 
06:51:26.000000000 -0500
+++ C:\src\PIL\mine\_imagingft.c        2008-02-02 16:39:20.000000000 -0500
@@ -316,13 +316,13 @@
     load_flags = FT_LOAD_RENDER;
     if (mask)
         load_flags |= FT_LOAD_TARGET_MONO;

     for (x = i = 0; font_getchar(string, i, &ch); i++) {
         if (i == 0 && self->face->glyph->metrics.horiBearingX < 0)
-            x = PIXEL(self->face->glyph->metrics.horiBearingX);
+            x = -PIXEL(self->face->glyph->metrics.horiBearingX);
         index = FT_Get_Char_Index(self->face, ch);
         if (kerning && last_index && index) {
             FT_Vector delta;
             FT_Get_Kerning(self->face, last_index, index, 
ft_kerning_default,
                            &delta);
             x += delta.x >> 6;

--Ned.

-- 
Ned Batchelder, http://nedbatchelder.com


More information about the Image-SIG mailing list