some primitive instructions
[gnome-osd-icons.git] / render.rb
blobcace1dfa997b17c37d00dfae545ade19f7da359f
1 #!/usr/bin/env ruby
3 require "rexml/document"
4 require "fileutils"
5 include REXML
7 INKSCAPE = '/usr/bin/inkscape'
8 SRC = "gnome-osd-icons.svg"
9 PREFIX = "gnome-osd-icons/24x24"
11 def chopSVG(icon)
12         FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
13         unless (File.exists?(icon[:file]) && !icon[:forcerender])
14                 FileUtils.cp(SRC,icon[:file]) 
15                 puts " >> #{icon[:name]}"
16                 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection  --verb=EditInvertInAllLayers "
17                 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
18                 cmd += "--verb=FileSave --verb=FileClose > /dev/null 2>&1"
19                 system(cmd)
20                 cmd = "#{INKSCAPE} -f #{icon[:file]} -z --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1"
21                 system(cmd)
22                 system cmd unless (!icon[:dir].match(/app/))
23         else
24                 puts " -- #{icon[:name]} already exists"
25         end
26 end #end of function
29 #main
30 # Open SVG file.
31 svg = Document.new(File.new(SRC, 'r'))
33 if (ARGV[0].nil?) #render all SVGs
34   puts "Rendering from icons in #{SRC}"
35         # Go through every layer.
36         svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context| 
37                 context_name = context.attributes.get_attribute("inkscape:label").value  
38                 puts "Going through layer '" + context_name + "'"
39                 context.each_element("g") do |icon|
40                         #puts "DEBUG #{icon.attributes.get_attribute('id')}"
41                         dir = "#{PREFIX}/#{context_name}"
42                         icon_name = icon.attributes.get_attribute("inkscape:label").value
43                         chopSVG({       :name => icon_name,
44                                                                 :id => icon.attributes.get_attribute("id"),
45                                                                 :dir => dir,
46                                                                 :file => "#{dir}/#{icon_name}.svg"})
47                 end
48         end
49   puts "\nrendered all SVGs"
50 else #only render the icons passed
51   icons = ARGV
52   ARGV.each do |icon_name|
53         icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
54         dir = "#{PREFIX}/#{icon.parent.attributes['inkscape:label']}"
55                 chopSVG({       :name => icon_name,
56                                                         :id => icon.attributes["id"],
57                                                         :dir => dir,
58                                                         :file => "#{dir}/#{icon_name}.svg",
59                                                         :forcerender => true})
60         end
61   puts "\nrendered #{ARGV.length} icons"
62 end