davebenvenuti-prawn 0.11.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +50 -0
  3. data/LICENSE +56 -0
  4. data/README +141 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/page_without_mediabox.pdf +193 -0
  62. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  63. data/data/pdfs/two_hexagons.pdf +90 -0
  64. data/data/pdfs/version_1_6.pdf +61 -0
  65. data/data/shift_jis_text.txt +1 -0
  66. data/examples/bounding_box/bounding_boxes.rb +44 -0
  67. data/examples/bounding_box/indentation.rb +35 -0
  68. data/examples/bounding_box/russian_boxes.rb +37 -0
  69. data/examples/bounding_box/stretched_nesting.rb +68 -0
  70. data/examples/example_helper.rb +8 -0
  71. data/examples/general/background.rb +24 -0
  72. data/examples/general/canvas.rb +16 -0
  73. data/examples/general/context_sensitive_headers.rb +38 -0
  74. data/examples/general/float.rb +12 -0
  75. data/examples/general/margin.rb +37 -0
  76. data/examples/general/measurement_units.rb +52 -0
  77. data/examples/general/metadata-info.rb +17 -0
  78. data/examples/general/multi_page_layout.rb +19 -0
  79. data/examples/general/outlines.rb +67 -0
  80. data/examples/general/page_geometry.rb +32 -0
  81. data/examples/general/page_numbering.rb +16 -0
  82. data/examples/general/repeaters.rb +48 -0
  83. data/examples/general/stamp.rb +42 -0
  84. data/examples/general/templates.rb +14 -0
  85. data/examples/graphics/basic_images.rb +24 -0
  86. data/examples/graphics/cmyk.rb +13 -0
  87. data/examples/graphics/curves.rb +12 -0
  88. data/examples/graphics/hexagon.rb +14 -0
  89. data/examples/graphics/image_fit.rb +16 -0
  90. data/examples/graphics/image_flow.rb +38 -0
  91. data/examples/graphics/image_position.rb +18 -0
  92. data/examples/graphics/line.rb +33 -0
  93. data/examples/graphics/png_types.rb +23 -0
  94. data/examples/graphics/polygons.rb +17 -0
  95. data/examples/graphics/remote_images.rb +13 -0
  96. data/examples/graphics/rounded_polygons.rb +20 -0
  97. data/examples/graphics/rounded_rectangle.rb +21 -0
  98. data/examples/graphics/ruport_style_helpers.rb +20 -0
  99. data/examples/graphics/stroke_bounds.rb +21 -0
  100. data/examples/graphics/stroke_cap_and_join.rb +46 -0
  101. data/examples/graphics/stroke_dash.rb +43 -0
  102. data/examples/graphics/transformations.rb +53 -0
  103. data/examples/graphics/transparency.rb +27 -0
  104. data/examples/grid/bounding_boxes.rb +22 -0
  105. data/examples/grid/column_gutter_grid.rb +21 -0
  106. data/examples/grid/multi_boxes.rb +52 -0
  107. data/examples/grid/show_grid.rb +14 -0
  108. data/examples/grid/simple_grid.rb +21 -0
  109. data/examples/m17n/chinese_text_wrapping.rb +18 -0
  110. data/examples/m17n/euro.rb +16 -0
  111. data/examples/m17n/sjis.rb +29 -0
  112. data/examples/m17n/utf8.rb +14 -0
  113. data/examples/m17n/win_ansi_charset.rb +55 -0
  114. data/examples/security/hello_foo.rb +9 -0
  115. data/examples/table/bill.rb +54 -0
  116. data/examples/table/cell.rb +13 -0
  117. data/examples/table/checkerboard.rb +23 -0
  118. data/examples/table/header.rb +15 -0
  119. data/examples/table/inline_format_table.rb +13 -0
  120. data/examples/table/multi_page_table.rb +10 -0
  121. data/examples/table/simple_table.rb +25 -0
  122. data/examples/table/subtable.rb +13 -0
  123. data/examples/table/widths.rb +21 -0
  124. data/examples/text/alignment.rb +19 -0
  125. data/examples/text/character_spacing.rb +13 -0
  126. data/examples/text/dfont.rb +49 -0
  127. data/examples/text/family_based_styling.rb +25 -0
  128. data/examples/text/font_calculations.rb +92 -0
  129. data/examples/text/font_size.rb +34 -0
  130. data/examples/text/hyphenation.rb +46 -0
  131. data/examples/text/indent_paragraphs.rb +23 -0
  132. data/examples/text/inline_format.rb +104 -0
  133. data/examples/text/kerning.rb +31 -0
  134. data/examples/text/rotated.rb +99 -0
  135. data/examples/text/shaped_text_box.rb +32 -0
  136. data/examples/text/simple_text.rb +18 -0
  137. data/examples/text/simple_text_ttf.rb +18 -0
  138. data/examples/text/span.rb +30 -0
  139. data/examples/text/text_box.rb +89 -0
  140. data/examples/text/text_box_returning_excess.rb +52 -0
  141. data/examples/text/text_flow.rb +68 -0
  142. data/lib/prawn.rb +26 -0
  143. data/lib/prawn/compatibility.rb +51 -0
  144. data/lib/prawn/core.rb +85 -0
  145. data/lib/prawn/core/annotations.rb +61 -0
  146. data/lib/prawn/core/byte_string.rb +9 -0
  147. data/lib/prawn/core/destinations.rb +90 -0
  148. data/lib/prawn/core/document_state.rb +78 -0
  149. data/lib/prawn/core/literal_string.rb +16 -0
  150. data/lib/prawn/core/name_tree.rb +165 -0
  151. data/lib/prawn/core/object_store.rb +236 -0
  152. data/lib/prawn/core/page.rb +199 -0
  153. data/lib/prawn/core/pdf_object.rb +108 -0
  154. data/lib/prawn/core/reference.rb +112 -0
  155. data/lib/prawn/core/text.rb +140 -0
  156. data/lib/prawn/core/text/formatted/arranger.rb +266 -0
  157. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  158. data/lib/prawn/core/text/formatted/wrap.rb +112 -0
  159. data/lib/prawn/core/text/line_wrap.rb +211 -0
  160. data/lib/prawn/core/text/wrap.rb +82 -0
  161. data/lib/prawn/document.rb +575 -0
  162. data/lib/prawn/document/bounding_box.rb +428 -0
  163. data/lib/prawn/document/graphics_state.rb +48 -0
  164. data/lib/prawn/document/internals.rb +170 -0
  165. data/lib/prawn/document/page_geometry.rb +136 -0
  166. data/lib/prawn/document/snapshot.rb +87 -0
  167. data/lib/prawn/document/span.rb +55 -0
  168. data/lib/prawn/encoding.rb +121 -0
  169. data/lib/prawn/errors.rb +86 -0
  170. data/lib/prawn/font.rb +368 -0
  171. data/lib/prawn/font/afm.rb +225 -0
  172. data/lib/prawn/font/dfont.rb +42 -0
  173. data/lib/prawn/font/ttf.rb +350 -0
  174. data/lib/prawn/graphics.rb +325 -0
  175. data/lib/prawn/graphics/cap_style.rb +38 -0
  176. data/lib/prawn/graphics/color.rb +205 -0
  177. data/lib/prawn/graphics/dash.rb +71 -0
  178. data/lib/prawn/graphics/join_style.rb +38 -0
  179. data/lib/prawn/graphics/transformation.rb +156 -0
  180. data/lib/prawn/graphics/transparency.rb +99 -0
  181. data/lib/prawn/images.rb +217 -0
  182. data/lib/prawn/images/jpg.rb +85 -0
  183. data/lib/prawn/images/png.rb +356 -0
  184. data/lib/prawn/layout.rb +20 -0
  185. data/lib/prawn/layout/grid.rb +259 -0
  186. data/lib/prawn/measurement_extensions.rb +46 -0
  187. data/lib/prawn/measurements.rb +71 -0
  188. data/lib/prawn/outline.rb +326 -0
  189. data/lib/prawn/repeater.rb +129 -0
  190. data/lib/prawn/security.rb +262 -0
  191. data/lib/prawn/security/arcfour.rb +51 -0
  192. data/lib/prawn/stamp.rb +126 -0
  193. data/lib/prawn/table.rb +451 -0
  194. data/lib/prawn/table/cell.rb +395 -0
  195. data/lib/prawn/table/cell/in_table.rb +27 -0
  196. data/lib/prawn/table/cell/subtable.rb +65 -0
  197. data/lib/prawn/table/cell/text.rb +135 -0
  198. data/lib/prawn/table/cells.rb +206 -0
  199. data/lib/prawn/text.rb +449 -0
  200. data/lib/prawn/text/box.rb +397 -0
  201. data/lib/prawn/text/formatted.rb +4 -0
  202. data/lib/prawn/text/formatted/box.rb +222 -0
  203. data/lib/prawn/text/formatted/fragment.rb +181 -0
  204. data/lib/prawn/text/formatted/parser.rb +213 -0
  205. data/prawn.gemspec +28 -0
  206. data/spec/annotations_spec.rb +90 -0
  207. data/spec/bounding_box_spec.rb +190 -0
  208. data/spec/cell_spec.rb +430 -0
  209. data/spec/destinations_spec.rb +15 -0
  210. data/spec/document_spec.rb +476 -0
  211. data/spec/extensions/mocha.rb +32 -0
  212. data/spec/font_spec.rb +324 -0
  213. data/spec/formatted_text_arranger_spec.rb +426 -0
  214. data/spec/formatted_text_box_spec.rb +756 -0
  215. data/spec/formatted_text_fragment_spec.rb +211 -0
  216. data/spec/graphics_spec.rb +446 -0
  217. data/spec/grid_spec.rb +85 -0
  218. data/spec/images_spec.rb +119 -0
  219. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  220. data/spec/jpg_spec.rb +25 -0
  221. data/spec/line_wrap_spec.rb +341 -0
  222. data/spec/measurement_units_spec.rb +23 -0
  223. data/spec/name_tree_spec.rb +112 -0
  224. data/spec/object_store_spec.rb +160 -0
  225. data/spec/outline_spec.rb +404 -0
  226. data/spec/pdf_object_spec.rb +170 -0
  227. data/spec/png_spec.rb +237 -0
  228. data/spec/reference_spec.rb +82 -0
  229. data/spec/repeater_spec.rb +96 -0
  230. data/spec/security_spec.rb +120 -0
  231. data/spec/snapshot_spec.rb +154 -0
  232. data/spec/span_spec.rb +49 -0
  233. data/spec/spec_helper.rb +34 -0
  234. data/spec/stamp_spec.rb +108 -0
  235. data/spec/stroke_styles_spec.rb +163 -0
  236. data/spec/table_spec.rb +687 -0
  237. data/spec/template_spec.rb +165 -0
  238. data/spec/text_at_spec.rb +125 -0
  239. data/spec/text_box_spec.rb +777 -0
  240. data/spec/text_spacing_spec.rb +75 -0
  241. data/spec/text_spec.rb +349 -0
  242. data/spec/text_with_inline_formatting_spec.rb +193 -0
  243. data/spec/transparency_spec.rb +89 -0
  244. data/vendor/pdf-inspector/README +18 -0
  245. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  246. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  247. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  248. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  249. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  250. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  251. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  252. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  253. data/vendor/ttfunk/example.rb +45 -0
  254. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  255. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  256. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  257. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  258. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  260. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  261. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  262. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  263. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  264. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  265. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  266. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  267. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  274. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  275. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  276. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  277. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  278. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  279. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  280. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  281. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  282. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  283. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  284. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  285. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  286. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  287. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  288. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  289. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  290. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  291. metadata +379 -0
@@ -0,0 +1,108 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "create_stamp before any page is added" do
4
+ it "should work with the font class" do
5
+ @pdf = Prawn::Document.new(:skip_page_creation => true)
6
+ lambda {
7
+ @pdf.create_stamp("my_stamp") do
8
+ @pdf.font.height
9
+ end
10
+ }.should.not.raise(Prawn::Errors::NotOnPage)
11
+ end
12
+ it "should work with setting color" do
13
+ @pdf = Prawn::Document.new(:skip_page_creation => true)
14
+ lambda {
15
+ @pdf.create_stamp("my_stamp") do
16
+ @pdf.fill_color = 'ff0000'
17
+ end
18
+ }.should.not.raise(Prawn::Errors::NotOnPage)
19
+ end
20
+ end
21
+
22
+ describe "#stamp_at" do
23
+ it "should work" do
24
+ create_pdf
25
+ @pdf.create_stamp("MyStamp")
26
+ @pdf.stamp_at("MyStamp", [100, 200])
27
+ # I had modified PDF::Inspector::XObject to receive the
28
+ # invoke_xobject message and count the number of times it was
29
+ # called, but it was only called once, so I reverted checking the
30
+ # output with a regular expression
31
+ @pdf.render.should =~ /\/Stamp1 Do.*?/m
32
+ end
33
+ end
34
+
35
+ describe "Document with a stamp" do
36
+ it "should raise NameTaken error when attempt to create stamp "+
37
+ "with same name as an existing stamp" do
38
+ create_pdf
39
+ @pdf.create_stamp("MyStamp")
40
+ lambda {
41
+ @pdf.create_stamp("MyStamp")
42
+ }.should.raise(Prawn::Errors::NameTaken)
43
+ end
44
+
45
+ it "should raise InvalidName error when attempt to create "+
46
+ "stamp with a blank name" do
47
+ create_pdf
48
+ lambda {
49
+ @pdf.create_stamp("")
50
+ }.should.raise(Prawn::Errors::InvalidName)
51
+ end
52
+
53
+ it "a new XObject should be defined for each stamp created" do
54
+ create_pdf
55
+ @pdf.create_stamp("MyStamp")
56
+ @pdf.create_stamp("AnotherStamp")
57
+ @pdf.stamp("MyStamp")
58
+ @pdf.stamp("AnotherStamp")
59
+
60
+ inspector = PDF::Inspector::XObject.analyze(@pdf.render)
61
+ xobjects = inspector.page_xobjects.last
62
+ xobjects.length.should == 2
63
+ end
64
+
65
+ it "calling stamp with a name that does not match an existing stamp "+
66
+ "should raise UndefinedObjectName" do
67
+ create_pdf
68
+ @pdf.create_stamp("MyStamp")
69
+ lambda {
70
+ @pdf.stamp("OtherStamp")
71
+ }.should.raise(Prawn::Errors::UndefinedObjectName)
72
+ end
73
+
74
+ it "stamp should be drawn into the document each time stamp is called" do
75
+ create_pdf
76
+ @pdf.create_stamp("MyStamp")
77
+ @pdf.stamp("MyStamp")
78
+ @pdf.stamp("MyStamp")
79
+ @pdf.stamp("MyStamp")
80
+ # I had modified PDF::Inspector::XObject to receive the
81
+ # invoke_xobject message and count the number of times it was
82
+ # called, but it was only called once, so I reverted checking the
83
+ # output with a regular expression
84
+ @pdf.render.should =~ /(\/Stamp1 Do.*?){3}/m
85
+ end
86
+
87
+ it "resources added during stamp creation should be added to the "+
88
+ "stamp XObject, not the page" do
89
+ create_pdf
90
+ @pdf.create_stamp("MyStamp") do
91
+ @pdf.transparent(0.5) { @pdf.circle_at([100, 100], :radius => 10)}
92
+ end
93
+ @pdf.stamp("MyStamp")
94
+
95
+ # Inspector::XObject does not give information about resources, so
96
+ # resorting to string matching
97
+
98
+ output = @pdf.render
99
+ objects = output.split("endobj")
100
+ objects.each do |object|
101
+ if object =~ /\/Type \/Page$/
102
+ object.should.not =~ /\/ExtGState/
103
+ elsif object =~ /\/Type \/XObject$/
104
+ object.should =~ /\/ExtGState/
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,163 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "When stroking with default settings" do
6
+ before(:each) { create_pdf }
7
+ it "cap_style should be :butt" do
8
+ @pdf.cap_style.should == :butt
9
+ end
10
+
11
+ it "join_style should be :miter" do
12
+ @pdf.join_style.should == :miter
13
+ end
14
+
15
+ it "dashed? should be false" do
16
+ @pdf.should.not.be.dashed
17
+ end
18
+ end
19
+
20
+ describe "Cap styles" do
21
+ before(:each) { create_pdf }
22
+
23
+ it "should be able to use assignment operator" do
24
+ @pdf.cap_style = :round
25
+ @pdf.cap_style.should == :round
26
+ end
27
+
28
+ describe "#cap_style(:butt)" do
29
+ it "rendered PDF should include butt style cap" do
30
+ @pdf.cap_style(:butt)
31
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
32
+ cap_style.should == 0
33
+ end
34
+ end
35
+
36
+ describe "#cap_style(:round)" do
37
+ it "rendered PDF should include round style cap" do
38
+ @pdf.cap_style(:round)
39
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
40
+ cap_style.should == 1
41
+ end
42
+ end
43
+
44
+ describe "#cap_style(:projecting_square)" do
45
+ it "rendered PDF should include projecting_square style cap" do
46
+ @pdf.cap_style(:projecting_square)
47
+ cap_style = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render).cap_style
48
+ cap_style.should == 2
49
+ end
50
+ end
51
+
52
+ it "should carry the current cap style settings over to new pages" do
53
+ @pdf.cap_style(:round)
54
+ @pdf.start_new_page
55
+ cap_styles = PDF::Inspector::Graphics::CapStyle.analyze(@pdf.render)
56
+ cap_styles.cap_style_count.should == 2
57
+ cap_styles.cap_style.should == 1
58
+ end
59
+ end
60
+
61
+ describe "Join styles" do
62
+ before(:each) { create_pdf }
63
+
64
+ it "should be able to use assignment operator" do
65
+ @pdf.join_style = :round
66
+ @pdf.join_style.should == :round
67
+ end
68
+
69
+ describe "#join_style(:miter)" do
70
+ it "rendered PDF should include miter style join" do
71
+ @pdf.join_style(:miter)
72
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
73
+ join_style.should == 0
74
+ end
75
+ end
76
+
77
+ describe "#join_style(:round)" do
78
+ it "rendered PDF should include round style join" do
79
+ @pdf.join_style(:round)
80
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
81
+ join_style.should == 1
82
+ end
83
+ end
84
+
85
+ describe "#join_style(:bevel)" do
86
+ it "rendered PDF should include bevel style join" do
87
+ @pdf.join_style(:bevel)
88
+ join_style = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render).join_style
89
+ join_style.should == 2
90
+ end
91
+ end
92
+
93
+ it "should carry the current join style settings over to new pages" do
94
+ @pdf.join_style(:round)
95
+ @pdf.start_new_page
96
+ join_styles = PDF::Inspector::Graphics::JoinStyle.analyze(@pdf.render)
97
+ join_styles.join_style_count.should == 2
98
+ join_styles.join_style.should == 1
99
+ end
100
+ end
101
+
102
+ describe "Dashes" do
103
+ before(:each) { create_pdf }
104
+
105
+ it "should be able to use assignment operator" do
106
+ @pdf.dash = 2
107
+ @pdf.should.be.dashed
108
+ end
109
+
110
+ describe "setting a dash" do
111
+ it "dashed? should be true" do
112
+ @pdf.dash(2)
113
+ @pdf.should.be.dashed
114
+ end
115
+ it "rendered PDF should include a stroked dash" do
116
+ @pdf.dash(2)
117
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
118
+ dashes.stroke_dash.should == [[2, 2], 0]
119
+ end
120
+ end
121
+
122
+ describe "setting a dash by passing a single argument" do
123
+ it "space between dashes should be the same length as the dash in the rendered PDF" do
124
+ @pdf.dash(2)
125
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
126
+ dashes.stroke_dash.should == [[2, 2], 0]
127
+ end
128
+ end
129
+
130
+ describe "with a space option that differs from the first argument" do
131
+ it "space between dashes in the rendered PDF should be different length than the length of the dash" do
132
+ @pdf.dash(2, :space => 3)
133
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
134
+ dashes.stroke_dash.should == [[2, 3], 0]
135
+ end
136
+ end
137
+
138
+ describe "with a non-zero phase option" do
139
+ it "rendered PDF should include a non-zero phase" do
140
+ @pdf.dash(2, :phase => 1)
141
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
142
+ dashes.stroke_dash.should == [[2, 2], 1]
143
+ end
144
+ end
145
+
146
+ describe "clearing stroke dash" do
147
+ it "should restore solid line" do
148
+ @pdf.dash(2)
149
+ @pdf.undash
150
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
151
+ dashes.stroke_dash.should == [[], 0]
152
+ end
153
+ end
154
+
155
+ it "should carry the current dash settings over to new pages" do
156
+ @pdf.dash(2)
157
+ @pdf.start_new_page
158
+ dashes = PDF::Inspector::Graphics::Dash.analyze(@pdf.render)
159
+ dashes.stroke_dash_count.should == 2
160
+ dashes.stroke_dash.should == [[2, 2], 0]
161
+ end
162
+
163
+ end
@@ -0,0 +1,687 @@
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
+ it "should allow a table with a header but no body" do
36
+ lambda { @pdf.table([["Header"]], :header => true) }.should.not.raise
37
+ end
38
+
39
+ # TODO: pending colspan
40
+ xit "should accurately count columns from data" do
41
+ # First data row may contain colspan which would hide true column count
42
+ data = [["Name:",{:text => "Some very long name", :colspan => 5}]]
43
+ pdf = Prawn::Document.new
44
+ table = Prawn::Table.new data, pdf
45
+ table.column_widths.length.should == 6
46
+ end
47
+ end
48
+
49
+ describe "#initialize" do
50
+ before(:each) do
51
+ @pdf = Prawn::Document.new
52
+ end
53
+
54
+ it "should instance_eval a 0-arg block" do
55
+ initializer = mock()
56
+ initializer.expects(:kick).once
57
+
58
+ @pdf.table([["a"]]){
59
+ self.should.be.an.instance_of(Prawn::Table); initializer.kick }
60
+ end
61
+
62
+ it "should call a 1-arg block with the document as the argument" do
63
+ initializer = mock()
64
+ initializer.expects(:kick).once
65
+
66
+ @pdf.table([["a"]]){ |doc|
67
+ doc.should.be.an.instance_of(Prawn::Table); initializer.kick }
68
+ end
69
+
70
+ it "should proxy cell methods to #cells" do
71
+ table = @pdf.table([["a"]], :cell_style => { :padding => 11 })
72
+ table.cells[0, 0].padding.should == [11, 11, 11, 11]
73
+ end
74
+
75
+ it "should set row and column length" do
76
+ table = @pdf.table([["a", "b", "c"], ["d", "e", "f"]])
77
+ table.row_length.should == 2
78
+ table.column_length.should == 3
79
+ end
80
+
81
+ it "should generate a text cell based on a String" do
82
+ t = @pdf.table([["foo"]])
83
+ t.cells[0,0].should.be.a.kind_of(Prawn::Table::Cell::Text)
84
+ end
85
+
86
+ it "should pass through a text cell" do
87
+ c = Prawn::Table::Cell::Text.new(@pdf, [0,0], :content => "foo")
88
+ t = @pdf.table([[c]])
89
+ t.cells[0,0].should == c
90
+ end
91
+ end
92
+
93
+ describe "cell accessors" do
94
+ before(:each) do
95
+ @pdf = Prawn::Document.new
96
+ @table = @pdf.table([%w[R0C0 R0C1], %w[R1C0 R1C1]])
97
+ end
98
+
99
+ it "should select rows by number or range" do
100
+ Set.new(@table.row(0).map { |c| c.content }).should ==
101
+ Set.new(%w[R0C0 R0C1])
102
+ Set.new(@table.rows(0..1).map { |c| c.content }).should ==
103
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
104
+ end
105
+
106
+ it "should allow negative row selectors" do
107
+ Set.new(@table.row(-1).map { |c| c.content }).should ==
108
+ Set.new(%w[R1C0 R1C1])
109
+ Set.new(@table.rows(-2..-1).map { |c| c.content }).should ==
110
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
111
+ Set.new(@table.rows(0..-1).map { |c| c.content }).should ==
112
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
113
+ end
114
+
115
+ it "should select columns by number or range" do
116
+ Set.new(@table.column(0).map { |c| c.content }).should ==
117
+ Set.new(%w[R0C0 R1C0])
118
+ Set.new(@table.columns(0..1).map { |c| c.content }).should ==
119
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
120
+ end
121
+
122
+ it "should allow negative column selectors" do
123
+ Set.new(@table.column(-1).map { |c| c.content }).should ==
124
+ Set.new(%w[R0C1 R1C1])
125
+ Set.new(@table.columns(-2..-1).map { |c| c.content }).should ==
126
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
127
+ Set.new(@table.columns(0..-1).map { |c| c.content }).should ==
128
+ Set.new(%w[R0C0 R0C1 R1C0 R1C1])
129
+ end
130
+
131
+ it "should allow rows and columns to be combined" do
132
+ @table.row(0).column(1).map { |c| c.content }.should == ["R0C1"]
133
+ end
134
+
135
+ it "should accept a filter block, returning a cell proxy" do
136
+ @table.cells.filter { |c| c.content =~ /R0/ }.column(1).map{ |c|
137
+ c.content }.should == ["R0C1"]
138
+ end
139
+
140
+ it "should accept the [] method, returning a Cell or nil" do
141
+ @table.cells[0, 0].content.should == "R0C0"
142
+ @table.cells[12, 12].should.be.nil
143
+ end
144
+
145
+ it "should proxy unknown methods to the cells" do
146
+ @table.cells.height = 200
147
+ @table.row(1).height = 100
148
+
149
+ @table.cells[0, 0].height.should == 200
150
+ @table.cells[1, 0].height.should == 100
151
+ end
152
+
153
+ it "should accept the style method, proxying its calls to the cells" do
154
+ @table.cells.style(:height => 200, :width => 200)
155
+ @table.column(0).style(:width => 100)
156
+
157
+ @table.cells[0, 1].width.should == 200
158
+ @table.cells[1, 0].height.should == 200
159
+ @table.cells[1, 0].width.should == 100
160
+ end
161
+
162
+ it "style method should accept a block, passing each cell to be styled" do
163
+ @table.cells.style { |c| c.height = 200 }
164
+ @table.cells[0, 1].height.should == 200
165
+ end
166
+
167
+ it "should return the width of selected columns for #width" do
168
+ c0_width = @table.column(0).map{ |c| c.width }.max
169
+ c1_width = @table.column(1).map{ |c| c.width }.max
170
+
171
+ @table.column(0).width.should == c0_width
172
+ @table.column(1).width.should == c1_width
173
+
174
+ @table.columns(0..1).width.should == c0_width + c1_width
175
+ @table.cells.width.should == c0_width + c1_width
176
+ end
177
+
178
+ it "should return the height of selected rows for #height" do
179
+ r0_height = @table.row(0).map{ |c| c.height }.max
180
+ r1_height = @table.row(1).map{ |c| c.height }.max
181
+
182
+ @table.row(0).height.should == r0_height
183
+ @table.row(1).height.should == r1_height
184
+
185
+ @table.rows(0..1).height.should == r0_height + r1_height
186
+ @table.cells.height.should == r0_height + r1_height
187
+ end
188
+ end
189
+
190
+ describe "layout" do
191
+ before(:each) do
192
+ @pdf = Prawn::Document.new
193
+ @long_text = "The quick brown fox jumped over the lazy dogs. " * 5
194
+ end
195
+
196
+ describe "width" do
197
+ it "should raise an error if the given width is outside of range" do
198
+ lambda do
199
+ @pdf.table([["foo"]], :width => 1)
200
+ end.should.raise(Prawn::Errors::CannotFit)
201
+
202
+ lambda do
203
+ @pdf.table([[@long_text]], :width => @pdf.bounds.width + 100)
204
+ end.should.raise(Prawn::Errors::CannotFit)
205
+ end
206
+
207
+ it "should accept the natural width for small tables" do
208
+ pad = 10 # default padding
209
+ @table = @pdf.table([["a"]])
210
+ @table.width.should == @table.cells[0, 0].natural_content_width + pad
211
+ end
212
+
213
+ it "width should equal sum(column_widths)" do
214
+ table = Prawn::Table.new([%w[ a b c ], %w[d e f]], @pdf) do
215
+ column(0).width = 50
216
+ column(1).width = 100
217
+ column(2).width = 150
218
+ end
219
+ table.width.should == 300
220
+ end
221
+
222
+ it "should calculate unspecified column widths as "+
223
+ "(max(string_width) + 2*horizontal_padding)" do
224
+ hpad, fs = 3, 12
225
+ columns = 2
226
+ table = Prawn::Table.new( [%w[ foo b ], %w[d foobar]], @pdf,
227
+ :cell_style => { :padding => hpad, :size => fs } )
228
+
229
+ col0_width = @pdf.width_of("foo", :size => fs)
230
+ col1_width = @pdf.width_of("foobar", :size => fs)
231
+
232
+ table.width.should == col0_width + col1_width + 2*columns*hpad
233
+ end
234
+
235
+ it "should allow mixing autocalculated and preset"+
236
+ "column widths within a single table" do
237
+ hpad, fs = 10, 6
238
+ stretchy_columns = 2
239
+
240
+ col0_width = 50
241
+ col1_width = @pdf.width_of("foo", :size => fs)
242
+ col2_width = @pdf.width_of("foobar", :size => fs)
243
+ col3_width = 150
244
+
245
+ table = Prawn::Table.new( [%w[snake foo b apple],
246
+ %w[kitten d foobar banana]], @pdf,
247
+ :cell_style => { :padding => hpad, :size => fs }) do
248
+
249
+ column(0).width = col0_width
250
+ column(3).width = col3_width
251
+ end
252
+
253
+ table.width.should == col1_width + col2_width +
254
+ 2*stretchy_columns*hpad +
255
+ col0_width + col3_width
256
+ end
257
+
258
+ it "should not exceed the maximum width of the margin_box" do
259
+ expected_width = @pdf.margin_box.width
260
+ data = [
261
+ ['This is a column with a lot of text that should comfortably exceed '+
262
+ 'the width of a normal document margin_box width', 'Some more text',
263
+ 'and then some more', 'Just a bit more to be extra sure']
264
+ ]
265
+ table = Prawn::Table.new(data, @pdf)
266
+
267
+ table.width.should == expected_width
268
+ end
269
+
270
+ it "should not exceed the maximum width of the margin_box even with" +
271
+ "manual widths specified" do
272
+ expected_width = @pdf.margin_box.width
273
+ data = [
274
+ ['This is a column with a lot of text that should comfortably exceed '+
275
+ 'the width of a normal document margin_box width', 'Some more text',
276
+ 'and then some more', 'Just a bit more to be extra sure']
277
+ ]
278
+ table = Prawn::Table.new(data, @pdf) { column(1).width = 100 }
279
+
280
+ table.width.should == expected_width
281
+ end
282
+
283
+ it "scales down only the non-preset column widths when the natural width" +
284
+ "exceeds the maximum width of the margin_box" do
285
+ expected_width = @pdf.margin_box.width
286
+ data = [
287
+ ['This is a column with a lot of text that should comfortably exceed '+
288
+ 'the width of a normal document margin_box width', 'Some more text',
289
+ 'and then some more', 'Just a bit more to be extra sure']
290
+ ]
291
+ table = Prawn::Table.new(data, @pdf) { column(1).width = 100; column(3).width = 50 }
292
+
293
+ table.width.should == expected_width
294
+ table.column_widths[1].should == 100
295
+ table.column_widths[3].should == 50
296
+ end
297
+
298
+ it "should allow width to be reset even after it has been calculated" do
299
+ @table = @pdf.table([[@long_text]])
300
+ @table.width
301
+ @table.width = 100
302
+ @table.width.should == 100
303
+ end
304
+
305
+ it "should shrink columns evenly when two equal columns compete" do
306
+ @table = @pdf.table([["foo", @long_text], [@long_text, "foo"]])
307
+ @table.cells[0, 0].width.should == @table.cells[0, 1].width
308
+ end
309
+
310
+ it "should grow columns evenly when equal deficient columns compete" do
311
+ @table = @pdf.table([["foo", "foobar"], ["foobar", "foo"]], :width => 500)
312
+ @table.cells[0, 0].width.should == @table.cells[0, 1].width
313
+ end
314
+
315
+ it "should respect manual widths" do
316
+ @table = @pdf.table([%w[foo bar baz], %w[baz bar foo]], :width => 500) do
317
+ column(1).width = 60
318
+ end
319
+ @table.column(1).width.should == 60
320
+ @table.column(0).width.should == @table.column(2).width
321
+ end
322
+
323
+ it "should allow table cells to be resized in block" do
324
+ lambda do
325
+ @pdf.table([%w[1 2 3 4 5]]) do |t|
326
+ t.width = 40
327
+ t.cells.size = 8
328
+ t.cells.padding = 0
329
+ end
330
+ end.should.not.raise(Prawn::Errors::CannotFit)
331
+ end
332
+
333
+ it "should be the width of the :width parameter" do
334
+ expected_width = 300
335
+ table = Prawn::Table.new( [%w[snake foo b apple],
336
+ %w[kitten d foobar banana]], @pdf,
337
+ :width => expected_width)
338
+
339
+ table.width.should == expected_width
340
+ end
341
+
342
+ it "should not exceed the :width option" do
343
+ expected_width = 400
344
+ data = [
345
+ ['This is a column with a lot of text that should comfortably exceed '+
346
+ 'the width of a normal document margin_box width', 'Some more text',
347
+ 'and then some more', 'Just a bit more to be extra sure']
348
+ ]
349
+ table = Prawn::Table.new(data, @pdf, :width => expected_width)
350
+
351
+ table.width.should == expected_width
352
+ end
353
+
354
+ it "should not exceed the :width option even with manual widths specified" do
355
+ expected_width = 400
356
+ data = [
357
+ ['This is a column with a lot of text that should comfortably exceed '+
358
+ 'the width of a normal document margin_box width', 'Some more text',
359
+ 'and then some more', 'Just a bit more to be extra sure']
360
+ ]
361
+ table = Prawn::Table.new(data, @pdf, :width => expected_width) do
362
+ column(1).width = 100
363
+ end
364
+
365
+ table.width.should == expected_width
366
+ end
367
+
368
+ # TODO: pending colspan
369
+ xit "should calculate unspecified column widths even " +
370
+ "with colspan cells declared" do
371
+ pdf = Prawn::Document.new
372
+ hpad, fs = 3, 5
373
+ columns = 3
374
+
375
+ data = [ [ { :text => 'foo', :colspan => 2 }, "foobar" ],
376
+ [ "foo", "foo", "foo" ] ]
377
+ table = Prawn::Table.new( data, pdf,
378
+ :horizontal_padding => hpad,
379
+ :font_size => fs )
380
+
381
+ col0_width = pdf.width_of("foo", :size => fs) # cell 1, 0
382
+ col1_width = pdf.width_of("foo", :size => fs) # cell 1, 1
383
+ col2_width = pdf.width_of("foobar", :size => fs) # cell 0, 1 (at col 2)
384
+
385
+ table.width.should == col0_width.ceil + col1_width.ceil +
386
+ col2_width.ceil + 2*columns*hpad
387
+ end
388
+ end
389
+
390
+ describe "height" do
391
+ it "should set all cells in a row to the same height" do
392
+ @table = @pdf.table([["foo", @long_text]])
393
+ @table.cells[0, 0].height.should == @table.cells[0, 1].height
394
+ end
395
+
396
+ it "should move y-position to the bottom of the table after drawing" do
397
+ old_y = @pdf.y
398
+ table = @pdf.table([["foo"]])
399
+ @pdf.y.should == old_y - table.height
400
+ end
401
+
402
+ it "should not wrap unnecessarily" do
403
+ # Test for FP errors and glitches
404
+ t = @pdf.table([["Bender Bending Rodriguez"]])
405
+ h = @pdf.height_of("one line")
406
+ (t.height - 10).should.be < h*1.5
407
+ end
408
+
409
+ it "should have a height of n rows" do
410
+ data = [["foo"],["bar"],["baaaz"]]
411
+
412
+ vpad = 4
413
+ origin = @pdf.y
414
+ @pdf.table data, :cell_style => { :padding => vpad }
415
+
416
+ table_height = origin - @pdf.y
417
+ font_height = @pdf.font.height
418
+
419
+ num_rows = data.length
420
+ table_height.should.be.close(
421
+ num_rows*font_height + 2*vpad*num_rows, 0.001 )
422
+ end
423
+
424
+ end
425
+
426
+ end
427
+
428
+ describe "Multi-page tables" do
429
+ it "should flow to the next page when hitting the bottom of the bounds" do
430
+ Prawn::Document.new { table([["foo"]] * 30) }.page_count.should == 1
431
+ Prawn::Document.new { table([["foo"]] * 31) }.page_count.should == 2
432
+ Prawn::Document.new { table([["foo"]] * 31); table([["foo"]] * 31) }.
433
+ page_count.should == 3
434
+ end
435
+
436
+ it "should respect the containing bounds" do
437
+ Prawn::Document.new do
438
+ bounding_box([0, cursor], :width => bounds.width, :height => 72) do
439
+ table([["foo"]] * 4)
440
+ end
441
+ end.page_count.should == 2
442
+ end
443
+
444
+ it "should not start a new page before finishing out a row" do
445
+ Prawn::Document.new do
446
+ table([[ (1..80).map{ |i| "Line #{i}" }.join("\n"), "Column 2" ]])
447
+ end.page_count.should == 1
448
+ end
449
+
450
+ it "should only start new page on long cells if it would gain us height" do
451
+ Prawn::Document.new do
452
+ text "Hello"
453
+ table([[ (1..80).map{ |i| "Line #{i}" }.join("\n"), "Column 2" ]])
454
+ end.page_count.should == 2
455
+ end
456
+
457
+ it "should not start a new page to gain height when at the top of " +
458
+ "a bounding box, even if stretchy" do
459
+ Prawn::Document.new do
460
+ bounding_box([bounds.left, bounds.top - 20], :width => 400) do
461
+ table([[ (1..80).map{ |i| "Line #{i}" }.join("\n"), "Column 2" ]])
462
+ end
463
+ end.page_count.should == 1
464
+ end
465
+
466
+ it "should still break to the next page if in a stretchy bounding box " +
467
+ "but not at the top" do
468
+ Prawn::Document.new do
469
+ bounding_box([bounds.left, bounds.top - 20], :width => 400) do
470
+ text "Hello"
471
+ table([[ (1..80).map{ |i| "Line #{i}" }.join("\n"), "Column 2" ]])
472
+ end
473
+ end.page_count.should == 2
474
+ end
475
+
476
+ it "should only draw first-page header if the first body row fits" do
477
+ pdf = Prawn::Document.new
478
+
479
+ pdf.y = 60 # not enough room for a table row
480
+ pdf.table [["Header"], ["Body"]], :header => true
481
+
482
+ output = PDF::Inspector::Page.analyze(pdf.render)
483
+ # Ensure we only drew the header once, on the second page
484
+ output.pages[0][:strings].should.be.empty
485
+ output.pages[1][:strings].should == ["Header", "Body"]
486
+ end
487
+ end
488
+
489
+ describe "#style" do
490
+ it "should send #style to its first argument, passing the style hash and" +
491
+ " block" do
492
+
493
+ stylable = stub()
494
+ stylable.expects(:style).with(:foo => :bar).once.yields
495
+
496
+ block = stub()
497
+ block.expects(:kick).once
498
+
499
+ Prawn::Document.new do
500
+ table([["x"]]) { style(stylable, :foo => :bar) { block.kick } }
501
+ end
502
+ end
503
+
504
+ it "should default to {} for the hash argument" do
505
+ stylable = stub()
506
+ stylable.expects(:style).with({}).once
507
+
508
+ Prawn::Document.new do
509
+ table([["x"]]) { style(stylable) }
510
+ end
511
+ end
512
+ end
513
+
514
+ describe "row_colors" do
515
+ it "should allow array syntax for :row_colors" do
516
+ data = [["foo"], ["bar"], ["baz"]]
517
+ pdf = Prawn::Document.new
518
+ t = pdf.table(data, :row_colors => ['cccccc', 'ffffff'])
519
+ t.cells.map{|x| x.background_color}.should == %w[cccccc ffffff cccccc]
520
+ end
521
+
522
+ it "should ignore headers" do
523
+ data = [["header"], ["foo"], ["bar"], ["baz"]]
524
+ pdf = Prawn::Document.new
525
+ t = pdf.table(data, :header => true,
526
+ :row_colors => ['cccccc', 'ffffff']) do
527
+ row(0).background_color = '333333'
528
+ end
529
+
530
+ t.cells.map{|x| x.background_color}.should ==
531
+ %w[333333 cccccc ffffff cccccc]
532
+ end
533
+ end
534
+
535
+ describe "inking" do
536
+ before(:each) do
537
+ @pdf = Prawn::Document.new
538
+ end
539
+
540
+ it "should set the x-position of each cell based on widths" do
541
+ @table = @pdf.table([["foo", "bar", "baz"]])
542
+
543
+ x = 0
544
+ (0..2).each do |col|
545
+ cell = @table.cells[0, col]
546
+ cell.x.should == x
547
+ x += cell.width
548
+ end
549
+ end
550
+
551
+ it "should set the y-position of each cell based on heights" do
552
+ y = 0
553
+ @table = @pdf.make_table([["foo"], ["bar"], ["baz"]])
554
+
555
+ (0..2).each do |row|
556
+ cell = @table.cells[row, 0]
557
+ cell.y.should.be.close(y, 0.01)
558
+ y -= cell.height
559
+ end
560
+ end
561
+
562
+ it "should output content cell by cell, row by row" do
563
+ data = [["foo","bar"],["baz","bang"]]
564
+ @pdf = Prawn::Document.new
565
+ @pdf.table(data)
566
+ output = PDF::Inspector::Text.analyze(@pdf.render)
567
+ output.strings.should == data.flatten
568
+ end
569
+
570
+ it "should not cause an error if rendering the very first row causes a " +
571
+ "page break" do
572
+ Prawn::Document.new do
573
+ arr = Array(1..5).collect{|i| ["cell #{i}"] }
574
+
575
+ move_down( y - (bounds.absolute_bottom + 3) )
576
+
577
+ lambda {
578
+ table(arr)
579
+ }.should.not.raise
580
+ end
581
+ end
582
+
583
+ it "should allow multiple inkings of the same table" do
584
+ pdf = Prawn::Document.new
585
+ t = Prawn::Table.new([["foo"]], pdf)
586
+
587
+ pdf.expects(:bounding_box).with{|(x, y), options| y.to_i == 495}.yields
588
+ pdf.expects(:bounding_box).with{|(x, y), options| y.to_i == 395}.yields
589
+ pdf.expects(:draw_text!).with{ |text, options| text == 'foo' }.twice
590
+
591
+ pdf.move_cursor_to(500)
592
+ t.draw
593
+
594
+ pdf.move_cursor_to(400)
595
+ t.draw
596
+ end
597
+ end
598
+
599
+ describe "headers" do
600
+ it "should add headers to output when specified" do
601
+ data = [["a", "b"], ["foo","bar"],["baz","bang"]]
602
+ @pdf = Prawn::Document.new
603
+ @pdf.table(data, :header => true)
604
+ output = PDF::Inspector::Text.analyze(@pdf.render)
605
+ output.strings.should == data.flatten
606
+ end
607
+
608
+ it "should repeat headers across pages" do
609
+ data = [["foo","bar"]]*30
610
+ headers = ["baz","foobar"]
611
+ @pdf = Prawn::Document.new
612
+ @pdf.table([headers] + data, :header => true)
613
+ output = PDF::Inspector::Text.analyze(@pdf.render)
614
+ output.strings.should == headers + data.flatten[0..-3] + headers +
615
+ data.flatten[-2..-1]
616
+ end
617
+
618
+ it "should not draw header twice when starting new page" do
619
+ @pdf = Prawn::Document.new
620
+ @pdf.y = 0
621
+ @pdf.table([["Header"], ["Body"]], :header => true)
622
+ output = PDF::Inspector::Text.analyze(@pdf.render)
623
+ output.strings.should == ["Header", "Body"]
624
+ end
625
+ end
626
+
627
+ describe "nested tables" do
628
+ before(:each) do
629
+ @pdf = Prawn::Document.new
630
+ @subtable = Prawn::Table.new([["foo"]], @pdf)
631
+ @table = @pdf.table([[@subtable, "bar"]])
632
+ end
633
+
634
+ it "can be created from an Array" do
635
+ cell = Prawn::Table::Cell.make(@pdf, [["foo"]])
636
+ cell.should.be.an.instance_of(Prawn::Table::Cell::Subtable)
637
+ cell.subtable.should.be.an.instance_of(Prawn::Table)
638
+ end
639
+
640
+ it "defaults its padding to zero" do
641
+ @table.cells[0, 0].padding.should == [0, 0, 0, 0]
642
+ end
643
+
644
+ it "has a subtable accessor" do
645
+ @table.cells[0, 0].subtable.should == @subtable
646
+ end
647
+
648
+ it "determines its dimensions from the subtable" do
649
+ @table.cells[0, 0].width.should == @subtable.width
650
+ @table.cells[0, 0].height.should == @subtable.height
651
+ end
652
+
653
+ end
654
+
655
+ describe "An invalid table" do
656
+
657
+ before(:each) do
658
+ @pdf = Prawn::Document.new
659
+ @bad_data = ["Single Nested Array"]
660
+ end
661
+
662
+ it "should raise error when invalid table data is given" do
663
+ assert_raises(Prawn::Errors::InvalidTableData) do
664
+ @pdf.table(@bad_data)
665
+ end
666
+ end
667
+
668
+ it "should raise an EmptyTableError with empty table data" do
669
+ lambda {
670
+ data = []
671
+ @pdf = Prawn::Document.new
672
+ @pdf.table(data)
673
+ }.should.raise( Prawn::Errors::EmptyTable )
674
+ end
675
+
676
+ it "should raise an EmptyTableError with nil table data" do
677
+ lambda {
678
+ data = nil
679
+ @pdf = Prawn::Document.new
680
+ @pdf.table(data)
681
+ }.should.raise( Prawn::Errors::EmptyTable )
682
+ end
683
+
684
+ end
685
+
686
+ end
687
+