Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Bitmap missing for glyph

Open joaohaas opened this issue 1 year ago • 4 comments

What did you do?

Tried to call ImageDraw.text((15, 36), "A B") with the following font: FS Lola Medium Regular.zip

What did you expect to happen?

Either a successful call, since that font is rendered correctly on browsers and as a system font, or the usage of one of the 'unidentified characters' (□, �).

What actually happened?

Because of the whitespace, the call fails with a OSError: Bitmap missing for glyph exception on PIL/ImageFont.py:607. I'm not exactly sure if there's any actual error with the font file, but as mentioned, it works fine on browsers and as a system font.

What are your OS, Python and Pillow versions?

  • OS: MacOS 13.3.1, Ubuntu 20.04
  • Python: 3.8
  • Pillow: 10.4
--------------------------------------------------------------------
Pillow 10.4.0
Python 3.8.18 (default, Nov  2 2023, 16:50:59)
       [Clang 14.0.3 (clang-1403.0.22.14.1)]
--------------------------------------------------------------------
Python executable is /Users/joaohaas/dev/server/env/bin/python3
Environment Python files loaded from /Users/joaohaas/dev/server/env
System Python files loaded from /opt/homebrew/Cellar/[email protected]/3.8.18_1/Frameworks/Python.framework/Versions/3.8
--------------------------------------------------------------------
Python Pillow modules loaded from /Users/joaohaas/dev/server/env/lib/python3.8/site-packages/PIL
Binary Pillow modules loaded from /Users/joaohaas/dev/server/env/lib/python3.8/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.14, harfbuzz 8.5.0
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("<path>/FS Lola Medium Regular.ttf", size=27)
image = Image.new("RGB", (440, 58), (255, 255, 255))
draw_object = ImageDraw.Draw(image)
draw_object.text((15, 36), "A B", font=font)

joaohaas avatar Jul 31 '24 17:07 joaohaas

It is absolutely normal for FreeType to render a NULL-buffer without any errors returned. particularly for whitespace characters. The size of such bitmaps will also be 0x0, with advance properly assigned, however.

apodtele avatar Aug 22 '24 22:08 apodtele

Thanks very much @apodtele

I've created #8324 to resolve this by drawing an empty character.

radarhere avatar Aug 23 '24 08:08 radarhere

The font is interesting indeed. The space character contains a degenerate contour of two points (0,0) and (0,1). So the output is NULL of size 0x1 (one raw of zero pixels each), i.e. bitmap.width = 0, bitmap.rows = 1. But it could be something else depending on the rendering mode.

I opened https://gitlab.freedesktop.org/freetype/freetype/-/issues/1295, if you have an opinion.

apodtele avatar Aug 24 '24 03:08 apodtele

If it is degenerate (https://lists.nongnu.org/archive/html/freetype-devel/2012-04/msg00068.html, "A degenerate contour is one that does not change the rendering if removed.") and I presume the size of the glyph doesn't affect the advance, then output makes no difference for rendering operations that use the same settings, right?

radarhere avatar Aug 24 '24 12:08 radarhere