Class: XlsxWriter::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx_writer/xml/image.rb

Constant Summary collapse

DEFAULT =
{
  :croptop => 0,
  :cropleft => 0
}
AUTO =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, original_path, width, height) ⇒ Image

Returns a new instance of Image.



19
20
21
22
23
24
25
# File 'lib/xlsx_writer/xml/image.rb', line 19

def initialize(document, original_path, width, height)
  @document = document
  @original_path = original_path
  @width = width
  @height = height
  @mutex = ::Mutex.new
end

Instance Attribute Details

#cropleftObject



40
41
42
# File 'lib/xlsx_writer/xml/image.rb', line 40

def cropleft
  @cropleft || DEFAULT[:cropleft]
end

#croptopObject



36
37
38
# File 'lib/xlsx_writer/xml/image.rb', line 36

def croptop
  @croptop || DEFAULT[:croptop]
end

#documentObject (readonly)

Returns the value of attribute document.



11
12
13
# File 'lib/xlsx_writer/xml/image.rb', line 11

def document
  @document
end

#heightObject (readonly)

Returns the value of attribute height.



14
15
16
# File 'lib/xlsx_writer/xml/image.rb', line 14

def height
  @height
end

#lcrObject

Returns the value of attribute lcr.



15
16
17
# File 'lib/xlsx_writer/xml/image.rb', line 15

def lcr
  @lcr
end

#original_pathObject (readonly)

Returns the value of attribute original_path.



12
13
14
# File 'lib/xlsx_writer/xml/image.rb', line 12

def original_path
  @original_path
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/xlsx_writer/xml/image.rb', line 13

def width
  @width
end

Instance Method Details

#absolute_pathObject



89
90
91
# File 'lib/xlsx_writer/xml/image.rb', line 89

def absolute_path
  "/#{relative_path}"
end

#generateObject



52
53
54
55
# File 'lib/xlsx_writer/xml/image.rb', line 52

def generate
  path
  true
end

#generated?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/xlsx_writer/xml/image.rb', line 57

def generated?
  @generated == true
end

#idObject



44
45
46
47
48
49
50
# File 'lib/xlsx_writer/xml/image.rb', line 44

def id
  if lcr
    lcr.image_id
  else
    o_spid #?
  end
end

#ndxObject



73
74
75
# File 'lib/xlsx_writer/xml/image.rb', line 73

def ndx
  document.images.index(self) + 1
end

#o_spidObject



85
86
87
# File 'lib/xlsx_writer/xml/image.rb', line 85

def o_spid
  "_x0000_s#{1025+ndx}"
end

#o_titleObject



81
82
83
# File 'lib/xlsx_writer/xml/image.rb', line 81

def o_title
  ::File.basename(original_path)
end

#pathObject



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

def path
  @path || @mutex.synchronize do
    @path ||= begin
      memo = ::File.join document.staging_dir, relative_path
      ::FileUtils.mkdir_p ::File.dirname(memo)
      ::FileUtils.cp original_path, memo
      @generated = true
      memo
    end
  end
end

#relative_pathObject



93
94
95
# File 'lib/xlsx_writer/xml/image.rb', line 93

def relative_path
  "xl/media/image#{ndx}.emf"
end

#ridObject



77
78
79
# File 'lib/xlsx_writer/xml/image.rb', line 77

def rid
  "rId#{ndx}"
end

#to_xmlObject



27
28
29
30
31
32
33
34
# File 'lib/xlsx_writer/xml/image.rb', line 27

def to_xml
  <<-EOS
<v:shape id="#{id}" o:spid="#{o_spid}" type="#_x0000_t75" style="position:absolute;margin-left:0;margin-top:0;width:#{width}pt;height:#{height}pt;z-index:1">
  <v:imagedata o:relid="#{rid}" o:title="#{o_title}" croptop="#{croptop}" cropleft="#{cropleft}"/>
  <o:lock v:ext="edit" rotation="t"/>
</v:shape>
EOS
end