def render_text(text, filepath, params):
    size = params.get('size', 18)
    weight = cairo.FONT_WEIGHT_NORMAL
    style = cairo.FONT_SLANT_NORMAL
    font = params.get('font', 'Sans')

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
    context = cairo.Context(surface)
    context.select_font_face(font, style, weight)
    context.set_font_size(size)
    extents = context.text_extents(text)
    x = -extents[0]
    baseline = params.get('baseline', -extents[1])
    width = max(1, extents[2])
    height = max(1, params.get('height', extents[3]))

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
    font_options = surface.get_font_options()
    font_options.set_antialias(cairo.ANTIALIAS_GRAY)
    context = cairo.Context(surface)

    # Paint the background, default is white.
    background = convert_color(params.get('background', (1, 1, 1, 1)))
    context.set_source_rgba(*background)
    context.paint()

    color = convert_color(params.get('color', (0, 0, 0, 1))) # black
    context.set_source_rgba(*color)

    context.select_font_face(font, style, weight)
    context.set_font_size(size)
    # We need to adjust by the text's offsets to center it.
    context.move_to(x, baseline)
    # We stroke and fill to make sure thinner parts are visible.
    context.text_path(text)
    #context.set_line_width(0.05)
    #context.stroke_preserve()
    context.fill()

    filepath = abspath(filepath)
    try:
        surface.write_to_png(filepath)
    except __HOLE__, e:
        raise IOError("Can't save image in %r: %s\n"
                      "Text: %r\n"
                      "Parameters: %r" % (filepath, e, text, params))
    optimizer = Optimizer()
    if optimizer.is_enabled():
        optimizer.optimize(filepath)
    surface.finish()

    return int(width), int(height)
dataset/ETHPy150Open akaihola/django-cairo-text/cairotext/templatetags/cairotext.py/render_text