Example #1
0
 def test_translate_ctm(self):
     gc = basecore2d.GraphicsContextBase()
     ident = affine.affine_identity()
     x, y = 2.0, 3.0
     desired = affine.translate(ident, x, y)
     gc.translate_ctm(x, y)
     actual = gc.get_ctm()
     self.assertTrue(alltrue(ravel(actual == desired)))
 def test_translate_ctm(self):
     gc = basecore2d.GraphicsContextBase()
     ident = affine.affine_identity()
     x, y = 2., 3.
     desired = affine.translate(ident, x, y)
     gc.translate_ctm(x, y)
     actual = gc.get_ctm()
     self.assert_(alltrue(ravel(actual == desired)))
Example #3
0
 def test_trs_factor(self):
     trans = affine.affine_identity()
     trans = affine.translate(trans,5,5)
     trans = affine.rotate(trans,2.4)
     trans = affine.scale(trans,10,10)
     tx,ty,sx,sy,angle = affine.trs_factor(trans)
     assert( (tx,ty) == (5,5))
     assert( (sx,sy) == (10,10))
     assert( angle == 2.4)
Example #4
0
 def test_tsr_factor(self):
     trans = affine.affine_identity()
     trans = affine.translate(trans,6,5)
     trans = affine.scale(trans,0.2,10)
     trans = affine.rotate(trans,2.4)
     tx,ty,sx,sy,angle = affine.tsr_factor(trans)
     assert( (tx,ty) == (6,5))
     assert( (sx,sy) == (0.2,10))
     assert( angle == 2.4)
 def test_tsr_factor(self):
     trans = affine.affine_identity()
     trans = affine.translate(trans, 6, 5)
     trans = affine.scale(trans, 0.2, 10)
     trans = affine.rotate(trans, 2.4)
     tx, ty, sx, sy, angle = affine.tsr_factor(trans)
     assert ((tx, ty) == (6, 5))
     assert ((sx, sy) == (0.2, 10))
     assert (angle == 2.4)
Example #6
0
    def get_event_transform(self, event=None, suffix=""):
        transform = affine.affine_identity()

        if isinstance(self.component, Component):
            # If we have zoom enabled, scale events.  Since affine transforms
            # multiply from the left, we build up the transform from the
            # inside of the viewport outwards.
            if self.enable_zoom and self.zoom != 1.0:
                transform = affine.translate(transform, *self.view_position)
                transform = affine.scale(transform, 1/self.zoom, 1/self.zoom)
                transform = affine.translate(transform, -self.outer_position[0],
                                                        -self.outer_position[1])
            else:
                x_offset = self.view_position[0] - self.outer_position[0]
                y_offset = self.view_position[1] - self.outer_position[1]
                transform = affine.translate(transform, x_offset, y_offset)

        return transform
 def test_translate(self):
     a, b, c, d, tx, ty = 1, 2, 3, 4, 5, 6
     transform1 = affine.affine_from_values(a, b, c, d, tx, ty)
     translate_transform = array([[0, 0, 0], [0, 0, 0], [.5, 1.5, 1]])
     tot_transform = affine.translate(transform1, .5, 1.5)
     pt1 = array([1., -1., 1.])
     actual = dot(pt1, tot_transform)
     # this does the first transform and the translate separately
     desired = dot(dot(pt1, translate_transform), transform1)
     assert (alltrue((actual - desired) < 1e-6))
Example #8
0
    def get_event_transform(self, event=None, suffix=""):
        transform = affine.affine_identity()

        if isinstance(self.component, Component):
            # If we have zoom enabled, scale events.  Since affine transforms
            # multiply from the left, we build up the transform from the
            # inside of the viewport outwards.
            if self.enable_zoom and self.zoom != 1.0:
                transform = affine.translate(transform, *self.view_position)
                transform = affine.scale(transform, 1 / self.zoom,
                                         1 / self.zoom)
                transform = affine.translate(transform,
                                             -self.outer_position[0],
                                             -self.outer_position[1])
            else:
                x_offset = self.view_position[0] - self.outer_position[0]
                y_offset = self.view_position[1] - self.outer_position[1]
                transform = affine.translate(transform, x_offset, y_offset)

        return transform
