File: make_icons.py

package info (click to toggle)
matplotlib 2.0.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 91,640 kB
  • ctags: 29,525
  • sloc: python: 122,697; cpp: 60,806; ansic: 30,799; objc: 2,830; makefile: 224; sh: 85
file content (117 lines) | stat: -rwxr-xr-x 3,161 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
"""
Generates 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.
"""

import matplotlib
matplotlib.use('agg')

import six

import os

from PIL import Image

import numpy as np

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib import cm
import matplotlib
import matplotlib.patheffects as PathEffects
matplotlib.rcdefaults()

matplotlib.rcParams['svg.fonttype'] = 'path'
matplotlib.rcParams['pdf.fonttype'] = 3
matplotlib.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 save_icon(fig, name):
    fig.savefig(os.path.join(IMAGES_ROOT, name + '.svg'))
    fig.savefig(os.path.join(IMAGES_ROOT, 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'))


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

    fig = plt.figure(figsize=(1, 1))
    fig.patch.set_alpha(0.0)
    text = fig.text(0.5, 0.48, six.unichr(ccode), ha='center', va='center',
                    fontproperties=prop)
    text.set_path_effects([PathEffects.Normal()])

    return fig


def make_matplotlib_icon():
    fig = plt.figure(figsize=(1, 1))
    fig.patch.set_alpha(0.0)
    ax = fig.add_axes([0.025, 0.025, 0.95, 0.95], projection='polar')
    ax.set_axisbelow(True)

    N = 7
    arc = 2. * np.pi
    theta = np.arange(0.0, arc, arc/N)
    radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])
    width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
    bars = ax.bar(theta, radii, width=width, bottom=0.0, linewidth=1,
                  edgecolor='k')

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

    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(False)

    for line in ax.get_ygridlines() + ax.get_xgridlines():
        line.set_lw(0.0)

    ax.set_yticks(np.arange(1, 9, 2))
    ax.set_rmax(9)

    return fig


icon_defs = [
    ('home', 0xf015),
    ('back', 0xf060),
    ('forward', 0xf061),
    ('zoom_to_rect', 0xf002),
    ('move', 0xf047),
    ('filesave', 0xf0c7),
    ('subplots', 0xf1de),
    ('qt4_editor_options', 0xf201)]


def make_icons():
    for name, ccode in icon_defs:
        fig = make_icon(FONT_PATH, ccode)
        save_icon(fig, name)
    fig = make_matplotlib_icon()
    save_icon(fig, 'matplotlib')


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