cairo 1.4.1 → 1.5.0

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.

@@ -3,7 +3,7 @@
3
3
  * Ruby Cairo Binding
4
4
  *
5
5
  * $Author: kou $
6
- * $Date: 2007/03/06 12:17:34 $
6
+ * $Date: 2007/05/22 14:24:34 $
7
7
  *
8
8
  * Copyright 2005 Øyvind Kolås <pippin@freedesktop.org>
9
9
  * Copyright 2004-2005 MenTaLguY <mental@rydia.com>
@@ -13,6 +13,7 @@
13
13
  */
14
14
 
15
15
  #include "rb_cairo.h"
16
+ #include "rb_cairo_private.h"
16
17
 
17
18
  VALUE rb_cCairo_Pattern;
18
19
  VALUE rb_cCairo_SolidPattern;
@@ -21,19 +22,57 @@ VALUE rb_cCairo_GradientPattern;
21
22
  VALUE rb_cCairo_LinearPattern;
22
23
  VALUE rb_cCairo_RadialPattern;
23
24
 
25
+ static ID id_parse, id_to_rgb, id_to_a, id_inspect;
26
+
24
27
  #define _SELF(self) (RVAL2CRPATTERN(self))
25
28
 
29
+ static VALUE
30
+ cr_color_parse (VALUE color)
31
+ {
32
+ return rb_funcall (rb_mCairo_Color, id_parse, 1, color);
33
+ }
34
+
26
35
  static inline void
27
36
  cr_pattern_check_status (cairo_pattern_t *pattern)
28
37
  {
29
38
  rb_cairo_check_status (cairo_pattern_status (pattern));
30
39
  }
31
40
 
41
+ static VALUE
42
+ cr_pattern_get_klass (cairo_pattern_t *pattern)
43
+ {
44
+ VALUE klass;
45
+ cairo_pattern_type_t type;
46
+
47
+ type = cairo_pattern_get_type (pattern);
48
+ switch (type)
49
+ {
50
+ case CAIRO_PATTERN_TYPE_SOLID:
51
+ klass = rb_cCairo_SolidPattern;
52
+ break;
53
+ case CAIRO_PATTERN_TYPE_SURFACE:
54
+ klass = rb_cCairo_SurfacePattern;
55
+ break;
56
+ case CAIRO_PATTERN_TYPE_LINEAR:
57
+ klass = rb_cCairo_LinearPattern;
58
+ break;
59
+ case CAIRO_PATTERN_TYPE_RADIAL:
60
+ klass = rb_cCairo_RadialPattern;
61
+ break;
62
+ default:
63
+ rb_raise (rb_eArgError, "unknown pattern type: %d", type);
64
+ break;
65
+ }
66
+
67
+ return klass;
68
+ }
69
+
70
+
32
71
  cairo_pattern_t *
33
72
  rb_cairo_pattern_from_ruby_object (VALUE obj)
34
73
  {
35
74
  cairo_pattern_t *pattern;
36
- if (!RTEST (rb_obj_is_kind_of (obj, rb_cCairo_Pattern)))
75
+ if (!rb_cairo__is_kind_of (obj, rb_cCairo_Pattern))
37
76
  {
38
77
  rb_raise (rb_eTypeError, "not a cairo pattern");
39
78
  }
@@ -51,10 +90,12 @@ cr_pattern_free (void *ptr)
51
90
  }
52
91
 
53
92
  VALUE
54
- rb_cairo_pattern_to_ruby_object (cairo_pattern_t *pattern, VALUE klass)
93
+ rb_cairo_pattern_to_ruby_object (cairo_pattern_t *pattern)
55
94
  {
56
95
  if (pattern)
57
96
  {
97
+ VALUE klass;
98
+ klass = cr_pattern_get_klass (pattern);
58
99
  cairo_pattern_reference (pattern);
59
100
  return Data_Wrap_Struct (klass, NULL, cr_pattern_free, pattern);
60
101
  }
@@ -77,12 +118,6 @@ cr_pattern_initialize (VALUE self)
77
118
  return Qnil;
78
119
  }
79
120
 
80
- static VALUE
81
- cr_pattern_get_type (VALUE self)
82
- {
83
- return INT2NUM (cairo_pattern_get_type (_SELF (self)));
84
- }
85
-
86
121
  static VALUE
87
122
  cr_solid_pattern_initialize (int argc, VALUE *argv, VALUE self)
88
123
  {
@@ -91,13 +126,22 @@ cr_solid_pattern_initialize (int argc, VALUE *argv, VALUE self)
91
126
  cairo_pattern_t *pattern;
92
127
 
93
128
  n = rb_scan_args (argc, argv, "13", &red, &green, &blue, &alpha);
94
-
95
- if (n == 1 && rb_obj_is_kind_of (red, rb_cArray) &&
129
+
130
+ if (n == 1)
131
+ {
132
+ VALUE color = red;
133
+
134
+ color = cr_color_parse (color);
135
+ if (rb_cairo__is_kind_of (color, rb_cCairo_Color_Base))
136
+ red = rb_funcall (rb_funcall (color, id_to_rgb, 0), id_to_a, 0);
137
+ }
138
+
139
+ if (n == 1 && rb_cairo__is_kind_of (red, rb_cArray) &&
96
140
  (RARRAY (red)->len == 3 || RARRAY (red)->len == 4))
97
141
  {
98
142
  VALUE ary = red;
99
143
  n = RARRAY (ary)->len;
100
-
144
+
101
145
  red = rb_ary_entry (ary, 0);
102
146
  green = rb_ary_entry (ary, 1);
103
147
  blue = rb_ary_entry (ary, 2);
@@ -119,15 +163,25 @@ cr_solid_pattern_initialize (int argc, VALUE *argv, VALUE self)
119
163
  }
120
164
  else
121
165
  {
166
+ VALUE inspected;
167
+
168
+ inspected = rb_funcall (argc == 1 ? red : rb_ary_new4 (argc, argv),
169
+ id_inspect, 0);
122
170
  rb_raise (rb_eArgError,
123
- "invalid argument (expect "
171
+ "invalid argument: %s (expect "
172
+ "(color_name), "
173
+ "(color_hex_triplet), "
174
+ "(Cairo::Color::RGB), "
175
+ "(Cairo::Color::CMYK), "
176
+ "(Cairo::Color::HSV), "
124
177
  "(red, green, blue), "
125
178
  "([red, green, blue]), "
126
179
  "(red, green, blue, alpha) or "
127
180
  "([red, green, blue, alpha])"
128
- ")");
181
+ ")",
182
+ RVAL2CSTR (inspected));
129
183
  }
130
-
184
+
131
185
  cr_pattern_check_status (pattern);
132
186
  DATA_PTR (self) = pattern;
133
187
  return Qnil;
@@ -174,57 +228,27 @@ cr_radial_pattern_initialize (VALUE self, VALUE cx0, VALUE cy0, VALUE radius0,
174
228
 
175
229
  /* Cairo::GradientPattern */
176
230
  static VALUE
177
- cr_gradient_pattern_add_color_stop_rgb (int argc, VALUE *argv, VALUE self)
231
+ cr_gradient_pattern_add_color_stop_generic (int argc, VALUE *argv, VALUE self)
178
232
  {
179
- VALUE offset, red, green, blue;
233
+ VALUE offset, red, green, blue, alpha;
180
234
  int n;
181
-
182
- n = rb_scan_args (argc, argv, "22", &offset, &red, &green, &blue);
183
235
 
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
- }
236
+ n = rb_scan_args (argc, argv, "23", &offset, &red, &green, &blue, &alpha);
193
237
 
194
- if (n == 4)
238
+ if (n == 2)
195
239
  {
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
- }
240
+ VALUE color = red;
214
241
 
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);
242
+ color = cr_color_parse (color);
243
+ if (rb_cairo__is_kind_of (color, rb_cCairo_Color_Base))
244
+ red = rb_funcall (rb_funcall (color, id_to_rgb, 0), id_to_a, 0);
245
+ }
222
246
 
223
- if (n == 2 && rb_obj_is_kind_of (red, rb_cArray))
247
+ if (n == 2 && rb_cairo__is_kind_of (red, rb_cArray))
224
248
  {
225
249
  VALUE ary = red;
226
250
  n = RARRAY (ary)->len + 1;
227
-
251
+
228
252
  red = rb_ary_entry (ary, 0);
229
253
  green = rb_ary_entry (ary, 1);
230
254
  blue = rb_ary_entry (ary, 2);
@@ -245,22 +269,28 @@ cr_gradient_pattern_add_color_stop_rgba (int argc, VALUE *argv, VALUE self)
245
269
  }
