Pango rotate ?


About a year ago John Hunter proposed a routine to rotate pango text ( draw_text_rotated)

I wonder if the procedure has been implemented by default in a pango method yet.
It would be VERY useful for many to have the possibility to rotate pangolayout, at least by 90,180,270 degrees




    > I want to rotate some text 90 degrees. I'm using pango
    > layouts and drawable.draw_layout to draw my text.

OK, got this working at the level of manipulating individual pixels in
a gtk.gdk.Image with pixbufs and pixmaps. For my purposes, making
ylabels, this is probably fast enough, but if anyone has a more
efficient solution, let me know...

However, if the 'Rotate' button is clicked repeatedly, which just
repeats the rotated drawing operation, strange things start to appear
in the region where the rotated text is rendered. Since repeated
clicks should just be repeating the same drawing operations, it is not
clear to me why this is happening.

Below is an example script.

John Hunter

import pygtk
import gtk
import pango

win = gtk.Window()
vbox = gtk.VBox()

class C:

    def __init__(self, text):
        self.text = text
    def realize(self, drawingArea):
        self.drawingArea = drawingArea
        self.drawable = drawingArea.window

        # text properties
        context = self.drawingArea.create_pango_context()
        self.layout = self.drawingArea.create_pango_layout(self.text)
        desc = pango.FontDescription('Sans 14')

    def draw_text(self, drawable=None, x=100, y=100):
        if drawable is None: drawable=self.drawable
        # draw some text in the foreground color
        gc = drawable.new_gc()
        drawable.draw_layout(gc, x=x, y=y, layout=self.layout)
    def draw_text_rotated(self):
        draw the text to a pixmap, rotate the image, fill a pixbuf
        and draw from the pixbuf

        inkRect, logicalRect = self.layout.get_pixel_extents()
        x, y, w, h = logicalRect
        winw, winh = self.drawable.get_size()
        pixmap = gtk.gdk.Pixmap(self.drawable, winw, winh)
        c.draw_text(drawable=pixmap, x=0, y=0)

        gc = pixmap.new_gc()
        if 0:
            # These lines test that the pixmap was drawn to ok
            pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, w, h)
            pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(),
                                     0, 0, 0, 0, w, h)
            pixbuf.render_to_drawable(self.drawable, gc, 0, 0, 0, 0, w, h, 0, 0, 0)

        imageIn = pixmap.get_image(x=0, y=0, width=w, height=h)
        imageOut = gtk.gdk.Image(type=0,
                                 width=h, height=w)
        for i in range(w):
            for j in range(h):
                imageOut.put_pixel(j, i, imageIn.get_pixel(w-i-1,j) )

        pixbuf = gtk.gdk.Pixbuf(colorspace=gtk.gdk.COLORSPACE_RGB,
                                has_alpha=0, bits_per_sample=8,
                                width=h, height=w)
        pixbuf.get_from_image(src=imageOut, cmap=imageIn.get_colormap(),
                              src_x=0, src_y=0, dest_x=0, dest_y=0,
                              width=h, height=w)
        pixbuf.render_to_drawable(self.drawable, gc,
                                  src_x=0, src_y=0,
                                  dest_x=50, dest_y=100,
                                  width=h, height=w,
                                  dither=0, x_dither=0, y_dither=0)

c = C('Some text')

da = gtk.DrawingArea()
da.connect('realize', c.realize)
vbox.pack_start(da, gtk.TRUE, gtk.TRUE)

hbox = gtk.HBox()
vbox.pack_start(hbox, gtk.FALSE, gtk.FALSE)

button = gtk.Button('Draw')
button.connect('clicked', lambda b: c.draw_text())
hbox.pack_start(button, gtk.TRUE, gtk.TRUE)

button = gtk.Button('Rotate')
button.connect('clicked', lambda b: c.draw_text_rotated())
hbox.pack_start(button, gtk.TRUE, gtk.TRUE)

button = gtk.Button('Quit')
button.connect('clicked', lambda b: gtk.mainquit())
hbox.pack_start(button, gtk.TRUE, gtk.TRUE)




Department of Statistics
deCODE genetics Sturlugata,8
570 2993 101 Reykjavík