26 from xml.dom
import minidom
29 rsvgPath =
"./rsvg-convert" 30 artworkLayerPrefix =
"artwork:" 57 def createDirectory (name):
59 if os.path.isfile (name):
61 if not os.path.exists (name):
64 print 'WARNING: createDirectory("%s") failed. Permission problems?' % name
67 def copyMergeDirectory (src, dst):
68 listing = os.listdir (src)
69 for file_name
in listing:
70 src_file_path = os.path.join (src, file_name)
71 dst_file_path = os.path.join (dst, file_name)
72 shutil.copyfile (src_file_path, dst_file_path)
74 def getDocumentSize (svg_element):
75 width = float(svg_element.getAttribute(
"width"))
76 height = float(svg_element.getAttribute(
"height"))
77 return [width, height]
79 def findChildLayerElement (parent_element):
80 for node
in parent_element.childNodes:
81 if node.nodeType == minidom.Node.ELEMENT_NODE:
82 if node.tagName ==
"g":
83 if node.getAttribute(
"inkscape:groupmode") ==
"layer":
87 def parsePlateLayer (layer):
89 for node
in layer.childNodes:
90 if node.nodeType == minidom.Node.ELEMENT_NODE:
91 if node.tagName ==
"rect":
92 x = float(node.getAttribute(
"x"))
93 y = float(node.getAttribute(
"y"))
94 width = float(node.getAttribute(
"width"))
95 height = float(node.getAttribute(
"height"))
96 rectangles.append([x, y, width, height])
100 def parseSVG (file_path):
101 print "Parsing " + file_path
102 svgdoc = minidom.parse (file_path)
103 for root_node
in svgdoc.childNodes:
104 if root_node.nodeType == minidom.Node.ELEMENT_NODE:
105 if root_node.tagName ==
"svg":
106 size = getDocumentSize (root_node)
107 layer = findChildLayerElement (root_node)
109 layer_name = layer.getAttribute (
"inkscape:label")
110 if layer_name[:len(artworkLayerPrefix)] == artworkLayerPrefix:
111 artwork_name = layer_name[len(artworkLayerPrefix):]
112 plate = findChildLayerElement(layer)
114 return artwork_name, size, parsePlateLayer(plate)
118 def renderSvgRsvg (file_path, out_dir, artwork_name, rectangle, _doc_size):
120 width = int(rectangle[2])
121 height = int(rectangle[3])
123 if not os.path.exists(rsvgPath):
124 print "Error: executable %s not found." % rsvgPath
126 os.spawnlp(os.P_WAIT, rsvgPath, rsvgPath,
127 "--source-rect=%g:%g:%g:%g" % (rectangle[0], rectangle[1], width, height),
128 "--output=" + os.path.join(out_dir,
"%gx%g/%s.png" % (width, height, artwork_name)),
131 def renderSvgIcon (file_path, out_dir):
132 artwork_name, doc_size, rectangles = parseSVG (file_path)
133 for rectangle
in rectangles:
134 renderSvgRsvg(file_path, out_dir, artwork_name, rectangle, doc_size)
136 def getTargetNames (file_path):
137 """get a list of target names to be rendered from the given source SVG 138 usable to setup the build targets for SCons 140 artwork_name, _ , rectangles = parseSVG (file_path)
141 return [
"%gx%g/%s.png" % (rectangle[2], rectangle[3], artwork_name)
for rectangle
in rectangles ]
145 print "render-icon.py SRCFILE.svg TARGETDIR" 146 print "An icon rendering utility script for lumiera" 148 def parseArguments(argv):
149 _optlist, args = getopt.getopt(argv,
"")
152 return args[0], args[1]
159 in_path, out_dir = parseArguments(argv)
161 if not (in_path
and out_dir):
162 print "Missing arguments in_path and out_dir." 165 if os.path.isfile(out_dir):
166 print "Unable to use '%s' as output directory, because it\'s a file." % out_dir
168 if not os.path.isdir(out_dir):
169 print "Output directory '%s' not found." % out_dir
173 createDirectory(os.path.join(out_dir,
"48x48"))
174 createDirectory(os.path.join(out_dir,
"32x32"))
175 createDirectory(os.path.join(out_dir,
"24x24"))
176 createDirectory(os.path.join(out_dir,
"22x22"))
177 createDirectory(os.path.join(out_dir,
"16x16"))
179 renderSvgIcon (in_path, out_dir)
184 if __name__==
"__main__":
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.