Ejemplo n.º 1
0
    def get_text_layout(self, ctx, text=None, exclude_extents=True):
        layout = PangoCairo.create_layout(ctx)
        pango_font_desc = Pango.FontDescription.from_string(self.font)
        layout.set_wrap(Pango.WrapMode(0))
        layout.set_font_description(pango_font_desc)

        if text is None:
            text = self.display_text
        layout.set_markup(text)

        if (self.max_width_chars > 0):
            font_size = pango_font_desc.get_size()
            if pango_font_desc.get_size_is_absolute():
                font_size *= Pango.SCALE
            layout.set_width(int(self.max_width_chars * font_size))

        text_rect = layout.get_pixel_extents()[0]
        text_width = text_rect.width
        text_height = text_rect.height
        text_left = text_rect.x
        text_top = text_rect.y

        if (self.max_width_chars > 0):
            text_width = layout.get_width() * 1. / Pango.SCALE

        text_point_rect = layout.get_extents()[0]

        if self.max_width_chars <= 0:  # or self.exposure<1:
            layout.set_width(int(text_point_rect.width))

        if self.x_align == X_ALIGN_LEFT:
            x = self.border_width + self.corner_radius
        elif self.x_align == X_ALIGN_RIGHT:
            x = self.width - text_width - self.border_width - self.corner_radius
        elif self.x_align == X_ALIGN_CENTER:
            x = (self.width - text_width) * .5

        if self.y_align == Y_ALIGN_TOP:
            y = self.border_width + self.corner_radius
        elif self.y_align == Y_ALIGN_BOTTOM:
            y = self.height - text_height - self.border_width - self.corner_radius
        elif self.y_align == Y_ALIGN_MIDDLE:
            y = (self.height - text_height) * .5

        if isinstance(self.font_color, GradientColor):
            ctx.set_source(self.font_color.get_pattern())
        elif self.font_color is not None:
            ctx.set_source_rgba(*self.font_color.get_array())

        layout.set_alignment(Pango.Alignment(int(round(self.line_align))))

        PangoCairo.update_layout(ctx, layout)
        if exclude_extents:
            x -= text_left
            y -= text_top
        return layout, x, y
Ejemplo n.º 2
0
    def calculate_text_size(self):
        surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 320, 120)
        context = cairo.Context(surf)

        layout = PangoCairo.create_layout(context)
        pango_font_desc = Pango.FontDescription.from_string(self.font)
        layout.set_wrap(Pango.WrapMode(0))
        layout.set_font_description(pango_font_desc)

        #from "pose", line-align may become float
        layout.set_alignment(Pango.Alignment(int(self.line_align)))

        if (self.max_width_chars > 0):
            layout.set_width(
                int(self.max_width_chars * pango_font_desc.get_size()))

        try:
            layout.set_markup(self.text)
        except:
            pass

        return layout.get_pixel_extents()[0]
Ejemplo n.º 3
0
def draw_text(ctx, text,
              x, y, width=None, corner=5, align=None, fit_width=False,
              height=None, fit_height=False,
              text_color=None, back_color=None, border_color=None, border_width=1,
              padding=0, font_name=None, pre_draw=None):

    layout = PangoCairo.create_layout(ctx)
    font = Pango.FontDescription.from_string(font_name)
    layout.set_wrap(Pango.WrapMode(0))
    layout.set_font_description(font)
    layout.set_alignment(Pango.Alignment(0))

    layout.set_markup(text)

    text_rect = layout.get_pixel_extents()[1]
    l = text_rect.x
    t = text_rect.y
    w= text_rect.width
    h = text_rect.height

    scale_x = 1
    scale_y = 1
    if width:
        if width<w:
            if fit_width:
                scale_x = width/float(w)
        else:
            layout.set_width(int(width*Pango.SCALE))
            #w = width
    else:
        width = w

    if height:
        if height<h:
            if fit_height:
                scale_y = height/float(h)
        else:
            h = height
    else:
        height = 0

    if align:
        if align.find("bottom-center")>=0:
            y -= t+h*scale_y+2*padding
        elif align.find("bottom")>=0:
            y -= -t+h*scale_y+padding

        if align.find("right")>=0:
            x -= w*scale_x+padding
        elif align.find("hcenter")>=0:
            x -= (w*scale_x+padding)*.5

    if back_color:
        ctx.save()
        if pre_draw: pre_draw(ctx)
        ctx.translate(x, y)
        ctx.scale(scale_x, scale_y)
        draw_rounded_rectangle(ctx, 0, 0, w+2*padding, h+2*padding, corner)
        ctx.restore()
        draw_fill(ctx, back_color)

    if border_color:
        ctx.save()
        if pre_draw: pre_draw(ctx)
        ctx.translate(x, y)
        ctx.scale(scale_x, scale_y)
        draw_rounded_rectangle(ctx, 0, 0, w+2*padding, h+2*padding, corner)
        ctx.restore()
        draw_stroke(ctx, border_width, border_color)
    if not text_color:
        ctx.set_source_rgba(0,0,0,1)
    elif isinstance(text_color, Color):
        ctx.set_source_rgba(*text_color.get_array())
    elif type(text_color) is str:
        ctx.set_source_rgba(*Color.from_html(text_color).get_array())

    ctx.save()
    if pre_draw: pre_draw(ctx)
    ctx.translate(x, y)

    ctx.move_to(padding, padding)
    ctx.scale(scale_x, scale_y)
    PangoCairo.show_layout(ctx, layout)
    ctx.restore()

    return Rect(x, y, w+2*padding, h+2*padding)