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/spec/table_spec.rb
ADDED
@@ -0,0 +1,583 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
4
|
+
require 'set'
|
5
|
+
|
6
|
+
describe "Prawn::Table" do
|
7
|
+
|
8
|
+
describe "converting data to Cell objects" do
|
9
|
+
before(:each) do
|
10
|
+
@pdf = Prawn::Document.new
|
11
|
+
@table = @pdf.table([%w[R0C0 R0C1], %w[R1C0 R1C1]])
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return a Prawn::Table" do
|
15
|
+
@table.should.be.an.instance_of Prawn::Table
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should flatten the data into the @cells array in row-major order" do
|
19
|
+
@table.cells.map { |c| c.content }.should == %w[R0C0 R0C1 R1C0 R1C1]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should add row and column numbers to each cell" do
|
23
|
+
c = @table.cells.to_a.first
|
24
|
+
c.row.should == 0
|
25
|
+
c.column.should == 0
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should allow empty fields" do
|
29
|
+
lambda {
|
30
|
+
data = [["foo","bar"],["baz",""]]
|
31
|
+
@pdf.table(data)
|
32
|
+
}.should.not.raise
|
33
|
+
end
|
34
|
+
|
35
|
+
# TODO: pending colspan
|
36
|
+
xit "should accurately count columns from data" do
|
37
|
+
# First data row may contain colspan which would hide true column count
|
38
|
+
data = [["Name:",{:text => "Some very long name", :colspan => 5}]]
|
39
|
+
pdf = Prawn::Document.new
|
40
|
+
table = Prawn::Table.new data, pdf
|
41
|
+
table.column_widths.length.should == 6
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#initialize" do
|
46
|
+
before(:each) do
|
47
|
+
@pdf = Prawn::Document.new
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should instance_eval a 0-arg block" do
|
51
|
+
initializer = mock()
|
52
|
+
initializer.expects(:kick).once
|
53
|
+
|
54
|
+
@pdf.table([["a"]]){
|
55
|
+
self.should.be.an.instance_of(Prawn::Table); initializer.kick }
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should call a 1-arg block with the document as the argument" do
|
59
|
+
initializer = mock()
|
60
|
+
initializer.expects(:kick).once
|
61
|
+
|
62
|
+
@pdf.table([["a"]]){ |doc|
|
63
|
+
doc.should.be.an.instance_of(Prawn::Table); initializer.kick }
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should proxy cell methods to #cells" do
|
67
|
+
table = @pdf.table([["a"]], :cell_style => { :padding => 11 })
|
68
|
+
table.cells[0, 0].padding.should == [11, 11, 11, 11]
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should set row and column length" do
|
72
|
+
table = @pdf.table([["a", "b", "c"], ["d", "e", "f"]])
|
73
|
+
table.row_length.should == 2
|
74
|
+
table.column_length.should == 3
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should generate a text cell based on a String" do
|
78
|
+
t = @pdf.table([["foo"]])
|
79
|
+
t.cells[0,0].should.be.a.kind_of(Prawn::Table::Cell::Text)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should pass through a text cell" do
|
83
|
+
c = Prawn::Table::Cell::Text.new(@pdf, [0,0], :content => "foo")
|
84
|
+
t = @pdf.table([[c]])
|
85
|
+
t.cells[0,0].should == c
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "cell accessors" do
|
90
|
+
before(:each) do
|
91
|
+
@pdf = Prawn::Document.new
|
92
|
+
@table = @pdf.table([%w[R0C0 R0C1], %w[R1C0 R1C1]])
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should select rows by number or range" do
|
96
|
+
Set.new(@table.row(0).map { |c| c.content }).should ==
|
97
|
+
Set.new(%w[R0C0 R0C1])
|
98
|
+
Set.new(@table.rows(0..1).map { |c| c.content }).should ==
|
99
|
+
Set.new(%w[R0C0 R0C1 R1C0 R1C1])
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should select columns by number or range" do
|
103
|
+
Set.new(@table.column(0).map { |c| c.content }).should ==
|
104
|
+
Set.new(%w[R0C0 R1C0])
|
105
|
+
Set.new(@table.columns(0..1).map { |c| c.content }).should ==
|
106
|
+
Set.new(%w[R0C0 R0C1 R1C0 R1C1])
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should allow rows and columns to be combined" do
|
110
|
+
@table.row(0).column(1).map { |c| c.content }.should == ["R0C1"]
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should accept a select block, returning a cell proxy" do
|
114
|
+
@table.cells.select { |c| c.content =~ /R0/ }.column(1).map{ |c|
|
115
|
+
c.content }.should == ["R0C1"]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should accept the [] method, returning a Cell or nil" do
|
119
|
+
@table.cells[0, 0].content.should == "R0C0"
|
120
|
+
@table.cells[12, 12].should.be.nil
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should proxy unknown methods to the cells" do
|
124
|
+
@table.cells.height = 200
|
125
|
+
@table.row(1).height = 100
|
126
|
+
|
127
|
+
@table.cells[0, 0].height.should == 200
|
128
|
+
@table.cells[1, 0].height.should == 100
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should accept the style method, proxying its calls to the cells" do
|
132
|
+
@table.cells.style(:height => 200, :width => 200)
|
133
|
+
@table.column(0).style(:width => 100)
|
134
|
+
|
135
|
+
@table.cells[0, 1].width.should == 200
|
136
|
+
@table.cells[1, 0].height.should == 200
|
137
|
+
@table.cells[1, 0].width.should == 100
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should return the width of selected columns for #width" do
|
141
|
+
c0_width = @table.column(0).map{ |c| c.width }.max
|
142
|
+
c1_width = @table.column(1).map{ |c| c.width }.max
|
143
|
+
|
144
|
+
@table.column(0).width.should == c0_width
|
145
|
+
@table.column(1).width.should == c1_width
|
146
|
+
|
147
|
+
@table.columns(0..1).width.should == c0_width + c1_width
|
148
|
+
@table.cells.width.should == c0_width + c1_width
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should return the height of selected rows for #height" do
|
152
|
+
r0_height = @table.row(0).map{ |c| c.height }.max
|
153
|
+
r1_height = @table.row(1).map{ |c| c.height }.max
|
154
|
+
|
155
|
+
@table.row(0).height.should == r0_height
|
156
|
+
@table.row(1).height.should == r1_height
|
157
|
+
|
158
|
+
@table.rows(0..1).height.should == r0_height + r1_height
|
159
|
+
@table.cells.height.should == r0_height + r1_height
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "layout" do
|
164
|
+
before(:each) do
|
165
|
+
@pdf = Prawn::Document.new
|
166
|
+
@long_text = "The quick brown fox jumped over the lazy dogs. " * 5
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "width" do
|
170
|
+
it "should raise an error if the given width is outside of range" do
|
171
|
+
lambda do
|
172
|
+
@pdf.table([["foo"]], :width => 1)
|
173
|
+
end.should.raise(Prawn::Errors::CannotFit)
|
174
|
+
|
175
|
+
lambda do
|
176
|
+
@pdf.table([[@long_text]], :width => @pdf.bounds.width + 100)
|
177
|
+
end.should.raise(Prawn::Errors::CannotFit)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should accept the natural width for small tables" do
|
181
|
+
pad = 10 # default padding
|
182
|
+
@table = @pdf.table([["a"]])
|
183
|
+
@table.width.should == @table.cells[0, 0].natural_content_width + pad
|
184
|
+
end
|
185
|
+
|
186
|
+
it "width should equal sum(column_widths)" do
|
187
|
+
table = Prawn::Table.new([%w[ a b c ], %w[d e f]], @pdf) do
|
188
|
+
column(0).width = 50
|
189
|
+
column(1).width = 100
|
190
|
+
column(2).width = 150
|
191
|
+
end
|
192
|
+
table.width.should == 300
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should calculate unspecified column widths as "+
|
196
|
+
"(max(string_width) + 2*horizontal_padding)" do
|
197
|
+
hpad, fs = 3, 12
|
198
|
+
columns = 2
|
199
|
+
table = Prawn::Table.new( [%w[ foo b ], %w[d foobar]], @pdf,
|
200
|
+
:cell_style => { :padding => hpad, :size => fs } )
|
201
|
+
|
202
|
+
col0_width = @pdf.width_of("foo", :size => fs)
|
203
|
+
col1_width = @pdf.width_of("foobar", :size => fs)
|
204
|
+
|
205
|
+
table.width.should == col0_width + col1_width + 2*columns*hpad
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should allow mixing autocalculated and preset"+
|
209
|
+
"column widths within a single table" do
|
210
|
+
hpad, fs = 10, 6
|
211
|
+
stretchy_columns = 2
|
212
|
+
|
213
|
+
col0_width = 50
|
214
|
+
col1_width = @pdf.width_of("foo", :size => fs)
|
215
|
+
col2_width = @pdf.width_of("foobar", :size => fs)
|
216
|
+
col3_width = 150
|
217
|
+
|
218
|
+
table = Prawn::Table.new( [%w[snake foo b apple],
|
219
|
+
%w[kitten d foobar banana]], @pdf,
|
220
|
+
:cell_style => { :padding => hpad, :size => fs }) do
|
221
|
+
|
222
|
+
column(0).width = col0_width
|
223
|
+
column(3).width = col3_width
|
224
|
+
end
|
225
|
+
|
226
|
+
table.width.should == col1_width + col2_width +
|
227
|
+
2*stretchy_columns*hpad +
|
228
|
+
col0_width + col3_width
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should not exceed the maximum width of the margin_box" do
|
232
|
+
expected_width = @pdf.margin_box.width
|
233
|
+
data = [
|
234
|
+
['This is a column with a lot of text that should comfortably exceed '+
|
235
|
+
'the width of a normal document margin_box width', 'Some more text',
|
236
|
+
'and then some more', 'Just a bit more to be extra sure']
|
237
|
+
]
|
238
|
+
table = Prawn::Table.new(data, @pdf)
|
239
|
+
|
240
|
+
table.width.should == expected_width
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should not exceed the maximum width of the margin_box even with" +
|
244
|
+
"manual widths specified" do
|
245
|
+
expected_width = @pdf.margin_box.width
|
246
|
+
data = [
|
247
|
+
['This is a column with a lot of text that should comfortably exceed '+
|
248
|
+
'the width of a normal document margin_box width', 'Some more text',
|
249
|
+
'and then some more', 'Just a bit more to be extra sure']
|
250
|
+
]
|
251
|
+
table = Prawn::Table.new(data, @pdf) { column(1).width = 100 }
|
252
|
+
|
253
|
+
table.width.should == expected_width
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should allow width to be reset even after it has been calculated" do
|
257
|
+
@table = @pdf.table([[@long_text]])
|
258
|
+
@table.width
|
259
|
+
@table.width = 100
|
260
|
+
@table.width.should == 100
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should shrink columns evenly when two equal columns compete" do
|
264
|
+
@table = @pdf.table([["foo", @long_text], [@long_text, "foo"]])
|
265
|
+
@table.cells[0, 0].width.should == @table.cells[0, 1].width
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should grow columns evenly when equal deficient columns compete" do
|
269
|
+
@table = @pdf.table([["foo", "foobar"], ["foobar", "foo"]], :width => 500)
|
270
|
+
@table.cells[0, 0].width.should == @table.cells[0, 1].width
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should respect manual widths" do
|
274
|
+
@table = @pdf.table([%w[foo bar baz], %w[baz bar foo]], :width => 500) do
|
275
|
+
column(1).width = 60
|
276
|
+
end
|
277
|
+
@table.column(1).width.should == 60
|
278
|
+
@table.column(0).width.should == @table.column(2).width
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should be the width of the :width parameter" do
|
282
|
+
expected_width = 300
|
283
|
+
table = Prawn::Table.new( [%w[snake foo b apple],
|
284
|
+
%w[kitten d foobar banana]], @pdf,
|
285
|
+
:width => expected_width)
|
286
|
+
|
287
|
+
table.width.should == expected_width
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should not exceed the :width option" do
|
291
|
+
expected_width = 400
|
292
|
+
data = [
|
293
|
+
['This is a column with a lot of text that should comfortably exceed '+
|
294
|
+
'the width of a normal document margin_box width', 'Some more text',
|
295
|
+
'and then some more', 'Just a bit more to be extra sure']
|
296
|
+
]
|
297
|
+
table = Prawn::Table.new(data, @pdf, :width => expected_width)
|
298
|
+
|
299
|
+
table.width.should == expected_width
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should not exceed the :width option even with manual widths specified" do
|
303
|
+
expected_width = 400
|
304
|
+
data = [
|
305
|
+
['This is a column with a lot of text that should comfortably exceed '+
|
306
|
+
'the width of a normal document margin_box width', 'Some more text',
|
307
|
+
'and then some more', 'Just a bit more to be extra sure']
|
308
|
+
]
|
309
|
+
table = Prawn::Table.new(data, @pdf, :width => expected_width) do
|
310
|
+
column(1).width = 100
|
311
|
+
end
|
312
|
+
|
313
|
+
table.width.should == expected_width
|
314
|
+
end
|
315
|
+
|
316
|
+
# TODO: pending colspan
|
317
|
+
xit "should calculate unspecified column widths even " +
|
318
|
+
"with colspan cells declared" do
|
319
|
+
pdf = Prawn::Document.new
|
320
|
+
hpad, fs = 3, 5
|
321
|
+
columns = 3
|
322
|
+
|
323
|
+
data = [ [ { :text => 'foo', :colspan => 2 }, "foobar" ],
|
324
|
+
[ "foo", "foo", "foo" ] ]
|
325
|
+
table = Prawn::Table.new( data, pdf,
|
326
|
+
:horizontal_padding => hpad,
|
327
|
+
:font_size => fs )
|
328
|
+
|
329
|
+
col0_width = pdf.width_of("foo", :size => fs) # cell 1, 0
|
330
|
+
col1_width = pdf.width_of("foo", :size => fs) # cell 1, 1
|
331
|
+
col2_width = pdf.width_of("foobar", :size => fs) # cell 0, 1 (at col 2)
|
332
|
+
|
333
|
+
table.width.should == col0_width.ceil + col1_width.ceil +
|
334
|
+
col2_width.ceil + 2*columns*hpad
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
describe "height" do
|
339
|
+
it "should set all cells in a row to the same height" do
|
340
|
+
@table = @pdf.table([["foo", @long_text]])
|
341
|
+
@table.cells[0, 0].height.should == @table.cells[0, 1].height
|
342
|
+
end
|
343
|
+
|
344
|
+
it "should move y-position to the bottom of the table after drawing" do
|
345
|
+
old_y = @pdf.y
|
346
|
+
table = @pdf.table([["foo"]])
|
347
|
+
@pdf.y.should == old_y - table.height
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should not wrap unnecessarily" do
|
351
|
+
# Test for FP errors and glitches
|
352
|
+
t = @pdf.table([["Bender Bending Rodriguez"]])
|
353
|
+
h = @pdf.height_of("one line")
|
354
|
+
(t.height - 10).should.be < h*1.5
|
355
|
+
end
|
356
|
+
|
357
|
+
it "should have a height of n rows" do
|
358
|
+
data = [["foo"],["bar"],["baaaz"]]
|
359
|
+
|
360
|
+
vpad = 4
|
361
|
+
origin = @pdf.y
|
362
|
+
@pdf.table data, :cell_style => { :padding => vpad }
|
363
|
+
|
364
|
+
table_height = origin - @pdf.y
|
365
|
+
font_height = @pdf.font.height
|
366
|
+
|
367
|
+
num_rows = data.length
|
368
|
+
table_height.should.be.close(
|
369
|
+
num_rows*font_height + 2*vpad*num_rows, 0.001 )
|
370
|
+
end
|
371
|
+
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
|
376
|
+
describe "Multi-page tables" do
|
377
|
+
it "should flow to the next page when hitting the bottom of the bounds" do
|
378
|
+
Prawn::Document.new { table([["foo"]] * 30) }.page_count.should == 1
|
379
|
+
Prawn::Document.new { table([["foo"]] * 31) }.page_count.should == 2
|
380
|
+
Prawn::Document.new { table([["foo"]] * 31); table([["foo"]] * 31) }.
|
381
|
+
page_count.should == 3
|
382
|
+
end
|
383
|
+
|
384
|
+
it "should respect the containing bounds" do
|
385
|
+
Prawn::Document.new do
|
386
|
+
bounding_box([0, cursor], :width => bounds.width, :height => 72) do
|
387
|
+
table([["foo"]] * 4)
|
388
|
+
end
|
389
|
+
end.page_count.should == 2
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
describe "#style" do
|
394
|
+
it "should send #style to its first argument, passing the style hash and" +
|
395
|
+
" block" do
|
396
|
+
|
397
|
+
stylable = stub()
|
398
|
+
stylable.expects(:style).with(:foo => :bar).once.yields
|
399
|
+
|
400
|
+
block = stub()
|
401
|
+
block.expects(:kick).once
|
402
|
+
|
403
|
+
Prawn::Document.new do
|
404
|
+
table([["x"]]) { style(stylable, :foo => :bar) { block.kick } }
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
it "should default to {} for the hash argument" do
|
409
|
+
stylable = stub()
|
410
|
+
stylable.expects(:style).with({}).once
|
411
|
+
|
412
|
+
Prawn::Document.new do
|
413
|
+
table([["x"]]) { style(stylable) }
|
414
|
+
end
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
describe "row_colors" do
|
419
|
+
it "should allow array syntax for :row_colors" do
|
420
|
+
data = [["foo"], ["bar"], ["baz"]]
|
421
|
+
pdf = Prawn::Document.new
|
422
|
+
t = pdf.table(data, :row_colors => ['cccccc', 'ffffff'])
|
423
|
+
t.cells.map{|x| x.background_color}.should == %w[cccccc ffffff cccccc]
|
424
|
+
end
|
425
|
+
|
426
|
+
it "should ignore headers" do
|
427
|
+
data = [["header"], ["foo"], ["bar"], ["baz"]]
|
428
|
+
pdf = Prawn::Document.new
|
429
|
+
t = pdf.table(data, :header => true,
|
430
|
+
:row_colors => ['cccccc', 'ffffff']) do
|
431
|
+
row(0).background_color = '333333'
|
432
|
+
end
|
433
|
+
|
434
|
+
t.cells.map{|x| x.background_color}.should ==
|
435
|
+
%w[333333 cccccc ffffff cccccc]
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
describe "inking" do
|
440
|
+
before(:each) do
|
441
|
+
@pdf = Prawn::Document.new
|
442
|
+
end
|
443
|
+
|
444
|
+
it "should set the x-position of each cell based on widths" do
|
445
|
+
@table = @pdf.table([["foo", "bar", "baz"]])
|
446
|
+
|
447
|
+
x = 0
|
448
|
+
(0..2).each do |col|
|
449
|
+
cell = @table.cells[0, col]
|
450
|
+
cell.x.should == x
|
451
|
+
x += cell.width
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
it "should set the y-position of each cell based on heights" do
|
456
|
+
y = 0
|
457
|
+
@table = @pdf.make_table([["foo"], ["bar"], ["baz"]])
|
458
|
+
|
459
|
+
(0..2).each do |row|
|
460
|
+
cell = @table.cells[row, 0]
|
461
|
+
cell.y.should.be.close(y, 0.01)
|
462
|
+
y -= cell.height
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
it "should output content cell by cell, row by row" do
|
467
|
+
data = [["foo","bar"],["baz","bang"]]
|
468
|
+
@pdf = Prawn::Document.new
|
469
|
+
@pdf.table(data)
|
470
|
+
output = PDF::Inspector::Text.analyze(@pdf.render)
|
471
|
+
output.strings.should == data.flatten
|
472
|
+
end
|
473
|
+
|
474
|
+
it "should not cause an error if rendering the very first row causes a " +
|
475
|
+
"page break" do
|
476
|
+
Prawn::Document.new do
|
477
|
+
arr = Array(1..5).collect{|i| ["cell #{i}"] }
|
478
|
+
|
479
|
+
move_down( y - (bounds.absolute_bottom + 3) )
|
480
|
+
|
481
|
+
lambda {
|
482
|
+
table(arr)
|
483
|
+
}.should.not.raise
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
it "should allow multiple inkings of the same table" do
|
488
|
+
pdf = Prawn::Document.new
|
489
|
+
t = Prawn::Table.new([["foo"]], pdf)
|
490
|
+
|
491
|
+
pdf.expects(:bounding_box).with{|(x, y), options| y.to_i == 495}.yields
|
492
|
+
pdf.expects(:bounding_box).with{|(x, y), options| y.to_i == 395}.yields
|
493
|
+
pdf.expects(:draw_text!).with{ |text, options| text == 'foo' }.twice
|
494
|
+
|
495
|
+
pdf.move_cursor_to(500)
|
496
|
+
t.draw
|
497
|
+
|
498
|
+
pdf.move_cursor_to(400)
|
499
|
+
t.draw
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
describe "headers" do
|
504
|
+
it "should add headers to output when specified" do
|
505
|
+
data = [["a", "b"], ["foo","bar"],["baz","bang"]]
|
506
|
+
@pdf = Prawn::Document.new
|
507
|
+
@pdf.table(data, :header => true)
|
508
|
+
output = PDF::Inspector::Text.analyze(@pdf.render)
|
509
|
+
output.strings.should == data.flatten
|
510
|
+
end
|
511
|
+
|
512
|
+
it "should repeat headers across pages" do
|
513
|
+
data = [["foo","bar"]]*30
|
514
|
+
headers = ["baz","foobar"]
|
515
|
+
@pdf = Prawn::Document.new
|
516
|
+
@pdf.table([headers] + data, :header => true)
|
517
|
+
output = PDF::Inspector::Text.analyze(@pdf.render)
|
518
|
+
output.strings.should == headers + data.flatten[0..-3] + headers +
|
519
|
+
data.flatten[-2..-1]
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
describe "nested tables" do
|
524
|
+
before(:each) do
|
525
|
+
@pdf = Prawn::Document.new
|
526
|
+
@subtable = Prawn::Table.new([["foo"]], @pdf)
|
527
|
+
@table = @pdf.table([[@subtable, "bar"]])
|
528
|
+
end
|
529
|
+
|
530
|
+
it "can be created from an Array" do
|
531
|
+
cell = Prawn::Table::Cell.make(@pdf, [["foo"]])
|
532
|
+
cell.should.be.an.instance_of(Prawn::Table::Cell::Subtable)
|
533
|
+
cell.subtable.should.be.an.instance_of(Prawn::Table)
|
534
|
+
end
|
535
|
+
|
536
|
+
it "defaults its padding to zero" do
|
537
|
+
@table.cells[0, 0].padding.should == [0, 0, 0, 0]
|
538
|
+
end
|
539
|
+
|
540
|
+
it "has a subtable accessor" do
|
541
|
+
@table.cells[0, 0].subtable.should == @subtable
|
542
|
+
end
|
543
|
+
|
544
|
+
it "determines its dimensions from the subtable" do
|
545
|
+
@table.cells[0, 0].width.should == @subtable.width
|
546
|
+
@table.cells[0, 0].height.should == @subtable.height
|
547
|
+
end
|
548
|
+
|
549
|
+
end
|
550
|
+
|
551
|
+
describe "An invalid table" do
|
552
|
+
|
553
|
+
before(:each) do
|
554
|
+
@pdf = Prawn::Document.new
|
555
|
+
@bad_data = ["Single Nested Array"]
|
556
|
+
end
|
557
|
+
|
558
|
+
it "should raise error when invalid table data is given" do
|
559
|
+
assert_raises(Prawn::Errors::InvalidTableData) do
|
560
|
+
@pdf.table(@bad_data)
|
561
|
+
end
|
562
|
+
end
|
563
|
+
|
564
|
+
it "should raise an EmptyTableError with empty table data" do
|
565
|
+
lambda {
|
566
|
+
data = []
|
567
|
+
@pdf = Prawn::Document.new
|
568
|
+
@pdf.table(data)
|
569
|
+
}.should.raise( Prawn::Errors::EmptyTable )
|
570
|
+
end
|
571
|
+
|
572
|
+
it "should raise an EmptyTableError with nil table data" do
|
573
|
+
lambda {
|
574
|
+
data = nil
|
575
|
+
@pdf = Prawn::Document.new
|
576
|
+
@pdf.table(data)
|
577
|
+
}.should.raise( Prawn::Errors::EmptyTable )
|
578
|
+
end
|
579
|
+
|
580
|
+
end
|
581
|
+
|
582
|
+
end
|
583
|
+
|