Example #9
0
    def translate_ctm(self, tx, ty):
        """ Translates the coordinate system by the value given by (tx, ty)

            Parameters
            ----------
            tx : float
                The distance to move in the x direction
            ty : float
                The distance to move in the y direction
        """
        self.state.ctm = affine.translate(self.state.ctm, tx, ty)
        self.active_subpath.append((TRANSLATE_CTM, (tx, ty)))
        self.path_transform_indices.append(len(self.active_subpath)-1)
Example #10
0
    def translate_ctm(self, tx, ty):
        """ Translates the coordinate system by the value given by (tx, ty)

            Parameters
            ----------
            tx : float
                The distance to move in the x direction
            ty : float
                The distance to move in the y direction
        """
        self.state.ctm = affine.translate(self.state.ctm, tx, ty)
        self.active_subpath.append((TRANSLATE_CTM, (tx, ty)))
        self.path_transform_indices.append(len(self.active_subpath) - 1)
Example #11
0
 def test_translate(self):
     a,b,c,d,tx,ty = 1,2,3,4,5,6
     transform1 = affine.affine_from_values(a,b,c,d,tx,ty)
     translate_transform = array([[0,0,0],
                                  [0,0,0],
                                  [.5,1.5,1]])
     tot_transform = affine.translate(transform1,.5,1.5)
     pt1 = array([1.,-1.,1.])
     actual = dot(pt1,tot_transform)
     # this does the first transform and the translate separately
     desired = dot(dot(pt1,translate_transform),
                              transform1)
     assert(alltrue( (actual - desired) < 1e-6 ))
    def test_transform_point(self):
        pt = array((1, 1))
        ctm = affine.affine_identity()
        ctm = affine.translate(ctm, 5, 5)
        new_pt = affine.transform_point(ctm, pt)
        assert (alltrue(new_pt == array((6, 6))))

        ctm = affine.rotate(ctm, pi)
        new_pt = affine.transform_point(ctm, pt)
        assert (sum(new_pt - array((4., 4.))) < 1e-15)

        ctm = affine.scale(ctm, 10, 10)
        new_pt = affine.transform_point(ctm, pt)
        assert (sum(new_pt - array((-5., -5.))) < 1e-15)
Example #13
0
    def test_transform_point(self):
        pt = array((1,1))
        ctm = affine.affine_identity()
        ctm = affine.translate(ctm,5,5)
        new_pt = affine.transform_point(ctm, pt)
        assert(alltrue(new_pt == array((6,6))))

        ctm = affine.rotate(ctm,pi)
        new_pt = affine.transform_point(ctm, pt)
        assert(sum(new_pt - array((4.,4.))) < 1e-15)

        ctm = affine.scale(ctm,10,10)
        new_pt = affine.transform_point(ctm, pt)
        assert(sum(new_pt - array((-5.,-5.))) < 1e-15)
    def test_transform_points(self):
        # not that thorough...
        pt = array(((1, 1), ))
        ctm = affine.affine_identity()
        ctm = affine.translate(ctm, 5, 5)
        new_pt = affine.transform_points(ctm, pt)
        assert (alltrue(new_pt[0] == array((6, 6))))

        ctm = affine.rotate(ctm, pi)
        new_pt = affine.transform_points(ctm, pt)
        assert (sum(new_pt[0] - array((4., 4.))) < 1e-15)

        ctm = affine.scale(ctm, 10, 10)
        new_pt = affine.transform_points(ctm, pt)
        assert (sum(new_pt[0] - array((-5., -5.))) < 1e-15)
Example #15
0
    def test_transform_points(self):
        # not that thorough...
        pt = array(((1,1),))
        ctm = affine.affine_identity()
        ctm = affine.translate(ctm,5,5)
        new_pt = affine.transform_points(ctm, pt)
        assert(alltrue(new_pt[0] == array((6,6))))

        ctm = affine.rotate(ctm,pi)
        new_pt = affine.transform_points(ctm, pt)
        assert(sum(new_pt[0] - array((4.,4.))) < 1e-15)

        ctm = affine.scale(ctm,10,10)
        new_pt = affine.transform_points(ctm, pt)
        assert(sum(new_pt[0] - array((-5.,-5.))) < 1e-15)
