Beispiel #1
0
    def __init__(self,
                 bot,
                 text,
                 x=0,
                 y=0,
                 width=None,
                 height=None,
                 outline=False,
                 ctx=None,
                 enableRendering=True,
                 **kwargs):
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)
        canvas = bot._canvas

        ###self._transform = canvas.transform # TODO remove - this is in grob

        self._ctx = ctx
        self._pang_ctx = None

        self._doRender = enableRendering

        self.text = unicode(text)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self._outline = outline

        self._fontfile = kwargs.get('font', canvas.fontfile)
        self._fontsize = kwargs.get('fontsize', canvas.fontsize)
        self._lineheight = kwargs.get('lineheight', canvas.lineheight)
        self._align = kwargs.get('align', canvas.align)
        self._indent = kwargs.get("indent")

        # we use the pango parser instead of trying this by hand
        self._fontface = pango.FontDescription(self._fontfile)

        # then we set fontsize (multiplied by pango.SCALE)
        self._fontface.set_absolute_size(self._fontsize * pango.SCALE)

        # missing styles?
        if kwargs.has_key("style"):
            if "italic" in kwargs["style"] or "oblique" in kwargs["style"]:
                self._style = pango.STYLE_ITALIC
                self._fontface.set_style(self._style)

        #we need to pre-render some stuff to enable metrics sizing
        self._pre_render()

        if (self._doRender
            ):  #this way we do not render if we only need to create metrics
            if bool(ctx):
                self._render(self._ctx)
            else:
                # Normal rendering, can be deferred
                self._deferred_render()
Beispiel #2
0
    def __init__(
        self,
        bot,
        text,
        x=0,
        y=0,
        width=None,
        height=None,
        outline=False,
        ctx=None,
        enableRendering=True,
        **kwargs
    ):
        self._canvas = canvas = bot._canvas
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)

        self.text = str(text)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.outline = outline

        self.font = kwargs.get("font", canvas.fontfile)
        self.fontsize = kwargs.get("fontsize", canvas.fontsize)
        self.style = kwargs.get("style", "normal")
        self.weight = kwargs.get("weight", "normal")

        self.align = kwargs.get("align", canvas.align)
        self.indent = kwargs.get("indent")
        self.lineheight = kwargs.get("lineheight", canvas.lineheight)

        # Setup hidden vars for Cairo / Pango specific bits:
        self._ctx = ctx
        self._pangocairo_ctx = None
        self._pango_fontface = Pango.FontDescription.from_string(self.font)

        # then we set fontsize (multiplied by Pango.SCALE)
        self._pango_fontface.set_absolute_size(self.fontsize * Pango.SCALE)
        self._pango_fontface.set_style(_style_name_to_pango(self.style))
        self._pango_fontface.set_weight(_weight_name_to_pango(self.weight))

        # Pre-render some stuff to enable metrics sizing
        self._pre_render()

        if (
            enableRendering
        ):  # this way we do not render if we only need to create metrics
            if bool(ctx):
                self._render(self._ctx)
            else:
                # Normal rendering, can be deferred
                self._deferred_render()
        self._prerendered = enableRendering
