File: rb_cairo_path.c

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (444 lines) | stat: -rw-r--r-- 11,201 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
 * Ruby Cairo Binding
 *
 * $Author: kou $
 * $Date: 2008-04-04 03:52:31 $
 *
 * Copyright 2005-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"

#define _SELF(self) (RVAL2CRPATH (self))

VALUE rb_cCairo_Point;
VALUE rb_cCairo_Path;
VALUE rb_cCairo_PathData;
VALUE rb_cCairo_PathMoveTo;
VALUE rb_cCairo_PathLineTo;
VALUE rb_cCairo_PathCurveTo;
VALUE rb_cCairo_PathClosePath;

static ID id_new, id_current_path;
static ID id_at_x, id_at_y, id_at_type, id_at_points, id_at_context;

static VALUE
cr_point_new (VALUE x, VALUE y)
{
  return rb_funcall (rb_cCairo_Point, id_new, 2, x, y);
}

static VALUE
cr_point_initialize (VALUE self, VALUE x, VALUE y)
{
  rb_ivar_set (self, id_at_x, x);
  rb_ivar_set (self, id_at_y, y);
  return Qnil;
}

static VALUE
cr_point_to_a (VALUE self)
{
  return rb_ary_new3 (2,
                      rb_ivar_get (self, id_at_x),
                      rb_ivar_get (self, id_at_y));
}

static VALUE
cr_path_data_initialize (VALUE self, VALUE type, VALUE points)
{
  rb_ivar_set (self, id_at_type, type);
  rb_ivar_set (self, id_at_points, points);
  return Qnil;
}

static VALUE
cr_path_data_move_to_p (VALUE self)
{
  return CBOOL2RVAL (RVAL2CRPATHDATATYPE (rb_ivar_get (self, id_at_type)) ==
                     CAIRO_PATH_MOVE_TO);
}

static VALUE
cr_path_data_line_to_p (VALUE self)
{
  return CBOOL2RVAL (RVAL2CRPATHDATATYPE (rb_ivar_get (self, id_at_type)) ==
                     CAIRO_PATH_LINE_TO);
}

static VALUE
cr_path_data_curve_to_p (VALUE self)
{
  return CBOOL2RVAL (RVAL2CRPATHDATATYPE (rb_ivar_get (self, id_at_type)) ==
                     CAIRO_PATH_CURVE_TO);
}

static VALUE
cr_path_data_close_path_p (VALUE self)
{
  return CBOOL2RVAL (RVAL2CRPATHDATATYPE (rb_ivar_get (self, id_at_type)) ==
                     CAIRO_PATH_CLOSE_PATH);
}

static VALUE
cr_path_data_each (VALUE self)
{
  return rb_ary_each (rb_ivar_get (self, id_at_points));
}


static VALUE
cr_path_data_to_a (VALUE self)
{
  return rb_ary_new3 (2,
                      rb_ivar_get (self, id_at_type),
                      rb_ivar_get (self, id_at_points));
}

static VALUE
cr_path_data_to_ruby_object (cairo_path_data_t *data)
{
  VALUE rb_data = Qnil;

  switch (data->header.type)
    {
    case CAIRO_PATH_MOVE_TO:
      rb_data = rb_funcall (rb_cCairo_PathMoveTo, id_new, 2,
                            rb_float_new (data[1].point.x),
                            rb_float_new (data[1].point.y));
      break;
    case CAIRO_PATH_LINE_TO:
      rb_data = rb_funcall (rb_cCairo_PathLineTo, id_new, 2,
                            rb_float_new (data[1].point.x),
                            rb_float_new (data[1].point.y));
      break;
    case CAIRO_PATH_CURVE_TO:
      rb_data = rb_funcall (rb_cCairo_PathCurveTo, id_new, 6,
                            rb_float_new (data[1].point.x),
                            rb_float_new (data[1].point.y),
                            rb_float_new (data[2].point.x),
                            rb_float_new (data[2].point.y),
                            rb_float_new (data[3].point.x),
                            rb_float_new (data[3].point.y));
      break;
    case CAIRO_PATH_CLOSE_PATH:
      rb_data = rb_funcall (rb_cCairo_PathClosePath, id_new, 0);
      break;
    }

  return rb_data;
}


static VALUE
cr_path_move_to_initialize (int argc, VALUE *argv, VALUE self)
{
  VALUE point, x, y;
  VALUE super_argv[2];

  rb_scan_args (argc, argv, "11", &x, &y);

  if (argc == 1)
    point = x;
  else
    point = cr_point_new (x, y);

  super_argv[0] = INT2NUM (CAIRO_PATH_MOVE_TO);
  super_argv[1] = rb_ary_new3 (1, point);
  rb_call_super (2, super_argv);
  return Qnil;
}

