def __init__(self, t, ct, objectId, depth, name, clipDepth):
        super(Place, self).__init__()

        self.id = objectId
        self.depth = depth
        self.swf_depth = 1
        self.clipDepth = clipDepth 
        self.name = name
        self.visible = True
        self.symbols = []

        self.sx = None
        self.sy = None
        self.wx = None
        self.wy = None
        self.tx = None
        self.ty = None

        if len(t) > 0:
            self.sx,self.sy,self.wx,self.wy,self.tx,self.ty=tuple(LUtil.get_values_as_float(t[0],'scaleX','scaleY','skewX','skewY','transX','transY'))

        self.ctf = []

        if len(ct) > 0:
            factors = LUtil.get_colortrans(ct[0], 'factor')
            offsets = LUtil.get_colortrans(ct[0], 'offset')

            self.ctf = factors + offsets
    def _get_styles_gradient(self, e, objectId):
        stops = []
        gclr = e.find('gradientColors')
        for gc in list(gclr):
            position = gc.get('position')
            c = gc.find('color/Color')
            color = LUtil.get_colortrans(c)
            stop = Stop(color, position)
            stops.append(stop)

        gtf = e.find('matrix/Transform')
        linearGrad = LinearGradient(objectId, gtf, stops)

        return linearGrad
    def _build_tree(self, scale_factor=1.0):

        stack = []
        
        # build tree structure
        root = Tree()
        root.key = "root"
        root.sx = 1.0 / scale_factor
        root.sy = 1.0 / scale_factor
        
        for k, v in self.places.iteritems():
            tree = Tree()
            tree.set_items(v.items())
            tree.key = "-obj" + k + "-1-1"
            tree.parent = root
            root.children.append(tree)
            stack.append(tree)

        key_count = {}
        
        while len(stack) > 0:
            current = stack.pop()
            currentSprite = self._search_sprite(LUtil.objectID_from_key(current.key))
            #currentSprite = self._search_sprite(current.key)
            if currentSprite is not None:
                f = currentSprite.frames[0]
                for p in f.places:
                    for s in p['symbols']:

                        if s in key_count.keys():
                            key_count[s] = key_count[s] + 1
                        else:
                            key_count[s] = 1
                            
                        tree = Tree()
                        tree.set_items(p.items())
                        tree.key = s + "-" + str(key_count[s])

                        #tree.key = LUtil.objectID_from_key(s)
                        tree.parent = current
                        current.children.append(tree)
                        stack.append(tree)

        self.tree = root
    def _make_shape(self, tree, shape):
        shapes = []

        if LUtil.objectID_from_key(tree.key) == shape.symbol:
            s = etree.Element("shape")

            s.set("key", tree.key)
            s.set("name", shape.name)
            s.set("x", str(shape.left))
            s.set("y", str(shape.top))
            s.set("width", str(shape.width))
            s.set("height", str(shape.height))
            
            shapes.append(s)

        for c in tree.children:
            c_shapes = self._make_shape(c, shape)

            shapes.extend(c_shapes)

        return shapes
        def make_shape_element(cls, shape, ctf, parent_key):

            group = etree.Element("g")
            group.set("id", shape.symbol + "shape")

            for e in shape.edges:

                # line colors
                linecolors = e.get_colors_from_category('l')
                for i, lineColor in zip(xrange(len(linecolors)), linecolors):
                    allpath = []

                    pathElm = Path('line')
                    path = cls._reverse(e.get_path(i, 'line'))
                    if len(path) > 0:
                        if len(e.colors) > 0: 
                            allpath.extend(path)
                            pathElm.set("stroke-width", str(e.lineWidths[i]))
                            pathElm.set("stroke", LUtil.rgb_to_hex(lineColor['l']))
                    merged_allpath_list = cls._merge_path(allpath)

                    all_data = ""

                    for merged_allpath in merged_allpath_list:
                        data = cls._path_data(merged_allpath)
                        all_data += data

                    if len(all_data) > 0:
                        pathElm.set("d", all_data)
                        group.append(pathElm)

                # solid colors
                solidcolors = e.get_colors_from_category('s')
                for i, color in zip(xrange(len(solidcolors)), solidcolors):

                    allpath = []

                    path = e.get_path(i, 'left')
                    if len(path) > 0:
                        allpath.extend(path)

                    path = cls._reverse(e.get_path(i, 'right'))
                    if len(path) > 0:
                        allpath.extend(path)

                    merged_allpath_list = cls._merge_path(allpath)

                    pathElm = Path()
                    clr = color['s']
                    for colorTransform in ctf:
                        if isinstance(clr, tuple):
                            clr = PUtil.colortrans(shape.symbol, color['s'], colorTransform)
                            clr = PUtil.colortrans(parent_key, clr, colorTransform)
                        else: # gradient
                            opacity = PUtil.colortrans_only_alpha(parent_key, colorTransform)/256
                            if opacity > 0.0:
                                pathElm.set("fill-opacity", str(opacity))

                    if isinstance(clr, tuple):
                        pathElm.set("fill", LUtil.rgb_to_hex(clr))
                        if clr[3] < 256:
                            opacity = float(clr[3])/256
                            if opacity > 0.0:
                                pathElm.set("fill-opacity", str(opacity))
                    else:
                        pathElm.set("fill", clr)

                    all_data = ""

                    for merged_allpath in merged_allpath_list:
                        data = cls._path_data(merged_allpath)
                        all_data += data

                    if len(all_data) > 0:
                        pathElm.set("d", all_data)
                        group.append(pathElm)

            if len(ctf) > 0:
                ctf.pop(0)

            return group
    def _proc_define_sprite(self, e, key_prefix=''):
        objectID = e.get("objectID")
        if objectID is None:
            return

        sprite = Sprite()
        sprite.symbol = "obj" + objectID

        has_do_action = False

        depth_table = {}
        po_table = {}

        frame_list = [] 
        fr = {}
        for p in e.xpath("./tags/*"):
            if p.tag == "DoAction":
                has_do_action = True
                break
            elif p.tag == "PlaceObject2":
                depth = int(p.get('depth'))
                placeObjectID = p.get("objectID")
                if not depth in depth_table:
                    depth_table[depth] = placeObjectID
                    po_table[depth] = self._get_place_object(p)

                fr[depth] = True
            elif p.tag == "RemoveObject2":
                depth = int(p.get('depth'))
                fr[depth] = False
            elif p.tag == "ShowFrame":
                for depth in depth_table.iterkeys():
                    if not depth in fr:
                        fr[depth] = frame_list[-1][depth]

                frame_list.append(fr)
                fr = {}
        for i, f in enumerate(frame_list):
            if len(f) < len(depth_table):
                pids = [(depth, False) for depth in depth_table.iterkeys() if not depth in f]
                frame_list[i].update(pids)

        frame = Frame()
        for p in e.xpath("./tags/*"):
            if p.tag == "PlaceObject2":

                po = self._get_place_object(p)

                depth = po['depth']
                placeObjectID = po["id"]

                if placeObjectID is not None:
                    symbols = [LUtil.make_key_string("obj" + placeObjectID, prefix=key_prefix, suffix='%d'%depth)]
                else:
                    symbols = sprite.searchSymbols(depth)

                poMatrixArray = [po[key] for key in self.MATRIX_KEY]
                if set(poMatrixArray) == set([None]):
                    logging.debug('including only ColorTransform animation')
                    places = [place for place in sprite.frames[-1].places if place.depth == depth]
                    if len(places) > 0:
                        p_temp = places[0]
                        po.items().update(dict((k, p_temp[k]) for k in self.MATRIX_KEY if p_temp[k] is not None))
                frame.addPlace(dict((k, po[k]) for k in self.MATRIX_KEY+self.PLACE_KEY), symbols, po['name'])

            elif p.tag == "ShowFrame":
                depth_list = [place.depth for place in frame.places]
                for depth, placeObjectID in depth_table.iteritems():
                    if not depth in depth_list and not frame_list[len(sprite.frames)][depth]:
                        symbols = [LUtil.make_key_string("obj" + placeObjectID, prefix=key_prefix, suffix='%d'%depth)]
                        po = po_table[depth]
                        frame.addPlace(dict((k, po[k]) for k in self.MATRIX_KEY+self.PLACE_KEY), symbols, None, frame_list[len(sprite.frames)][depth])
                sprite.addFrame(frame)
                frame = Frame()
                # doaction is not implemented
                if has_do_action:
                    break

        self.sprites[objectID] = sprite
 def __init__(self, clr, position, attrib=None, nsmap=None,  **_extra):
     super(Stop, self).__init__(attrib=None, nsmap=None, **_extra)
     self.tag = 'stop'
     self.set('stop-color'  , LUtil.rgb_to_hex(clr))
     self.set('stop-opacity', str(float(clr[3]/255)))
     self.set('offset'      , str(float(position)/255))
 def _make_gradient(self, gtf):
     transform = Transform()
     transform.set_items(dict(zip(transform.MATRIX, LUtil.get_transform_matrix(gtf))))
     return transform.get_matrix()