File: rb_cairo_device.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 (484 lines) | stat: -rw-r--r-- 13,539 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* -*- 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"
#include "rb_cairo_io.h"

#ifdef HAVE_RUBY_ST_H
#  include <ruby/st.h>
#else
#  include <st.h>
#endif

#ifdef CAIRO_HAS_XML_SURFACE
#  include <cairo-xml.h>
#endif

#if defined(CAIRO_HAS_SCRIPT_SURFACE) || defined(CAIRO_HAS_XML_SURFACE)
#  define NEED_DEFINE_OUTPUT_INITIALIZE 1
#endif

VALUE rb_cCairo_Device = Qnil;
VALUE rb_cCairo_DRMDevice = Qnil;
VALUE rb_cCairo_GLDevice = Qnil;
VALUE rb_cCairo_ScriptDevice = Qnil;
VALUE rb_cCairo_XCBDevice = Qnil;
VALUE rb_cCairo_XlibDevice = Qnil;
VALUE rb_cCairo_XMLDevice = Qnil;
VALUE rb_cCairo_CoglDevice = Qnil;
VALUE rb_cCairo_Win32Device = Qnil;

#if CAIRO_CHECK_VERSION(1, 10, 0)

static cairo_user_data_key_t cr_closure_key;
static cairo_user_data_key_t cr_object_holder_key;
static cairo_user_data_key_t cr_finished_key;

#define _SELF  (RVAL2CRDEVICE(self))

static inline void
cr_device_check_status (cairo_device_t *device)
{
  rb_cairo_check_status (cairo_device_status (device));
}

static VALUE
cr_device_get_klass (cairo_device_t *device)
{
  VALUE klass;
  cairo_device_type_t type;

  type = cairo_device_get_type (device);
  switch (type)
    {
    case CAIRO_DEVICE_TYPE_DRM:
      klass = rb_cCairo_DRMDevice;
      break;
    case CAIRO_DEVICE_TYPE_GL:
      klass = rb_cCairo_GLDevice;
      break;
    case CAIRO_DEVICE_TYPE_SCRIPT:
      klass = rb_cCairo_ScriptDevice;
      break;
    case CAIRO_DEVICE_TYPE_XCB:
      klass = rb_cCairo_XCBDevice;
      break;
    case CAIRO_DEVICE_TYPE_XLIB:
      klass = rb_cCairo_XlibDevice;
      break;
    case CAIRO_DEVICE_TYPE_XML:
      klass = rb_cCairo_XMLDevice;
      break;
#  if CAIRO_CHECK_VERSION(1, 11, 4)
    case CAIRO_DEVICE_TYPE_COGL:
      klass = rb_cCairo_CoglDevice;
      break;
    case CAIRO_DEVICE_TYPE_WIN32:
      klass = rb_cCairo_Win32Device;
      break;
#  endif
    default:
      klass = rb_cCairo_Device;
      break;
    }

  if (NIL_P (klass))
    rb_raise (rb_eArgError, "unknown device type: %d", type);

  return klass;
}

static VALUE
cr_device_script_supported_p (VALUE klass)
{
#ifdef CAIRO_HAS_SCRIPT_SURFACE
  return Qtrue;
#else
  return Qfalse;
#endif
}

static VALUE
cr_device_xml_supported_p (VALUE klass)
{
#ifdef CAIRO_HAS_XML_SURFACE
  return Qtrue;
#else
  return Qfalse;
#endif
}

/* constructor/de-constructor */
static void
cr_device_free (void *ptr)
{
  cairo_device_t *device = ptr;
  cairo_device_destroy (device);
}

static const rb_data_type_t cr_device_type = {
  "Cairo::Device",
  {
    NULL,
    cr_device_free,
  },
  NULL,
  NULL,
  RUBY_TYPED_FREE_IMMEDIATELY,
};

cairo_device_t *
rb_cairo_device_from_ruby_object (VALUE obj)
{
  cairo_device_t *device;
  if (!rb_cairo__is_kind_of (obj, rb_cCairo_Device))
    {
      rb_raise (rb_eTypeError, "not a cairo device");
    }
  TypedData_Get_Struct (obj, cairo_device_t, &cr_device_type, device);
  if (!device)
    rb_cairo_check_status (CAIRO_STATUS_NULL_POINTER);
  return device;
}

#ifdef NEED_DEFINE_OUTPUT_INITIALIZE
static rb_cairo__object_holder_t *
cr_object_holder_new (VALUE object)
{
  return rb_cairo__object_holder_new (rb_cCairo_Device, object);
}

static void
cr_object_holder_free (void *ptr)
{
  rb_cairo__object_holder_free (rb_cCairo_Device, ptr);
}
#endif

VALUE
rb_cairo_device_to_ruby_object (cairo_device_t *device)
{
  if (device)
    {
      VALUE klass;
      klass = cr_device_get_klass (device);
      cairo_device_reference (device);
      return TypedData_Wrap_Struct (klass, &cr_device_type, device);
    }
  else
    {
      return Qnil;
    }
}

