Class: Mathematical::Process

Inherits:
Object
  • Object
show all
Defined in:
ext/mathematical/mathematical.c

Instance Method Summary collapse

Constructor Details

#initialize(rb_Options) ⇒ Object



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
# File 'ext/mathematical/mathematical.c', line 42

static VALUE MATHEMATICAL_init(VALUE self, VALUE rb_Options)
{
  Check_Type (rb_Options, T_HASH);
  VALUE rb_ppi, rb_zoom, rb_maxsize, rb_format;

  rb_ppi = rb_hash_aref(rb_Options, CSTR2SYM("ppi"));
  rb_zoom = rb_hash_aref(rb_Options, CSTR2SYM("zoom"));
  rb_maxsize = rb_hash_aref(rb_Options, CSTR2SYM("maxsize"));
  rb_format = rb_hash_aref(rb_Options, CSTR2SYM("formatInt"));

  Check_Type(rb_ppi, T_FLOAT);
  Check_Type(rb_zoom, T_FLOAT);
  Check_Type(rb_maxsize, T_FIXNUM);
  Check_Type(rb_format, T_FIXNUM);

  rb_iv_set(self, "@ppi", rb_ppi);
  rb_iv_set(self, "@zoom", rb_zoom);
  rb_iv_set(self, "@maxsize", rb_maxsize);
  rb_iv_set(self, "@format", rb_format);

  rb_iv_set(self, "@png", Qnil);
  rb_iv_set(self, "@svg", Qnil);

  return self;
}

Instance Method Details

#process(rb_Input) ⇒ Object



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
# File 'ext/mathematical/mathematical.c', line 197

static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input)
{
  unsigned long maxsize = (unsigned long) FIX2INT(rb_iv_get(self, "@maxsize"));

  // make sure that the passed latex string is not larger than the maximum value of a signed long (or the maxsize option)
  if (maxsize == 0) {
    maxsize = LONG_MAX;
  }

#if !GLIB_CHECK_VERSION(2,36,0)
  g_type_init ();
#endif

  const char *latex_code;
  unsigned long latex_size;

  VALUE output;

  switch (TYPE(rb_Input)) {
  case T_STRING: {
    latex_code = StringValueCStr(rb_Input);
    latex_size = (unsigned long) strlen(latex_code);
    output = process(self, maxsize, latex_code, latex_size);
    break;
  }
  case T_ARRAY: {
    int length = RARRAY_LEN(rb_Input), i;
    VALUE hash;
    output = rb_ary_new2(length);
    global_start = 1;

    for (i = 0; i < length; i++) {
      // grab the ith element
      VALUE math = rb_ary_entry(rb_Input, i);

      // get the string and length
      latex_code = StringValueCStr(math);
      latex_size = (unsigned long) strlen(latex_code);

      // `process` can potentially raise a bunch of exceptions, so we need to wrap
      // the call in a rescue. And `rb_rescue` only takes one argument, so we need
      // to pack everything in an array, and then unpack it in `process_helper`.
      VALUE args[5];
      args[0] = self;
      args[1] = ULONG2NUM(maxsize);
      args[2] = math;
      args[3] = ULONG2NUM(latex_size);
      hash = rb_rescue(process_helper, args, process_failed, 0);

      // the call errored; just store the same string
      if (hash == Qnil) {
        rb_ary_store(output, i, math);
      } else {
        rb_ary_store(output, i, hash);
      }
    }
    break;
  }
  default: {
    /* should be impossible, Ruby code prevents this */
    print_and_raise(rb_eTypeError, "not valid value");
    output = NULL;
    break;
  }
  }

  return output;
}