File: rb_cairo_private.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 (254 lines) | stat: -rw-r--r-- 5,638 bytes parent folder | download | duplicates (3)
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
/* -*- c-file-style: "gnu"; indent-tabs-mode: nil -*- */
/*
 * Ruby Cairo Binding
 *
 * $Author: kou $
 * $Date: 2008-08-17 07:21:42 $
 *
 * Copyright 2005-2019 Kouhei Sutou <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_cairo__cFFIPointer;

static ID cr_id_normalize_const_name;
static ID cr_id_objects;
static ID cr_id_dup;
static ID cr_id_inspect;
static ID cr_id_exit_application;

VALUE
rb_cairo__float_array (double *values, unsigned count)
{
  VALUE result;
  unsigned i;

  result = rb_ary_new2 (count);
  for (i = 0; i < count; i++)
    {
      rb_ary_push (result, rb_float_new (values[i]));
    }
  return result;
}

void
rb_cairo__glyphs_to_array (VALUE rb_array, cairo_glyph_t *glyphs, int length)
{
  int i;

  for (i = 0; i < length; i++)
    {
      memcpy ((char *) &glyphs[i],
              (char *) RVAL2CRGLYPH (rb_ary_entry (rb_array, i)),
              sizeof (cairo_glyph_t));
    }
}

VALUE
rb_cairo__const_get (VALUE name, const char *prefix)
{
  VALUE rb_normalized_name;
  char *const_name, *normalized_name;
  size_t prefix_len, normalized_name_len;

  rb_normalized_name = rb_funcall (rb_mCairo,
                                   cr_id_normalize_const_name, 1, name);
  normalized_name = RVAL2CSTR (rb_normalized_name);

  prefix_len = strlen (prefix);
  normalized_name_len = strlen (normalized_name);

  const_name = ALLOCA_N (char, prefix_len + normalized_name_len + 1);
  strcpy (const_name, prefix);
  strcpy (const_name + prefix_len, normalized_name);

  return rb_const_get (rb_mCairo, rb_intern (const_name));
}

cairo_bool_t
rb_cairo__is_kind_of (VALUE object, VALUE klass)
{
  return RVAL2CBOOL (rb_obj_is_kind_of (object, klass));
}

rb_cairo__object_holder_t *
rb_cairo__object_holder_new (VALUE klass, VALUE object)
{
  rb_cairo__object_holder_t *holder;

  holder = ALLOC(rb_cairo__object_holder_t);
  rb_cairo__gc_guard_add (klass, object);
  holder->object = object;
  return holder;
}

void
rb_cairo__object_holder_free (VALUE klass, void *ptr)
{
  rb_cairo__object_holder_t *holder = ptr;

  if (!NIL_P (holder->object))
    rb_cairo__gc_guard_remove (klass, holder->object);

  xfree (holder);
}

void
rb_cairo__initialize_gc_guard_holder_class (VALUE klass)
{
  rb_ivar_set (klass, cr_id_objects, rb_hash_new ());
}

void
rb_cairo__gc_guard_add (VALUE klass, VALUE object)
{
  rb_hash_aset (rb_ivar_get (klass, cr_id_objects), object, Qnil);
}

void
rb_cairo__gc_guard_remove (VALUE klass, VALUE object)
{
  rb_hash_delete (rb_ivar_get (klass, cr_id_objects), object);
}

VALUE
rb_cairo__gc_guarded_objects (VALUE klass)
{
  return rb_funcall (rb_ivar_get (klass, cr_id_objects), cr_id_dup, 0);
}

const char *
rb_cairo__inspect (VALUE object)
{
  VALUE inspected;

  inspected = rb_funcall (object, cr_id_inspect, 0);
  return RSTRING_PTR (inspected);
}

#if CAIRO_CHECK_VERSION(1, 7, 2)
VALUE
rb_cairo__glyphs_to_ruby_object (cairo_glyph_t *glyphs, int num_glyphs)
{
  int i;
  VALUE rb_glyphs;

  rb_glyphs = rb_ary_new2 (num_glyphs);
  for (i = 0; i < num_glyphs; i++)
    {
      RARRAY_PTR (rb_glyphs)[i] = CRGLYPH2RVAL (glyphs + i);
    }

  return rb_glyphs;
}

void
rb_cairo__glyphs_from_ruby_object (VALUE rb_glyphs,
                                   cairo_glyph_t **glyphs, int *num_glyphs)
{
  int i, len;

  if (NIL_P (rb_glyphs))
    {
      *num_glyphs = -1;
      return;
    }

  len = (int) RARRAY_LEN (rb_glyphs);
  if (*num_glyphs < len)
    *glyphs = cairo_glyph_allocate (len);
  *num_glyphs = len;
  for (i = 0; i < len; i++)
    {
      cairo_glyph_t *glyph;

      glyph = *glyphs + i;
      *glyph = *(RVAL2CRGLYPH (RARRAY_PTR (rb_glyphs)[i]));
    }
}

VALUE
rb_cairo__text_clusters_to_ruby_object (cairo_text_cluster_t *clusters,
                                        int num_clusters)
{
  int i;
  VALUE rb_clusters;

  rb_clusters = rb_ary_new2 (num_clusters);
  for (i = 0; i < num_clusters; i++)
    {
      RARRAY_PTR (rb_clusters)[i] = CRTEXTCLUSTER2RVAL (clusters + i);
    }

  return rb_clusters;
}

void
rb_cairo__text_clusters_from_ruby_object (VALUE rb_clusters,
                                          cairo_text_cluster_t **clusters,
                                          int *num_clusters)
{
  int i, len;

  if (NIL_P (rb_clusters))
    {
      *num_clusters = -1;
      return;
    }

  len = (int) RARRAY_LEN (rb_clusters);
  if (*num_clusters < len)
    *clusters = cairo_text_cluster_allocate (len);
  *num_clusters = len;
  for (i = 0; i < len; i++)
    {
      cairo_text_cluster_t *cluster;

      cluster = *clusters + i;
      *cluster = *(RVAL2CRTEXTCLUSTER (RARRAY_PTR (rb_clusters)[i]));
    }
}
#endif

VALUE
rb_cairo__invoke_callback (cr_callback_func_t func, VALUE data)
{
  int state = 0;
  VALUE result, exception;

  result = rb_protect (func, data, &state);
  if (state)
    {
      exception = RB_ERRINFO;
      if (exception)
        rb_funcall (rb_mCairo, cr_id_exit_application, 2,
                    exception, INT2NUM (EXIT_FAILURE));
    }
  return result;
}

void
Init_cairo_private (void)
{
  cr_id_normalize_const_name = rb_intern ("normalize_const_name");
  cr_id_objects = rb_intern ("objects");
  cr_id_dup = rb_intern ("dup");
  cr_id_inspect = rb_intern ("inspect");
  cr_id_exit_application = rb_intern ("exit_application");

  if (rb_const_defined (rb_cObject, rb_intern ("FFI")))
    {
      rb_cairo__cFFIPointer =
        rb_const_get (rb_const_get (rb_cObject, rb_intern ("FFI")),
                      rb_intern ("Pointer"));
    }
  else
    {
      rb_cairo__cFFIPointer = Qnil;
    }
}