Class: Prawn::Graphics::Cell
- Inherits:
-
Object
- Object
- Prawn::Graphics::Cell
- Defined in:
- lib/prawn/graphics/cell.rb
Overview
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
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#background_color ⇒ Object
Returns the value of attribute background_color.
-
#border_color ⇒ Object
Returns the value of attribute border_color.
-
#border_style ⇒ Object
Returns the value of attribute border_style.
-
#border_width ⇒ Object
Returns the value of attribute border_width.
-
#borders ⇒ Object
writeonly
Sets the attribute borders.
-
#document ⇒ Object
Returns the value of attribute document.
-
#height ⇒ Object
The height of the cell in PDF points.
-
#horizontal_padding ⇒ Object
Returns the value of attribute horizontal_padding.
-
#point ⇒ Object
Returns the value of attribute point.
-
#text_color ⇒ Object
Returns the value of attribute text_color.
-
#vertical_padding ⇒ Object
Returns the value of attribute vertical_padding.
-
#width ⇒ Object
The width of the cell in PDF points.
Instance Method Summary collapse
-
#draw ⇒ Object
Draws the cell onto the PDF document.
-
#initialize(options = {}) ⇒ Cell
constructor
Creates a new cell object.
-
#text_area_height ⇒ Object
The height of the text area excluding the vertical padding.
-
#text_area_width ⇒ Object
The width of the text area excluding the horizonal padding.
-
#to_s ⇒ Object
Returns the cell’s text as a string.
Constructor Details
#initialize(options = {}) ⇒ 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
: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
or:top_and_bottom
. Defaults to :all.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/prawn/graphics/cell.rb', line 54 def initialize(={}) @point = [:point] @document = [:document] @text = [:text].to_s @text_color = [:text_color] @width = [:width] @height = [:height] @borders = [:borders] @border_width = [:border_width] || 1 @border_style = [:border_style] || :all @border_color = [:border_color] @background_color = [:background_color] @align = [:align] || :left @font_size = [:font_size] @horizontal_padding = [:horizontal_padding] || 0 @vertical_padding = [:vertical_padding] || 0 if [:padding] @horizontal_padding = @vertical_padding = [:padding] end end |
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def align @align end |
#background_color ⇒ Object
Returns the value of attribute background_color.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def background_color @background_color end |
#border_color ⇒ Object
Returns the value of attribute border_color.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def border_color @border_color end |
#border_style ⇒ Object
Returns the value of attribute border_style.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def border_style @border_style end |
#border_width ⇒ Object
Returns the value of attribute border_width.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def border_width @border_width end |
#borders=(value) ⇒ Object
Sets the attribute borders
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def borders=(value) @borders = value end |
#document ⇒ Object
Returns the value of attribute document.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def document @document end |
#height ⇒ Object
The height of the cell in PDF points
104 105 106 |
# File 'lib/prawn/graphics/cell.rb', line 104 def height @height || text_area_height + 2*@vertical_padding end |
#horizontal_padding ⇒ Object
Returns the value of attribute horizontal_padding.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def horizontal_padding @horizontal_padding end |
#point ⇒ Object
Returns the value of attribute point.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def point @point end |
#text_color ⇒ Object
Returns the value of attribute text_color.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def text_color @text_color end |
#vertical_padding ⇒ Object
Returns the value of attribute vertical_padding.
77 78 79 |
# File 'lib/prawn/graphics/cell.rb', line 77 def vertical_padding @vertical_padding end |
#width ⇒ Object
The width of the cell in PDF points
97 98 99 100 |
# File 'lib/prawn/graphics/cell.rb', line 97 def width @width || (@document.font.metrics.string_width(@text, @font_size || @document.font.size)) + 2*@horizontal_padding end |
Instance Method Details
#draw ⇒ Object
Draws the cell onto the PDF document
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/prawn/graphics/cell.rb', line 116 def draw rel_point = @point if @background_color @document.mask(:fill_color) do @document.fill_color @background_color h = borders.include?(:bottom) ? height - border_width : height + border_width / 2.0 @document.fill_rectangle [rel_point[0] + border_width / 2.0, rel_point[1] - border_width / 2.0 ], width - border_width, h end end if @border_width > 0 @document.mask(:line_width) do @document.line_width = @border_width @document.mask(:stroke_color) do @document.stroke_color @border_color if @border_color if borders.include?(:left) @document.stroke_line [rel_point[0], rel_point[1] + (@border_width / 2.0)], [rel_point[0], rel_point[1] - height - @border_width / 2.0 ] end if borders.include?(:right) @document.stroke_line( [rel_point[0] + width, rel_point[1] + (@border_width / 2.0)], [rel_point[0] + width, rel_point[1] - height - @border_width / 2.0] ) end if borders.include?(:top) @document.stroke_line( [ rel_point[0] + @border_width / 2.0, rel_point[1] ], [ rel_point[0] - @border_width / 2.0 + width, rel_point[1] ]) end if borders.include?(:bottom) @document.stroke_line [rel_point[0], rel_point[1] - height ], [rel_point[0] + width, rel_point[1] - height] end end end borders end @document.bounding_box( [@point[0] + @horizontal_padding, @point[1] - @vertical_padding], :width => text_area_width, :height => height - @vertical_padding) do @document.move_up @document.font.line_gap = {:align => @align} [:size] = @font_size if @font_size @document.mask(:fill_color) do @document.fill_color @text_color if @text_color @document.text @text, end end end |
#text_area_height ⇒ Object
The height of the text area excluding the vertical padding
110 111 112 |
# File 'lib/prawn/graphics/cell.rb', line 110 def text_area_height @document.font.height_of(@text, :line_width => text_area_width) end |
#text_area_width ⇒ Object
The width of the text area excluding the horizonal padding
91 92 93 |
# File 'lib/prawn/graphics/cell.rb', line 91 def text_area_width width - 2*@horizontal_padding end |
#to_s ⇒ Object
Returns the cell’s text as a string.
85 86 87 |
# File 'lib/prawn/graphics/cell.rb', line 85 def to_s @text end |