Debian Codegraph @

src/vector/extract-vector-icons.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # Legal Stuff:
4 #
5 # This file is part of the Paper Icon Theme and is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free Software
7 # Foundation; version 3.
8 #
9 # This file is part of the Paper Icon Theme and is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>
16 #
17 #
18 # Thanks to the GNOME icon developers for the original version of this script
19
20 require "rexml/document"
21 require "fileutils"
22 include REXML
23
24 INKSCAPE = '/usr/bin/inkscape'
25 SRC16 = "vector/source-16x16.svg"
26 PREFIX16 = "../Paper/16x16"
27
28 def chopSVG(icon)
29 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
30 unless (File.exists?(icon[:file]) && !icon[:forcerender])
31 FileUtils.cp(SRC16,icon[:file])
32 puts " >> #{icon[:name]}"
33 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers "
34 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
35 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
36 system(cmd)
37 #saving as plain SVG gets rid of the classes :/
38 #cmd = "#{INKSCAPE} -f #{icon[:file]} --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1"
39 #system(cmd)
40 svgcrop = Document.new(File.new(icon[:file], 'r'))
41 svgcrop.root.each_element("//rect") do |rect|
42 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 16 vs 15.99999
43 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
44 if w == 16 && h == 16
45 rect.remove
46 end
47 end
48 icon_f = File.new(icon[:file],'w+')
49 icon_f.puts svgcrop
50 icon_f.close
51 else
52 puts " -- #{icon[:name]} already exists"
53 end
54 end #end of function
55
56 def get_output_filename(d,n)
57 if (/rtl$/.match(n))
58 outfile = "#{d}/#{n.chomp('-rtl')}-rtl.svg"
59 else
60 outfile = "#{d}/#{n}.svg"
61 end
62 return outfile
63 end
64
65 #main
66 # Open SVG file.
67 svg = Document.new(File.new(SRC16, 'r'))
68
69 if (ARGV[0].nil?) #render all SVGs
70 puts "Rendering from icons in #{SRC16}"
71 # Go through every layer.
72 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
73 context_name = context.attributes.get_attribute("inkscape:label").value
74 puts "Going through layer '" + context_name + "'"
75 context.each_element("g") do |icon|
76 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
77 dir = "#{PREFIX16}/#{context_name}"
78 icon_name = icon.attributes.get_attribute("inkscape:label").value
79 chopSVG({ :name => icon_name,
80 :id => icon.attributes.get_attribute("id"),
81 :dir => dir,
82 :file => get_output_filename(dir, icon_name)})
83 end
84 end
85 puts "\nrendered all SVGs"
86 else #only render the icons passed
87 icons = ARGV
88 ARGV.each do |icon_name|
89 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
90 dir = "#{PREFIX16}/#{icon.parent.attributes['inkscape:label']}"
91 chopSVG({ :name => icon_name,
92 :id => icon.attributes["id"],
93 :dir => dir,
94 :file => get_output_filename(dir, icon_name),
95 :forcerender => true})
96 end
97 puts "\nrendered #{ARGV.length} icons"
98 end
99
100 SRC48 = "vector/source-48x48.svg"
101 PREFIX48 = "../Paper/48x48"
102
103 def chopSVG(icon)
104 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
105 unless (File.exists?(icon[:file]) && !icon[:forcerender])
106 FileUtils.cp(SRC48,icon[:file])
107 puts " >> #{icon[:name]}"
108 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers "
109 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
110 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
111 system(cmd)
112 #saving as plain SVG gets rid of the classes :/
113 #cmd = "#{INKSCAPE} -f #{icon[:file]} --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1"
114 #system(cmd)
115 svgcrop = Document.new(File.new(icon[:file], 'r'))
116 svgcrop.root.each_element("//rect") do |rect|
117 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 16 vs 15.99999
118 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
119 if w == 16 && h == 16
120 rect.remove
121 end
122 end
123 icon_f = File.new(icon[:file],'w+')
124 icon_f.puts svgcrop
125 icon_f.close
126 else
127 puts " -- #{icon[:name]} already exists"
128 end
129 end #end of function
130
131 def get_output_filename(d,n)
132 if (/rtl$/.match(n))
133 outfile = "#{d}/#{n.chomp('-rtl')}-rtl.svg"
134 else
135 outfile = "#{d}/#{n}.svg"
136 end
137 return outfile
138 end
139
140 #main
141 # Open SVG file.
142 svg = Document.new(File.new(SRC48, 'r'))
143
144 if (ARGV[0].nil?) #render all SVGs
145 puts "Rendering from icons in #{SRC48}"
146 # Go through every layer.
147 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
148 context_name = context.attributes.get_attribute("inkscape:label").value
149 puts "Going through layer '" + context_name + "'"
150 context.each_element("g") do |icon|
151 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
152 dir = "#{PREFIX48}/#{context_name}"
153 icon_name = icon.attributes.get_attribute("inkscape:label").value
154 chopSVG({ :name => icon_name,
155 :id => icon.attributes.get_attribute("id"),
156 :dir => dir,
157 :file => get_output_filename(dir, icon_name)})
158 end
159 end
160 puts "\nrendered all SVGs"
161 else #only render the icons passed
162 icons = ARGV
163 ARGV.each do |icon_name|
164 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
165 dir = "#{PREFIX48}/#{icon.parent.attributes['inkscape:label']}"
166 chopSVG({ :name => icon_name,
167 :id => icon.attributes["id"],
168 :dir => dir,
169 :file => get_output_filename(dir, icon_name),
170 :forcerender => true})
171 end
172 puts "\nrendered #{ARGV.length} icons"
173 end
174
175 #EOF