Skip to content

Commit 06270c9

Browse files
committed
dviread: Skip font map lines with invalid special.
As noted in the pfdtex manual, `SlantFont` and `ExtendFont` are only allowed for T1 fonts, and within range ±1 and ±2, respectively. This can be confirmed the same way as the previous commit, by copying the lines from the `test.map` (though using a _real_ tfmname).
1 parent ab74d64 commit 06270c9

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

lib/matplotlib/dviread.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,6 @@ def _parse_and_cache_line(self, line):
908908
basename = unquoted
909909
elif quoted:
910910
special = quoted
911-
if basename is None:
912-
basename = tfmname
913911
effects = {}
914912
if special:
915913
words = reversed(special.split())
@@ -918,6 +916,24 @@ def _parse_and_cache_line(self, line):
918916
effects["slant"] = float(next(words))
919917
elif word == b"ExtendFont":
920918
effects["extend"] = float(next(words))
919+
920+
# Verify some properties of the line that would cause it to be ignored
921+
# otherwise.
922+
is_t1 = False
923+
if fontfile is not None:
924+
if not fontfile.endswith((b".ttf", b".ttc", b".otf"):
925+
is_t1 = True
926+
elif basename is not None:
927+
is_t1 = True
928+
if not is_t1 and ("slant" in effects or "extend" in effects):
929+
return
930+
if abs(effects.get("slant", 0)) > 1:
931+
return
932+
if abs(effects.get("extend", 0)) > 2:
933+
return
934+
935+
if basename is None:
936+
basename = tfmname
921937
if encodingfile is not None and not encodingfile.startswith(b"/"):
922938
encodingfile = find_tex_file(encodingfile)
923939
if fontfile is not None and not fontfile.startswith(b"/"):

lib/matplotlib/tests/baseline_images/dviread/test.map

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
TeXfont1 PSfont1 <font1.pfb <font1.enc
33
TeXfont2 PSfont2 <font2.enc <font2.pfa
44
TeXfont3 PSfont3 "1.23 UnknownEffect" <[enc3.foo < font3.pfa
5-
TeXfont4 PSfont4 "-0.1 SlantFont 2.2 ExtendFont" <font4.enc <font4.pfa
5+
TeXfont4 PSfont4 "-0.1 SlantFont 1.2 ExtendFont" <font4.enc <font4.pfa
66
TeXfont5 PSfont5 <encoding1.enc <encoding2.enc <font5.pfb
77
TeXfont6 PSfont6
88
TeXfont7 PSfont7 < font7.enc
@@ -11,3 +11,16 @@ TeXfont9 </absolute/font9.pfb
1111
% Only the first of a duplicate key is used.
1212
TeXfontA PSfontA1
1313
TeXfontA PSfontA2
14+
%
15+
% Check SlantFont/ExtendFont
16+
%
17+
% Options are only allowed on T1 fonts (3 TrueType, 1 bitmap, and 1 T1 below).
18+
TeXfontB PSfontB1 "-0.1 SlantFont 1.2 ExtendFont" <fontB1.ttf
19+
TeXfontB PSfontB2 "-0.1 SlantFont" <fontB1.ttc
20+
TeXfontB PSfontB3 "1.2 ExtendFont" <fontB1.otf
21+
TeXfontB "0.1 SlantFont 1.2 ExtendFont"
22+
% Also, only within range ±1 / ±2.
23+
TeXfontB PSfontB4 "1.1 SlantFont 1.2 ExtendFont"
24+
TeXfontB PSfontB5 "0.1 SlantFont 2.2 ExtendFont"
25+
% This is the only one that works:
26+
TeXfontB PSfontB6

lib/matplotlib/tests/test_dviread.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_PsfontsMap(monkeypatch):
2828
else:
2929
assert entry.filename == b'font%d.pfb' % n
3030
if n == 4:
31-
assert entry.effects == {'slant': -0.1, 'extend': 2.2}
31+
assert entry.effects == {'slant': -0.1, 'extend': 1.2}
3232
else:
3333
assert entry.effects == {}
3434
# Some special cases
@@ -47,6 +47,9 @@ def test_PsfontsMap(monkeypatch):
4747
# First of duplicates only.
4848
entry = fontmap[b'TeXfontA']
4949
assert entry.psname == b'PSfontA1'
50+
# Slant/Extend only works for T1 fonts.
51+
entry = fontmap[b'TeXfontB']
52+
assert entry.psname == b'PSfontB6'
5053
# Missing font
5154
with pytest.raises(KeyError, match='no-such-font'):
5255
fontmap[b'no-such-font']

0 commit comments

Comments
 (0)