Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Last active August 24, 2016 09:33
Show Gist options
  • Save Jacajack/f1f065c49ce3cae9cf6cc9d85426c9fe to your computer and use it in GitHub Desktop.
Save Jacajack/f1f065c49ce3cae9cf6cc9d85426c9fe to your computer and use it in GitHub Desktop.
Numix folder icon development kit v0.0001
#!/usr/bin/perl
#If you are a Perl programmer
#Do not read this code... I beg you...
use Term::ANSIColor;
#Output sizes
my @sizes = ( 16, 22, 24, 32, 48, 64 );
#Read base symbol svg file (and find group with title="symbol")
my ( $svg ) = do { local $/; <> };
#Collapse
( $svg ) =~ s/[\n\r\t]/ /sgim;
( $svg ) =~ s/( ){2,}/ /sgim;
my ( $attributes, $symbol ) = $svg =~ /<g ([^<]*?)>(?:.?)<title.*>symbol<\/title>(.*?)<\/g>/sgim;
#Yup, I just used regex to parse XML
#I deserve to die, I know...
#Change working directory
#print "Note: please remove '_' from output files' names when they are ready, not to accidentally overwrite them!\n";
print color "red";
if ( not -e "symbols" )
{
mkdir( "symbols" ) or die( "Cannot create output directory - $!" );
}
chdir( "symbols" ) or die( "Cannot access output directory - $!" );
#Output svg files with different sizes
my $err = 0;
foreach $size ( @sizes )
{
print color "yellow";
print "Generating SVG symbol of size $size x $size...";
print color "red";
open( f, ">" . $size . ".svg" ) or die( " error - $!\n" );
print f "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 $size $size\">";
print f "<g $attributes><title>symbol</title>";
print f $symbol;
print f "</g>";
print f "</svg>";
close( f );
print color "green";
print " done\n";
}
print color "";
#!/usr/bin/perl
use Term::ANSIColor;
use JSON::Parse ':all';
use File::Path qw( mkpath );
use Data::Dumper;
my @baseIcons;
my @symbols;
my @outputIcons;
#Read associations file
my @conf;
my $txtconf;
my $conflength;
open( fconf, "<paste.json" ) or die( "Cannot read configuration file" );
while ( <fconf> )
{
$txtconf .= $_;
}
close( fconf );
@conf = @{ parse_json( $txtconf ) };
for ( my $i = 0; $i < scalar( @conf ); $i++ )
{
$baseIcons[$i] = $conf[$i][0];
$symbols[$i] = $conf[$i][1];
$outputIcons[$i] = $conf[$i][2];
}
if ( scalar( @baseIcons ) != scalar( @symbols ) || scalar( @baseIcons ) != scalar( @outputIcons ) )
{
die( "Input files count does not match!" );
}
my $attributes = "";
my $symbol = "";
my $svgBase = "";
my $svgSymbol = "";
my $svgOutput = "";
my $size = "";
my $dir = "";
for ( my $i = 0; $i < scalar( @baseIcons ); $i++ )
{
open( fb, "<" . $baseIcons[$i] ) or die( "File IO error" );
print color "yellow";
print "Reading $baseIcons[$i]...\n";
print color "red";
while ( <fb> )
{
$svgBase .= $_;
}
close( fb );
print color "yellow";
print "Reading $symbols[$i]...\n";
print color "red";
open( fs, "<" . $symbols[$i] ) or die( "File IO error" );
while ( <fs> )
{
$svgSymbol .= $_;
}
close( fs );
( $attributes, $symbol ) = $svgSymbol =~ /<g ([^<]*?)>(?:.?)<title.*>symbol<\/title>(.*?)<\/g>/sgim;
( $size ) = $svgBase =~ /<svg.*viewBox.?=.?"\d+ \d+ \d+ (\d+)">/;
print color "yellow";
print "Icon size: $size px\n";
$svgBase =~ s/<\/svg>/<g $attributes>$symbol<\/g><\/svg>/;
$svgOutput = $svgBase;
( $dir ) = $outputIcons[$i] =~ /(.*)\/[^\/]*\.svg/sgi;
if ( not -e $dir )
{
print "Creating directory '$dir'\n";
mkpath( $dir, 0, 0777 );
}
open( fo, ">" . $outputIcons[$i] );
print fo $svgOutput;
close( fo );
print "\n";
$attributes = "";
$symbol = "";
$svgBase = "";
$svgSymbol = "";
$svgOutput = "";
$size = "";
$dir = "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment