File: seed-cairo-pattern.c

package info (click to toggle)
seed 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,900 kB
  • sloc: ansic: 24,336; sh: 11,196; makefile: 773; xml: 187; python: 173
file content (215 lines) | stat: -rw-r--r-- 7,111 bytes parent folder | download
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
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 * Seed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include <seed.h>
#include <cairo/cairo.h>

#include "seed-cairo.h"
#include "seed-cairo-matrix.h"

#define CAIRO_PATTERN_PRIV(obj) ((cairo_pattern_t *)seed_object_get_private(obj))

#define CHECK_PATTERN(obj, res) ({					\
      if (!seed_object_is_of_class (ctx, obj, seed_cairo_pattern_class)){ \
	seed_make_exception (ctx, exception, "ArgumentError", "Object is not a Cairo Pattern"); \
	return seed_make_##res (ctx);					\
      }									\
      if (!seed_object_get_private (obj)){				\
	seed_make_exception (ctx, exception, "ArgumentError", "Cairo pattern has been destroyed"); \
	return seed_make_##res (ctx);}})

#define CHECK_THIS() if (!seed_object_get_private (this_object)){	\
    seed_make_exception (ctx, exception, "ArgumentError", "Cairo pattern has been destroyed"); \
    return seed_make_undefined (ctx);}

#define CHECK_THIS_BOOL(res) if (!seed_object_get_private (this_object)){ \
    seed_make_exception (ctx, exception, "ArgumentError", "Cairo pattern has been destroyed"); return FALSE;}




SeedClass seed_cairo_pattern_class;

SeedClass
seed_get_cairo_pattern_class ()
{
  return seed_cairo_pattern_class;
}

static void
seed_cairo_pattern_finalize (SeedObject obj)
{
  cairo_pattern_t *s = CAIRO_PATTERN_PRIV(obj);
  if (s)
    {
      cairo_pattern_set_user_data (s, seed_get_cairo_key(), NULL, NULL);
      cairo_pattern_destroy (s);
    }
}

cairo_pattern_t *
seed_object_to_cairo_pattern (SeedContext ctx, SeedObject obj, SeedException *exception)
{
  if (seed_object_is_of_class (ctx, obj, seed_cairo_pattern_class))
    return CAIRO_PATTERN_PRIV (obj);
  seed_make_exception (ctx, exception, "ArgumentError", "Object is not a Cairo Pattern");
  return NULL;
}

SeedObject
seed_object_from_cairo_pattern (SeedContext ctx, cairo_pattern_t *pat)
{
  SeedObject jsobj;

  jsobj = cairo_pattern_get_user_data (pat, seed_get_cairo_key());
  if (jsobj)
    return jsobj;

  jsobj = seed_make_object (ctx, seed_cairo_pattern_class, pat);
  cairo_pattern_set_user_data (pat, seed_get_cairo_key(), jsobj, seed_cairo_destroy_func);
  return jsobj;
}

static SeedObject
seed_cairo_construct_linear_gradient (SeedContext ctx,
				      SeedObject constructor,
				      size_t argument_count,
				      const SeedValue arguments[],
				      SeedException * exception)
{
  gdouble x0,y0,x1,y1;

  if (argument_count != 4)
    {
      EXPECTED_EXCEPTION("LinearGradient constructor", "4 arguments");
    }

  x0 = seed_value_to_double (ctx, arguments[0], exception);
  y0 = seed_value_to_double (ctx, arguments[1], exception);
  x1 = seed_value_to_double (ctx, arguments[2], exception);
  y1 = seed_value_to_double (ctx, arguments[3], exception);

  return seed_object_from_cairo_pattern (ctx, cairo_pattern_create_linear (x0, y0, x1, y1));
}

