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