summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-05-30 08:39:35 +0900
committerRandy Morgan <[email protected]>2012-05-30 08:39:35 +0900
commit2ec3335c66e42158975a4d3531e12b5560444d3c (patch)
tree6ac36605737f3135f824e6e7550da7565a5ea980
parentea66a00e1a6275ce263ba943c45d1e6ff844eb0c (diff)
downloadcaxlsx-2ec3335c66e42158975a4d3531e12b5560444d3c.tar.gz
caxlsx-2ec3335c66e42158975a4d3531e12b5560444d3c.zip
enable anchor swapping between one and two cell anchors for drawings
-rw-r--r--lib/axlsx/drawing/pic.rb38
-rw-r--r--test/drawing/tc_pic.rb19
2 files changed, 49 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
diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb
index 5fc1f1c3..a892678d 100644
--- a/test/drawing/tc_pic.rb
+++ b/test/drawing/tc_pic.rb
@@ -18,6 +18,25 @@ class TestPic < Test::Unit::TestCase
assert_equal(@image.image_src, @test_img)
end
+ def test_anchor_swapping
+ #swap from one cell to two cell when end_at is specified
+ assert(@image.anchor.is_a?(Axlsx::OneCellAnchor))
+ start_at = @image.anchor.from
+ @image.end_at 10,5
+ assert(@image.anchor.is_a?(Axlsx::TwoCellAnchor))
+ assert_equal(start_at.col, @image.anchor.from.col)
+ assert_equal(start_at.row, @image.anchor.from.row)
+ assert_equal(10,@image.anchor.to.col)
+ assert_equal(5, @image.anchor.to.row)
+
+ #swap from two cell to one cell when width or height are specified
+ @image.width = 200
+ assert(@image.anchor.is_a?(Axlsx::OneCellAnchor))
+ assert_equal(start_at.col, @image.anchor.from.col)
+ assert_equal(start_at.row, @image.anchor.from.row)
+ assert_equal(200, @image.width)
+
+ end
def test_hyperlink
assert_equal(@image.hyperlink.href, "https://github.com/randym")
@image.hyperlink = "http://axlsx.blogspot.com"