Class: Mathematical::Process

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

Instance Method Summary collapse

Constructor Details

#initialize(rb_Options) ⇒ Object



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

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_delimiter;

  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"));
  rb_delimiter = rb_hash_aref(rb_Options, CSTR2SYM("delimiter"));

  Check_Type(rb_ppi, T_FLOAT);
  Check_Type(rb_zoom, T_FLOAT);
  Check_Type(rb_maxsize, T_FIXNUM);
  Check_Type(rb_format, T_FIXNUM);
  Check_Type(rb_delimiter, 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, "@delimiter", rb_delimiter);

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

  return self;
}

Instance Method Details

#process(rb_Input, rb_ParseType) ⇒ Object



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

static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_Input, VALUE rb_ParseType)
{
  Check_Type(rb_ParseType, T_FIXNUM);

  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);

    VALUE args[6];
    args[0] = self;
    args[1] = ULONG2NUM(maxsize);
    args[2] = rb_Input;
    args[3] = ULONG2NUM(latex_size);
    args[4] = rb_iv_get(self, "@delimiter");
    args[5] = rb_ParseType;

    output = rb_rescue(process_helper, args, process_rescue, rb_Input);
    break;
  }
  case T_ARRAY: {
    int length = RARRAY_LEN(rb_Input), i;
    VALUE hash;
    output = rb_ary_new2(length);

    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);

      VALUE args[6];
      args[0] = self;
      args[1] = ULONG2NUM(maxsize);
      args[2] = math;
      args[3] = ULONG2NUM(latex_size);
      args[4] = rb_iv_get(self, "@delimiter");
      args[5] = rb_ParseType;

      hash = rb_rescue(process_helper, args, process_rescue, math);

      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;
}