Skip to content

Add FontFile.to_imagefont()#9419

Open
fjhenigman wants to merge 10 commits intopython-pillow:mainfrom
fjhenigman:usepcf
Open

Add FontFile.to_imagefont()#9419
fjhenigman wants to merge 10 commits intopython-pillow:mainfrom
fjhenigman:usepcf

Conversation

@fjhenigman
Copy link

Following up #9417.
This makes it possible for applications to directly use pcf and bdf font files, instead of going through a conversion step that creates new files.
A little refactoring in ImageFont so that metrics can be passed directly might make this cleaner.
If that makes sense let me know and I can update this PR.

@radarhere radarhere changed the title Add FontFile.to_imagefont(). Add FontFile.to_imagefont() Feb 5, 2026
@radarhere
Copy link
Member

I've created fjhenigman#1 with some suggestions.

@fjhenigman
Copy link
Author

fjhenigman commented Feb 12, 2026

thanks, I merged those in.
let me know if there's anything else I can do.

@fjhenigman
Copy link
Author

Sorry for not knowing how this works...
Do I need to do something? Thanks.

@radarhere
Copy link
Member

No problem. No, you don't need to do anything. As far as I'm concerned, this is ready to be merged. I'd just like to give one of the other core team members a chance to look it over.

Our next release is on April 1, so I don't think it makes a difference when this is merged between now and then.

Comment on lines +11 to +13
PIL uses its own font file format to store bitmap fonts, limited to 256 characters. You
can use :py:meth:`~PIL.FontFile.FontFile.to_imagefont` to convert BDF and PCF font
descriptors (X window font formats) to this format::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PIL uses its own font file format to store bitmap fonts, limited to 256 characters. You
can use :py:meth:`~PIL.FontFile.FontFile.to_imagefont` to convert BDF and PCF font
descriptors (X window font formats) to this format::
Pillow uses its own font file format to store bitmap fonts, limited to 256 characters. You
can use :py:meth:`~PIL.FontFile.FontFile.to_imagefont` to convert BDF and PCF font
descriptors (X Window font formats) to this format::

Comment on lines +115 to +116
for id in range(256):
m = self.metrics[id]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid shadowing builtin:

Suggested change
for id in range(256):
m = self.metrics[id]
for i in range(256):
m = self.metrics[i]

assert_image_equal_tofile(im, "Tests/images/test_draw_pbm_target.png")


def test_to_imagefont(tmp_path: Path) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused:

Suggested change
def test_to_imagefont(tmp_path: Path) -> None:
def test_to_imagefont() -> None:

self.metrics[i] = d, dst, s

def _encode_metrics(self) -> bytes:
values: tuple[int, ...] = ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use a list...

Suggested change
values: tuple[int, ...] = ()
values: list[int] = []

Comment on lines +118 to +120
values += m[0] + m[1] + m[2]
else:
values += (0,) * 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... we can avoid repeatedly creating new tuples:

Suggested change
values += m[0] + m[1] + m[2]
else:
values += (0,) * 10
values.extend(m[0] + m[1] + m[2])
else:
values.extend((0,) * 10)

Comment on lines +122 to +127
metrics = b""
for v in values:
if v < 0:
v += 65536
metrics += _binary.o16be(v)
return metrics
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, mutable bytearray instead of immutable bytes:

Suggested change
metrics = b""
for v in values:
if v < 0:
v += 65536
metrics += _binary.o16be(v)
return metrics
data = bytearray()
for v in values:
if v < 0:
v += 65536
data += _binary.o16be(v)
return bytes(data)

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.

3 participants