static VALUE
cr_path_line_to_initialize (int argc, VALUE *argv, VALUE self)
{
  VALUE point, x, y;
  VALUE super_argv[2];

  rb_scan_args (argc, argv, "11", &x, &y);

  if (argc == 1)
    point = x;
  else
    point = cr_point_new (x, y);

  super_argv[0] = INT2NUM (CAIRO_PATH_LINE_TO);
  super_argv[1] = rb_ary_new3 (1, point);
  rb_call_super (2, super_argv);
  return Qnil;
}

static VALUE
cr_path_curve_to_initialize (int argc, VALUE *argv, VALUE self)
{
  VALUE point1, point2, point3, x1, y1, x2, y2, x3, y3;
  VALUE super_argv[2];

  rb_scan_args (argc, argv, "33", &x1, &y1, &x2, &y2, &x3, &y3);

  if (argc == 3)
    {
      point1 = x1;
      point2 = y1;
      point3 = x2;
    }
  else if (argc == 6)
    {
      point1 = cr_point_new (x1, y1);
      point2 = cr_point_new (x2, y2);
      point3 = cr_point_new (x3, y3);
    }
  else
    {
      VALUE inspected_arg = rb_inspect (rb_ary_new4 (argc, argv));
      rb_raise (rb_eArgError,
                "invalid argument: %s (expect "
                "(point1, point2, point3) or "
                "(x1, y1, x2, y2, x3, y3))",
                StringValuePtr (inspected_arg));
    }

  super_argv[0] = INT2NUM (CAIRO_PATH_CURVE_TO);
  super_argv[1] = rb_ary_new3 (3, point1, point2, point3);
  rb_call_super (2, super_argv);
  return Qnil;
}

static VALUE
cr_path_close_path_initialize (VALUE self)
{
  VALUE super_argv[2];
  super_argv[0] = INT2NUM (CAIRO_PATH_CLOSE_PATH);
  super_argv[1] = rb_ary_new ();
  rb_call_super (2, super_argv);
  return Qnil;
}


static void
cr_path_free (void *ptr)
{
  cairo_path_t *path = ptr;
  ruby_xfree (path->data);
  ruby_xfree (path);
}

static const rb_data_type_t cr_path_type = {
  "Cairo::Path",
  {
    NULL,
    cr_path_free,
  },
  NULL,
  NULL,
  RUBY_TYPED_FREE_IMMEDIATELY,
};

cairo_path_t *
rb_cairo_path_from_ruby_object (VALUE obj)
{
  cairo_path_t *path;

  if (!rb_cairo__is_kind_of (obj, rb_cCairo_Path))
    {
      rb_raise (rb_eTypeError, "not a cairo path");
    }
  TypedData_Get_Struct (obj, cairo_path_t, &cr_path_type, path);

  return path;
}

VALUE
rb_cairo_path_to_ruby_object (cairo_path_t *path)
{
  if (path)
    {
      VALUE rb_path;
      cairo_path_t *copied_path;
      copied_path = RB_ALLOC (cairo_path_t);
      copied_path->data = RB_ALLOC_N (cairo_path_data_t, path->num_data);
      memcpy(copied_path->data,
             path->data,
             sizeof (cairo_path_data_t) * path->num_data);
      rb_path = TypedData_Wrap_Struct (rb_cCairo_Path,
                                       &cr_path_type,
                                       copied_path);
      return rb_path;
    }
  else
    {
      return Qnil;
    }
}

static VALUE
cr_path_allocate (VALUE klass)
{
  return TypedData_Wrap_Struct (klass, &cr_path_type, NULL);
}

static VALUE
cr_path_initialize (VALUE self)
{
  cairo_path_t *path;

  path = RB_ALLOC (cairo_path_t);
  path->status = CAIRO_STATUS_SUCCESS;
  path->data = NULL;
  path->num_data = 0;
  RTYPEDDATA_DATA (self) = path;

  return Qnil;
}

static VALUE
cr_path_empty_p (VALUE self)
{
  cairo_path_t *path = _SELF (self);

  return CBOOL2RVAL (path->num_data == 0);
}

static int
cairo_path_get_size (cairo_path_t *path)
{
  int i, size;

  for (i = 0, size = 0; i < path->num_data; i += path->data[i].header.length)
    size++;

  return size;
}

static VALUE
cr_path_size (VALUE self)
{
  cairo_path_t *path = _SELF (self);
  return INT2NUM (cairo_path_get_size (path));
}

