19 from xml.dom
import minidom
22 rsvgPath =
"./rsvg-convert" 23 artworkLayerPrefix =
"artwork:" 50 def createDirectory (name):
52 if os.path.isfile (name):
54 if not os.path.exists (name):
57 print 'WARNING: createDirectory("%s") failed. Permission problems?' % name
60 def copyMergeDirectory (src, dst):
61 listing = os.listdir (src)
62 for file_name
in listing:
63 src_file_path = os.path.join (src, file_name)
64 dst_file_path = os.path.join (dst, file_name)
65 shutil.copyfile (src_file_path, dst_file_path)
67 def getDocumentSize (svg_element):
68 width = float(svg_element.getAttribute(
"width"))
69 height = float(svg_element.getAttribute(
"height"))
70 return [width, height]
72 def findChildLayerElement (parent_element):
73 for node
in parent_element.childNodes:
74 if node.nodeType == minidom.Node.ELEMENT_NODE:
75 if node.tagName ==
"g":
76 if node.getAttribute(
"inkscape:groupmode") ==
"layer":
80 def parsePlateLayer (layer):
82 for node
in layer.childNodes:
83 if node.nodeType == minidom.Node.ELEMENT_NODE:
84 if node.tagName ==
"rect":
85 x = float(node.getAttribute(
"x"))
86 y = float(node.getAttribute(
"y"))
87 width = float(node.getAttribute(
"width"))
88 height = float(node.getAttribute(
"height"))
89 rectangles.append([x, y, width, height])
93 def parseSVG (file_path):
94 print "Parsing " + file_path
95 svgdoc = minidom.parse (file_path)
96 for root_node
in svgdoc.childNodes:
97 if root_node.nodeType == minidom.Node.ELEMENT_NODE:
98 if root_node.tagName ==
"svg":
99 size = getDocumentSize (root_node)
100 layer = findChildLayerElement (root_node)
102 layer_name = layer.getAttribute (
"inkscape:label")
103 if layer_name[:len(artworkLayerPrefix)] == artworkLayerPrefix:
104 artwork_name = layer_name[len(artworkLayerPrefix):]
105 plate = findChildLayerElement(layer)
107 return artwork_name, size, parsePlateLayer(plate)
111 def renderSvgRsvg (file_path, out_dir, artwork_name, rectangle, _doc_size):
113 width = int(rectangle[2])
114 height = int(rectangle[3])
116 if not os.path.exists(rsvgPath):
117 print "Error: executable %s not found." % rsvgPath
119 os.spawnlp(os.P_WAIT, rsvgPath, rsvgPath,
120 "--source-rect=%g:%g:%g:%g" % (rectangle[0], rectangle[1], width, height),
121 "--output=" + os.path.join(out_dir,
"%gx%g/%s.png" % (width, height, artwork_name)),
124 def renderSvgIcon (file_path, out_dir):
125 artwork_name, doc_size, rectangles = parseSVG (file_path)
126 for rectangle
in rectangles:
127 renderSvgRsvg(file_path, out_dir, artwork_name, rectangle, doc_size)
129 def getTargetNames (file_path):
130 """get a list of target names to be rendered from the given source SVG 131 usable to setup the build targets for SCons 133 artwork_name, _ , rectangles = parseSVG (file_path)
134 return [
"%gx%g/%s.png" % (rectangle[2], rectangle[3], artwork_name)
for rectangle
in rectangles ]
138 print "render-icon.py SRCFILE.svg TARGETDIR" 139 print "An icon rendering utility script for lumiera" 141 def parseArguments(argv):
142 _optlist, args = getopt.getopt(argv,
"")
145 return args[0], args[1]
152 in_path, out_dir = parseArguments(argv)
154 if not (in_path
and out_dir):
155 print "Missing arguments in_path and out_dir." 158 if os.path.isfile(out_dir):
159 print "Unable to use '%s' as output directory, because it\'s a file." % out_dir
161 if not os.path.isdir(out_dir):
162 print "Output directory '%s' not found." % out_dir
166 createDirectory(os.path.join(out_dir,
"48x48"))
167 createDirectory(os.path.join(out_dir,
"32x32"))
168 createDirectory(os.path.join(out_dir,
"24x24"))
169 createDirectory(os.path.join(out_dir,
"22x22"))
170 createDirectory(os.path.join(out_dir,
"16x16"))
172 renderSvgIcon (in_path, out_dir)
177 if __name__==
"__main__":
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.