Class: Docxi::Word::Contents::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/docxi/word/contents/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Image.



8
9
10
11
12
# File 'lib/docxi/word/contents/image.rb', line 8

def initialize(media, options={})
  @media = media
  # @media.file.rewind
  @options = options
end

Instance Attribute Details

#mediaObject

Returns the value of attribute media.



7
8
9
# File 'lib/docxi/word/contents/image.rb', line 7

def media
  @media
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/docxi/word/contents/image.rb', line 7

def options
  @options
end

Instance Method Details

#render(xml) ⇒ 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/docxi/word/contents/image.rb', line 20

def render(xml)
  xml['w'].r do
    if options[:pH] and options[:pV] and options[:wrap]
      xml['w'].drawing do
        xml['wp'].anchor( "behindDoc" => 1 ,"distT" => 0,"distB" => 0, "distL" => 0, "distR" => 0 ,"simplePos" => 0 ,"locked" => 0, "layoutInCell" => 1, "allowOverlap" => 1, "relativeHeight" => 3) do
          xml['wp'].simplePos('x'=> 0 , 'y' => 0 )
          xml['wp'].positionH("relativeFrom" => "column") do
            xml['wp'].posOffset options[:pH] 
          end
          xml['wp'].positionV("relativeFrom" => "paragraph") do
            xml['wp'].posOffset options[:pV] 
          end
          xml['wp'].extent( 'cx' => ( options[:width] * options[:height] * 14.92 ).to_i, 'cy' => ( options[:width] * options[:height] * 14.92 ).to_i ) if options[:width] && options[:height]
          xml['wp'].effectExtent( 'l' => 0, 't' => 0, 'r' => 0, 'b' => 0 )
          xml['wp'].wrapNone
          xml['wp'].docPr( 'id' => 1, 'name'=> "Image", 'descr' => "image")
          xml['wp'].cNvGraphicFramePr do
            xml.graphicFrameLocks( 'xmlns:a' => "http://schemas.openxmlformats.org/drawingml/2006/main", 'noChangeAspect' => "1" ) do
              xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "a" }
            end
          end
          xml.graphic( 'xmlns:a' => "http://schemas.openxmlformats.org/drawingml/2006/main" ) do
            xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "a" }
            xml['a'].graphicData( 'uri' => "http://schemas.openxmlformats.org/drawingml/2006/picture") do
              xml.pic( 'xmlns:pic' => "http://schemas.openxmlformats.org/drawingml/2006/picture") do
                xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "pic" }
                xml['pic'].nvPicPr do
                  xml['pic'].cNvPr( 'id' => 0, 'name' => "Image" )
                  xml['pic'].cNvPicPr do
                    xml['a'].picLocks( 'noChangeAspect' => "1", 'noChangeArrowheads' => "1" )
                  end
                end
                xml['pic'].blipFill do
                  xml['a'].blip( 'r:embed' => @media.sequence )
                  xml['a'].stretch do
                    xml['a'].fillRect
                  end
                end
                xml['pic'].spPr( 'bwMode' => "auto" ) do
                  xml['a'].xfrm do
                    xml['a'].off( 'x' => 0, 'y' => 0 )
                    xml['a'].ext( 'cx' => ( options[:width] * options[:height] * 14.92 ).to_i, 'cy' => ( options[:width] * options[:height] * 14.92).to_i ) if options[:width] && options[:height]
                  end
                  xml['a'].prstGeom( 'prst' => "rect" ) do
                    xml['a'].avLst
                  end
                  xml['a'].noFill
                  xml['a'].ln('w' => "9525") do
                    xml['a'].noFill
                    xml['a'].miter('lim'=>"800000")
                    xml['a'].headEnd
                    xml['a'].tailEnd
                  end
                end
              end
            end
          end
        end
      end
    else  
      xml['w'].pict do
        unless options[:fill_color]
          xml['v'].rect( 'id' => @media.uniq_id, 'type' => @media.type, 'style' => styles, :stroked => "f" ) do
            xml['v'].imagedata( 'r:id' => @media.sequence, 'o:title' => @options[:title] )
            xml['v'].wrap('v:type' => 'none') if options[:wrap]
          end
        else
          xml['v'].rect("fillcolor"=> options[:fill_color], 'id' => @media.uniq_id, 'type' => @media.type, 'style' => styles, :stroked => "f" ) do
            if options[:opacity].present?
              xml['v'].fill("opacity" => options[:opacity])
            end
            xml['v'].imagedata( 'r:id' => @media.sequence, 'o:title' => @options[:title] )
            xml['v'].wrap('v:type' => 'none') if options[:wrap]
          end
        end
      end
    end
  end
end

#stylesObject



14
15
16
17
18
# File 'lib/docxi/word/contents/image.rb', line 14

def styles
  if @options[:style]
    @options[:style].collect{|k, v| [k, v].join(':')}.join(';')
  end
end