def _render(self, width, height):
        """Render our SVG to a Pygame image"""
        handle = rsvg.Handle(data=self.svg)
        originalSize = (width, height)
        scale = 1.0
        hw, hh = handle.get_dimension_data()[:2]
        if hw and hh:
            if not width:
                if not height:
                    width, height = hw, hh
                else:
                    scale = float(height) / hh
                    width = hh / float(hw) * height
            elif not height:
                scale = float(width) / hw
                height = hw / float(hh) * width
            else:
                # scale only, only rendering as large as it is...
                if width / height > hw / hh:
                    # want it taller than it is...
                    width = hh / float(hw) * height
                else:
                    height = hw / float(hh) * width
                scale = float(height) / hh

            csrf, ctx = _cairoimage.newContext(int(width), int(height))
            ctx.scale(scale, scale)
            handle.render_cairo(ctx)
            return _cairoimage.asImage(csrf)
        return None
Example #2
0
    def _render(self, width, height):
        """Render our SVG to a Pygame image"""
        import rsvg

        handle = rsvg.Handle(data=self.svg)
        originalSize = (width, height)
        scale = 1.0
        hw, hh = handle.get_dimension_data()[:2]
        if hw and hh:
            if not width:
                if not height:
                    width, height = hw, hh
                else:
                    scale = float(height) / hh
                    width = hh / float(hw) * height
            elif not height:
                scale = float(width) / hw
                height = hw / float(hh) * width
            else:
                # scale only, only rendering as large as it is...
                if width / height > hw / hh:
                    # want it taller than it is...
                    width = hh / float(hw) * height
                else:
                    height = hw / float(hh) * width
                scale = float(height) / hh

            csrf, ctx = _cairoimage.newContext(int(width), int(height))
            ctx.scale(scale, scale)
            handle.render_cairo(ctx)
            return _cairoimage.asImage(csrf)
        return None
    def doPaint(
            self,
            text,
            antialias=True,
            color=(
                255,
                255,
                255),
            background=None):
        """Render the __font onto a new Surface and return it.
        We ignore 'antialias' and use system settings.

        text -- (unicode) string with the text to doPaint
        antialias -- attempt to antialias the text or not
        color -- three or four-tuple of 0-255 values specifying rendering
            colour for the text
        background -- three or four-tuple of 0-255 values specifying rendering
            colour for the background, or None for trasparent background

        returns a pygame image instance
        """
        log.info(
            'doPaint: %r, antialias = %s, color=%s, background=%s',
            text,
            antialias,
            color,
            background)

        layout = self._createLayout(text)
        # determine pixel __size
        (logical, ink) = layout.get_pixel_extents()
        ink = pygame.rect.Rect(ink)

        # Create a new Cairo ImageSurface
        csrf, cctx = _cairoimage.newContext(ink.w, ink.h)
        cctx = pangocairo.CairoContext(cctx)

        # Mangle the colors on little-endian machines. The reason for this
        # is that Cairo writes native-endian 32-bit ARGB values whereas
        # Pygame expects endian-independent values in whatever format. So we
        # tell our users not to expect transparency here (avoiding the A issue)
        # and we swizzle all the colors around.

        # doPaint onto it
        if background is not None:
            background = _cairoimage.mangle_color(background)
            cctx.set_source_rgba(*background)
            cctx.paint()

        log.debug('incoming color: %s', color)
        color = _cairoimage.mangle_color(color)
        log.debug('  translated color: %s', color)

        cctx.new_path()
        cctx.layout_path(layout)
        cctx.set_source_rgba(*color)
        cctx.fill()

        # Create and return a new Pygame Image derived from the Cairo Surface
        return _cairoimage.asImage(csrf)
Example #4
0
    def render(self, text, antialias=True, color=(255,255,255), background=None ):
        """Render the font onto a new Surface and return it.
        We ignore 'antialias' and use system settings.
        
        text -- (unicode) string with the text to render
        antialias -- attempt to antialias the text or not
        color -- three or four-tuple of 0-255 values specifying rendering 
            colour for the text 
        background -- three or four-tuple of 0-255 values specifying rendering 
            colour for the background, or None for trasparent background
        
        returns a pygame image instance
        """
        log.info( 'render: %r, antialias = %s, color=%s, background=%s', text, antialias, color, background )

        # create layout
        layout = pango.Layout(gtk.gdk.pango_context_get())
        layout.set_font_description(self.fd)
        if self.underline:
            attrs = layout.get_attributes()
            if not attrs:
                attrs = pango.AttrList()
            attrs.insert(pango.AttrUnderline(pango.UNDERLINE_SINGLE, 0, 32767))
            layout.set_attributes( attrs )
        layout.set_text(text)

        # determine pixel size
        (logical, ink) = layout.get_pixel_extents()
        ink = pygame.rect.Rect(ink)

        # Create a new Cairo ImageSurface
        csrf,cctx = _cairoimage.newContext( ink.w, ink.h )
        cctx = pangocairo.CairoContext(cctx)

        # Mangle the colors on little-endian machines. The reason for this 
        # is that Cairo writes native-endian 32-bit ARGB values whereas
        # Pygame expects endian-independent values in whatever format. So we
        # tell our users not to expect transparency here (avoiding the A issue)
        # and we swizzle all the colors around.

        # render onto it
        if background is not None:
            background = _cairoimage.mangle_color( background )
            cctx.set_source_rgba(*background)
            cctx.paint()
        
        log.debug( 'incoming color: %s', color )
        color = _cairoimage.mangle_color( color )
        log.debug( '  translated color: %s', color )

        cctx.new_path()
        cctx.layout_path(layout)
        cctx.set_source_rgba(*color)
        cctx.fill()

        # Create and return a new Pygame Image derived from the Cairo Surface
        return _cairoimage.asImage( csrf )
Example #5
0
    """
    import rsvg
    handle = rsvg.Handle(data=data)
    originalSize = (width, height)
    scale = 1.0
    hw, hh = handle.get_dimension_data()[:2]
    if hw and hh:
        if not width:
            if not height:
                width, height = hw, hh
            else:
                scale = float(height) / hh
                width = hh / float(hw) * height
        elif not height:
            scale = float(width) / hw
            height = hw / float(hh) * width
        else:
            # scale only, only rendering as large as it is...
            if width / height > hw / hh:
                # want it taller than it is...
                width = hh / float(hw) * height
            else:
                height = hw / float(hh) * width
            scale = float(height) / hh

        csrf, ctx = _cairoimage.newContext(int(width), int(height))
        ctx.scale(scale, scale)
        handle.render_cairo(ctx)
        return _cairoimage.asImage(csrf)
    return None
Example #6
0
    """
    import rsvg
    handle = rsvg.Handle( data = data )
    originalSize = (width,height)
    scale = 1.0
    hw,hh = handle.get_dimension_data()[:2]
    if hw and hh:
        if not width:
            if not height:
                width,height = hw,hh 
            else:
                scale = float(height)/hh
                width = hw/float(hh) * height
        elif not height:
            scale = float(width)/hw
            height = hh/float(hw) * width
        else:
            # scale only, only rendering as large as it is...
            if width/height > hw/hh:
                # want it taller than it is...
                width = hh/float(hw) * height
            else:
                height = hw/float(hh) * width
            scale = float(height)/hh
        
        csrf, ctx = _cairoimage.newContext( int(width), int(height) )
        ctx.scale( scale, scale )
        handle.render_cairo( ctx )
        return _cairoimage.asImage( csrf )
    return None