diff options
| author | Randy Morgan <[email protected]> | 2012-05-30 08:39:35 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-05-30 08:39:35 +0900 |
| commit | 2ec3335c66e42158975a4d3531e12b5560444d3c (patch) | |
| tree | 6ac36605737f3135f824e6e7550da7565a5ea980 /lib/axlsx/drawing/pic.rb | |
| parent | ea66a00e1a6275ce263ba943c45d1e6ff844eb0c (diff) | |
| download | caxlsx-2ec3335c66e42158975a4d3531e12b5560444d3c.tar.gz caxlsx-2ec3335c66e42158975a4d3531e12b5560444d3c.zip | |
enable anchor swapping between one and two cell anchors for drawings
Diffstat (limited to 'lib/axlsx/drawing/pic.rb')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index e7b5bbbb..ea460413 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -90,8 +90,7 @@ module Axlsx # @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 @@ -119,7 +118,7 @@ module Axlsx # @see width def width=(v) - return unless @anchor.is_a?(OneCellAnchor) + use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.width = v end @@ -128,18 +127,17 @@ module Axlsx # @see OneCellAnchor.width # @note this is a noop if you are using a TwoCellAnchor def height - return unless @anchor.is_a?(OneCellAnchor) @anchor.height end # @see height # @note This is a noop if you are using a TwoCellAnchor def height=(v) - return unless @anchor.is_a?(OneCellAnchor) + 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 @@ -156,7 +154,7 @@ module Axlsx # @param [Integer] y The row # @return [Marker] def end_at(x, y) - return unless @anchor.is_a?(TwoCellAnchor) + use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor) @anchor.to.col = x @anchor.to.row = y @anchor.to @@ -180,6 +178,30 @@ module Axlsx 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.pop + @anchor.drawing.anchors[@anchor.drawing.anchors.index(@anchor)] = new_anchor + @anchor = new_anchor + end end end |
