public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
Draw backgrounds by cell
sandal (author)
Thu Jul 24 13:42:56 -0700 2008
commit  01215f4107607a2431361b5df55221a60c204e8e
tree    d59b8dfcffd619aebb096eb29bf46d2977bd6425
parent  3867937e578440e363e0f67f927adf006228450a
...
7
8
9
10
 
11
12
13
...
41
42
43
44
 
45
46
47
...
7
8
9
 
10
11
12
13
...
41
42
43
 
44
45
46
47
0
@@ -7,7 +7,7 @@
0
 # Because this almost appears to be a feature display-wise, we will leave it 
0
 # alone for now.
0
 
0
-$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
0
+$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', 'lib')
0
 require "prawn"
0
 
0
 Prawn::Document.generate("table_with_background_color_problems.pdf") do 
0
@@ -41,7 +41,7 @@ Prawn::Document.generate("table_with_background_color_problems.pdf") do
0
     :border     => 1,
0
     :position   => :center,
0
     :headers    => ["Column A","Column B","#"],
0
-    :row_colors => ["eeeeee"]
0
+    :row_colors => ["cccccc"]
0
 
0
   pad(20) do
0
     text "This should appear in the original font size"
...
48
49
50
51
 
 
52
53
54
...
58
59
60
61
 
62
63
64
...
96
97
98
 
 
 
 
 
 
 
 
 
 
99
100
101
...
166
167
168
169
 
170
171
172
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 
208
209
210
...
48
49
50
 
51
52
53
54
55
...
59
60
61
 
62
63
64
65
...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
...
177
178
179
 
180
181
182
183
...
186
187
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
190
191
192
193
194
195
196
0
@@ -48,7 +48,8 @@ module Prawn
0
         @text         = options[:text]
0
         @width        = options[:width]
0
         @border       = options[:border]
0
-        @border_style = options[:border_style] || :all
0
+        @border_style = options[:border_style] || :all               
0
+        @background_color = options[:background_color]
0
 
0
         @horizontal_padding = options[:horizontal_padding] || 0
0
         @vertical_padding   = options[:vertical_padding]   || 0
0
@@ -58,7 +59,7 @@ module Prawn
0
         end
0
       end
0
 
0
-      attr_accessor :point, :border_style, :border
0
+      attr_accessor :point, :border_style, :border, :background_color
0
       attr_writer   :height #:nodoc:
0
 
0
       # The width of the text area excluding the horizonal padding
0
@@ -96,6 +97,16 @@ module Prawn
0
         if @border
0
           @document.mask(:line_width) do
0
             @document.line_width = @border
0
+            
0
+            if @background_color    
0
+              @document.mask(:fill_color) do
0
+                @document.fill_color @background_color  
0
+                h  = borders.include?(:bottom) ? height - border : height + border / 2.0
0
+                @document.fill_rectangle [rel_point[0] + border / 2.0, 
0
+                                          rel_point[1] - border / 2.0 ], 
0
+                    width - border, h  
0
+              end
0
+            end 
0
 
0
             if borders.include?(:left)
0
               @document.stroke_line [rel_point[0], rel_point[1] + (@border / 2.0)], 
0
@@ -166,7 +177,7 @@ module Prawn
0
 
0
       def <<(cell)
0
         @cells << cell
0
-        @height = cell.height if cell.height > @height
0
+        @height = cell.height if cell.height > @height 
0
         @width += cell.width
0
         self
0
       end
0
@@ -175,36 +186,11 @@ module Prawn
0
         y = @document.y
0
         x = @document.bounds.absolute_left
0
 
0
-        # TODO: This is a bit of a hack and can be cleaned up
0
-        if @background_color
0
-
0
-          @document.mask(:fill_color, :stroke_color) do
0
-            @document.fill_color = @background_color
0
-            @document.stroke_color = @background_color
0
-
0
-            @document.canvas do
0
-              case border_style
0
-              when :all
0
-                point = [x,y-border]
0
-                h = height - border
0
-              when :no_top
0
-                point = [x,y-border/2.0]  
0
-                h     = height - border / 2.0
0
-              else
0
-                point = [x,y-border/2.0]
0
-                h     = height
0
-             end
0
-                    
0
-              @document.fill_and_stroke_rectangle point, width, h
0
-            end
0
-          end   
0
-
0
-        end
0
-
0
         @cells.each do |e|
0
           e.point  = [x - @document.bounds.absolute_left, 
0
                       y - @document.bounds.absolute_bottom]
0
           e.height = @height
0
+          e.background_color = @background_color
0
           e.draw
0
           x += e.width
0
         end

Comments