#!/usr/bin/python import sys import xml.dom.minidom import getopt import re import os import string #define help output def usage(): print '\nUsage: sugar-iconify.py [options] input.svg\n' print 'Options:\n' print '\t -c\t\tApply default color entities (#666666, #ffffff) to output' print '\t -d directory\tThe preferred output directory' print '\t -e\t\tDo not insert entities for strokes and fills' print '\t -f hex\t\tHex value to replace with fill entity' print '\t -g\t\tAutomatically accept guesses for stroke and fill entities' print '\t -h\t\tDisplay this help message' print '\t -i\t\tInsert "isolated stroke" entities' print '\t -m\t\tMultiple export; export top level groups as separate icons' print '\t -o\t\tOverwrite the input file; overridden by -m' print '\t -p pattern\tOnly export icons whose name matches pattern; for use with -m' print '\t -s hex\t\tHex value to replace with stroke entity' print '\t -x\t\tOutput example SVGs, for previewing their appearance in Sugar; ignored with -m' print '\t -v\t\tverbose' #check for valid arguments try: opts,arg = getopt.getopt(sys.argv[1:], "s:f:gcd:imp:oehvx") except: usage() sys.exit(2) if len(arg) < 1: usage() sys.exit(2) #declare variables and constants default_stroke_color = '#666666' default_fill_color = '#ffffff' transparent_color = '#00000000' stroke_color = default_stroke_color fill_color = default_fill_color stroke_entity = "stroke_color" fill_entity = "fill_color" iso_stroke_entity = "iso_stroke_color" output_path = '' pattern = '' entities_passed = 0 use_default_colors = False confirm_guess = True use_entities = True multiple = False verbose = False overwrite_input = False output_examples = False use_iso_strokes = False #interpret arguments for o, a in opts: if o == '-s': stroke_color = '#' + a.lstrip('#').lower() entities_passed += 1 elif o == '-f': fill_color = '#' + a.lstrip('#').lower() entities_passed += 1 elif o == '-g': confirm_guess = False elif o == '-c': use_default_colors = True elif o == '-o': overwrite_input = True elif o == '-d': output_path = a.rstrip('/') + '/' elif o == '-e': use_entities = False elif o == '-v': verbose = True elif o == '-p': pattern = a elif o == '-h': usage() sys.exit(2) elif o == '-m': multiple = True elif o == '-x': output_examples = True elif o == '-i': use_iso_strokes = True #isolate important parts of the input path svgfilepath = arg[0].rstrip('/') svgdirpath, sep, svgfilename = svgfilepath.rpartition('/') svgbasename = re.sub(r'(.*)\.([^.]+)', r'\1', svgfilename) #load the SVG as text try: svgfile = open(svgfilepath, 'r') except: sys.exit('Error: Could not locate ' + svgfilepath) try: svgtext = svgfile.read() svgfile.close() except: svgfile.close() sys.exit('Error: Could not read ' + svgfilepath) #determine the creator of the SVG (we only care about Inkscape and Illustrator) creator = 'unknown' if re.search('illustrator', svgtext, re.I): creator = 'illustrator' elif re.search('inkscape', svgtext, re.I): creator = 'inkscape' if verbose: print 'The creator of this svg is ' + creator + '.' #hack the entities into the readonly DTD if use_entities: #before replacing them, we read the stroke/fill values out, should they have previously been defined, #to prevent needing to make guesses for them later stroke_match = re.search(r'stroke_color\s*\"([^"]*)\"', svgtext) fill_match = re.search(r'fill_color\s*\"([^"]*)\"', svgtext) if stroke_match is not None: stroke_color = stroke_match.group(1).lower() entities_passed += 1 if fill_match is not None: fill_color = fill_match.group(1).lower() entities_passed += 1 #define the entities if fill_match and stroke_match: entities = '\t\n' entities += '\t\n' if use_iso_strokes: entities += '\t\n' else: entities = '\t\n' entities += '\t\n' if use_iso_strokes: entities += '\t\n' #for simplicity, we simply replace the entire entity declaration block; this obviously would remove #any other custom entities declared within the SVG, but we assume that's an extreme edge case svgtext, n = re.subn(r'(\[]*)(\[[^\]]*\])*\>', r'\1 \n[\n' + entities + ']>\n', svgtext) #add a doctype if none already exists, adding the appropriate entities as well if n == 0: svgtext,n = re.subn("\n\n\ \n\ \n\ \t\n\ \tSugar Icon Preview: ~~~\n\ \t\n\ \t\n\ \n\ \n\ \t\t\n\ \t\t
\n\ \t\t\t

