Class: RubyDocx::Elements::OleObject

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/ole_object.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

#image_pathObject

Returns the value of attribute image_path.



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

def image_path
  @image_path
end

Returns the value of attribute link.



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

def link
  @link
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#zipObject

Returns the value of attribute zip.



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

def zip
  @zip
end

Instance Method Details

#base64_dataObject



69
70
71
# File 'lib/ruby_docx/elements/ole_object.rb', line 69

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

#dataObject



65
66
67
# File 'lib/ruby_docx/elements/ole_object.rb', line 65

def data
  self.to_png
end

#relation_idObject

def style

element = @node.xpath(".//v:shape").first
if element && element.attributes.keys.index("style")
  element.attributes["style"].value
else
  nil
end

end



38
39
40
41
42
43
44
45
# File 'lib/ruby_docx/elements/ole_object.rb', line 38

def relation_id
  element = @node.xpath(".//o:OLEObject").first
  if element && element.attributes.keys.index("id")
    element.attributes["id"].value
  else
    nil
  end
end

#replace(lnk) ⇒ Object



97
98
99
# File 'lib/ruby_docx/elements/ole_object.rb', line 97

def replace(lnk)
  @link = lnk
end

#save(path) ⇒ Object



91
92
93
94
95
# File 'lib/ruby_docx/elements/ole_object.rb', line 91

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

#sizeObject



73
74
75
76
77
78
79
80
81
# File 'lib/ruby_docx/elements/ole_object.rb', line 73

def size
  return @size if @size

  sz = ImageSize.new(self.data)

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

#styleObject



83
84
85
86
87
88
89
# File 'lib/ruby_docx/elements/ole_object.rb', line 83

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

#to_htmlObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_docx/elements/ole_object.rb', line 101

def to_html
  if self.style
    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

  else
    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='height: 13px;' />"
    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='height: 13px;' />"
    end
  end

end

#to_latexObject



56
57
58
# File 'lib/ruby_docx/elements/ole_object.rb', line 56

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

#to_mathmlObject



47
48
49
50
51
52
53
54
# File 'lib/ruby_docx/elements/ole_object.rb', line 47

def to_mathml
  io = StringIO.new(@zip.read("word/" + @path))
  # xml = Mathtype::Converter.new(io)
  # # puts xml.to_xml
  # xml.to_xml

  mathml = MathTypeToMathML::Converter.new(io).convert
end

#to_pngObject



60
61
62
63
# File 'lib/ruby_docx/elements/ole_object.rb', line 60

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

#to_sObject



119
120
121
# File 'lib/ruby_docx/elements/ole_object.rb', line 119

def to_s
  self.to_latex.to_s
end