1#!/usr/bin/env ruby 2 3require "rexml/document" 4require "fileutils" 5include REXML 6 7 8INKSCAPE = '/usr/bin/inkscape' 9#INKSCAPE = '/usr/bin/inkscape' # like this works for me, while using `which` inkscape hangs 10SRC = "src/symbolic/gnome-stencils.svg" 11PREFIX = "Adwaita/scalable" 12 13def chopSVG(icon) 14 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir]) 15 unless (File.exists?(icon[:file]) && !icon[:forcerender]) 16 FileUtils.cp(SRC,icon[:file]) 17 puts " >> #{icon[:name]}" 18 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers " 19 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum " 20 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1" 21 system(cmd) 22 #saving as plain SVG gets rid of the classes :/ 23 #cmd = "#{INKSCAPE} -f #{icon[:file]} -z --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1" 24 #system(cmd) 25 svgcrop = Document.new(File.new(icon[:file], 'r')) 26 svgcrop.root.each_element("//rect") do |rect| 27 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 16 vs 15.99999 28 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs 29 if w == 16 && h == 16 30 rect.remove 31 end 32 end 33 icon_f = File.new(icon[:file],'w+') 34 icon_f.puts svgcrop 35 icon_f.close 36 else 37 puts " -- #{icon[:name]} already exists" 38 end 39end #end of function 40 41def get_output_filename(d,n) 42 if (/rtl$/.match(n)) 43 outfile = "#{d}/#{n.chomp('-rtl')}-symbolic-rtl.svg" 44 else 45 outfile = "#{d}/#{n}-symbolic.svg" 46 end 47 return outfile 48end 49 50#main 51# Open SVG file. 52svg = Document.new(File.new(SRC, 'r')) 53 54if (ARGV[0].nil?) #render all SVGs 55 puts "Rendering from icons in #{SRC}" 56 # Go through every layer. 57 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context| 58 context_name = context.attributes.get_attribute("inkscape:label").value 59 puts "Going through layer '" + context_name + "'" 60 context.each_element("g") do |icon| 61 #puts "DEBUG #{icon.attributes.get_attribute('id')}" 62 dir = "#{PREFIX}/#{context_name}" 63 icon_name = icon.attributes.get_attribute("inkscape:label").value 64 chopSVG({ :name => icon_name, 65 :id => icon.attributes.get_attribute("id"), 66 :dir => dir, 67 :file => get_output_filename(dir, icon_name)}) 68 end 69 end 70 puts "\nrendered all SVGs" 71else #only render the icons passed 72 icons = ARGV 73 ARGV.each do |icon_name| 74 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"] 75 dir = "#{PREFIX}/#{icon.parent.attributes['inkscape:label']}" 76 chopSVG({ :name => icon_name, 77 :id => icon.attributes["id"], 78 :dir => dir, 79 :file => get_output_filename(dir, icon_name), 80 :forcerender => true}) 81 end 82 puts "\nrendered #{ARGV.length} icons" 83end 84