Debian Codegraph @

src/panel/extract-dark-panel-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 #INKSCAPE = '/usr/bin/inkscape' # like this works for me, while using `which` inkscape hangs
26
27 # Render 24x24 pixel panel icons
28 SRC24 = "panel-dark/source-panel-24.svg"
29 PREFIX24 = "../Paper-Mono-Dark/24x24"
30
31
32 def chopSVG(icon)
33 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
34 unless (File.exists?(icon[:file]) && !icon[:forcerender])
35 FileUtils.cp(SRC24,icon[:file])
36 puts " >> #{icon[:name]}"
37 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers "
38 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
39 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
40 system(cmd)
41 #saving as plain SVG gets rid of the classes :/
42 #cmd = "#{INKSCAPE} -f #{icon[:file]} -z --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1"
43 #system(cmd)
44 svgcrop = Document.new(File.new(icon[:file], 'r'))
45 svgcrop.root.each_element("//rect") do |rect|
46 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 24 vs 23.99999
47 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
48 if w == 24 && h == 24
49 rect.remove
50 end
51 end
52 icon_f = File.new(icon[:file],'w+')
53 icon_f.puts svgcrop
54 icon_f.close
55 else
56 puts " -- #{icon[:name]} already exists"
57 end
58 end #end of function
59
60 def get_output_filename(d,n)
61 if (/rtl$/.match(n))
62 outfile = "#{d}/#{n.chomp('-rtl')}-rtl.svg"
63 else
64 outfile = "#{d}/#{n}.svg"
65 end
66 return outfile
67 end
68
69 #main
70 # Open SVG file.
71 svg = Document.new(File.new(SRC24, 'r'))
72
73 if (ARGV[0].nil?) #render all SVGs
74 puts "Rendering from icons in #{SRC24}"
75 # Go through every layer.
76 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
77 context_name = context.attributes.get_attribute("inkscape:label").value
78 puts "Going through layer '" + context_name + "'"
79 context.each_element("g") do |icon|
80 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
81 dir = "#{PREFIX24}/#{context_name}"
82 icon_name = icon.attributes.get_attribute("inkscape:label").value
83 chopSVG({ :name => icon_name,
84 :id => icon.attributes.get_attribute("id"),
85 :dir => dir,
86 :file => get_output_filename(dir, icon_name)})
87 end
88 end
89 puts "\nrendered all SVGs"
90 else #only render the icons passed
91 icons = ARGV
92 ARGV.each do |icon_name|
93 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
94 dir = "#{PREFIX24}/#{icon.parent.attributes['inkscape:label']}"
95 chopSVG({ :name => icon_name,
96 :id => icon.attributes["id"],
97 :dir => dir,
98 :file => get_output_filename(dir, icon_name),
99 :forcerender => true})
100 end
101 puts "\nrendered #{ARGV.length} icons"
102 end
103
104
105 # Render 22x22 pixel panel icons
106 SRC22 = "panel-dark/source-panel-22.svg"
107 PREFIX22 = "../Paper-Mono-Dark/22x22"
108
109
110 def chopSVG(icon)
111 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
112 unless (File.exists?(icon[:file]) && !icon[:forcerender])
113 FileUtils.cp(SRC22,icon[:file])
114 puts " >> #{icon[:name]}"
115 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers "
116 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
117 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
118 system(cmd)
119 #saving as plain SVG gets rid of the classes :/
120 #cmd = "#{INKSCAPE} -f #{icon[:file]} -z --vacuum-defs -l #{icon[:file]} > /dev/null 2>&1"
121 #system(cmd)
122 svgcrop = Document.new(File.new(icon[:file], 'r'))
123 svgcrop.root.each_element("//rect") do |rect|
124 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 24 vs 23.99999
125 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
126 if w == 24 && h == 24
127 rect.remove
128 end
129 end
130 icon_f = File.new(icon[:file],'w+')
131 icon_f.puts svgcrop
132 icon_f.close
133 else
134 puts " -- #{icon[:name]} already exists"
135 end
136 end #end of function
137
138 def get_output_filename(d,n)
139 if (/rtl$/.match(n))
140 outfile = "#{d}/#{n.chomp('-rtl')}-rtl.svg"
141 else
142 outfile = "#{d}/#{n}.svg"
143 end
144 return outfile
145 end
146
147 #main
148 # Open SVG file.
149 svg = Document.new(File.new(SRC22, 'r'))
150
151 if (ARGV[0].nil?) #render all SVGs
152 puts "Rendering from icons in #{SRC22}"
153 # Go through every layer.
154 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
155 context_name = context.attributes.get_attribute("inkscape:label").value
156 puts "Going through layer '" + context_name + "'"
157 context.each_element("g") do |icon|
158 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
159 dir = "#{PREFIX22}/#{context_name}"
160 icon_name = icon.attributes.get_attribute("inkscape:label").value
161 chopSVG({ :name => icon_name,
162 :id => icon.attributes.get_attribute("id"),
163 :dir => dir,
164 :file => get_output_filename(dir, icon_name)})
165 end
166 end
167 puts "\nrendered all SVGs"
168 else #only render the icons passed
169 icons = ARGV
170 ARGV.each do |icon_name|
171 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
172 dir = "#{PREFIX22}/#{icon.parent.attributes['inkscape:label']}"
173 chopSVG({ :name => icon_name,
174 :id => icon.attributes["id"],
175 :dir => dir,
176 :file => get_output_filename(dir, icon_name),
177 :forcerender => true})
178 end
179 puts "\nrendered #{ARGV.length} icons"
180 end