示例#1
0
    def get_text_width_height(self, s, prop, ismath, rgb=(0, 0, 0)):
        """
        get the width and height in display coords of the string s
        with FontPropertry prop

        # passing rgb is a little hack to make cacheing in the
        # texmanager more efficient.  It is not meant to be used
        # outside the backend
        """

        if ismath == 'TeX':
            # todo: handle props
            size = prop.get_size_in_points()
            Z = self.texmanager.get_rgba(s, size, rgb)
            m, n, tmp = Z.shape
            return n, m

        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height
        font = self._get_agg_font(prop)
        font.set_text(s, 0.0)  # the width and height of unrotated string
        w, h = font.get_width_height()
        w /= 64.0  # convert from subpixels
        h /= 64.0
        return w, h
示例#2
0
    def get_text_width_height(self, s, prop, ismath):
        if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
               s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        ctx = self.text_ctx
        ctx.save()
        ctx.select_font (prop.get_name(),
                         self.fontangles [prop.get_style()],
                         self.fontweights[prop.get_weight()])

        # Cairo (says it) uses 1/96 inch user space units, ref: cairo_gstate.c
        # but if /96.0 is used the font is too small

        #size = prop.get_size_in_points() * self.dpi.get() / 96.0
        size = prop.get_size_in_points() * self.dpi.get() / 72.0
        
        # problem - scale remembers last setting and font can become
        # enormous causing program to crash
        # save/restore prevents the problem
        ctx.scale_font (size)
        
        w, h = ctx.text_extents (s)[2:4]
        ctx.restore()
            
        return w, h
示例#3
0
    def get_text_width_height(self, s, prop, ismath, rgb=(0,0,0)):
        """
        get the width and height in display coords of the string s
        with FontPropertry prop

        # passing rgb is a little hack to make cacheing in the
        # texmanager more efficient.  It is not meant to be used
        # outside the backend
        """

        if ismath=='TeX':
            # todo: handle props
            size = prop.get_size_in_points()
            dpi = self.dpi.get()
            Z = self.texmanager.get_rgba(s, size, dpi, rgb)
            m,n,tmp = Z.shape
            return n,m
        
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height
        font = self._get_agg_font(prop)
        font.set_text(s, 0.0)  # the width and height of unrotated string
        w, h = font.get_width_height()
        w /= 64.0  # convert from subpixels
        h /= 64.0
        return w, h
示例#4
0
    def get_text_width_height(self, s, prop, ismath):
        if ismath:
            width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        layout, inkRect, logicalRect = self._get_pango_layout(s, prop)
        l, b, w, h = inkRect
        return w, h + 1
示例#5
0
    def get_text_width_height(self, s, prop, ismath):
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        layout, inkRect, logicalRect = self._get_pango_layout(s, prop)
        l, b, w, h = inkRect
        return w, h + 1
