Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8c0521

Browse files
committed
Improve the Type-1 font parsing
Move Type1Font._tokens into a top-level function _tokenize that is a coroutine. The parsing stage consuming the tokens can instruct the tokenizer to return a binary token - this is necessary when decrypting the CharStrings and Subrs arrays, since the preceding context determines which parts of the data need to be decrypted. The function now also parses the encrypted portion of the font file. To support usage as a coroutine, move the whitespace filtering into the function, since passing the information about binary tokens would not easily work through a filter. The function now returns tokens as subclasses of a new _Token class, which carry the position and value of the token and can have token-specific helper methods. The position data will be needed when modifying the file, as the font is transformed or subsetted. A new helper function _expression can be used to consume tokens that form a balanced subexpression delimited by [] or {}. This helps fix a bug in UniqueID removal: if the font includes PostScript code that checks if the UniqueID is set in the current dictionary, the previous code broke that code instead of removing the UniqueID definition. Fonts can include UniqueID in the encrypted portion as well as the cleartext one, and removal is now done in both portions. Fix a bug related to font weight: the key is title-cased and not lower-cased, so font.prop['weight'] should not exist.
1 parent b9e71c5 commit b8c0521

File tree

5 files changed

+631
-177
lines changed

5 files changed

+631
-177
lines changed

LICENSE/LICENSE_COURIERTEN

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The Courier10PitchBT-Bold.pfb file is a Type-1 version of
2+
Courier 10 Pitch BT Bold by Bitstream, obtained from
3+
<https://ctan.org/tex-archive/fonts/courierten>. It is included
4+
here as test data only, but the following license applies.
5+
6+
7+
(c) Copyright 1989-1992, Bitstream Inc., Cambridge, MA.
8+
9+
You are hereby granted permission under all Bitstream propriety rights
10+
to use, copy, modify, sublicense, sell, and redistribute the 4 Bitstream
11+
Charter (r) Type 1 outline fonts and the 4 Courier Type 1 outline fonts
12+
for any purpose and without restriction; provided, that this notice is
13+
left intact on all copies of such fonts and that Bitstream's trademark
14+
is acknowledged as shown below on all unmodified copies of the 4 Charter
15+
Type 1 fonts.
16+
17+
BITSTREAM CHARTER is a registered trademark of Bitstream Inc.
18+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
``Type1Font`` objects include more properties
2+
---------------------------------------------
3+
4+
The `.type1font.Type1Font.prop` dictionary now includes more keys, such
5+
as `CharStrings` and `Subrs`. The value of the `Encoding` key is now a
6+
dictionary mapping codes to glyph names. The
7+
`.type1font.Type1Font.transform` method now correctly removes `UniqueID`
8+
properties from the font.
37.2 KB
Binary file not shown.

lib/matplotlib/tests/test_type1font.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,46 @@ def test_Type1Font():
1313
assert font.parts[0] == rawdata[0x0006:0x10c5]
1414
assert font.parts[1] == rawdata[0x10cb:0x897f]
1515
assert font.parts[2] == rawdata[0x8985:0x8ba6]
16-
assert font.parts[1:] == slanted.parts[1:]
17-
assert font.parts[1:] == condensed.parts[1:]
1816
assert font.decrypted.startswith(b'dup\n/Private 18 dict dup begin')
1917
assert font.decrypted.endswith(b'mark currentfile closefile\n')
18+
assert slanted.decrypted.startswith(b'dup\n/Private 18 dict dup begin')
19+
assert slanted.decrypted.endswith(b'mark currentfile closefile\n')
20+
assert b'UniqueID 5000793' in font.parts[0]
21+
assert b'UniqueID 5000793' in font.decrypted
22+
assert font._pos['UniqueID'] == [(797, 818), (4483, 4504)]
23+
24+
len0 = len(font.parts[0])
25+
for key in font._pos.keys():
26+
for pos0, pos1 in font._pos[key]:
27+
if pos0 < len0:
28+
assert font.parts[0][pos0:pos1].startswith(f'/{key}'.encode('ascii'))
29+
else:
30+
assert font.decrypted[pos0-len0:pos1-len0].startswith(f'/{key}'.encode('ascii'))
31+
assert {'FontType', 'FontMatrix', 'PaintType', 'ItalicAngle', 'RD'} < set(font._pos.keys())
32+
33+
assert b'UniqueID 5000793' not in slanted.parts[0]
34+
assert b'UniqueID 5000793' not in slanted.decrypted
35+
assert 'UniqueID' not in slanted._pos
36+
assert font.prop['Weight'] == 'Medium'
37+
assert not font.prop['isFixedPitch']
38+
assert font.prop['ItalicAngle'] == 0
39+
assert slanted.prop['ItalicAngle'] == -45
40+
assert font.prop['Encoding'][5] == 'Pi'
41+
assert isinstance(font.prop['CharStrings']['Pi'], bytes)
2042

2143
differ = difflib.Differ()
2244
diff = list(differ.compare(
2345
font.parts[0].decode('latin-1').splitlines(),
2446
slanted.parts[0].decode('latin-1').splitlines()))
2547
for line in (
2648
# Removes UniqueID
27-
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
28-
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
49+
'- /UniqueID 5000793 def',
2950
# Changes the font name
3051
'- /FontName /CMR10 def',
31-
'+ /FontName /CMR10_Slant_1000 def',
52+
'+ /FontName/CMR10_Slant_1000 def',
3253
# Alters FontMatrix
3354
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
34-
'+ /FontMatrix [0.001 0 0.001 0.001 0 0]readonly def',
55+
'+ /FontMatrix [0.001 0 0.001 0.001 0 0] readonly def',
3556
# Alters ItalicAngle
3657
'- /ItalicAngle 0 def',
3758
'+ /ItalicAngle -45.0 def'):
@@ -42,17 +63,26 @@ def test_Type1Font():
4263
condensed.parts[0].decode('latin-1').splitlines()))
4364
for line in (
4465
# Removes UniqueID
45-
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
46-
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
66+
'- /UniqueID 5000793 def',
4767
# Changes the font name
4868
'- /FontName /CMR10 def',
49-
'+ /FontName /CMR10_Extend_500 def',
69+
'+ /FontName/CMR10_Extend_500 def',
5070
# Alters FontMatrix
5171
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
52-
'+ /FontMatrix [0.0005 0 0 0.001 0 0]readonly def'):
72+
'+ /FontMatrix [0.0005 0 0 0.001 0 0] readonly def'):
5373
assert line in diff, 'diff to condensed font must contain %s' % line
5474

5575

76+
def test_Type1Font_2():
77+
filename = os.path.join(os.path.dirname(__file__), 'Courier10PitchBT-Bold.pfb')
78+
font = t1f.Type1Font(filename)
79+
assert font.prop['Weight'] == 'Bold'
80+
assert font.prop['isFixedPitch']
81+
assert font.prop['Encoding'][65] == 'A' # the font specifies StandardEncoding
82+
(pos0, pos1), = font._pos['Encoding']
83+
assert font.parts[0][pos0:pos1] == b'/Encoding StandardEncoding'
84+
85+
5686
def test_overprecision():
5787
# We used to output too many digits in FontMatrix entries and
5888
# ItalicAngle, which could make Type-1 parsers unhappy.

0 commit comments

Comments
 (0)