Skip to content

Factor out saving metrics in FontFile#9417

Closed
fjhenigman wants to merge 1 commit intopython-pillow:mainfrom
fjhenigman:savemet
Closed

Factor out saving metrics in FontFile#9417
fjhenigman wants to merge 1 commit intopython-pillow:mainfrom
fjhenigman:savemet

Conversation

@fjhenigman
Copy link

@fjhenigman fjhenigman commented Feb 4, 2026

Facilitates creating an ImageFont from a PCF or BDF file on the fly, without creating any new files, with code like this:

def ifont(path):
    with open(path, 'rb') as f:
        ff = PcfFontFile.PcfFontFile(f)
    ff.compile()
    buf = io.BytesIO()
    ff.save_metrics(buf)
    buf.seek(0)
    imgfont = ImageFont.ImageFont()
    imgfont._load_pilfont_data(buf, ff.bitmap)
    return imgfont

This isn't ideal, creating a file in memory and using an underscore function. Maybe it's preferable to add functionality to ImageFont.py instead?

@radarhere radarhere changed the title Factor out saving metrics in FontFile. Factor out saving metrics in FontFile Feb 4, 2026
@radarhere
Copy link
Member

Hi. To outline the problem, you've said that you would like to convert FontFile instances to ImageFont instances.

I'm guessing you saw https://pillow.readthedocs.io/en/stable/reference/ImageFont.html#imagefont-module and https://github.com/python-pillow/pillow-scripts/blob/main/Scripts/pilfont.py. They've sort of presumed that you only need to convert once, or maybe only convert a small number of fonts, but presumably you would like your users to be able to make this conversion for any PCF or BDF font they might choose at runtime, and you would like to do so efficiently.

My suggestion would be adding a to_imagefont() method to FontFile. What do you think?

Also, just to properly understand the need, what is it that you want to do with the FontFile instances? Draw text?

@fjhenigman
Copy link
Author

Yes exactly, I am using ImageDraw to draw text and would like to support bdf and pcf fonts. If it was just for me or just a couple fonts, I would use pilfont.py to convert them and deal with the resulting bitmaps and .pil files. But for an application with wider use, it would be nice if any user could directly feed it any pcf or bdf files they may have.
I will prepare another PR adding a to_imagefont() method to FontFile as you suggest.
Close this PR unless you want it.
Thanks.

@radarhere radarhere closed this Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants