lib/axlsx/drawing/pic.rb in axlsx-1.3.6 vs lib/axlsx/drawing/pic.rb in axlsx-2.0.0

- old
+ new

@@ -35,11 +35,11 @@ # A description of the picture # @return [String] attr_reader :descr # The path to the image you want to include - # Only local images are supported at this time and only jpg support + # Only local images are supported at this time. # @return [String] attr_reader :image_src # The anchor for this image # @return [OneCellAnchor] @@ -65,11 +65,11 @@ @hyperlink end def image_src=(v) Axlsx::validate_string(v) - RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v).delete('.') + RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v.downcase).delete('.') raise ArgumentError, "File does not exist" unless File.exist?(v) @image_src = v end # @see name @@ -87,11 +87,12 @@ # 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 @@ -100,19 +101,14 @@ # @return [String] def pn "#{IMAGE_PN % [(index+1), extname]}" end - # The relational id withing the drawing's relationships - def id - @anchor.drawing.charts.size + @anchor.drawing.images.index(self) + 1 - end - - # Returns a relationship object for this object - # @return Axlsx::Relationship + # The relationship object for this pic. + # @return [Relationship] def relationship - Relationship.new(IMAGE_R, "../#{pn}") + Relationship.new(self, IMAGE_R, "../#{pn}") end # providing access to the anchor's width attribute # @param [Integer] v # @see OneCellAnchor.width @@ -146,24 +142,22 @@ # 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 + def start_at(x, y=nil) + @anchor.start_at x, 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) + def end_at(x, y=nil) use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor) - @anchor.to.col = x - @anchor.to.row = y + @anchor.end_at x, y @anchor.to end # Serializes the object # @param [String] str @@ -175,11 +169,11 @@ @hyperlink.to_xml_string(str) if @hyperlink.is_a?(Hyperlink) str << '</xdr:cNvPr><xdr:cNvPicPr>' picture_locking.to_xml_string(str) str << '</xdr:cNvPicPr></xdr:nvPicPr>' str << '<xdr:blipFill>' - str << '<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="rId' << id.to_s << '"/>' + str << '<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '"/>' 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 @@ -187,25 +181,25 @@ 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)) + new_anchor = OneCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row]) + swap_anchor(new_anchor) 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 + new_anchor = TwoCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row]) + swap_anchor(new_anchor) 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 + new_anchor.instance_variable_set "@object", @anchor.object @anchor = new_anchor end end end