gint asCEnum(USER_OBJECT_ s_enum, GType etype) { GEnumClass *eclass = g_type_class_ref(etype); GEnumValue *evalue = NULL; gint eval = 0; if (IS_INTEGER(s_enum) || IS_NUMERIC(s_enum)) { eval = asCInteger(s_enum); evalue = g_enum_get_value(eclass, eval); if (evalue == NULL) { PROBLEM "Could not map to enum value %d", asCInteger(s_enum) ERROR; } } else if (IS_CHARACTER(s_enum)) { const gchar* ename = asCString(s_enum); evalue = g_enum_get_value_by_name(eclass, ename); if (evalue == NULL) evalue = g_enum_get_value_by_nick(eclass, ename); if (evalue == NULL) evalue = g_enum_get_value(eclass, atoi(ename)); if (evalue == NULL) { PROBLEM "Could not parse enum value %s", asCString(s_enum) ERROR; } } eval = evalue->value; return(eval); }
/** the cairo people say that we shouldn't do this - oh well */ cairo_path_t * asCCairoPath(USER_OBJECT_ s_path) { cairo_path_t *path; cairo_path_data_t *element; GSList *data = NULL, *cur; gint i,j; /* init path structure */ path = (cairo_path_t*)R_alloc(1, sizeof(cairo_path_t)); /* set status code */ path->status = CAIRO_STATUS_SUCCESS; /* for each path element, create points according to type and store in list */ for (i = 0; i < GET_LENGTH(s_path); i++) { USER_OBJECT_ s_element = VECTOR_ELT(s_path, i); int points = 0, len; cairo_path_data_type_t type = asCInteger(getAttrib(s_element, install("type"))); /* how many points do we need for this type of element? */ switch(type) { case CAIRO_PATH_MOVE_TO: case CAIRO_PATH_LINE_TO: points = 1; break; case CAIRO_PATH_CURVE_TO: points = 3; break; case CAIRO_PATH_CLOSE_PATH: points = 0; break; default: PROBLEM "Converting Cairo path: did not understand type %d", type ERROR; } len = points + 1; /* have to include header */ element = (cairo_path_data_t*)R_alloc(len, sizeof(cairo_path_data_t)); /* define header element */ element[0].header.type = type; element[0].header.length = len; data = g_slist_append(data, &element[0]); /* add header to list */ for (j = 1; j < len; j++) { /* define points */ element[j].point.x = INTEGER_DATA(s_element)[2*j]; element[j].point.y = INTEGER_DATA(s_element)[2*j+1]; data = g_slist_append(data, &element[j]); /* add point to list */ } } /* initialize the path's data array */ path->num_data = g_slist_length(data); path->data = (cairo_path_data_t*)R_alloc(path->num_data, sizeof(cairo_path_data_t)); /* copy list into array */ cur = data; for(i = 0; i < path->num_data; i++) { path->data[i] = ((cairo_path_data_t*)cur->data)[0]; cur = g_slist_next(cur); } return(path); }