-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathBdfFontFile.py
More file actions
Ignoring revisions in .git-blame-ignore-revs.
124 lines (104 loc) · 3.26 KB
/
Copy pathBdfFontFile.py
File metadata and controls
124 lines (104 loc) · 3.26 KB
Edit and raw actions
OlderNewer
1
#
2
# The Python Imaging Library
3
# $Id$
4
#
5
# bitmap distribution font (bdf) file parser
6
#
7
# history:
8
# 1996-05-16 fl created (as bdf2pil)
9
# 1997-08-25 fl converted to FontFile driver
10
# 2001-05-25 fl removed bogus __init__ call
11
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
12
# 2003-04-22 fl more robustification (from Graham Dumpleton)
13
#
14
# Copyright (c) 1997-2003 by Secret Labs AB.
15
# Copyright (c) 1997-2003 by Fredrik Lundh.
16
#
17
# See the README file for information on usage and redistribution.
18
#
19
20
"""
21
Parse X Bitmap Distribution Format (BDF)
22
"""
23
24
from __future__ import annotations
25
26
from typing import BinaryIO
27
28
from . import FontFile, Image
29
30
31
def bdf_char(
32
f: BinaryIO,
33
) -> (
34
tuple[
35
str,
36
int,
37
tuple[tuple[int, int], tuple[int, int, int, int], tuple[int, int, int, int]],
38
Image.Image,
39
]
40
| None
41
):
42
# skip to STARTCHAR
43
while True:
44
s = f.readline()
45
if not s:
46
return None
47
if s.startswith(b"STARTCHAR"):
48
break
49
id = s[9:].strip().decode("ascii")
50
51
# load symbol properties
52
props = {}
53
while True:
54
s = f.readline()
55
if not s or s.startswith(b"BITMAP"):
56
break
57
i = s.find(b" ")
58
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
59
60
# load bitmap
61
bitmap = bytearray()
62
while True:
63
s = f.readline()
64
if not s or s.startswith(b"ENDCHAR"):
65
break
66
bitmap += s[:-1]
67
68
# The word BBX
69
# followed by the width in x (BBw), height in y (BBh),
70
# and x and y displacement (BBxoff0, BByoff0)
71
# of the lower left corner from the origin of the character.
72
width, height, x_disp, y_disp = (int(p) for p in props["BBX"].split())
73
Image._decompression_bomb_check((width, height))
74
75
# The word DWIDTH
76
# followed by the width in x and y of the character in device pixels.
77
dwx, dwy = (int(p) for p in props["DWIDTH"].split())
78
79
bbox = (
80
(dwx, dwy),
81
(x_disp, -y_disp - height, width + x_disp, -y_disp),
82
(0, 0, width, height),
83
)
84
85
try:
86
im = Image.frombytes("1", (width, height), bitmap, "hex", "1")
87
except ValueError:
88
# deal with zero-width characters
89
im = Image.new("1", (width, height))
90
91
return id, int(props["ENCODING"]), bbox, im
92
93
94
class BdfFontFile(FontFile.FontFile):
95
"""Font file plugin for the X11 BDF format."""
96
97
def __init__(self, fp: BinaryIO) -> None:
98
super().__init__()
99
100
s = fp.readline()
101
if not s.startswith(b"STARTFONT 2.1"):
102
msg = "not a valid BDF file"
103
raise SyntaxError(msg)
104
105
props = {}
106
comments = []
107
108
while True:
109
s = fp.readline()
110
if not s or s.startswith(b"ENDPROPERTIES"):
111
break
112
i = s.find(b" ")
113
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
114
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
115
if s.find(b"LogicalFontDescription") < 0:
116
comments.append(s[i + 1 : -1].decode("ascii"))
117
118
while True:
119
c = bdf_char(fp)
120
if not c:
121
break
122
id, ch, (xy, dst, src), im = c
123
if 0 <= ch < len(self.glyph):
124
self.glyph[ch] = xy, dst, src, im