Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tests/test_font_pcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def test_draw(request: pytest.FixtureRequest, tmp_path: Path) -> None:
assert_image_equal_tofile(im, "Tests/images/test_draw_pbm_target.png")


def test_to_imagefont(tmp_path: Path) -> None:
Comment thread
radarhere marked this conversation as resolved.
Outdated
with open(fontname, "rb") as test_file:
pcffont = PcfFontFile.PcfFontFile(test_file)
imagefont = pcffont.to_imagefont()
im = Image.new("L", (130, 30), "white")
draw = ImageDraw.Draw(im)
draw.text((0, 0), message, "black", font=imagefont)
assert_image_equal_tofile(im, "Tests/images/test_draw_pbm_target.png")


def test_textsize(request: pytest.FixtureRequest, tmp_path: Path) -> None:
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
Expand Down
18 changes: 17 additions & 1 deletion Tests/test_fontfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from __future__ import annotations

from io import BytesIO
from pathlib import Path

import pytest

from PIL import FontFile, Image


def test_puti16() -> None:
fp = BytesIO()
FontFile.puti16(fp, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
assert fp.getvalue() == (
b"\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04"
b"\x00\x05\x00\x06\x00\x07\x00\x08\x00\t"
)


def test_compile() -> None:
font = FontFile.FontFile()
font.glyph[0] = ((0, 0), (0, 0, 0, 0), (0, 0, 0, 1), Image.new("L", (0, 0)))
Expand All @@ -24,5 +34,11 @@ def test_save(tmp_path: Path) -> None:
tempname = str(tmp_path / "temp.pil")

font = FontFile.FontFile()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="No bitmap created"):
font.save(tempname)


def test_to_imagefont() -> None:
font = FontFile.FontFile()
with pytest.raises(ValueError, match="No bitmap created"):
font.to_imagefont()
39 changes: 32 additions & 7 deletions src/PIL/FontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import os
from typing import BinaryIO

from . import Image, _binary
from . import Image, ImageFont, _binary

WIDTH = 800

Expand Down Expand Up @@ -110,6 +110,22 @@ def compile(self) -> None:
self.bitmap.paste(im.crop(src), s)
self.metrics[i] = d, dst, s

def _encode_metrics(self) -> bytes:
values: tuple[int, ...] = ()
Comment thread
fjhenigman marked this conversation as resolved.
Outdated
for id in range(256):
m = self.metrics[id]
Comment thread
radarhere marked this conversation as resolved.
Outdated
if m:
values += m[0] + m[1] + m[2]
else:
values += (0,) * 10
Comment thread
fjhenigman marked this conversation as resolved.
Outdated

metrics = b""
for v in values:
if v < 0:
v += 65536
metrics += _binary.o16be(v)
return metrics
Comment thread
fjhenigman marked this conversation as resolved.
Outdated

def save(self, filename: str) -> None:
"""Save font"""

Expand All @@ -126,9 +142,18 @@ def save(self, filename: str) -> None:
fp.write(b"PILfont\n")
fp.write(f";;;;;;{self.ysize};\n".encode("ascii")) # HACK!!!
fp.write(b"DATA\n")
for id in range(256):
m = self.metrics[id]
if not m:
puti16(fp, (0,) * 10)
else:
puti16(fp, m[0] + m[1] + m[2])
fp.write(self._encode_metrics())

def to_imagefont(self) -> ImageFont.ImageFont:
"""Convert to ImageFont"""

self.compile()

# font data
if not self.bitmap:
msg = "No bitmap created"
raise ValueError(msg)

imagefont = ImageFont.ImageFont()
imagefont._load(self.bitmap, self._encode_metrics())
return imagefont
3 changes: 3 additions & 0 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None:
# read PILfont metrics
data = file.read(256 * 20)

self._load(image, data)

def _load(self, image: Image.Image, data: bytes) -> None:
image.load()

self.font = Image.core.font(image.im, data)
Expand Down
Loading