lib/prawn/images.rb in prawn-1.0.0.rc1 vs lib/prawn/images.rb in prawn-1.0.0.rc2

- old
+ new

@@ -12,12 +12,13 @@ module Images # Add the image at filename to the current page. Currently only # JPG and PNG files are supported. # - # NOTE: Prawn is very slow at rendering PNGs with alpha channels. The - # workaround for those who don't mind installing RMagick is to use: + # NOTE: Prawn is very slow at rendering PNGs with alpha channels, and this + # uses a lot of RAM. The workaround for those who don't mind installing + # RMagick is to use: # # http://github.com/amberbit/prawn-fast-png # # Arguments: # <tt>file</tt>:: path to file or an object that responds to #read @@ -30,14 +31,14 @@ # <tt>:width</tt>:: the width of the image [actual width of the image] # <tt>:scale</tt>:: scale the dimensions of the image proportionally # <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [width,height] # # Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do - # pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg" + # pigs = "#{Prawn::DATADIR}/images/pigs.jpg" # image pigs, :at => [50,450], :width => 450 # - # dice = "#{Prawn::BASEDIR}/data/images/dice.png" + # dice = "#{Prawn::DATADIR}/images/dice.png" # image dice, :at => [50, 450], :scale => 0.75 # end # # If only one of :width / :height are provided, the image will be scaled # proportionally. When both are provided, the image will be stretched to @@ -74,10 +75,16 @@ # Builds an info object (Prawn::Images::*) and a PDF reference representing # the given image. Return a pair: [pdf_obj, info]. # def build_image_object(file) + # Rewind if the object we're passed is an IO, so that multiple embeds of + # the same IO object will work + file.rewind if file.respond_to?(:rewind) + # read the file as binary so the size is calculated correctly + file.binmode if file.respond_to?(:binmode) + if file.respond_to?(:read) image_content = file.read else raise ArgumentError, "#{file} not found" unless File.file?(file) image_content = File.binread(file) @@ -137,21 +144,10 @@ private def image_position(w,h,options) options[:position] ||= :left - x = case options[:position] - when :left - bounds.absolute_left - when :center - bounds.absolute_left + (bounds.width - w) / 2.0 - when :right - bounds.absolute_right - w - when Numeric - options[:position] + bounds.absolute_left - end - y = case options[:vposition] when :top bounds.absolute_top when :center bounds.absolute_top - (bounds.height - h) / 2.0 @@ -160,19 +156,29 @@ when Numeric bounds.absolute_top - options[:vposition] else determine_y_with_page_flow(h) end + + x = case options[:position] + when :left + bounds.left_side + when :center + bounds.left_side + (bounds.width - w) / 2.0 + when :right + bounds.right_side - w + when Numeric + options[:position] + bounds.left_side + end + return [x,y] end def determine_y_with_page_flow(h) if overruns_page?(h) - start_new_page - bounds.absolute_top - else - self.y + bounds.move_past_bottom end + self.y end def overruns_page?(h) (self.y - h) < reference_bounds.absolute_bottom end