Debian Codegraph @

src/panel/extract-panel-icons.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # Legal Stuff:
4 #
5 # This file 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 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
21 require "rexml/document"
22 require "fileutils"
23 include REXML
24
25 # INKSCAPE = 'flatpak run org.inkscape.Inkscape'
26 INKSCAPE = '/usr/bin/inkscape'
27 THEME= "Paper"
28 SIZES = ["16", "22", "24"]
29 COLORS = ["light", "dark"]
30
31 # install with `sudo npm install -g svgo`
32 SVGO = '/usr/local/bin/svgo'
33
34 def chopSVG(icon)
35 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
36 unless (File.exists?(icon[:file]) && !icon[:forcerender])
37 FileUtils.cp(SRC,icon[:file])
38 puts " >> #{icon[:name]}"
39 cmd = "#{INKSCAPE} -f #{icon[:file]} --select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers "
40 cmd += "--verb=EditDelete --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=StrokeToPath --verb=FileVacuum "
41 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
42 system(cmd)
43 #saving as plain SVG gets rid of the classes :/
44 cmd = "#{INKSCAPE} --vacuum-defs -z #{icon[:file]} --export-plain-svg=#{icon[:file]} > /dev/null 2>&1"
45 system(cmd)
46 #completely vaccuum with svgo
47 cmd = "#{SVGO} --pretty --disable=convertShapeToPath -i #{icon[:file]} -o #{icon[:file]} > /dev/null 2>&1"
48 system(cmd)
49 # crop
50 svgcrop = Document.new(File.new(icon[:file], 'r'))
51 svgcrop.root.each_element("//rect") do |rect|
52 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 16 vs 15.99999
53 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
54 if w == 16 && h == 16
55 rect.remove
56 end
57 end
58 icon_f = File.new(icon[:file],'w+')
59 icon_f.puts svgcrop
60 icon_f.close
61 else
62 puts " -- #{icon[:name]} already exists"
63 end
64 end #end of function
65
66 def get_output_filename(d,n)
67 if (/rtl$/.match(n))
68 outfile = "#{d}/#{n.chomp('-rtl')}-rtl.svg"
69 else
70 outfile = "#{d}/#{n}.svg"
71 end
72 return outfile
73 end
74
75 ## DO IT
76 for COLOR in COLORS
77 for SIZE in SIZES
78 SRC="./#{COLOR}/source-panel-#{SIZE}.svg"
79 PREFIX="../../#{THEME}/#{SIZE}x#{SIZE}"
80
81 svg = Document.new(File.new(SRC, 'r'))
82
83 if (ARGV[0].nil?) #render all SVGs
84 puts "Rendering from icons in #{SRC}"
85 # Go through every layer.
86 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
87 context_name = context.attributes.get_attribute("inkscape:label").value
88 puts "Going through layer '" + context_name + "'"
89 context.each_element("g") do |icon|
90 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
91 dir = "#{PREFIX}/#{context_name}"
92 icon_name = icon.attributes.get_attribute("inkscape:label").value
93 if icon_name.end_with?("-alt")
94 puts " >> skipping icon '" + icon_name + "'"
95 else
96 chopSVG({ :name => icon_name,
97 :id => icon.attributes.get_attribute("id"),
98 :dir => dir,
99 :file => get_output_filename(dir, icon_name)})
100 end
101 end
102 end
103 puts "\nrendered all SVGs"
104 else #only render the icons passed
105 icons = ARGV
106 ARGV.each do |icon_name|
107 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
108 dir = "#{PREFIX}/#{icon.parent.attributes['inkscape:label']}"
109 chopSVG({ :name => icon_name,
110 :id => icon.attributes["id"],
111 :dir => dir,
112 :file => get_output_filename(dir, icon_name),
113 :forcerender => true})
114 end
115 puts "\nrendered #{ARGV.length} icons"
116 end
117 end
118 end
119 #EOF