Class: Mathematical::Process

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

Instance Method Summary collapse

Constructor Details

#initialize(rb_Options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'ext/mathematical/mathematical.c', line 42

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'ext/mathematical/mathematical.c', line 58

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

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

  if (mathml == 0) 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);
  g_free (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);

  return 0;
}