1
1
#!/usr/bin/env python
2
2
"""
3
- Generates the toolbar icon images from the FontAwesome font.
3
+ Generates the Matplotlib icon, and the toolbar icon images from the FontAwesome
4
+ font.
4
5
5
- First download and extract FontAwesome from http://fontawesome.io/.
6
- Place the FontAwesome.otf file in the tools directory (same directory
7
- as this script).
8
-
9
- Generates SVG, PDF in one size (size they are vectors) and PNG, PPM and GIF in
10
- 24x24 and 48x48.
6
+ Generates SVG, PDF in one size (since they are vectors), and PNG in 24x24 and
7
+ 48x48.
11
8
"""
12
9
13
- import matplotlib
14
- matplotlib .use ('agg' ) # noqa
15
-
16
- import os
17
-
18
- from PIL import Image
19
-
20
- import numpy as np
10
+ from argparse import ArgumentParser , ArgumentDefaultsHelpFormatter
11
+ from io import BytesIO
12
+ from pathlib import Path
13
+ import tarfile
14
+ import urllib .request
21
15
16
+ import matplotlib as mpl
22
17
import matplotlib .pyplot as plt
23
18
from matplotlib .font_manager import FontProperties
24
- from matplotlib import cm
25
- from matplotlib import patheffects
26
- matplotlib .rcdefaults ()
19
+ import numpy as np
27
20
28
- matplotlib .rcParams ['svg.fonttype' ] = 'path'
29
- matplotlib .rcParams ['pdf.fonttype' ] = 3
30
- matplotlib .rcParams ['pdf.compression' ] = 9
31
21
22
+ plt .rcdefaults ()
23
+ plt .rcParams ['svg.fonttype' ] = 'path'
24
+ plt .rcParams ['pdf.fonttype' ] = 3
25
+ plt .rcParams ['pdf.compression' ] = 9
32
26
33
- IMAGES_ROOT = os .path .join (
34
- os .path .dirname (__file__ ), '..' , 'lib' , 'matplotlib' , 'mpl-data' , 'images' )
35
- FONT_PATH = os .path .join (
36
- os .path .dirname (__file__ ), 'FontAwesome.otf' )
37
27
28
+ def get_fontawesome ():
29
+ cached_path = Path (mpl .get_cachedir (), "FontAwesome.otf" )
30
+ if not cached_path .exists ():
31
+ with urllib .request .urlopen (
32
+ "https://github.com/FortAwesome/Font-Awesome"
33
+ "/archive/v4.7.0.tar.gz" ) as req , \
34
+ tarfile .open (fileobj = BytesIO (req .read ()), mode = "r:gz" ) as tf :
35
+ cached_path .write_bytes (tf .extractfile (tf .getmember (
36
+ "Font-Awesome-4.7.0/fonts/FontAwesome.otf" )).read ())
37
+ return cached_path
38
38
39
- def save_icon (fig , name ):
40
- fig .savefig (os .path .join (IMAGES_ROOT , name + '.svg' ))
41
- fig .savefig (os .path .join (IMAGES_ROOT , name + '.pdf' ))
42
39
40
+ def save_icon (fig , dest_dir , name ):
41
+ fig .savefig (dest_dir / (name + '.svg' ))
42
+ fig .savefig (dest_dir / (name + '.pdf' ))
43
43
for dpi , suffix in [(24 , '' ), (48 , '_large' )]:
44
- fig .savefig (os .path .join (IMAGES_ROOT , name + suffix + '.png' ), dpi = dpi )
45
-
46
- img = Image .open (os .path .join (IMAGES_ROOT , name + suffix + '.png' ))
47
- img .save (os .path .join (IMAGES_ROOT , name + suffix + '.ppm' ))
44
+ fig .savefig (dest_dir / (name + suffix + '.png' ), dpi = dpi )
48
45
49
46
50
- def make_icon (fontfile , ccode ):
51
- prop = FontProperties (fname = fontfile , size = 68 )
47
+ def make_icon (font_path , ccode ):
48
+ prop = FontProperties (fname = font_path , size = 68 )
52
49
53
50
fig = plt .figure (figsize = (1 , 1 ))
54
51
fig .patch .set_alpha (0.0 )
55
52
text = fig .text (0.5 , 0.48 , chr (ccode ), ha = 'center' , va = 'center' ,
56
53
fontproperties = prop )
57
- text .set_path_effects ([patheffects .Normal ()])
58
54
59
55
return fig
60
56
@@ -74,7 +70,7 @@ def make_matplotlib_icon():
74
70
edgecolor = 'k' )
75
71
76
72
for r , bar in zip (radii , bars ):
77
- bar .set_facecolor (cm .jet (r / 10. ))
73
+ bar .set_facecolor (mpl . cm .jet (r / 10 ))
78
74
79
75
ax .tick_params (labelleft = False , labelright = False ,
80
76
labelbottom = False , labeltop = False )
@@ -95,19 +91,25 @@ def make_matplotlib_icon():
95
91
('filesave' , 0xf0c7 ),
96
92
('subplots' , 0xf1de ),
97
93
('qt4_editor_options' , 0xf201 ),
98
- ('help' , 0xf128 )]
94
+ ('help' , 0xf128 ),
95
+ ]
99
96
100
97
101
98
def make_icons ():
99
+ parser = ArgumentParser (formatter_class = ArgumentDefaultsHelpFormatter )
100
+ parser .add_argument (
101
+ "-d" , "--dest-dir" ,
102
+ type = Path ,
103
+ default = Path (__file__ ).parent / "../lib/matplotlib/mpl-data/images" ,
104
+ help = "Directory where to store the images." )
105
+ args = parser .parse_args ()
106
+ font_path = get_fontawesome ()
102
107
for name , ccode in icon_defs :
103
- fig = make_icon (FONT_PATH , ccode )
104
- save_icon (fig , name )
108
+ fig = make_icon (font_path , ccode )
109
+ save_icon (fig , args . dest_dir , name )
105
110
fig = make_matplotlib_icon ()
106
- save_icon (fig , 'matplotlib' )
111
+ save_icon (fig , args . dest_dir , 'matplotlib' )
107
112
108
113
109
- if __name__ == '__main__' :
110
- if not os .path .exists (FONT_PATH ):
111
- print ("Download the FontAwesome.otf file and place it in the tools "
112
- "directory" )
114
+ if __name__ == "__main__" :
113
115
make_icons ()
0 commit comments