VALUE
rb_cairo_device_to_ruby_object_with_destroy (cairo_device_t *device)
{
  VALUE rb_device;

  rb_device = rb_cairo_device_to_ruby_object (device);
  if (device)
    cairo_device_destroy (device);

  return rb_device;
}

static VALUE
cr_device_allocate (VALUE klass)
{
  return TypedData_Wrap_Struct (klass, &cr_device_type, NULL);
}

static VALUE
cr_device_initialize (int argc, VALUE *argv, VALUE self)
{
  rb_raise(rb_eNotImpError,
           "%s class creation isn't supported on this cairo installation",
           rb_obj_classname(self));

  return Qnil;
}

/* Backend device manipulation */
static VALUE
cr_device_destroy (VALUE self)
{
  cairo_device_t *device;

  device = _SELF;
  cairo_device_destroy (device);
  RTYPEDDATA_DATA (self) = NULL;

  return self;
}

static VALUE
cr_device_finish (VALUE self)
{
  cairo_device_t *device;
  rb_cairo__io_callback_closure_t *closure;

  device = _SELF;
  closure = cairo_device_get_user_data (device, &cr_closure_key);

  cairo_device_finish (device);
  cairo_device_set_user_data (device, &cr_finished_key, (void *)CR_TRUE, NULL);
  cairo_device_set_user_data (device, &cr_object_holder_key, NULL, NULL);

  if (closure && !NIL_P (closure->error))
    rb_exc_raise (closure->error);
  cr_device_check_status (device);

  return self;
}

static VALUE
cr_device_flush (VALUE self)
{
  cairo_device_flush (_SELF);
  cr_device_check_status (_SELF);
  return self;
}

static VALUE
cr_device_release (VALUE self)
{
  cairo_device_release (_SELF);
  cr_device_check_status (_SELF);
  return self;
}

static VALUE
cr_device_acquire (VALUE self)
{
  cairo_device_acquire (_SELF);
  cr_device_check_status (_SELF);
  if (rb_block_given_p ())
    return rb_ensure (rb_yield, self, cr_device_release, self);
  else
    return self;
}

static int
cr_finish_all_guarded_devices_at_end_iter (VALUE key, VALUE value, VALUE data)
{
  cr_device_finish (key);
  return ST_CONTINUE;
}

static void
cr_finish_all_guarded_devices_at_end (VALUE data)
{
  rb_hash_foreach (rb_cairo__gc_guarded_objects (rb_cCairo_Device),
                   cr_finish_all_guarded_devices_at_end_iter,
                   Qnil);
}

#ifdef NEED_DEFINE_OUTPUT_INITIALIZE
static void
yield_and_finish (VALUE self)
{
  cairo_device_t *device;

  rb_yield (self);

  device = _SELF;
  if (!cairo_device_get_user_data (device, &cr_finished_key))
    cr_device_finish (self);
}
#endif

#define DEFINE_OUTPUT_INITIALIZE(type)                                  \
static VALUE                                                            \
cr_ ## type ## _device_initialize (VALUE self,                          \
                                   VALUE file_name_or_output)           \
{                                                                       \
  cairo_device_t *device;                                               \
                                                                        \
  if (rb_respond_to (file_name_or_output, rb_cairo__io_id_write))       \
    {                                                                   \
      rb_cairo__io_callback_closure_t *closure;                         \
                                                                        \
      closure = rb_cairo__io_closure_new (file_name_or_output);         \
      device =                                                          \
        cairo_ ## type ## _create_for_stream (rb_cairo__io_write_func,  \
                                                     (void *)closure);  \
      if (cairo_device_status (device))                                 \
        {                                                               \
          rb_cairo__io_closure_destroy (closure);                       \
        }                                                               \
      else                                                              \
        {                                                               \
          rb_ivar_set (self, rb_cairo__io_id_output,                    \
                       file_name_or_output);                            \
          cairo_device_set_user_data (device, &cr_closure_key,          \
                                      closure,                          \
                                      rb_cairo__io_closure_free);       \
          cairo_device_set_user_data (device, &cr_object_holder_key,    \
                                      cr_object_holder_new (self),      \
                                      cr_object_holder_free);           \
        }                                                               \
    }                                                                   \
  else                                                                  \
    {                                                                   \
      const char *file_name;                                            \
      file_name = StringValueCStr (file_name_or_output);                \
      device = cairo_ ## type ## _create (file_name);                   \
    }                                                                   \
                                                                        \
  cr_device_check_status (device);                                      \
  RTYPEDDATA_DATA (self) = device;                                             \
  if (rb_block_given_p ())                                              \
    yield_and_finish (self);                                            \
  return Qnil;                                                          \
}

#  ifdef CAIRO_HAS_SCRIPT_SURFACE
DEFINE_OUTPUT_INITIALIZE(script)

