piglop-prawn 0.10.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +49 -0
- data/LICENSE +56 -0
- data/README +140 -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/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 +43 -0
- data/examples/bounding_box/indentation.rb +34 -0
- data/examples/bounding_box/russian_boxes.rb +36 -0
- data/examples/bounding_box/stretched_nesting.rb +67 -0
- data/examples/example_helper.rb +4 -0
- data/examples/general/background.rb +23 -0
- data/examples/general/canvas.rb +15 -0
- data/examples/general/context_sensitive_headers.rb +37 -0
- data/examples/general/float.rb +11 -0
- data/examples/general/margin.rb +36 -0
- data/examples/general/measurement_units.rb +51 -0
- data/examples/general/metadata-info.rb +16 -0
- data/examples/general/multi_page_layout.rb +18 -0
- data/examples/general/outlines.rb +50 -0
- data/examples/general/page_geometry.rb +31 -0
- data/examples/general/page_numbering.rb +15 -0
- data/examples/general/repeaters.rb +47 -0
- data/examples/general/stamp.rb +41 -0
- data/examples/general/templates.rb +13 -0
- data/examples/graphics/basic_images.rb +23 -0
- data/examples/graphics/cmyk.rb +12 -0
- data/examples/graphics/curves.rb +11 -0
- data/examples/graphics/hexagon.rb +13 -0
- data/examples/graphics/image_fit.rb +15 -0
- data/examples/graphics/image_flow.rb +37 -0
- data/examples/graphics/image_position.rb +17 -0
- data/examples/graphics/line.rb +32 -0
- data/examples/graphics/png_types.rb +22 -0
- data/examples/graphics/polygons.rb +16 -0
- data/examples/graphics/remote_images.rb +12 -0
- data/examples/graphics/rounded_polygons.rb +19 -0
- data/examples/graphics/rounded_rectangle.rb +20 -0
- data/examples/graphics/ruport_style_helpers.rb +19 -0
- data/examples/graphics/stroke_bounds.rb +20 -0
- data/examples/graphics/stroke_cap_and_join.rb +45 -0
- data/examples/graphics/stroke_dash.rb +42 -0
- data/examples/graphics/transformations.rb +52 -0
- data/examples/graphics/transparency.rb +26 -0
- data/examples/m17n/chinese_text_wrapping.rb +17 -0
- data/examples/m17n/euro.rb +15 -0
- data/examples/m17n/sjis.rb +28 -0
- data/examples/m17n/utf8.rb +13 -0
- data/examples/m17n/win_ansi_charset.rb +54 -0
- data/examples/security/hello_foo.rb +8 -0
- data/examples/table/bill.rb +53 -0
- data/examples/table/cell.rb +12 -0
- data/examples/table/checkerboard.rb +22 -0
- data/examples/table/header.rb +14 -0
- data/examples/table/inline_format_table.rb +12 -0
- data/examples/table/multi_page_table.rb +9 -0
- data/examples/table/simple_table.rb +24 -0
- data/examples/table/subtable.rb +12 -0
- data/examples/table/widths.rb +20 -0
- data/examples/text/alignment.rb +18 -0
- data/examples/text/dfont.rb +48 -0
- data/examples/text/family_based_styling.rb +24 -0
- data/examples/text/font_calculations.rb +91 -0
- data/examples/text/font_size.rb +33 -0
- data/examples/text/hyphenation.rb +45 -0
- data/examples/text/indent_paragraphs.rb +18 -0
- data/examples/text/inline_format.rb +72 -0
- data/examples/text/kerning.rb +30 -0
- data/examples/text/rotated.rb +98 -0
- data/examples/text/shaped_text_box.rb +31 -0
- data/examples/text/simple_text.rb +17 -0
- data/examples/text/simple_text_ttf.rb +17 -0
- data/examples/text/text_box.rb +88 -0
- data/examples/text/text_box_returning_excess.rb +51 -0
- data/examples/text/text_flow.rb +67 -0
- data/lib/prawn/compatibility.rb +51 -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 +84 -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 +219 -0
- data/lib/prawn/core/page.rb +179 -0
- data/lib/prawn/core/pdf_object.rb +83 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text/formatted/arranger.rb +260 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +113 -0
- data/lib/prawn/core/text/line_wrap.rb +198 -0
- data/lib/prawn/core/text/wrap.rb +80 -0
- data/lib/prawn/core/text.rb +115 -0
- data/lib/prawn/core.rb +89 -0
- data/lib/prawn/document/bounding_box.rb +425 -0
- data/lib/prawn/document/graphics_state.rb +48 -0
- data/lib/prawn/document/internals.rb +176 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document.rb +573 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +94 -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/font.rb +342 -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/graphics.rb +321 -0
- data/lib/prawn/images/jpg.rb +46 -0
- data/lib/prawn/images/png.rb +226 -0
- data/lib/prawn/images.rb +348 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +278 -0
- data/lib/prawn/repeater.rb +129 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table/accessors.rb +180 -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 +128 -0
- data/lib/prawn/table/cell.rb +330 -0
- data/lib/prawn/table.rb +421 -0
- data/lib/prawn/text/box.rb +380 -0
- data/lib/prawn/text/formatted/box.rb +215 -0
- data/lib/prawn/text/formatted/fragment.rb +127 -0
- data/lib/prawn/text/formatted/parser.rb +202 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text.rb +442 -0
- data/lib/prawn.rb +25 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +337 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +461 -0
- data/spec/font_spec.rb +314 -0
- data/spec/formatted_text_arranger_spec.rb +400 -0
- data/spec/formatted_text_box_spec.rb +659 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/images_spec.rb +96 -0
- data/spec/inline_formatted_text_parser_spec.rb +446 -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 +229 -0
- data/spec/pdf_object_spec.rb +127 -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 +138 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/stamp_spec.rb +108 -0
- data/spec/stroke_styles_spec.rb +163 -0
- data/spec/table_spec.rb +583 -0
- data/spec/template_spec.rb +158 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +675 -0
- data/spec/text_spacing_spec.rb +57 -0
- data/spec/text_spec.rb +328 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +75 -0
- data/vendor/pdf-inspector/README +18 -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/pdf-inspector/lib/pdf/inspector.rb +26 -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/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/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.rb +18 -0
- data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -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/cmap.rb +34 -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/glyf.rb +64 -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/format0.rb +62 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -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/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/post.rb +91 -0
- data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk.rb +102 -0
- metadata +364 -0
data/lib/prawn/table.rb
ADDED
@@ -0,0 +1,421 @@
|
|
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/accessors'
|
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
|
+
@cells.each do |cell|
|
234
|
+
if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom
|
235
|
+
# start a new page or column
|
236
|
+
@pdf.bounds.move_past_bottom
|
237
|
+
draw_header
|
238
|
+
offset = @pdf.y - cell.y
|
239
|
+
end
|
240
|
+
|
241
|
+
# Don't modify cell.x / cell.y here, as we want to reuse the original
|
242
|
+
# values when re-inking the table. #draw should be able to be called
|
243
|
+
# multiple times.
|
244
|
+
x, y = cell.x, cell.y
|
245
|
+
y += offset
|
246
|
+
|
247
|
+
# Translate coordinates to the bounds we are in, since drawing is
|
248
|
+
# relative to the cursor, not ref_bounds.
|
249
|
+
x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
|
250
|
+
y -= @pdf.bounds.absolute_bottom
|
251
|
+
|
252
|
+
# Set background color, if any.
|
253
|
+
if @row_colors && (!@header || cell.row > 0)
|
254
|
+
index = @header ? (cell.row - 1) : cell.row
|
255
|
+
cell.background_color = @row_colors[index % @row_colors.length]
|
256
|
+
end
|
257
|
+
|
258
|
+
cell.draw([x, y])
|
259
|
+
last_y = y
|
260
|
+
end
|
261
|
+
|
262
|
+
@pdf.move_cursor_to(last_y - @cells.last.height)
|
263
|
+
end
|
264
|
+
|
265
|
+
protected
|
266
|
+
|
267
|
+
# Converts the array of cellable objects given into instances of
|
268
|
+
# Prawn::Table::Cell, and sets up their in-table properties so that they
|
269
|
+
# know their own position in the table.
|
270
|
+
#
|
271
|
+
def make_cells(data)
|
272
|
+
assert_proper_table_data(data)
|
273
|
+
|
274
|
+
cells = []
|
275
|
+
|
276
|
+
@row_length = data.length
|
277
|
+
@column_length = data.map{ |r| r.length }.max
|
278
|
+
|
279
|
+
data.each_with_index do |row_cells, row_number|
|
280
|
+
row_cells.each_with_index do |cell_data, column_number|
|
281
|
+
cell = Cell.make(@pdf, cell_data)
|
282
|
+
cell.extend(Cell::InTable)
|
283
|
+
cell.row = row_number
|
284
|
+
cell.column = column_number
|
285
|
+
cells << cell
|
286
|
+
end
|
287
|
+
end
|
288
|
+
cells
|
289
|
+
end
|
290
|
+
|
291
|
+
# Raises an error if the data provided cannot be converted into a valid
|
292
|
+
# table.
|
293
|
+
#
|
294
|
+
def assert_proper_table_data(data)
|
295
|
+
if data.nil? || data.empty?
|
296
|
+
raise Prawn::Errors::EmptyTable,
|
297
|
+
"data must be a non-empty, non-nil, two dimensional array " +
|
298
|
+
"of cell-convertible objects"
|
299
|
+
end
|
300
|
+
|
301
|
+
unless data.all? { |e| Array === e }
|
302
|
+
raise Prawn::Errors::InvalidTableData,
|
303
|
+
"data must be a two dimensional array of cellable objects"
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
# If the table has a header, draw it at the current position.
|
308
|
+
#
|
309
|
+
def draw_header
|
310
|
+
if @header
|
311
|
+
y = @pdf.cursor
|
312
|
+
row(0).each do |cell|
|
313
|
+
cell.y = y
|
314
|
+
cell.draw
|
315
|
+
end
|
316
|
+
@pdf.move_cursor_to(y - row(0).height)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
# Returns an array of each column's natural (unconstrained) width.
|
321
|
+
#
|
322
|
+
def natural_column_widths
|
323
|
+
@natural_column_widths ||= (0...column_length).map { |c| column(c).width }
|
324
|
+
end
|
325
|
+
|
326
|
+
# Returns the "natural" (unconstrained) width of the table. This may be
|
327
|
+
# extremely silly; for example, the unconstrained width of a paragraph of
|
328
|
+
# text is the width it would assume if it were not wrapped at all. Could be
|
329
|
+
# a mile long.
|
330
|
+
#
|
331
|
+
def natural_width
|
332
|
+
@natural_width ||= natural_column_widths.inject(0) { |sum, w| sum + w }
|
333
|
+
end
|
334
|
+
|
335
|
+
# Calculate and return the constrained column widths, taking into account
|
336
|
+
# each cell's min_width, max_width, and any user-specified constraints on
|
337
|
+
# the table or column size.
|
338
|
+
#
|
339
|
+
# Because the natural widths can be silly, this does not always work so well
|
340
|
+
# at guessing a good size for columns that have vastly different content. If
|
341
|
+
# you see weird problems like CannotFit errors or shockingly bad column
|
342
|
+
# sizes, you should specify more column widths manually.
|
343
|
+
#
|
344
|
+
def column_widths
|
345
|
+
@column_widths ||= begin
|
346
|
+
if width < cells.min_width
|
347
|
+
raise Errors::CannotFit,
|
348
|
+
"Table's width was set too small to contain its contents"
|
349
|
+
end
|
350
|
+
|
351
|
+
if width > cells.max_width
|
352
|
+
raise Errors::CannotFit,
|
353
|
+
"Table's width was set larger than its contents' maximum width"
|
354
|
+
end
|
355
|
+
|
356
|
+
if width < natural_width
|
357
|
+
# Shrink the table to fit the requested width.
|
358
|
+
f = (width - cells.min_width).to_f / (natural_width - cells.min_width)
|
359
|
+
|
360
|
+
(0...column_length).map do |c|
|
361
|
+
min, nat = column(c).min_width, column(c).width
|
362
|
+
(f * (nat - min)) + min
|
363
|
+
end
|
364
|
+
elsif width > natural_width
|
365
|
+
# Expand the table to fit the requested width.
|
366
|
+
f = (width - cells.width).to_f / (cells.max_width - cells.width)
|
367
|
+
|
368
|
+
(0...column_length).map do |c|
|
369
|
+
nat, max = column(c).width, column(c).max_width
|
370
|
+
(f * (max - nat)) + nat
|
371
|
+
end
|
372
|
+
else
|
373
|
+
natural_column_widths
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
# Returns an array with the height of each row.
|
379
|
+
#
|
380
|
+
def row_heights
|
381
|
+
@natural_row_heights ||= (0...row_length).map{ |r| row(r).height }
|
382
|
+
end
|
383
|
+
|
384
|
+
# Assigns the calculated column widths to each cell. This ensures that each
|
385
|
+
# cell in a column is the same width. After this method is called,
|
386
|
+
# subsequent calls to column_widths and width should return the finalized
|
387
|
+
# values that will be used to ink the table.
|
388
|
+
#
|
389
|
+
def set_column_widths
|
390
|
+
column_widths.each_with_index do |w, col_num|
|
391
|
+
column(col_num).width = w
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
# Assigns the row heights to each cell. This ensures that every cell in a
|
396
|
+
# row is the same height.
|
397
|
+
#
|
398
|
+
def set_row_heights
|
399
|
+
row_heights.each_with_index { |h, row_num| row(row_num).height = h }
|
400
|
+
end
|
401
|
+
|
402
|
+
# Set each cell's position based on the widths and heights of cells
|
403
|
+
# preceding it.
|
404
|
+
#
|
405
|
+
def position_cells
|
406
|
+
# Calculate x- and y-positions as running sums of widths / heights.
|
407
|
+
x_positions = column_widths.inject([0]) { |ary, x|
|
408
|
+
ary << (ary.last + x); ary }[0..-2]
|
409
|
+
x_positions.each_with_index { |x, i| column(i).x = x }
|
410
|
+
|
411
|
+
# y-positions assume an infinitely long canvas starting at zero -- this
|
412
|
+
# is corrected for in Table#draw, and page breaks are properly inserted.
|
413
|
+
y_positions = row_heights.inject([0]) { |ary, y|
|
414
|
+
ary << (ary.last - y); ary}[0..-2]
|
415
|
+
y_positions.each_with_index { |y, i| row(i).y = y }
|
416
|
+
end
|
417
|
+
|
418
|
+
end
|
419
|
+
|
420
|
+
|
421
|
+
end
|