def text_path(context, font, size, text, debug=False):
"""Create a Pango text layout and return it as a Cairo path"""
context.save()
pg_layout = PangoCairo.create_layout(context)
pg_context = pg_layout.get_context()
font_options = cairo.FontOptions()
font_options.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
PangoCairo.context_set_font_options(pg_context, font_options)
font_descp = "{0} {1:d}".format(font, size)
pg_layout.set_font_description(Pango.FontDescription(font_descp))
pg_layout.set_text(text, -1) # force length calculation
PangoCairo.update_layout(context, pg_layout)
# TODO watch out for ink & logical, need check
extents = pg_layout.get_pixel_extents()[0]
# TODO debug necessary?
if debug:
context.set_source_rgb(0, 0, 0)
PangoCairo.show_layout(context, pg_layout)
print("text_path: ({0}, {1}, {2}, {3})".format(
extents.x, extents.y, extents.width, extents.height))
context.rectangle(extents.x, extents.y, extents.width, extents.height)
context.set_line_width(1.0)
context.set_line_join(cairo.LINE_JOIN_MITER)
context.set_source_rgb(0, 0.8, 0)
context.stroke()
#PangoCairo.layout_path(context, pg_layout)
PangoCairo.layout_line_path(context, pg_layout.get_line(0))
path = context.copy_path()
# clear the path
context.new_path()
context.restore()
return (path, extents, xheight(pg_layout).height)
评论列表
文章目录