Class: Mathematical::Process

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

Instance Method Summary collapse

Constructor Details

#initialize(rb_Options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/mathematical/mathematical.c', line 60

static VALUE MATHEMATICAL_init(VALUE self, VALUE rb_Options) {
  Check_Type (rb_Options, T_HASH);
  VALUE ppi, zoom;

  ppi = rb_hash_aref(rb_Options, CSTR2SYM("ppi"));
  zoom = rb_hash_aref(rb_Options, CSTR2SYM("zoom"));

  Check_Type(ppi, T_FLOAT);
  Check_Type(zoom, T_FLOAT);

  rb_iv_set(self, "@ppi", ppi);
  rb_iv_set(self, "@zoom", zoom);

  return self;
}

Instance Method Details

#process(rb_LatexCode, rb_TempFile) ⇒ Object



76
77
78
79
80
81
82
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
# File 'ext/mathematical/mathematical.c', line 76

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

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

  const char *tempfile = StringValueCStr(rb_TempFile);

  g_type_init ();

  // convert the TeX math to MathML
  char * mathml = lsm_itex_to_mathml(latex_code, latex_size);

  if (mathml == NULL) rb_raise(rb_eRuntimeError, "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_eRuntimeError, "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;
  width *= zoom;
  height *= zoom;

  cairo_t *cairo;
  cairo_surface_t *surface;

  surface = cairo_svg_surface_create (tempfile, 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);

  char* svg_contents = readFile(tempfile);

  if (svg_contents == NULL) rb_raise(rb_eRuntimeError, "Failed to read SVG contents");

  return rb_str_new2(readFile(tempfile));
}