Icon Validation

\n\ \t\t\t
    \n\ \t\t\t\t
  • Ensure that your icons appear to be centered within their boxes.\n\ \t\t\t\t\tIf they appear off-center or cropped, you may have created your icon on canvas other than the required 55px square.\n\ \t\t\t\t\tClick to toggle the 55px canvas border.\n\ \t\t\t\t
  • If your icon appears off-center but has the correct 55px canvas, it may simply have uneven visual balance.\n\ \t\t\t\t\tThis means, though it may be technically centered, differences in the distribution of \"mass\" cause it to appear otherwise. \n\ \t\t\t\t\tTry shifting the icon slightly on your canvas, while ensuring that you don't accidentally exceed the 55px boundary.\n\ \t\t\t\t\n\ \t\t\t\t
  • Ensure that the first two icons appear entirely in gray, and that all of the third icon is colored blue and green, with the latter being the fill color.\n\ \t\t\t\t\tIf any fail to meet these requirements, your icon does not have proper stroke and/or fill entities defined.\n\ \t\t\t\t\tInvestigate the -s and -f options of sugar-iconify, and be sure that your input SVG doesn't have extra colors in its palette.\n\ \t\t\t\t
  • Ensure that your icon reads clearly when viewed only as strokes.\n\ \t\t\t\t\tThis visual style will be used to represent activities/objects which are inactive, or uninstantiated.\n\ \t\t\t\t\tConsider applying outlining strokes to any filled shapes that do not already have them.\n\ \t\t\t\t
  • Ensure that your icon reads clearly when viewed only as fills.\n\ \t\t\t\t\tThis visual style will be used for representing activity types within other icons, such as invitations, transfers, and objects. \n\ \t\t\t\t\tIf you have strokes which are isolated from fills, neither outlining them nor sitting against a filled background, please \n\ \t\t\t\t\tinvestigate the -i option of the sugar-iconify script.\n\ \t\t\t
