示例#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