Example #16
0
    def device_transform_device_ctm(self, func, args):
        """ Default implementation for handling scaling matrices.

            Many implementations will just use this function.  Others, like
            OpenGL, can benefit from overriding the method and using
            hardware acceleration.
        """
        if func == SCALE_CTM:
            self.device_ctm = affine.scale(self.device_ctm, args[0], args[1])
        elif func == ROTATE_CTM:
            self.device_ctm = affine.rotate(self.device_ctm, args[0])
        elif func == TRANSLATE_CTM:
            self.device_ctm = affine.translate(self.device_ctm, args[0],
                                               args[1])
        elif func == CONCAT_CTM:
            self.device_ctm = affine.concat(self.device_ctm, args[0])
        elif func == LOAD_CTM:
            self.device_ctm = args[0].copy()
Example #17
0
    def device_transform_device_ctm(self, func, args):
        """ Default implementation for handling scaling matrices.

            Many implementations will just use this function.  Others, like
            OpenGL, can benefit from overriding the method and using
            hardware acceleration.
        """
        if func == SCALE_CTM:
            self.device_ctm = affine.scale(self.device_ctm, args[0], args[1])
        elif func == ROTATE_CTM:
            self.device_ctm = affine.rotate(self.device_ctm, args[0])
        elif func == TRANSLATE_CTM:
            self.device_ctm = affine.translate(self.device_ctm, args[0],
                                               args[1])
        elif func == CONCAT_CTM:
            self.device_ctm = affine.concat(self.device_ctm, args[0])
        elif func == LOAD_CTM:
            self.device_ctm = args[0].copy()
Example #18
0
    def device_draw_image(self, img, rect):
        """
        draw_image(img_gc, rect=(x,y,w,h))

        Draws another gc into this one.  If 'rect' is not provided, then
        the image gc is drawn into this one, rooted at (0,0) and at full
        pixel size.  If 'rect' is provided, then the image is resized
        into the (w,h) given and drawn into this GC at point (x,y).

        img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's
        Agg backend (kiva.agg.GraphicsContextArray).

        Requires the Python Imaging Library (PIL).
        """
        from PIL import Image as PilImage

        # We turn img into a PIL object, since that is what ReportLab
        # requires.  To do this, we first determine if the input image
        # GC needs to be converted to RGBA/RGB.  If so, we see if we can
        # do it nicely (using convert_pixel_format), and if not, we do
        # it brute-force using Agg.


        if type(img) == type(array([])):
            # Numeric array
            converted_img = agg.GraphicsContextArray(img, pix_format='rgba32')
            format = 'RGBA'
        elif isinstance(img, agg.GraphicsContextArray):
            if img.format().startswith('RGBA'):
                format = 'RGBA'
            elif img.format().startswith('RGB'):
                format = 'RGB'
            else:
                converted_img = img.convert_pixel_format('rgba32', inplace=0)
                format = 'RGBA'
            # Should probably take this into account
            # interp = img.get_image_interpolation()
        else:
            warnings.warn("Cannot render image of type %r into SVG context."
                          % type(img))
            return

        # converted_img now holds an Agg graphics context with the image
        pil_img = PilImage.fromstring(format,
                                      (converted_img.width(),
                                       converted_img.height()),
                                      converted_img.bmp_array.tostring())
        if rect == None:
            rect = (0, 0, img.width(), img.height())

        left, top, width, height = rect
        if width != img.width() or height != img.height():
            # This is not strictly required.
            pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST)

        png_buffer = StringIO()
        pil_img.save(png_buffer, 'png')
        b64_img_data = b64encode(png_buffer.getvalue())
        png_buffer.close()

        # Draw the actual image.
        m = self.get_ctm()
        # Place the image on the page.
        # Using bottom instead of top here to account for the y-flip.
        m = affine.translate(m, left, height + top)
        transform = 'matrix(%f,%f,%f,%f,%f,%f) scale(1,-1)' % affine.affine_params(m)
        # Flip y to reverse the flip at the start of the document.
        image_data = 'data:image/png;base64,' + b64_img_data
        self._emit('image', transform=transform,
                   width=str(width), height=str(height),
                   preserveAspectRatio='none',
                   kw={ 'xlink:href': image_data })