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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* -*- 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"
SeedClass seed_matrix_class;
SeedValue
seed_value_from_cairo_matrix (SeedContext ctx,
const cairo_matrix_t *matrix,
SeedException *exception)
{
SeedValue elems[6];
elems[0] = seed_value_from_double(ctx, matrix->xx, exception);
elems[1] = seed_value_from_double(ctx, matrix->yx, exception);
elems[2] = seed_value_from_double(ctx, matrix->xy, exception);
elems[3] = seed_value_from_double(ctx, matrix->yy, exception);
elems[4] = seed_value_from_double(ctx, matrix->x0, exception);
elems[5] = seed_value_from_double(ctx, matrix->y0, exception);
return seed_make_array (ctx, elems, 6, exception);
}
gboolean
seed_value_to_cairo_matrix (SeedContext ctx,
SeedValue value,
cairo_matrix_t *matrix,
SeedException *exception)
{
if (!seed_value_is_object (ctx, value))
return FALSE;
matrix->xx = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 0, exception), exception);
matrix->yx = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 1, exception), exception);
matrix->xy = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 2, exception), exception);
matrix->yy = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 3, exception), exception);
matrix->x0 = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 4, exception), exception);
matrix->y0 = seed_value_to_double (ctx, seed_object_get_property_at_index (ctx, (SeedObject) value, 5, exception), exception);
return TRUE;
}
// Should probably be a property?
static SeedValue
seed_cairo_matrix_init_identity (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
cairo_matrix_t m;
cairo_matrix_init_identity (&m);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_init_translate (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 2)
{
EXPECTED_EXCEPTION("init_translate", "2 arguments");
}
x = seed_value_to_double (ctx, arguments[0], exception);
y = seed_value_to_double (ctx, arguments[1], exception);
cairo_matrix_init_translate (&m, x, y);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_translate (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 3)
{
EXPECTED_EXCEPTION("translate", "3 arguments");
}
if (!seed_value_to_cairo_matrix (ctx, arguments[0], &m, exception))
{
seed_make_exception (ctx, exception, "ArgumentError", "translate needs an array [xx, yx, xy, yy, x0, y0]");
}
x = seed_value_to_double (ctx, arguments[1], exception);
y = seed_value_to_double (ctx, arguments[2], exception);
cairo_matrix_translate (&m, x, y);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_init_scale (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 2)
{
EXPECTED_EXCEPTION("init_scale", "2 arguments");
}
x = seed_value_to_double (ctx, arguments[0], exception);
y = seed_value_to_double (ctx, arguments[1], exception);
cairo_matrix_init_scale (&m, x, y);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_scale (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 3)
{
EXPECTED_EXCEPTION("scale", "3 arguments");
}
if (!seed_value_to_cairo_matrix (ctx, arguments[0], &m, exception))
{
seed_make_exception (ctx, exception, "ArgumentError", "scale needs an array [xx, yx, xy, yy, x0, y0]");
}
x = seed_value_to_double (ctx, arguments[1], exception);
y = seed_value_to_double (ctx, arguments[2], exception);
cairo_matrix_scale (&m, x, y);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_init_rotate (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble angle;
cairo_matrix_t m;
if (argument_count != 1)
{
EXPECTED_EXCEPTION("init_rotate", "1 arguments");
}
angle = seed_value_to_double (ctx, arguments[0], exception);
cairo_matrix_init_rotate (&m, angle);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_rotate (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
gdouble angle;
cairo_matrix_t m;
if (argument_count != 2)
{
EXPECTED_EXCEPTION("rotate", "2 arguments");
}
if (!seed_value_to_cairo_matrix (ctx, arguments[0], &m, exception))
{
seed_make_exception (ctx, exception, "ArgumentError", "rotate needs an array [xx, yx, xy, yy, x0, y0]");
}
angle = seed_value_to_double (ctx, arguments[1], exception);
cairo_matrix_rotate (&m, angle);
return seed_value_from_cairo_matrix (ctx, &m, exception);
}
static SeedValue
seed_cairo_matrix_transform_distance (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
SeedValue ret[2];
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 3)
{
EXPECTED_EXCEPTION("transform_distance", "3 arguments");
}
if (!seed_value_to_cairo_matrix (ctx, arguments[0], &m, exception))
{
seed_make_exception (ctx, exception, "ArgumentError", "transform_distance needs an array [xx, yx, xy, yy, x0, y0]");
}
x = seed_value_to_double (ctx, arguments[1], exception);
y = seed_value_to_double (ctx, arguments[2], exception);
cairo_matrix_transform_distance (&m, &x, &y);
ret[0] = seed_value_from_double (ctx, x, exception);
ret[1] = seed_value_from_double (ctx, y, exception);
return seed_make_array (ctx, ret, 2, exception);
}
static SeedValue
seed_cairo_matrix_transform_point (SeedContext ctx,
SeedObject function,
SeedObject this_object,
gsize argument_count,
const SeedValue arguments[],
SeedException *exception)
{
SeedValue ret[2];
gdouble x, y;
cairo_matrix_t m;
if (argument_count != 3)
{
EXPECTED_EXCEPTION("transform_point", "3 arguments");
}
if (!seed_value_to_cairo_matrix (ctx, arguments[0], &m, exception))
{
seed_make_exception (ctx, exception, "ArgumentError", "transform_point needs an array [xx, yx, xy, yy, x0, y0]");
}
x = seed_value_to_double (ctx, arguments[1], exception);
y = seed_value_to_double (ctx, arguments[2], exception);
cairo_matrix_transform_point (&m, &x, &y);
ret[0] = seed_value_from_double (ctx, x, exception);
ret[1] = seed_value_from_double (ctx, y, exception);
return seed_make_array (ctx, ret, 2, exception);
}
seed_static_function matrix_funcs[] = {
{"init_identity", seed_cairo_matrix_init_identity, 0},
{"init_translate", seed_cairo_matrix_init_translate, 0},
{"init_scale", seed_cairo_matrix_init_scale, 0},
{"init_rotate", seed_cairo_matrix_init_rotate, 0},
{"translate", seed_cairo_matrix_translate, 0},
{"scale", seed_cairo_matrix_scale, 0},
{"rotate", seed_cairo_matrix_rotate, 0},
{"transform_point", seed_cairo_matrix_transform_point, 0},
{"transform_distance", seed_cairo_matrix_transform_distance, 0},
{0, 0, 0}
};
void
seed_define_cairo_matrix (SeedContext ctx,
SeedObject namespace_ref)
{
seed_class_definition matrix_def = seed_empty_class;
matrix_def.class_name = "Matrix";
matrix_def.static_functions = matrix_funcs;
seed_matrix_class = seed_create_class (&matrix_def);
seed_object_set_property (ctx, namespace_ref, "Matrix", seed_make_object (ctx, seed_matrix_class, NULL));
}
|