| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
require "rexml/document" |
| 4 |
require "ftools" |
| 5 |
include REXML |
| 6 |
INKSCAPE = '/usr/bin/inkscape' |
| 7 |
SRC = "./src" |
| 8 |
|
| 9 |
def renderit(file,explicit) |
| 10 |
svg = Document.new(File.new("#{SRC}/#{file}", 'r')) |
| 11 |
#puts "DEBUG: #{file}" |
| 12 |
svg.root.each_element("//g[contains(@inkscape:label,'baseplate')]") do |icon| |
| 13 |
if icon.attributes['inkscape:groupmode']=='layer' #only look inside layers, there may be pasted groups |
| 14 |
context = icon.elements["text[@inkscape:label='context']/tspan"].nil? ? 'blank' : icon.elements["text[@inkscape:label='context']/tspan"].text |
| 15 |
icon_name = icon.elements["text[@inkscape:label='icon-name']/tspan"].nil? ? 'blank' : icon.elements["text[@inkscape:label='icon-name']/tspan"].text |
| 16 |
puts "#{file}:#{icon.attributes['inkscape:label']} #{context}/#{icon_name}" |
| 17 |
icon.each_element("rect") do |box| |
| 18 |
dir = "hicolor/#{box.attributes['width']}x#{box.attributes['height']}/#{context}" |
| 19 |
out = "#{dir}/#{icon_name.gsub(/$/,'.png')}" |
| 20 |
cmd = "#{INKSCAPE} -i #{box.attributes['id']} -e #{out} #{SRC}/#{file} > /dev/null 2>&1" |
| 21 |
File.makedirs(dir) unless File.exists?(dir) |
| 22 |
if (!explicit && File.exists?(out)) |
| 23 |
print "-" #skip if PNG exists |
| 24 |
else |
| 25 |
system(cmd) |
| 26 |
print "." |
| 27 |
end |
| 28 |
end |
| 29 |
puts '' |
| 30 |
end |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
if (ARGV[0].nil?) #render all SVGs |
| 35 |
system("mkdir hicolor/") unless File.exists?('hicolor') |
| 36 |
puts "Rendering from SVGs in #{SRC}" |
| 37 |
Dir.foreach(SRC) do |file| |
| 38 |
renderit(file, false) if file.match(/svg$/) |
| 39 |
end |
| 40 |
puts "\nrendered all SVGs" |
| 41 |
else #only render the SVG passed |
| 42 |
file = "#{ARGV[0]}.svg" |
| 43 |
if (File.exists?("#{SRC}/#{file}")) |
| 44 |
renderit(file, true) |
| 45 |
puts "\nrendered #{file}" |
| 46 |
else |
| 47 |
puts "[E] No such file (#{file})" |
| 48 |
end |
| 49 |
end |