comparison nodejs/paints.cc @ 733:163f0d9e6382

Add binding for linear and radial paints for JS
author Thinker K.F. Li <thinker@branda.to>
date Fri, 20 Aug 2010 09:34:49 +0800
parents 0b98bdc53215
children d0ee92a96c47
comparison
equal deleted inserted replaced
732:6879aa403306 733:163f0d9e6382
64 paint_color_set(paint, r, g, b, a); 64 paint_color_set(paint, r, g, b, a);
65 65
66 rdman_paint_changed(rdman, paint); 66 rdman_paint_changed(rdman, paint);
67 } 67 }
68 68
69 /*! \brief Set stops for linear paint for Javascript code.
70 */
71 static void
72 xnjsmb_paint_linear_set_stops(paint_t *paint, Handle<Value> stops) {
73 Array *stops_o;
74 Array *stop_o;
75 int nstops;
76 grad_stop_t *grad_stops, *old_grad_stops;
77 int i;
78
79 stops_o = Array::Cast(*stops);
80 nstops = stops_o->Length();
81 grad_stops = (grad_stop_t *)malloc(sizeof(grad_stop_t) * nstops);
82 ASSERT(grad_stops != NULL);
83
84 for(i = 0; i < nstops; i++) {
85 stop_o = Array::Cast(*stops_o->Get(i));
86 ASSERT(stop_o->Length() == 5);
87 grad_stop_init(grad_stops + i,
88 stop_o->Get(0)->ToNumber()->Value(), /* off */
89 stop_o->Get(1)->ToNumber()->Value(), /* r */
90 stop_o->Get(2)->ToNumber()->Value(), /* g */
91 stop_o->Get(3)->ToNumber()->Value(), /* b */
92 stop_o->Get(4)->ToNumber()->Value()); /* a */
93 }
94
95 old_grad_stops = paint_linear_stops(paint, nstops, grad_stops);
96 if(old_grad_stops)
97 free(old_grad_stops); /* The stops, here, were allocated for
98 * previous calling of this
99 * function. */
100 }
101
102 /*! \brief Set stops for radial paint for Javascript code.
103 */
104 static void
105 xnjsmb_paint_radial_set_stops(paint_t *paint, Handle<Value> stops) {
106 Array *stops_o;
107 Array *stop_o;
108 int nstops;
109 grad_stop_t *grad_stops, *old_grad_stops;
110 int i;
111
112 stops_o = Array::Cast(*stops);
113 nstops = stops_o->Length();
114 grad_stops = (grad_stop_t *)malloc(sizeof(grad_stop_t) * nstops);
115 ASSERT(grad_stops != NULL);
116
117 for(i = 0; i < nstops; i++) {
118 stop_o = Array::Cast(*stops_o->Get(i));
119 ASSERT(stop_o->Length() == 5);
120 grad_stop_init(grad_stops + i,
121 stop_o->Get(0)->ToNumber()->Value(), /* off */
122 stop_o->Get(1)->ToNumber()->Value(), /* r */
123 stop_o->Get(2)->ToNumber()->Value(), /* g */
124 stop_o->Get(3)->ToNumber()->Value(), /* b */
125 stop_o->Get(4)->ToNumber()->Value()); /* a */
126 }
127
128 old_grad_stops = paint_linear_stops(paint, nstops, grad_stops);
129 if(old_grad_stops)
130 free(old_grad_stops); /* The stops, here, were allocated for
131 * previous calling of this
132 * function. */
133 }
134
69 #include "paints-inc.h" 135 #include "paints-inc.h"
70 136
71 /*! \defgroup xnjsmb_paints_cons Constructor of paints 137 /*! \defgroup xnjsmb_paints_cons Constructor of paints
72 * 138 *
73 * @{ 139 * @{
103 } 169 }
104 170
105 return paint; 171 return paint;
106 } 172 }
107 173
174 paint_t *
175 xnjsmb_paint_linear_new(njs_runtime_t *rt,
176 float x1, float y1, float x2, float y2,
177 const char **err) {
178 paint_t *paint;
179 redraw_man_t *rdman;
180
181 rdman = X_njs_MB_rdman(rt);
182 paint = rdman_paint_linear_new(rdman, x1, y1, x2, y2);
183 if(paint == NULL) {
184 *err = "can not allocate a paint_linear_t";
185 return NULL;
186 }
187
188 return paint;
189 }
190
191 paint_t *
192 xnjsmb_paint_radial_new(njs_runtime_t *rt,
193 float cx, float cy, float r,
194 const char **err) {
195 paint_t *paint;
196 redraw_man_t *rdman;
197
198 rdman = X_njs_MB_rdman(rt);
199 paint = rdman_paint_radial_new(rdman, cx, cy, r);
200 if(paint == NULL) {
201 *err = "can not allocate a paint_radial_t";
202 return NULL;
203 }
204
205 return paint;
206 }
207
108 /* @} */ 208 /* @} */
109 209
110 /*! \defgroup xnjsmb_paints_export Exported wrapper maker for paints 210 /*! \defgroup xnjsmb_paints_export Exported wrapper maker for paints
111 * 211 *
112 * These functions are used by MB runtime to wrap C paints to JS 212 * These functions are used by MB runtime to wrap C paints to JS
126 Handle<Value> 226 Handle<Value>
127 export_xnjsmb_auto_paint_image_new(paint_t *paint) { 227 export_xnjsmb_auto_paint_image_new(paint_t *paint) {
128 Handle<Value> ret; 228 Handle<Value> ret;
129 229
130 ret = xnjsmb_auto_paint_image_new(paint); 230 ret = xnjsmb_auto_paint_image_new(paint);
231
232 return ret;
233 }
234
235 Handle<Value>
236 export_xnjsmb_auto_paint_linear_new(paint_t *paint) {
237 Handle<Value> ret;
238
239 ret = xnjsmb_auto_paint_linear_new(paint);
240
241 return ret;
242 }
243
244 Handle<Value>
245 export_xnjsmb_auto_paint_radial_new(paint_t *paint) {
246 Handle<Value> ret;
247
248 ret = xnjsmb_auto_paint_radial_new(paint);
131 249
132 return ret; 250 return ret;
133 } 251 }
134 /* @} */ 252 /* @} */
135 253