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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# Convert Font Awesome, Google Material Design and Kenney Game icon font
# parameters to C++11, C89 and None compatible formats.
#
#------------------------------------------------------------------------------
# 1 - Source material
#
# 1.1 - Font Awesome
# https://github.com/FortAwesome/Font-Awesome/blob/master/fonts/fontawesome-webfont.ttf
# https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml
# 1.2 - Material Design
# https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf
# https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints
# 1.3 - Kenney icons
# https://github.com/SamBrishes/kenney-icon-font/blob/master/fonts/kenney-icon-font.ttf
# https://raw.githubusercontent.com/SamBrishes/kenney-icon-font/master/css/kenney-icons.css
#
#------------------------------------------------------------------------------
# 2 - Data samples
#
# 2.1 - Font Awesome
# - input: - name: Music
# id: music
# unicode: f001
# created: 1.0
# filter:
# - note
# - sound
# categories:
# - Web Application Icons
# - output C++11: #define ICON_FA_MUSIC u8"\uf001"
# - output C89: #define ICON_FA_MUSIC "\xEF\x80\x81"
# - output None: var icon-fa-music ""
#
# 2.2 - Google Material Design icons
# - input: 3d_rotation e84d
# - output C++11: #define ICON_MD_3D_ROTATION u8"\ue84d"
# - output C89: #define ICON_MD_3D_ROTATION "\xEE\xA1\x8D"
# - output None: var icon-md-3d_rotation ""
#
# 2.3 - Kenney Game icons
# - input: .ki-home:before{ content: "\e900"; }
# - output C++11: #define ICON_KI_HOME u8"\ue900"
# - output C89: #define ICON_KI_HOME "\xEE\xA4\x80"
# - output None: var icon-ki-home ""
#
# 2.4 - All fonts
# - computed min and max unicode fonts ICON_MIN and ICON_MAX
# - output C89, C++11: #define ICON_MIN_FA 0xf000
# #define ICON_MAX_FA 0xf295
# - output None: var icon-min-fa 0xf000
# var icon-max-fa 0xf2b2
#
#------------------------------------------------------------------------------
# 3 - Script dependencies
#
# 3.1 - Python 2.7 - https://www.python.org/download/releases/2.7/
# 3.2 - Requests - http://docs.python-requests.org/
# 3.3 - PyYAML - http://pyyaml.org/
#
#------------------------------------------------------------------------------
# 4 - References
#
# None language: https://bitbucket.org/duangle/nonelang/src
#
#------------------------------------------------------------------------------
import requests
import yaml
# Fonts
class Font:
font_tff = '[ ERROR - missing tff file info ]'
font_url = '[ ERROR - missing font data url ]'
font_name = '[ ERROR - missing font name ]'
font_abbr = '[ ERROR - missing font abbreviation ]'
@classmethod
def get_icons( cls, input ):
# intermediate representation of the fonts data, identify the min and max
print( '[ ERROR - missing implementation of class method get_icons for {!s} ]'.format( cls.font_name ))
icons_data = {}
icons_data.update({ 'font_min' : '[ ERROR - missing font min ]',
'font_max' : '[ ERROR - missing font max ]',
'icons' : '[ ERROR - missing list of pairs [ font icon name, code ]]' })
return icons_data
@classmethod
def download( cls ):
input_raw = ''
try :
response = requests.get( cls.font_url, timeout = 2 )
if response.status_code == 200:
input_raw = response.content
print( 'Downloaded - ' + cls.font_name )
except Exception as e :
print( '[ ERROR - {!s}: {!s} ]'.format( cls.font_name, e ))
return input_raw
@classmethod
def get_intermediate_representation( cls ):
font_ir = {}
input_raw = cls.download()
if input_raw:
icons_data = cls.get_icons( input_raw )
font_ir.update( icons_data )
font_ir.update({ 'font_tff' : cls.font_tff,
'font_url' : cls.font_url,
'font_name' : cls.font_name,
'font_abbr' : cls.font_abbr })
print( 'Generated intermediate data - ' + cls.font_name )
return font_ir
class FontFA( Font ):
font_tff = 'https://github.com/FortAwesome/Font-Awesome/blob/master/fonts/fontawesome-webfont.ttf'
font_url = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml'
font_name = 'font_awesome'
font_abbr = 'FA'
@classmethod
def get_icons( self, input ):
icons_data = {}
data = yaml.safe_load( input )
if data:
font_min = 'ffff'
font_max = '0'
icons = []
for item in data[ 'icons' ]:
if item[ 'unicode' ] < font_min:
font_min = item[ 'unicode' ]
if item[ 'unicode' ] >= font_max:
font_max = item[ 'unicode' ]
icons.append([ item[ 'id' ], item[ 'unicode' ]])
icons_data.update({ 'font_min' : font_min,
'font_max' : font_max,
'icons' : icons })
return icons_data
class FontMD( Font ):
font_tff = 'https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf'
font_url = 'https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints'
font_name = 'material_design'
font_abbr = 'MD'
@classmethod
def get_icons( self, input ):
icons_data = {}
lines = str.split( input, '\n' )
if lines:
font_min = 'ffff'
font_max = '0'
icons = []
for line in lines :
words = str.split(line)
if words and len( words ) >= 2:
if words[ 1 ] < font_min:
font_min = words[ 1 ]
if words[ 1 ] >= font_max:
font_max = words[ 1 ]
icons.append( words )
icons_data.update({ 'font_min' : font_min,
'font_max' : font_max,
'icons' : icons })
return icons_data
class FontKI( Font ):
font_tff = 'https://github.com/SamBrishes/kenney-icon-font/blob/master/fonts/kenney-icon-font.ttf'
font_url = 'https://raw.githubusercontent.com/SamBrishes/kenney-icon-font/master/css/kenney-icons.css'
font_name = 'kenney'
font_abbr = 'KI'
@classmethod
def get_icons( self, input ):
icons_data = {}
lines = str.split( input, '\n' )
if lines:
font_min = 'ffff'
font_max = '0'
icons = []
for line in lines :
if '.ki-' in line:
words = str.split(line)
if words and '.ki-' in words[ 0 ]:
font_id = words[ 0 ].partition( '.ki-' )[2].partition( ':before' )[0]
font_code = words[ 2 ].partition( '"\\' )[2].partition( '";' )[0]
if font_code < font_min:
font_min = font_code
if font_code >= font_max:
font_max = font_code
icons.append([ font_id, font_code ])
icons_data.update({ 'font_min' : font_min,
'font_max' : font_max,
'icons' : icons })
return icons_data
# Languages
class Language:
language_name = '[ ERROR - missing language name ]'
file_name = '[ ERROR - missing file name ]'
intermediate = {}
def __init__( self, intermediate ):
self.intermediate = intermediate
@classmethod
def prelude( cls ):
print('[ ERROR - missing implementation of class method prelude for {!s} ]'.format(cls.language_name))
result = '[ ERROR - missing prelude ]'
return result
@classmethod
def lines_minmax( cls ):
print('[ ERROR - missing implementation of class method lines_minmax for {!s} ]'.format(cls.language_name))
result = '[ ERROR - missing min and max ]'
return result
@classmethod
def line_icon( cls, icon ):
print('[ ERROR - missing implementation of class method line_icon for {!s} ]'.format( cls.language_name ))
result = '[ ERROR - missing icon line ]'
return result
@classmethod
def convert( cls ):
result = cls.prelude() + cls.lines_minmax()
for icon in cls.intermediate.get( 'icons' ):
line_icon = cls.line_icon( icon )
result += line_icon
print ( 'Converted - {!s} for {!s}' ).format( cls.intermediate.get( 'font_name' ), cls.language_name)
return result
@classmethod
def save_to_file( cls ):
filename = cls.file_name.format( name = str(cls.intermediate.get( 'font_name' )).replace( ' ', '' ))
converted = cls.convert()
with open( filename, 'w' ) as f:
f.write( converted )
print( 'Saved - {!s}' ).format( filename )
class LanguageC89( Language ):
language_name = 'C89'
file_name = 'icons_{name}.h'
@classmethod
def prelude( cls ):
tmpl_prelude = '// Generated by GenerateIconFontCppHeaders.py for language {lang}\n' + \
'// from {url}\n' + \
'// for use with {tff}\n' + \
'#pragma once\n\n'
result = tmpl_prelude.format(lang = cls.language_name,
url = cls.intermediate.get('font_url'),
tff = cls.intermediate.get('font_tff'))
return result
@classmethod
def lines_minmax( cls ):
tmpl_line_minmax = '#define ICON_{minmax}_{abbr} 0x{val}\n'
result = tmpl_line_minmax.format(minmax = 'MIN',
abbr = cls.intermediate.get('font_abbr'),
val = cls.intermediate.get('font_min')) + \
tmpl_line_minmax.format(minmax = 'MAX',
abbr = cls.intermediate.get('font_abbr'),
val = cls.intermediate.get('font_max'))
return result
@classmethod
def line_icon( cls, icon ):
tmpl_line_icon = '#define ICON_{abbr}_{icon} "{code}"\n'
icon_name = str.upper( icon[ 0 ]).replace( '-', '_' )
code_base = ''.join([ '{0:x}'.format( ord( x )) for x in unichr( int( icon[ 1 ], 16 )).encode( 'utf-8' )]).upper()
icon_code = '\\x' + code_base[ :2 ] + '\\x' + code_base[ 2:4 ] + '\\x' + code_base[ 4: ]
result = tmpl_line_icon.format( abbr = cls.intermediate.get( 'font_abbr' ),
icon = icon_name,
code = icon_code )
return result
class LanguageCpp11( LanguageC89 ):
language_name = 'C++11'
file_name = 'Icons{name}.h'
@classmethod
def line_icon( cls, icon ):
tmpl_line_icon = '#define ICON_{abbr}_{icon} u8"\u{code}"\n'
icon_name = str.upper( icon[ 0 ]).replace( '-', '_' )
icon_code = icon[ 1 ]
result = tmpl_line_icon.format( abbr = cls.intermediate.get('font_abbr'),
icon = icon_name,
code = icon_code)
return result
class LanguageNone( Language ):
language_name = 'None'
file_name = 'Icons{name}.n'
@classmethod
def prelude( cls ):
tmpl_prelude = 'none\n' + \
'; Generated by GenerateIconFontCppHeaders.py for language {lang}\n' + \
'; from {url}\n' + \
'; for use with {tff}\n' + \
'\n$\n'
result = tmpl_prelude.format( lang = cls.language_name,
url = cls.intermediate.get( 'font_url' ),
tff = cls.intermediate.get( 'font_tff' ))
return result
@classmethod
def lines_minmax( cls ):
tmpl_line_minmax = ' var icon-{minmax}-{abbr} 0x{val}\n'
result = tmpl_line_minmax.format( minmax = 'min',
abbr = cls.intermediate.get( 'font_abbr' ).lower(),
val = cls.intermediate.get( 'font_min' )) + \
tmpl_line_minmax.format( minmax = 'max',
abbr = cls.intermediate.get( 'font_abbr' ).lower(),
val = cls.intermediate.get( 'font_max' ))
return result
@classmethod
def line_icon( cls, icon ):
tmpl_line_icon = ' var icon-{abbr}-{icon} "{code}"\n'
icon_name = str.upper( icon[ 0 ]).replace( '-', '_' ).lower()
icon_code = unichr( int( icon[ 1 ], 16 )).encode( 'utf-8' )
result = tmpl_line_icon.format( abbr = cls.intermediate.get( 'font_abbr' ).lower(),
icon = icon_name,
code = icon_code )
return result
# Main
fonts = [ FontKI , FontMD, FontFA ]
languages = [ LanguageC89 ]
intermediates = []
for font in fonts:
intermediates.append( font.get_intermediate_representation())
for interm in intermediates:
Language.intermediate = interm
for lang in languages:
lang.save_to_file()
|