Sha256: 4e0901582d213d474ec075f3aee6c2c6fce555f7c00e1e6744b6616a80ecb59b

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

require 'fileutils'

module XlsxWriter
  class Image
    DEFAULT = {
      :croptop => 0,
      :cropleft => 0
    }
    AUTO = false

    attr_reader :document
    attr_reader :original_path
    attr_reader :width
    attr_reader :height
    attr_accessor :lcr
    attr_writer :croptop
    attr_writer :cropleft

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

    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
    
    def croptop
      @croptop || DEFAULT[:croptop]
    end
    
    def cropleft
      @cropleft || DEFAULT[:cropleft]
    end
    
    def id
      if lcr
        lcr.image_id
      else
        o_spid #?
      end
    end

    def generate
      path
      true
    end

    def generated?
      @generated == true
    end

    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
    
    def ndx
      document.images.index(self) + 1
    end
    
    def rid
      "rId#{ndx}"
    end
    
    def o_title
      ::File.basename(original_path)
    end
    
    def o_spid
      "_x0000_s#{1025+ndx}"
    end
    
    def absolute_path
      "/#{relative_path}"
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
xlsx_writer-0.3.2 lib/xlsx_writer/generators/image.rb
xlsx_writer-0.3.1 lib/xlsx_writer/generators/image.rb
xlsx_writer-0.3.0 lib/xlsx_writer/generators/image.rb
xlsx_writer-0.2.2 lib/xlsx_writer/generators/image.rb
xlsx_writer-0.2.1 lib/xlsx_writer/generators/image.rb
xlsx_writer-0.2.0 lib/xlsx_writer/generators/image.rb