Debian Codegraph @

src/scalable/extract-symbolic-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 SRC = "./source-symbolic.svg"
28 PREFIX = "../../Paper/scalable"
29
30 # SVGO is a Node.js SVG optimization tool install with 'sudo npm install -g svgo'
31 # script will skip if SVGO is not present
32 #SVGO = '/usr/local/bin/svgo'
33 SVGO = '/usr/bin/svgo'
34
35 def chopSVG(icon)
36 FileUtils.mkdir_p(icon[:dir]) unless File.exists?(icon[:dir])
37 unless (File.exists?(icon[:file]) && !icon[:forcerender])
38 FileUtils.cp(SRC,icon[:file])
39 puts " >> #{icon[:name]}"
40 # extract the icon
41 cmd = "#{INKSCAPE} -f #{icon[:file]} "
42 cmd += "--select #{icon[:id]} --verb=FitCanvasToSelection --verb=EditInvertInAllLayers --verb=EditDelete " # delete everything but the icon
43 cmd += "--verb=FileVacuum --verb=FileSave --verb=FileQuit > /dev/null 2>&1"
44 system(cmd)
45 # remove bounding rectangle
46 svgcrop = Document.new(File.new(icon[:file], 'r'))
47 svgcrop.root.each_element("//rect") do |rect|
48 w = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #get rid of 16 vs 15.99999
49 h = ((rect.attributes["width"].to_f * 10).round / 10.0).to_i #Inkscape bugs
50 if w == 16 && h == 16
51 rect.remove
52 end
53 end
54 icon_f = File.new(icon[:file],'w+')
55 icon_f.puts svgcrop
56 icon_f.close
57 # convert any strokes and objects to paths
58 cmd = "#{INKSCAPE} -f #{icon[:file]} --verb=EditSelectAll --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=SelectionUnGroup --verb=ObjectToPath --verb=StrokeToPath "
59 cmd += "--verb=FileSave --verb=FileQuit > /dev/null 2>&1"
60 system(cmd)
61 # save as plain SVG
62 cmd = "#{INKSCAPE} -f #{icon[:file]} -z --vacuum-defs --export-plain-svg=#{icon[:file]} > /dev/null 2>&1"
63 system(cmd)
64 # remove as many extraneous elements as possible with SVGO
65 cmd = "#{SVGO} --pretty --disable=convertShapeToPath --disable=convertPathData --enable=removeStyleElement -i #{icon[:file]} -o #{icon[:file]} > /dev/null 2>&1"
66 system(cmd)
67 else
68 puts " -- #{icon[:name]} already exists"
69 end
70 end #end of function
71
72 def get_output_filename(d,n)
73 if (/rtl$/.match(n))
74 outfile = "#{d}/#{n.chomp('-rtl')}-symbolic-rtl.svg"
75 else
76 outfile = "#{d}/#{n}-symbolic.svg"
77 end
78 return outfile
79 end
80
81 #main
82 # Open SVG file.
83 svg = Document.new(File.new(SRC, 'r'))
84
85 if (ARGV[0].nil?) #render all SVGs
86 puts "Rendering from icons in #{SRC}"
87 # Go through every layer.
88 svg.root.each_element("/svg/g[@inkscape:groupmode='layer']") do |context|
89 context_name = context.attributes.get_attribute("inkscape:label").value
90 puts "Going through layer '" + context_name + "'"
91 context.each_element("g") do |icon|
92 #puts "DEBUG #{icon.attributes.get_attribute('id')}"
93 dir = "#{PREFIX}/#{context_name}"
94 icon_name = icon.attributes.get_attribute("inkscape:label").value
95 # prevent rendering of icons ending in :
96 if icon_name.end_with?("-alt", "-old", "-template", "-source", "-ltr", "-working")
97 puts " ++ skipping icon '" + icon_name + "'"
98 elsif icon_name =~ /\d$/
99 puts " ++ skipping icon '" + icon_name + "'"
100 else
101 chopSVG({ :name => icon_name,
102 :id => icon.attributes.get_attribute("id"),
103 :dir => dir,
104 :file => get_output_filename(dir, icon_name)})
105 end
106 end
107 end
108 puts "\nrendered all SVGs"
109 else #only render the icons passed
110 icons = ARGV
111 ARGV.each do |icon_name|
112 icon = svg.root.elements["//g[@inkscape:label='#{icon_name}']"]
113 if icon_name == "process-working"
114 dir = "#{PREFIX32}/#{icon.parent.attributes['inkscape:label']}"
115 else
116 dir = "#{PREFIX}/#{icon.parent.attributes['inkscape:label']}"
117 end
118 chopSVG({ :name => icon_name,
119 :id => icon.attributes["id"],
120 :dir => dir,
121 :file => get_output_filename(dir, icon_name),
122 :forcerender => true})
123 end
124 puts "\nrendered #{ARGV.length} icons"
125 end