我正在用 lua 编写一些小部件,供 conky 用来显示一些东西。
我到达了一个点,我想将文本居中。按照 this 教程,我将 C 代码移植到 lua 代码中,现在看起来像这样:

local extents
local utf8 = "cairo"
local x, y
cairo_select_font_face(cr, "Ubuntu", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_set_font_size(cr, 13)
cairo_text_extents(cr, utf8, extents)
x = 128.0 - (extents.width / 2 + extents.x_bearing)
y = 128.0 - (extents.height / 2 + extents.y_bearing)

cairo_move_to(cr, x, y)
cairo_show_text(cr, utf8)

我现在处理的问题是应该传递给 Ccairo_text_extents_t 数据类型 cairo_text_extents 没有被 lua 识别,实际上 conky 关闭时没有任何输出。

有没有办法让 lua 识别该数据类型?

最佳答案

我终于找到了答案。在 conky 中有一个函数可以满足我的需要,如指定的 here :



因此,执行以下操作就足够了:

local extents = cairo_text_extents_t:create()
tolua.takeownership(extents)
local utf8 = "cairo"
local x, y
cairo_select_font_face(cr, "Ubuntu", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_set_font_size(cr, 52)
cairo_text_extents(cr, utf8, extents)
x = 128.0 - (extents.width / 2 + extents.x_bearing)
y = 128.0 - (extents.height / 2 + extents.y_bearing)

cairo_move_to (cr, x, y)
cairo_show_text (cr, utf8)

关于lua - lua 无法识别 cairo_text_extents_t,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42671518/

10-11 20:57
查看更多