Showing posts with label cairo. Show all posts
Showing posts with label cairo. Show all posts

Friday, November 27, 2009

Mimaggia, cairo tester

Hello,
From GNOME

I've created a simple pygtk project for testing cairo statements. It includes a sort of terminal where you write statements in a simple language. It features immediate preview of what you write.

Say it's a new format for images.
If you want to test it and see the code, ask me.

Tuesday, July 21, 2009

Aruanne, pdf reporting framework

Hello,
a while ago I've been talking about pango cairo and how to generate pdf with a couple of tables.
In the meantime I've worked on it, improving and enhancing new kinds of elements. This has lead to the creation of a small project, a library providing a simple framework for generating mostly PDF reports (I haven't tried to generate SVG or something else yet).

After a couple of release requests, I've found finally the time to publish a sort of working code.

Here's the git repository and here you can download the snapshot tarball.

Any patches welcome.

Wednesday, May 27, 2009

Render tables with pangocairo like reportlab

Hello,
lately I was wondering if there was any alternative to the well known reportlab python software for creating PDF reports. I immediately thought about Cairo. The only two problems are:
  • Cairo doesn't create multiple pages
  • No support for creating tables containing text, necessary for table-based reports
I still can't realize how to achieve the first feature, but the second one could be solved using Pango layouts.
The idea is to create the cells of the table using such layouts, so that the text get wrapped etc.

Here's the Pango tables code snippet containing the necessary classes for achieving the job. Notice that the methods in the snippet often make use of Pango units instead of pixels.
Now let's use the Table class as follows: create a table with two rows and two columns, then show it twice with different background colors.

surface = cairo.PDFSurface ("test.pdf", 300, 400)
cr = cairo.Context (surface)
cr = pangocairo.CairoContext (cairo.Context (surface))

sizes = [pango.SCALE*12*10, pango.SCALE*12*10]
data = [["first test with pango tables", "seems to work correctly"],
["though it needs", "support for borders and spans"]]

table = Table (cr, sizes, data, pango.FontDescription ("Sans 12"))
cr.rectangle (0, 0, pango.PIXELS(table.get_width ()), pango.PIXELS(table.get_height()))
cr.set_source_rgb (0.8, 0.8, 0.8)
cr.fill ()
cr.set_source_rgb (0, 0, 0)
table.show_table (cr)

cr.translate (0, 200)
cr.rectangle (0, 0, pango.PIXELS(table.get_width ()), pango.PIXELS(table.get_height()))
cr.set_source_rgb (0.4, 0.5, 0.7)
cr.fill ()
cr.set_source_rgb (0, 0, 0)
table.show_table (cr)

Here's the result test.pdf:


Sunday, April 26, 2009

Create a GdkPixbuf from cairo surface

Hello,
here's the inverted code snippet of the previous post.

w, h = surface.get_width(), surface.get_height()

pixmap = gtk.gdk.Pixmap (None, w, h, 24)

cr = pixmap.cairo_create ()

cr.set_source_surface (surface, 0, 0)

cr.paint ()

pixbuf = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, True, 8, w, h)

pixbuf = pixbuf.get_from_drawable (pixmap, gtk.gdk.colormap_get_system(), 0, 0, 0, 0, w, h)

Saturday, April 25, 2009

Create a cairo surface from a pixbuf

Hello,
sometimes in the code of a couple of projects I see some hard algorithms to transform a pixbuf in a cairo surface.
Maybe most of people don't know that GdkCairoContext, contrarily to cairo_t, is created against a cairo context not a cairo surface:

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())

cr = cairo.Context (surface)

gdkcr = gtk.gdk.CairoContext (cr)

gdkcr.set_source_pixbuf (pixbuf)

gdkcr.paint ()


Notice gtk.gdk.CairoContext (cr), which cr is not the surface. That's the key of the code snippet.

For instance this can be applied on a ClutterCairoTexture to render pixbufs in the canvas.