Example #1
0
    def write(
        self,
        outfile,
        group,
        visible=(0.0, 0.0, 500.0, 500.0),
        size=(500, 500),
        bgcolor=(0, 0, 0),
        win=None,
        scale=(1.0, 1.0),
    ):
        """print out svg code"""

        # get camera properties
        if win != None:
            visible = win.get_visible()
            size = win.get_size()
            bgcolor = win.get_bgcolor()
        (x, y, x2, y2) = visible
        (width, height) = size

        # determine camera transform
        boxWidth = x2 - x
        boxHeight = y2 - y
        self.scalex = width / boxWidth
        self.scaley = height / boxHeight
        self.s = scale  # prescale

        self.out = outfile
        self.curcolor = [1, 1, 1, 1]

        print >> self.out, svgHeader
        print >> self.out, svgTag % (width, height)
        print >> self.out, "<g style='font-family: courier'>"
        print >> self.out, "<g transform='translate(0, %d)'>" % (height)
        print >> self.out, "<g transform='scale(1, -1)'>"
        print >> self.out, "<rect x='0' y='0' width='%d' height='%d' fill='%s'/>" % (
            width,
            height,
            color2string(bgcolor),
        )

        # transform camera
        print >> outfile, "<g transform='scale(%f, %f)'>" % (self.scalex / self.s[0], self.scaley / self.s[1])
        print >> outfile, "<g transform='translate(%d, %d)'>" % (self.s[0] * -x, self.s[1] * -y)
        print >> outfile, "<g stroke-width='%f'>" % (1 / max(self.scalex * self.s[0], self.scaley * self.s[1]))

        mat = transform.makeTransMatrix((x, y))
        self.trans.append(transform.multMatrix(mat, self.trans[-1]))
        mat = transform.makeScaleMatrix((self.scalex, self.scaley))
        self.trans.append(transform.multMatrix(mat, self.trans[-1]))

        self.printElm(group)

        print >> self.out, "</g></g></g>"
        print >> self.out, "</g></g></g>"
        print >> self.out, svgEndTag
Example #2
0
    def pushTransform(self, elm):

        c = elm.get()[1:]
        if isinstance(elm, translate):
            mat = transform.makeTransMatrix(c)

        elif isinstance(elm, scale):
            mat = transform.makeScaleMatrix(c)

        elif isinstance(elm, rotate):
            mat = transform.makeRotateMatrix(c[0], [0, 0])

        elif isinstance(elm, flip):
            mat = transform.makeFlipMatrix(c)

        else:
            mat = transform.makeIdentityMatrix()

        # should this be flipped?
        self.trans.append(transform.multMatrix(mat, self.trans[-1]))