Message-ID: <44EE9E1A.9050409 at tin.it> Date: Fri, 25 Aug 2006 08:52:10 +0200 From: Stalkern 2 MIME-Version: 1.0 To: lablgtk at math.nagoya-u.ac.jp Subject: Re: how to put text in a drawing_area ? References: <16685.28897.429085.9744 at soggy.deldotd.com> <20040826.165052.74190188.garrigue at kurims.kyoto-u.ac.jp> <44EE235F.2050809 at tin.it> In-Reply-To: <44EE235F.2050809 at tin.it> Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=ISO-8859-1 Content-Length: 5073 Stalkern 2 wrote: > Jacques GARRIGUE wrote: >> From: briand at aracnet.com >> >>> Seems like a very simple thing and in fact gDraw.mli contains the >>> following comment : >>> >>> >>> (** Functions for drawing points, lines, arcs, and text >>> at gtkdoc gdk gdk-Drawing-Primitives *) >>> >>> >>> However there is no text method and I can't seem to trace out a link >>> to the regular text widgets, nor can I find anything in the examples >>> directory... >> You need to use the #put_layout method. >> But for that you must first create a layout with: >> >> let layout = area#misc#pango_context#create_layout in >> >> Then you write in the layout, before drawing it: >> >> Pango.Layout.set_text layout "my text"; >> drawable#put_layout layout ~x:10 ~y:10 >> >> Hope this helps. >> >> Jacques >> >> P.S. maybe we could add a simpler utility method... >> > > Hello to everybody > > I'm plotting some stuff and of course I'm attempting to write the name > of the Y values _vertically_ (not very original, is it?). > > The Gnome Developers' API says clearly "There is no way to draw scaled > or rotated text with GDK." (http://developer.gnome.org/doc/GGAD/z132.html). > > However, a simple "rotation" like the one that I'm looking for can be > done by swapping coords rather simply. > > Here is code in GTK/Python, by John Hunter > (http://www.daa.com.au/pipermail/pygtk/2004-September/008660.html) > > [...] I've found the _whole_ script in pygtk; here I post it because it is MUCH clearer (and runs out-of-the-box) ---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---- """ John Hunter (http://www.daa.com.au/pipermail/pygtk/2003-April/005052.html) """ import pygtk pygtk.require('2.0') import gtk import pango win = gtk.Window() win.show() vbox = gtk.VBox() vbox.show() win.add(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') self.layout.set_font_description(desc) 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() self.drawable.clear_area(50, 100, h, w) imageIn = self.drawable.get_image(x=50, y=100, width=h, height=w) imageOut = gtk.gdk.Image(type=0, visual=self.drawable.get_visual(), width=w, height=h) imageOut.set_colormap(imageIn.get_colormap()) for i in range(h): for j in range(w): imageOut.put_pixel(j, i, imageIn.get_pixel(i,w-j-1) ) pixmap = gtk.gdk.Pixmap(self.drawable, winw, winh) gc = pixmap.new_gc() pixmap.draw_image(gc, imageOut, 0, 0, 0, 0, w, h) c.draw_text(drawable=pixmap, x=0, y=0) 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) return imageIn = pixmap.get_image(x=0, y=0, width=w, height=h) imageOut = gtk.gdk.Image(type=0, visual=pixmap.get_visual(), width=h, height=w) imageOut.set_colormap(imageIn.get_colormap()) for i in range(w): for j in range(h): imageOut.put_pixel(j, i, imageIn.get_pixel(w-i-1,j) ) self.drawable.draw_image(gc, imageOut, 0, 0, 50, 100, h, w) c = C('Some text') da = gtk.DrawingArea() da.connect('realize', c.realize) da.set_size_request(300,300) da.show() vbox.pack_start(da, gtk.TRUE, gtk.TRUE) hbox = gtk.HBox() hbox.show() vbox.pack_start(hbox, gtk.FALSE, gtk.FALSE) button = gtk.Button('Draw') button.show() button.connect('clicked', lambda b: c.draw_text()) hbox.pack_start(button, gtk.TRUE, gtk.TRUE) button = gtk.Button('Rotate') button.show() button.connect('clicked', lambda b: c.draw_text_rotated()) hbox.pack_start(button, gtk.TRUE, gtk.TRUE) button = gtk.Button('Quit') button.show() button.connect('clicked', lambda b: gtk.mainquit()) hbox.pack_start(button, gtk.TRUE, gtk.TRUE) gtk.mainloop() ----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<---- Ernesto