public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
Added :fit argument for image()
trym (author)
Wed Oct 29 11:14:57 -0700 2008
commit  dc678c367065fc53e12baad242010bd1352c5422
tree    bfa0829f33caa3789f900274454ec5ee6fabb600
parent  50e53a09409d600d000d1e4f7b45ba9539138fab
...
22
23
24
25
 
 
26
27
28
...
51
52
53
54
 
55
56
57
...
118
119
120
121
122
 
 
123
124
125
...
252
253
254
255
256
257
258
...
267
268
269
 
 
 
 
 
 
 
 
 
 
 
270
271
272
273
274
...
22
23
24
 
25
26
27
28
29
...
52
53
54
 
55
56
57
58
...
119
120
121
 
 
122
123
124
125
126
...
253
254
255
 
256
257
258
...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
 
282
283
284
0
@@ -22,7 +22,8 @@ module Prawn
0
     # <tt>:position</tt>::  One of (:left, :center, :right) or an x-offset
0
     # <tt>:height</tt>:: the height of the image [actual height of the image]
0
     # <tt>:width</tt>:: the width of the image [actual width of the image]
0
-    # <tt>:scale</tt>:: scale the dimensions of the image proportionally   
0
+    # <tt>:scale</tt>:: scale the dimensions of the image proportionally
0
+    # <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [with,height]
0
     # 
0
     #   Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do     
0
     #     pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg" 
0
@@ -51,7 +52,7 @@ module Prawn
0
     # (See also: Prawn::Images::PNG , Prawn::Images::JPG)
0
     # 
0
     def image(file, options={})     
0
-      Prawn.verify_options [:at,:position, :height, :width, :scale], options
0
+      Prawn.verify_options [:at,:position, :height, :width, :scale, :fit], options
0
       
0
       if file.respond_to?(:read)
0
         image_content = file.read
0
@@ -118,8 +119,8 @@ module Prawn
0
         bounds.absolute_right - w
0
       when Numeric
0
         options[:position] + bounds.absolute_left
0
-      end       
0
-      
0
+      end
0
+
0
       return [x,y]
0
     end
0
 
0
@@ -252,7 +253,6 @@ module Prawn
0
     end
0
 
0
     def calc_image_dimensions(info, options)
0
-      # TODO: allow the image to be aligned in a box
0
       w = options[:width] || info.width
0
       h = options[:height] || info.height
0
 
0
@@ -267,8 +267,18 @@ module Prawn
0
       elsif options[:scale] 
0
         w = info.width * options[:scale]
0
         h = info.height * options[:scale]
0
+      elsif options[:fit] 
0
+        bw, bh = options[:fit]
0
+        bp = bw / bh.to_f
0
+        ip = info.width / info.height.to_f
0
+        if ip > bp
0
+          w = bw
0
+          h = bw / ip
0
+        else
0
+          h = bh
0
+          w = bh * ip
0
+        end
0
       end
0
-
0
       [w,h]
0
     end
0
 

Comments