246
270
  else
247
271
  {
248
- VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
272
+ VALUE inspected;
273
+
274
+ inspected = rb_funcall (rb_ary_new4 (argc, argv), id_inspect, 0);
249
275
  rb_raise (rb_eArgError,
250
276
  "invalid argument: %s (expect "
277
+ "(offset, color_name), "
278
+ "(offset, color_hex_triplet), "
279
+ "(offset, Cairo::Color::RGB), "
280
+ "(offset, Cairo::Color::CMYK), "
281
+ "(offset, Cairo::Color::HSV), "
251
282
  "(offset, red, green, blue), "
252
283
  "(offset, [red, green, blue]), "
253
284
  "(offset, red, green, blue, alpha) or "
254
285
  "(offset, [red, green, blue, alpha])"
255
286
  ")",
256
- StringValuePtr (inspected_arg));
287
+ RVAL2CSTR (inspected));
257
288
  }
258
289
 
259
290
  cr_pattern_check_status (_SELF (self));
260
291
  return self;
261
292
  }
262
293
 
263
-
264
294
  /* Cairo::Pattern */
265
295
  static VALUE
266
296
  cr_pattern_set_matrix (VALUE self, VALUE matrix)
@@ -320,6 +350,12 @@ cr_solid_pattern_get_rgba (VALUE self)
320
350
  rb_float_new (blue), rb_float_new (alpha));
321
351
  }
322
352
 
353
+ static VALUE
354
+ cr_solid_pattern_get_color (VALUE self)
355
+ {
356
+ return cr_color_parse (cr_solid_pattern_get_rgba (self));
357
+ }
358
+
323
359
  static VALUE
324
360
  cr_surface_pattern_get_surface (VALUE self)
325
361
  {
@@ -344,6 +380,18 @@ cr_gradient_pattern_get_color_stop_rgba (VALUE self, VALUE index)
344
380
  rb_float_new (blue), rb_float_new (alpha));
345
381
  }
346
382
 
383
+ static VALUE
384
+ cr_gradient_pattern_get_color_stop_color (VALUE self, VALUE index)
385
+ {
386
+ VALUE result, offset, rgba;
387
+
388
+ result = cr_gradient_pattern_get_color_stop_rgba (self, index);
389
+ offset = rb_ary_shift (result);
390
+ rgba = result;
391
+
392
+ return rb_ary_new3 (2, offset, cr_color_parse (rgba));
393
+ }
394
+
347
395
  static VALUE
348
396
  cr_gradient_pattern_get_color_stop_count (VALUE self)
349
397
  {
@@ -378,9 +426,15 @@ cr_radial_pattern_get_radial_circles (VALUE self)
378
426
  &x0, &y0, &r0,
379
427
  &x1, &y1, &r1);
380
428
  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));
429
+ return rb_ary_new3 (2,
430
+ rb_ary_new3 (3,
431
+ rb_float_new (x0),
432
+ rb_float_new (y0),
433
+ rb_float_new (r0)),
434
+ rb_ary_new3 (3,
435
+ rb_float_new (x1),
436
+ rb_float_new (y1),
437
+ rb_float_new (r1)));
384
438
  }
385
439
  #endif
386
440
 
@@ -390,13 +444,17 @@ cr_radial_pattern_get_radial_circles (VALUE self)
390
444
  void
391
445
  Init_cairo_pattern (void)
