cairo 1.4.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of cairo might be problematic. Click here for more details.

@@ -0,0 +1,480 @@
1
+ /* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Ruby Cairo Binding
4
+ *
5
+ * $Author: kou $
6
+ * $Date: 2007/03/06 12:17:34 $
7
+ *
8
+ * Copyright 2005 Øyvind Kolås <pippin@freedesktop.org>
9
+ * Copyright 2004-2005 MenTaLguY <mental@rydia.com>
10
+ *
11
+ * This file is made available under the same terms as Ruby
12
+ *
13
+ */
14
+
15
+ #include "rb_cairo.h"
16
+
17
+ VALUE rb_cCairo_Pattern;
18
+ VALUE rb_cCairo_SolidPattern;
19
+ VALUE rb_cCairo_SurfacePattern;
20
+ VALUE rb_cCairo_GradientPattern;
21
+ VALUE rb_cCairo_LinearPattern;
22
+ VALUE rb_cCairo_RadialPattern;
23
+
24
+ #define _SELF(self) (RVAL2CRPATTERN(self))
25
+
26
+ static inline void
27
+ cr_pattern_check_status (cairo_pattern_t *pattern)
28
+ {
29
+ rb_cairo_check_status (cairo_pattern_status (pattern));
30
+ }
31
+
32
+ cairo_pattern_t *
33
+ rb_cairo_pattern_from_ruby_object (VALUE obj)
34
+ {
35
+ cairo_pattern_t *pattern;
36
+ if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Pattern)))
37
+ {
38
+ rb_raise (rb_eTypeError, "not a cairo pattern");
39
+ }
40
+ Data_Get_Struct (obj, cairo_pattern_t, pattern);
41
+ return pattern;
42
+ }
43
+
44
+ static void
45
+ cr_pattern_free (void *ptr)
46
+ {
47
+ if (ptr)
48
+ {
49
+ cairo_pattern_destroy ((cairo_pattern_t *) ptr);
50
+ }
51
+ }
52
+
53
+ VALUE
54
+ rb_cairo_pattern_to_ruby_object (cairo_pattern_t *pattern, VALUE klass)
55
+ {
56
+ if (pattern)
57
+ {
58
+ cairo_pattern_reference (pattern);
59
+ return Data_Wrap_Struct (klass, NULL, cr_pattern_free, pattern);
60
+ }
61
+ else
62
+ {
63
+ return Qnil;
64
+ }
65
+ }
66
+
67
+ static VALUE
68
+ cr_pattern_allocate (VALUE klass)
69
+ {
70
+ return Data_Wrap_Struct (klass, NULL, cr_pattern_free, NULL);
71
+ }
72
+
73
+ static VALUE
74
+ cr_pattern_initialize (VALUE self)
75
+ {
76
+ rb_raise (rb_eTypeError, "abstract class");
77
+ return Qnil;
78
+ }
79
+
80
+ static VALUE
81
+ cr_pattern_get_type (VALUE self)
82
+ {
83
+ return INT2NUM (cairo_pattern_get_type (_SELF (self)));
84
+ }
85
+
86
+ static VALUE
87
+ cr_solid_pattern_initialize (int argc, VALUE *argv, VALUE self)
88
+ {
89
+ VALUE red, green, blue, alpha;
90
+ int n;
91
+ cairo_pattern_t *pattern;
92
+
93
+ n = rb_scan_args (argc, argv, "13", &red, &green, &blue, &alpha);
94
+
95
+ if (n == 1 && rb_obj_is_kind_of (red, rb_cArray) &&
96
+ (RARRAY (red)->len == 3 || RARRAY (red)->len == 4))
97
+ {
98
+ VALUE ary = red;
99
+ n = RARRAY (ary)->len;
100
+
101
+ red = rb_ary_entry (ary, 0);
102
+ green = rb_ary_entry (ary, 1);
103
+ blue = rb_ary_entry (ary, 2);
104
+ alpha = rb_ary_entry (ary, 3);
105
+ }
106
+
107
+ if (n == 3)
108
+ {
109
+ pattern = cairo_pattern_create_rgb (NUM2DBL (red),
110
+ NUM2DBL (green),
111
+ NUM2DBL (blue));
112
+ }
113
+ else if (n == 4)
114
+ {
115
+ pattern = cairo_pattern_create_rgba (NUM2DBL (red),
116
+ NUM2DBL (green),
117
+ NUM2DBL (blue),
118
+ NUM2DBL (alpha));
119
+ }
120
+ else
121
+ {
122
+ rb_raise (rb_eArgError,
123
+ "invalid argument (expect "
124
+ "(red, green, blue), "
125
+ "([red, green, blue]), "
126
+ "(red, green, blue, alpha) or "
127
+ "([red, green, blue, alpha])"
128
+ ")");
129
+ }
130
+
131
+ cr_pattern_check_status (pattern);
132
+ DATA_PTR (self) = pattern;
133
+ return Qnil;
134
+ }
135
+
136
+ static VALUE
137
+ cr_surface_pattern_initialize (VALUE self, VALUE surface)
138
+ {
139
+ cairo_pattern_t *pattern;
140
+
141
+ pattern = cairo_pattern_create_for_surface (RVAL2CRSURFACE (surface));
142
+ cr_pattern_check_status (pattern);
143
+ DATA_PTR (self) = pattern;
144
+ return Qnil;
145
+ }
146
+
147
+ static VALUE
148
+ cr_linear_pattern_initialize (VALUE self, VALUE x0, VALUE y0,
149
+ VALUE x1, VALUE y1)
150
+ {
151
+ cairo_pattern_t *pattern;
152
+
153
+ pattern = cairo_pattern_create_linear (NUM2DBL (x0), NUM2DBL (y0),
154
+ NUM2DBL (x1), NUM2DBL (y1));
155
+ cr_pattern_check_status (pattern);
156
+ DATA_PTR (self) = pattern;
157
+ return Qnil;
158
+ }
159
+
160
+ static VALUE
161
+ cr_radial_pattern_initialize (VALUE self, VALUE cx0, VALUE cy0, VALUE radius0,
162
+ VALUE cx1, VALUE cy1, VALUE radius1)
163
+ {
164
+ cairo_pattern_t *pattern;
165
+
166
+ pattern = cairo_pattern_create_radial (NUM2DBL (cx0), NUM2DBL (cy0),
167
+ NUM2DBL (radius0),
168
+ NUM2DBL (cx1), NUM2DBL (cy1),
169
+ NUM2DBL (radius1));
170
+ cr_pattern_check_status (pattern);
171
+ DATA_PTR (self) = pattern;
172
+ return Qnil;
173
+ }
174
+
175
+ /* Cairo::GradientPattern */
176
+ static VALUE
177
+ cr_gradient_pattern_add_color_stop_rgb (int argc, VALUE *argv, VALUE self)
178
+ {
179
+ VALUE offset, red, green, blue;
180
+ int n;
181
+
182
+ n = rb_scan_args (argc, argv, "22", &offset, &red, &green, &blue);
183
+
184
+ if (n == 2 && rb_obj_is_kind_of (red, rb_cArray))
185
+ {
186
+ VALUE ary = red;
187
+ n = RARRAY (ary)->len + 1;
188
+
189
+ red = rb_ary_entry (ary, 0);
190
+ green = rb_ary_entry (ary, 1);
191
+ blue = rb_ary_entry (ary, 2);
192
+ }
193
+
194
+ if (n == 4)
195
+ {
196
+ cairo_pattern_add_color_stop_rgb (_SELF (self), NUM2DBL (offset),
197
+ NUM2DBL (red), NUM2DBL (green),
198
+ NUM2DBL (blue));
199
+ }
200
+ else
201
+ {
202
+ VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
203
+ rb_raise (rb_eArgError,
204
+ "invalid argument: %s (expect "
205
+ "(offset, red, green, blue) or "
206
+ "(offset, [red, green, blue])"
207
+ ")",
208
+ StringValuePtr (inspected_arg));
209
+ }
210
+
211
+ cr_pattern_check_status (_SELF (self));
212
+ return self;
213
+ }
214
+
215
+ static VALUE
216
+ cr_gradient_pattern_add_color_stop_rgba (int argc, VALUE *argv, VALUE self)
217
+ {
218
+ VALUE offset, red, green, blue, alpha;
219
+ int n;
220
+
221
+ n = rb_scan_args (argc, argv, "23", &offset, &red, &green, &blue, &alpha);
222
+
223
+ if (n == 2 && rb_obj_is_kind_of (red, rb_cArray))
224
+ {
225
+ VALUE ary = red;
226
+ n = RARRAY (ary)->len + 1;
227
+
228
+ red = rb_ary_entry (ary, 0);
229
+ green = rb_ary_entry (ary, 1);
230
+ blue = rb_ary_entry (ary, 2);
231
+ alpha = rb_ary_entry (ary, 3);
232
+ }
233
+
234
+ if (n == 4 || (n == 5 && NIL_P (alpha)))
235
+ {
236
+ cairo_pattern_add_color_stop_rgb (_SELF (self), NUM2DBL (offset),
237
+ NUM2DBL (red), NUM2DBL (green),
238
+ NUM2DBL (blue));
239
+ }
240
+ else if (n == 5)
241
+ {
242
+ cairo_pattern_add_color_stop_rgba (_SELF (self), NUM2DBL (offset),
243
+ NUM2DBL (red), NUM2DBL (green),
244
+ NUM2DBL (blue), NUM2DBL (alpha));
245
+ }
246
+ else
247
+ {
248
+ VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
249
+ rb_raise (rb_eArgError,
250
+ "invalid argument: %s (expect "
251
+ "(offset, red, green, blue), "
252
+ "(offset, [red, green, blue]), "
253
+ "(offset, red, green, blue, alpha) or "
254
+ "(offset, [red, green, blue, alpha])"
255
+ ")",
256
+ StringValuePtr (inspected_arg));
257
+ }
258
+
259
+ cr_pattern_check_status (_SELF (self));
260
+ return self;
261
+ }
262
+
263
+
264
+ /* Cairo::Pattern */
265
+ static VALUE
266
+ cr_pattern_set_matrix (VALUE self, VALUE matrix)
267
+ {
268
+ cairo_pattern_set_matrix (_SELF (self), RVAL2CRMATRIX (matrix));
269
+ cr_pattern_check_status (_SELF (self));
270
+ return self;
271
+ }
272
+
273
+ static VALUE
274
+ cr_pattern_get_matrix (VALUE self)
275
+ {
276
+ cairo_matrix_t matrix;
277
+ cairo_pattern_get_matrix (_SELF (self), &matrix);
278
+ cr_pattern_check_status (_SELF (self));
279
+ return CRMATRIX2RVAL (&matrix);
280
+ }
281
+
282
+ static VALUE
283
+ cr_pattern_set_extend (VALUE self, VALUE extend)
284
+ {
285
+ cairo_pattern_set_extend (_SELF (self), RVAL2CREXTEND (extend));
286
+ cr_pattern_check_status (_SELF (self));
287
+ return self;
288
+ }
289
+
290
+ static VALUE
291
+ cr_pattern_get_extend (VALUE self)
292
+ {
293
+ return INT2NUM (cairo_pattern_get_extend (_SELF (self)));
294
+ }
295
+
296
+ static VALUE
297
+ cr_pattern_set_filter (VALUE self, VALUE filter)
298
+ {
299
+ cairo_pattern_set_filter (_SELF (self), RVAL2CRFILTER (filter));
300
+ cr_pattern_check_status (_SELF (self));
301
+ return self;
302
+ }
303
+
304
+ static VALUE
305
+ cr_pattern_get_filter (VALUE self)
306
+ {
307
+ return INT2NUM (cairo_pattern_get_filter (_SELF (self)));
308
+ }
309
+
310
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
311
+ static VALUE
312
+ cr_solid_pattern_get_rgba (VALUE self)
313
+ {
314
+ double red, green, blue, alpha;
315
+
316
+ rb_cairo_check_status (cairo_pattern_get_rgba (_SELF (self),
317
+ &red, &green, &blue, &alpha));
318
+ return rb_ary_new3 (4,
319
+ rb_float_new (red), rb_float_new (green),
320
+ rb_float_new (blue), rb_float_new (alpha));
321
+ }
322
+
323
+ static VALUE
324
+ cr_surface_pattern_get_surface (VALUE self)
325
+ {
326
+ cairo_surface_t *surface;
327
+
328
+ rb_cairo_check_status (cairo_pattern_get_surface (_SELF (self), &surface));
329
+ return CRSURFACE2RVAL (surface);
330
+ }
331
+
332
+ static VALUE
333
+ cr_gradient_pattern_get_color_stop_rgba (VALUE self, VALUE index)
334
+ {
335
+ cairo_status_t status;
336
+ double offset, red, green, blue, alpha;
337
+
338
+ status = cairo_pattern_get_color_stop_rgba (_SELF (self), NUM2INT (index),
339
+ &offset, &red, &green, &blue,
340
+ &alpha);
341
+ rb_cairo_check_status (status);
342
+ return rb_ary_new3 (5, rb_float_new (offset),
343
+ rb_float_new (red), rb_float_new (green),
344
+ rb_float_new (blue), rb_float_new (alpha));
345
+ }
346
+
347
+ static VALUE
348
+ cr_gradient_pattern_get_color_stop_count (VALUE self)
349
+ {
350
+ cairo_status_t status;
351
+ int count;
352
+
353
+ status = cairo_pattern_get_color_stop_count (_SELF (self), &count);
354
+ rb_cairo_check_status (status);
355
+ return INT2NUM (count);
356
+ }
357
+
358
+ static VALUE
359
+ cr_linear_pattern_get_linear_points (VALUE self)
360
+ {
361
+ cairo_status_t status;
362
+ double x0, y0, x1, y1;
363
+
364
+ status = cairo_pattern_get_linear_points (_SELF (self), &x0, &y0, &x1, &y1);
365
+ rb_cairo_check_status (status);
366
+ return rb_ary_new3 (4,
367
+ rb_float_new (x0), rb_float_new (y0),
368
+ rb_float_new (x1), rb_float_new (y1));
369
+ }
370
+
371
+ static VALUE
372
+ cr_radial_pattern_get_radial_circles (VALUE self)
373
+ {
374
+ cairo_status_t status;
375
+ double x0, y0, r0, x1, y1, r1;
376
+
377
+ status = cairo_pattern_get_radial_circles (_SELF (self),
378
+ &x0, &y0, &r0,
379
+ &x1, &y1, &r1);
380
+ rb_cairo_check_status (status);
381
+ return rb_ary_new3 (6,
382
+ rb_float_new (x0), rb_float_new (y0), rb_float_new (r0),
383
+ rb_float_new (x1), rb_float_new (y1), rb_float_new (r1));
384
+ }
385
+ #endif
386
+
387
+ /* Cairo::SurfacePattern */
388
+ /* none */
389
+
390
+ void
391
+ Init_cairo_pattern (void)
392
+ {
393
+ rb_cCairo_Pattern =
394
+ rb_define_class_under (rb_mCairo, "Pattern", rb_cObject);
395
+
396
+ rb_define_alloc_func (rb_cCairo_Pattern, cr_pattern_allocate);
397
+
398
+ rb_define_method (rb_cCairo_Pattern, "initialize", cr_pattern_initialize, 0);
399
+ rb_define_method (rb_cCairo_Pattern, "type", cr_pattern_get_type, 0);
400
+
401
+ rb_define_method (rb_cCairo_Pattern, "set_matrix", cr_pattern_set_matrix, 1);
402
+ rb_define_method (rb_cCairo_Pattern, "matrix", cr_pattern_get_matrix, 0);
403
+ rb_define_method (rb_cCairo_Pattern, "set_extend", cr_pattern_set_extend, 1);
404
+ rb_define_alias (rb_cCairo_Pattern, "__extend__", "extend");
405
+ rb_define_method (rb_cCairo_Pattern, "extend", cr_pattern_get_extend, 0);
406
+ rb_define_method (rb_cCairo_Pattern, "set_filter", cr_pattern_set_filter, 1);
407
+ rb_define_method (rb_cCairo_Pattern, "filter", cr_pattern_get_filter, 0);
408
+
409
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_Pattern);
410
+
411
+ rb_cCairo_SolidPattern =
412
+ rb_define_class_under (rb_mCairo, "SolidPattern", rb_cCairo_Pattern);
413
+
414
+ rb_define_method (rb_cCairo_SolidPattern, "initialize",
415
+ cr_solid_pattern_initialize, -1);
416
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
417
+ rb_define_method (rb_cCairo_SolidPattern, "rgba",
418
+ cr_solid_pattern_get_rgba, 0);
419
+ #endif
420
+
421
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_SolidPattern);
422
+
423
+ rb_cCairo_SurfacePattern =
424
+ rb_define_class_under (rb_mCairo, "SurfacePattern", rb_cCairo_Pattern);
425
+
426
+ rb_define_method (rb_cCairo_SurfacePattern, "initialize",
427
+ cr_surface_pattern_initialize, 1);
428
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
429
+ rb_define_method (rb_cCairo_SurfacePattern, "surface",
430
+ cr_surface_pattern_get_surface, 0);
431
+ #endif
432
+
433
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_SurfacePattern);
434
+
435
+ rb_cCairo_GradientPattern =
436
+ rb_define_class_under (rb_mCairo, "GradientPattern", rb_cCairo_Pattern);
437
+
438
+ rb_define_method (rb_cCairo_GradientPattern, "add_color_stop_rgb",
439
+ cr_gradient_pattern_add_color_stop_rgb, -1);
440
+ rb_define_method (rb_cCairo_GradientPattern, "add_color_stop_rgba",
441
+ cr_gradient_pattern_add_color_stop_rgba, -1);
442
+ rb_define_alias (rb_cCairo_GradientPattern,
443
+ "add_color_stop", "add_color_stop_rgba");
444
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
445
+ rb_define_method (rb_cCairo_GradientPattern, "get_color_stop_rgba",
446
+ cr_gradient_pattern_get_color_stop_rgba, 1);
447
+ rb_define_alias (rb_cCairo_GradientPattern,
448
+ "get_color_stop", "get_color_stop_rgba");
449
+ rb_define_method (rb_cCairo_GradientPattern, "color_stop_count",
450
+ cr_gradient_pattern_get_color_stop_count, 0);
451
+ #endif
452
+
453
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_GradientPattern);
454
+
455
+ rb_cCairo_LinearPattern =
456
+ rb_define_class_under (rb_mCairo, "LinearPattern",
457
+ rb_cCairo_GradientPattern);
458
+
459
+ rb_define_method (rb_cCairo_LinearPattern, "initialize",
460
+ cr_linear_pattern_initialize, 4);
461
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
462
+ rb_define_method (rb_cCairo_LinearPattern, "points",
463
+ cr_linear_pattern_get_linear_points, 0);
464
+ #endif
465
+
466
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_LinearPattern);
467
+
468
+ rb_cCairo_RadialPattern =
469
+ rb_define_class_under (rb_mCairo, "RadialPattern",
470
+ rb_cCairo_GradientPattern);
471
+
472
+ rb_define_method (rb_cCairo_RadialPattern, "initialize",
473
+ cr_radial_pattern_initialize, 6);
474
+ #if CAIRO_CHECK_VERSION(1, 3, 0)
475
+ rb_define_method (rb_cCairo_RadialPattern, "circles",
476
+ cr_radial_pattern_get_radial_circles, 0);
477
+ #endif
478
+
479
+ RB_CAIRO_DEF_SETTERS (rb_cCairo_RadialPattern);
480
+ }