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.
|
|
|
|
#
|
|
|
|
|
2012-10-16 06:49:13 +04:00
|
|
|
from . import Image
|
|
|
|
from . import FontFile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# parse X Bitmap Distribution Format (BDF)
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
bdf_slant = {
|
|
|
|
"R": "Roman",
|
|
|
|
"I": "Italic",
|
|
|
|
"O": "Oblique",
|
|
|
|
"RI": "Reverse Italic",
|
|
|
|
"RO": "Reverse Oblique",
|
|
|
|
"OT": "Other"
|
|
|
|
}
|
|
|
|
|
|
|
|
bdf_spacing = {
|
|
|
|
"P": "Proportional",
|
|
|
|
"M": "Monospaced",
|
|
|
|
"C": "Cell"
|
|
|
|
}
|
|
|
|
|
|
|
|
def bdf_char(f):
|
|
|
|
|
|
|
|
# skip to STARTCHAR
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
s = f.readline()
|
|
|
|
if not s:
|
|
|
|
return None
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if s[:9] == b"STARTCHAR":
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
id = s[9:].strip().decode('ascii')
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# load symbol properties
|
|
|
|
props = {}
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
s = f.readline()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if not s or s[:6] == b"BITMAP":
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
i = s.find(b" ")
|
|
|
|
props[s[:i].decode('ascii')] = s[i+1:-1].decode('ascii')
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# load bitmap
|
|
|
|
bitmap = []
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
s = f.readline()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if not s or s[:7] == b"ENDCHAR":
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
|
|
|
bitmap.append(s[:-1])
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
bitmap = b"".join(bitmap)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2012-10-16 05:58:46 +04:00
|
|
|
[x, y, l, d] = [int(s) for s in props["BBX"].split()]
|
|
|
|
[dx, dy] = [int(s) for s in props["DWIDTH"].split()]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y)
|
|
|
|
|
|
|
|
try:
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
##
|
|
|
|
# Font file plugin for the X11 BDF format.
|
|
|
|
|
|
|
|
class BdfFontFile(FontFile.FontFile):
|
|
|
|
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
|
|
|
FontFile.FontFile.__init__(self)
|
|
|
|
|
|
|
|
s = fp.readline()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if s[:13] != b"STARTFONT 2.1":
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a valid BDF file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
props = {}
|
|
|
|
comments = []
|
|
|
|
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
s = fp.readline()
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if not s or s[:13] == b"ENDPROPERTIES":
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
i = s.find(b" ")
|
|
|
|
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:
|
|
|
|
comments.append(s[i+1:-1].decode('ascii'))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2012-10-11 02:11:13 +04:00
|
|
|
font = props["FONT"].split("-")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2012-10-11 02:11:13 +04:00
|
|
|
font[4] = bdf_slant[font[4].upper()]
|
|
|
|
font[11] = bdf_spacing[font[11].upper()]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
ascent = int(props["FONT_ASCENT"])
|
|
|
|
descent = int(props["FONT_DESCENT"])
|
|
|
|
|
2012-10-11 02:11:13 +04:00
|
|
|
fontname = ";".join(font[1:])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# print "#", fontname
|
|
|
|
# for i in comments:
|
|
|
|
# print "#", i
|
|
|
|
|
|
|
|
font = []
|
2012-10-17 07:39:56 +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 ch >= 0 and ch < len(self.glyph):
|
|
|
|
self.glyph[ch] = xy, dst, src, im
|