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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
* Ruby Cairo Binding
*
* Copyright 2010-2022 Sutou Kouhei <kou@cozmixng.org>
*
* This file is made available under the same terms as Ruby
*
*/
#include "rb_cairo.h"
#include "rb_cairo_private.h"
VALUE rb_cCairo_Region = Qnil;
#if CAIRO_CHECK_VERSION(1, 10, 0)
#define _SELF (RVAL2CRREGION(self))
static void
cr_region_free (void *ptr)
{
cairo_region_destroy ((cairo_region_t *) ptr);
}
static const rb_data_type_t cr_region_type = {
"Cairo::Region",
{
NULL,
cr_region_free,
},
NULL,
NULL,
RUBY_TYPED_FREE_IMMEDIATELY,
};
static inline void
cr_region_check_status (cairo_region_t *region)
{
rb_cairo_check_status (cairo_region_status (region));
}
cairo_region_t *
rb_cairo_region_from_ruby_object (VALUE obj)
{
cairo_region_t *region;
if (!rb_cairo__is_kind_of (obj, rb_cCairo_Region))
{
rb_raise (rb_eTypeError, "not a cairo region");
}
TypedData_Get_Struct (obj, cairo_region_t, &cr_region_type, region);
return region;
}
VALUE
rb_cairo_region_to_ruby_object (cairo_region_t *region)
{
if (region)
{
cairo_region_reference (region);
return TypedData_Wrap_Struct (rb_cCairo_Region, &cr_region_type, region);
}
else
{
return Qnil;
}
}
static VALUE
cr_region_allocate (VALUE klass)
{
return TypedData_Wrap_Struct (klass, &cr_region_type, NULL);
}
static VALUE
cr_region_initialize (int argc, VALUE *argv, VALUE self)
{
cairo_region_t *region;
if (argc == 0)
{
region = cairo_region_create ();
}
else
{
int i;
cairo_rectangle_int_t *rectangles;
rectangles = ALLOCA_N (cairo_rectangle_int_t, argc);
for (i = 0; i < argc; i++)
{
VALUE rb_rectangle;
rb_rectangle = rb_check_array_type (argv[i]);
if (RARRAY_LEN (rb_rectangle) != 4)
rb_raise (rb_eArgError,
"invalid argument (expect "
"() or ([x, y, width, height], ...): %s",
rb_cairo__inspect (rb_ary_new4 (argc, argv)));
rectangles[i].x = NUM2INT (RARRAY_PTR (rb_rectangle)[0]);
rectangles[i].y = NUM2INT (RARRAY_PTR (rb_rectangle)[1]);
rectangles[i].width = NUM2INT (RARRAY_PTR (rb_rectangle)[2]);
rectangles[i].height = NUM2INT (RARRAY_PTR (rb_rectangle)[3]);
}
region = cairo_region_create_rectangles (rectangles, argc);
}
cr_region_check_status (region);
RTYPEDDATA_DATA (self) = region;
return Qnil;
}
static VALUE
cr_region_dup (VALUE self)
{
cairo_region_t *copied_region;
VALUE rb_copied_region;
copied_region = cairo_region_copy (_SELF);
cr_region_check_status (copied_region);
rb_copied_region = CRREGION2RVAL (copied_region);
cairo_region_destroy (copied_region);
return rb_copied_region;
}
static VALUE
cr_region_equal (VALUE self, VALUE other)
{
cairo_region_t *region, *other_region;
if (!rb_cairo__is_kind_of (other, rb_cCairo_Region))
return Qfalse;
region = _SELF;
other_region = RVAL2CRREGION (other);
return CBOOL2RVAL (cairo_region_equal (region, other_region));
}
static VALUE
cr_region_get_extents (VALUE self)
{
cairo_region_t *region;
cairo_rectangle_int_t extents;
region = _SELF;
cairo_region_get_extents (region, &extents);
cr_region_check_status (region);
return rb_ary_new3 (4,
INT2NUM (extents.x), INT2NUM (extents.y),
INT2NUM (extents.width), INT2NUM (extents.height));
}
static VALUE
cr_region_num_rectangles (VALUE self)
{
cairo_region_t *region;
int num_rectangles;
region = _SELF;
num_rectangles = cairo_region_num_rectangles (region);
cr_region_check_status (region);
return INT2NUM (num_rectangles);
}
static VALUE
cr_region_get_rectangle (VALUE self, VALUE index)
{
cairo_region_t *region;
cairo_rectangle_int_t extents;
region = _SELF;
cairo_region_get_rectangle (region, NUM2INT (index), &extents);
cr_region_check_status (region);
return rb_ary_new3 (4,
INT2NUM (extents.x), INT2NUM (extents.y),
INT2NUM (extents.width), INT2NUM (extents.height));
}
static VALUE
cr_region_is_empty (VALUE self)
{
return CBOOL2RVAL (cairo_region_is_empty (_SELF));
}
static VALUE
cr_region_containts_rectangle (int argc, VALUE *argv, VALUE self)
{
cairo_region_t *region;
cairo_rectangle_int_t rectangle;
cairo_region_overlap_t overlap;
VALUE arg1, arg2, arg3, arg4;
const char *error_message =
"invalid argument (expect "
"(x, y, width, height) or ([x, y, width, height])): %s";
rb_scan_args (argc, argv, "13", &arg1, &arg2, &arg3, &arg4);
region = _SELF;
if (argc == 1)
{
VALUE rb_rectangle;
rb_rectangle = rb_check_array_type (arg1);
if (RARRAY_LEN (rb_rectangle) != 4)
rb_raise (rb_eArgError, error_message,
rb_cairo__inspect (rb_ary_new4 (argc, argv)));
rectangle.x = NUM2INT (RARRAY_PTR (rb_rectangle)[0]);
rectangle.y = NUM2INT (RARRAY_PTR (rb_rectangle)[1]);
rectangle.width = NUM2INT (RARRAY_PTR (rb_rectangle)[2]);
rectangle.height = NUM2INT (RARRAY_PTR (rb_rectangle)[3]);
}
else if (argc == 4)
{
rectangle.x = NUM2INT (arg1);
rectangle.y = NUM2INT (arg2);
rectangle.width = NUM2INT (arg3);
rectangle.height = NUM2INT (arg4);
}
else
{
rb_raise (rb_eArgError, error_message,
rb_cairo__inspect (rb_ary_new4 (argc, argv)));
}
overlap = cairo_region_contains_rectangle (region, &rectangle);
cr_region_check_status (region);
return INT2NUM (overlap);
}
static VALUE
cr_region_containts_point (int argc, VALUE *argv, VALUE self)
{
cairo_region_t *region;
int x, y;
VALUE arg1, arg2;
const char *error_message =
"invalid argument (expect "
"(x, y) or ([x, y])): %s";
rb_scan_args (argc, argv, "11", &arg1, &arg2);
region = _SELF;
if (argc == 1)
{
VALUE point;
point = rb_check_array_type (arg1);
if (RARRAY_LEN (point) != 4)
rb_raise (rb_eArgError, error_message,
rb_cairo__inspect (rb_ary_new4 (argc, argv)));
x = NUM2INT (RARRAY_PTR (point)[0]);
y = NUM2INT (RARRAY_PTR (point)[1]);
}
else
{
x = NUM2INT (arg1);
y = NUM2INT (arg2);
}
return CBOOL2RVAL (cairo_region_contains_point (region, x, y));
}
static VALUE
cr_region_translate (int argc, VALUE *argv, VALUE self)
{
cairo_region_t *region;
int x, y;
VALUE arg1, arg2;
const char *error_message =
"invalid argument (expect "
"(x, y) or ([x, y])): %s";
rb_scan_args (argc, argv, "11", &arg1, &arg2);
region = _SELF;
if (argc == 1)
{
VALUE point;
point = rb_check_array_type (arg1);
if (RARRAY_LEN (point) != 4)
rb_raise (rb_eArgError, error_message,
rb_cairo__inspect (rb_ary_new4 (argc, argv)));
x = NUM2INT (RARRAY_PTR (point)[0]);
y = NUM2INT (RARRAY_PTR (point)[1]);
}
else
{
x = NUM2INT (arg1);
y = NUM2INT (arg2);
}
cairo_region_translate (region, x, y);
cr_region_check_status (region);
return Qnil;
}
#define DEFINE_OPERATOR(type) \
static VALUE \
cr_region_ ## type (int argc, VALUE *argv, VALUE self) \
{ \
cairo_status_t status; \
cairo_region_t *region, *other = NULL; \
cairo_rectangle_int_t rectangle; \
VALUE arg1, arg2, arg3, arg4; \
const char *error_message = \
"invalid argument (expect " \
"(region), (x, y, width, height) or ([x, y, width, height])): %s"; \
\
rb_scan_args (argc, argv, "13", &arg1, &arg2, &arg3, &arg4); \
\
region = _SELF; \
if (argc == 1) \
{ \
if (rb_cairo__is_kind_of (arg1, rb_cCairo_Region)) \
{ \
other = RVAL2CRREGION (arg1); \
} \
else \
{ \
VALUE rb_rectangle; \
\
rb_rectangle = rb_check_array_type (arg1); \
if (RARRAY_LEN (rb_rectangle) != 4) \
rb_raise (rb_eArgError, error_message, \
rb_cairo__inspect (rb_ary_new4 (argc, argv))); \
rectangle.x = NUM2INT (RARRAY_PTR (rb_rectangle)[0]); \
rectangle.y = NUM2INT (RARRAY_PTR (rb_rectangle)[1]); \
rectangle.width = NUM2INT (RARRAY_PTR (rb_rectangle)[2]); \
rectangle.height = NUM2INT (RARRAY_PTR (rb_rectangle)[3]); \
} \
} \
else if (argc == 4) \
{ \
rectangle.x = NUM2INT (arg1); \
rectangle.y = NUM2INT (arg2); \
rectangle.width = NUM2INT (arg3); \
rectangle.height = NUM2INT (arg4); \
} \
else \
{ \
rb_raise (rb_eArgError, error_message, \
rb_cairo__inspect (rb_ary_new4 (argc, argv))); \
} \
\
if (other) \
status = cairo_region_ ## type (region, other); \
else \
status = cairo_region_ ## type ## _rectangle (region, &rectangle); \
rb_cairo_check_status (status); \
cr_region_check_status (region); \
return Qnil; \
}
DEFINE_OPERATOR(subtract)
DEFINE_OPERATOR(intersect)
DEFINE_OPERATOR(union)
DEFINE_OPERATOR(xor)
#endif
void
Init_cairo_region (void)
{
#if CAIRO_CHECK_VERSION(1, 10, 0)
rb_cCairo_Region =
rb_define_class_under (rb_mCairo, "Region", rb_cObject);
rb_define_alloc_func (rb_cCairo_Region, cr_region_allocate);
rb_define_method (rb_cCairo_Region, "initialize", cr_region_initialize, -1);
rb_define_method (rb_cCairo_Region, "dup", cr_region_dup, 0);
rb_define_method (rb_cCairo_Region, "==", cr_region_equal, 1);
rb_define_method (rb_cCairo_Region, "extents", cr_region_get_extents, 0);
rb_define_method (rb_cCairo_Region, "num_rectangles",
cr_region_num_rectangles, 0);
rb_define_method (rb_cCairo_Region, "[]",
cr_region_get_rectangle, 1);
rb_define_method (rb_cCairo_Region, "empty?", cr_region_is_empty, 0);
rb_define_method (rb_cCairo_Region, "contains_rectangle",
cr_region_containts_rectangle, -1);
rb_define_method (rb_cCairo_Region, "contains_point?",
cr_region_containts_point, -1);
rb_define_method (rb_cCairo_Region, "translate!", cr_region_translate, -1);
rb_define_method (rb_cCairo_Region, "subtract!", cr_region_subtract, -1);
rb_define_method (rb_cCairo_Region, "intersect!", cr_region_intersect, -1);
rb_define_method (rb_cCairo_Region, "union!", cr_region_union, -1);
rb_define_method (rb_cCairo_Region, "xor!", cr_region_xor, -1);
RB_CAIRO_DEF_SETTERS (rb_cCairo_Region);
#endif
}
|