Class: DocXify::Element::Image

Inherits:
Base
  • Object
show all
Defined in:
lib/docxify/element/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Image

Returns a new instance of Image.



6
7
8
9
10
11
12
13
14
# File 'lib/docxify/element/image.rb', line 6

def initialize(file, options = {})
  super()
  @file = file

  @align = options[:align] || :left
  @after = options[:after]
  @height_cm = options[:height_cm] || 5
  @width_cm = options[:width_cm] || 5
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



4
5
6
# File 'lib/docxify/element/image.rb', line 4

def align
  @align
end

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/docxify/element/image.rb', line 4

def file
  @file
end

#height_cmObject

Returns the value of attribute height_cm.



4
5
6
# File 'lib/docxify/element/image.rb', line 4

def height_cm
  @height_cm
end

#width_cmObject

Returns the value of attribute width_cm.



4
5
6
# File 'lib/docxify/element/image.rb', line 4

def width_cm
  @width_cm
end

Instance Method Details

#idObject



16
17
18
# File 'lib/docxify/element/image.rb', line 16

def id
  rand(1_000_000_000)
end

#to_s(_container = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/docxify/element/image.rb', line 20

def to_s(_container = nil)
  xml = "<w:p>"

  xml << "<w:pPr>"
  if @align == :right
    xml << "<w:jc w:val=\"right\"/>"
  elsif @align == :center
    xml << "<w:jc w:val=\"center\"/>"
  end

  xml << "<w:spacing w:after=\"#{DocXify.pt2spacing @after}\"/>" if @after

  xml << "</w:pPr>"

  xml << <<~XML
      <w:r>
        <w:rPr>
          <w:noProof/>
        </w:rPr>
        <w:drawing>
          <wp:inline distB="0" distL="0" distR="0" distT="0">
            <wp:extent cx="#{DocXify.cm2emu(@width_cm)}" cy="#{DocXify.cm2emu(@height_cm)}"/>
            <wp:effectExtent b="0" l="0" r="0" t="0"/>
            <wp:docPr id="#{id}" name="Picture 1"/>
            <wp:cNvGraphicFramePr>
              <a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
            </wp:cNvGraphicFramePr>
            <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                  <pic:nvPicPr>
                    <pic:cNvPr id="#{id}" name="Picture #{id}"/>
                    <pic:cNvPicPr/>
                  </pic:nvPicPr>
                  <pic:blipFill>
                    <a:blip cstate="print" r:embed="#{@file.reference}">
                      <a:extLst>
                        <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                          <a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/>
                        </a:ext>
                      </a:extLst>
                    </a:blip>
                    <a:stretch>
                      <a:fillRect/>
                    </a:stretch>
                  </pic:blipFill>
                  <pic:spPr>
                    <a:xfrm>
                      <a:off x="0" y="0"/>
                      <a:ext cx="#{DocXify.cm2emu(@width_cm)}" cy="#{DocXify.cm2emu(@height_cm)}"/>
                    </a:xfrm>
                    <a:prstGeom prst="rect">
                      <a:avLst/>
                    </a:prstGeom>
                  </pic:spPr>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>
      </w:r>
    </w:p>
  XML
end