Skip to content

Commit

Permalink
Added mixin for exporting object names as classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomassalo committed Jan 17, 2016
1 parent 1f1bf68 commit d4ba101
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bin/swf2svg.py
@@ -1,6 +1,6 @@
import argparse
from swf.movie import SWF
from swf.export import SVGExporter, SingleShapeSVGExporterMixin, FrameSVGExporterMixin
from swf.export import SVGExporter, SingleShapeSVGExporterMixin, FrameSVGExporterMixin, NamesSVGExporterMixin

parser = argparse.ArgumentParser(description="Convert an SWF file into an SVG")
parser.add_argument("--swf", type=argparse.FileType('rb'),
Expand All @@ -11,6 +11,8 @@
help="Only export shape SHAPE (integer)", required=False)
parser.add_argument("--frame", type=int,
help="Export frame FRAME (0-based index) instead of frame 0", required=False)
parser.add_argument("--names", action='store_true',
help='For each element, extract SWF instanceName to class="n-<name>"', required=False)

options = parser.parse_args()
argparse.swf_file = options.swf
Expand All @@ -32,6 +34,10 @@
export_mixins.append(FrameSVGExporterMixin)
export_opts['frame'] = options.frame

if options.names:
export_mixins.append(NamesSVGExporterMixin)


# create the SVG exporter
svg_exporter = SVGExporter()

Expand Down
13 changes: 13 additions & 0 deletions swf/export.py
Expand Up @@ -899,12 +899,25 @@ def get_display_tags(self, tags, z_sorted=True):

if not tag.hasCharacter:
tag.characterId = orig_tag.characterId
# this is for NamesSVGExporterMixin
if not tag.hasName:
tag.instanceName = orig_tag.instanceName
frame_tags[tag.depth] = tag
elif isinstance(tag, TagRemoveObject):
del frame_tags[tag.depth]

return super(FrameSVGExporterMixin, self).get_display_tags(frame_tags.values(), z_sorted)

class NamesSVGExporterMixin(object):
'''
Add class="n-<name>" to SVG elements for tags that have an instanceName.
'''
def export_display_list_item(self, tag, parent=None):
use = super(NamesSVGExporterMixin, self).export_display_list_item(tag, parent)
if hasattr(tag, 'instanceName') and tag.instanceName is not None:
use.set('class', 'n-%s' % tag.instanceName)
return use


class SVGFilterFactory(object):
# http://commons.oreilly.com/wiki/index.php/SVG_Essentials/Filters
Expand Down

0 comments on commit d4ba101

Please sign in to comment.