2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# THIS IS WORK IN PROGRESS
|
|
|
|
#
|
|
|
|
# The Python Imaging Library
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# portable compiled font file parser
|
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 1997-08-19 fl created
|
|
|
|
# 2003-09-13 fl fixed loading of unicode fonts
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2019-01-13 05:05:46 +03:00
|
|
|
import io
|
2019-07-06 23:40:53 +03:00
|
|
|
|
|
|
|
from . import FontFile, Image
|
|
|
|
from ._binary import i8, i16be as b16, i16le as l16, i32be as b32, i32le as l32
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# declarations
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
PCF_MAGIC = 0x70636601 # "\x01fcp"
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
PCF_PROPERTIES = 1 << 0
|
|
|
|
PCF_ACCELERATORS = 1 << 1
|
|
|
|
PCF_METRICS = 1 << 2
|
|
|
|
PCF_BITMAPS = 1 << 3
|
|
|
|
PCF_INK_METRICS = 1 << 4
|
|
|
|
PCF_BDF_ENCODINGS = 1 << 5
|
|
|
|
PCF_SWIDTHS = 1 << 6
|
|
|
|
PCF_GLYPH_NAMES = 1 << 7
|
|
|
|
PCF_BDF_ACCELERATORS = 1 << 8
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
BYTES_PER_ROW = [
|
2019-03-21 16:28:20 +03:00
|
|
|
lambda bits: ((bits + 7) >> 3),
|
|
|
|
lambda bits: ((bits + 15) >> 3) & ~1,
|
|
|
|
lambda bits: ((bits + 31) >> 3) & ~3,
|
|
|
|
lambda bits: ((bits + 63) >> 3) & ~7,
|
2010-07-31 06:52:47 +04:00
|
|
|
]
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def sz(s, o):
|
2019-03-21 16:28:20 +03:00
|
|
|
return s[o : s.index(b"\0", o)]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Font file plugin for the X11 PCF format.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class PcfFontFile(FontFile.FontFile):
|
|
|
|
|
|
|
|
name = "name"
|
|
|
|
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
|
|
|
magic = l32(fp.read(4))
|
|
|
|
if magic != PCF_MAGIC:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a PCF file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2016-11-05 20:31:11 +03:00
|
|
|
super().__init__()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
count = l32(fp.read(4))
|
|
|
|
self.toc = {}
|
|
|
|
for i in range(count):
|
|
|
|
type = l32(fp.read(4))
|
|
|
|
self.toc[type] = l32(fp.read(4)), l32(fp.read(4)), l32(fp.read(4))
|
|
|
|
|
|
|
|
self.fp = fp
|
|
|
|
|
|
|
|
self.info = self._load_properties()
|
|
|
|
|
|
|
|
metrics = self._load_metrics()
|
|
|
|
bitmaps = self._load_bitmaps(metrics)
|
|
|
|
encoding = self._load_encoding()
|
|
|
|
|
|
|
|
#
|
|
|
|
# create glyph structure
|
|
|
|
|
|
|
|
for ch in range(256):
|
|
|
|
ix = encoding[ch]
|
|
|
|
if ix is not None:
|
|
|
|
x, y, l, r, w, a, d, f = metrics[ix]
|
2019-03-21 16:28:20 +03:00
|
|
|
glyph = (w, 0), (l, d - y, x + l, d), (0, 0, x, y), bitmaps[ix]
|
2010-07-31 06:52:47 +04:00
|
|
|
self.glyph[ch] = glyph
|
|
|
|
|
|
|
|
def _getformat(self, tag):
|
|
|
|
|
|
|
|
format, size, offset = self.toc[tag]
|
|
|
|
|
|
|
|
fp = self.fp
|
|
|
|
fp.seek(offset)
|
|
|
|
|
|
|
|
format = l32(fp.read(4))
|
|
|
|
|
|
|
|
if format & 4:
|
|
|
|
i16, i32 = b16, b32
|
|
|
|
else:
|
|
|
|
i16, i32 = l16, l32
|
|
|
|
|
|
|
|
return fp, format, i16, i32
|
|
|
|
|
|
|
|
def _load_properties(self):
|
|
|
|
|
|
|
|
#
|
|
|
|
# font properties
|
|
|
|
|
|
|
|
properties = {}
|
|
|
|
|
|
|
|
fp, format, i16, i32 = self._getformat(PCF_PROPERTIES)
|
|
|
|
|
|
|
|
nprops = i32(fp.read(4))
|
|
|
|
|
|
|
|
# read property description
|
|
|
|
p = []
|
|
|
|
for i in range(nprops):
|
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
|
|
|
p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
|
2010-07-31 06:52:47 +04:00
|
|
|
if nprops & 3:
|
2019-01-13 05:05:46 +03:00
|
|
|
fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return properties
|
|
|
|
|
|
|
|
def _load_metrics(self):
|
|
|
|
|
|
|
|
#
|
|
|
|
# font metrics
|
|
|
|
|
|
|
|
metrics = []
|
|
|
|
|
|
|
|
fp, format, i16, i32 = self._getformat(PCF_METRICS)
|
|
|
|
|
|
|
|
append = metrics.append
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
if (format & 0xFF00) == 0x100:
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# "compressed" metrics
|
|
|
|
for i in range(i16(fp.read(2))):
|
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
|
|
|
left = i8(fp.read(1)) - 128
|
|
|
|
right = i8(fp.read(1)) - 128
|
|
|
|
width = i8(fp.read(1)) - 128
|
|
|
|
ascent = i8(fp.read(1)) - 128
|
|
|
|
descent = i8(fp.read(1)) - 128
|
2010-07-31 06:52:47 +04:00
|
|
|
xsize = right - left
|
|
|
|
ysize = ascent + descent
|
2019-03-21 16:28:20 +03:00
|
|
|
append((xsize, ysize, left, right, width, ascent, descent, 0))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# "jumbo" metrics
|
|
|
|
for i in range(i32(fp.read(4))):
|
|
|
|
left = i16(fp.read(2))
|
|
|
|
right = i16(fp.read(2))
|
|
|
|
width = i16(fp.read(2))
|
|
|
|
ascent = i16(fp.read(2))
|
|
|
|
descent = i16(fp.read(2))
|
|
|
|
attributes = i16(fp.read(2))
|
|
|
|
xsize = right - left
|
|
|
|
ysize = ascent + descent
|
2019-03-21 16:28:20 +03:00
|
|
|
append((xsize, ysize, left, right, width, ascent, descent, attributes))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return metrics
|
|
|
|
|
|
|
|
def _load_bitmaps(self, metrics):
|
|
|
|
|
|
|
|
#
|
|
|
|
# bitmap data
|
|
|
|
|
|
|
|
bitmaps = []
|
|
|
|
|
|
|
|
fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
|
|
|
|
|
|
|
|
nbitmaps = i32(fp.read(4))
|
|
|
|
|
|
|
|
if nbitmaps != len(metrics):
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("Wrong number of bitmaps")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
offsets = []
|
|
|
|
for i in range(nbitmaps):
|
|
|
|
offsets.append(i32(fp.read(4)))
|
|
|
|
|
|
|
|
bitmapSizes = []
|
|
|
|
for i in range(4):
|
|
|
|
bitmapSizes.append(i32(fp.read(4)))
|
|
|
|
|
2015-04-08 14:12:37 +03:00
|
|
|
# byteorder = format & 4 # non-zero => MSB
|
2019-03-21 16:28:20 +03:00
|
|
|
bitorder = format & 8 # non-zero => MSB
|
2014-08-26 17:47:10 +04:00
|
|
|
padindex = format & 3
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
bitmapsize = bitmapSizes[padindex]
|
|
|
|
offsets.append(bitmapsize)
|
|
|
|
|
|
|
|
data = fp.read(bitmapsize)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
pad = BYTES_PER_ROW[padindex]
|
2010-07-31 06:52:47 +04:00
|
|
|
mode = "1;R"
|
|
|
|
if bitorder:
|
|
|
|
mode = "1"
|
|
|
|
|
|
|
|
for i in range(nbitmaps):
|
|
|
|
x, y, l, r, w, a, d, f = metrics[i]
|
2019-03-21 16:28:20 +03:00
|
|
|
b, e = offsets[i], offsets[i + 1]
|
|
|
|
bitmaps.append(Image.frombytes("1", (x, y), data[b:e], "raw", mode, pad(x)))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return bitmaps
|
|
|
|
|
|
|
|
def _load_encoding(self):
|
|
|
|
|
|
|
|
# map character code to bitmap index
|
|
|
|
encoding = [None] * 256
|
|
|
|
|
|
|
|
fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
|
|
|
|
|
|
|
|
firstCol, lastCol = i16(fp.read(2)), i16(fp.read(2))
|
|
|
|
firstRow, lastRow = i16(fp.read(2)), i16(fp.read(2))
|
|
|
|
|
2018-10-18 21:49:14 +03:00
|
|
|
i16(fp.read(2)) # default
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
nencoding = (lastCol - firstCol + 1) * (lastRow - firstRow + 1)
|
|
|
|
|
|
|
|
for i in range(nencoding):
|
|
|
|
encodingOffset = i16(fp.read(2))
|
|
|
|
if encodingOffset != 0xFFFF:
|
|
|
|
try:
|
2019-03-21 16:28:20 +03:00
|
|
|
encoding[i + firstCol] = encodingOffset
|
2010-07-31 06:52:47 +04:00
|
|
|
except IndexError:
|
2014-08-26 17:47:10 +04:00
|
|
|
break # only load ISO-8859-1 glyphs
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return encoding
|