Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2fc9611

Browse files
committed
Improve the Type-1 font parsing
Move Type1Font._tokens into a top-level function _tokenize that is acoroutine. The parsing stage consuming the tokens can instruct thetokenizer to return a binary token - this is necessary when decryptingthe CharStrings and Subrs arrays, since the preceding context determineswhich 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 thefunction, since passing the information about binary tokens would noteasily 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 havetoken-specific helper methods. The position data will be needed whenmodifying the file, as the font is transformed or subsetted.A new helper function _expression can be used to consume tokens thatform a balanced subexpression delimited by [] or {}. This helps fix abug in UniqueID removal: if the font includes PostScript code thatchecks if the UniqueID is set in the current dictionary, the previouscode broke that code instead of removing the UniqueID definition. Fontscan include UniqueID in the encrypted portion as well as the cleartextone, and removal is now done in both portions.Fix a bug related to font weight: the key is title-cased and notlower-cased, so font.prop['weight'] should not exist.
1 parentb9e71c5 commit2fc9611

File tree

5 files changed

+634
-177
lines changed

5 files changed

+634
-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: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,48 @@ def test_Type1Font():
1313
assertfont.parts[0]==rawdata[0x0006:0x10c5]
1414
assertfont.parts[1]==rawdata[0x10cb:0x897f]
1515
assertfont.parts[2]==rawdata[0x8985:0x8ba6]
16-
assertfont.parts[1:]==slanted.parts[1:]
17-
assertfont.parts[1:]==condensed.parts[1:]
1816
assertfont.decrypted.startswith(b'dup\n/Private 18 dict dup begin')
1917
assertfont.decrypted.endswith(b'mark currentfile closefile\n')
18+
assertslanted.decrypted.startswith(b'dup\n/Private 18 dict dup begin')
19+
assertslanted.decrypted.endswith(b'mark currentfile closefile\n')
20+
assertb'UniqueID 5000793'infont.parts[0]
21+
assertb'UniqueID 5000793'infont.decrypted
22+
assertfont._pos['UniqueID']== [(797,818), (4483,4504)]
23+
24+
len0=len(font.parts[0])
25+
forkeyinfont._pos.keys():
26+
forpos0,pos1infont._pos[key]:
27+
ifpos0<len0:
28+
data=font.parts[0][pos0:pos1]
29+
else:
30+
data=font.decrypted[pos0-len0:pos1-len0]
31+
assertdata.startswith(f'/{key}'.encode('ascii'))
32+
assert {'FontType','FontMatrix','PaintType','ItalicAngle','RD'
33+
}<set(font._pos.keys())
34+
35+
assertb'UniqueID 5000793'notinslanted.parts[0]
36+
assertb'UniqueID 5000793'notinslanted.decrypted
37+
assert'UniqueID'notinslanted._pos
38+
assertfont.prop['Weight']=='Medium'
39+
assertnotfont.prop['isFixedPitch']
40+
assertfont.prop['ItalicAngle']==0
41+
assertslanted.prop['ItalicAngle']==-45
42+
assertfont.prop['Encoding'][5]=='Pi'
43+
assertisinstance(font.prop['CharStrings']['Pi'],bytes)
2044

2145
differ=difflib.Differ()
2246
diff=list(differ.compare(
2347
font.parts[0].decode('latin-1').splitlines(),
2448
slanted.parts[0].decode('latin-1').splitlines()))
2549
forlinein (
2650
# Removes UniqueID
27-
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
28-
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
51+
'- /UniqueID 5000793 def',
2952
# Changes the font name
3053
'- /FontName /CMR10 def',
31-
'+ /FontName/CMR10_Slant_1000 def',
54+
'+ /FontName/CMR10_Slant_1000 def',
3255
# Alters FontMatrix
3356
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
34-
'+ /FontMatrix [0.001 0 0.001 0.001 0 0]readonly def',
57+
'+ /FontMatrix [0.001 0 0.001 0.001 0 0]readonly def',
3558
# Alters ItalicAngle
3659
'- /ItalicAngle 0 def',
3760
'+ /ItalicAngle -45.0 def'):
@@ -42,17 +65,27 @@ def test_Type1Font():
4265
condensed.parts[0].decode('latin-1').splitlines()))
4366
forlinein (
4467
# Removes UniqueID
45-
'- FontDirectory/CMR10 known{/CMR10 findfont dup/UniqueID known{dup',
46-
'+ FontDirectory/CMR10 known{/CMR10 findfont dup',
68+
'- /UniqueID 5000793 def',
4769
# Changes the font name
4870
'- /FontName /CMR10 def',
49-
'+ /FontName/CMR10_Extend_500 def',
71+
'+ /FontName/CMR10_Extend_500 def',
5072
# Alters FontMatrix
5173
'- /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def',
52-
'+ /FontMatrix [0.0005 0 0 0.001 0 0]readonly def'):
74+
'+ /FontMatrix [0.0005 0 0 0.001 0 0]readonly def'):
5375
assertlineindiff,'diff to condensed font must contain %s'%line
5476

5577

78+
deftest_Type1Font_2():
79+
filename=os.path.join(os.path.dirname(__file__),
80+
'Courier10PitchBT-Bold.pfb')
81+
font=t1f.Type1Font(filename)
82+
assertfont.prop['Weight']=='Bold'
83+
assertfont.prop['isFixedPitch']
84+
assertfont.prop['Encoding'][65]=='A'# the font uses StandardEncoding
85+
(pos0,pos1),=font._pos['Encoding']
86+
assertfont.parts[0][pos0:pos1]==b'/Encoding StandardEncoding'
87+
88+
5689
deftest_overprecision():
5790
# We used to output too many digits in FontMatrix entries and
5891
# ItalicAngle, which could make Type-1 parsers unhappy.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp