Class: RubyDocx::Elements::Drawing

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

Instance Attribute Summary collapse

Attributes inherited from Element

#doc, #grid, #node

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_s, #to_xml

Constructor Details

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

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/ruby_docx/elements/drawing.rb', line 7

def height
  @height
end

Returns the value of attribute link.



6
7
8
# File 'lib/ruby_docx/elements/drawing.rb', line 6

def link
  @link
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/ruby_docx/elements/drawing.rb', line 6

def path
  @path
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/ruby_docx/elements/drawing.rb', line 7

def width
  @width
end

#zipObject

Returns the value of attribute zip.



6
7
8
# File 'lib/ruby_docx/elements/drawing.rb', line 6

def zip
  @zip
end

Instance Method Details

#base64_dataObject



83
84
85
# File 'lib/ruby_docx/elements/drawing.rb', line 83

def base64_data
  "data:#{self.mime};base64,#{Base64.strict_encode64(self.data)}"
end

#dataObject



9
10
11
12
13
14
15
16
# File 'lib/ruby_docx/elements/drawing.rb', line 9

def data
  if self.path
    @zip.read("word/#{@path}")
  else

  end

end

#mimeObject



79
80
81
# File 'lib/ruby_docx/elements/drawing.rb', line 79

def mime
  MimeMagic.by_magic(self.data).type
end

#relation_idObject



18
19
20
21
22
23
24
25
# File 'lib/ruby_docx/elements/drawing.rb', line 18

def relation_id
  element = @node.xpath(".//pic:blipFill/a:blip", "a" => "http://schemas.openxmlformats.org/drawingml/2006/main", "pic" => "http://schemas.openxmlformats.org/drawingml/2006/picture").first
  if element && element.attributes.keys.index("embed")
    element.attributes["embed"].value
  else
    nil
  end
end

#replace(lnk) ⇒ Object



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

def replace(lnk)
  @link = lnk
end

#save(path) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/ruby_docx/elements/drawing.rb', line 96

def save(path)
  if @zip && @path
    file = File.new(path, "wb")
    file.write(self.data)
    file.close
  end
end

#styleObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_docx/elements/drawing.rb', line 47

def style
  element = @node.xpath(".//v:shape").first

  if element && element.attributes.keys.index("style")
    element.attributes["style"].value
  else
    s = ""
    if self.width
      s += "width: #{self.width}px;"
    end

    if self.height
      s += "height: #{self.height}px;"
    end

    if self.width || self.height
      s
    else
      nil
    end
  end
end

#titleObject



70
71
72
73
74
75
76
77
# File 'lib/ruby_docx/elements/drawing.rb', line 70

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

#to_htmlObject



87
88
89
90
91
92
93
94
# File 'lib/ruby_docx/elements/drawing.rb', line 87

def to_html
  if @link.to_s.size > 0
    "<img src='#{@link}' alt=\"#{self.title}\" style=\"#{self.style}\" />"
  else
    "<img src='#{self.base64_data}' alt=\"#{self.title}\" style=\"#{self.style}\" />"
  end

end