示例#6
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
       # mathtext using the gtk/gdk method

        if numx.which[0] == "numarray":
            warnings.warn("_draw_mathtext() currently works for numpy, but "
                          "not numarray")
            return

        if not HAVE_CAIRO_NUMPY:
            warnings.warn("cairo with Numeric support is required for "
                          "_draw_mathtext()")
            return

        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(
            s, self.dpi.get(), size)

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

        imw, imh, s = fonts[0].image_as_str()
        N = imw*imh

        # a numpixels by num fonts array
        Xall = numx.zeros((N,len(fonts)), typecode=numx.UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image() # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:,i] = numx.fromstring(s, numx.UInt8)

        # get the max alpha at each pixel
        Xs = numx.mlab.max (Xall,1)

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

        pa = numx.zeros(shape=(imh,imw,4), typecode=numx.UInt8)
        rgb = gc.get_rgb()
        pa[:,:,0] = int(rgb[0]*255)
        pa[:,:,1] = int(rgb[1]*255)
        pa[:,:,2] = int(rgb[2]*255)
        pa[:,:,3] = Xs

        # works for numpy pa, not a numarray pa
        surface = cairo.ImageSurface.create_for_array (pa)
        gc.ctx.set_source_surface (surface, x, y)
        gc.ctx.paint()
示例#7
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size)

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

        imw, imh, image_str = fonts[0].image_as_str()
        N = imw * imh

        # a numpixels by num fonts array
        Xall = zeros((N, len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image()  # <-- Rotate
            imw, imh, image_str = font.image_as_str()
            Xall[:, i] = fromstring(image_str, UInt8)

        # get the max alpha at each pixel
        Xs = numerix.mlab.max(Xall, 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)
示例#8
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
        # mathtext using the gtk/gdk method

        if numx.which[0] == "numarray":
            warnings.warn("_draw_mathtext() currently works for numpy, but "
                          "not numarray")
            return

        if not HAVE_CAIRO_NUMPY:
            warnings.warn("cairo with Numeric support is required for "
                          "_draw_mathtext()")
            return

        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size)

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

        imw, imh, s = fonts[0].image_as_str()
        N = imw * imh

        # a numpixels by num fonts array
        Xall = numx.zeros((N, len(fonts)), typecode=numx.UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image()  # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:, i] = numx.fromstring(s, numx.UInt8)

        # get the max alpha at each pixel
        Xs = numx.mlab.max(Xall, 1)

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

        pa = numx.zeros(shape=(imh, imw, 4), typecode=numx.UInt8)
        rgb = gc.get_rgb()
        pa[:, :, 0] = int(rgb[0] * 255)
        pa[:, :, 1] = int(rgb[1] * 255)
        pa[:, :, 2] = int(rgb[2] * 255)
        pa[:, :, 3] = Xs

        # works for numpy pa, not a numarray pa
        surface = cairo.ImageSurface.create_for_array(pa)
        gc.ctx.set_source_surface(surface, x, y)
        gc.ctx.paint()
示例#9
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
       # mathtext using the gtk/gdk method

        if numerix.which[0] == "numarray":
            warnings.warn("_draw_mathtext() currently works for numpy, but not numarray")
            return

        if not HAVE_CAIRO_NUMPY:
            warnings.warn("cairo.numpy module required for _draw_mathtext()")
            return

        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(
            s, self.dpi.get(), size)

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

        imw, imh, s = fonts[0].image_as_str()
        N = imw*imh

        # a numpixels by num fonts array
        Xall = zeros((N,len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image() # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:,i] = fromstring(s, UInt8)  

        # get the max alpha at each pixel
        Xs = numerix.mlab.max(Xall,1)

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

        pa = zeros(shape=(imh,imw,4), typecode=UInt8)
        rgb = gc.get_rgb()
        pa[:,:,0] = int(rgb[0]*255)
        pa[:,:,1] = int(rgb[1]*255)
        pa[:,:,2] = int(rgb[2]*255)
        pa[:,:,3] = Xs

        # works for numpy pa, not a numarray pa
        surface = cairo.numpy.surface_create_for_array(pa)
        gc.ctx.translate (x,y)
        gc.ctx.show_surface (surface, imw, imh)
示例#10
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(
            s, self.dpi.get(), size)

        if angle==90:
            width, height = height, width
            x -= width
        y -= height
        
        imw, imh, image_str = fonts[0].image_as_str()
        N = imw*imh

        # a numpixels by num fonts array
        Xall = zeros((N,len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image() # <-- Rotate
            imw, imh, image_str = font.image_as_str()
            Xall[:,i] = fromstring(image_str, UInt8)  

        # get the max alpha at each pixel
        Xs = numerix.mlab.max(Xall,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)
示例#11
0
    def get_text_width_height(self, s, prop, ismath):
        """
        get the width and height in display coords of the string s
        with FontPropertry prop
        """
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        layout = self.get_pango_layout(s, prop)
        inkRect, logicalRect = layout.get_pixel_extents()
        rect = inkRect
        #rect = logicalRect
        l, b, w, h = rect
        return w, h + 1
示例#12
0
    def get_text_width_height(self, s, prop, ismath):
        """
        get the width and height in display coords of the string s
        with FontPropertry prop
        """

        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height
        font = self._get_agg_font(prop)
        font.set_text(s, 0.0)  # the width and height of unrotated string
        w, h = font.get_width_height()
        w /= 64.0  # convert from subpixels
        h /= 64.0
        return w+2, h+2
示例#13
0
    def get_text_width_height(self, s, prop, ismath):
        """
        get the width and height in display coords of the string s
        with FontPropertry prop
        """

        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height
        font = self._get_agg_font(prop)
        font.set_text(s, 0.0)  # the width and height of unrotated string
        w, h = font.get_width_height()
        w /= 64.0  # convert from subpixels
        h /= 64.0
        return w + 2, h + 2
示例#14
0
    def draw_mathtext(self, gc, x, y, s, prop, angle):

        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size)

        if angle == 90:
            width, height = height, width
        x = int(x)
        y = int(y)

        rgb = gc.get_rgb()
        #rgba = (rgb[0], rgb[1], rgb[2], gc.get_alpha())

        imw, imh, s = fonts[0].image_as_str()
        N = imw * imh

        # a numpixels by num fonts array
        Xall = zeros((N, len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image()  # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:, i] = fromstring(s, UInt8)

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

        # convert it to it's proper shape

        Xs.shape = imh, imw

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

        try:
            pa = pb.get_pixels_array()
        except AttributeError:
            pa = pb.pixel_array
        except RuntimeError, exc:  #  pygtk was not compiled with Numeric Python support
            print >> sys.stderr, 'Error:', exc
            return
示例#15
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size)

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

        imw, imh, s = fonts[0].image_as_str()
        N = imw * imh

        # a numpixels by num fonts array
        Xall = zeros((N, len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image()  # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:, i] = fromstring(s, UInt8)

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

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

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

        try:
            pa = pb.get_pixels_array()
        except AttributeError:
            pa = pb.pixel_array
        except RuntimeError, exc:  #  'pygtk was not compiled with Numeric Python support'
            verbose.report_error('mathtext not supported: %s' % exc)
            return
示例#16
0
 def draw_mathtext(self, gc, x, y, s, prop, angle):    
     """
     Draw the math text using matplotlib.mathtext
     """
     if __debug__: verbose.report('RendererAgg.draw_mathtext', 'debug-annoying')
     size = prop.get_size_in_points()
     width, height, fonts = math_parse_s_ft2font(
         s, self.dpi.get(), size, angle)
     
     if angle == 90:
         width, height = height, width
     for font in fonts:
         if angle == 90:             
             font.horiz_image_to_vert_image() # <-- Rotate
             self._renderer.draw_text( font, int(x)-width, int(y)-height, gc)
         else:
             self._renderer.draw_text( font, int(x), int(y)-height, gc)                
     if 0:
         self._renderer.draw_rectangle(gc, None,
                                       int(x),
                                       self.height-int(y),
                                       width, height)
示例#17
0
    def _draw_mathtext(self, gc, x, y, s, prop, angle):
        size = prop.get_size_in_points()
        width, height, fonts = math_parse_s_ft2font(
            s, self.dpi.get(), size)

        if angle==90:
            width, height = height, width
            x -= width
        y -= height
        
        imw, imh, s = fonts[0].image_as_str()
        N = imw*imh

        # a numpixels by num fonts array
        Xall = zeros((N,len(fonts)), typecode=UInt8)

        for i, font in enumerate(fonts):
            if angle == 90:
                font.horiz_image_to_vert_image() # <-- Rotate
            imw, imh, s = font.image_as_str()
            Xall[:,i] = fromstring(s, UInt8)  

        # get the max alpha at each pixel
        Xs = numerix.mlab.max(Xall,1)

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

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

        try:
            pa = pb.get_pixels_array()
        except AttributeError:
            pa = pb.pixel_array
        except RuntimeError, exc: #  'pygtk was not compiled with Numeric Python support'
            verbose.report_error('mathtext not supported: %s' % exc)
            return        
示例#18
0
 def draw_mathtext(self, gc, x, y, s, prop, angle):    
     """
     Draw the math text using matplotlib.mathtext
     """
     if DEBUG: print 'RendererAgg.draw_mathtext'
     size = prop.get_size_in_points()
     width, height, fonts = math_parse_s_ft2font(
         s, self.dpi.get(), size, angle)
     
     if angle == 90:
         width, height = height, width
     for font in fonts:
         if angle == 90:             
             font.horiz_image_to_vert_image() # <-- Rotate
             self._renderer.draw_text( font, int(x)-width, int(y)-height, gc)
         else:
             self._renderer.draw_text( font, int(x), int(y)-height, gc)                
     if 0:
         self._renderer.draw_rectangle(gc, None,
                                       int(x),
                                       self.height-int(y),
                                       width, height)
示例#19
0
    def get_text_width_height(self, s, prop, ismath):
        if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
               s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        ctx = self.text_ctx
        ctx.save()
        ctx.select_font (prop.get_name(),
                         self.fontangles [prop.get_style()],
                         self.fontweights[prop.get_weight()])

        # 1.4 scales font to a similar size to GTK / GTKAgg backends
        size = prop.get_size_in_points() * self.dpi.get() / PIXELS_PER_INCH * 1.4
        # problem - scale remembers last setting and font can become
        # enormous causing program to crash
        # save/restore prevents the problem
        ctx.scale_font (size)
        
        w, h = ctx.text_extents (s)[2:4]
        ctx.restore()
            
        return w, h
示例#20
0
    def get_text_width_height(self, s, prop, ismath):
        if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
        if ismath:
            width, height, fonts = math_parse_s_ft2font(
                s, self.dpi.get(), prop.get_size_in_points())
            return width, height

        ctx = self.text_ctx
        ctx.save()
        ctx.select_font(prop.get_name(), self.fontangles[prop.get_style()],
                        self.fontweights[prop.get_weight()])

        # 1.4 scales font to a similar size to GTK / GTKAgg backends
        size = prop.get_size_in_points() * self.dpi.get(
        ) / PIXELS_PER_INCH * 1.4
        # problem - scale remembers last setting and font can become
        # enormous causing program to crash
        # save/restore prevents the problem
        ctx.scale_font(size)

        w, h = ctx.text_extents(s)[2:4]
        ctx.restore()

        return w, h