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,127 @@
1
+ module Cairo
2
+ class Context
3
+ module Path
4
+ module Point
5
+ module_function
6
+ def distance(a, b)
7
+ ax, ay = a
8
+ bx, by = b
9
+ Math.sqrt((bx - ax) ** 2 + (by - ay) ** 2)
10
+ end
11
+ end
12
+
13
+ def transform_path(path, &block)
14
+ save do
15
+ new_path
16
+ path.each do |type, points|
17
+ case type
18
+ when PATH_CURVE_TO
19
+ curve_to(*points.collect(&block).flatten)
20
+ when PATH_MOVE_TO
21
+ move_to(*points.collect(&block).flatten)
22
+ when PATH_LINE_TO
23
+ line_to(*points.collect(&block).flatten)
24
+ when PATH_CLOSE_PATH
25
+ close_path
26
+ end
27
+ end
28
+ copy_path
29
+ end
30
+ end
31
+
32
+ def map_path_onto(path)
33
+ parameterized_path = parameterize_path(path)
34
+ transformed_path = transform_path(copy_path) do |x, y|
35
+ current_point = nil
36
+ d = x
37
+ i = -1
38
+ type = points = nil
39
+ path.each do |type, points|
40
+ i += 1
41
+ break if d <= parameterized_path[i]
42
+ d -= parameterized_path[i]
43
+ case type
44
+ when PATH_MOVE_TO
45
+ current_point = points[0]
46
+ when PATH_LINE_TO
47
+ current_point = points[0]
48
+ when PATH_CURVE_TO
49
+ current_point = points[2]
50
+ when PATH_CLOSE_PATH
51
+ end
52
+ end
53
+
54
+ case type
55
+ when PATH_MOVE_TO
56
+ [x, y]
57
+ when PATH_LINE_TO
58
+ ratio = d / parameterized_path[i]
59
+ current_x, current_y = current_point
60
+ lx, ly = points[0]
61
+ new_x = current_x * (1 - ratio) + lx * ratio
62
+ new_y = current_y * (1 - ratio) + ly * ratio
63
+ dx = -(current_x - lx)
64
+ dy = -(current_y - ly)
65
+
66
+ ratio = y / parameterized_path[i]
67
+ [new_x + -dy * ratio, new_y + dx * ratio]
68
+ when PATH_CURVE_TO
69
+ ratio = d / parameterized_path[i]
70
+ current_x, current_y = current_point
71
+ cx0, cy0 = points[0]
72
+ cx1, cy1 = points[1]
73
+ cx2, cy2 = points[2]
74
+
75
+ new_x = current_x * (1 - ratio) * (1 - ratio) * (1 - ratio) +
76
+ 3 * cx0 * (1 - ratio) * (1 - ratio) * ratio +
77
+ 3 * cx1 * (1 - ratio) * ratio * ratio +
78
+ cx2 * ratio * ratio * ratio
79
+ new_y = current_y * (1 - ratio) * (1 - ratio) * (1 - ratio) +
80
+ 3 * cy0 * (1 - ratio) * (1 - ratio) * ratio +
81
+ 3 * cy1 * (1 - ratio) * ratio * ratio +
82
+ cy2 * ratio * ratio * ratio
83
+
84
+ dx = -3 * current_x * (1 - ratio) * (1 - ratio) +
85
+ 3 * cx0 * (1 - 4 * ratio + 3 * ratio * ratio) +
86
+ 3 * cx1 * ( 2 * ratio - 3 * ratio * ratio) +
87
+ 3 * cx2 * ratio * ratio
88
+ dy = -3 * current_y * (1 - ratio) * (1 - ratio) +
89
+ 3 * cy0 * (1 - 4 * ratio + 3 * ratio * ratio) +
90
+ 3 * cy1 * ( 2 * ratio - 3 * ratio * ratio) +
91
+ 3 * cy2 * ratio * ratio
92
+
93
+ ratio = y / Math.sqrt(dx ** 2, dy ** 2)
94
+
95
+ [new_x + -dy * ratio, new_y + dx * ratio]
96
+ when PATH_CLOSE_PATH
97
+ [x, y]
98
+ end
99
+ end
100
+ new_path
101
+ append_path(transformed_path)
102
+ end
103
+
104
+ private
105
+ def parameterize_path(path)
106
+ current_point = nil
107
+ path.collect do |type, points|
108
+ result = 0
109
+ case type
110
+ when PATH_MOVE_TO
111
+ current_point = points[0]
112
+ when PATH_LINE_TO
113
+ result = Point.distance(current_point, points[0])
114
+ current_point = points[0]
115
+ when PATH_CURVE_TO
116
+ result = Point.distance(current_point, points[0])
117
+ result += Point.distance(points[0], points[1])
118
+ result += Point.distance(points[1], points[2])
119
+ current_point = points[2]
120
+ when PATH_CLOSE_PATH
121
+ end
122
+ result
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,19 @@
1
+ module Cairo
2
+ class Context
3
+ module Quad
4
+ def quad_to(x1, y1, x2, y2)
5
+ x0, y0 = current_point
6
+ cx1 = x0 + 2 * (x1 - x0) / 3.0
7
+ cy1 = y0 + 2 * (y1 - y0) / 3.0
8
+ cx2 = cx1 + (x2 - x0) / 3.0
9
+ cy2 = cy1 + (y2 - y0) / 3.0
10
+ curve_to(cx1, cy1, cx2, cy2, x2, y2)
11
+ end
12
+
13
+ def rel_quad_to(x1, y1, x2, y2)
14
+ x0, y0 = current_point
15
+ quad_to(x1 + x0, y1 + y0, x2 + x0, y2 + x0)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ module Cairo
2
+ class Context
3
+ module Rectangle
4
+ def rounded_rectangle(x, y, width, height, x_radius, y_radius=nil)
5
+ x1 = x
6
+ x2 = x1 + width
7
+ y1 = y
8
+ y2 = y1 + height
9
+
10
+ y_radius ||= x_radius
11
+
12
+ x_radius = [x_radius, width / 2.0].min
13
+ y_radius = [y_radius, height / 2.0].min
14
+
15
+ xr1 = x_radius
16
+ xr2 = x_radius / 2.0
17
+ yr1 = y_radius
18
+ yr2 = y_radius / 2.0
19
+
20
+ move_to(x1 + xr1, y1)
21
+ line_to(x2 - xr1, y1)
22
+ curve_to(x2 - xr2, y1, x2, y1 + yr2, x2, y1 + yr1)
23
+ line_to(x2, y2 - yr1)
24
+ curve_to(x2, y2 - yr2, x2 - xr2, y2, x2 - xr1, y2)
25
+ line_to(x1 + xr1, y2)
26
+ curve_to(x1 + xr2, y2, x1, y2 - yr2, x1, y2 - yr1)
27
+ line_to(x1, y1 + yr1)
28
+ curve_to(x1, y1 + yr2, x1 + xr2, y1, x1 + xr1, y1)
29
+ close_path
30
+ end
31
+ end
32
+ end
33
+ end
data/src/rb_cairo.c ADDED
@@ -0,0 +1,90 @@
1
+ /* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Ruby Cairo Binding
4
+ *
5
+ * $Author: kou $
6
+ * $Date: 2007/03/10 11:54:17 $
7
+ *
8
+ * Copyright 2006-2007 Kouhei Sutou <kou@cozmixng.org>
9
+ * Copyright 2005 Øyvind Kolås <pippin@freedesktop.org>
10
+ * Copyright 2004-2005 MenTaLguY <mental@rydia.com>
11
+ *
12
+ * This file is made available under the same terms as Ruby
13
+ *
14
+ */
15
+
16
+
17
+ #include "rb_cairo.h"
18
+
19
+ VALUE rb_mCairo;
20
+
21
+ extern void Init_cairo_context (void);
22
+ extern void Init_cairo_path (void);
23
+ extern void Init_cairo_matrix (void);
24
+ extern void Init_cairo_surface (void);
25
+ extern void Init_cairo_constants (void);
26
+ extern void Init_cairo_exception (void);
27
+ extern void Init_cairo_font (void);
28
+ extern void Init_cairo_font_extents (void);
29
+ extern void Init_cairo_font_options (void);
30
+ extern void Init_cairo_scaled_font (void);
31
+ extern void Init_cairo_text_extents (void);
32
+ extern void Init_cairo_pattern (void);
33
+ extern void Init_cairo_glyph (void);
34
+
35
+ static ID id__add_one_arg_setter;
36
+
37
+ void
38
+ rb_cairo_def_setters (VALUE klass)
39
+ {
40
+ rb_funcall (rb_mCairo, id__add_one_arg_setter, 1, klass);
41
+ }
42
+
43
+ void
44
+ Init_cairo ()
45
+ {
46
+ int major, minor, micro;
47
+
48
+ id__add_one_arg_setter = rb_intern("__add_one_arg_setter");
49
+
50
+ rb_mCairo = rb_define_module ("Cairo");
51
+
52
+ rb_define_const (rb_mCairo, "BUILD_VERSION",
53
+ rb_ary_new3 (3,
54
+ INT2FIX (CAIRO_VERSION_MAJOR),
55
+ INT2FIX (CAIRO_VERSION_MINOR),
56
+ INT2FIX (CAIRO_VERSION_MICRO)));
57
+
58
+ major = cairo_version () / 10000;
59
+ minor = (cairo_version () % 10000) / 100;
60
+ micro = cairo_version () % 100;
61
+
62
+ rb_define_const (rb_mCairo, "VERSION",
63
+ rb_ary_new3 (3,
64
+ INT2FIX (major),
65
+ INT2FIX (minor),
66
+ INT2FIX (micro)));
67
+
68
+ rb_define_const (rb_mCairo, "MAJOR_VERSION", INT2FIX (major));
69
+ rb_define_const (rb_mCairo, "MINOR_VERSION", INT2FIX (minor));
70
+ rb_define_const (rb_mCairo, "MICRO_VERSION", INT2FIX (micro));
71
+
72
+ rb_define_const (rb_mCairo, "BINDINGS_VERSION",
73
+ rb_ary_new3 (4,
74
+ INT2FIX (1), INT2FIX (4), INT2FIX (1), Qnil));
75
+
76
+ Init_cairo_constants ();
77
+
78
+ Init_cairo_context ();
79
+ Init_cairo_path ();
80
+ Init_cairo_matrix ();
81
+ Init_cairo_surface ();
82
+ Init_cairo_exception ();
83
+ Init_cairo_font ();
84
+ Init_cairo_font_extents ();
85
+ Init_cairo_font_options ();
86
+ Init_cairo_scaled_font ();
87
+ Init_cairo_text_extents ();
88
+ Init_cairo_pattern ();
89
+ Init_cairo_glyph ();
90
+ }
data/src/rb_cairo.h ADDED
@@ -0,0 +1,180 @@
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
+
16
+ #ifndef RB_CAIRO_H
17
+ #define RB_CAIRO_H
18
+
19
+ #include <cairo.h>
20
+
21
+ #if CAIRO_HAS_PS_SURFACE
22
+ # include <cairo-ps.h>
23
+ #endif
24
+
25
+ #if CAIRO_HAS_PDF_SURFACE
26
+ # include <cairo-pdf.h>
27
+ #endif
28
+
29
+ #if CAIRO_HAS_SVG_SURFACE
30
+ # include <cairo-svg.h>
31
+ #endif
32
+
33
+ #define CAIRO_CHECK_VERSION(major, minor, micro) \
34
+ (CAIRO_VERSION_MAJOR > (major) || \
35
+ (CAIRO_VERSION_MAJOR == (major) && CAIRO_VERSION_MINOR > (minor)) || \
36
+ (CAIRO_VERSION_MAJOR == (major) && CAIRO_VERSION_MINOR == (minor) && \
37
+ CAIRO_VERSION_MICRO >= (micro)))
38
+
39
+ #include "ruby.h"
40
+
41
+ #if defined(RUBY_CAIRO_PLATFORM_WIN32) && !defined(RUBY_CAIRO_STATIC_COMPILATION)
42
+ # ifdef RUBY_CAIRO_COMPILATION
43
+ # define RUBY_CAIRO_VAR __declspec(dllexport)
44
+ # else
45
+ # define RUBY_CAIRO_VAR extern __declspec(dllimport)
46
+ # endif
47
+ #else
48
+ # define RUBY_CAIRO_VAR extern
49
+ #endif
50
+
51
+ RUBY_CAIRO_VAR VALUE rb_mCairo;
52
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Context;
53
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Path;
54
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Matrix;
55
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Pattern;
56
+ RUBY_CAIRO_VAR VALUE rb_cCairo_SolidPattern;
57
+ RUBY_CAIRO_VAR VALUE rb_cCairo_SurfacePattern;
58
+ RUBY_CAIRO_VAR VALUE rb_cCairo_GradientPattern;
59
+ RUBY_CAIRO_VAR VALUE rb_cCairo_LinearPattern;
60
+ RUBY_CAIRO_VAR VALUE rb_cCairo_RadialPattern;
61
+ RUBY_CAIRO_VAR VALUE rb_cCairo_FontFace;
62
+ RUBY_CAIRO_VAR VALUE rb_cCairo_FontExtents;
63
+ RUBY_CAIRO_VAR VALUE rb_cCairo_FontOptions;
64
+ RUBY_CAIRO_VAR VALUE rb_cCairo_ScaledFont;
65
+ RUBY_CAIRO_VAR VALUE rb_cCairo_TextExtents;
66
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Glyph;
67
+ RUBY_CAIRO_VAR VALUE rb_cCairo_Surface;
68
+
69
+ #define RVAL2CRCONTEXT(obj) (rb_cairo_context_from_ruby_object(obj))
70
+ #define CRCONTEXT2RVAL(cr) (rb_cairo_context_to_ruby_object(cr))
71
+
72
+ #define RVAL2CRPATH(obj) (rb_cairo_path_from_ruby_object(obj))
73
+ #define CRPATH2RVAL(path) (rb_cairo_path_to_ruby_object(path))
74
+
75
+ #define RVAL2CRMATRIX(obj) (rb_cairo_matrix_from_ruby_object(obj))
76
+ #define CRMATRIX2RVAL(matrix) (rb_cairo_matrix_to_ruby_object(matrix))
77
+
78
+ #define RVAL2CRPATTERN(obj) (rb_cairo_pattern_from_ruby_object(obj))
79
+ #define CRPATTERN2RVAL(pattern, klass) (rb_cairo_pattern_to_ruby_object(pattern, klass))
80
+
81
+ #define RVAL2CRFONTFACE(obj) (rb_cairo_font_face_from_ruby_object(obj))
82
+ #define CRFONTFACE2RVAL(face) (rb_cairo_font_face_to_ruby_object(face))
83
+
84
+ #define RVAL2CRFONTEXTENTS(obj) (rb_cairo_font_extents_from_ruby_object(obj))
85
+ #define CRFONTEXTENTS2RVAL(ext) (rb_cairo_font_extents_to_ruby_object(ext))
86
+
87
+ #define RVAL2CRFONTOPTIONS(obj) (rb_cairo_font_options_from_ruby_object(obj))
88
+ #define CRFONTOPTIONS2RVAL(opt) (rb_cairo_font_options_to_ruby_object(opt))
89
+
90
+ #define RVAL2CRSCALEDFONT(obj) (rb_cairo_scaled_font_from_ruby_object(obj))
91
+ #define CRSCALEDFONT2RVAL(font) (rb_cairo_scaled_font_to_ruby_object(font))
92
+
93
+ #define RVAL2CRTEXTEXTENTS(obj) (rb_cairo_text_extents_from_ruby_object(obj))
94
+ #define CRTEXTEXTENTS2RVAL(ext) (rb_cairo_text_extents_to_ruby_object(ext))
95
+
96
+ #define RVAL2CRGLYPH(obj) (rb_cairo_glyph_from_ruby_object(obj))
97
+ #define CRGLYPH2RVAL(glyph) (rb_cairo_glyph_to_ruby_object(glyph))
98
+
99
+ #define RVAL2CRSURFACE(obj) (rb_cairo_surface_from_ruby_object(obj))
100
+ #define CRSURFACE2RVAL(surface) (rb_cairo_surface_to_ruby_object(surface))
101
+
102
+ cairo_t *rb_cairo_context_from_ruby_object (VALUE obj);
103
+ VALUE rb_cairo_context_to_ruby_object (cairo_t *cr);
104
+
105
+ cairo_path_t *rb_cairo_path_from_ruby_object (VALUE obj);
106
+ VALUE rb_cairo_path_to_ruby_object (cairo_path_t *path);
107
+
108
+ cairo_matrix_t *rb_cairo_matrix_from_ruby_object (VALUE obj);
109
+ VALUE rb_cairo_matrix_to_ruby_object (cairo_matrix_t *matrix);
110
+
111
+ cairo_pattern_t *rb_cairo_pattern_from_ruby_object (VALUE obj);
112
+ VALUE rb_cairo_pattern_to_ruby_object (cairo_pattern_t *pat, VALUE klass);
113
+
114
+ cairo_font_face_t *rb_cairo_font_face_from_ruby_object (VALUE obj);
115
+ VALUE rb_cairo_font_face_to_ruby_object (cairo_font_face_t *face);
116
+
117
+ cairo_font_extents_t *rb_cairo_font_extents_from_ruby_object (VALUE obj);
118
+ VALUE rb_cairo_font_extents_to_ruby_object (cairo_font_extents_t *extents);
119
+
120
+ cairo_font_options_t *rb_cairo_font_options_from_ruby_object (VALUE obj);
121
+ VALUE rb_cairo_font_options_to_ruby_object (cairo_font_options_t *options);
122
+
123
+ cairo_scaled_font_t *rb_cairo_scaled_font_from_ruby_object (VALUE obj);
124
+ VALUE rb_cairo_scaled_font_to_ruby_object (cairo_scaled_font_t *options);
125
+
126
+ cairo_text_extents_t *rb_cairo_text_extents_from_ruby_object (VALUE obj);
127
+ VALUE rb_cairo_text_extents_to_ruby_object (cairo_text_extents_t *extents);
128
+
129
+ cairo_glyph_t *rb_cairo_glyph_from_ruby_object (VALUE obj);
130
+ VALUE rb_cairo_glyph_to_ruby_object (cairo_glyph_t *glyph);
131
+
132
+ cairo_surface_t *rb_cairo_surface_from_ruby_object (VALUE obj);
133
+ VALUE rb_cairo_surface_to_ruby_object (cairo_surface_t *surface);
134
+
135
+
136
+ #define RVAL2CROPERATOR(obj) (rb_cairo_operator_from_ruby_object(obj))
137
+ #define RVAL2CRANTIALIAS(obj) (rb_cairo_antialias_from_ruby_object(obj))
138
+ #define RVAL2CRFILLRULE(obj) (rb_cairo_fill_rule_from_ruby_object(obj))
139
+ #define RVAL2CRLINECAP(obj) (rb_cairo_line_cap_from_ruby_object(obj))
140
+ #define RVAL2CRLINEJOIN(obj) (rb_cairo_line_join_from_ruby_object(obj))
141
+ #define RVAL2CRFONTSLANT(obj) (rb_cairo_font_slant_from_ruby_object(obj))
142
+ #define RVAL2CRFONTWEIGHT(obj) (rb_cairo_font_weight_from_ruby_object(obj))
143
+ #define RVAL2CRSUBPIXELORDER(obj) (rb_cairo_subpixel_order_from_ruby_object(obj))
144
+ #define RVAL2CRHINTSTYLE(obj) (rb_cairo_hint_style_from_ruby_object(obj))
145
+ #define RVAL2CRHINTMETRICS(obj) (rb_cairo_hint_metrics_from_ruby_object(obj))
146
+ #define RVAL2CRPATHDATATYPE(obj) (rb_cairo_path_data_type_from_ruby_object(obj))
147
+ #define RVAL2CRCONTENT(obj) (rb_cairo_content_from_ruby_object(obj))
148
+ #define RVAL2CRFORMAT(obj) (rb_cairo_format_from_ruby_object(obj))
149
+ #define RVAL2CREXTEND(obj) (rb_cairo_extend_from_ruby_object(obj))
150
+ #define RVAL2CRFILTER(obj) (rb_cairo_filter_from_ruby_object(obj))
151
+ #if CAIRO_HAS_SVG_SURFACE
152
+ #define RVAL2CRSVGVERSION(obj) (rb_cairo_svg_version_from_ruby_object(obj))
153
+ #endif
154
+
155
+ cairo_operator_t rb_cairo_operator_from_ruby_object (VALUE obj);
156
+ cairo_antialias_t rb_cairo_antialias_from_ruby_object (VALUE obj);
157
+ cairo_fill_rule_t rb_cairo_fill_rule_from_ruby_object (VALUE obj);
158
+ cairo_line_cap_t rb_cairo_line_cap_from_ruby_object (VALUE obj);
159
+ cairo_line_join_t rb_cairo_line_join_from_ruby_object (VALUE obj);
160
+ cairo_font_slant_t rb_cairo_font_slant_from_ruby_object (VALUE obj);
161
+ cairo_font_weight_t rb_cairo_font_weight_from_ruby_object (VALUE obj);
162
+ cairo_subpixel_order_t rb_cairo_subpixel_order_from_ruby_object (VALUE obj);
163
+ cairo_hint_style_t rb_cairo_hint_style_from_ruby_object (VALUE obj);
164
+ cairo_hint_metrics_t rb_cairo_hint_metrics_from_ruby_object (VALUE obj);
165
+ cairo_path_data_type_t rb_cairo_path_data_type_from_ruby_object (VALUE obj);
166
+ cairo_content_t rb_cairo_content_from_ruby_object (VALUE obj);
167
+ cairo_format_t rb_cairo_format_from_ruby_object (VALUE obj);
168
+ cairo_extend_t rb_cairo_extend_from_ruby_object (VALUE obj);
169
+ cairo_filter_t rb_cairo_filter_from_ruby_object (VALUE obj);
170
+ #if CAIRO_HAS_SVG_SURFACE
171
+ cairo_svg_version_t rb_cairo_svg_version_from_ruby_object (VALUE obj);
172
+ #endif
173
+
174
+ void rb_cairo_check_status (cairo_status_t status);
175
+
176
+
177
+ #define RB_CAIRO_DEF_SETTERS(klass) rb_cairo_def_setters(klass);
178
+ void rb_cairo_def_setters (VALUE klass);
179
+
180
+ #endif