Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Added FreeTypeFont has_characters()

Open radarhere opened this issue 4 months ago • 2 comments

Resolves #9158. The issue requested a method to check if glyphs are present in a font. This question has arisen previously in https://github.com/python-pillow/Pillow/issues/7380#issuecomment-1708041463, and I think this would also help users from #4808, in a less elegant way.

This adds FreeTypeFont.has_characters(text). I expect that the issue was only thinking about checking a single character at a time, but if user wanted to check multiple characters at once, I don't see any reason not to let them. If any character in the string is missing, then False is returned.

The C code in here is based on https://github.com/python-pillow/Pillow/blob/9d39fe6adac233ccd26ba20bff9adda53a1ec89d/src/_imagingft.c#L432-L463

radarhere avatar Aug 17 '25 22:08 radarhere

I'm on the fence with this. It feels outside the core focus of Pillow, and it's not too tricky to do this with Pillow in Python:

https://github.com/python-pillow/Pillow/issues/9158#issuecomment-3194350459

Plus it's a even easier with a dedicated FreeType library:

https://github.com/python-pillow/Pillow/issues/9158#issue-3328268124

hugovk avatar Oct 15 '25 08:10 hugovk

A font is roughly a collection of images and checking if an image exists in the collection is pretty basic functionality?

Of course it's doable in other ways but:

  1. The pillow way suggested is verbose and inefficient.
  2. The freetype way requires loading the font twice (once in Pillow, once in Freetype).

glebm avatar Oct 20 '25 10:10 glebm