Re: "draw" icon with string



Dear cecashon,

thanks for your code. This works currently.
I need to improve it in the future to support different plattforms
(fonts) and screen resolutions (scale).

kind
Christian


On 2018-09-10 19:32
<cecashon aol com> wrote:
 
Give this a try. It creates a surface, draws on it and then returns
the surface so that it can be put in an image widget. 


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
import cairo

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Surface")
        self.set_default_size(400, 400)
        self.set_position(Gtk.WindowPosition.CENTER)

        surface = self.get_surface()

        image = Gtk.Image()
        image.set_from_surface(surface)
        image.set_vexpand(True)
        image.set_hexpand(True)

        grid = Gtk.Grid()
        grid.attach(image, 0, 0, 1, 1)
        self.add(grid)

    def get_surface(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200)
        cr = cairo.Context(surface)
        
        cr.set_source_rgb(0.8, 0.8, 0.8)
        cr.paint()

        cr.set_source_rgb(0.0, 1.0, 0.0)
        cr.set_line_width(6)
        cr.rectangle(0, 0, 200.0, 200.0) 
        cr.stroke()
        cr.set_source_rgb(0.0, 0.0, 1.0)
        cr.set_line_width(3)
        cr.move_to(0.0, 100.0)
        cr.line_to(200.0, 100.0)
        cr.stroke()
        cr.move_to(100.0, 0.0)
        cr.line_to(100.0, 200.0)
        cr.stroke()

        cr.set_source_rgb(1.0, 0.0, 1.0)
        cr.select_font_face("Arial", cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_BOLD) cr.set_font_size(40)
        (x, y, width, height, dx, dy) = cr.text_extents("Cairo")
        cr.move_to(200/2 - width/2, 200/2 + height/2)     
        cr.show_text("Cairo")

        cr.set_source_rgb(0.0, 0.0, 0.0)
        cr.set_line_width(1)
        cr.rectangle(200/2-width/2, 200/2 - height/2, width, height)
        cr.stroke() 

        return surface

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
   
 
 
-----Original Message-----
From: c.buhtz--- via gtk-app-devel-list <gtk-app-devel-list gnome org>
To: c.buhtz--- via gtk-app-devel-list <gtk-app-devel-list gnome org>
Cc: c.buhtz <c buhtz posteo jp>
Sent: Sun, Sep 9, 2018 2:14 pm
Subject: Re: "draw" icon with string

On 2018-09-09 23:01 "c.buhtz--- via gtk-app-devel-list"
<gtk-app-devel-list gnome org> wrote:
It is unclear to me how to create a cairo.Surface instance.
<https://pycairo.readthedocs.io/en/latest/reference/surfaces.html#cairo.Surface>  

And I can not see any text/string drawing methods in the surface
class. _______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]