#!/usr/bin/perl -w # Convert Illustrator file to PHP/Ming code # (c) editions Systeme D, June 2005 # rewritten from ai2pdf code # Should alert quite visibly on 'n' # ----- Process file use Math::Bezier::Convert qw(:all); $shiftx =-10; # -4.5 for pen $shifty =-10; $precision=2; open (OUTFILE, ">icons_output.pl") or die "Can't open output file: $!\n"; foreach $filename (@ARGV) { $iconname=$filename; $iconname=~s/\.ai$//; print "$iconname\n"; $/="\015"; $scale=20; # 20 usually? 9 for pen $left=32767; $right=-32767; $top=-32767; $bottom=32767; $inline=''; open (INFILE, $filename) or die "Can't open file: $!\n"; while ($inline ne '%%EndSetup') { chomp ($inline=); } while () { chomp ($inline=$_); $inline=~s/\012/ /g; # print "$inline\n"; if ($inline=~/([\d\.]+) g/) { # set fill grey $fr=C_Col($1); $fg=C_Col($1); $fb=C_Col($1); # | } elsif ($inline=~/([\d\.]+) G/) { # set stroke grey $sr=C_Col($1); $sg=C_Col($1); $sb=C_Col($1); # | } elsif ($inline=~/([\d\.]+) ([\d\.]+) m/) { # moveto push @m, "movePenTo($1,$2);"; # | Update_Maxima($1,$2); # | } elsif ($inline=~/(^[\d\.]+) ([\d\.]+) [lL]$/) { # lineto push @m, "drawLineTo($1,$2);"; # | Update_Maxima($1,$2); # | } elsif ($inline=~/([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) [cC]/) { # curveto C push @m, "curveSegment($1,$2,$3,$4,$5,$6);"; # | Update_Maxima($5,$6); # | } elsif ($inline=~/([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) [vV]/) { # curveto V push @m, "curveSegment($x,$y,$1,$2,$3,$4);"; # | Update_Maxima($3,$4); # | } elsif ($inline=~/([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) [yY]/) { # curveto Y push @m, "curveSegment($1,$2,$3,$4,$3,$4);"; # | Update_Maxima($3,$4); # | } elsif ($inline=~/([\d\.]+) w/) { # width $sw=$1; # | } elsif (uc $inline eq 'F') { # fill push @m, "fill($fr,$fg,$fb);" #Ê | } elsif (uc $inline eq 'S') { # stroke push @m, "stroke($sw,$sr,$sg,$sb);" #Ê | } elsif (uc $inline eq 'B') { # fill and stroke push @m, "fillStroke($fr,$fg,$fb,$sw,$sr,$sg,$sb);" #Ê | } elsif ($inline=~/^([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) Xa/) { # CMYK RGB fill $fr=C_Col($5); $fg=C_Col($6); $fb=C_Col($7); # | } elsif ($inline=~/^([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) XA/) { # CMYK RGB stroke $sr=C_Col($5); $sg=C_Col($6); $sb=C_Col($7); # | } elsif ($inline=~/^\d A$/) { # ignore locked } elsif ($inline=~/^\d O$/) { # ignore overprint } elsif ($inline=~/^\d R$/) { # | } elsif ($inline=~/^\d D$/) { # ignore winding order } elsif ($inline=~/^\d Ap$/) { # ignore centre point } elsif ($inline=~/^\d XR$/) { # ignore fill path by rule } elsif ($inline=~/^\(.+\) Ln$/) { # ignore layer name } elsif ($inline=~/ Lb$/) { # ignore begin layer } elsif ($inline eq 'LB') { # | } elsif ($inline=~/^gsave/) { # ignore page save } elsif ($inline=~/^Adobe_Illustrator/) { # ignore Illustrator stuff } elsif ($inline=~/([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) k/) { # set fill colour print "# CMYK not supported ($inline)\n"; #Ê | if ($4<0.01) { $fr=$fg=$fb=255; } else { $fr=$fg=$fb=0; } # | } elsif ($inline=~/([\d\.]+) ([\d\.]+) ([\d\.]+) ([\d\.]+) K/) { # set stroke colour print "# CMYK not supported ($inline)\n"; #Ê | if ($4<0.01) { $sr=$sg=$sb=255; } else { $sr=$sg=$sb=0; } # | } elsif ($inline=~/^\*?[uU]/) { # warn if compound paths print "# warning: contains compound path ($inline)\n"; # | } elsif ($inline=~/^\%/) { # ignore comments } else { # (unsupported) print "# unsupported: $inline\n"; # | } } close INFILE; # exit; # ----- Output file $scale=$scale/($right-$left); $action=''; print OUTFILE "# ------ $iconname sprite\n\n"; # print OUTFILE "\$st=new SWF::Sprite();\n"; # print OUTFILE "\$spbutton=new SWF::Button();\n"; print OUTFILE "\$ec=new SWF::Sprite();\n"; foreach $line (@m) { if ($line=~/^(\w+)\((.*)\);$/) { $cmd=$1; @p=split(/,/,$2); if ($cmd eq 'movePenTo') { $xcurs=$p[0]; $ycurs=$p[1]; $action='move'; } elsif ($cmd eq 'drawLineTo') { Flush_Curve(); if ($action eq 'move') { Flush_Move(); } $xcurs=$p[0]; $x=Convert_X($p[0]); $ycurs=$p[1]; $y=Convert_Y($p[1]); push (@shape,"\$s->drawLineTo($x,$y);"); $action='draw'; } elsif ($cmd eq 'curveSegment') { push (@cubic, $p[0]); push (@cubic, $p[1]); push (@cubic, $p[2]); push (@cubic, $p[3]); push (@cubic, $p[4]); push (@cubic, $p[5]); } elsif ($cmd eq 'fill') { Flush_Curve(); unshift(@shape,"\$s->setRightFill($p[0],$p[1],$p[2]);"); End_Shape(); } elsif ($cmd eq 'stroke') { $p[0]=C_Width($p[0]); unshift(@shape,"\$s->setLine($p[0],$p[1],$p[2],$p[3]);"); Flush_Curve(); End_Shape(); } elsif ($cmd eq 'fillStroke') { Flush_Curve(); $p[3]=C_Width($p[3]); unshift(@shape,"\$s->setRightFill($p[0],$p[1],$p[2]);"); unshift(@shape,"\$s->setLine($p[3],$p[4],$p[5],$p[6]);"); End_Shape(); } } else { print "# wrong command\n"; } } @m=(); print OUTFILE "\n"; print OUTFILE '$ec->nextFrame(); $m->addExport($ec,"'; print OUTFILE "${iconname}\");\n\n"; # print OUTFILE '$st->add($spbutton); $st->nextFrame(); $m->addExport($st,"'; # print OUTFILE "${iconname}\");\n\n"; } system ('open icons_output.pl'); # =========== # Subroutines # ----- Update_Maxima(x,y) sub Update_Maxima { $x=$_[0]; $y=$_[1]; if ($x<$left ) { $left =$x; } if ($x>$right ) { $right =$x; } if ($y<$bottom) { $bottom=$y; } if ($y>$top ) { $top =$y; } } # ----- Flush_Curve sub Flush_Curve { unless (@cubic) { return; } unshift (@cubic,$ycurs); undef $ycurs; unshift (@cubic,$xcurs); undef $xcurs; # foreach $t (@cubic) { print $t.', '; }; print "\n"; @curve=cubic_to_quadratic(@cubic); @cubic=(); $x=Convert_X($curve[0]); $y=Convert_Y($curve[1]); if ($action eq 'move') { push (@shape,"\$s->movePenTo($x,$y);"); } for ($i=2; $idrawCurveTo($x1,$y1,$x2,$y2);"); } $action=''; } # ----- Flush_Move sub Flush_Move { $x=Convert_X($xcurs); undef $xcurs; $y=Convert_Y($ycurs); undef $ycurs; push (@shape,"\$s->movePenTo($x,$y);"); } # ----- Convert_X(x),Convert_Y(y) # C_Col(colour),C_Width(stroke width) sub Convert_X { return sprintf("%.${precision}f",($_[0]-$left)*$scale+$shiftx); } sub Convert_Y { return sprintf("%.${precision}f",($top-$_[0] )*$scale+$shifty); } sub C_Col { return sprintf("%.0f",($_[0]*255)); } sub C_Width { return sprintf("%.${precision}f",($_[0]*$scale)); } # ----- End_Shape sub End_Shape { print OUTFILE "\n\$s=new SWF::Shape();\n"; foreach $shapeline (@shape) { print OUTFILE "$shapeline\n"; } # print OUTFILE "\$spbutton->addShape(\$s,SWFBUTTON_HIT | SWFBUTTON_UP | SWFBUTTON_DOWN | SWFBUTTON_OVER);\n"; print OUTFILE "\$ec->add(\$s);\n"; @shape=(); }