\n\ \t\t\tFor more information, please see the OLPC wiki page on making sugar icons.\n\ \t\t
\n\ \n\ \n\ " #finally, do the icon conversion and export if multiple: #export each icon as a separate file by top level group n_icons_exported = 0 n_warnings = 0 for icon in icons: try: #skip whitespace and unnamed icons if icon.localName == 'g' and icon.attributes: icon_name = '' try: if creator == 'inkscape' and icon.attributes.getNamedItem('inkscape:label'): icon_name = icon.attributes.getNamedItem('inkscape:label').nodeValue else: icon_name = icon.attributes.getNamedItem('id').nodeValue except: pass #skip the template layers if not icon_name.startswith('_'): #skip non-matches if pattern == '' or re.search(pattern, icon_name): if verbose: print '\nExporting ' + icon_name + '.svg...' icon_xml = xml.dom.minidom.Document(); #construct the SVG icon_xml.appendChild(doctype) icon_xml.appendChild(svg.cloneNode(0)) icon_xml.childNodes[1].appendChild(icon) icon_xml.childNodes[1].childNodes[0].setAttribute('display', 'block') if use_entities: strokes_replaced, fills_replaced = replaceEntities(icon_xml.childNodes[1]) if not strokes_replaced and not fills_replaced: print 'Warning: no entity replacements were made in %s' % icon_name elif not strokes_replaced: print 'Warning: no stroke entity replacements were made in %s' % icon_name elif not fills_replaced: print 'Warning: no fill entity replacements were made in %s' % icon_name if not strokes_replaced or not fills_replaced: n_warnings += 1 #write the file try: f = open(output_path + icon_name + '.svg', 'w') except: sys.exit('Error: Could not locate directory ' + output_path) try: #had to hack here to remove the automatic encoding of '&' by toxml() in entity refs #I'm sure there is a way to prevent need for this if I knew the XML DOM better icon_svgtext = icon_xml.toxml() icon_svgtext = re.sub('&', '&', icon_svgtext) if not use_default_colors: icon_svgtext = re.sub(r'ENTITY stroke_color "[^"]*"', r'ENTITY stroke_color "' + stroke_color + '"', icon_svgtext) icon_svgtext = re.sub(r'ENTITY fill_color "[^"]*"', r'ENTITY fill_color "' + fill_color + '"', icon_svgtext) f.write(icon_svgtext) f.close() except: sys.exit('Error: Could not write file ' + icon_name + '.svg') n_icons_exported += 1 except: #catch any errors we may have missed, so the rest of the icons can export normally if(icon_name): print 'Error: Could not export' + icon_name + '.svg' if verbose: if n_icons_exported == 1: print 'Successfully exported 1 icon' else: print 'Successfully exported %d icons' % n_icons_exported if n_warnings == 1: print 'Warnings were reported for 1 icon' elif n_warnings > 1: print 'Warnings were reported for %d icons' % n_warnings else: #output a single converted icon if not overwrite_input: outfilename = re.sub(r'(.*\.)([^.]+)', r'\1sugar.\2', svgfilename) if verbose: print 'Exporting ' + outfilename + ' ...' else: outfilename = svgfilename if verbose: print 'Overwriting ' + outfilename + ' ...' #remove the template layers for node in svg.childNodes: #only check named nodes if node.localName == 'g' and node.attributes: try: if creator == 'inkscape' and node.attributes.getNamedItem('inkscape:label'): node_name = node.attributes.getNamedItem('inkscape:label').nodeValue else: node_name = node.attributes.getNamedItem('id').nodeValue if node_name.startswith('_'): node.parentNode.removeChild(node) except: pass if use_entities: strokes_replaced, fills_replaced = replaceEntities(svgxml) if not strokes_replaced and not fills_replaced: print 'Warning: no entity replacements were made' elif not strokes_replaced: print 'Warning: no stroke entity replacements were made' elif not fills_replaced: print 'Warning: no fill entity replacements were made' if use_iso_strokes: strokes_fixed = fix_isolated_strokes(svgxml) if strokes_fixed > 0 and verbose: print "%d isolated strokes fixed" % strokes_fixed #create the output file(s) if output_examples: example_path = output_path + re.sub(r'(.*\.)([^.]+)', r'\1preview', svgfilename) + '/' try: os.mkdir(example_path) except: pass try: f = open(example_path + 'preview.html', 'w') except: print "Error: could not create HTML preview file" try: f.write(re.sub(r'~~~', svgbasename, previewHTML)) f.close() except: sys.exit('Error: could not write to HTML preview file') example_colors = [(default_stroke_color, "#FFFFFF", default_stroke_color), ("#FFFFFF", default_stroke_color, default_stroke_color), ("#0000AA", "#00DD00", "#0000AA")] example_filenames = [re.sub(r'(.*\.)([^.]+)', r'\1stroke.\2', svgfilename), re.sub(r'(.*\.)([^.]+)', r'\1fill.\2', svgfilename), re.sub(r'(.*\.)([^.]+)', r'\1both.\2', svgfilename) ] icon_svgtext = svgxml.toxml() icon_svgtext = re.sub('&', '&', icon_svgtext) for i in range(0, len(example_filenames)): try: f = open(example_path + example_filenames[i], 'w') except: sys.exit('Error: Could not save to ' + example_path + example_filenames[i]) try: icon_svgtext = re.sub(r'ENTITY stroke_color "[^"]*"', r'ENTITY stroke_color "' + example_colors[i][0] + '"', icon_svgtext) icon_svgtext = re.sub(r'ENTITY fill_color "[^"]*"', r'ENTITY fill_color "' + example_colors[i][1] + '"', icon_svgtext) if use_iso_strokes: icon_svgtext = re.sub(r'ENTITY iso_stroke_color "[^"]*"', r'ENTITY iso_stroke_color "' + example_colors[i][2] + '"', icon_svgtext) f.write(icon_svgtext) f.close() except: sys.exit('Error: Could not write file ' + output_path + example_filenames[i]) try: f = open(output_path + outfilename, 'w') except: sys.exit('Error: Could not save to ' + output_path + outfilename) try: icon_svgtext = svgxml.toxml() icon_svgtext = re.sub('&', '&', icon_svgtext) if not use_default_colors: icon_svgtext = re.sub(r'ENTITY stroke_color "[^"]*"', r'ENTITY stroke_color "' + stroke_color + '"', icon_svgtext) icon_svgtext = re.sub(r'ENTITY fill_color "[^"]*"', r'ENTITY fill_color "' + fill_color + '"', icon_svgtext) if use_iso_strokes: icon_svgtext = re.sub(r'ENTITY iso_stroke_color "[^"]*"', r'ENTITY iso_stroke_color "' + stroke_color + '"', icon_svgtext) f.write(icon_svgtext) f.close() except: sys.exit('Error: Could not write file ' + output_path + outfilename)