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);
g_type_init ();
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;
}
|