xref: /aoo41x/main/solenv/bin/image-sort.pl (revision cdf0e10c)
1#!/usr/bin/perl -w
2
3my @global_list = ();
4my %global_hash = ();
5my $base_path;
6
7sub read_icons($)
8{
9    my $fname = shift;
10    my $fileh;
11    my @images;
12    open ($fileh, "$base_path/$fname") || die "Can't open $base_path/$fname: $!";
13    while (<$fileh>) {
14	m/xlink:href=\"\.uno:(\S+)\"\s+/ || next;
15	push @images, lc($1);
16    }
17    close ($fileh);
18
19    return @images;
20}
21
22# filter out already seen icons & do prefixing
23sub read_new_icons($$)
24{
25    my $fname = shift;
26    my $prefix = shift;
27    my @images = read_icons ($fname);
28    my @new_icons;
29    my %new_icons;
30    for my $icon (@images) {
31	my $iname = "res/commandimagelist/" . $prefix . $icon . ".png";
32	if (!defined $global_hash{$iname} &&
33	    !defined $new_icons{$iname}) {
34	    push @new_icons, $iname;
35	    $new_icons{$iname} = 1;
36	}
37    }
38    return @new_icons;
39}
40
41sub process_group($@)
42{
43    my $prefix = shift;
44    my @uiconfigs = @_;
45    my %group;
46    my $cur_max = 1.0;
47
48# a very noddy sorting algorithm
49    for my $uiconfig (@uiconfigs) {
50	my @images = read_new_icons ($uiconfig, $prefix);
51	my $prev = '';
52	for my $icon (@images) {
53	    if (!defined $group{$icon}) {
54		if (!defined $group{$prev}) {
55		    $group{$icon} = $cur_max;
56		    $cur_max += 1.0;
57		} else {
58		    $group{$icon} = $group{$prev} + (1.0 - 0.5 / $cur_max);
59		}
60	    } # else a duplicate
61	}
62    }
63    for my $icon (sort { $group{$a} <=> $group{$b} } keys %group) {
64	push @global_list, $icon;
65	$global_hash{$icon} = 1;
66    }
67}
68
69sub process_file($$)
70{
71    my @images = read_new_icons (shift, shift);
72
73    for my $icon (@images) {
74	push @global_list, $icon;
75	$global_hash{$icon} = 1;
76    }
77}
78
79sub chew_controlfile($)
80{
81    my $fname = shift;
82    my $fileh;
83    my @list;
84    open ($fileh, $fname) || die "Can't open $fname: $!";
85    while (<$fileh>) {
86	/^\#/ && next;
87	s/[\r\n]*$//;
88	/^\s*$/ && next;
89
90	my $line = $_;
91	if ($line =~ s/^-- (\S+)\s*//) {
92	    # control code
93	    my $code = $1;
94	    my $small = (lc ($line) eq 'small');
95	    if (lc($code) eq 'group') {
96		if (!$small) { process_group ("lc_", @list); }
97		process_group ("sc_", @list);
98	    } elsif (lc ($code) eq 'ordered') {
99		if (!$small) {
100		    for my $file (@list) { process_file ($file, "lc_"); }
101		}
102		for my $file (@list) { process_file ($file, "sc_"); }
103	    } elsif (lc ($code) eq 'literal') {
104		for my $file (@list) {
105		    if (!defined $global_hash{$file}) {
106			push @global_list, $file;
107			$global_hash{$file} = 1;
108		    }
109		}
110	    } else {
111		die ("Unknown code '$code'");
112	    }
113	    @list = ();
114	} else {
115	    push @list, $line;
116	}
117    }
118    close ($fileh);
119}
120
121if (!@ARGV) {
122    print "image-sort <image-sort.lst> /path/to/OOOo/source/root\n";
123    exit 1;
124}
125
126# where the control file lives
127my $control = shift @ARGV;
128# where the uiconfigs live
129$base_path = shift @ARGV;
130# output
131if (@ARGV) {
132    my $outf = shift @ARGV;
133    open ($output, ">$outf") || die "Can't open $outf: $!";
134    $stdout_out = 0;
135} else {
136    $output = STDOUT;
137    $stdout_out = 1;
138}
139
140chew_controlfile ($control);
141
142for my $icon (@global_list) {
143    print $output $icon . "\n" if (!($icon =~ /^sc_/));
144}
145for my $icon (@global_list) {
146    print $output $icon . "\n" if ($icon =~ /^sc_/);
147}
148
149close $output if (!$stdout_out);
150