lib/axlsx/drawing/pic.rb in axlsx-1.3.1 vs lib/axlsx/drawing/pic.rb in axlsx-1.3.2

- old
+ new

@@ -3,18 +3,37 @@ # 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 + include Axlsx::OptionsParser + + # 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 + @hyperlink = nil + @anchor.drawing.worksheet.workbook.images << self + parse_options options + start_at(*options[:start_at]) if options[:start_at] + yield self if block_given? + @picture_locking = PictureLocking.new(options) + end + # allowed file extenstions ALLOWED_EXTENSIONS = ['gif', 'jpeg', 'png', 'jpg'] # The name to use for this picture # @return [String] attr_reader :name - # A description of the picture # @return [String] attr_reader :descr # The path to the image you want to include @@ -27,30 +46,10 @@ attr_reader :anchor # The picture locking attributes for this picture attr_reader :picture_locking - # 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 - @hyperlink = nil - @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? - @picture_locking = PictureLocking.new(options) - end - attr_reader :hyperlink # sets or updates a hyperlink for this image. # @param [String] v The href value for the hyper link # @option options @see Hyperlink#initialize All options available to the Hyperlink class apply - however href will be overridden with the v parameter value. @@ -134,23 +133,23 @@ # @note This is a noop if you are using a TwoCellAnchor def height=(v) use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.height = v end - - # This is a short cut method to set the start anchor position + + # 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 @anchor.from end - + # noop if not using a two cell anchor # @param [Integer] x The column # @param [Integer] y The row # @return [Marker] def end_at(x, y) @@ -176,19 +175,19 @@ str << '<a:stretch><a:fillRect/></a:stretch></xdr:blipFill><xdr:spPr>' str << '<a:xfrm><a:off x="0" y="0"/><a:ext cx="2336800" cy="2161540"/></a:xfrm>' str << '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr></xdr:pic>' end - + private # Changes the anchor to a one cell anchor. def use_one_cell_anchor return if @anchor.is_a?(OneCellAnchor) swap_anchor(OneCellAnchor.new(@anchor.drawing, :from => @anchor.from)) end - + #changes the anchor type to a two cell anchor def use_two_cell_anchor return if @anchor.is_a?(TwoCellAnchor) swap_anchor(TwoCellAnchor.new(@anchor.drawing)).tap do |new_anchor| new_anchor.from.col = @anchor.from.col @@ -200,8 +199,7 @@ def swap_anchor(new_anchor) new_anchor.drawing.anchors.delete(new_anchor) @anchor.drawing.anchors[@anchor.drawing.anchors.index(@anchor)] = new_anchor @anchor = new_anchor end - end end