#!/usr/bin/perl

# fti2svg.pl - converts SGI IRIX .fti icons to .svg icons suitable
#              for Nautilus
#
# Copyright 2002 Caleb Shay <caleb@webninja.com>
# You can copy this code, distribute it, print it out and tape it to the wall.
# But if you change it, please send me the changes, and if you use it in
# another product, I want a copy.  That's it, have fun.
#
# Usage: fti2svg.pl file.fti [file2.fti ...] file.svg
#
# If passed multiple fti files it will composite them from the bottom up
# ie file.fti would be UNDER file2.fti
#
# I do exactly 0 sanity checking, so watch your arguments
#
# It's a hack, it's ugly, it works.
# What do you want?  It's perl.

$svgfile = pop;
foreach $arg (@ARGV) {
	open(FTI,$arg);
	push @lines,<FTI>;
	close(FTI);
}

# The Fltk color map, do you have any idea how hard it was
# to figure out how these colors map?

#fixed colors
@colormap[0] = "#000000";
@colormap[1] = "#f80000";
@colormap[2] = "#00fc00";
@colormap[3] = "#f8fc00";
@colormap[4] = "#0000f8";
@colormap[5] = "#f800f8";
@colormap[6] = "#00fcf8";
@colormap[7] = "#f8fcf8";
@colormap[8] = "#505450";
@colormap[9] = "#c07070";
@colormap[10] = "#70c470";
@colormap[11] = "#888c38";
@colormap[12] = "#7070c0";
@colormap[13] = "#883888";
@colormap[14] = "#388c88";
@colormap[15] = "#a8a8a8";

# dead colors
@colormap[16] = "#505450";
@colormap[17] = "#505450";
@colormap[18] = "#505450";
@colormap[19] = "#505450";
@colormap[20] = "#505450";
@colormap[21] = "#505450";
@colormap[22] = "#505450";
@colormap[23] = "#505450";
@colormap[24] = "#505450";
@colormap[25] = "#505450";
@colormap[26] = "#505450";
@colormap[27] = "#505450";
@colormap[28] = "#505450";
@colormap[29] = "#505450";
@colormap[30] = "#505450";
@colormap[31] = "#505450";

# Gray scale
$i=32;
$rb = 0;
$g = 0;
for ($x=0;$x<=23;$x++) {
	@colormap[$i] = sprintf("#%02x%02x%02x",$rb,$g,$rb);
	if ($i%2) {
		$rb += 8;
	} else {
		$rb += 16;
	}
	$g += 12;
	$i++;
}

# 5x8x5 fltk color cube

$x = 56;
for ($b = 0; $b <= 255; $b += 64) {
	if ($b == 64) {
		$b = 56;
	}
	for ($r = 0; $r <= 255; $r += 64) {
		if ($r == 64) {
			$r = 56;
		}
		for ($g = 0; $g <= 255; $g += 36) {
			@colormap[$x] = sprintf("#%02x%02x%02x",$r,$g,$b);
			$x++;
		}
	}
}
$svg = "<?xml version=\"1.0\" standalone=\"no\"?>\n";
$svg .= "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20000303 Stylable//EN\"\n";
$svg .= "\t\"http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd\">\n";
$svg .= "<svg width=\"50pt\" height=\"50pt\" viewBox=\"0 0 50 50\" xml:space=\"preserve\">\n";
$svg .= "<g transform=\"scale(0.5)\">\n";

