Beispiel #1
0
    def getAABB(self):
        aabb = AABB()

        lastX = 0
        lastY = 0

        # to acces values with simplepath format
        (x, y) = range(-2, 0)

        if self.tag == addNS('path', 'svg'):
            blocks = Factory().create('path_parser').parse(self.get('d'))
            for vertex in blocks:
                for (cmd, values) in vertex:
                    if values is not None:
                        if cmd == 'C':
                            aabb.addBezier((lastX, lastY), values)
                        elif cmd == 'A':
                            aabb.addArc((lastX, lastY), values)
                        else:
                            aabb.addPoint(values[x], values[y])

                        lastX = values[x]
                        lastY = values[y]

        elif self.tag in [addNS('rect', 'svg'), addNS('image', 'svg')]:
            x = float(self.get('x'))
            y = float(self.get('y'))
            width = float(self.get('width'))
            height = float(self.get('height'))
            aabb.addPoint(x, y)
            aabb.addPoint(x + width, y + height)

        elif self.tag == addNS('use', 'svg'):
            x = float(self.get('x'))
            y = float(self.get('y'))
            # the first character of an href is a #
            imageId = self.get(addNS('href', 'xlink'))[1:]
            image = self.svg.getImage(imageId)
            width = float(image.get('width'))
            height = float(image.get('height'))
            aabb.addPoint(x, y)
            aabb.addPoint(x + width, y + height)

        else:
            raise Exception("Can't get AABB of a node which is neither a path \
    nor a rect.\nnode tag:%s" % self.tag)

        return aabb
Beispiel #2
0
class Element:
    def __init__(self, **kwargs):
        self._id = kwargs['_id']
        self.infos = kwargs['infos']
        self.pathsInfos = kwargs['vertex']
        self.matrix = getValue(kwargs, 'matrix')
        if isIdentity(self.matrix):
            self.matrix = None
        self.content = []
        self.aabb = AABB()

        # added by writeContent
        self.ratio = 1.0
        self.newWidth = 1.0
        self.newHeight = 1.0

    def pointInLevelSpace(self, x, y):
        return x - self.newWidth / 2, -y + self.newHeight / 2

    def keepOnlyXY(self):
        """ keep only x and y from the (cmd, values) in vertex
        with simplepath, (x, y) are the two last in values
        """
        newBlocks = []
        for vertex in self.blocks:
            vertex = [(values[-2], values[-1]) for cmd, values in vertex]
            newBlocks.append(vertex)

        self.blocks = newBlocks

    def transform(self):
        newBlocks = []
        for vertex in self.blocks:
            if self.matrix is not None:
                # apply transform
                vertex = [self.matrix.applyOnPoint(x, y) for x, y in vertex]
            # apply ratio
            vertex = [(x * self.ratio, y * self.ratio) for x, y in vertex]

            newBlocks.append(vertex)

        self.blocks = newBlocks

    def addToAABB(self):
        for vertex in self.blocks:
            # add point to the aabb
            [self.aabb.addPoint(x, y) for x, y in vertex]

    def preProcessVertex(self):
        """ apply transformations on block vertex
            and add them to the bounding box of the block """
        self.blocks = Factory().create('path_parser').parse(self.pathsInfos)
        self.keepOnlyXY()
        self.transform()
        self.addToAABB()

    def addElementParams(self):
        for key, value in self.infos.iteritems():
            if type(value) == dict:
                if key.startswith('_'):
                    continue
                elif key == 'param':
                    for key, value in value.iteritems():
                        line = "\t\t<param name=\"%s\" value=\"%s\"/>"
                        self.content.append(line % (key, value))
                else:
                    xmlLine = "\t\t<%s" % key
                    for key, value in value.iteritems():
                        if key.startswith('_'):
                            continue
                        xmlLine += " %s=\"%s\"" % (key, value)
                    xmlLine += "/>"
                    self.content.append(xmlLine)