Object
A cell is a special-purpose bounding box designed to flow text within a bordered area. This is used by Prawn’s Document::Table implementation but can also be used standalone for drawing text boxes via Document#cell
Creates a new cell object. Generally used indirectly via Document#cell
Of the available options listed below, :point, :width, and :text must be provided. If you are not using the Document#cell shortcut, the :document must also be provided.
| :point | Absolute [x,y] coordinate of the top-left corner of the cell. |
| :document | The Prawn::Document object to render on. |
| :text | The text to be flowed within the cell |
| :text_color | The color of the text to be displayed |
| :width | The width in PDF points of the cell. |
| :height | The height in PDF points of the cell. |
| :horizontal_padding | The horizontal padding in PDF points |
| :vertical_padding | The vertical padding in PDF points |
| :padding | Overrides both horizontal and vertical padding |
| :align | One of :left, :right, :center |
| :borders | An array of sides which should have a border. Any of :top, :left, :right, :bottom |
| :border_width | The border line width. Defaults to 1. |
| :border_style | One of :all, :no_top, :no_bottom, :sides, :none, :bottom_only. Defaults to :all. |
| :border_color | The color of the cell border. |
| :font_size | The font size for the cell text. |
| :font_style | The font style for the cell text. |
# File lib/prawn/table/cell.rb, line 57
57: def initialize(options={})
58: @point = options[:point]
59: @document = options[:document]
60: @text = options[:text].to_s
61: @text_color = options[:text_color]
62: @width = options[:width]
63: @height = options[:height]
64: @borders = options[:borders]
65: @border_width = options[:border_width] || 1
66: @border_style = options[:border_style] || :all
67: @border_color = options[:border_color]
68: @background_color = options[:background_color]
69: @align = options[:align] || :left
70: @font_size = options[:font_size]
71: @font_style = options[:font_style]
72:
73: @horizontal_padding = options[:horizontal_padding] || 0
74: @vertical_padding = options[:vertical_padding] || 0
75:
76: if options[:padding]
77: @horizontal_padding = @vertical_padding = options[:padding]
78: end
79:
80: end
Draws the cell onto the PDF document
# File lib/prawn/table/cell.rb, line 128
128: def draw
129: margin = @border_width / 2.0
130:
131: if @background_color
132: old_color = @document.fill_color || "000000"
133: @document.fill_color(@background_color)
134: h = borders.include?(:bottom) ?
135: height - ( 2 * margin ) : height + margin
136: @document.fill_rectangle [x, y ], width, h
137:
138: @document.fill_color(old_color)
139: end
140:
141: if @border_width > 0
142: @document.mask(:line_width) do
143: @document.line_width = @border_width
144:
145: @document.mask(:stroke_color) do
146: @document.stroke_color @border_color if @border_color
147:
148: if borders.include?(:left)
149: @document.stroke_line [x, y + margin],
150: [x, y - height - margin ]
151: end
152:
153: if borders.include?(:right)
154: @document.stroke_line(
155: [x + width, y + margin],
156: [x + width, y - height - margin] )
157: end
158:
159: if borders.include?(:top)
160: @document.stroke_line(
161: [ x, y ],
162: [ x + width, y ])
163: end
164:
165: if borders.include?(:bottom)
166: @document.stroke_line [x, y - height ],
167: [x + width, y - height]
168: end
169: end
170:
171: end
172:
173: borders
174:
175: end
176:
177: @document.bounding_box( [x + @horizontal_padding,
178: y - @vertical_padding],
179: :width => text_area_width,
180: :height => height - @vertical_padding) do
181: @document.move_down((@document.font.line_gap +
182: @document.font.descender) / 2)
183:
184: options = {:align => @align, :final_gap => false}
185:
186: options[:size] = @font_size if @font_size
187: options[:style] = @font_style if @font_style
188:
189: old_color = @document.fill_color || "000000"
190: @document.fill_color @text_color if @text_color
191: @document.text @text, options
192: @document.fill_color old_color
193: end
194: end
The height of the cell in PDF points
# File lib/prawn/table/cell.rb, line 108
108: def height
109: @height || text_area_height + 2*@vertical_padding
110: end
The height of the text area excluding the vertical padding
# File lib/prawn/table/cell.rb, line 114
114: def text_area_height
115: text_height = 0
116: if @font_size
117: @document.font_size(@font_size) do
118: text_height = @document.height_of(@text, :width => text_area_width)
119: end
120: else
121: text_height = @document.height_of(@text, :width => text_area_width)
122: end
123: text_height
124: end
The width of the text area excluding the horizonal padding
# File lib/prawn/table/cell.rb, line 96
96: def text_area_width
97: width - 2*@horizontal_padding
98: end
# File lib/prawn/table/cell.rb, line 208
208: def borders
209: @borders ||= case @border_style
210: when :all
211: [:top,:left,:right,:bottom]
212: when :sides
213: [:left,:right]
214: when :no_top
215: [:left,:right,:bottom]
216: when :no_bottom
217: [:left,:right,:top]
218: when :bottom_only
219: [:bottom]
220: when :none
221: []
222: end
223: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.