1cdf0e10cSrcweir#!/usr/bin/perl -w 2*bb113e63SAndrew Rist# ************************************************************* 3*bb113e63SAndrew Rist# 4*bb113e63SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 5*bb113e63SAndrew Rist# or more contributor license agreements. See the NOTICE file 6*bb113e63SAndrew Rist# distributed with this work for additional information 7*bb113e63SAndrew Rist# regarding copyright ownership. The ASF licenses this file 8*bb113e63SAndrew Rist# to you under the Apache License, Version 2.0 (the 9*bb113e63SAndrew Rist# "License"); you may not use this file except in compliance 10*bb113e63SAndrew Rist# with the License. You may obtain a copy of the License at 11*bb113e63SAndrew Rist# 12*bb113e63SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 13*bb113e63SAndrew Rist# 14*bb113e63SAndrew Rist# Unless required by applicable law or agreed to in writing, 15*bb113e63SAndrew Rist# software distributed under the License is distributed on an 16*bb113e63SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17*bb113e63SAndrew Rist# KIND, either express or implied. See the License for the 18*bb113e63SAndrew Rist# specific language governing permissions and limitations 19*bb113e63SAndrew Rist# under the License. 20*bb113e63SAndrew Rist# 21*bb113e63SAndrew Rist# ************************************************************* 22cdf0e10cSrcweir 23cdf0e10cSrcweirmy @global_list = (); 24cdf0e10cSrcweirmy %global_hash = (); 25cdf0e10cSrcweirmy $base_path; 26cdf0e10cSrcweir 27cdf0e10cSrcweirsub read_icons($) 28cdf0e10cSrcweir{ 29cdf0e10cSrcweir my $fname = shift; 30cdf0e10cSrcweir my $fileh; 31cdf0e10cSrcweir my @images; 32cdf0e10cSrcweir open ($fileh, "$base_path/$fname") || die "Can't open $base_path/$fname: $!"; 33cdf0e10cSrcweir while (<$fileh>) { 34cdf0e10cSrcweir m/xlink:href=\"\.uno:(\S+)\"\s+/ || next; 35cdf0e10cSrcweir push @images, lc($1); 36cdf0e10cSrcweir } 37cdf0e10cSrcweir close ($fileh); 38cdf0e10cSrcweir 39cdf0e10cSrcweir return @images; 40cdf0e10cSrcweir} 41cdf0e10cSrcweir 42cdf0e10cSrcweir# filter out already seen icons & do prefixing 43cdf0e10cSrcweirsub read_new_icons($$) 44cdf0e10cSrcweir{ 45cdf0e10cSrcweir my $fname = shift; 46cdf0e10cSrcweir my $prefix = shift; 47cdf0e10cSrcweir my @images = read_icons ($fname); 48cdf0e10cSrcweir my @new_icons; 49cdf0e10cSrcweir my %new_icons; 50cdf0e10cSrcweir for my $icon (@images) { 51cdf0e10cSrcweir my $iname = "res/commandimagelist/" . $prefix . $icon . ".png"; 52cdf0e10cSrcweir if (!defined $global_hash{$iname} && 53cdf0e10cSrcweir !defined $new_icons{$iname}) { 54cdf0e10cSrcweir push @new_icons, $iname; 55cdf0e10cSrcweir $new_icons{$iname} = 1; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir } 58cdf0e10cSrcweir return @new_icons; 59cdf0e10cSrcweir} 60cdf0e10cSrcweir 61cdf0e10cSrcweirsub process_group($@) 62cdf0e10cSrcweir{ 63cdf0e10cSrcweir my $prefix = shift; 64cdf0e10cSrcweir my @uiconfigs = @_; 65cdf0e10cSrcweir my %group; 66cdf0e10cSrcweir my $cur_max = 1.0; 67cdf0e10cSrcweir 68cdf0e10cSrcweir# a very noddy sorting algorithm 69cdf0e10cSrcweir for my $uiconfig (@uiconfigs) { 70cdf0e10cSrcweir my @images = read_new_icons ($uiconfig, $prefix); 71cdf0e10cSrcweir my $prev = ''; 72cdf0e10cSrcweir for my $icon (@images) { 73cdf0e10cSrcweir if (!defined $group{$icon}) { 74cdf0e10cSrcweir if (!defined $group{$prev}) { 75cdf0e10cSrcweir $group{$icon} = $cur_max; 76cdf0e10cSrcweir $cur_max += 1.0; 77cdf0e10cSrcweir } else { 78cdf0e10cSrcweir $group{$icon} = $group{$prev} + (1.0 - 0.5 / $cur_max); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } # else a duplicate 81cdf0e10cSrcweir } 82cdf0e10cSrcweir } 83cdf0e10cSrcweir for my $icon (sort { $group{$a} <=> $group{$b} } keys %group) { 84cdf0e10cSrcweir push @global_list, $icon; 85cdf0e10cSrcweir $global_hash{$icon} = 1; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir} 88cdf0e10cSrcweir 89cdf0e10cSrcweirsub process_file($$) 90cdf0e10cSrcweir{ 91cdf0e10cSrcweir my @images = read_new_icons (shift, shift); 92cdf0e10cSrcweir 93cdf0e10cSrcweir for my $icon (@images) { 94cdf0e10cSrcweir push @global_list, $icon; 95cdf0e10cSrcweir $global_hash{$icon} = 1; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir} 98cdf0e10cSrcweir 99cdf0e10cSrcweirsub chew_controlfile($) 100cdf0e10cSrcweir{ 101cdf0e10cSrcweir my $fname = shift; 102cdf0e10cSrcweir my $fileh; 103cdf0e10cSrcweir my @list; 104cdf0e10cSrcweir open ($fileh, $fname) || die "Can't open $fname: $!"; 105cdf0e10cSrcweir while (<$fileh>) { 106cdf0e10cSrcweir /^\#/ && next; 107cdf0e10cSrcweir s/[\r\n]*$//; 108cdf0e10cSrcweir /^\s*$/ && next; 109cdf0e10cSrcweir 110cdf0e10cSrcweir my $line = $_; 111cdf0e10cSrcweir if ($line =~ s/^-- (\S+)\s*//) { 112cdf0e10cSrcweir # control code 113cdf0e10cSrcweir my $code = $1; 114cdf0e10cSrcweir my $small = (lc ($line) eq 'small'); 115cdf0e10cSrcweir if (lc($code) eq 'group') { 116cdf0e10cSrcweir if (!$small) { process_group ("lc_", @list); } 117cdf0e10cSrcweir process_group ("sc_", @list); 118cdf0e10cSrcweir } elsif (lc ($code) eq 'ordered') { 119cdf0e10cSrcweir if (!$small) { 120cdf0e10cSrcweir for my $file (@list) { process_file ($file, "lc_"); } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir for my $file (@list) { process_file ($file, "sc_"); } 123cdf0e10cSrcweir } elsif (lc ($code) eq 'literal') { 124cdf0e10cSrcweir for my $file (@list) { 125cdf0e10cSrcweir if (!defined $global_hash{$file}) { 126cdf0e10cSrcweir push @global_list, $file; 127cdf0e10cSrcweir $global_hash{$file} = 1; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir } else { 131cdf0e10cSrcweir die ("Unknown code '$code'"); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir @list = (); 134cdf0e10cSrcweir } else { 135cdf0e10cSrcweir push @list, $line; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138cdf0e10cSrcweir close ($fileh); 139cdf0e10cSrcweir} 140cdf0e10cSrcweir 141cdf0e10cSrcweirif (!@ARGV) { 142cdf0e10cSrcweir print "image-sort <image-sort.lst> /path/to/OOOo/source/root\n"; 143cdf0e10cSrcweir exit 1; 144cdf0e10cSrcweir} 145cdf0e10cSrcweir 146cdf0e10cSrcweir# where the control file lives 147cdf0e10cSrcweirmy $control = shift @ARGV; 148cdf0e10cSrcweir# where the uiconfigs live 149cdf0e10cSrcweir$base_path = shift @ARGV; 150cdf0e10cSrcweir# output 151cdf0e10cSrcweirif (@ARGV) { 152cdf0e10cSrcweir my $outf = shift @ARGV; 153cdf0e10cSrcweir open ($output, ">$outf") || die "Can't open $outf: $!"; 154cdf0e10cSrcweir $stdout_out = 0; 155cdf0e10cSrcweir} else { 156cdf0e10cSrcweir $output = STDOUT; 157cdf0e10cSrcweir $stdout_out = 1; 158cdf0e10cSrcweir} 159cdf0e10cSrcweir 160cdf0e10cSrcweirchew_controlfile ($control); 161cdf0e10cSrcweir 162cdf0e10cSrcweirfor my $icon (@global_list) { 163cdf0e10cSrcweir print $output $icon . "\n" if (!($icon =~ /^sc_/)); 164cdf0e10cSrcweir} 165cdf0e10cSrcweirfor my $icon (@global_list) { 166cdf0e10cSrcweir print $output $icon . "\n" if ($icon =~ /^sc_/); 167cdf0e10cSrcweir} 168cdf0e10cSrcweir 169cdf0e10cSrcweirclose $output if (!$stdout_out); 170