davebenvenuti-prawn 0.11.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +50 -0
- data/LICENSE +56 -0
- data/README +141 -0
- data/Rakefile +52 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/examples/bounding_box/bounding_boxes.rb +44 -0
- data/examples/bounding_box/indentation.rb +35 -0
- data/examples/bounding_box/russian_boxes.rb +37 -0
- data/examples/bounding_box/stretched_nesting.rb +68 -0
- data/examples/example_helper.rb +8 -0
- data/examples/general/background.rb +24 -0
- data/examples/general/canvas.rb +16 -0
- data/examples/general/context_sensitive_headers.rb +38 -0
- data/examples/general/float.rb +12 -0
- data/examples/general/margin.rb +37 -0
- data/examples/general/measurement_units.rb +52 -0
- data/examples/general/metadata-info.rb +17 -0
- data/examples/general/multi_page_layout.rb +19 -0
- data/examples/general/outlines.rb +67 -0
- data/examples/general/page_geometry.rb +32 -0
- data/examples/general/page_numbering.rb +16 -0
- data/examples/general/repeaters.rb +48 -0
- data/examples/general/stamp.rb +42 -0
- data/examples/general/templates.rb +14 -0
- data/examples/graphics/basic_images.rb +24 -0
- data/examples/graphics/cmyk.rb +13 -0
- data/examples/graphics/curves.rb +12 -0
- data/examples/graphics/hexagon.rb +14 -0
- data/examples/graphics/image_fit.rb +16 -0
- data/examples/graphics/image_flow.rb +38 -0
- data/examples/graphics/image_position.rb +18 -0
- data/examples/graphics/line.rb +33 -0
- data/examples/graphics/png_types.rb +23 -0
- data/examples/graphics/polygons.rb +17 -0
- data/examples/graphics/remote_images.rb +13 -0
- data/examples/graphics/rounded_polygons.rb +20 -0
- data/examples/graphics/rounded_rectangle.rb +21 -0
- data/examples/graphics/ruport_style_helpers.rb +20 -0
- data/examples/graphics/stroke_bounds.rb +21 -0
- data/examples/graphics/stroke_cap_and_join.rb +46 -0
- data/examples/graphics/stroke_dash.rb +43 -0
- data/examples/graphics/transformations.rb +53 -0
- data/examples/graphics/transparency.rb +27 -0
- data/examples/grid/bounding_boxes.rb +22 -0
- data/examples/grid/column_gutter_grid.rb +21 -0
- data/examples/grid/multi_boxes.rb +52 -0
- data/examples/grid/show_grid.rb +14 -0
- data/examples/grid/simple_grid.rb +21 -0
- data/examples/m17n/chinese_text_wrapping.rb +18 -0
- data/examples/m17n/euro.rb +16 -0
- data/examples/m17n/sjis.rb +29 -0
- data/examples/m17n/utf8.rb +14 -0
- data/examples/m17n/win_ansi_charset.rb +55 -0
- data/examples/security/hello_foo.rb +9 -0
- data/examples/table/bill.rb +54 -0
- data/examples/table/cell.rb +13 -0
- data/examples/table/checkerboard.rb +23 -0
- data/examples/table/header.rb +15 -0
- data/examples/table/inline_format_table.rb +13 -0
- data/examples/table/multi_page_table.rb +10 -0
- data/examples/table/simple_table.rb +25 -0
- data/examples/table/subtable.rb +13 -0
- data/examples/table/widths.rb +21 -0
- data/examples/text/alignment.rb +19 -0
- data/examples/text/character_spacing.rb +13 -0
- data/examples/text/dfont.rb +49 -0
- data/examples/text/family_based_styling.rb +25 -0
- data/examples/text/font_calculations.rb +92 -0
- data/examples/text/font_size.rb +34 -0
- data/examples/text/hyphenation.rb +46 -0
- data/examples/text/indent_paragraphs.rb +23 -0
- data/examples/text/inline_format.rb +104 -0
- data/examples/text/kerning.rb +31 -0
- data/examples/text/rotated.rb +99 -0
- data/examples/text/shaped_text_box.rb +32 -0
- data/examples/text/simple_text.rb +18 -0
- data/examples/text/simple_text_ttf.rb +18 -0
- data/examples/text/span.rb +30 -0
- data/examples/text/text_box.rb +89 -0
- data/examples/text/text_box_returning_excess.rb +52 -0
- data/examples/text/text_flow.rb +68 -0
- data/lib/prawn.rb +26 -0
- data/lib/prawn/compatibility.rb +51 -0
- data/lib/prawn/core.rb +85 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +78 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +165 -0
- data/lib/prawn/core/object_store.rb +236 -0
- data/lib/prawn/core/page.rb +199 -0
- data/lib/prawn/core/pdf_object.rb +108 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text.rb +140 -0
- data/lib/prawn/core/text/formatted/arranger.rb +266 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +112 -0
- data/lib/prawn/core/text/line_wrap.rb +211 -0
- data/lib/prawn/core/text/wrap.rb +82 -0
- data/lib/prawn/document.rb +575 -0
- data/lib/prawn/document/bounding_box.rb +428 -0
- data/lib/prawn/document/graphics_state.rb +48 -0
- data/lib/prawn/document/internals.rb +170 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +86 -0
- data/lib/prawn/font.rb +368 -0
- data/lib/prawn/font/afm.rb +225 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +350 -0
- data/lib/prawn/graphics.rb +325 -0
- data/lib/prawn/graphics/cap_style.rb +38 -0
- data/lib/prawn/graphics/color.rb +205 -0
- data/lib/prawn/graphics/dash.rb +71 -0
- data/lib/prawn/graphics/join_style.rb +38 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +217 -0
- data/lib/prawn/images/jpg.rb +85 -0
- data/lib/prawn/images/png.rb +356 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +129 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table.rb +451 -0
- data/lib/prawn/table/cell.rb +395 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +135 -0
- data/lib/prawn/table/cells.rb +206 -0
- data/lib/prawn/text.rb +449 -0
- data/lib/prawn/text/box.rb +397 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +222 -0
- data/lib/prawn/text/formatted/fragment.rb +181 -0
- data/lib/prawn/text/formatted/parser.rb +213 -0
- data/prawn.gemspec +28 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +430 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +476 -0
- data/spec/extensions/mocha.rb +32 -0
- data/spec/font_spec.rb +324 -0
- data/spec/formatted_text_arranger_spec.rb +426 -0
- data/spec/formatted_text_box_spec.rb +756 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +119 -0
- data/spec/inline_formatted_text_parser_spec.rb +502 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +341 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +160 -0
- data/spec/outline_spec.rb +404 -0
- data/spec/pdf_object_spec.rb +170 -0
- data/spec/png_spec.rb +237 -0
- data/spec/reference_spec.rb +82 -0
- data/spec/repeater_spec.rb +96 -0
- data/spec/security_spec.rb +120 -0
- data/spec/snapshot_spec.rb +154 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/stamp_spec.rb +108 -0
- data/spec/stroke_styles_spec.rb +163 -0
- data/spec/table_spec.rb +687 -0
- data/spec/template_spec.rb +165 -0
- data/spec/text_at_spec.rb +125 -0
- data/spec/text_box_spec.rb +777 -0
- data/spec/text_spacing_spec.rb +75 -0
- data/spec/text_spec.rb +349 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +89 -0
- data/vendor/pdf-inspector/README +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
- data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
- data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
- data/vendor/ttfunk/example.rb +45 -0
- data/vendor/ttfunk/lib/ttfunk.rb +102 -0
- data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
- data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
- data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
- data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
- data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
- data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
- data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
- data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
- data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
- data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
- data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
- metadata +379 -0
data/lib/prawn/table.rb
ADDED
@@ -0,0 +1,451 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# table.rb: Table drawing functionality.
|
4
|
+
#
|
5
|
+
# Copyright December 2009, Brad Ediger. All rights reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'prawn/table/cells'
|
10
|
+
require 'prawn/table/cell'
|
11
|
+
require 'prawn/table/cell/in_table'
|
12
|
+
require 'prawn/table/cell/text'
|
13
|
+
require 'prawn/table/cell/subtable'
|
14
|
+
|
15
|
+
module Prawn
|
16
|
+
|
17
|
+
class Document
|
18
|
+
|
19
|
+
# Set up and draw a table on this document. A block can be given, which will
|
20
|
+
# be run after cell setup but before layout and drawing.
|
21
|
+
#
|
22
|
+
# See the documentation on Prawn::Table for details on the arguments.
|
23
|
+
#
|
24
|
+
def table(data, options={}, &block)
|
25
|
+
t = Table.new(data, self, options, &block)
|
26
|
+
t.draw
|
27
|
+
t
|
28
|
+
end
|
29
|
+
|
30
|
+
# Set up, but do not draw, a table. Useful for creating subtables to be
|
31
|
+
# inserted into another Table. Call +draw+ on the resulting Table to ink it.
|
32
|
+
#
|
33
|
+
# See the documentation on Prawn::Table for details on the arguments.
|
34
|
+
#
|
35
|
+
def make_table(data, options={}, &block)
|
36
|
+
Table.new(data, self, options, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Next-generation table drawing for Prawn.
|
42
|
+
#
|
43
|
+
# = Data
|
44
|
+
#
|
45
|
+
# Data, for a Prawn table, is a two-dimensional array of objects that can be
|
46
|
+
# converted to cells ("cellable" objects). Cellable objects can be:
|
47
|
+
#
|
48
|
+
# String::
|
49
|
+
# Produces a text cell. This is the most common usage.
|
50
|
+
# Prawn::Table::Cell::
|
51
|
+
# If you have already built a Cell or have a custom subclass of Cell you
|
52
|
+
# want to use in a table, you can pass through Cell objects.
|
53
|
+
# Prawn::Table::
|
54
|
+
# Creates a subtable (a table within a cell). You can use
|
55
|
+
# Prawn::Document#make_table to create a table for use as a subtable
|
56
|
+
# without immediately drawing it. See examples/table/bill.rb for a
|
57
|
+
# somewhat complex use of subtables.
|
58
|
+
# Array::
|
59
|
+
# Creates a simple subtable. Create a Table object using make_table (see
|
60
|
+
# above) if you need more control over the subtable's styling.
|
61
|
+
#
|
62
|
+
# = Options
|
63
|
+
#
|
64
|
+
# Prawn/Layout provides many options to control style and layout of your
|
65
|
+
# table. These options are implemented with a uniform interface: the +:foo+
|
66
|
+
# option always sets the +foo=+ accessor. See the accessor and method
|
67
|
+
# documentation for full details on the options you can pass. Some
|
68
|
+
# highlights:
|
69
|
+
#
|
70
|
+
# +cell_style+::
|
71
|
+
# A hash of style options to style all cells. See the documentation on
|
72
|
+
# Prawn::Table::Cell for all cell style options.
|
73
|
+
# +header+::
|
74
|
+
# If set to +true+, the first row will be repeated on every page. The
|
75
|
+
# header must be included as the first row of your data. Row numbering
|
76
|
+
# (for styling and other row-specific options) always indexes based on
|
77
|
+
# your data array. Whether or not you have a header, row(n) always refers
|
78
|
+
# to the nth element (starting from 0) of the +data+ array.
|
79
|
+
# +column_widths+::
|
80
|
+
# Sets widths for individual columns. Manually setting widths can give
|
81
|
+
# better results than letting Prawn guess at them, as Prawn's algorithm
|
82
|
+
# for defaulting widths is currently pretty boneheaded. If you experience
|
83
|
+
# problems like weird column widths or CannotFit errors, try manually
|
84
|
+
# setting widths on more columns.
|
85
|
+
#
|
86
|
+
# = Initializer Block
|
87
|
+
#
|
88
|
+
# If a block is passed to methods that initialize a table
|
89
|
+
# (Prawn::Table.new, Prawn::Document#table, Prawn::Document#make_table), it
|
90
|
+
# will be called after cell setup but before layout. This is a very flexible
|
91
|
+
# way to specify styling and layout constraints. This code sets up a table
|
92
|
+
# where the second through the fourth rows (1-3, indexed from 0) are each one
|
93
|
+
# inch (72 pt) wide:
|
94
|
+
#
|
95
|
+
# pdf.table(data) do |table|
|
96
|
+
# table.rows(1..3).width = 72
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# As with Prawn::Document#initialize, if the block has no arguments, it will
|
100
|
+
# be evaluated in the context of the object itself. The above code could be
|
101
|
+
# rewritten as:
|
102
|
+
#
|
103
|
+
# pdf.table(data) do
|
104
|
+
# rows(1..3).width = 72
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
class Table
|
108
|
+
|
109
|
+
# Set up a table on the given document. Arguments:
|
110
|
+
#
|
111
|
+
# +data+::
|
112
|
+
# A two-dimensional array of cell-like objects. See the "Data" section
|
113
|
+
# above for the types of objects that can be put in a table.
|
114
|
+
# +document+::
|
115
|
+
# The Prawn::Document instance on which to draw the table.
|
116
|
+
# +options+::
|
117
|
+
# A hash of attributes and values for the table. See the "Options" block
|
118
|
+
# above for details on available options.
|
119
|
+
#
|
120
|
+
def initialize(data, document, options={}, &block)
|
121
|
+
@pdf = document
|
122
|
+
@cells = make_cells(data)
|
123
|
+
@header = false
|
124
|
+
options.each { |k, v| send("#{k}=", v) }
|
125
|
+
|
126
|
+
if block
|
127
|
+
block.arity < 1 ? instance_eval(&block) : block[self]
|
128
|
+
end
|
129
|
+
|
130
|
+
set_column_widths
|
131
|
+
set_row_heights
|
132
|
+
position_cells
|
133
|
+
end
|
134
|
+
|
135
|
+
# Number of rows in the table.
|
136
|
+
#
|
137
|
+
attr_reader :row_length
|
138
|
+
|
139
|
+
# Number of columns in the table.
|
140
|
+
#
|
141
|
+
attr_reader :column_length
|
142
|
+
|
143
|
+
# Manually set the width of the table.
|
144
|
+
#
|
145
|
+
attr_writer :width
|
146
|
+
|
147
|
+
# Returns the width of the table in PDF points.
|
148
|
+
#
|
149
|
+
def width
|
150
|
+
@width ||= [natural_width, @pdf.bounds.width].min
|
151
|
+
end
|
152
|
+
|
153
|
+
# Sets column widths for the table. The argument can be one of the following
|
154
|
+
# types:
|
155
|
+
#
|
156
|
+
# +Array+::
|
157
|
+
# <tt>[w0, w1, w2, ...]</tt> (specify a width for each column)
|
158
|
+
# +Hash+::
|
159
|
+
# <tt>{0 => w0, 1 => w1, ...}</tt> (keys are column names, values are
|
160
|
+
# widths)
|
161
|
+
# +Numeric+::
|
162
|
+
# +72+ (sets width for all columns)
|
163
|
+
#
|
164
|
+
def column_widths=(widths)
|
165
|
+
case widths
|
166
|
+
when Array
|
167
|
+
widths.each_with_index { |w, i| column(i).width = w }
|
168
|
+
when Hash
|
169
|
+
widths.each { |i, w| column(i).width = w }
|
170
|
+
when Numeric
|
171
|
+
columns.width = widths
|
172
|
+
else
|
173
|
+
raise ArgumentError, "cannot interpret column widths"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# Returns the height of the table in PDF points.
|
178
|
+
#
|
179
|
+
def height
|
180
|
+
cells.height
|
181
|
+
end
|
182
|
+
|
183
|
+
# If +true+, designates the first row as a header row to be repeated on
|
184
|
+
# every page. Does not change row numbering -- row numbers always index into
|
185
|
+
# the data array provided, with no modification.
|
186
|
+
#
|
187
|
+
attr_writer :header
|
188
|
+
|
189
|
+
# Accepts an Array of alternating row colors to stripe the table.
|
190
|
+
#
|
191
|
+
attr_writer :row_colors
|
192
|
+
|
193
|
+
# Sets styles for all cells.
|
194
|
+
#
|
195
|
+
# pdf.table(data, :cell_style => { :borders => [:left, :right] })
|
196
|
+
#
|
197
|
+
def cell_style=(style_hash)
|
198
|
+
cells.style(style_hash)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Allows generic stylable content. This is an alternate syntax that some
|
202
|
+
# prefer to the attribute-based syntax. This code using style:
|
203
|
+
#
|
204
|
+
# pdf.table(data) do
|
205
|
+
# style(row(0), :background_color => 'ff00ff')
|
206
|
+
# style(column(0)) { |c| c.border_width += 1 }
|
207
|
+
# end
|
208
|
+
#
|
209
|
+
# is equivalent to:
|
210
|
+
#
|
211
|
+
# pdf.table(data) do
|
212
|
+
# row(0).style :background_color => 'ff00ff'
|
213
|
+
# column(0).style { |c| c.border_width += 1 }
|
214
|
+
# end
|
215
|
+
#
|
216
|
+
def style(stylable, style_hash={}, &block)
|
217
|
+
stylable.style(style_hash, &block)
|
218
|
+
end
|
219
|
+
|
220
|
+
# Draws the table onto the document at the document's current y-position.
|
221
|
+
#
|
222
|
+
def draw
|
223
|
+
# The cell y-positions are based on an infinitely long canvas. The offset
|
224
|
+
# keeps track of how much we have to add to the original, theoretical
|
225
|
+
# y-position to get to the actual position on the current page.
|
226
|
+
offset = @pdf.y
|
227
|
+
|
228
|
+
# Reference bounds are the non-stretchy bounds used to decide when to
|
229
|
+
# flow to a new column / page.
|
230
|
+
ref_bounds = @pdf.bounds.stretchy? ? @pdf.margin_box : @pdf.bounds
|
231
|
+
|
232
|
+
last_y = @pdf.y
|
233
|
+
|
234
|
+
# Determine whether we're at the top of the current bounds (margin box or
|
235
|
+
# bounding box). If we're at the top, we couldn't gain any more room by
|
236
|
+
# breaking to the next page -- this means, in particular, that if the
|
237
|
+
# first row is taller than the margin box, we will only move to the next
|
238
|
+
# page if we're below the top. Some floating-point tolerance is added to
|
239
|
+
# the calculation.
|
240
|
+
#
|
241
|
+
# Note that we use the actual bounds, not the reference bounds. This is
|
242
|
+
# because even if we are in a stretchy bounding box, flowing to the next
|
243
|
+
# page will not buy us any space if we are at the top.
|
244
|
+
if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001
|
245
|
+
# we're at the top of our bounds
|
246
|
+
started_new_page_at_row = 0
|
247
|
+
else
|
248
|
+
started_new_page_at_row = -1
|
249
|
+
|
250
|
+
# If there isn't enough room left on the page to fit the first data row
|
251
|
+
# (excluding the header), start the table on the next page.
|
252
|
+
needed_height = row(0).height
|
253
|
+
needed_height += row(1).height if @header
|
254
|
+
if needed_height > @pdf.y - ref_bounds.absolute_bottom
|
255
|
+
@pdf.bounds.move_past_bottom
|
256
|
+
offset = @pdf.y
|
257
|
+
started_new_page_at_row = 0
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
@cells.each do |cell|
|
262
|
+
if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom &&
|
263
|
+
cell.row > started_new_page_at_row
|
264
|
+
# start a new page or column
|
265
|
+
@pdf.bounds.move_past_bottom
|
266
|
+
draw_header unless cell.row == 0
|
267
|
+
offset = @pdf.y - cell.y
|
268
|
+
started_new_page_at_row = cell.row
|
269
|
+
end
|
270
|
+
|
271
|
+
# Don't modify cell.x / cell.y here, as we want to reuse the original
|
272
|
+
# values when re-inking the table. #draw should be able to be called
|
273
|
+
# multiple times.
|
274
|
+
x, y = cell.x, cell.y
|
275
|
+
y += offset
|
276
|
+
|
277
|
+
# Translate coordinates to the bounds we are in, since drawing is
|
278
|
+
# relative to the cursor, not ref_bounds.
|
279
|
+
x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
|
280
|
+
y -= @pdf.bounds.absolute_bottom
|
281
|
+
|
282
|
+
# Set background color, if any.
|
283
|
+
if @row_colors && (!@header || cell.row > 0)
|
284
|
+
index = @header ? (cell.row - 1) : cell.row
|
285
|
+
cell.background_color = @row_colors[index % @row_colors.length]
|
286
|
+
end
|
287
|
+
|
288
|
+
cell.draw([x, y])
|
289
|
+
last_y = y
|
290
|
+
end
|
291
|
+
|
292
|
+
@pdf.move_cursor_to(last_y - @cells.last.height)
|
293
|
+
end
|
294
|
+
|
295
|
+
# Calculate and return the constrained column widths, taking into account
|
296
|
+
# each cell's min_width, max_width, and any user-specified constraints on
|
297
|
+
# the table or column size.
|
298
|
+
#
|
299
|
+
# Because the natural widths can be silly, this does not always work so well
|
300
|
+
# at guessing a good size for columns that have vastly different content. If
|
301
|
+
# you see weird problems like CannotFit errors or shockingly bad column
|
302
|
+
# sizes, you should specify more column widths manually.
|
303
|
+
#
|
304
|
+
def column_widths
|
305
|
+
@column_widths ||= begin
|
306
|
+
if width < cells.min_width
|
307
|
+
raise Errors::CannotFit,
|
308
|
+
"Table's width was set too small to contain its contents"
|
309
|
+
end
|
310
|
+
|
311
|
+
if width > cells.max_width
|
312
|
+
raise Errors::CannotFit,
|
313
|
+
"Table's width was set larger than its contents' maximum width"
|
314
|
+
end
|
315
|
+
|
316
|
+
if width < natural_width
|
317
|
+
# Shrink the table to fit the requested width.
|
318
|
+
f = (width - cells.min_width).to_f / (natural_width - cells.min_width)
|
319
|
+
|
320
|
+
(0...column_length).map do |c|
|
321
|
+
min, nat = column(c).min_width, column(c).width
|
322
|
+
(f * (nat - min)) + min
|
323
|
+
end
|
324
|
+
elsif width > natural_width
|
325
|
+
# Expand the table to fit the requested width.
|
326
|
+
f = (width - cells.width).to_f / (cells.max_width - cells.width)
|
327
|
+
|
328
|
+
(0...column_length).map do |c|
|
329
|
+
nat, max = column(c).width, column(c).max_width
|
330
|
+
(f * (max - nat)) + nat
|
331
|
+
end
|
332
|
+
else
|
333
|
+
natural_column_widths
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
# Returns an array with the height of each row.
|
339
|
+
#
|
340
|
+
def row_heights
|
341
|
+
@natural_row_heights ||= (0...row_length).map{ |r| row(r).height }
|
342
|
+
end
|
343
|
+
|
344
|
+
protected
|
345
|
+
|
346
|
+
# Converts the array of cellable objects given into instances of
|
347
|
+
# Prawn::Table::Cell, and sets up their in-table properties so that they
|
348
|
+
# know their own position in the table.
|
349
|
+
#
|
350
|
+
def make_cells(data)
|
351
|
+
assert_proper_table_data(data)
|
352
|
+
|
353
|
+
cells = []
|
354
|
+
|
355
|
+
@row_length = data.length
|
356
|
+
@column_length = data.map{ |r| r.length }.max
|
357
|
+
|
358
|
+
data.each_with_index do |row_cells, row_number|
|
359
|
+
row_cells.each_with_index do |cell_data, column_number|
|
360
|
+
cell = Cell.make(@pdf, cell_data)
|
361
|
+
cell.extend(Cell::InTable)
|
362
|
+
cell.row = row_number
|
363
|
+
cell.column = column_number
|
364
|
+
cells << cell
|
365
|
+
end
|
366
|
+
end
|
367
|
+
cells
|
368
|
+
end
|
369
|
+
|
370
|
+
# Raises an error if the data provided cannot be converted into a valid
|
371
|
+
# table.
|
372
|
+
#
|
373
|
+
def assert_proper_table_data(data)
|
374
|
+
if data.nil? || data.empty?
|
375
|
+
raise Prawn::Errors::EmptyTable,
|
376
|
+
"data must be a non-empty, non-nil, two dimensional array " +
|
377
|
+
"of cell-convertible objects"
|
378
|
+
end
|
379
|
+
|
380
|
+
unless data.all? { |e| Array === e }
|
381
|
+
raise Prawn::Errors::InvalidTableData,
|
382
|
+
"data must be a two dimensional array of cellable objects"
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
# If the table has a header, draw it at the current position.
|
387
|
+
#
|
388
|
+
def draw_header
|
389
|
+
if @header
|
390
|
+
y = @pdf.cursor
|
391
|
+
row(0).each do |cell|
|
392
|
+
cell.y = y
|
393
|
+
cell.draw
|
394
|
+
end
|
395
|
+
@pdf.move_cursor_to(y - row(0).height)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
# Returns an array of each column's natural (unconstrained) width.
|
400
|
+
#
|
401
|
+
def natural_column_widths
|
402
|
+
@natural_column_widths ||= (0...column_length).map { |c| column(c).width }
|
403
|
+
end
|
404
|
+
|
405
|
+
# Returns the "natural" (unconstrained) width of the table. This may be
|
406
|
+
# extremely silly; for example, the unconstrained width of a paragraph of
|
407
|
+
# text is the width it would assume if it were not wrapped at all. Could be
|
408
|
+
# a mile long.
|
409
|
+
#
|
410
|
+
def natural_width
|
411
|
+
@natural_width ||= natural_column_widths.inject(0) { |sum, w| sum + w }
|
412
|
+
end
|
413
|
+
|
414
|
+
# Assigns the calculated column widths to each cell. This ensures that each
|
415
|
+
# cell in a column is the same width. After this method is called,
|
416
|
+
# subsequent calls to column_widths and width should return the finalized
|
417
|
+
# values that will be used to ink the table.
|
418
|
+
#
|
419
|
+
def set_column_widths
|
420
|
+
column_widths.each_with_index do |w, col_num|
|
421
|
+
column(col_num).width = w
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
# Assigns the row heights to each cell. This ensures that every cell in a
|
426
|
+
# row is the same height.
|
427
|
+
#
|
428
|
+
def set_row_heights
|
429
|
+
row_heights.each_with_index { |h, row_num| row(row_num).height = h }
|
430
|
+
end
|
431
|
+
|
432
|
+
# Set each cell's position based on the widths and heights of cells
|
433
|
+
# preceding it.
|
434
|
+
#
|
435
|
+
def position_cells
|
436
|
+
# Calculate x- and y-positions as running sums of widths / heights.
|
437
|
+
x_positions = column_widths.inject([0]) { |ary, x|
|
438
|
+
ary << (ary.last + x); ary }[0..-2]
|
439
|
+
x_positions.each_with_index { |x, i| column(i).x = x }
|
440
|
+
|
441
|
+
# y-positions assume an infinitely long canvas starting at zero -- this
|
442
|
+
# is corrected for in Table#draw, and page breaks are properly inserted.
|
443
|
+
y_positions = row_heights.inject([0]) { |ary, y|
|
444
|
+
ary << (ary.last - y); ary}[0..-2]
|
445
|
+
y_positions.each_with_index { |y, i| row(i).y = y }
|
446
|
+
end
|
447
|
+
|
448
|
+
end
|
449
|
+
|
450
|
+
|
451
|
+
end
|
@@ -0,0 +1,395 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# cell.rb: Table cell drawing.
|
4
|
+
#
|
5
|
+
# Copyright December 2009, Gregory Brown and Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
module Prawn
|
10
|
+
class Document
|
11
|
+
|
12
|
+
# Instantiates and draws a cell on the document.
|
13
|
+
#
|
14
|
+
# cell(:content => "Hello world!", :at => [12, 34])
|
15
|
+
#
|
16
|
+
# See Prawn::Table::Cell.make for full options.
|
17
|
+
#
|
18
|
+
def cell(options={})
|
19
|
+
cell = Table::Cell.make(self, options.delete(:content), options)
|
20
|
+
cell.draw
|
21
|
+
cell
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set up, but do not draw, a cell. Useful for creating cells with
|
25
|
+
# formatting options to be inserted into a Table. Call +draw+ on the
|
26
|
+
# resulting Cell to ink it.
|
27
|
+
#
|
28
|
+
# See the documentation on Prawn::Cell for details on the arguments.
|
29
|
+
#
|
30
|
+
def make_cell(content, options={})
|
31
|
+
Prawn::Table::Cell.make(self, content, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class Table
|
37
|
+
|
38
|
+
# A Cell is a rectangular area of the page into which content is drawn. It
|
39
|
+
# has a framework for sizing itself and adding padding and simple styling.
|
40
|
+
# There are several standard Cell subclasses that handle things like text,
|
41
|
+
# Tables, and (in the future) stamps, images, and arbitrary content.
|
42
|
+
#
|
43
|
+
# Cells are a basic building block for table support (see Prawn::Table).
|
44
|
+
#
|
45
|
+
# Please subclass me if you want new content types! I'm designed to be very
|
46
|
+
# extensible. See the different standard Cell subclasses in
|
47
|
+
# lib/prawn/table/cell/*.rb for a template.
|
48
|
+
#
|
49
|
+
class Cell
|
50
|
+
|
51
|
+
# Amount of dead space (in PDF points) inside the borders but outside the
|
52
|
+
# content. Padding defaults to 5pt.
|
53
|
+
#
|
54
|
+
attr_reader :padding
|
55
|
+
|
56
|
+
# If provided, the minimum width that this cell will permit.
|
57
|
+
#
|
58
|
+
def min_width
|
59
|
+
set_width_constraints
|
60
|
+
@min_width
|
61
|
+
end
|
62
|
+
|
63
|
+
# If provided, the maximum width that this cell can be drawn in.
|
64
|
+
#
|
65
|
+
def max_width
|
66
|
+
set_width_constraints
|
67
|
+
@max_width
|
68
|
+
end
|
69
|
+
|
70
|
+
# Manually specify the cell's height.
|
71
|
+
#
|
72
|
+
attr_writer :height
|
73
|
+
|
74
|
+
# Specifies which borders to enable. Must be an array of zero or more of:
|
75
|
+
# <tt>[:left, :right, :top, :bottom]</tt>.
|
76
|
+
#
|
77
|
+
attr_accessor :borders
|
78
|
+
|
79
|
+
# Specifies the width, in PDF points, of the cell's borders.
|
80
|
+
#
|
81
|
+
attr_accessor :border_width
|
82
|
+
|
83
|
+
# Specifies the color of the cell borders. Given in HTML RGB format, e.g.,
|
84
|
+
# "ccffff".
|
85
|
+
#
|
86
|
+
attr_accessor :border_color
|
87
|
+
|
88
|
+
# Specifies the content for the cell. Must be a "cellable" object. See the
|
89
|
+
# "Data" section of the Prawn::Table documentation for details on cellable
|
90
|
+
# objects.
|
91
|
+
#
|
92
|
+
attr_accessor :content
|
93
|
+
|
94
|
+
# The background color, if any, for this cell. Specified in HTML RGB
|
95
|
+
# format, e.g., "ccffff". The background is drawn under the whole cell,
|
96
|
+
# including any padding.
|
97
|
+
#
|
98
|
+
attr_accessor :background_color
|
99
|
+
|
100
|
+
# Instantiates a Cell based on the given options. The particular class of
|
101
|
+
# cell returned depends on the :content argument. See the Prawn::Table
|
102
|
+
# documentation under "Data" for allowable content types.
|
103
|
+
#
|
104
|
+
def self.make(pdf, content, options={})
|
105
|
+
at = options.delete(:at) || [0, pdf.cursor]
|
106
|
+
content = "" if content.nil?
|
107
|
+
|
108
|
+
if content.is_a?(Hash)
|
109
|
+
options.update(content)
|
110
|
+
content = options[:content]
|
111
|
+
else
|
112
|
+
options[:content] = content
|
113
|
+
end
|
114
|
+
|
115
|
+
case content
|
116
|
+
when Prawn::Table::Cell
|
117
|
+
content
|
118
|
+
when String
|
119
|
+
Cell::Text.new(pdf, at, options)
|
120
|
+
when Prawn::Table
|
121
|
+
Cell::Subtable.new(pdf, at, options)
|
122
|
+
when Array
|
123
|
+
subtable = Prawn::Table.new(options[:content], pdf, {})
|
124
|
+
Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
|
125
|
+
else
|
126
|
+
# TODO: other types of content
|
127
|
+
raise ArgumentError, "Content type not recognized: #{content.inspect}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# A small amount added to the bounding box width to cover over floating-
|
132
|
+
# point errors when round-tripping from content_width to width and back.
|
133
|
+
# This does not change cell positioning; it only slightly expands each
|
134
|
+
# cell's bounding box width so that rounding error does not prevent a cell
|
135
|
+
# from rendering.
|
136
|
+
#
|
137
|
+
FPTolerance = 1
|
138
|
+
|
139
|
+
# Sets up a cell on the document +pdf+, at the given x/y location +point+,
|
140
|
+
# with the given +options+. Cell, like Table, follows the "options set
|
141
|
+
# accessors" paradigm (see "Options" under the Table documentation), so
|
142
|
+
# any cell accessor <tt>cell.foo = :bar</tt> can be set by providing the
|
143
|
+
# option <tt>:foo => :bar</tt> here.
|
144
|
+
#
|
145
|
+
def initialize(pdf, point, options={})
|
146
|
+
@pdf = pdf
|
147
|
+
@point = point
|
148
|
+
|
149
|
+
# Set defaults; these can be changed by options
|
150
|
+
@padding = [5, 5, 5, 5]
|
151
|
+
@borders = [:top, :bottom, :left, :right]
|
152
|
+
@border_width = 1
|
153
|
+
@border_color = '000000'
|
154
|
+
|
155
|
+
options.each { |k, v| send("#{k}=", v) }
|
156
|
+
end
|
157
|
+
|
158
|
+
# Supports setting multiple properties at once.
|
159
|
+
#
|
160
|
+
# cell.style(:padding => 0, :border_width => 2)
|
161
|
+
#
|
162
|
+
# is the same as:
|
163
|
+
#
|
164
|
+
# cell.padding = 0
|
165
|
+
# cell.border_width = 2
|
166
|
+
#
|
167
|
+
def style(options={}, &block)
|
168
|
+
options.each { |k, v| send("#{k}=", v) }
|
169
|
+
|
170
|
+
# The block form supports running a single block for multiple cells, as
|
171
|
+
# in Cells#style.
|
172
|
+
block.call(self) if block
|
173
|
+
end
|
174
|
+
|
175
|
+
# Returns the cell's width in points, inclusive of padding.
|
176
|
+
#
|
177
|
+
def width
|
178
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
179
|
+
# from #content_width.
|
180
|
+
@width || (content_width + padding_left + padding_right)
|
181
|
+
end
|
182
|
+
|
183
|
+
# Manually sets the cell's width, inclusive of padding.
|
184
|
+
#
|
185
|
+
def width=(w)
|
186
|
+
@width = @min_width = @max_width = w
|
187
|
+
end
|
188
|
+
|
189
|
+
# Returns the width of the bare content in the cell, excluding padding.
|
190
|
+
#
|
191
|
+
def content_width
|
192
|
+
if @width # manually set
|
193
|
+
return @width - padding_left - padding_right
|
194
|
+
end
|
195
|
+
|
196
|
+
natural_content_width
|
197
|
+
end
|
198
|
+
|
199
|
+
# Returns the width this cell would naturally take on, absent other
|
200
|
+
# constraints. Must be implemented in subclasses.
|
201
|
+
#
|
202
|
+
def natural_content_width
|
203
|
+
raise NotImplementedError,
|
204
|
+
"subclasses must implement natural_content_width"
|
205
|
+
end
|
206
|
+
|
207
|
+
# Returns the cell's height in points, inclusive of padding.
|
208
|
+
#
|
209
|
+
def height
|
210
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
211
|
+
# from #content_height.
|
212
|
+
@height || (content_height + padding_top + padding_bottom)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Returns the height of the bare content in the cell, excluding padding.
|
216
|
+
#
|
217
|
+
def content_height
|
218
|
+
if @height # manually set
|
219
|
+
return @height - padding_top - padding_bottom
|
220
|
+
end
|
221
|
+
|
222
|
+
natural_content_height
|
223
|
+
end
|
224
|
+
|
225
|
+
# Returns the height this cell would naturally take on, absent
|
226
|
+
# constraints. Must be implemented in subclasses.
|
227
|
+
#
|
228
|
+
def natural_content_height
|
229
|
+
raise NotImplementedError,
|
230
|
+
"subclasses must implement natural_content_height"
|
231
|
+
end
|
232
|
+
|
233
|
+
# Draws the cell onto the document. Pass in a point [x,y] to override the
|
234
|
+
# location at which the cell is drawn.
|
235
|
+
#
|
236
|
+
def draw(pt=[x, y])
|
237
|
+
set_width_constraints
|
238
|
+
|
239
|
+
draw_background(pt)
|
240
|
+
draw_borders(pt)
|
241
|
+
@pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top],
|
242
|
+
:width => content_width + FPTolerance,
|
243
|
+
:height => content_height + FPTolerance) do
|
244
|
+
draw_content
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# x-position of the cell within the parent bounds.
|
249
|
+
#
|
250
|
+
def x
|
251
|
+
@point[0]
|
252
|
+
end
|
253
|
+
|
254
|
+
# Set the x-position of the cell within the parent bounds.
|
255
|
+
#
|
256
|
+
def x=(val)
|
257
|
+
@point[0] = val
|
258
|
+
end
|
259
|
+
|
260
|
+
# y-position of the cell within the parent bounds.
|
261
|
+
#
|
262
|
+
def y
|
263
|
+
@point[1]
|
264
|
+
end
|
265
|
+
|
266
|
+
# Set the y-position of the cell within the parent bounds.
|
267
|
+
#
|
268
|
+
def y=(val)
|
269
|
+
@point[1] = val
|
270
|
+
end
|
271
|
+
|
272
|
+
# Sets padding on this cell. The argument can be one of:
|
273
|
+
#
|
274
|
+
# * an integer (sets all padding)
|
275
|
+
# * a two-element array [vertical, horizontal]
|
276
|
+
# * a three-element array [top, horizontal, bottom]
|
277
|
+
# * a four-element array [top, right, bottom, left]
|
278
|
+
#
|
279
|
+
def padding=(pad)
|
280
|
+
@padding = case
|
281
|
+
when pad.nil?
|
282
|
+
[0, 0, 0, 0]
|
283
|
+
when Numeric === pad # all padding
|
284
|
+
[pad, pad, pad, pad]
|
285
|
+
when pad.length == 2 # vert, horiz
|
286
|
+
[pad[0], pad[1], pad[0], pad[1]]
|
287
|
+
when pad.length == 3 # top, horiz, bottom
|
288
|
+
[pad[0], pad[1], pad[2], pad[1]]
|
289
|
+
when pad.length == 4 # top, right, bottom, left
|
290
|
+
[pad[0], pad[1], pad[2], pad[3]]
|
291
|
+
else
|
292
|
+
raise ArgumentError, ":padding must be a number or an array [v,h] " +
|
293
|
+
"or [t,r,b,l]"
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
protected
|
298
|
+
|
299
|
+
# Sets the cell's minimum and maximum width. Deferred until requested
|
300
|
+
# because padding and size can change.
|
301
|
+
#
|
302
|
+
def set_width_constraints
|
303
|
+
@min_width ||= padding_left + padding_right
|
304
|
+
@max_width ||= @pdf.bounds.width
|
305
|
+
end
|
306
|
+
|
307
|
+
# Draws the cell's background color.
|
308
|
+
#
|
309
|
+
def draw_background(pt)
|
310
|
+
x, y = pt
|
311
|
+
margin = @border_width / 2
|
312
|
+
if @background_color
|
313
|
+
@pdf.mask(:fill_color) do
|
314
|
+
@pdf.fill_color @background_color
|
315
|
+
h = @borders.include?(:bottom) ? height - (2*margin) :
|
316
|
+
height + margin
|
317
|
+
@pdf.fill_rectangle [x, y], width, h
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
# Draws borders around the cell. Borders are centered on the bounds of
|
323
|
+
# the cell outside of any padding, so the caller is responsible for
|
324
|
+
# setting appropriate padding to ensure the border does not overlap with
|
325
|
+
# cell content.
|
326
|
+
#
|
327
|
+
def draw_borders(pt)
|
328
|
+
x, y = pt
|
329
|
+
return if @border_width <= 0
|
330
|
+
# Draw left / right borders one-half border width beyond the center of
|
331
|
+
# the corner, so that the corners end up square.
|
332
|
+
margin = @border_width / 2.0
|
333
|
+
|
334
|
+
@pdf.mask(:line_width, :stroke_color) do
|
335
|
+
@pdf.line_width = @border_width
|
336
|
+
@pdf.stroke_color = @border_color if @border_color
|
337
|
+
|
338
|
+
@borders.each do |border|
|
339
|
+
from, to = case border
|
340
|
+
when :top
|
341
|
+
[[x, y], [x+width, y]]
|
342
|
+
when :bottom
|
343
|
+
[[x, y-height], [x+width, y-height]]
|
344
|
+
when :left
|
345
|
+
[[x, y+margin], [x, y-height-margin]]
|
346
|
+
when :right
|
347
|
+
[[x+width, y+margin], [x+width, y-height-margin]]
|
348
|
+
end
|
349
|
+
@pdf.stroke_line(from, to)
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# Draws cell content within the cell's bounding box. Must be implemented
|
355
|
+
# in subclasses.
|
356
|
+
#
|
357
|
+
def draw_content
|
358
|
+
raise NotImplementedError, "subclasses must implement draw_content"
|
359
|
+
end
|
360
|
+
|
361
|
+
def padding_top
|
362
|
+
@padding[0]
|
363
|
+
end
|
364
|
+
|
365
|
+
def padding_top=(val)
|
366
|
+
@padding[0] = val
|
367
|
+
end
|
368
|
+
|
369
|
+
def padding_right
|
370
|
+
@padding[1]
|
371
|
+
end
|
372
|
+
|
373
|
+
def padding_right=(val)
|
374
|
+
@padding[1] = val
|
375
|
+
end
|
376
|
+
|
377
|
+
def padding_bottom
|
378
|
+
@padding[2]
|
379
|
+
end
|
380
|
+
|
381
|
+
def padding_bottom=(val)
|
382
|
+
@padding[2] = val
|
383
|
+
end
|
384
|
+
|
385
|
+
def padding_left
|
386
|
+
@padding[3]
|
387
|
+
end
|
388
|
+
|
389
|
+
def padding_left=(val)
|
390
|
+
@padding[3] = val
|
391
|
+
end
|
392
|
+
|
393
|
+
end
|
394
|
+
end
|
395
|
+
end
|