lib/prawn/images.rb in prawn-2.2.2 vs lib/prawn/images.rb in prawn-2.3.0

- old
+ new

@@ -1,6 +1,9 @@ # encoding: ASCII-8BIT + +# frozen_string_literal: true + # images.rb : Implements PDF image embedding # # Copyright April 2008, James Healy, Gregory Brown. All Rights Reserved. # # This is free software. Please see the LICENSE and COPYING files for details. @@ -61,13 +64,13 @@ # This method returns an image info object which can be used to check the # dimensions of an image object if needed. # (See also: Prawn::Images::PNG , Prawn::Images::JPG) # def image(file, options = {}) - Prawn.verify_options [ - :at, :position, :vposition, :height, - :width, :scale, :fit + Prawn.verify_options %i[ + at position vposition height + width scale fit ], options pdf_obj, info = build_image_object(file) embed_image(pdf_obj, info, options) @@ -77,12 +80,11 @@ # Builds an info object (Prawn::Images::*) and a PDF reference representing # the given image. Return a pair: [pdf_obj, info]. # # @private def build_image_object(file) - io = verify_and_open_image(file) - image_content = io.read + image_content = verify_and_read_image(file) image_sha1 = Digest::SHA1.hexdigest(image_content) # if this image has already been embedded, just reuse it if image_registry[image_sha1] info = image_registry[image_sha1][:info] @@ -130,67 +132,67 @@ renderer.add_content("\nq\n#{cm_params} cm\n/#{label} Do\nQ") end private - def verify_and_open_image(io_or_path) + def verify_and_read_image(io_or_path) # File or IO if io_or_path.respond_to?(:rewind) io = io_or_path # Rewind if the object we're passed is an IO, so that multiple embeds of # the same IO object will work io.rewind # read the file as binary so the size is calculated correctly # guard binmode because some objects acting io-like don't implement it io.binmode if io.respond_to?(:binmode) - return io + return io.read end # String or Pathname io_or_path = Pathname.new(io_or_path) raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file? - io = io_or_path.open('rb') - io + + io_or_path.binread end - def image_position(w, h, options) + def image_position(width, height, options) options[:position] ||= :left y = case options[:vposition] when :top bounds.absolute_top when :center - bounds.absolute_top - (bounds.height - h) / 2.0 + bounds.absolute_top - (bounds.height - height) / 2.0 when :bottom - bounds.absolute_bottom + h + bounds.absolute_bottom + height when Numeric bounds.absolute_top - options[:vposition] else - determine_y_with_page_flow(h) + determine_y_with_page_flow(height) end x = case options[:position] when :left bounds.left_side when :center - bounds.left_side + (bounds.width - w) / 2.0 + bounds.left_side + (bounds.width - width) / 2.0 when :right - bounds.right_side - w + bounds.right_side - width when Numeric options[:position] + bounds.left_side end [x, y] end - def determine_y_with_page_flow(h) - if overruns_page?(h) + def determine_y_with_page_flow(height) + if overruns_page?(height) bounds.move_past_bottom end y end - def overruns_page?(h) - (y - h) < reference_bounds.absolute_bottom + def overruns_page?(height) + (y - height) < reference_bounds.absolute_bottom end def image_registry @image_registry ||= {} end