File: diageometrymodule.c

package info (click to toggle)
diacanvas2 0.13.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,360 kB
  • ctags: 2,897
  • sloc: ansic: 20,741; xml: 8,608; sh: 7,458; python: 669; makefile: 536
file content (172 lines) | stat: -rw-r--r-- 3,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

/* include this first, before NO_IMPORT_PYGOBJECT is defined */
#include <pygobject.h>
#include <pygtk/pygtk.h>
#include <diacanvas/dia-canvas.h>

static PyObject *
diacanvaspoint_from_value(const GValue *value)
{
	DiaPoint *point = (DiaPoint*) g_value_get_boxed(value);
	g_message (G_STRLOC": %f %f", point->x, point->y);
	return Py_BuildValue("(dd)", point->x, point->y);
}

static int
diacanvaspoint_to_value (GValue *value, PyObject *py_point)
{
	int i;
	PyObject *sitem;
	gdouble val[2];
	DiaPoint point;

	if (PySequence_Length(py_point) != 2) {
		PyErr_SetString(PyExc_TypeError,
				"argument must be a 2 tuple of floats.");
		return -1;
	}

	for (i = 0; i < 2; i++) {
		sitem = PySequence_GetItem(py_point, i);
		Py_DECREF(sitem);
		sitem = PyNumber_Float(sitem);
		if (sitem)
			val[i] = PyFloat_AsDouble(sitem);
		else {
			PyErr_Clear();
			PyErr_SetString(PyExc_TypeError,
					"sequence item not a float");
			return -1;
		}
		Py_DECREF(sitem);
	}
	point.x = val[0];
	point.y = val[1];

	g_value_set_boxed (value, &point);

	return 0;
}

static PyObject *
diacanvasrectangle_from_value(const GValue *value)
{
	DiaRectangle *rect = (DiaRectangle*) g_value_get_boxed (value);
	return Py_BuildValue("(dddd)", rect->left, rect->top,
				rect->right, rect->bottom);
}

static int
diacanvasrectangle_to_value (GValue *value, PyObject *py_rect)
{
	int i;
	PyObject *sitem;
	gdouble val[4];
	DiaRectangle rect;

	if (PySequence_Length(py_rect) != 4) {
		PyErr_SetString(PyExc_TypeError,
				"argument must be a 4 tuple of floats.");
		return -1;
	}

	for (i = 0; i < 4; i++) {
		sitem = PySequence_GetItem(py_rect, i);
		Py_DECREF(sitem);
		sitem = PyNumber_Float(sitem);
		if (sitem)
			val[i] = PyFloat_AsDouble(sitem);
		else {
			PyErr_Clear();
			PyErr_SetString(PyExc_TypeError,
					"sequence item not a float");
			return -1;
		}
		Py_DECREF(sitem);
	}
	rect.left = val[0];
	rect.top = val[1];
	rect.right = val[2];
	rect.bottom = val[3];

	g_value_set_boxed (value, &rect);

	return 0;
}

static PyObject *
diacanvasaffine_from_value(const GValue *value)
{
	gdouble *a = (gdouble*) g_value_get_boxed (value);
	return Py_BuildValue("(dddddd)", a[0], a[1], a[2], a[3], a[4], a[5]);
}

static int
diacanvasaffine_to_value (GValue *value, PyObject *py_affine)
{
	int i;
	PyObject *sitem;
	gdouble a[6];

	if (PySequence_Length(py_affine) != 6) {
		PyErr_SetString(PyExc_TypeError,
				"argument must be a 6 tuple of floats.");
		return -1;
	}

	for (i = 0; i < 6; i++) {
		sitem = PySequence_GetItem(py_affine, i);
		Py_DECREF(sitem);
		sitem = PyNumber_Float(sitem);
		if (sitem)
			a[i] = PyFloat_AsDouble(sitem);
		else {
			PyErr_Clear();
			PyErr_SetString(PyExc_TypeError,
					"sequence item not a float");
			return -1;
		}
		Py_DECREF(sitem);
	}

	g_value_set_boxed (value, a);

	return 0;
}

void pydiageometry_register_classes (PyObject *d);

extern PyMethodDef pydiageometry_functions[];

DL_EXPORT(void)
initgeometry (void)
{
	PyObject *m, *d;

	/* Standard initialization: */
	init_pygobject ();
	init_pygtk ();

	pyg_register_boxed_custom (DIA_TYPE_POINT,
				   diacanvaspoint_from_value,
				   diacanvaspoint_to_value);
	pyg_register_boxed_custom (DIA_TYPE_RECTANGLE,
				   diacanvasrectangle_from_value,
				   diacanvasrectangle_to_value);
	pyg_register_boxed_custom (DIA_TYPE_AFFINE,
				   diacanvasaffine_from_value,
				   diacanvasaffine_to_value);

	m = Py_InitModule ("diacanvas.geometry", pydiageometry_functions);
	d = PyModule_GetDict (m);

	pydiageometry_register_classes (d);

	if (PyErr_Occurred ()) {
		Py_FatalError ("can't initialise module diacanvas.geometry");
	}
}