Jump to content

User:Gringer/swfxml2svg

From Wikipedia, the free encyclopedia
#!/usr/bin/perl
use warnings;
use strict;

# swfxml2svg.pl -- extracts primitive shapes from SWFmill XML file
# Author: David Eccles (gringer) <programming@gringer.org>

#  -- Begin GPL license blurb --

# Copyright 2010 David Eccles (gringer)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#  -- End GPL license blurb --

# The most recent version of this code can be found at  
# http://en.wikipedia.org/wiki/User:Gringer/swfxml2svg


# The proper way to do this would be using XML parsers and SVG encoders 
# (e.g. see http://en.wikipedia.org/wiki/User:Gringer/perlshaper)

# All shapes are given a black stroke and red fill

my $oldX = 0;
my $oldY = 0;
my $oldShapeX = 0;
my $oldShapeY = 0;
my $continue = 0;
my $midPath = 0;

my $fontID = "";

my $doPrint = 0;

while(<>){
    if(/<swf/){
        print("<svg>\n");
        $doPrint = 1;
    }
    if(/DefineFont3 objectID="([0-9\-a-z]+)"/){
        $fontID = $1;
    }
    if(/Glyph map="([0-9\-a-z]+)"/){
        print(" <g id=\"font${fontID}glyph$1\">\n");
    }
    if(/\/Glyph>/){
        print(" </g>\n");
    }
    if(/ShapeSetup x="([0-9\-]+)" y="([0-9\-]+)"/){
        my $xpos = 0 + $1 / 20;
        my $ypos = 0 + $2 / 20;
        $oldX = $xpos;
        $oldY = $ypos;
        if($midPath){
            print("Z M$xpos,$ypos");
        } else {
            $oldShapeX = $xpos;
            $oldShapeY = $ypos;
            print("  <path stroke=\"black\" ".
                    "fill=\"red\" d=\"M$xpos,$ypos");
        }
        $midPath = 1;
    }
    if(/ShapeSetup\//){
        if($midPath){
            print("\" />\n");
        }
        $midPath = 0;
    }
    if(/LineTo x="([0-9\-]+)" y="([0-9\-]+)"/){
        my $xpos = $oldX + $1 / 20;
        my $ypos = $oldY + $2 / 20;
        $oldX = $xpos;
        $oldY = $ypos;
        print(" L$xpos,$ypos");
    }
    if(/CurveTo x1="([0-9\-]+)" y1="([0-9\-]+)" x2="([0-9\-]+)" y2="([0-9\-]+)"/){
        my $x1pos = $oldX + $1 / 20;
        my $y1pos = $oldY + $2 / 20;
        $oldX = $x1pos;
        $oldY = $y1pos;
        my $x2pos = $oldX + $3 / 20;
        my $y2pos = $oldY + $4 / 20;
        $oldX = $x2pos;
        $oldY = $y2pos;
        print(" Q$x1pos,$y1pos $x2pos,$y2pos");
    }
    if(/<\/swf/){
        print("</svg>\n");
    }
}

A text version of the GPL can be found here