| 1 |
# Convert Font Awesome, Fork Awesome, Google Material Design, Kenney Game and Fontaudio |
| 2 |
# icon font parameters to C89, C++11 and C# compatible formats. |
| 3 |
# |
| 4 |
#------------------------------------------------------------------------------ |
| 5 |
# 1 - Source material |
| 6 |
# |
| 7 |
# 1.1 - Font Awesome |
| 8 |
# 1.1.1 - version 4 |
| 9 |
# https://raw.githubusercontent.com/FortAwesome/Font-Awesome/fa-4/src/icons.yml |
| 10 |
# https://github.com/FortAwesome/Font-Awesome/blob/fa-4/fonts/fontawesome-webfont.ttf |
| 11 |
# 1.1.2 - version 5 |
| 12 |
# https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/metadata/icons.yml |
| 13 |
# https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-brands-400.ttf |
| 14 |
# https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-regular-400.ttf |
| 15 |
# https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-solid-900.ttf |
| 16 |
# 1.1.3 - version 5 Pro |
| 17 |
# Download files from https://fontawesome.com |
| 18 |
# \fontawesome-pro-n.n.n-web\metadata\icons.yml |
| 19 |
# \fontawesome-pro-n.n.n-web\webfonts\fa-brands-400.ttf |
| 20 |
# \fontawesome-pro-n.n.n-web\webfonts\fa-light-300.ttf |
| 21 |
# \fontawesome-pro-n.n.n-web\webfonts\fa-regular-400.ttf |
| 22 |
# \fontawesome-pro-n.n.n-web\webfonts\fa-solid-900.ttf |
| 23 |
# 1.2 - Fork Awesome |
| 24 |
# https://raw.githubusercontent.com/ForkAwesome/Fork-Awesome/master/src/icons/icons.yml |
| 25 |
# https://github.com/ForkAwesome/Fork-Awesome/blob/master/fonts/forkawesome-webfont.ttf |
| 26 |
# 1.3 - Google Material Design |
| 27 |
# https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints |
| 28 |
# https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf |
| 29 |
# 1.4 - Kenney Game icons |
| 30 |
# https://raw.githubusercontent.com/nicodinh/kenney-icon-font/master/css/kenney-icons.css |
| 31 |
# https://github.com/nicodinh/kenney-icon-font/blob/master/fonts/kenney-icon-font.ttf |
| 32 |
# 1.5 - Fontaudio |
| 33 |
# https://raw.githubusercontent.com/fefanto/fontaudio/master/font/fontaudio.css |
| 34 |
# https://github.com/fefanto/fontaudio/blob/master/font/fontaudio.ttf |
| 35 |
#------------------------------------------------------------------------------ |
| 36 |
# 2 - Data sample |
| 37 |
# |
| 38 |
# Font Awesome example: |
| 39 |
# - input: music: |
| 40 |
# changes: |
| 41 |
# - '1' |
| 42 |
# - 5.0.0 |
| 43 |
# label: Music |
| 44 |
# search: |
| 45 |
# terms: |
| 46 |
# - note |
| 47 |
# - sound |
| 48 |
# styles: |
| 49 |
# - solid |
| 50 |
# unicode: f001 |
| 51 |
# - output C++11: #define ICON_FA_MUSIC u8"\uf001" |
| 52 |
# - output C89: #define ICON_FA_MUSIC "\xEF\x80\x81" |
| 53 |
# - output C#: public const string Music = "\uf001"; |
| 54 |
# |
| 55 |
# All fonts have computed min and max unicode fonts ICON_MIN and ICON_MAX |
| 56 |
# - output C89, C++11: #define ICON_MIN_FA 0xf000 |
| 57 |
# #define ICON_MAX_FA 0xf2e0 |
| 58 |
# Exception for Font Awesome brands: we use ICON_MIN_FAB and ICON_MAX_FAB |
| 59 |
# to differentiate between brand and non-brand icons so they can be used together |
| 60 |
# (the defines must be unique in C and C++). |
| 61 |
# - output C#: public const int IconMin = 0xf000; |
| 62 |
# public const int IconMax = 0xf2e0; |
| 63 |
# |
| 64 |
#------------------------------------------------------------------------------ |
| 65 |
# 3 - Script dependencies |
| 66 |
# |
| 67 |
# 3.1 - Fonts source material online |
| 68 |
# 3.2 - Python 3 - https://www.python.org/downloads/ |
| 69 |
# 3.3 - Requests - https://pypi.org/project/requests/ |
| 70 |
# 3.4 - PyYAML - https://pypi.org/project/PyYAML/ |
| 71 |
# |
| 72 |
#------------------------------------------------------------------------------ |
| 73 |
# 4 - References |
| 74 |
# |
| 75 |
# GitHub repository: https://github.com/juliettef/IconFontCppHeaders/ |
| 76 |
# |
| 77 |
#------------------------------------------------------------------------------ |
| 78 |
|
| 79 |
|
| 80 |
import requests |
| 81 |
import yaml |
| 82 |
import os |
| 83 |
import sys |
| 84 |
|
| 85 |
if sys.version_info[0] < 3: |
| 86 |
raise Exception( "Python 3 or a more recent version is required." ) |
| 87 |
|
| 88 |
# Fonts |
| 89 |
|
| 90 |
class Font: |
| 91 |
font_name = '[ ERROR - missing font name ]' |
| 92 |
font_abbr = '[ ERROR - missing font abbreviation ]' |
| 93 |
font_minmax_abbr = '' # optional - use if min and max defines must be differentiated. See Font Awesome Brand for example. |
| 94 |
font_data = '[ ERROR - missing font data file or url ]' |
| 95 |
font_ttf = '[ ERROR - missing ttf file or url ]' |
| 96 |
font_file_name_ttf = '[ ERROR - missing ttf file name ]' |
| 97 |
|
| 98 |
@classmethod |
| 99 |
def get_icons( cls, input ): |
| 100 |
# intermediate representation of the fonts data, identify the min and max |
| 101 |
print( '[ ERROR - missing implementation of class method get_icons for {!s} ]'.format( cls.font_name )) |
| 102 |
icons_data = {} |
| 103 |
icons_data.update({ 'font_min' : '[ ERROR - missing font min ]', |
| 104 |
'font_max' : '[ ERROR - missing font max ]', |
| 105 |
'icons' : '[ ERROR - missing list of pairs [ font icon name, code ]]' }) |
| 106 |
return icons_data |
| 107 |
|
| 108 |
@classmethod |
| 109 |
def get_intermediate_representation( cls ): |
| 110 |
font_ir = {} |
| 111 |
input_raw = '' |
| 112 |
if 'http' in cls.font_data: # if url, download data |
| 113 |
response = requests.get( cls.font_data, timeout = 2 ) |
| 114 |
if response.status_code == 200: |
| 115 |
input_raw = response.text |
| 116 |
print( 'Downloaded - ' + cls.font_name ) |
| 117 |
else: |
| 118 |
raise Exception( 'Download failed - ' + cls.font_name ) |
| 119 |
else: # read data from file if present |
| 120 |
if os.path.isfile( cls.font_data ): |
| 121 |
with open( cls.font_data, 'r' ) as f: |
| 122 |
input_raw = f.read() |
| 123 |
print( 'File read - ' + cls.font_name ) |
| 124 |
else: |
| 125 |
raise Exception( 'File ' + cls.font_data + ' missing - ' + cls.font_name ) |
| 126 |
if input_raw: |
| 127 |
icons_data = cls.get_icons( input_raw ) |
| 128 |
font_ir.update( icons_data ) |
| 129 |
font_ir.update({ 'font_ttf' : cls.font_ttf, |
| 130 |
'font_data' : cls.font_data, |
| 131 |
'font_file_name_ttf' : cls.font_file_name_ttf, |
| 132 |
'font_name' : cls.font_name, |
| 133 |
'font_abbr' : cls.font_abbr, |
| 134 |
'font_minmax_abbr' : cls.font_minmax_abbr }) |
| 135 |
print( 'Generated intermediate data - ' + cls.font_name ) |
| 136 |
return font_ir |
| 137 |
|
| 138 |
|
| 139 |
class FontFA4( Font ): # legacy Font Awesome version 4 |
| 140 |
font_name = 'Font Awesome 4' |
| 141 |
font_abbr = 'FA' |
| 142 |
font_data = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/fa-4/src/icons.yml' |
| 143 |
font_ttf = 'https://github.com/FortAwesome/Font-Awesome/blob/fa-4/fonts/fontawesome-webfont.ttf' |
| 144 |
font_file_name_ttf = [[ font_abbr, font_ttf[ font_ttf.rfind('/')+1: ]]] |
| 145 |
|
| 146 |
@classmethod |
| 147 |
def get_icons( self, input ): |
| 148 |
icons_data = { } |
| 149 |
data = yaml.safe_load(input) |
| 150 |
font_min = 'ffff' |
| 151 |
font_max = '0' |
| 152 |
icons = [] |
| 153 |
for item in data[ 'icons' ]: |
| 154 |
if item[ 'unicode' ] < font_min: |
| 155 |
font_min = item[ 'unicode' ] |
| 156 |
if item[ 'unicode' ] >= font_max: |
| 157 |
font_max = item[ 'unicode' ] |
| 158 |
icons.append([ item[ 'id' ], item[ 'unicode' ]]) |
| 159 |
icons_data.update({ 'font_min' : font_min, |
| 160 |
'font_max' : font_max, |
| 161 |
'icons' : icons }) |
| 162 |
return icons_data |
| 163 |
|
| 164 |
|
| 165 |
class FontFK( FontFA4 ): # Fork Awesome, based on Font Awesome 4 |
| 166 |
font_name = 'Fork Awesome' |
| 167 |
font_abbr = 'FK' |
| 168 |
font_data = 'https://raw.githubusercontent.com/ForkAwesome/Fork-Awesome/master/src/icons/icons.yml' |
| 169 |
font_ttf = 'https://github.com/ForkAwesome/Fork-Awesome/blob/master/fonts/forkawesome-webfont.ttf' |
| 170 |
font_file_name_ttf = [[ font_abbr, font_ttf[ font_ttf.rfind('/')+1: ]]] |
| 171 |
|
| 172 |
|
| 173 |
class FontFA5( Font ): # Font Awesome version 5 - Regular and Solid styles |
| 174 |
font_name = 'Font Awesome 5' |
| 175 |
font_abbr = 'FA' |
| 176 |
font_data = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/metadata/icons.yml' |
| 177 |
font_ttf = 'https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-solid-900.ttf, ' +\ |
| 178 |
'https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-regular-400.ttf, ' |
| 179 |
font_file_name_ttf = [[ 'FAR', 'fa-regular-400.ttf' ], [ 'FAS', 'fa-solid-900.ttf' ]] |
| 180 |
font_fa_style = [ 'regular', 'solid' ] |
| 181 |
|
| 182 |
@classmethod |
| 183 |
def get_icons( self, input ): |
| 184 |
icons_data = { } |
| 185 |
data = yaml.safe_load(input) |
| 186 |
if data: |
| 187 |
font_min = 'ffff' |
| 188 |
font_max = '0' |
| 189 |
icons = [] |
| 190 |
for key in data: |
| 191 |
item = data[ key ] |
| 192 |
for style in item[ 'styles' ]: |
| 193 |
if style in self.font_fa_style: |
| 194 |
if [ key, item[ 'unicode' ]] not in icons: |
| 195 |
if item[ 'unicode' ] < font_min: |
| 196 |
font_min = item[ 'unicode' ] |
| 197 |
if item[ 'unicode' ] >= font_max: |
| 198 |
font_max = item[ 'unicode' ] |
| 199 |
icons.append([ key, item[ 'unicode' ] ]) |
| 200 |
icons_data.update({ 'font_min':font_min, 'font_max':font_max, 'icons':icons }) |
| 201 |
return icons_data |
| 202 |
|
| 203 |
|
| 204 |
class FontFA5Brands( FontFA5 ): # Font Awesome version 5 - Brand style |
| 205 |
font_name = 'Font Awesome 5 Brands' |
| 206 |
font_minmax_abbr = 'FAB' |
| 207 |
font_ttf = 'https://github.com/FortAwesome/Font-Awesome/blob/master/webfonts/fa-brands-400.ttf' |
| 208 |
font_file_name_ttf = [[ 'FAB', 'fa-brands-400.ttf' ]] |
| 209 |
font_fa_style = [ 'brands' ] |
| 210 |
|
| 211 |
|
| 212 |
class FontFA5Pro( FontFA5 ): # Font Awesome version 5 Pro - Light, Regular and Solid styles |
| 213 |
font_name = 'Font Awesome 5 Pro' |
| 214 |
font_data = 'icons.yml' |
| 215 |
font_ttf = 'fa-light-300.ttf, fa-regular-400.ttf, fa-solid-900.ttf' |
| 216 |
font_file_name_ttf = [[ 'FAL', 'fa-light-300.ttf' ], [ 'FAR', 'fa-regular-400.ttf' ], [ 'FAS', 'fa-solid-900.ttf' ]] |
| 217 |
font_fa_style = [ 'light', 'regular', 'solid' ] |
| 218 |
|
| 219 |
|
| 220 |
class FontFA5ProBrands( FontFA5 ): # Font Awesome version 5 Pro - Brand style |
| 221 |
font_name = 'Font Awesome 5 Pro Brands' |
| 222 |
font_minmax_abbr = 'FAB' |
| 223 |
font_data = 'icons.yml' |
| 224 |
font_ttf = 'fa-brands-400.ttf' |
| 225 |
font_file_name_ttf = [[ 'FAB', 'fa-brands-400.ttf' ]] |
| 226 |
font_fa_style = [ 'brands' ] |
| 227 |
|
| 228 |
|
| 229 |
class FontMD( Font ): # Material Design |
| 230 |
font_name = 'Material Design' |
| 231 |
font_abbr = 'MD' |
| 232 |
font_data = 'https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints' |
| 233 |
font_ttf = 'https://github.com/google/material-design-icons/blob/master/iconfont/MaterialIcons-Regular.ttf' |
| 234 |
font_file_name_ttf = [[ font_abbr, font_ttf[ font_ttf.rfind('/')+1: ]]] |
| 235 |
|
| 236 |
@classmethod |
| 237 |
def get_icons( self, input ): |
| 238 |
icons_data = {} |
| 239 |
lines = str.split( input, '\n' ) |
| 240 |
if lines: |
| 241 |
font_min = 'ffff' |
| 242 |
font_max = '0' |
| 243 |
icons = [] |
| 244 |
for line in lines : |
| 245 |
words = str.split(line) |
| 246 |
if words and len( words ) >= 2: |
| 247 |
if words[ 1 ] < font_min: |
| 248 |
font_min = words[ 1 ] |
| 249 |
if words[ 1 ] >= font_max: |
| 250 |
font_max = words[ 1 ] |
| 251 |
icons.append( words ) |
| 252 |
icons_data.update({ 'font_min' : font_min, |
| 253 |
'font_max' : font_max, |
| 254 |
'icons' : icons }) |
| 255 |
return icons_data |
| 256 |
|
| 257 |
|
| 258 |
class FontKI( Font ): # Kenney Game icons |
| 259 |
font_name = 'Kenney' |
| 260 |
font_abbr = 'KI' |
| 261 |
font_data = 'https://raw.githubusercontent.com/nicodinh/kenney-icon-font/master/css/kenney-icons.css' |
| 262 |
font_ttf = 'https://github.com/nicodinh/kenney-icon-font/blob/master/fonts/kenney-icon-font.ttf' |
| 263 |
font_file_name_ttf = [[ font_abbr, font_ttf[ font_ttf.rfind('/')+1: ]]] |
| 264 |
|
| 265 |
@classmethod |
| 266 |
def get_icons( self, input ): |
| 267 |
icons_data = {} |
| 268 |
lines = str.split( input, '\n' ) |
| 269 |
if lines: |
| 270 |
font_min = 'ffff' |
| 271 |
font_max = '0' |
| 272 |
icons = [] |
| 273 |
for line in lines : |
| 274 |
if '.ki-' in line: |
| 275 |
words = str.split(line) |
| 276 |
if words and '.ki-' in words[ 0 ]: |
| 277 |
font_id = words[ 0 ].partition( '.ki-' )[2].partition( ':before' )[0] |
| 278 |
font_code = words[ 2 ].partition( '"\\' )[2].partition( '";' )[0] |
| 279 |
if font_code < font_min: |
| 280 |
font_min = font_code |
| 281 |
if font_code >= font_max: |
| 282 |
font_max = font_code |
| 283 |
icons.append([ font_id, font_code ]) |
| 284 |
icons_data.update({ 'font_min' : font_min, |
| 285 |
'font_max' : font_max, |
| 286 |
'icons' : icons }) |
| 287 |
return icons_data |
| 288 |
|
| 289 |
|
| 290 |
class FontFAD( Font ): # Fontaudio |
| 291 |
font_name = 'Fontaudio' |
| 292 |
font_abbr = 'FAD' |
| 293 |
font_data = 'https://raw.githubusercontent.com/fefanto/fontaudio/master/font/fontaudio.css' |
| 294 |
font_ttf = 'https://github.com/fefanto/fontaudio/raw/master/font/fontaudio.ttf' |
| 295 |
font_file_name_ttf = [[ font_abbr, font_ttf[ font_ttf.rfind('/') + 1: ]]] |
| 296 |
|
| 297 |
@classmethod |
| 298 |
def get_icons( self, input ): |
| 299 |
icons_data = {} |
| 300 |
lines = str.split( input, '}\n' ) |
| 301 |
if lines: |
| 302 |
font_min = 'ffff' |
| 303 |
font_max = '0' |
| 304 |
icons = [] |
| 305 |
for line in lines : |
| 306 |
if '.icon-fad-' in line: |
| 307 |
words = str.split( line ) |
| 308 |
if words and '.icon-fad-' in words[ 0 ]: |
| 309 |
font_id = words[ 0 ].partition( '.icon-fad-' )[2].partition( ':before' )[0] |
| 310 |
font_code = words[ 3 ].partition( '"\\' )[2].partition( '";' )[0] |
| 311 |
if font_code < font_min: |
| 312 |
font_min = font_code |
| 313 |
if font_code >= font_max: |
| 314 |
font_max = font_code |
| 315 |
icons.append([ font_id, font_code ]) |
| 316 |
icons_data.update({ 'font_min' : font_min, |
| 317 |
'font_max' : font_max, |
| 318 |
'icons' : icons }) |
| 319 |
return icons_data |
| 320 |
|
| 321 |
|
| 322 |
# Languages |
| 323 |
|
| 324 |
|
| 325 |
class Language: |
| 326 |
language_name = '[ ERROR - missing language name ]' |
| 327 |
file_name = '[ ERROR - missing file name ]' |
| 328 |
intermediate = {} |
| 329 |
|
| 330 |
def __init__( self, intermediate ): |
| 331 |
self.intermediate = intermediate |
| 332 |
|
| 333 |
@classmethod |
| 334 |
def prelude( cls ): |
| 335 |
print( '[ ERROR - missing implementation of class method prelude for {!s} ]'.format( cls.language_name )) |
| 336 |
result = '[ ERROR - missing prelude ]' |
| 337 |
return result |
| 338 |
|
| 339 |
@classmethod |
| 340 |
def lines_minmax( cls ): |
| 341 |
print( '[ ERROR - missing implementation of class method lines_minmax for {!s} ]'.format( cls.language_name )) |
| 342 |
result = '[ ERROR - missing min and max ]' |
| 343 |
return result |
| 344 |
|
| 345 |
@classmethod |
| 346 |
def line_icon( cls, icon ): |
| 347 |
print( '[ ERROR - missing implementation of class method line_icon for {!s} ]'.format( cls.language_name )) |
| 348 |
result = '[ ERROR - missing icon line ]' |
| 349 |
return result |
| 350 |
|
| 351 |
@classmethod |
| 352 |
def epilogue( cls ): |
| 353 |
return '' |
| 354 |
|
| 355 |
@classmethod |
| 356 |
def convert( cls ): |
| 357 |
result = cls.prelude() + cls.lines_minmax() |
| 358 |
for icon in cls.intermediate.get( 'icons' ): |
| 359 |
line_icon = cls.line_icon( icon ) |
| 360 |
result += line_icon |
| 361 |
result += cls.epilogue() |
| 362 |
print(( 'Converted - {!s} for {!s}' ).format( cls.intermediate.get( 'font_name' ), cls.language_name )) |
| 363 |
return result |
| 364 |
|
| 365 |
@classmethod |
| 366 |
def save_to_file( cls ): |
| 367 |
filename = cls.file_name.format( name = str(cls.intermediate.get( 'font_name' )).replace( ' ', '' )) |
| 368 |
converted = cls.convert() |
| 369 |
with open( filename, 'w' ) as f: |
| 370 |
f.write( converted ) |
| 371 |
print(( 'Saved - {!s}' ).format( filename )) |
| 372 |
|
| 373 |
|
| 374 |
class LanguageC89( Language ): |
| 375 |
language_name = 'C89' |
| 376 |
file_name = 'Icons{name}_c.h' |
| 377 |
|
| 378 |
@classmethod |
| 379 |
def prelude( cls ): |
| 380 |
tmpl_prelude = '// Generated by https://github.com/juliettef/IconFontCppHeaders script GenerateIconFontCppHeaders.py for language {lang}\n' + \ |
| 381 |
'// from {font_data}\n' + \ |
| 382 |
'// for use with {font_ttf}\n' + \ |
| 383 |
'#pragma once\n\n' |
| 384 |
result = tmpl_prelude.format(lang = cls.language_name, |
| 385 |
font_data = cls.intermediate.get( 'font_data' ), |
| 386 |
font_ttf = cls.intermediate.get( 'font_ttf' )) |
| 387 |
tmpl_prelude_define_file_name = '#define FONT_ICON_FILE_NAME_{font_abbr} "{file_name_ttf}"\n' |
| 388 |
file_names_ttf = cls.intermediate.get( 'font_file_name_ttf' ) |
| 389 |
for file_name_ttf in file_names_ttf: |
| 390 |
result += tmpl_prelude_define_file_name.format( font_abbr = file_name_ttf[ 0 ], file_name_ttf = file_name_ttf[ 1 ]) |
| 391 |
return result + '\n' |
| 392 |
|
| 393 |
@classmethod |
| 394 |
def lines_minmax( cls ): |
| 395 |
tmpl_line_minmax = '#define ICON_{minmax}_{abbr} 0x{val}\n' |
| 396 |
result = tmpl_line_minmax.format( minmax = 'MIN', |
| 397 |
abbr = cls.intermediate.get( 'font_minmax_abbr' ) if cls.intermediate.get( 'font_minmax_abbr' ) else cls.intermediate.get('font_abbr'), |
| 398 |
val = cls.intermediate.get( 'font_min' )) + \ |
| 399 |
tmpl_line_minmax.format( minmax = 'MAX', |
| 400 |
abbr = cls.intermediate.get( 'font_minmax_abbr' ) if cls.intermediate.get( 'font_minmax_abbr' ) else cls.intermediate.get('font_abbr'), |
| 401 |
val = cls.intermediate.get( 'font_max' )) |
| 402 |
return result |
| 403 |
|
| 404 |
@classmethod |
| 405 |
def line_icon( cls, icon ): |
| 406 |
tmpl_line_icon = '#define ICON_{abbr}_{icon} "{code}"\n' |
| 407 |
icon_name = str.upper( icon[ 0 ]).replace( '-', '_' ) |
| 408 |
icon_code = repr( chr( int( icon[ 1 ], 16 )).encode( 'utf-8' ))[ 2:-1 ] |
| 409 |
result = tmpl_line_icon.format( abbr = cls.intermediate.get( 'font_abbr' ), |
| 410 |
icon = icon_name, |
| 411 |
code = icon_code ) |
| 412 |
return result |
| 413 |
|
| 414 |
|
| 415 |
class LanguageCpp11( LanguageC89 ): |
| 416 |
language_name = 'C++11' |
| 417 |
file_name = 'Icons{name}.h' |
| 418 |
|
| 419 |
@classmethod |
| 420 |
def line_icon( cls, icon ): |
| 421 |
tmpl_line_icon = '#define ICON_{abbr}_{icon} u8"\\u{code}"\n' |
| 422 |
icon_name = str.upper( icon[ 0 ]).replace( '-', '_' ) |
| 423 |
icon_code = icon[ 1 ] |
| 424 |
result = tmpl_line_icon.format( abbr = cls.intermediate.get( 'font_abbr' ), |
| 425 |
icon = icon_name, |
| 426 |
code = icon_code) |
| 427 |
return result |
| 428 |
|
| 429 |
|
| 430 |
class LanguageCSharp( Language ): |
| 431 |
language_name = "C#" |
| 432 |
file_name = 'Icons{name}.cs' |
| 433 |
|
| 434 |
@classmethod |
| 435 |
def prelude( cls ): |
| 436 |
tmpl_prelude = '// Generated by https://github.com/juliettef/IconFontCppHeaders script GenerateIconFontCppHeaders.py for language {lang}\n' + \ |
| 437 |
'// from {font_data}\n' + \ |
| 438 |
'// for use with {font_ttf}\n' + \ |
| 439 |
'namespace IconFonts\n' + \ |
| 440 |
'{{\n' + \ |
| 441 |
' public class {font_name}\n' + \ |
| 442 |
' {{\n' |
| 443 |
|
| 444 |
result = tmpl_prelude.format(lang = cls.language_name, |
| 445 |
font_data = cls.intermediate.get( 'font_data' ), |
| 446 |
font_ttf = cls.intermediate.get( 'font_ttf' ), |
| 447 |
font_name = cls.intermediate.get( 'font_name' ).replace( ' ', '' ) |
| 448 |
) |
| 449 |
tmpl_prelude_define_file_name = ' public const string FontIconFileName = "{file_name_ttf}";\n' |
| 450 |
file_names_ttf = cls.intermediate.get( 'font_file_name_ttf' ) |
| 451 |
for file_name_ttf in file_names_ttf: |
| 452 |
result += tmpl_prelude_define_file_name.format( file_name_ttf = file_name_ttf[ 1 ]) |
| 453 |
return result + '\n' |
| 454 |
|
| 455 |
@classmethod |
| 456 |
def epilogue( cls ): |
| 457 |
return ' }\n' + \ |
| 458 |
'}\n' |
| 459 |
|
| 460 |
@classmethod |
| 461 |
def lines_minmax( cls ): |
| 462 |
tmpl_line_minmax = ' public const int Icon{minmax} = 0x{val};\n' |
| 463 |
result = tmpl_line_minmax.format(minmax = 'Min', |
| 464 |
val = cls.intermediate.get( 'font_min' )) + \ |
| 465 |
tmpl_line_minmax.format(minmax = 'Max', |
| 466 |
val = cls.intermediate.get( 'font_max' )) |
| 467 |
return result |
| 468 |
|
| 469 |
@classmethod |
| 470 |
def line_icon( cls, icon ): |
| 471 |
|
| 472 |
tmpl_line_icon = ' public const string {icon} = "\\u{code}";\n' |
| 473 |
icon_name = cls.to_camelcase(icon[ 0 ]) |
| 474 |
icon_code = icon[ 1 ] |
| 475 |
|
| 476 |
if icon_name[ 0 ].isdigit(): |
| 477 |
# Variable may not start with a digit |
| 478 |
icon_name = 'The' + icon_name |
| 479 |
|
| 480 |
if icon_name == cls.intermediate.get( 'font_name' ).replace( ' ', '' ): |
| 481 |
# Member may not have same name as enclosing class |
| 482 |
icon_name += 'Icon' |
| 483 |
|
| 484 |
result = tmpl_line_icon.format( icon = icon_name, |
| 485 |
code = icon_code) |
| 486 |
return result |
| 487 |
|
| 488 |
@classmethod |
| 489 |
def to_camelcase( cls, text ): |
| 490 |
parts = text.split( '-' ) |
| 491 |
for i in range( len( parts ) ): |
| 492 |
p = parts[i] |
| 493 |
parts[ i ] = p[ 0 ].upper() + p[ 1: ].lower() |
| 494 |
return ''.join( parts ) |
| 495 |
|
| 496 |
|
| 497 |
# Main |
| 498 |
fonts = [ FontFA4, FontFA5, FontFA5Brands, FontFA5Pro, FontFA5ProBrands, FontFK, FontMD, FontKI, FontFAD ] |
| 499 |
languages = [ LanguageC89, LanguageCpp11, LanguageCSharp ] |
| 500 |
|
| 501 |
intermediates = [] |
| 502 |
for font in fonts: |
| 503 |
try: |
| 504 |
font_intermediate = font.get_intermediate_representation() |
| 505 |
if font_intermediate: |
| 506 |
intermediates.append( font_intermediate ) |
| 507 |
except Exception as e: |
| 508 |
print( '[ ERROR: {!s} ]'.format( e )) |
| 509 |
if intermediates: |
| 510 |
for interm in intermediates: |
| 511 |
Language.intermediate = interm |
| 512 |
for lang in languages: |
| 513 |
if lang: |
| 514 |
lang.save_to_file() |