foreach $line (@lines) {
	if ($line =~ /^#\W*(.*)?/) {
		$id = $1;
		next;
	}
	if ($line =~ /^\W*color\((.*)\)/) {
		$color = $1;
		if ($color eq "shadowcolor") {
			$color = "#C0C0C0";
			$opacity = "fill-opacity:50%;";
		} elsif ($color eq "outlinecolor") {
			$color = "#000000";
			$opacity = "fill-opacity:100%;";
		} elsif ($color eq "iconcolor") {
			$color = "#FFFFFF";
			$opacity = "fill-opacity:100%;";
		} elsif ($color eq "0") {
			$color = "#000000";
		} else {
			if ($color < 0)  {
				$color = -$color;
				$c1 = $color >> 4;
				$c2 = $color & 15;
				$c1 = @colormap[$c1];
				$c2 = @colormap[$c2];
				$r1 = hex substr $c1,1,2;
				$g1 = hex substr $c1,3,2;
				$b1 = hex substr $c1,5,2;
				$r2 = hex substr $c2,1,2;
				$g2 = hex substr $c2,3,2;
				$b2 = hex substr $c2,5,2;
				$r3 = $r1/2+$r2/2;
				$g3 = $g1/2+$g2/2;
				$b3 = $b1/2+$b2/2;
				$color = sprintf("#%02x%02x%02x",$r3,$g3,$b3);
				
			} else {
				$color = @colormap[$color];
			}
		}
		next;
	}
	if ($line =~ /^\W*bgnpolygon/) {
		$svg .= "\t<g id=\"$id\" style=\"fill-rule:nonzero;clip-rule:nonzero;fill:$color;stroke:none;stroke-miterlimit:4;$opacity\">\n";
		$svg .= "\t\t<path d=\"M";
		$startPath = 1;
		$sep = "L";
		$close = "z";
		next;
	}
	if ($line =~ /^\W*bgnoutlinepolygon/) {
		$svg .= "\t<g id=\"$id\" style=\"fill-rule:none;clip-rule:nonzero;fill:$color;stroke:{STROKECOLOR};stroke-miterlimit:4;$opacity\">\n";
		$svg .= "\t\t<path d=\"M";
		$startPath = 1;
		$sep = "L";
		$close = "z";
		next;
	}
	if ($line =~ /^\W*bgnline/) {
		$svg .= "\t<g id=\"$id\" style=\"fill-rule:none;clip-rule:nonzero;fill:none;stroke:$color;stroke-miterlimit:4;$opacity\">\n";
		$svg .= "\t\t<path d=\"M";
		$startPath = 1;
		$sep = "L";
		$close = "";
		next;
	}
	if ($line =~ /^\W*bgnclosedline/) {
		$svg .= "\t<g id=\"$id\" style=\"fill-rule:none;clip-rule:nonzero;fill:none;stroke:$color;stroke-miterlimit:4;$opacity\">\n";
		$svg .= "\t\t<path d=\"M";
		$startPath = 1;
		$sep = "L";
		$close = "z";
		next;
	}
	if ($line =~ /^\W*vertex\(([0-9\.]*),\W?([0-9\.]*)\);/) {
			$y = 100-$2;
		if ($startPath == 1) {
			$svg .= "$1 $y";
			$startPath=0
		} else {
			$svg .= "L$1 $y";
		}
		next;
	}
	if ($line =~ /^\W*endpolygon/) {
		$svg .= "$close\"/>\n\t</g>\n";
		next;
	}
	if ($line =~ /^\W*endoutlinepolygon\((.*)\);/) {
		$svg .= "$close\"/>\n\t</g>\n";
		$scol = $1;
		if ($scol eq "outlinecolor") {
			$scol = "#000000";
		} elsif ($scol eq "iconcolor") {
			$scol = "#FFFFFF";
		} else {
			if ($scol < 0)  {
				$scol = -$scol;
				$c1 = $scol >> 4;
				$c2 = $scol & 15;
				$scol = ($c1 + $c2)/2;
			}
			$scol = @colormap[$scol];
		}
		$svg =~ s/{STROKECOLOR}/$scol/g;
	}
	if ($line =~ /^\W*endline/) {
		$svg .= "$close\"/>\n\t</g>\n";
	}
	if ($line =~ /^\W*endclosedline/) {
		$svg .= "$close\"/>\n\t</g>\n";
	}
}

$svg .= "</g>\n";
$svg .= "</svg>";

open(SVG,">$svgfile");
print SVG $svg;
close(SVG);
