下面的代碼示例是在Lua中,旨在與Conky一起使用。我的想法是計算一條複雜的路徑,保存它,然後渲染它兩次 - 一次作爲填充區域,然後再次作爲描邊路徑,從而創建一個輪廓區域。開羅保存的路徑與變換渲染「向後」
問題是,與直接呈現相比,保存的路徑似乎呈現「倒退」。
第一部分用綠色創建一個旋轉的L形狀,該形狀如預期般出現。
第二部分嘗試創建與保存路徑相同的L形,然後將其渲染爲紅色。由此產生的路徑不會旋轉,但由於在追加路徑之前我沒有調用cairo_rotate()
,所以我認爲這是有道理的。
第三部分呈現一組逐漸旋轉的青色L形狀,這與預期相符。
第四部分嘗試執行與第三部分相同的操作,這次使用保存的路徑並呈現爲黃色。但是路徑出現了錯誤的方向。
我的猜測是開羅變換函數將它們的變換應用到在cairo_t
上下文中積累的未削弱路徑分量(如果有的話)。但我不太明白爲什麼它會以一種方式「向前」,而另一種方式則是「向後」。
我對開羅路徑和轉換有什麼誤解?
function tests (cr)
local wiggle
local mat_base = cairo_matrix_t:create()
tolua.takeownership (mat_base)
cairo_set_line_width (cr, 4)
cairo_identity_matrix (cr)
---
-- Direct stroke of L-shaped figure.
cairo_save (cr)
cairo_translate (cr, 128, 128)
cairo_new_path (cr)
cairo_rotate (cr, math.pi/6)
cairo_move_to (cr, 150, 0)
cairo_line_to (cr, 0, 0)
cairo_line_to (cr, 0, 50)
cairo_set_source_rgb (cr, 0, 1, 0)
cairo_stroke (cr)
cairo_restore (cr)
---
-- Capture L-shaped figure as path, then render.
cairo_save (cr)
cairo_translate (cr, 384, 128)
cairo_new_path (cr)
cairo_rotate (cr, math.pi/6)
cairo_move_to (cr, 150, 0)
cairo_line_to (cr, 0, 0)
cairo_line_to (cr, 0, 50)
wiggle = cairo_copy_path (cr)
cairo_restore (cr)
cairo_save (cr)
cairo_translate (cr, 384, 128)
cairo_new_path (cr)
cairo_append_path (cr, wiggle)
cairo_set_source_rgb (cr, 1, 0, 0)
cairo_stroke (cr)
cairo_path_destroy (wiggle)
cairo_restore (cr)
---
-- Direct stroke of a set of rotated L-shaped figures.
cairo_save (cr)
cairo_translate (cr, 640, 128)
cairo_get_matrix (cr, mat_base)
cairo_new_path (cr)
for i = 0, 4 do
cairo_set_matrix (cr, mat_base)
cairo_rotate (cr, i * math.pi/6)
cairo_move_to (cr, 150, 0)
cairo_line_to (cr, 0, 0)
cairo_line_to (cr, 0, 50)
end
cairo_set_source_rgb (cr, 0, 1, 1)
cairo_stroke (cr)
cairo_restore (cr)
---
-- Capture rotated L-shaped figures as path, then render.
cairo_save (cr)
cairo_translate (cr, 896, 128)
cairo_get_matrix (cr, mat_base)
cairo_new_path (cr)
for i = 0, 4 do
cairo_set_matrix (cr, mat_base)
cairo_rotate (cr, i * math.pi/6)
cairo_move_to (cr, 150, 0)
cairo_line_to (cr, 0, 0)
cairo_line_to (cr, 0, 50)
end
wiggle = cairo_copy_path (cr)
cairo_restore (cr)
cairo_save (cr)
cairo_new_path (cr)
cairo_translate (cr, 896, 128)
cairo_append_path (cr, wiggle)
cairo_set_source_rgb (cr, 1, 1, 0)
cairo_stroke (cr)
cairo_path_destroy (wiggle)
cairo_restore (cr)
--
end