-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenicons.py
192 lines (159 loc) · 5.51 KB
/
genicons.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# genicons.py - generate all the icons.
# Copyright 2016 Creative Commons Corporation.
# Standard library
import errno
import math
import os
import os.path
# Third-party
import cairo
import pangocairo
SUITES = {
"l": {
"by": ["b"],
"by-nc": ["bn", "be", "by"],
"by-nd": ["bd"],
"by-sa": ["ba"],
"by-nc-nd": ["bnd", "bed", "byd"],
"by-nc-sa": ["bna", "bea", "bya"],
},
"p": {"cc-zero": ["0"], "public-domain-mark": ["p"]},
}
DIMENSIONS = ((88, 31, 31, 1), (80, 15, 15, 4), (76, 22, 22, 1))
BACKGROUNDS = (
"transparent",
"000000",
# Bootstrap well grey
"eeeeee",
"ffffff",
)
STEPS = ["00", "11", "22", "33", "66", "99", "ff"]
FOREGROUNDS = [
"%s%s%s" % (r, g, b) for r in STEPS for g in STEPS for b in STEPS
]
HEX_TO_FLOAT = 1.0 / 255.0
def hexToFloat(digits):
return HEX_TO_FLOAT * int(digits, 16)
def setColor(ctx, color_string):
if color_string == "transparent":
ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0)
else:
ctx.set_source_rgb(
hexToFloat(color_string[0:2]),
hexToFloat(color_string[2:4]),
hexToFloat(color_string[4:6]),
)
def sizeChars(ctx, chars):
# x, y, width, height, dx, dy
return [ctx.text_extents(char) for char in chars]
def showChars(ctx, chars, foreground, padding, width, height):
sizes = sizeChars(ctx, chars)
total_padding = padding * (len(chars) - 1)
total_width = reduce(lambda x, y: y[3] + x, sizes, 0.0)
x = 0.5 + math.ceil((width / 2) - ((total_width + total_padding) / 2))
y = math.ceil(height / 2) + math.floor(sizes[0][3] / 2)
setColor(ctx, foreground)
for char, size in zip(chars, sizes):
ctx.move_to(math.ceil(x), math.ceil(y))
ctx.show_text(char)
x += size[2] + padding
ctx.stroke()
def createContext(width, height):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = cairo.Context(surface)
ctx = pangocairo.CairoContext(context)
return ctx
def setBackground(ctx, background, width, height):
setColor(ctx, background)
ctx.rectangle(0, 0, width, height)
ctx.fill()
def configureFont(ctx, size):
ctx.select_font_face("CC Icons")
ctx.set_font_size(size)
def iconFilename(dimensions, characters):
filename = "%ix%i" % (dimensions[0], dimensions[1])
if "y" in characters:
filename += "-y"
elif "e" in characters:
filename += "-e"
return filename + ".png"
def iconPath(suite, descriptor, background, foreground):
foreground_path = os.path.join(
foreground[0:2], foreground[2:4], foreground[4:6]
)
return os.path.join(suite, descriptor, background, foreground_path)
def genicon(
suite,
characters,
font_size,
padding,
width,
height,
background,
foreground,
):
ctx = createContext(width, height)
setBackground(ctx, background, width, height)
configureFont(ctx, font_size)
showChars(ctx, characters, foreground, padding, width, height)
return ctx
font_map = pangocairo.cairo_font_map_get_default()
font_families = [family.get_name() for family in font_map.list_families()]
if "CC Icons" not in font_families:
raise Exception(
"CC Icons font not installed. See" " <https://wiki.debian.org/Fonts>."
)
script_dir = os.path.dirname(__file__)
basedir = os.path.realpath(
os.path.abspath(os.path.join(script_dir, "..", "www", "i"))
)
print "# basedir:", basedir
for suite, licenses in SUITES.iteritems():
for lic, module_chars in licenses.iteritems():
for chars in module_chars:
for dimensions in DIMENSIONS:
for background in BACKGROUNDS:
for foreground in FOREGROUNDS:
# e.g. white on white
if foreground == background:
continue
path = os.path.realpath(
os.path.abspath(
os.path.join(
basedir,
iconPath(
suite, lic, background, foreground
),
)
)
)
filepath = os.path.realpath(
os.path.abspath(
os.path.join(
path, iconFilename(dimensions, chars)
)
)
)
if os.path.exists(filepath):
continue
width = dimensions[0]
height = dimensions[1]
font_size = dimensions[2]
padding = dimensions[3]
ctx = genicon(
suite,
chars,
font_size,
padding,
width,
height,
background,
foreground,
)
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Will raise and exception on error
ctx.get_target().write_to_png(filepath)