Skip to content

Improvements to make_icons.py. #14941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def set_file(self, file):
Set the filename of the fontfile to use. In this case, all
other properties will be ignored.
"""
self._file = file
self._file = os.fspath(file) if file is not None else None

def set_fontconfig_pattern(self, pattern):
"""
Expand Down
90 changes: 46 additions & 44 deletions tools/make_icons.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
#!/usr/bin/env python
"""
Generates the toolbar icon images from the FontAwesome font.
Generates the Matplotlib icon, and the toolbar icon images from the FontAwesome
font.

First download and extract FontAwesome from http://fontawesome.io/.
Place the FontAwesome.otf file in the tools directory (same directory
as this script).

Generates SVG, PDF in one size (size they are vectors) and PNG, PPM and GIF in
24x24 and 48x48.
Generates SVG, PDF in one size (since they are vectors), and PNG in 24x24 and
48x48.
"""

import matplotlib
matplotlib.use('agg') # noqa

import os

from PIL import Image

import numpy as np
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from io import BytesIO
from pathlib import Path
import tarfile
import urllib.request

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib import cm
from matplotlib import patheffects
matplotlib.rcdefaults()
import numpy as np

matplotlib.rcParams['svg.fonttype'] = 'path'
matplotlib.rcParams['pdf.fonttype'] = 3
matplotlib.rcParams['pdf.compression'] = 9

plt.rcdefaults()
plt.rcParams['svg.fonttype'] = 'path'
plt.rcParams['pdf.fonttype'] = 3
plt.rcParams['pdf.compression'] = 9

IMAGES_ROOT = os.path.join(
os.path.dirname(__file__), '..', 'lib', 'matplotlib', 'mpl-data', 'images')
FONT_PATH = os.path.join(
os.path.dirname(__file__), 'FontAwesome.otf')

def get_fontawesome():
cached_path = Path(mpl.get_cachedir(), "FontAwesome.otf")
if not cached_path.exists():
with urllib.request.urlopen(
"https://github.com/FortAwesome/Font-Awesome"
"/archive/v4.7.0.tar.gz") as req, \
tarfile.open(fileobj=BytesIO(req.read()), mode="r:gz") as tf:
cached_path.write_bytes(tf.extractfile(tf.getmember(
"Font-Awesome-4.7.0/fonts/FontAwesome.otf")).read())
return cached_path

def save_icon(fig, name):
fig.savefig(os.path.join(IMAGES_ROOT, name + '.svg'))
fig.savefig(os.path.join(IMAGES_ROOT, name + '.pdf'))

def save_icon(fig, dest_dir, name):
fig.savefig(dest_dir / (name + '.svg'))
fig.savefig(dest_dir / (name + '.pdf'))
for dpi, suffix in [(24, ''), (48, '_large')]:
fig.savefig(os.path.join(IMAGES_ROOT, name + suffix + '.png'), dpi=dpi)

img = Image.open(os.path.join(IMAGES_ROOT, name + suffix + '.png'))
img.save(os.path.join(IMAGES_ROOT, name + suffix + '.ppm'))
fig.savefig(dest_dir / (name + suffix + '.png'), dpi=dpi)


def make_icon(fontfile, ccode):
prop = FontProperties(fname=fontfile, size=68)
def make_icon(font_path, ccode):
prop = FontProperties(fname=font_path, size=68)

fig = plt.figure(figsize=(1, 1))
fig.patch.set_alpha(0.0)
text = fig.text(0.5, 0.48, chr(ccode), ha='center', va='center',
fontproperties=prop)
text.set_path_effects([patheffects.Normal()])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this did not have any effect, so removing is fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent was probably to force the text to be rendered as a bezier rather than relying on a font file that may not exist in the end user's computer, but we set svg.fonttype to path so that also does the conversion, and the pdf backend takes care of embedding the font, so we should be good?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the details here, so relying on your investigation. But your question mark makes me wonder how sure you are...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code of patheffects.py I'm fairly confident that the description is correct, but to know what the original intent was, we would need to get @mdboom's input, if he still lurks aroud...


return fig

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

for r, bar in zip(radii, bars):
bar.set_facecolor(cm.jet(r/10.))
bar.set_facecolor(mpl.cm.jet(r / 10))

ax.tick_params(labelleft=False, labelright=False,
labelbottom=False, labeltop=False)
Expand All @@ -95,19 +91,25 @@ def make_matplotlib_icon():
('filesave', 0xf0c7),
('subplots', 0xf1de),
('qt4_editor_options', 0xf201),
('help', 0xf128)]
('help', 0xf128),
]


def make_icons():
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument(
"-d", "--dest-dir",
type=Path,
default=Path(__file__).parent / "../lib/matplotlib/mpl-data/images",
help="Directory where to store the images.")
args = parser.parse_args()
font_path = get_fontawesome()
for name, ccode in icon_defs:
fig = make_icon(FONT_PATH, ccode)
save_icon(fig, name)
fig = make_icon(font_path, ccode)
save_icon(fig, args.dest_dir, name)
fig = make_matplotlib_icon()
save_icon(fig, 'matplotlib')
save_icon(fig, args.dest_dir, 'matplotlib')


if __name__ == '__main__':
if not os.path.exists(FONT_PATH):
print("Download the FontAwesome.otf file and place it in the tools "
"directory")
if __name__ == "__main__":
make_icons()