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
|
# File 'ext/mathematical/mathematical.c', line 83
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
// convert the TeX math to MathML
char * mathml = lsm_itex_to_mathml(latex_code, latex_size);
if (mathml == NULL) rb_raise(rb_eParseError, "Failed to parse itex");
int mathml_size = strlen(mathml);
LsmDomDocument *document;
document = lsm_dom_document_new_from_memory(mathml, mathml_size, NULL);
lsm_itex_free_mathml_buffer (mathml);
if (document == NULL) rb_raise(rb_eDocumentCreationError, "Failed to create document");
LsmDomView *view;
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;
surface = cairo_svg_surface_create_for_stream (cairoSvgSurfaceCallback, self, width_pt, height_pt);
cairo = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_scale (cairo, zoom, zoom);
lsm_dom_view_render (view, cairo, 0, 0);
cairo_destroy (cairo);
g_object_unref (view);
g_object_unref (document);
if (rb_iv_get(self, "@svg") == Qnil) rb_raise(rb_eDocumentReadError, "Failed to read SVG contents");
VALUE result_hash = rb_hash_new();
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));
rb_hash_aset (result_hash, rb_tainted_str_new2 ("svg"), rb_iv_get(self, "@svg"));
// we need to clear out this key when attempting multiple calls. See http://git.io/i1hblQ
rb_iv_set(self, "@svg", Qnil);
return result_hash;
}
|