cairo_text_extents_t 未被 lua 识别

cairo_text_extents_t is not recognized by lua

我正在 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)

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

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

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

cairo_text_extents_t:create() function
Call this function to return a new cairo_text_extents_t structure. A creation function for this structure is not provided by the cairo API. After calling this, you should use tolua.takeownership() on the return value to ensure ownership is passed properly.

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

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)