Beispiel #3
0
    def __init__(self, bot, text, x=0, y=0, width=None, height=None, outline=False, ctx=None, enableRendering=True, **kwargs):
        self._canvas = canvas = bot._canvas
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)
        
        ###self._transform = canvas.transform # TODO remove - this is in grob

        self._ctx = ctx
        self._pang_ctx = None
        
        self._doRender = enableRendering
                
        self.text = unicode(text)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self._outline = outline

        self._fontfile = kwargs.get('font', canvas.fontfile)
        self._fontsize = kwargs.get('fontsize', canvas.fontsize)
        self._lineheight = kwargs.get('lineheight', canvas.lineheight)
        self._align = kwargs.get('align', canvas.align)
        self._indent = kwargs.get("indent")

        # we use the pango parser instead of trying this by hand
        self._fontface = pango.FontDescription(self._fontfile)
                                                      
        # then we set fontsize (multiplied by pango.SCALE)
        self._fontface.set_absolute_size(self._fontsize*pango.SCALE)

        # the style
        self._style = pango.STYLE_NORMAL
        if kwargs.has_key("style"):
            if kwargs["style"]=="italic" or kwargs["style"]=="oblique":
                self._style = pango.STYLE_ITALIC
        self._fontface.set_style(self._style)
        
        #we need to pre-render some stuff to enable metrics sizing
        self._pre_render()
        
        if (self._doRender): #this way we do not render if we only need to create metrics
          if bool(ctx):
              self._render(self._ctx)
          else:
              # Normal rendering, can be deferred
              self._deferred_render()
Beispiel #4
0
    def __init__(self, bot, path = None, x = 0, y = 0, width=None, height=None, alpha=1.0, data=None, pathmode=CORNER, **kwargs):
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)

        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alpha = alpha
        self.path = path
        self.data = data
        self._pathmode = pathmode
        sh = sw = None  # Surface Height and Width
        
        if isinstance(self.data, cairo.ImageSurface):
            sw = self.data.get_width()
            sh = self.data.get_height()
            self._imagesurface = self.data
        else:
            # checks if image data is passed in command call, in this case it wraps
            # the data in a StringIO oject in order to use it as a file
            # the data itself must contain an entire image, not just pixel data
            # it can be useful for example to retrieve images from the web without 
            # writing temp files (e.g. using nodebox's web library, see example 1 of the library)
            # if no data is passed the path is used to open a local file
            if self.data is None:
                surfaceref = self._surface_cache.get(path)
                if surfaceref:
                    imagesurface = surfaceref.surface
                    sw = imagesurface.get_width()
                    sh = imagesurface.get_height()
                elif os.path.splitext(path)[1].lower() == '.svg' and rsvg is not None:
                    handle = rsvg.Handle(path)
                    sw, sh = handle.get_dimension_data()[:2]
                    imagesurface = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, 0, 0, sw, sh)
                    ctx = cairo.Context(imagesurface)
                    handle.render_cairo(ctx)
                elif os.path.splitext(path)[1].lower() == '.png':
                    imagesurface = cairo.ImageSurface.create_from_png(path)
                    sw = imagesurface.get_width()
                    sh = imagesurface.get_height()
                elif Gtk is not None:
                    pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
                    sw = pixbuf.get_width()
                    sh = pixbuf.get_height()

                    ''' create a new cairo surface to place the image on '''
                    surface = cairo.ImageSurface(0, sw, sh)
                    ''' create a context to the new surface '''
                    ct = cairo.Context(surface)
                    ''' create a GDK formatted Cairo context to the new Cairo native context '''
                    ct2 = Gdk.CairoContext(ct)
                    ''' draw from the pixbuf to the new surface '''
                    ct2.set_source_pixbuf(pixbuf, 0, 0)
                    ct2.paint()
                    ''' surface now contains the image in a Cairo surface '''
                    imagesurface = ct2.get_target()
                else:
                    img = PILImage.open(path)

                    if img.mode != 'RGBA':
                        img = img.convert("RGBA")

                    sw, sh = img.size
                    # Would be nice to not have to do some of these conversions :-\
                    bgra_data = img.tostring('raw', 'BGRA', 0, 1)
                    bgra_array = array.array('B', bgra_data)
                    imagesurface = cairo.ImageSurface.create_for_data(bgra_array, cairo.FORMAT_ARGB32, sw, sh, sw*4)

                self._surface_cache[path] = SurfaceRef(imagesurface)
            else:
                img = PILImage.open(StringIO(self.data))

                if img.mode != 'RGBA':
                    img = img.convert("RGBA")
                
                sw, sh = img.size
                # Would be nice to not have to do some of these conversions :-\
                bgra_data = img.tostring('raw', 'BGRA', 0, 1)
                bgra_array = array.array('B', bgra_data)
                imagesurface = cairo.ImageSurface.create_for_data(bgra_array, cairo.FORMAT_ARGB32, sw, sh, sw*4) 

            if width is not None or height is not None:
                if width:
                    wscale = float(width) / sw
                else:
                    wscale = 1.0
                if height:
                    hscale = float(height) / sh
                else:
                    if width:
                        hscale = wscale
                    else:   
                        hscale = 1.0
                self._transform.scale(wscale, hscale)

            self.width = width or sw
            self.height = height or sh
            self._imagesurface = imagesurface

        self._deferred_render()
