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

Commitb8c0521

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 commitb8c0521

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
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+
assertfont.parts[0][pos0:pos1].startswith(f'/{key}'.encode('ascii'))
29+
else:
30+
assertfont.decrypted[pos0-len0:pos1-len0].startswith(f'/{key}'.encode('ascii'))
31+
assert {'FontType','FontMatrix','PaintType','ItalicAngle','RD'}<set(font._pos.keys())
32+
33+
assertb'UniqueID 5000793'notinslanted.parts[0]
34+
assertb'UniqueID 5000793'notinslanted.decrypted
35+
assert'UniqueID'notinslanted._pos
36+
assertfont.prop['Weight']=='Medium'
37+
assertnotfont.prop['isFixedPitch']
38+
assertfont.prop['ItalicAngle']==0
39+
assertslanted.prop['ItalicAngle']==-45
40+
assertfont.prop['Encoding'][5]=='Pi'
41+
assertisinstance(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
forlinein (
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
forlinein (
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
assertlineindiff,'diff to condensed font must contain %s'%line
5474

5575

76+
deftest_Type1Font_2():
77+
filename=os.path.join(os.path.dirname(__file__),'Courier10PitchBT-Bold.pfb')
78+
font=t1f.Type1Font(filename)
79+
assertfont.prop['Weight']=='Bold'
80+
assertfont.prop['isFixedPitch']
81+
assertfont.prop['Encoding'][65]=='A'# the font specifies StandardEncoding
82+
(pos0,pos1),=font._pos['Encoding']
83+
assertfont.parts[0][pos0:pos1]==b'/Encoding StandardEncoding'
84+
85+
5686
deftest_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)

[8]ページ先頭

©2009-2025 Movatter.jp