flash shape to svg

Intro

I have made a script to convert a flash shape to svg (the easy way).

I hope it converts all flash shapes, because the shape I needed it for only contained “moveTo“, “splineTo” and “lineTo” functions.

Requirement

This script requires a dump from swftoolsswfdump:

swfdump -D test.swf > test.dump

The script

# Copyright 2011 Mathias Teugels. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice, this list of
#      conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright notice, this list
#      of conditions and the following disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY MATHIAS TEUGELS ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL  OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Mathias Teugels.

import sys

f = open(sys.argv[1])
l = f.readlines()
f.close()

res = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg2">"""

res += '<path d="'

for line in l:
    line = line.replace('\n', '').replace(')', '').replace('(', '')

    analyse = line.split(' ')[-5:]

    if analyse[0] == 'splineTo':
        res += "Q%s,%s %s,%s " % (analyse[1],analyse[2],analyse[3],analyse[4])
    else:
        analyse = analyse[2:]

        if analyse[0] == 'moveTo':
            res += "M%s,%s " % (analyse[1], analyse[2])

        elif analyse[0] == 'lineTo':
            res += "L%s,%s " % (analyse[1],analyse[2])

res += 'Z" style="fill:#000000"/>'
res += "</svg>"
print res

Usage

python shape2svg.py /path/to/your/dump > /path/to/your/svg

The svg will contain one element with a path that should create the same shape.

Feedback

If any shapes do not convert properly, I’d love to hear about it: Talk to me at identi.ca/cpf

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.