Skip to content

Commit 3419465

Browse files
authored
Merge pull request #14941 from anntzer/make_icons
Improvements to make_icons.py.
2 parents 7de91c8 + d791e85 commit 3419465

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

lib/matplotlib/font_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def set_file(self, file):
837837
Set the filename of the fontfile to use. In this case, all
838838
other properties will be ignored.
839839
"""
840-
self._file = file
840+
self._file = os.fspath(file) if file is not None else None
841841

842842
def set_fontconfig_pattern(self, pattern):
843843
"""

tools/make_icons.py

+46-44
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
#!/usr/bin/env python
22
"""
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.
45
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.
118
"""
129

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
2115

16+
import matplotlib as mpl
2217
import matplotlib.pyplot as plt
2318
from matplotlib.font_manager import FontProperties
24-
from matplotlib import cm
25-
from matplotlib import patheffects
26-
matplotlib.rcdefaults()
19+
import numpy as np
2720

28-
matplotlib.rcParams['svg.fonttype'] = 'path'
29-
matplotlib.rcParams['pdf.fonttype'] = 3
30-
matplotlib.rcParams['pdf.compression'] = 9
3121

22+
plt.rcdefaults()
23+
plt.rcParams['svg.fonttype'] = 'path'
24+
plt.rcParams['pdf.fonttype'] = 3
25+
plt.rcParams['pdf.compression'] = 9
3226

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')
3727

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
3838

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'))
4239

40+
def save_icon(fig, dest_dir, name):
41+
fig.savefig(dest_dir / (name + '.svg'))
42+
fig.savefig(dest_dir / (name + '.pdf'))
4343
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)
4845

4946

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)
5249

5350
fig = plt.figure(figsize=(1, 1))
5451
fig.patch.set_alpha(0.0)
5552
text = fig.text(0.5, 0.48, chr(ccode), ha='center', va='center',
5653
fontproperties=prop)
57-
text.set_path_effects([patheffects.Normal()])
5854

5955
return fig
6056

@@ -74,7 +70,7 @@ def make_matplotlib_icon():
7470
edgecolor='k')
7571

7672
for r, bar in zip(radii, bars):
77-
bar.set_facecolor(cm.jet(r/10.))
73+
bar.set_facecolor(mpl.cm.jet(r / 10))
7874

7975
ax.tick_params(labelleft=False, labelright=False,
8076
labelbottom=False, labeltop=False)
@@ -95,19 +91,25 @@ def make_matplotlib_icon():
9591
('filesave', 0xf0c7),
9692
('subplots', 0xf1de),
9793
('qt4_editor_options', 0xf201),
98-
('help', 0xf128)]
94+
('help', 0xf128),
95+
]
9996

10097

10198
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()
102107
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)
105110
fig = make_matplotlib_icon()
106-
save_icon(fig, 'matplotlib')
111+
save_icon(fig, args.dest_dir, 'matplotlib')
107112

108113

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__":
113115
make_icons()

0 commit comments

Comments
 (0)