source: josm/trunk/scripts/geticons.pl@ 19176

Last change on this file since 19176 was 18743, checked in by stoecker, 19 months ago

fix icon script and warning in SVG image

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.4 KB
Line 
1#! /usr/bin/perl -w
2# short tool to find out all used icons and allows deleting unused icons
3# when building release files
4
5my @default = (
6 "resources/styles/standard/*.xml",
7 "resources/styles/standard/*.mapcss",
8 "resources/data/*.xml",
9 "src/org/openstreetmap/josm/*.java",
10 "src/org/openstreetmap/josm/*/*.java",
11 "src/org/openstreetmap/josm/*/*/*.java",
12 "src/org/openstreetmap/josm/*/*/*/*.java",
13 "src/org/openstreetmap/josm/*/*/*/*/*.java",
14 "src/org/openstreetmap/josm/*/*/*/*/*/*.java",
15 "src/org/openstreetmap/josm/*/*/*/*/*/*/*.java"
16);
17
18my %icons;
19
20my $o = $/;
21
22for my $arg (@ARGV ? @ARGV : @default)
23{
24 for my $file (glob($arg))
25 {
26 my @defs;
27 open(FILE,"<",$file) or die "Could not open $file\n";
28 #print "Read file $file\n";
29 $/ = $file =~ /\.java$/ ? ";" : $o;
30 my $extends = "";
31 while(my $l = <FILE>)
32 {
33 if($l =~ /extends MapMode/)
34 {
35 $extends = "mapmode/";
36 }
37 elsif($l =~ /extends ToggleDialog/)
38 {
39 $extends = "dialogs/";
40 }
41 elsif($l =~ /extends JosmAction/)
42 {
43 $extends = "";
44 }
45 if($l =~ /private static final String ([A-Z_]+) = ("[^"]+")/)
46 {
47 push(@defs, [$1, $2]);
48 }
49 next if $l =~ /NO-ICON/;
50 for my $d (@defs)
51 {
52 $l =~ s/$d->[0]/$d->[1]/g;
53 }
54 if($l =~ /icon\s*[:=]\s*["']([^"'+]+?)["']/)
55 {
56 my $i = $1;
57 ++$icons{$i};
58 }
59
60 if(($l =~ /(?:icon-image|repeat-image|fill-image)\s*:\s*(\"?(.*?)\"?)\s*;/) && ($1 ne "none"))
61 {
62 my $i = $2;
63 ++$icons{$i};
64 }
65 if($l =~ /ImageProvider(?:\.get)?\(\"([^\"]*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\)/)
66 {
67 my $i = $1;
68 ++$icons{$i};
69 }
70 while($l =~ /\/\*\s*ICON\s*\*\/\s*\"(.*?)\"/g)
71 {
72 my $i = $1;
73 ++$icons{$i};
74 }
75 while($l =~ /\/\*\s*ICON\((.*?)\)\s*\*\/\s*\"(.*?)\"/g)
76 {
77 my $i = "$1$2";
78 ++$icons{$i};
79 }
80 if($l =~ /new\s+ImageLabel\(\"(.*?)\"/)
81 {
82 my $i = "statusline/$1";
83 ++$icons{$i};
84 }
85 if($l =~ /setIcon\(\"(.*?)\"/)
86 {
87 my $i = "statusline/$1";
88 ++$icons{$i};
89 }
90 if($l =~ /ImageProvider\.get(?:IfAvailable)?\(\"(.*?)\",\s*\"(.*?)\"(?:, (?:ImageProvider\.)?ImageSizes\.[A-Z]+)?\s*\)/)
91 {
92 my $i = "$1/$2";
93 ++$icons{$i};
94 }
95 if($l =~ /new ImageProvider\(\"(.*?)\",\s*\"(.*?)\"\s*\)/)
96 {
97 my $i = "$1/$2";
98 ++$icons{$i};
99 }
100 if($l =~ /getCursor\(\"(.*?)\",\s*\"(.*?)\"/)
101 {
102 my $i = "cursor/modifier/$2";
103 ++$icons{$i};
104 $i = "cursor/$1";
105 ++$icons{$i};
106 }
107 if($l =~ /ImageProvider\.getCursor\(\"(.*?)\",\s*null\)/)
108 {
109 my $i = "cursor/$1";
110 ++$icons{$i};
111 }
112 if($l =~ /super\(\s*tr\(\".*?\"\),\s*\"(.*?)\"/s)
113 {
114 my $i = "$extends$1";
115 ++$icons{$i};
116 }
117 if($l =~ /super\(\s*trc\(\".*?\",\s*\".*?\"\),\s*\"(.*?)\"/s)
118 {
119 my $i = "$extends$1";
120 ++$icons{$i};
121 }
122 if($l =~ /setButtonIcons.*\{(.*)\}/ || $l =~ /setButtonIcons\((.*)\)/ )
123 {
124 my $t = $1;
125 while($t =~ /\"(.*?)\"/g)
126 {
127 my $i = $1;
128 ++$icons{$i};
129 }
130 }
131 }
132 close FILE;
133 }
134}
135
136my %haveicons;
137
138for($i = 1; my @ifiles = (glob("resources/images".("/*" x $i).".png"), glob("resources/images".("/*" x $i).".svg")); ++$i)
139{
140 for my $ifile (sort @ifiles)
141 {
142 $ifile =~ s/^resources\/images\///;
143 # svg comes after png due to the glob, so only check for svg's
144 if($ifile =~ /^(.*)\.svg$/)
145 {
146 if($haveicons{"$1.png"})
147 {
148 print STDERR "$1: File exists twice as .svg and .png.\n";
149 }
150 # check for unwanted svg effects
151 if(open FILE, "<","resources/images/$ifile")
152 {
153 my $hadfont = 0;
154 undef $/;
155 my $f = <FILE>;
156 close FILE;
157 for my $sep ("'", '"')
158 {
159 while($f =~ /style\s*=\s*$sep([^$sep]+)$sep/g)
160 {
161 for my $x (split(/\s*;\s*/, $1))
162 {
163 print STDERR "$ifile: Style starts with minus: $x\n" if $x =~ /^-/;
164 ++$hadfont if($x =~ /^font-/);
165 if($x =~ /^font-family:(.*)$/ && $x !~ /^font-family:'Droid Sans'/)
166 {
167 print STDERR "$ifile: Unwanted font-family: $1\n"
168 }
169 }
170 }
171 }
172 if($f =~ /<style[^>]+type=['"]text\/css['"][^>]*>/m)
173 {
174 print STDERR "$ifile: CSS-Style in SVG icon not supported\n";
175 }
176 if($f =~ /viewBox\s*=\s*["']([^"']+)["']/)
177 {
178 my $x = $1;
179 print STDERR "$ifile: ViewBox has float values: $x\n" if $x =~ /\./;
180 }
181 if($f !~ /<text/ && $hadfont)
182 {
183 print STDERR "$ifile: Font-style without <text>\n";
184 }
185 }
186 else
187 {
188 print STDERR "$ifile: Could not open file: $1";
189 }
190 }
191 $haveicons{$ifile} = 1;
192 }
193}
194
195for my $img (sort keys %icons)
196{
197 if($img =~ /\.(png|svg)/)
198 {
199 print STDERR "$img: File does not exist!\n" if(!-f "resources/images/$img");
200 delete $haveicons{$img};
201 }
202 else
203 {
204 print STDERR "$img(.svg|.png): File does not exist!\n" if(!-f "resources/images/$img.png" && !-f "resources/images/$img.svg");
205 delete $haveicons{"$img.svg"};
206 delete $haveicons{"$img.png"};
207 }
208}
209
210for my $img (sort keys %haveicons)
211{
212 print "$img: Unused image.\n";
213}
Note: See TracBrowser for help on using the repository browser.