Class: LatexToPng::Convert

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = { filepath: nil, formula: nil, size: nil }) ⇒ Convert

or formula or filepath



48
49
50
51
52
53
54
# File 'lib/latex_to_png.rb', line 48

def initialize opts={ filepath: nil, formula: nil, size: nil }

    @filepath = opts[:filepath]  if opts[:filepath]
    @size = (opts[:size] || 10).to_i
    @formula = opts[:formula] if opts[:formula]

end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



35
36
37
# File 'lib/latex_to_png.rb', line 35

def filepath
  @filepath
end

Class Method Details

.templateObject



39
40
41
42
43
# File 'lib/latex_to_png.rb', line 39

def template
  return @doc if @doc
  @doc = ERB.new(File.read("#{ File.dirname __FILE__}/templates/equation.erb"))
  @doc
end

Instance Method Details

#convert(filepath, size, name = filepath.split("/").last.split(".").first) ⇒ Object



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
# File 'lib/latex_to_png.rb', line 84

def convert filepath, size,
  name = filepath.split("/").last.split(".").first
  dirname =  File.dirname filepath
  density = ((300/10)*size).to_i
# debugger
    #convert for .dvi
    # dvi to .ps
    # .ps to .png "q*" option is to run quietly
    # %x(
    #   cd #{dirname}; latex  -halt-on-error  -output-format=pdf #{filepath} &&
    #   convert -density #{density}x#{density} #{name}.pdf  -trim  #{name}.png  1>&2 > /dev/null
    #   )
    #
    #   Thread.new {
    #      %x(cd #{dirname}; rm -f  #{name}.log #{name}.pdf #{name}.aux &)
    #   }.run()

     %x(
       cd #{dirname}; latex -halt-on-error  #{filepath} &&
       dvips -q* -E #{name}.dvi &&
       convert -density #{density}x#{density} #{name}.ps #{name}.png  1>&2 > /dev/null
       )

      Thread.new {
         %x(cd #{dirname}; rm -f #{name}.dvi #{name}.log  #{name}.aux  #{name}.ps &)
      }.run()



     png_path = "#{filepath.gsub(/.tex$/,"")}.png"

     if File.exist?(png_path)
       return open(png_path)
     else
       raise StandardError("Image not generated")
     end
end

#from_estatic(filepath, size) ⇒ Object



79
80
81
82
# File 'lib/latex_to_png.rb', line 79

def from_estatic filepath, size

  convert(filepath, size )
end

#from_formula(formula, size) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/latex_to_png.rb', line 61

def from_formula formula, size


  tmp_file = Tempfile.new("formula")
  tmp_file.write  mount_tex(formula)
  tmp_file.close
  # debugger
  filepath = tmp_file.path

  convert(filepath,size )

end

#mount_tex(formula) ⇒ Object



74
75
76
# File 'lib/latex_to_png.rb', line 74

def mount_tex formula
   self.class.template.result(Formula.new( formula ).instance_eval { binding })
end

#size_in_points(size_in_pixels) ⇒ Object



56
57
58
59
# File 'lib/latex_to_png.rb', line 56

def size_in_points size_in_pixels
  size = Sizes.invert[size_in_pixels]
  return (size.nil?)? size_in_points("#{size_in_pixels.to_i + 1}px") : size
end

#to_pngObject



123
124
125
126
127
128
129
130
# File 'lib/latex_to_png.rb', line 123

def to_png
  if @formula
    from_formula @formula, @size

  else
    from_estatic @filepath, @size
 end
end