Class: Mathematical::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/mathematical/render.rb

Constant Summary collapse

DEFAULT_OPTS =
{
  :ppi => 72.0,
  :zoom => 1.0,
  :base64 => false,
  :maxsize => 0
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Render

Returns a new instance of Render.

Raises:

  • (TypeError)


14
15
16
17
18
19
# File 'lib/mathematical/render.rb', line 14

def initialize(opts = {})
  @config = DEFAULT_OPTS.merge(opts)
  raise(TypeError, "maxsize must be an integer!") unless @config[:maxsize].is_a? Fixnum
  raise(TypeError, "maxsize cannot be less than 0!") if @config[:maxsize] < 0
  @processer = Mathematical::Process.new(@config)
end

Instance Method Details

#render(maths) ⇒ Object

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mathematical/render.rb', line 21

def render(maths)
  raise(TypeError, "text must be a string!") unless maths.is_a? String
  raise(ArgumentError, "text must be in itex format (`$...$` or `$$...$$`)!") unless maths =~ /\A\${1,2}/

  # seems to be a bug in [email protected] where the "Vertical spacing and page breaks in multiline display" (\\)
  # do not work, and yield an "unknown character" error
  maths.gsub!(/\\\\/, "\\\\\\\\")

  # `{align}` *should* be valid, according to AMS-Latex, but it seems [email protected] does not like it.
  maths.gsub!(/\\begin\{align\}/, "\\begin{aligned}")
  maths.gsub!(/\\end\{align\}/, "\\end{aligned}")

  begin
    raise RuntimeError unless svg_hash = @processer.process(maths)
    svg_hash["svg"] = svg_hash["svg"][xml_header.length..-1] # remove starting <?xml...> tag
    svg_hash["svg"] = svg_to_base64(svg_hash["svg"]) if @config[:base64]
    svg_hash
  rescue ParseError, DocumentCreationError, DocumentReadError => e # an error in the C code, probably a bad TeX parse
    $stderr.puts "#{e.message}: #{maths}"
    maths
  end
end