Sha256: 2138290c8d6412773ae3a412d188a7b01e4880e53c5ee29ff5cf801cb0f7b586

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Thinreports
  module Generator
    module PrawnExt
      module CalcImageDimensions
        # Implement :auto_fit option for image size calculation.
        #
        # When the image is larger than the box, the original: fit option does not change
        # the image size. The :auto_fit option changes the image size to fit in the box
        # while maintaining the aspect ratio.
        #
        # Usage:
        #   image '/path/to/image.png', at: [100, 100], auto_fit: [100, 100]
        #
        def calc_image_dimensions(options)
          if options[:auto_fit]
            image_width = options[:width] || width
            image_height = options[:height] || height

            box_width, box_height = options.delete(:auto_fit)

            if image_width > box_width || image_height > box_height
              options[:fit] = [box_width, box_height]
            end
          end
          super(options)
        end
      end
    end
  end
end

Prawn::Images::Image.prepend Thinreports::Generator::PrawnExt::CalcImageDimensions

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinreports-0.10.1 lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb
thinreports-0.10.0 lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb