Class: Mathematical::Process

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

Instance Method Summary collapse

Constructor Details

#initialize(rb_Options) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/mathematical/mathematical.c', line 132

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("format"));

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

  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_LatexCode) ⇒ Object



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

static VALUE MATHEMATICAL_process(VALUE self, VALUE rb_LatexCode) {
  Check_Type (rb_LatexCode, T_STRING);

  unsigned long maxsize = (unsigned long) FIX2INT(rb_iv_get(self, "@maxsize"));

  const char *latex_code = StringValueCStr(rb_LatexCode);
  unsigned long latex_size = (unsigned long) strlen(latex_code);

  // 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 (latex_size > maxsize)
    rb_raise(rb_eMaxsizeError, "Size of latex string (%lu) is greater than the maxsize (%lu)!", latex_size, maxsize);

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

  VALUE result_hash = rb_hash_new();
  const char* rb_format = RSTRING_PTR(rb_iv_get(self, "@format"));

  // convert the TeX math to MathML
  char * mathml = mtex2MML_parse(latex_code, latex_size);
  if (mathml == NULL) rb_raise(rb_eParseError, "Failed to parse mtex");

  if (strncmp(rb_format, "mathml", 6) == 0) {
    rb_hash_aset (result_hash, rb_tainted_str_new2 ("mathml"),    rb_str_new2(mathml));
    mtex2MML_free_string(mathml);
    return result_hash;
  }

  int mathml_size = strlen(mathml);

  LsmDomDocument *document;
  document = lsm_dom_document_new_from_memory(mathml, mathml_size, NULL);

  if (mathml != NULL)
    mtex2MML_free_string(mathml);

  if (document == NULL) rb_raise(rb_eDocumentCreationError, "Failed to create document");

  LsmDomView *view;
  FileFormat format;

  double ppi = NUM2DBL(rb_iv_get(self, "@ppi"));
  double zoom = NUM2DBL(rb_iv_get(self, "@zoom"));

  view = lsm_dom_document_create_view (document);
  lsm_dom_view_set_resolution (view, ppi);

  double width_pt = 2.0, height_pt = 2.0;
  unsigned int height, width;

  lsm_dom_view_get_size (view, &width_pt, &height_pt, NULL);
  lsm_dom_view_get_size_pixels (view, &width, &height, NULL);

  width_pt *= zoom;
  height_pt *= zoom;

  cairo_t *cairo;
  cairo_surface_t *surface;

  if (strncmp(rb_format, "svg", 3) == 0) {
    format = FORMAT_SVG;
    surface = cairo_svg_surface_create_for_stream (cairoSvgSurfaceCallback, self, width_pt, height_pt);
  }
  else if (strncmp(rb_format, "png", 3) == 0) {
    format = FORMAT_PNG;
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
  }

  cairo = cairo_create (surface);
  cairo_surface_destroy (surface);
  cairo_scale (cairo, zoom, zoom);
  lsm_dom_view_render (view, cairo, 0, 0);

  switch (format) {
    case FORMAT_PNG:
      cairo_surface_write_to_png_stream (cairo_get_target (cairo), cairoPngSurfaceCallback, self);
      break;
    default:
      break;
  }

  cairo_destroy (cairo);
  g_object_unref (view);
  g_object_unref (document);

  switch (format) {
    case FORMAT_SVG:
      if (rb_iv_get(self, "@svg") == Qnil) rb_raise(rb_eDocumentReadError, "Failed to read SVG contents");
      rb_hash_aset (result_hash, rb_tainted_str_new2 ("svg"),    rb_iv_get(self, "@svg"));
      break;
    case FORMAT_PNG:
      if (rb_iv_get(self, "@png") == Qnil) rb_raise(rb_eDocumentReadError, "Failed to read PNG contents");
      rb_hash_aset (result_hash, rb_tainted_str_new2 ("png"),    rb_iv_get(self, "@png"));
      break;
    default:
      break;
  }

  rb_hash_aset (result_hash, rb_tainted_str_new2 ("width"),  INT2FIX(width_pt));
  rb_hash_aset (result_hash, rb_tainted_str_new2 ("height"), INT2FIX(height_pt));

  // we need to clear out this key when attempting multiple calls. See http://git.io/i1hblQ
  rb_iv_set(self, "@svg", Qnil);
  rb_iv_set(self, "@png", Qnil);

  return result_hash;
}