Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

If bitmap buffer is empty, do not render anything

Open radarhere opened this issue 1 year ago • 5 comments

Resolves #8272

#6846 added an error if the bitmap buffer is empty. https://github.com/python-pillow/Pillow/blob/d6cfebd016db25549d05a9c5caa2ef3b53cff5c5/src/_imagingft.c#L1028-L1032

However, this new issue found the buffer is empty for a space character in a particular font, which seems like a realistic scenario, and has been confirmed as valid by one of the FreeType maintainers - https://github.com/python-pillow/Pillow/issues/8272#issuecomment-2305870693

So if the bitmap buffer is empty, this PR just doesn't draw anything, uses the advances to update the position for the next character, and moves on.

radarhere avatar Aug 23 '24 08:08 radarhere

We might want to add a test that triggers the if (stroker != NULL) branch in font_render to make sure that passing such a glyph to the stroker API is safe.

nulano avatar Aug 23 '24 16:08 nulano

You're concerned about passing a glyph where bitmap buffer is empty to this block? test_bitmap_font_stroke actually already does this.

radarhere avatar Aug 24 '24 09:08 radarhere

Note then the glyph outline in question is not empty but degenerate. It contains two points but still produces a blank space. In this particular case, it results in a 0×1 bitmap, aka one empty row. Do not check for bitmap.rows, or check both dimentions

apodtele avatar Aug 24 '24 11:08 apodtele

The glyph outline may not be empty, but the bitmap buffer is. From my understanding, we're safe to skip drawing and just advance if that is the case. I presume the size of the glyph does not affect the advance.

radarhere avatar Aug 24 '24 12:08 radarhere

Correct, just advance if the buffer is NULL regardless of its dimensions. You have sufficient error checking from FreeType calls.

apodtele avatar Aug 24 '24 12:08 apodtele