iconmanager/getter.pl
2024-04-25 18:59:10 +02:00

145 lines
3.1 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
#use LWP::Simple;
use Data::Dumper;
my %param = (
noDownload => 0, # ignore icons that need a download
changeColor => '#0000ff', # undef: do not change color
);
my @conf = ();
{
my $fh = undef;
open($fh, '<', 'icon.conf') or die "Cannot read icon.conf, because: $!";
my $i=1;
while(my $line = <$fh>) {
$i++;
if($line=~m#^\s*([a-zA-Z_0-9]+)\s*=\s*(.*?)\s*(?:--.*)?$#) {
my ($name, $def) = ($1, $2);
push @conf, {row => $i, name => $name, origdef => $def};
next
} elsif($line=~m/^\s*#/) {
next
}
$i--;
die "Bad line $i: $line"
}
}
my %set = (
phosphor => {
mode => 'file',
where => sub {
my $pwd = shift;
$pwd=~m#(.*):(.*)# or die "Bad icon in phsophor: '$pwd'";
my ($kind, $name) = ($1, $2);
return "phosphor_orig/SVGs Flat/$kind/$name-$kind.svg"
},
},
material => {
mode => 'url',
where => sub {
my $pwd = shift;
return "https://fonts.gstatic.com/s/i/short-term/release/materialsymbolsoutlined/$pwd/default/24px.svg"
},
},
);
for my $o(@conf) {
if($o->{origdef}=~m#^([a-z]+):(.*)#) {
my ($set, $name) = ($1, $2);
my $thisSet = $set{$set};
die "Error in row $o->{row}: Cannot find icon set $set" unless defined $thisSet;
if($thisSet->{mode} eq 'file') {
#my $pp1 = $thisSet->{where};
#my $pp2 = $name;
#$pp2=~s#:#/#g;
#my $path = "$pp1/$pp2.svg";
my $path = $thisSet->{where}->($name);
my $rh = undef;
open($rh, '<', $path) or die "Could not read $path, because: $!";
$o->{svg} = join '', <$rh>
} elsif($thisSet->{mode} eq 'url') {
next if $param{noDownload};
my $path = $thisSet->{where}->($name);
$o->{svg} = qx(wget '$path' -O -);
} else {
die "Error in set definition $set: Mode $thisSet->{mode} is not implemented"
}
} elsif($o->{origdef}=~m#^/#) {
die ".. not allowed as part of a path in $o->{origdef}" if $o->{origdef}=~m#\.\.#;
my $fh = undef;
my $path = "input$o->{origdef}";
open($fh, '<', $path) or die "could not read $path, because: $!";
$o->{svg} = join '', <$fh>
} else {
die "Bad icon definition for icon $o->{name}: $o->{origdef}";
}
}
print Data::Dumper::Dumper(\@conf);
mkdir("output");
for my $o(@conf) {
my $cont = $o->{svg};
next unless $cont;
if($cont!~m#^\s*<svg[^<>]*fill=[^<>]*>#) {
$cont=~s#^\s*(<svg[^<>]*)>#$1 fill="currentColor">#
}
if($param{changeColor}) {
$cont=~s#fill="currentColor"#fill="$param{changeColor}"#g
}
my $fn = "output/$o->{name}.svg";
my $wh = undef;
open($wh, '>', $fn) or die "Could not write $fn, because: $!";
print $wh $cont
}
my $fh = undef;
open($fh, '>', 'summary.html') or die "Could not write summary.html, because: $!";
print $fh qq#<!DOCTYPE html>
<head>
</head>
<body>
<table border="3">
<tr>
<th>row</th>
<th>
UniWorX-Ident
</th>
<th>
Font
</th>
<th>
Icon
</th>
</tr>
#;
for my $o(@conf) {
print $fh qq#
<tr>
<td>$o->{row}</td>
<td>$o->{name}</td>
<td>$o->{origdef}</td>
<td><img src="output/$o->{name}.svg" /></td>
</tr>
#;
}
print $fh qq#</table></body>
#;