コード例 #1
0
    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()
コード例 #2
0
 def renderText(self, surface,
                text, y_offset, size,
                shade, w=(2,3), wrap=True):
     if len(text) < 1:
         return
     def setdesc(l, size):
         l.set_font_description(
                 FontDescription(
                     self.font + " " + str(size)))
     origin = Context(surface)
     origin.translate(self.w * (w[1] - w[0]) /
                      (w[1] * 2), y_offset)
     box = CairoContext(origin)
 #  Start laying out text 
     layout = box.create_layout()
     setdesc(layout, size)
     width = self.w * w[0] / w[1]
     if wrap:
         layout.set_width(width * pango.SCALE)
     else:
         layout.set_width(-1)
     layout.set_alignment(pango.ALIGN_CENTER)
     layout.set_text(text)
 #   Resize text to make sure it doesn't exceed width.
     wi, n = layout.get_pixel_size()
     if wi > width:
         s = size * width / wi
         setdesc(layout, s)
     layout.set_width(width * pango.SCALE)
 #   Draw a transparent pane behind the text
     origin.set_source_rgba(1, 1, 1, 0.7)
     origin.rectangle(*layout.get_pixel_extents()[1])
     origin.fill()
 #   Draw text
     origin.set_source_rgb(shade, shade, shade)
     box.update_layout(layout)
     box.show_layout(layout)