static VALUE
cr_path_ref (VALUE self, VALUE index)
{
  cairo_path_t *path = _SELF (self);
  int i, requested_index, real_index;

  requested_index = NUM2INT (index);
  if (requested_index < 0)
    {
      requested_index += cairo_path_get_size (path);
      if (requested_index < 0)
        return Qnil;
    }


  for (i = 0, real_index = 0; i < requested_index; i++)
    {
      if (real_index >= path->num_data)
        return Qnil;
      real_index += path->data[real_index].header.length;
    }

  if (real_index < path->num_data)
    return cr_path_data_to_ruby_object (&path->data[real_index]);
  else
    return Qnil;
}

static VALUE
cr_path_each (VALUE self)
{
  cairo_path_t *path = _SELF(self);
  int i;

  for (i = 0; i < path->num_data; i += path->data[i].header.length)
    {
      rb_yield (cr_path_data_to_ruby_object (&(path->data[i])));
    }

  return self;
}

void
Init_cairo_path (void)
{
  id_new = rb_intern ("new");
  id_current_path = rb_intern ("current_path");

  id_at_x = rb_intern ("@x");
  id_at_y = rb_intern ("@y");
  id_at_type = rb_intern ("@type");
  id_at_points = rb_intern ("@points");
  id_at_context = rb_intern ("@context");

  rb_cCairo_Point = rb_define_class_under (rb_mCairo, "Point", rb_cObject);
  rb_define_attr (rb_cCairo_Point, "x", CR_TRUE, CR_FALSE);
  rb_define_attr (rb_cCairo_Point, "y", CR_TRUE, CR_FALSE);
  rb_define_method (rb_cCairo_Point, "initialize", cr_point_initialize, 2);

  rb_define_method (rb_cCairo_Point, "to_a", cr_point_to_a, 0);
  rb_define_alias (rb_cCairo_Point, "to_ary", "to_a");


  rb_cCairo_PathData = rb_define_class_under (rb_mCairo, "PathData", rb_cObject);

  rb_include_module (rb_cCairo_PathData, rb_mEnumerable);

  rb_define_attr (rb_cCairo_PathData, "type", CR_TRUE, CR_FALSE);
  rb_define_attr (rb_cCairo_PathData, "points", CR_TRUE, CR_FALSE);
  rb_define_method (rb_cCairo_PathData, "initialize",
                    cr_path_data_initialize, 2);

  rb_define_method (rb_cCairo_PathData, "move_to?", cr_path_data_move_to_p, 0);
  rb_define_method (rb_cCairo_PathData, "line_to?", cr_path_data_line_to_p, 0);
  rb_define_method (rb_cCairo_PathData, "curve_to?",
                    cr_path_data_curve_to_p, 0);
  rb_define_method (rb_cCairo_PathData, "close_path?",
                    cr_path_data_close_path_p, 0);

  rb_define_method (rb_cCairo_PathData, "each", cr_path_data_each, 0);

  rb_define_method (rb_cCairo_PathData, "to_a", cr_path_data_to_a, 0);
  rb_define_alias (rb_cCairo_PathData, "to_ary", "to_a");


  rb_cCairo_PathMoveTo =
    rb_define_class_under (rb_mCairo, "PathMoveTo", rb_cCairo_PathData);
  rb_define_method (rb_cCairo_PathMoveTo, "initialize",
                    cr_path_move_to_initialize, -1);

  rb_cCairo_PathLineTo =
    rb_define_class_under (rb_mCairo, "PathLineTo", rb_cCairo_PathData);
  rb_define_method (rb_cCairo_PathLineTo, "initialize",
                    cr_path_line_to_initialize, -1);

  rb_cCairo_PathCurveTo =
    rb_define_class_under (rb_mCairo, "PathCurveTo", rb_cCairo_PathData);
  rb_define_method (rb_cCairo_PathCurveTo, "initialize",
                    cr_path_curve_to_initialize, -1);

  rb_cCairo_PathClosePath =
    rb_define_class_under (rb_mCairo, "PathClosePath", rb_cCairo_PathData);
  rb_define_method (rb_cCairo_PathClosePath, "initialize",
                    cr_path_close_path_initialize, 0);


  rb_cCairo_Path = rb_define_class_under (rb_mCairo, "Path", rb_cObject);
  rb_define_alloc_func (rb_cCairo_Path, cr_path_allocate);

  rb_include_module (rb_cCairo_Path, rb_mEnumerable);

  rb_define_method (rb_cCairo_Path, "initialize", cr_path_initialize, 0);

  rb_define_method (rb_cCairo_Path, "empty?", cr_path_empty_p, 0);
  rb_define_method (rb_cCairo_Path, "size", cr_path_size, 0);
  rb_define_alias (rb_cCairo_Path, "length", "size");
  rb_define_method (rb_cCairo_Path, "[]", cr_path_ref, 1);

  rb_define_method (rb_cCairo_Path, "each", cr_path_each, 0);
}