diff options
| author | Randy Morgan <[email protected]> | 2011-11-23 21:45:54 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2011-11-23 21:45:54 +0900 |
| commit | c8c63518b4d58ca8875f81602792050cbec318f2 (patch) | |
| tree | 14f6e2c5286e5509879b6b664205f3ea2f38180c /lib/axlsx/drawing/pic.rb | |
| parent | 2dea87f6f601795e32c7c14fbba5717c4b04fc1e (diff) | |
| download | caxlsx-c8c63518b4d58ca8875f81602792050cbec318f2.tar.gz caxlsx-c8c63518b4d58ca8875f81602792050cbec318f2.zip | |
Adding image support and some document clean up for .8 release
Diffstat (limited to 'lib/axlsx/drawing/pic.rb')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb new file mode 100644 index 00000000..7043df35 --- /dev/null +++ b/lib/axlsx/drawing/pic.rb @@ -0,0 +1,158 @@ +# -*- coding: utf-8 -*- +module Axlsx + # a Pic object represents an image in your worksheet + # Worksheet#add_image is the recommended way to manage images in your sheets + # @see Worksheet#add_image + class Pic + + # allowed file extenstions + ALLOWED_EXTENSIONS = ['gif', 'jpeg', 'png', 'jpg'] + + # The name to use for this picture + # @return [String] + attr_accessor :name + + # providing access to the anchor's width attribute + # @param [Integer] v + # @see OneCellAnchor.width + attr_accessor :width + + # providing access to update the anchor's height attribute + # @param [Integer] v + # @see OneCellAnchor.width + attr_accessor :height + + # A description of the picture + # @return [String] + attr_accessor :descr + + # The path to the image you want to include + # Only local images are supported at this time and only jpg support + # @return [String] + attr_accessor :image_src + + # The anchor for this image + # @return [OneCellAnchor] + attr_reader :anchor + + # returns the extension of image_src without the preceeding '.' + # @return [String] + attr_reader :extname + + # The file name of image_src without any path information + # @return [String] + attr_reader :file_name + + # The part name for this image used in serialization and relationship building + # @return [String] + attr_reader :pn + + # The index of this image in the workbooks images collections + # @return [Index] + attr_reader :index + + # The name of the image file, sans directory info + # @return [String] + attr_reader :file_name + + # Creates a new Pic(ture) object + # @param [Anchor] anchor the anchor that holds this image + # @option options [String] name + # @option options [String] descr + # @option options [String] image_src + # @option options [Array] start_at + # @option options [Intger] width + # @option options [Intger] height + def initialize(anchor, options={}) + @anchor = anchor + @anchor.drawing.worksheet.workbook.images << self + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + start_at *options[:start_at] if options[:start_at] + yield self if block_given? + end + + def image_src=(v) + Axlsx::validate_string(v) + RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v).delete('.') + raise ArgumentError, "File does not exist" unless File.exist?(v) + @image_src = v + end + + def name=(v) Axlsx::validate_string(v); @name = v; end + def descr=(v) Axlsx::validate_string(v); @descr = v; end + + def file_name + File.basename(image_src) unless image_src.nil? + end + + def extname + File.extname(image_src).delete('.') unless image_src.nil? + end + + def index + @anchor.drawing.worksheet.workbook.images.index(self) + end + + def pn + "#{IMAGE_PN % [(index+1), extname]}" + end + + def width + @anchor.width + end + + def width=(v) + @anchor.width = v + end + + def height=(v) + @anchor.height = v + end + + def height + @anchor.height + end + + # This is a short cut method to set the start anchor position + # If you need finer granularity in positioning use + # graphic_frame.anchor.from.colOff / rowOff + # @param [Integer] x The column + # @param [Integer] y The row + # @return [Marker] + def start_at(x, y) + @anchor.from.col = x + @anchor.from.row = y + end + + # Serializes the picture + # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. + # @return [String] + def to_xml(xml) + xml.send('xdr:pic') { + xml.send('xdr:nvPicPr') { + xml.send('xdr:cNvPr', :id=>"2", :name=>name, :descr=>descr) + xml.send('xdr:cNvPicPr') { + xml.send('a:picLocks', :noChangeAspect=>1) + } + } + xml.send('xdr:blipFill') { + xml.send('a:blip', :'xmlns:r' => XML_NS_R, :'r:embed'=>"rId1") + xml.send('a:stretch') { + xml.send('a:fillRect') + } + } + xml.send('xdr:spPr') { + xml.send('a:xfrm') { + xml.send('a:off', :x=>0, :y=>0) + xml.send('a:ext', :cx=>2336800, :cy=>2161540) + } + xml.send('a:prstGeom', :prst=>:rect) { + xml.send('a:avLst') + } + } + } + end + end +end |