Beispiel #5
0
    def __init__(self,
                 bot,
                 path=None,
                 x=0,
                 y=0,
                 width=None,
                 height=None,
                 alpha=1.0,
                 data=None,
                 pathmode=CORNER,
                 **kwargs):
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)

        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alpha = alpha
        self.path = path
        self.data = data
        self._pathmode = pathmode
        sh = sw = None  # Surface Height and Width

        if isinstance(self.data, cairo.ImageSurface):
            sw = self.data.get_width()
            sh = self.data.get_height()
            self._surface = self.data
        else:
            # checks if image data is passed in command call, in this case it wraps
            # the data in a StringIO oject in order to use it as a file
            # the data itself must contain an entire image, not just pixel data
            # it can be useful for example to retrieve images from the web without
            # writing temp files (e.g. using nodebox's web library, see example 1 of the library)
            # if no data is passed the path is used to open a local file
            if self.data is None:
                surfaceref = self._surface_cache.get(path)
                if surfaceref:
                    surface = surfaceref.surface
                    if isinstance(surface, cairo.RecordingSurface):
                        extents = surface.get_extents()
                        # extents has x, y which we dont use right now
                        sw = extents.width
                        sh = extents.height
                    else:
                        sw = surface.get_width()
                        sh = surface.get_height()
                elif os.path.splitext(
                        path)[1].lower() == ".svg" and Rsvg is not None:
                    handle = Rsvg.Handle()
                    svg = handle.new_from_file(path)
                    dimensions = svg.get_dimensions()
                    sw = dimensions.width
                    sh = dimensions.height
                    surface = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA,
                                                     (0, 0, sw, sh))
                    ctx = cairo.Context(surface)
                    pycairo_ctx = driver.ensure_pycairo_context(ctx)
                    svg.render_cairo(pycairo_ctx)
                elif os.path.splitext(path)[1].lower() == ".png":
                    surface = cairo.ImageSurface.create_from_png(path)
                    sw = surface.get_width()
                    sh = surface.get_height()
                else:
                    img = PILImage.open(path)

                    if img.mode != "RGBA":
                        img = img.convert("RGBA")

                    sw, sh = img.size
                    # Would be nice to not have to do some of these conversions :-\
                    bgra_data = img.tobytes("raw", "BGRA", 0, 1)
                    bgra_array = array.array("B", bgra_data)
                    surface = cairo.ImageSurface.create_for_data(
                        bgra_array, cairo.FORMAT_ARGB32, sw, sh, sw * 4)

                self._surface_cache[path] = SurfaceRef(surface)
            else:
                img = PILImage.open(StringIO(self.data))

                if img.mode != "RGBA":
                    img = img.convert("RGBA")

                sw, sh = img.size
                # Would be nice to not have to do some of these conversions :-\
                bgra_data = img.tobytes("raw", "BGRA", 0, 1)
                bgra_array = array.array("B", bgra_data)
                surface = cairo.ImageSurface.create_for_data(
                    bgra_array, cairo.FORMAT_ARGB32, sw, sh, sw * 4)

            if width is not None or height is not None:
                if width:
                    wscale = float(width) / sw
                else:
                    wscale = 1.0
                if height:
                    hscale = float(height) / sh
                else:
                    if width:
                        hscale = wscale
                    else:
                        hscale = 1.0
                self._transform.scale(wscale, hscale)

            self.width = width or sw
            self.height = height or sh
            self._surface = surface

        self._deferred_render()
