This commit is contained in:
Iliyan Angelov
2025-12-01 06:50:10 +02:00
parent 91f51bc6fe
commit 62c1fe5951
4682 changed files with 544807 additions and 31208 deletions

View File

@@ -15,6 +15,7 @@
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations
import io
@@ -25,6 +26,11 @@ from ._binary import i16le as l16
from ._binary import i32be as b32
from ._binary import i32le as l32
TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Callable
from typing import BinaryIO
# --------------------------------------------------------------------
# declarations
@@ -40,7 +46,7 @@ PCF_SWIDTHS = 1 << 6
PCF_GLYPH_NAMES = 1 << 7
PCF_BDF_ACCELERATORS = 1 << 8
BYTES_PER_ROW = [
BYTES_PER_ROW: list[Callable[[int], int]] = [
lambda bits: ((bits + 7) >> 3),
lambda bits: ((bits + 15) >> 3) & ~1,
lambda bits: ((bits + 31) >> 3) & ~3,
@@ -48,7 +54,7 @@ BYTES_PER_ROW = [
]
def sz(s, o):
def sz(s: bytes, o: int) -> bytes:
return s[o : s.index(b"\0", o)]
@@ -57,7 +63,7 @@ class PcfFontFile(FontFile.FontFile):
name = "name"
def __init__(self, fp, charset_encoding="iso8859-1"):
def __init__(self, fp: BinaryIO, charset_encoding: str = "iso8859-1"):
self.charset_encoding = charset_encoding
magic = l32(fp.read(4))
@@ -103,7 +109,9 @@ class PcfFontFile(FontFile.FontFile):
bitmaps[ix],
)
def _getformat(self, tag):
def _getformat(
self, tag: int
) -> tuple[BinaryIO, int, Callable[[bytes], int], Callable[[bytes], int]]:
format, size, offset = self.toc[tag]
fp = self.fp
@@ -118,7 +126,7 @@ class PcfFontFile(FontFile.FontFile):
return fp, format, i16, i32
def _load_properties(self):
def _load_properties(self) -> dict[bytes, bytes | int]:
#
# font properties
@@ -129,27 +137,24 @@ class PcfFontFile(FontFile.FontFile):
nprops = i32(fp.read(4))
# read property description
p = []
for i in range(nprops):
p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
p = [(i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))) for _ in range(nprops)]
if nprops & 3:
fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
data = fp.read(i32(fp.read(4)))
for k, s, v in p:
k = sz(data, k)
if s:
v = sz(data, v)
properties[k] = v
property_value: bytes | int = sz(data, v) if s else v
properties[sz(data, k)] = property_value
return properties
def _load_metrics(self):
def _load_metrics(self) -> list[tuple[int, int, int, int, int, int, int, int]]:
#
# font metrics
metrics = []
metrics: list[tuple[int, int, int, int, int, int, int, int]] = []
fp, format, i16, i32 = self._getformat(PCF_METRICS)
@@ -182,12 +187,12 @@ class PcfFontFile(FontFile.FontFile):
return metrics
def _load_bitmaps(self, metrics):
def _load_bitmaps(
self, metrics: list[tuple[int, int, int, int, int, int, int, int]]
) -> list[Image.Image]:
#
# bitmap data
bitmaps = []
fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
nbitmaps = i32(fp.read(4))
@@ -196,13 +201,9 @@ class PcfFontFile(FontFile.FontFile):
msg = "Wrong number of bitmaps"
raise OSError(msg)
offsets = []
for i in range(nbitmaps):
offsets.append(i32(fp.read(4)))
offsets = [i32(fp.read(4)) for _ in range(nbitmaps)]
bitmap_sizes = []
for i in range(4):
bitmap_sizes.append(i32(fp.read(4)))
bitmap_sizes = [i32(fp.read(4)) for _ in range(4)]
# byteorder = format & 4 # non-zero => MSB
bitorder = format & 8 # non-zero => MSB
@@ -218,6 +219,7 @@ class PcfFontFile(FontFile.FontFile):
if bitorder:
mode = "1"
bitmaps = []
for i in range(nbitmaps):
xsize, ysize = metrics[i][:2]
b, e = offsets[i : i + 2]
@@ -227,7 +229,7 @@ class PcfFontFile(FontFile.FontFile):
return bitmaps
def _load_encoding(self):
def _load_encoding(self) -> list[int | None]:
fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
first_col, last_col = i16(fp.read(2)), i16(fp.read(2))
@@ -238,7 +240,7 @@ class PcfFontFile(FontFile.FontFile):
nencoding = (last_col - first_col + 1) * (last_row - first_row + 1)
# map character code to bitmap index
encoding = [None] * min(256, nencoding)
encoding: list[int | None] = [None] * min(256, nencoding)
encoding_offsets = [i16(fp.read(2)) for _ in range(nencoding)]