lib/axlsx/drawing/pic.rb in axlsx-1.1.5 vs lib/axlsx/drawing/pic.rb in axlsx-1.1.6

- old
+ new

@@ -88,12 +88,11 @@ # returns the extension of image_src without the preceeding '.' # @return [String] def extname File.extname(image_src).delete('.') unless image_src.nil? - end - + end # The index of this image in the workbooks images collections # @return [Index] def index @anchor.drawing.worksheet.workbook.images.index(self) end @@ -111,40 +110,57 @@ # providing access to the anchor's width attribute # @param [Integer] v # @see OneCellAnchor.width def width + return unless @anchor.is_a?(OneCellAnchor) @anchor.width end # @see width def width=(v) + use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.width = v end # providing access to update the anchor's height attribute # @param [Integer] v # @see OneCellAnchor.width + # @note this is a noop if you are using a TwoCellAnchor def height @anchor.height end # @see height + # @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) + use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor) + @anchor.to.col = x + @anchor.to.row = y + @anchor.to + end # Serializes the object # @param [String] str # @return [String] def to_xml_string(str = '') @@ -159,9 +175,33 @@ str << '<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="rId' << id.to_s << '"/>' 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 + new_anchor.from.row = @anchor.from.row + end + end + + # refactoring of swapping code, law of demeter be damned! + 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