22
33from __future__ import (absolute_import , division , print_function ,
44 unicode_literals )
5+ from six import BytesIO
56
67import matplotlib .afm as afm
78
89
10+ AFM_TEST_DATA = b"""StartFontMetrics 2.0
11+ Comment Comments are ignored.
12+ Comment Creation Date:Mon Nov 13 12:34:11 GMT 2017
13+ FontName MyFont-Bold
14+ EncodingScheme FontSpecific
15+ FullName My Font Bold
16+ FamilyName Test Fonts
17+ Weight Bold
18+ ItalicAngle 0.0
19+ IsFixedPitch false
20+ UnderlinePosition -100
21+ UnderlineThickness 50
22+ Version 001.000
23+ Notice Copyright (c) 2017 No one.
24+ FontBBox 0 -321 1234 369
25+ StartCharMetrics 3
26+ C 0 ; WX 250 ; N space ; B 0 0 0 0 ;
27+ C 42 ; WX 1141 ; N foo ; B 40 60 800 360 ;
28+ C 99 ; WX 583 ; N bar ; B 40 -10 543 210 ;
29+ EndCharMetrics
30+ EndFontMetrics
31+ """
32+
33+
934def test_nonascii_str ():
1035 # This tests that we also decode bytes as utf-8 properly.
1136 # Else, font files with non ascii characters fail to load.
@@ -14,3 +39,46 @@ def test_nonascii_str():
1439
1540 ret = afm ._to_str (byte_str )
1641 assert ret == inp_str
42+
43+
44+ def test_parse_header ():
45+ fh = BytesIO (AFM_TEST_DATA )
46+ header = afm ._parse_header (fh )
47+ assert header == {
48+ b'StartFontMetrics' : 2.0 ,
49+ b'FontName' : 'MyFont-Bold' ,
50+ b'EncodingScheme' : 'FontSpecific' ,
51+ b'FullName' : 'My Font Bold' ,
52+ b'FamilyName' : 'Test Fonts' ,
53+ b'Weight' : 'Bold' ,
54+ b'ItalicAngle' : 0.0 ,
55+ b'IsFixedPitch' : False ,
56+ b'UnderlinePosition' : - 100 ,
57+ b'UnderlineThickness' : 50 ,
58+ b'Version' : '001.000' ,
59+ b'Notice' : 'Copyright (c) 2017 No one.' ,
60+ b'FontBBox' : [0 , - 321 , 1234 , 369 ],
61+ b'StartCharMetrics' : 3 ,
62+ }
63+
64+
65+ def test_parse_char_metrics ():
66+ fh = BytesIO (AFM_TEST_DATA )
67+ afm ._parse_header (fh ) # position
68+ metrics = afm ._parse_char_metrics (fh )
69+ assert metrics == (
70+ {0 : (250.0 , 'space' , [0 , 0 , 0 , 0 ]),
71+ 42 : (1141.0 , 'foo' , [40 , 60 , 800 , 360 ]),
72+ 99 : (583.0 , 'bar' , [40 , - 10 , 543 , 210 ]),
73+ },
74+ {'space' : (250.0 , [0 , 0 , 0 , 0 ]),
75+ 'foo' : (1141.0 , [40 , 60 , 800 , 360 ]),
76+ 'bar' : (583.0 , [40 , - 10 , 543 , 210 ]),
77+ })
78+
79+
80+ def test_get_familyname_guessed ():
81+ fh = BytesIO (AFM_TEST_DATA )
82+ fm = afm .AFM (fh )
83+ del fm ._header [b'FamilyName' ] # remove FamilyName, so we have to guess
84+ assert fm .get_familyname () == 'My Font'
0 commit comments