392
446
  {
447
+ id_parse = rb_intern ("parse");
448
+ id_to_rgb = rb_intern ("to_rgb");
449
+ id_to_a = rb_intern ("to_a");
450
+ id_inspect = rb_intern ("inspect");
451
+
393
452
  rb_cCairo_Pattern =
394
453
  rb_define_class_under (rb_mCairo, "Pattern", rb_cObject);
395
454
 
396
455
  rb_define_alloc_func (rb_cCairo_Pattern, cr_pattern_allocate);
397
456
 
398
457
  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
458
 
401
459
  rb_define_method (rb_cCairo_Pattern, "set_matrix", cr_pattern_set_matrix, 1);
402
460
  rb_define_method (rb_cCairo_Pattern, "matrix", cr_pattern_get_matrix, 0);
@@ -416,6 +474,8 @@ Init_cairo_pattern (void)
416
474
  #if CAIRO_CHECK_VERSION(1, 3, 0)
417
475
  rb_define_method (rb_cCairo_SolidPattern, "rgba",
418
476
  cr_solid_pattern_get_rgba, 0);
477
+ rb_define_method (rb_cCairo_SolidPattern, "color",
478
+ cr_solid_pattern_get_color, 0);
419
479
  #endif
420
480
 
421
481
  RB_CAIRO_DEF_SETTERS (rb_cCairo_SolidPattern);
@@ -435,17 +495,17 @@ Init_cairo_pattern (void)
435
495
  rb_cCairo_GradientPattern =
436
496
  rb_define_class_under (rb_mCairo, "GradientPattern", rb_cCairo_Pattern);
437
497
 
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);
498
+ rb_define_method (rb_cCairo_GradientPattern, "add_color_stop",
499
+ cr_gradient_pattern_add_color_stop_generic, -1);
442
500
  rb_define_alias (rb_cCairo_GradientPattern,
443
- "add_color_stop", "add_color_stop_rgba");
501
+ "add_color_stop_rgb", "add_color_stop");
502
+ rb_define_alias (rb_cCairo_GradientPattern,
503
+ "add_color_stop_rgba", "add_color_stop");
444
504
  #if CAIRO_CHECK_VERSION(1, 3, 0)
445
505
  rb_define_method (rb_cCairo_GradientPattern, "get_color_stop_rgba",
446
506
  cr_gradient_pattern_get_color_stop_rgba, 1);
447
- rb_define_alias (rb_cCairo_GradientPattern,
448
- "get_color_stop", "get_color_stop_rgba");
507
+ rb_define_method (rb_cCairo_GradientPattern, "get_color_stop_color",
508
+ cr_gradient_pattern_get_color_stop_color, 1);
449
509
  rb_define_method (rb_cCairo_GradientPattern, "color_stop_count",
450
510
  cr_gradient_pattern_get_color_stop_count, 0);
451
511
  #endif
@@ -3,7 +3,7 @@
3
3
  * Ruby Cairo Binding
4
4
  *
5
5
  * $Author: kou $
6
- * $Date: 2007/03/06 12:17:34 $
6
+ * $Date: 2007/05/18 14:07:48 $
7
7
  *
8
8
  * Copyright 2005 Kouhei Sutou <kou@cozmixng.org>
9
9
  *
@@ -12,6 +12,9 @@
12
12
  */
13
13
 
14
14
  #include "rb_cairo.h"
15
+ #include "rb_cairo_private.h"
16
+
17
+ static ID id_normalize_const_name;
15
18
 
16
19
  VALUE
17
20
  rb_cairo__float_array (double *values, unsigned count)
@@ -28,24 +31,48 @@ rb_cairo__float_array (double *values, unsigned count)
28
31
  }
29
32
 
30
33
  void
