【发布时间】:2018-09-16 20:52:33
【问题描述】:
我是 lua 新手,并试图通过为 conky 创建脚本来深入了解它。在我的示例中,我试图将 cairo 功能封装到可以添加到画布的 Canvas 对象 和 drawable 对象(即文本对象)中。
当我尝试将 cairo_surface 和 cairo 对象存储在表中时,我无法再使用它们了。即使没有发生错误(没有消息或段错误或泄漏)在第二个示例中没有显示任何文本。
此示例有效:
Canvas = {
init = function (w)
local cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height)
local cr = cairo_create(cs)
return cr, cs
end,
destroy = function (cr, cs)
cairo_destroy(cr)
cairo_surface_destroy(cs)
end
}
function conky_main ()
if conky_window == nil then
return
else
local cr, cs = Canvas.init(conky_window)
local tx = Text:new{text="Hello World!"}
tx:draw(cr)
Canvas.destroy(cr, cs)
end
end
此示例不起作用:
Canvas = {
init = function (w) -- returns table instead of 2 variables
return {
cs = cairo_xlib_surface_create(w.display,w.drawable,w.visual,w.width,w.height),
cr = cairo_create(cs)
}
end,
destroy = function (cnv)
cairo_destroy(cnv.cr)
cairo_surface_destroy(cnv.cs)
end
}
function conky_main ()
if conky_window == nil then
return
else
local cnv = Canvas.init(conky_window)
local tx = Text:new{text="Hello World!"}
tx:draw(cnv.cr) -- access table member instead of variable
Canvas.destroy(cnv)
end
end
【问题讨论】: