Exemplo n.º 1
0
    def save(self, filename, format='auto', backend='auto', backend_options={}):
        if format == 'auto':
            format = filename.split('.')[-1]
        if backend == 'auto':
            if format == 'png':
                backend = 'image'
            else:
                backend = 'cairo'

        # Create the backend instance
        if backend == 'image':
            backend = MathtexBackendImage()
        elif backend == 'cairo':
            if not HAVE_CAIRO_BACKEND:
                raise RuntimeError("Cairo backend requested when not available.")
            backend = MathtexBackendCairo()

        # Set the options for the backend
        backend.options = backend_options

        # Render!
        self.render_to_backend(backend)

        # Save
        backend.save(filename, format)
Exemplo n.º 2
0
    def as_rgba_bitmap(self):
        """
        Renders the expression to an RGBA bitmap using the Image backend and
        returns it.
        """
        backend = MathtexBackendImage()
        self.render_to_backend(backend)

        return backend.as_rgba()
Exemplo n.º 3
0
    def as_rgba_bitmap(self):
        """
        Renders the expression to an RGBA bitmap using the Image backend and
        returns it.
        """
        backend = MathtexBackendImage()
        self.render_to_backend(backend)

        return backend.as_rgba()
Exemplo n.º 4
0
    def as_mask(self):
        """
        Renders the expression to an alpha mask using the Image backend.
        The result is returned as a numpy array.
        """
        backend = MathtexBackendImage()
        self.render_to_backend(backend)

        return backend.as_mask()
Exemplo n.º 5
0
    def as_mask(self):
        """
        Renders the expression to an alpha mask using the Image backend.
        The result is returned as a numpy array.
        """
        backend = MathtexBackendImage()
        self.render_to_backend(backend)

        return backend.as_mask()
Exemplo n.º 6
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        if not HAVE_MATHTEX:
            return

        m = Mathtex(s, matplotlib.rcParams['mathtext.fontset'],
                    prop.get_size_in_points(), self.dpi, rcParams['mathtext.default'],
                    cache=True)
        b = MathtexBackendImage()
        m.render_to_backend(b)

        width, height = m.width, m.height + m.depth
        font_image = b.image

        if angle==90:
            width, height = height, width
            x -= width
        y -= height

        imw = font_image.get_width()
        imh = font_image.get_height()
        N = imw * imh

        # a numpixels by num fonts array
        Xall = npy.zeros((N,1), npy.uint8)

        image_str = font_image.as_str()
        Xall[:,0] = npy.fromstring(image_str, npy.uint8)

        # get the max alpha at each pixel
        Xs = npy.amax(Xall,axis=1)

        # convert it to it's proper shape
        Xs.shape = imh, imw

        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True,
                                bits_per_sample=8, width=imw, height=imh)

        array = pixbuf_get_pixels_array(pixbuf)

        rgb = gc.get_rgb()
        array[:,:,0]=int(rgb[0]*255)
        array[:,:,1]=int(rgb[1]*255)
        array[:,:,2]=int(rgb[2]*255)
        array[:,:,3]=Xs

        try: # new in 2.2
            # can use None instead of gc.gdkGC, if don't need clipping
            self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0,
                                          int(x), int(y), imw, imh,
                                          gdk.RGB_DITHER_NONE, 0, 0)
        except AttributeError:
            # deprecated in 2.2
            pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0,
                                  int(x), int(y), imw, imh,
                                  gdk.RGB_DITHER_NONE, 0, 0)
Exemplo n.º 7
0
    def save(self,
             filename,
             format='auto',
             backend='auto',
             backend_options={}):
        if format == 'auto':
            format = filename.split('.')[-1]
        if backend == 'auto':
            if format == 'png':
                backend = 'image'
            else:
                backend = 'cairo'

        # Create the backend instance
        if backend == 'image':
            backend = MathtexBackendImage()
        elif backend == 'cairo':
            if not HAVE_CAIRO_BACKEND:
                raise RuntimeError(
                    "Cairo backend requested when not available.")
            backend = MathtexBackendCairo()

        # Set the options for the backend
        backend.options = backend_options

        # Render!
        self.render_to_backend(backend)

        # Save
        backend.save(filename, format)
Exemplo n.º 8
0
    def draw_mathtext(self, gc, x, y, s, prop, angle):
        """
        Draw the math text using mathtex.mathtex_main
        """
        if __debug__: verbose.report('RendererAgg.draw_mathtext',
                                     'debug-annoying')
        if HAVE_MATHTEX:
            m = Mathtex(s, rcParams['mathtext.fontset'], prop.get_size_in_points(),
                        self.dpi, rcParams['mathtext.default'], cache=True)
            b = MathtexBackendImage()

            m.render_to_backend(b)

            self._renderer.draw_text_image(b.image, int(x), int(y) + 1, angle, gc)
Exemplo n.º 9
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        if not HAVE_MATHTEX:
            return

        m = Mathtex(s,
                    rcParams['mathtext.fontset'],
                    prop.get_size_in_points(),
                    self.dpi,
                    rcParams['mathtext.default'],
                    cache=True)
        b = MathtexBackendImage()
        m.render_to_backend(b)

        gc.draw_mathtext(x, y, angle, 255 - b.image.as_array())