Class: RubyDocx::Elements::Equation

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/equation.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#doc, #grid, #node

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_xml

Constructor Details

This class inherits a constructor from RubyDocx::Elements::Element

Instance Attribute Details

Returns the value of attribute link.



8
9
10
# File 'lib/ruby_docx/elements/equation.rb', line 8

def link
  @link
end

Instance Method Details

#base64_dataObject



58
59
60
# File 'lib/ruby_docx/elements/equation.rb', line 58

def base64_data
  "data:image/png;base64,#{Base64.strict_encode64(self.to_png)}"
end

#dataObject



54
55
56
# File 'lib/ruby_docx/elements/equation.rb', line 54

def data
  self.to_png
end

#replace(lnk) ⇒ Object



68
69
70
# File 'lib/ruby_docx/elements/equation.rb', line 68

def replace(lnk)
  @link = lnk
end

#replace_with_latex(txt) ⇒ Object



27
28
# File 'lib/ruby_docx/elements/equation.rb', line 27

def replace_with_latex(txt)
end

#replace_with_mathml(txt) ⇒ Object



24
25
# File 'lib/ruby_docx/elements/equation.rb', line 24

def replace_with_mathml(txt)
end

#save(path) ⇒ Object



62
63
64
65
66
# File 'lib/ruby_docx/elements/equation.rb', line 62

def save(path)
  file = File.new(path, "wb")
  file.write(self.to_png)
  file.close
end

#sizeObject



35
36
37
38
39
40
41
42
43
# File 'lib/ruby_docx/elements/equation.rb', line 35

def size
  return @size if @size

  sz = ImageSize.new(self.data)

  @size = [sz.width/7.2, sz.height/7.2]
rescue
  nil
end

#styleObject



45
46
47
48
49
50
51
# File 'lib/ruby_docx/elements/equation.rb', line 45

def style
  if self.size
    "width: #{self.size[0]}px; height: #{self.size[1]}px;"
  else
    ""
  end
end

#to_htmlObject



72
73
74
75
76
77
78
# File 'lib/ruby_docx/elements/equation.rb', line 72

def to_html
  if @link
    "<img class=\"Wirisformula\" role='math' src='#{@link}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml.gsub("\n", "").gsub("<", "«").gsub(">", "»").gsub("\"", "¨")}\" style='#{self.style}' />"
  else
    "<img class=\"Wirisformula\" role='math' src='#{self.base64_data}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml.gsub("\n", "").gsub("<", "«").gsub(">", "»").gsub("\"", "¨")}\" style='#{self.style}' />"
  end
end

#to_latexObject



15
16
17
# File 'lib/ruby_docx/elements/equation.rb', line 15

def to_latex
   TeXMath.convert("#{self.to_omml}", :from => :omml, :to => :tex)
end

#to_mathmlObject



19
20
21
# File 'lib/ruby_docx/elements/equation.rb', line 19

def to_mathml
   TeXMath.convert("#{self.to_omml}", :from => :omml, :to => :mathml)
end

#to_ommlObject



10
11
12
13
# File 'lib/ruby_docx/elements/equation.rb', line 10

def to_omml
  ele = @node.xpath(".//m:oMath").first
  ele.to_s
end

#to_pngObject



30
31
32
33
# File 'lib/ruby_docx/elements/equation.rb', line 30

def to_png
  @path ||= Calculus::Expression.new("#{self.to_latex}", :parse => false).to_png
  File.read(@path)
end

#to_sObject



80
81
82
# File 'lib/ruby_docx/elements/equation.rb', line 80

def to_s
  self.to_latex.to_s
end