31
- rb_cairo__glyphs_to_array (VALUE rb_array, cairo_glyph_t **glyphs, int *length)
34
+ rb_cairo__glyphs_to_array (VALUE rb_array, cairo_glyph_t *glyphs, int length)
32
35
  {
33
36
  int i;
34
-
35
- if (!rb_obj_is_kind_of (rb_array, rb_cArray))
36
- rb_raise (rb_eTypeError, "expected array");
37
-
38
- *length = RARRAY(rb_array)->len;
39
- *glyphs = ALLOCA_N (cairo_glyph_t, *length);
40
-
41
- if (!*glyphs)
42
- rb_cairo_check_status (CAIRO_STATUS_NO_MEMORY);
43
37
 
44
- for (i = 0; i < *length; i++)
38
+ for (i = 0; i < length; i++)
45
39
  {
46
- memcpy ((char *) &(*glyphs)[i],
40
+ memcpy ((char *) &glyphs[i],
47
41
  (char *) RVAL2CRGLYPH (rb_ary_entry (rb_array, i)),
48
42
  sizeof (cairo_glyph_t));
49
43
  }
50
44
  }
51
45
 
46
+ VALUE
47
+ rb_cairo__const_get (VALUE name, const char *prefix)
48
+ {
49
+ VALUE rb_normalized_name;
50
+ char *const_name, *normalized_name;
51
+ size_t prefix_len, normalized_name_len;
52
+
53
+ rb_normalized_name = rb_funcall (rb_mCairo, id_normalize_const_name, 1, name);
54
+ normalized_name = RVAL2CSTR (rb_normalized_name);
55
+
56
+ prefix_len = strlen (prefix);
57
+ normalized_name_len = strlen (normalized_name);
58
+
59
+ const_name = ALLOCA_N (char, prefix_len + normalized_name_len + 1);
60
+ strncpy (const_name, prefix, prefix_len);
61
+ strncpy (const_name + prefix_len, normalized_name, normalized_name_len);
62
+ const_name[prefix_len + normalized_name_len] = '\0';
63
+
64
+ return rb_const_get (rb_mCairo, rb_intern (const_name));
65
+ }
66
+
67
+ cairo_bool_t
68
+ rb_cairo__is_kind_of (VALUE object, VALUE klass)
69
+ {
70
+ return RVAL2CBOOL (rb_obj_is_kind_of (object, klass));
71
+ }
72
+
73
+
74
+ void
75
+ Init_cairo_private (void)
76
+ {
77
+ id_normalize_const_name = rb_intern ("normalize_const_name");
78
+ }
@@ -3,7 +3,7 @@
3
3
  * Ruby Cairo Binding
4
4
  *
5
5
  * $Author: kou $
6
- * $Date: 2007/03/06 12:17:34 $
6
+ * $Date: 2007/05/18 14:07:48 $
7
7
  *
8
8
  * Copyright 2005 Kouhei Sutou <kou@cozmixng.org>
9
9
  *
@@ -17,7 +17,45 @@
17
17
  #define CR_TRUE 1
18
18
  #define CR_FALSE 0
19
19
 
20
+ #define CSTR2RVAL(str) rb_str_new2(str)
21
+ #define RVAL2CSTR(str) StringValueCStr(str)
22
+
23
+ #define CBOOL2RVAL(bool) (bool ? Qtrue : Qfalse)
24
+ #define RVAL2CBOOL(bool) RTEST(bool)
25
+
26
+ extern void Init_cairo_private (void);
27
+ extern void Init_cairo_constants (void);
28
+
29
+ extern void Init_cairo_context (void);
30
+ extern void Init_cairo_path (void);
31
+ extern void Init_cairo_matrix (void);
32
+ extern void Init_cairo_surface (void);
33
+ extern void Init_cairo_exception (void);
34
+ extern void Init_cairo_font (void);
35
+ extern void Init_cairo_font_extents (void);
36
+ extern void Init_cairo_font_options (void);
37
+ extern void Init_cairo_scaled_font (void);
38
+ extern void Init_cairo_text_extents (void);
39
+ extern void Init_cairo_pattern (void);
40
+ extern void Init_cairo_glyph (void);
41
+
42
+ #define RB_CAIRO__GLYPHS_TO_ARRAY(rb_array, glyphs, length) \
43
+ do \
44
+ { \
45
+ Check_Type (rb_array, T_ARRAY); \
46
+ length = RARRAY (rb_array)->len; \
47
+ glyphs = ALLOCA_N (cairo_glyph_t, length); \
48
+ \
49
+ if (!glyphs) \
50
+ rb_cairo_check_status (CAIRO_STATUS_NO_MEMORY); \
51
+ \
52
+ rb_cairo__glyphs_to_array (rb_array, glyphs, length); \
53
+ } while (0)
54
+
20
55
  VALUE rb_cairo__float_array (double *values, unsigned count);
21
- void rb_cairo__glyphs_to_array (VALUE rb_array, cairo_glyph_t **glyphs, int *length);
56
+ void rb_cairo__glyphs_to_array (VALUE rb_array, cairo_glyph_t *glyphs, int length);
57
+
58
+ VALUE rb_cairo__const_get (VALUE name, const char *prefix);
59
+ cairo_bool_t rb_cairo__is_kind_of (VALUE object, VALUE klass);
22
60
 
23
61
  #endif