summaryrefslogtreecommitdiff
path: root/solenv/bin/image-sort.pl
blob: 5b2441d6a3d8eb82e937115a90700a5e4012244c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/perl -w
# -*- Mode: Perl; tab-width: 4; indent-tabs-mode: nil -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#

my @global_list = ();
my %global_hash = ();
my $base_path;

sub read_icons($)
{
    my $fname = shift;
    my $fileh;
    my @images;
    if (! -e "$base_path/$fname") {
        print "Skipping non-existent $base_path/$fname\n";
        return @images;
    }
    open ($fileh, "$base_path/$fname") || die "Can't open $base_path/$fname: $!";
    while (<$fileh>) {
        m/xlink:href=\"\.uno:(\S+)\"\s+/ || next;
        push @images, lc($1);
    }
    close ($fileh);

    return @images;
}

# filter out already seen icons & do prefixing
sub read_new_icons($$)
{
    my $fname = shift;
    my $prefix = shift;
    my @images = read_icons ($fname);
    my @new_icons;
    my %new_icons;
    for my $icon (@images) {
        my $iname = "cmd/" . $prefix . $icon . ".png";
        if (!defined $global_hash{$iname} &&
            !defined $new_icons{$iname}) {
            push @new_icons, $iname;
            $new_icons{$iname} = 1;
        }
    }
    return @new_icons;
}

sub process_group($@)
{
    my $prefix = shift;
    my @uiconfigs = @_;
    my %group;
    my $cur_max = 1.0;

# a very noddy sorting algorithm
    for my $uiconfig (@uiconfigs) {
        my @images = read_new_icons ($uiconfig, $prefix);
        my $prev = '';
        for my $icon (@images) {
            if (!defined $group{$icon}) {
                if (!defined $group{$prev}) {
                    $group{$icon} = $cur_max;
                    $cur_max += 1.0;
                } else {
                    $group{$icon} = $group{$prev} + (1.0 - 0.5 / $cur_max);
                }
            } # else a duplicate
        }
    }
    for my $icon (sort { $group{$a} <=> $group{$b} } keys %group) {
        push @global_list, $icon;
        $global_hash{$icon} = 1;
    }
}

sub process_file($$)
{
    my @images = read_new_icons (shift, shift);

    for my $icon (@images) {
        push @global_list, $icon;
        $global_hash{$icon} = 1;
    }
}

sub chew_controlfile($)
{
    my $fname = shift;
    my $fileh;
    my @list;
    open ($fileh, $fname) || die "Can't open $fname: $!";
    while (<$fileh>) {
        /^\#/ && next;
        s/[\r\n]*$//;
        /^\s*$/ && next;

        my $line = $_;
        if ($line =~ s/^-- (\S+)\s*//) {
            # control code
            my $code = $1;
            my $small = (lc ($line) eq 'small');
            if (lc($code) eq 'group') {
                if (!$small) {
                    process_group ("lc_", @list);
                }
                process_group ("sc_", @list);
            } elsif (lc ($code) eq 'ordered') {
                if (!$small) {
                    for my $file (@list) {
                        process_file ($file, "lc_");
                    }
                }
                for my $file (@list) {
                    process_file ($file, "sc_");
                }
            } elsif (lc ($code) eq 'literal') {
                for my $file (@list) {
                    if (!defined $global_hash{$file}) {
                        push @global_list, $file;
                        $global_hash{$file} = 1;
                    }
                }
            } else {
                die ("Unknown code '$code'");
            }
            @list = ();
        } else {
            push @list, $line;
        }
    }
    close ($fileh);
}

if (!@ARGV) {
    print "image-sort <image-sort.lst> /path/to/OOOo/source/root\n";
    exit 1;
}

# where the control file lives
my $control = shift @ARGV;
# where the uiconfigs live
$base_path = shift @ARGV;
# output
if (@ARGV) {
    my $outf = shift @ARGV;
    open ($output, ">$outf") || die "Can't open $outf: $!";
    $stdout_out = 0;
} else {
    $output = STDOUT;
    $stdout_out = 1;
}

chew_controlfile ($control);

for my $icon (@global_list) {
    print $output $icon . "\n" if (!($icon =~ /^sc_/));
}
for my $icon (@global_list) {
    print $output $icon . "\n" if ($icon =~ /^sc_/);
}

close $output if (!$stdout_out);

# dnl vim:set shiftwidth=4 softtabstop=4 expandtab: