lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

19/10/2010 00:44, W Wlourf wrote:
Hello,

Background : I use Lua and Cairo to write some little widgets for Conky
on my Linux System (http://u-scripts.blogspot.com/)

I've seen this example on how to iterating through a cairo_path_t in C.
I can reproduce it in Python but ... not in Lua.


Could you give the Python bindings address (is that pyCairo)?


Here is the sample code from
http://www.dynaset.org/dogusanh/luacairo.html in the section
cairo_path_data_t :

*****
* Here is sample code for iterating through a #cairo_path_t:
*
*
* int i;
* cairo_path_t *path;
* cairo_path_data_t *data;
*
* path = cairo_copy_path (cr);
*
* for (i=0; i < path->num_data; i += path->data[i].header.length) {
* data = &path->data[i];
* switch (data->header.type) {
* case CAIRO_PATH_MOVE_TO:
* do_move_to_things (data[1].point.x, data[1].point.y);
* break;
* case CAIRO_PATH_LINE_TO:
* do_line_to_things (data[1].point.x, data[1].point.y);
* break;
* case CAIRO_PATH_CURVE_TO:
* do_curve_to_things (data[1].point.x, data[1].point.y,
* data[2].point.x, data[2].point.y,
* data[3].point.x, data[3].point.y);
* break;
* case CAIRO_PATH_CLOSE_PATH:
* do_close_path_things ();
* break;
* }
* }
* cairo_path_destroy (path);
*****

What I've done so far is to retrieve the cairo_path_length with .numdata
and cairo_path_data_t with .data :


How? My binding doesn't give access those, I think :)

path=cairo_copy_path(cr)
print ("status,data,num_data",path.status, path.data, path.num_data)

but how to loop through the cairo_path_data_t like in C ??


Will something like as follows work for you?

------------------------------------------------------------------------------
-- cairo_path_t *path;
-- cairo_path_data_t *data;

local path = cairo.copy_path (cr);

local i = 0
local numdata = path_num_data(path)

while i < numdata do
    -- data = &path->data[i];
    local headertype = path_data_header_type(path, i)

    if headertype == CAIRO.PATH_MOVE_TO then

        local x = path_data_point_x(path, i, 1)
        local y = path_data_point_y(path, i, 1)

        do_move_to_things (x, y);

    elseif headertype == CAIRO.PATH_LINE_TO then

        local x = path_data_point_x(path, i, 1)
        local y = path_data_point_y(path, i, 1)

        do_line_to_things (x, y);

    elseif headertype == CAIRO.PATH_CURVE_TO then

        local x1 = path_data_point_x(path, i, 1)
        local y1 = path_data_point_y(path, i, 1)
        local x2 = path_data_point_x(path, i, 2)
        local y2 = path_data_point_y(path, i, 2)
        local x3 = path_data_point_x(path, i, 3)
        local y3 = path_data_point_y(path, i, 3)

        do_curve_to_things (x1, y1, x2, y2, x3, y3);

    elseif headertype == CAIRO.PATH_CLOSE_PATH then

        do_close_path_things ();

    end

    local headerlength = path_data_header_length(path, i)
    i = i + headerlength
end

cairo.path_destroy (path);
-------------------------------------------------------------------------


I someone can help thanks !!

Wlourf