static VALUE
cr_script_device_write_comment (VALUE self, VALUE comment)
{
  cairo_device_t *device;

  device = _SELF;
  cairo_script_write_comment (device,
                              StringValuePtr (comment),
                              (int) RSTRING_LEN (comment));
  cr_device_check_status (device);
  return Qnil;
}

static VALUE
cr_script_device_set_mode (VALUE self, VALUE mode)
{
  cairo_device_t *device;

  device = _SELF;
  cairo_script_set_mode (device, RVAL2CRSCRIPTMODE (mode));
  cr_device_check_status (device);
  return Qnil;
}

static VALUE
cr_script_device_get_mode (VALUE self)
{
  return INT2NUM (cairo_script_get_mode (_SELF));
}

static VALUE
cr_script_device_reply (VALUE self, VALUE recording_surface)
{
  cairo_device_t *device;

  device = _SELF;
  cairo_script_from_recording_surface (device,
                                       RVAL2CRSURFACE (recording_surface));
  cr_device_check_status (device);
  return Qnil;
}
#  endif

#  ifdef CAIRO_HAS_XML_SURFACE
DEFINE_OUTPUT_INITIALIZE(xml)

static VALUE
cr_xml_device_reply (VALUE self, VALUE recording_surface)
{
  cairo_device_t *device;

  device = _SELF;
  cairo_xml_for_recording_surface (device,
                                   RVAL2CRSURFACE (recording_surface));
  cr_device_check_status (device);
  return Qnil;
}
#  endif

#endif

void
Init_cairo_device (void)
{
#if CAIRO_CHECK_VERSION(1, 10, 0)
  rb_cCairo_Device =
    rb_define_class_under (rb_mCairo, "Device", rb_cObject);
  rb_define_alloc_func (rb_cCairo_Device, cr_device_allocate);

  rb_cairo__initialize_gc_guard_holder_class (rb_cCairo_Device);
  rb_set_end_proc(cr_finish_all_guarded_devices_at_end, Qnil);

  rb_define_singleton_method (rb_cCairo_Device, "script_supported?",
                              cr_device_script_supported_p, 0);
  rb_define_singleton_method (rb_cCairo_Device, "xml_supported?",
                              cr_device_xml_supported_p, 0);

  rb_define_method (rb_cCairo_Device, "initialize", cr_device_initialize, -1);
  rb_define_method (rb_cCairo_Device, "destroy", cr_device_destroy, 0);
  rb_define_method (rb_cCairo_Device, "finish", cr_device_finish, 0);
  rb_define_method (rb_cCairo_Device, "flush", cr_device_flush, 0);
  rb_define_method (rb_cCairo_Device, "acquire", cr_device_acquire, 0);
  rb_define_method (rb_cCairo_Device, "release", cr_device_release, 0);

  RB_CAIRO_DEF_SETTERS (rb_cCairo_Device);

  rb_cCairo_DRMDevice =
    rb_define_class_under (rb_mCairo, "DRMDevice", rb_cCairo_Device);

  rb_cCairo_GLDevice =
    rb_define_class_under (rb_mCairo, "GLDevice", rb_cCairo_Device);

  rb_cCairo_ScriptDevice =
    rb_define_class_under (rb_mCairo, "ScriptDevice", rb_cCairo_Device);
#  ifdef CAIRO_HAS_SCRIPT_SURFACE
  rb_define_method (rb_cCairo_ScriptDevice, "initialize",
                    cr_script_device_initialize, 1);

  rb_define_method (rb_cCairo_ScriptDevice, "write_comment",
                    cr_script_device_write_comment, 1);

  rb_define_method (rb_cCairo_ScriptDevice, "set_mode",
                    cr_script_device_set_mode, 1);
  rb_define_method (rb_cCairo_ScriptDevice, "mode",
                    cr_script_device_get_mode, 0);

  rb_define_method (rb_cCairo_ScriptDevice, "reply",
                    cr_script_device_reply, 1);

  RB_CAIRO_DEF_SETTERS (rb_cCairo_ScriptDevice);
#  endif

  rb_cCairo_XCBDevice =
    rb_define_class_under (rb_mCairo, "XCBDevice", rb_cCairo_Device);

  rb_cCairo_XlibDevice =
    rb_define_class_under (rb_mCairo, "XlibDevice", rb_cCairo_Device);

  rb_cCairo_XMLDevice =
    rb_define_class_under (rb_mCairo, "XMLDevice", rb_cCairo_Device);
#  ifdef CAIRO_HAS_XML_SURFACE
  rb_define_method (rb_cCairo_XMLDevice, "initialize",
                    cr_xml_device_initialize, 1);

  rb_define_method (rb_cCairo_XMLDevice, "reply",
                    cr_xml_device_reply, 1);

  RB_CAIRO_DEF_SETTERS (rb_cCairo_XMLDevice);
#  endif

  rb_cCairo_CoglDevice =
    rb_define_class_under (rb_mCairo, "CoglDevice", rb_cCairo_Device);

  rb_cCairo_Win32Device =
    rb_define_class_under (rb_mCairo, "Win32Device", rb_cCairo_Device);

#endif
}