def text_block(ctx, obj, text, font, lang="en-US", debug=False):
ctx.save()
lyt = PangoCairo.create_layout(ctx)
pg_ctx = lyt.get_context()
pg_ctx.set_language(Pango.Language.from_string(lang))
fo = cairo.FontOptions()
fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
PangoCairo.context_set_font_options(pg_ctx, fo)
font_family = font.family if not font.replace else font.replace
pg_font = Pango.FontDescription("{} {}px".format(font_family, font.size))
lyt.set_font_description(pg_font)
lyt.set_markup(text, -1) # force length calculation
lyt.set_height(obj.height * Pango.SCALE * 1.5) # TODO what?
lyt.set_width(obj.width * Pango.SCALE)
lyt.set_alignment(Pango.Alignment.CENTER)
#PangoCairo.update_layout(ctx, lyt)
pg_size = lyt.get_pixel_size()
ink, logical = lyt.get_pixel_extents()
while ink.height > obj.height and pg_font.get_size() > 0:
pg_font.set_size(pg_font.get_size() - Pango.SCALE)
lyt.set_font_description(pg_font)
ink, logical = lyt.get_pixel_extents()
#x = obj["x"] - pext.x - pext.width / 2
#y = obj["y"] - pext.y - pext.height / 2
x = (obj.x + obj.width / 2) - ((ink.x + ink.width / 2))
y = (obj.y + obj.height / 2) - ((ink.y + ink.height / 2))
#x = obj["x"]
#y = obj["y"]
if debug:
print("x,y: %s, %s" % (x, y))
ctx.translate(x, y)
PangoCairo.update_layout(ctx, lyt)
PangoCairo.layout_path(ctx, lyt)
ctx.set_line_width(9.0)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_source_rgb(*font.color)
PangoCairo.show_layout(ctx, lyt)
ctx.new_path()
ctx.restore()
if debug:
rectangle(ctx, ink.x, ink.y, ink.width, ink.height, True, 2.0, (0, 1, 0))
rectangle(ctx, obj.x, obj.y, obj.width, obj.height, True, 2.0, (0.8, 0.8, 0))
评论列表
文章目录