Beispiel #6
0
Datei: img.py Projekt: msarch/py
    def __init__(self, bot, path = None, x = 0, y = 0, width=None, height=None, alpha=1.0, data=None, pathmode=CORNER, **kwargs):
        Grob.__init__(self, bot)
        ColorMixin.__init__(self, **kwargs)

        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.alpha = alpha
        self.path = path
        self.data = data
        self._pathmode = pathmode
        sh = sw = None  # Surface Height and Width
        
        if isinstance(self.data, cairo.ImageSurface):
            sw = self.data.get_width()
            sh = self.data.get_height()
            self._imagesurface = self.data
        else:
            # checks if image data is passed in command call, in this case it wraps
            # the data in a StringIO oject in order to use it as a file
            # the data itself must contain an entire image, not just pixel data
            # it can be useful for example to retrieve images from the web without 
            # writing temp files (e.g. using nodebox's web library, see example 1 of the library)
            # if no data is passed the path is used to open a local file
            if self.data is None:
                if os.path.splitext(path)[1].lower() == '.svg':
                    if path in self._surface_cache:
                        imagesurface = self._surface_cache[path]
                        sw = imagesurface.get_width()
                        sh = imagesurface.get_height()
                    else:
                        handle = rsvg.Handle(path)
                        sw, sh = handle.get_dimension_data()[:2]
                        imagesurface = RecordingSurface(sw, sh)
                        ctx = cairo.Context(imagesurface)
                        handle.render_cairo(ctx)
                else:
                    if path in self._surface_cache:
                        imagesurface = self._surface_cache[path]
                        sw = imagesurface.get_width()
                        sh = imagesurface.get_height()
                    else:
                        if os.path.splitext(path)[1].lower() == '.png':
                            imagesurface = cairo.ImageSurface.create_from_png(path)
                            sw = imagesurface.get_width()
                            sh = imagesurface.get_height()
                        else:
                            pixbuf = gtk.gdk.pixbuf_new_from_file(path)
                            sw = pixbuf.get_width()
                            sh = pixbuf.get_height()

                            ''' create a new cairo surface to place the image on '''
                            surface = cairo.ImageSurface(0, sw, sh)
                            ''' create a context to the new surface '''
                            ct = cairo.Context(surface)
                            ''' create a GDK formatted Cairo context to the new Cairo native context '''
                            ct2 = gtk.gdk.CairoContext(ct)
                            ''' draw from the pixbuf to the new surface '''
                            ct2.set_source_pixbuf(pixbuf, 0, 0)
                            ct2.paint()
                            ''' surface now contains the image in a Cairo surface '''
                            imagesurface = ct2.get_target()
                self._surface_cache[path] = imagesurface
            else:
                img = PILImage.open(StringIO(self.data))

                if img.mode != 'RGBA':
                    img = img.convert("RGBA")
                
                sw, sh = img.size
                # Would be nice to not have to do some of these conversions :-\
                bgra_data = img.tostring('raw', 'BGRA', 0, 1)
                bgra_array = array.array('B', bgra_data)
                imagesurface = cairo.ImageSurface.create_for_data(bgra_array, cairo.FORMAT_ARGB32, sw, sh, sw*4) 

            if width is not None or height is not None:
                if width:
                    wscale = float(width) / sw
                else:
                    wscale = 1.0
                if height:
                    hscale = float(height) / sh
                else:
                    if width:
                        hscale = wscale
                    else:   
                        hscale = 1.0
                self._transform.scale(wscale, hscale)

            self.width = width or sw
            self.height = height or sh
            self._imagesurface = imagesurface

        self._deferred_render()