diff options
Diffstat (limited to '3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py')
-rw-r--r-- | 3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py | 428 |
1 files changed, 298 insertions, 130 deletions
diff --git a/3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py b/3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py index 09eae5c4566..fe7fe8e95e2 100644 --- a/3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py +++ b/3rdparty/bgfx/3rdparty/iconfontheaders/GenerateIconFontCppHeaders.py @@ -1,13 +1,18 @@ -#!/usr/bin/python # Convert Font Awesome, Google Material Design and Kenney Game icon font -# parameters to C++11 and C89 compatible formats. +# parameters to C++11, C89 and None compatible formats. # #------------------------------------------------------------------------------ # 1 - Source material # -# 1.1 - Font Awesome - https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml -# 1.2 - Material Design - https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints -# 1.3 - Kenney icons - https://raw.githubusercontent.com/SamBrishes/kenney-icon-font/master/css/kenney-icons.css +# 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 @@ -24,21 +29,26 @@ # - 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: #define ICON_MIN_FA 0xf000 -# #define ICON_MAX_FA 0xf295 +# - 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 @@ -48,136 +58,294 @@ # 3.3 - PyYAML - http://pyyaml.org/ # #------------------------------------------------------------------------------ +# 4 - References +# +# None language: https://bitbucket.org/duangle/nonelang/src +# +#------------------------------------------------------------------------------ import requests import yaml -LINE_FORMAT_MINMAX = '#define ICON_{!s}_{!s} 0x{!s}\n' - -UNICODE_MIN = 'ffff' -UNICODE_MAX = '0' -TIMEOUT = 2 - -MESSAGE_SUCCESS = '{!s} fonts - conversion success: {!s}' -MESSAGE_ERROR = '{!s} fonts - error \n\t{!s}' - - -def get_prelude( url ): - prelude = '// Generated by GenerateIconFontCppHeaders.py \n// from {!s}\n#pragma once\n\n'.format( url ) - return prelude - - -def line_format( font_abbr, font, unicode, cpp11 = True ): - if cpp11: - result = '#define ICON_{!s}_{!s} u8"\u{!s}"\n'.format( font_abbr, font, unicode ) - else: - unicode_base = ''.join([ '{0:x}'.format( ord( x )) for x in unichr( int( unicode, 16 )).encode( 'utf-8' )]).upper() - unicode = '\\x' + unicode_base[ :2 ] + '\\x' + unicode_base[ 2:4 ] + '\\x' + unicode_base[ 4: ] - result = '#define ICON_{!s}_{!s} "{!s}"\n'.format( font_abbr, font, unicode ) - return result - - -def convert_font_awesome( font_name, font_abbr, source_url, output_file, cpp11 ): - try: - response = requests.get( source_url, timeout = TIMEOUT ) - if response.status_code == 200: - input = yaml.safe_load( response.content ) - min = UNICODE_MIN - max = UNICODE_MAX - output_fonts = '' - for item in input[ 'icons' ]: - font = '' - for char in item[ 'id' ]: - font += '_' if ( char == '-' ) else str.upper( char ) - unicode = item[ 'unicode' ] - if unicode < min: - min = unicode - elif unicode >= max: - max = unicode - output_fonts += line_format( font_abbr, font, unicode, cpp11 ) - output = get_prelude( source_url ) + \ - LINE_FORMAT_MINMAX.format( 'MIN', font_abbr, min ) + \ - LINE_FORMAT_MINMAX.format( 'MAX', font_abbr, max ) + \ - output_fonts - with open( output_file, 'w' ) as f: - f.write( output ) - print( MESSAGE_SUCCESS.format( font_name, output_file )) - except Exception as e: - print( MESSAGE_ERROR.format( font_name, e )) - - -def convert_material_design( font_name, font_abbr, source_url, output_file, cpp11 ): - try: - response = requests.get( source_url, timeout = TIMEOUT ) - if response.status_code == 200: - input = str.split( response.content, '\n' ) - min = UNICODE_MIN - max = UNICODE_MAX - output_fonts = '' - for line in input: - words = str.split( line ) - if words: - font = '' - for char in words[ 0 ]: - font += '_' if ( char == '-' ) else str.upper( char ) - unicode = words[ 1 ] - if unicode < min: - min = unicode - elif unicode >= max: - max = unicode - output_fonts += line_format( font_abbr, font, unicode, cpp11 ) - output = get_prelude( source_url ) + \ - LINE_FORMAT_MINMAX.format( 'MIN', font_abbr, min ) + \ - LINE_FORMAT_MINMAX.format( 'MAX', font_abbr, max ) + \ - output_fonts - with open( output_file, 'w' ) as f: - f.write( output ) - print( MESSAGE_SUCCESS.format( font_name, output_file )) - except Exception as e: - print( MESSAGE_ERROR.format( font_name, e )) - - -def convert_kenney( font_name, font_abbr, source_url, output_file, cpp11 ): - try: - response = requests.get( source_url, timeout = TIMEOUT ) - if response.status_code == 200: - input = str.split( response.content, '\n' ) - min = UNICODE_MIN - max = UNICODE_MAX - output_fonts = '' - font_begin= '.ki-' - font_end = ':before' - unicode_begin = '"\\' - unicode_end = '";' - for line in input: - words = str.split( line ) - if words: - if font_begin in words[ 0 ]: - font = '' - word = words[ 0 ][( words[ 0 ].find( font_begin ) + len( font_begin )) : ( words[ 0 ].find( font_end ))] - for char in word: - font += '_' if ( char == '-' ) else str.upper( char ) - unicode = str( words[ 2 ][( words[ 2 ].find( unicode_begin ) + len( unicode_begin )) : words[ 2 ].find( unicode_end )]) - if unicode < min: - min = unicode - elif unicode >= max: - max = unicode - output_fonts += line_format( font_abbr, font, unicode, cpp11 ) - output = get_prelude( source_url ) + \ - LINE_FORMAT_MINMAX.format( 'MIN', font_abbr, min ) + \ - LINE_FORMAT_MINMAX.format( 'MAX', font_abbr, max ) + \ - output_fonts - with open( output_file, 'w' ) as f: - f.write( output ) - print( MESSAGE_SUCCESS.format( font_name, output_file )) - except Exception as e: - print( MESSAGE_ERROR.format( font_name, e )) +# 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 -convert_font_awesome( 'Font Awesome', 'FA', 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml', 'icons_font_awesome.h', False ) -convert_material_design( 'Material Design', 'MD', 'https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints', 'icons_material_design.h', False ) -convert_kenney( 'Kenney', 'KI', 'https://raw.githubusercontent.com/SamBrishes/kenney-icon-font/master/css/kenney-icons.css', 'icons_kenney.h', False ) +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() |