def write_text(self): if not self.text: # Nothing useful to draw, return now! return if self.resized( ) or self.text != self.prev_text or self.text_overlay is None: # Need to recreate the cache drawing self.text_overlay = ImageSurface(FORMAT_ARGB32, self.width, self.height) ctx = Context(self.text_overlay) pg = CairoContext(ctx) pl = pg.create_layout() pl.set_font_description(self.text_font) pl.set_width(-1) # No text wrapping pl.set_text(self.text) plsize = pl.get_size() text_width = plsize[0] // SCALE text_height = plsize[1] // SCALE area_width_without_text = self.width - text_width area_height_without_text = self.height - text_height ctx.move_to(area_width_without_text // 2, area_height_without_text // 2) ctx.set_source_rgb(1, 1, 1) pg.update_layout(pl) pg.show_layout(pl) self.cr.set_source_surface(self.text_overlay) self.cr.paint()