Pillow/src/PIL/BdfFontFile.py

113 lines
2.9 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library
# $Id$
#
# bitmap distribution font (bdf) file parser
#
# history:
# 1996-05-16 fl created (as bdf2pil)
# 1997-08-25 fl converted to FontFile driver
# 2001-05-25 fl removed bogus __init__ call
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
# 2003-04-22 fl more robustification (from Graham Dumpleton)
#
# Copyright (c) 1997-2003 by Secret Labs AB.
# Copyright (c) 1997-2003 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#
2016-11-26 03:05:56 +03:00
from . import FontFile, Image
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
# parse X Bitmap Distribution Format (BDF)
# --------------------------------------------------------------------
bdf_slant = {
2014-08-26 17:47:10 +04:00
"R": "Roman",
"I": "Italic",
"O": "Oblique",
"RI": "Reverse Italic",
"RO": "Reverse Oblique",
2019-03-21 16:28:20 +03:00
"OT": "Other",
2010-07-31 06:52:47 +04:00
}
2019-03-21 16:28:20 +03:00
bdf_spacing = {"P": "Proportional", "M": "Monospaced", "C": "Cell"}
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
def bdf_char(f):
2010-07-31 06:52:47 +04:00
# skip to STARTCHAR
while True:
2010-07-31 06:52:47 +04:00
s = f.readline()
if not s:
return None
if s[:9] == b"STARTCHAR":
2010-07-31 06:52:47 +04:00
break
2019-03-21 16:28:20 +03:00
id = s[9:].strip().decode("ascii")
2010-07-31 06:52:47 +04:00
# load symbol properties
props = {}
while True:
2010-07-31 06:52:47 +04:00
s = f.readline()
if not s or s[:6] == b"BITMAP":
2010-07-31 06:52:47 +04:00
break
i = s.find(b" ")
2019-03-21 16:28:20 +03:00
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
2010-07-31 06:52:47 +04:00
# load bitmap
bitmap = []
while True:
2010-07-31 06:52:47 +04:00
s = f.readline()
if not s or s[:7] == b"ENDCHAR":
2010-07-31 06:52:47 +04:00
break
bitmap.append(s[:-1])
bitmap = b"".join(bitmap)
2010-07-31 06:52:47 +04:00
[x, y, l, d] = [int(p) for p in props["BBX"].split()]
[dx, dy] = [int(p) for p in props["DWIDTH"].split()]
2010-07-31 06:52:47 +04:00
2019-03-21 16:28:20 +03:00
bbox = (dx, dy), (l, -d - y, x + l, -d), (0, 0, x, y)
2010-07-31 06:52:47 +04:00
try:
im = Image.frombytes("1", (x, y), bitmap, "hex", "1")
2010-07-31 06:52:47 +04:00
except ValueError:
# deal with zero-width characters
im = Image.new("1", (x, y))
return id, int(props["ENCODING"]), bbox, im
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
##
# Font file plugin for the X11 BDF format.
2019-03-21 16:28:20 +03:00
class BdfFontFile(FontFile.FontFile):
2010-07-31 06:52:47 +04:00
def __init__(self, fp):
super().__init__()
2010-07-31 06:52:47 +04:00
s = fp.readline()
if s[:13] != b"STARTFONT 2.1":
raise SyntaxError("not a valid BDF file")
2010-07-31 06:52:47 +04:00
props = {}
comments = []
while True:
2010-07-31 06:52:47 +04:00
s = fp.readline()
if not s or s[:13] == b"ENDPROPERTIES":
2010-07-31 06:52:47 +04:00
break
i = s.find(b" ")
2019-03-21 16:28:20 +03:00
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
if s.find(b"LogicalFontDescription") < 0:
2019-03-21 16:28:20 +03:00
comments.append(s[i + 1 : -1].decode("ascii"))
2010-07-31 06:52:47 +04:00
while True:
2010-07-31 06:52:47 +04:00
c = bdf_char(fp)
if not c:
break
id, ch, (xy, dst, src), im = c
if 0 <= ch < len(self.glyph):
2010-07-31 06:52:47 +04:00
self.glyph[ch] = xy, dst, src, im