Skip to content

Do not return negative width for text length#9623

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:getlength
Jun 19, 2026
Merged

Do not return negative width for text length#9623
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:getlength

Conversation

@radarhere

@radarhere radarhere commented May 11, 2026

Copy link
Copy Markdown
Member

Pillow/src/_imaging.c

Lines 2774 to 2783 in 24696af

static inline int
textwidth(ImagingFontObject *self, const unsigned char *text) {
int xsize;
for (xsize = 0; *text; text++) {
xsize += self->glyphs[*text].dx;
}
return xsize;
}

It is possible for dx to be negative. While there's probably scenarios where that makes sense for an individual glyph, when it comes to the total text width, I don't think it makes sense to say that that is negative.

This function determines the value returned by getlength(). I think it's more helpful to return zero. This is likely to be used for a drawing operation, and if nothing would be drawn, then what's the difference?

@radarhere radarhere merged commit 87e7883 into python-pillow:main Jun 19, 2026
57 checks passed
@radarhere radarhere deleted the getlength branch June 19, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant