-
Notifications
You must be signed in to change notification settings - Fork 0
/
cairopath.c
376 lines (328 loc) · 10.5 KB
/
cairopath.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* -*- Mode: c; indent-tabs-mode: nil; c-basic-offset: 2; c-file-style: "gnu" -*-
*
* pike bindings for Cairo (http://www.cairographics.org)
*
* Started 2006-02-03
* Copyright (C) 2006, David Vest <davve@davve.net>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include "cairomod.h"
/*! @module Cairo
*/
/*! @class Path
*/
#ifdef HAVE_CAIRO
#ifdef THIS
#undef THIS
#endif
#define THIS ((struct cairo_mod_path *)Pike_fp->current_storage)
struct program *cairo_mod_path_program = NULL;
struct program *cairo_mod_path_iterator_program = NULL;
struct program *cairo_mod_path_element_program = NULL;
static void init_cairo_mod_path( struct object *o )
{
MEMSET(THIS, 0, sizeof(struct cairo_mod_path));
}
static void exit_cairo_mod_path( struct object *o )
{
if (THIS->path)
cairo_path_destroy (THIS->path);
}
static void f_path_create(INT32 args)
{
}
/*! @decl PathIterator _get_iterator()
*!
*! Get an iterator for a given path. The iterator is implicitly used in the
*! @tt{foreach(;;)@} statement.
*!
*! @example
*!
*! Cairo.Context ctx = ...;
*! Cairo.Path p = ctx->copy_path();
*! foreach(p; int i; Cairo.PathElement e)
*! {
*! ... examine e here ...
*! }
*!
*/
static void f_path_get_iterator(INT32 args)
{
struct object* iterator_object;
struct cairo_mod_path_iterator* iterator;
iterator_object = clone_object(cairo_mod_path_iterator_program, 0);
iterator = (struct cairo_mod_path_iterator*)get_storage(iterator_object,
cairo_mod_path_iterator_program);
assert(iterator);
iterator->path_obj = Pike_fp->current_object;
add_ref(Pike_fp->current_object);
push_object(iterator_object);
}
/*! @endclass */
/*! @class PathIterator
*/
#ifdef THIS
#undef THIS
#endif
#define THIS ((struct cairo_mod_path_iterator *)Pike_fp->current_storage)
static void init_cairo_mod_path_iterator(struct object *o)
{
MEMSET(THIS, 0, sizeof(struct cairo_mod_path_iterator));
}
static void exit_cairo_mod_path_iterator(struct object *o)
{
if (THIS->path_obj)
sub_ref(THIS->path_obj);
}
/*! @decl int(0..1) `!()
*! Checks if the iterator is pointing to a valid path element of the path.
*/
static void f_path_iterator_not_operator(INT32 args)
{
struct cairo_mod_path* pw;
cairo_path_t* path;
int j,i;
pw = (struct cairo_mod_path*)get_storage(THIS->path_obj, cairo_mod_path_program);
path = pw->path;
for (i=0,j=0; j<=THIS->idx && i<path->num_data; i+= path->data[i].header.length, j++);
push_int(!(THIS->idx >= 0 && THIS->idx <= j));
}
/*! @decl void `+=(int step)
*! Move @tt{step@} steps forward in the path.
*/
static void f_path_iterator_add_self(INT32 args)
{
int i;
get_all_args("`+=", args, "%d", &i);
THIS->idx += i;
pop_n_elems(args);
ref_push_object(Pike_fp->current_object);
}
/*! @decl int index()
*! Return the current index of the path element that the iterator is pointing at.
*/
static void f_path_iterator_index(INT32 args)
{
push_int(THIS->idx);
}
/*! @decl int value()
*! Return the current path element that the iterator is pointing at.
*/
static void f_path_iterator_value(INT32 args)
{
struct cairo_mod_path* pw;
struct object* o;
struct cairo_mod_path_element* pew;
cairo_path_t* path;
cairo_path_data_t* data = NULL;
int j,i;
pw = (struct cairo_mod_path*)get_storage(THIS->path_obj, cairo_mod_path_program);
path = pw->path;
for (i=0, j=0; i<path->num_data; i+= path->data[i].header.length, j++)
{
if (j == THIS->idx)
{
data = &path->data[i];
break;
}
}
if (!data)
{
}
o = clone_object(cairo_mod_path_element_program, 0);
pew = (struct cairo_mod_path_element*)get_storage(o, cairo_mod_path_element_program);
pew->path_obj = THIS->path_obj;
add_ref(THIS->path_obj);
pew->element = data;
ref_push_object(o);
}
static void f_path_iterator_next(INT32 args)
{
struct cairo_mod_path* pw;
cairo_path_t* path;
int j,i;
pw = (struct cairo_mod_path*)get_storage(THIS->path_obj, cairo_mod_path_program);
path = pw->path;
THIS->idx++;
for (i=0,j=0; j<=THIS->idx && i<path->num_data; i+= path->data[i].header.length, j++);
push_int(THIS->idx >= 0 && THIS->idx < j);
}
static void f_path_iterator_first(INT32 args)
{
struct cairo_mod_path* pw;
cairo_path_t* path;
int j,i;
pw = (struct cairo_mod_path*)get_storage(THIS->path_obj, cairo_mod_path_program);
path = pw->path;
THIS->idx = 0;
push_int(path->num_data > 0);
}
/*! @endclass */
/*! @class PathElement
*/
#ifdef THIS
#undef THIS
#endif
#define THIS ((struct cairo_mod_path_element *)Pike_fp->current_storage)
static void init_cairo_mod_path_element(struct object *o)
{
MEMSET(THIS, 0, sizeof(struct cairo_mod_path_element));
}
static void exit_cairo_mod_path_element(struct object* o)
{
if (THIS->path_obj)
sub_ref(THIS->path_obj);
}
/*! @decl int get_type()
*/
static void f_path_element_get_type(INT32 args)
{
push_int(THIS->element->header.type);
}
/*! @decl mapping(string:float) get_point(int point)
*/
static void f_path_element_get_point(INT32 args)
{
int i;
get_all_args("get_point", args, "%d", &i);
i++; // index includes header
if (i > 0 && i < THIS->element->header.length)
{
push_text( "x" );
push_float(THIS->element[i].point.x);
push_text( "y" );
push_float(THIS->element[i].point.y);
f_aggregate_mapping(4);
}
else
push_undefined();
}
/*! @decl string _sprintf()
*/
static void f_path_element_sprintf(INT32 args)
{
INT_TYPE mode;
struct mapping *opts;
cairo_path_data_t* data = THIS->element;
get_all_args("_sprintf", args, "%i%m", &mode, &opts);
pop_n_elems (args);
if (mode == 'O')
{
switch (data->header.type)
{
case CAIRO_PATH_MOVE_TO:
push_constant_text ("%O(CAIRO_PATH_MOVE_TO(%f,%f))");
ref_push_object(Pike_fp->current_object);
f_object_program (1);
push_float(data[1].point.x); push_float(data[1].point.y);
f_sprintf(4);
break;
case CAIRO_PATH_LINE_TO:
push_constant_text ("%O(CAIRO_PATH_LINE_TO(%f,%f))");
ref_push_object(Pike_fp->current_object);
f_object_program (1);
push_float(data[1].point.x); push_float(data[1].point.y);
f_sprintf(4);
break;
case CAIRO_PATH_CURVE_TO:
push_constant_text ("%O(CAIRO_PATH_CURVE_TO(%f,%f,%f,%f,%f))");
ref_push_object(Pike_fp->current_object);
f_object_program (1);
push_float(data[1].point.x); push_float(data[1].point.y);
push_float(data[2].point.x); push_float(data[2].point.y);
push_float(data[3].point.x); push_float(data[3].point.y);
f_sprintf(8);
break;
case CAIRO_PATH_CLOSE_PATH:
push_constant_text ("%O(CAIRO_PATH_CLOSE_PATH()");
ref_push_object(Pike_fp->current_object);
f_object_program (1);
f_sprintf(2);
break;
default:
push_undefined();
return;
}
}
else
push_undefined();
}
/*! @endclass */
void cairo_mod_init_path(void)
{
/*! @decl constant CAIRO_PATH_MOVE_TO
*! @decl constant CAIRO_PATH_LINE_TO
*! @decl constant CAIRO_PATH_CURVE_TO
*! @decl constant CAIRO_PATH_CLOSE_PATH
*!
*! The various types of path elements in an Cairo path
*/
ADD_INT_CONSTANT("CAIRO_PATH_MOVE_TO", CAIRO_PATH_MOVE_TO, 0);
ADD_INT_CONSTANT("CAIRO_PATH_LINE_TO", CAIRO_PATH_LINE_TO, 0);
ADD_INT_CONSTANT("CAIRO_PATH_CURVE_TO", CAIRO_PATH_CURVE_TO, 0);
ADD_INT_CONSTANT("CAIRO_PATH_CLOSE_PATH", CAIRO_PATH_CLOSE_PATH, 0);
/* Cairo.Path */
start_new_program();
{
ADD_STORAGE(struct cairo_mod_path);
ADD_FUNCTION("_get_iterator", f_path_get_iterator, tFunc(tVoid,tObj), ID_PUBLIC);
set_init_callback(init_cairo_mod_path);
set_exit_callback(exit_cairo_mod_path);
}
add_program_constant("Path",
cairo_mod_path_program = end_program(),
0);
/* Cairo.PathElement */
start_new_program();
{
ADD_STORAGE(struct cairo_mod_path_element);
ADD_FUNCTION("get_type", f_path_element_get_type, tFunc(tNone,tInt), ID_PUBLIC);
ADD_FUNCTION("get_point", f_path_element_get_point, tFunc(tInt, tMapping), ID_PUBLIC);
ADD_FUNCTION("_sprintf", f_path_element_sprintf, tFunc(tInt tMapping, tStr), ID_STATIC);
set_init_callback(init_cairo_mod_path_element);
set_exit_callback(exit_cairo_mod_path_element);
}
add_program_constant("PathElement",
cairo_mod_path_element_program = end_program(),
0);
/* Cairo.PathIterator */
start_new_program();
{
ADD_STORAGE(struct cairo_mod_path_iterator);
ADD_FUNCTION("`!", f_path_iterator_not_operator, tFunc(tVoid,tInt), ID_PUBLIC);
ADD_FUNCTION("`+=", f_path_iterator_add_self, tFunc(tInt,tObj), ID_PUBLIC);
ADD_FUNCTION("index", f_path_iterator_index, tFunc(tNone,tObj), ID_PUBLIC);
ADD_FUNCTION("value", f_path_iterator_value, tFunc(tNone,tObj), ID_PUBLIC);
ADD_FUNCTION("first", f_path_iterator_first, tFunc(tNone,tInt), ID_PUBLIC);
ADD_FUNCTION("next", f_path_iterator_next, tFunc(tNone,tObj), ID_PUBLIC);
set_init_callback(init_cairo_mod_path_iterator);
set_exit_callback(exit_cairo_mod_path_iterator);
}
add_program_constant("PathIterator",
cairo_mod_path_iterator_program = end_program(),
0);
}
/*! @endmodule */
#endif // HAVE_CAIRO