static SeedObject
seed_cairo_construct_radial_gradient (SeedContext ctx,
				      SeedObject constructor,
				      size_t argument_count,
				      const SeedValue arguments[],
				      SeedException * exception)
{
  gdouble cx0, cy0, r0, cx1, cy1, r1;

  if (argument_count != 6)
    {
      EXPECTED_EXCEPTION("RadialGradient constructor", "6 arguments");
    }

  cx0 = seed_value_to_double (ctx, arguments[0], exception);
  cy0 = seed_value_to_double (ctx, arguments[1], exception);
  r0 = seed_value_to_double (ctx, arguments[2], exception);
  cx1 = seed_value_to_double (ctx, arguments[3], exception);
  cy1 = seed_value_to_double (ctx, arguments[4], exception);
  r1 = seed_value_to_double (ctx, arguments[5], exception);


  return seed_object_from_cairo_pattern (ctx, cairo_pattern_create_radial (cx0, cy0, r0, cx1, cy1, r1));
}

static SeedValue
seed_cairo_pattern_add_color_stop_rgb (SeedContext ctx,
				       SeedObject function,
				       SeedObject this_object,
				       gsize argument_count,
				       const SeedValue arguments[],
				       SeedException *exception)
{
  gdouble offset, r, g, b;
  cairo_pattern_t *pat;
  CHECK_THIS();
  if (argument_count != 4)
    {
      EXPECTED_EXCEPTION("add_color_stop_rgb", "4 arguments");
    }

  pat = seed_object_get_private (this_object);
  offset = seed_value_to_double (ctx, arguments[0], exception);
  r = seed_value_to_double (ctx, arguments[1], exception);
  g = seed_value_to_double (ctx, arguments[2], exception);
  b = seed_value_to_double (ctx, arguments[3], exception);

  cairo_pattern_add_color_stop_rgb (pat, offset, r, g, b);

  return seed_make_undefined (ctx);
}

static SeedValue
seed_cairo_pattern_add_color_stop_rgba (SeedContext ctx,
				       SeedObject function,
				       SeedObject this_object,
				       gsize argument_count,
				       const SeedValue arguments[],
				       SeedException *exception)
{
  gdouble offset, r, g, b, a;
  cairo_pattern_t *pat;
  CHECK_THIS();
  if (argument_count != 5)
    {
      EXPECTED_EXCEPTION("add_color_stop_rgba", "5 arguments");
    }

  pat = seed_object_get_private (this_object);
  offset = seed_value_to_double (ctx, arguments[0], exception);
  r = seed_value_to_double (ctx, arguments[1], exception);
  g = seed_value_to_double (ctx, arguments[2], exception);
  b = seed_value_to_double (ctx, arguments[3], exception);
  a = seed_value_to_double (ctx, arguments[4], exception);

  cairo_pattern_add_color_stop_rgba (pat, offset, r, g, b, a);

  return seed_make_undefined (ctx);
}

seed_static_function pattern_funcs[] = {
  {"add_color_stop_rgb", seed_cairo_pattern_add_color_stop_rgb, 0},
  {"add_color_stop_rgba", seed_cairo_pattern_add_color_stop_rgba, 0},
  {0,0,0}
};

void
seed_define_cairo_pattern (SeedContext ctx,
			   SeedObject namespace_ref)
{
  SeedObject linear_constructor, radial_constructor;
  seed_class_definition pattern_def = seed_empty_class;

  pattern_def.class_name = "Pattern";
  pattern_def.finalize = seed_cairo_pattern_finalize;
  pattern_def.static_functions = pattern_funcs;

  seed_cairo_pattern_class = seed_create_class (&pattern_def);

  linear_constructor = seed_make_constructor (ctx, NULL, seed_cairo_construct_linear_gradient);
  seed_object_set_property(ctx, namespace_ref, "LinearGradient", linear_constructor);

  radial_constructor = seed_make_constructor (ctx, NULL, seed_cairo_construct_radial_gradient);
  seed_object_set_property(ctx, namespace_ref, "RadialGradient", radial_constructor);
}