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

Last change on this file since 16876 was 16876, checked in by stoecker, 3 years ago

see #15240 - detect CSS in icons checker

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