nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,134 @@
1
+ # encoding: utf-8
2
+ #
3
+ # stamp.rb : Implements a repeatable stamp
4
+ #
5
+ # Copyright October 2009, Daniel Nelson. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ #
9
+
10
+ module Prawn
11
+
12
+ # The Prawn::Stamp module is used to create content that will be
13
+ # included multiple times in a document. Using a stamp has three
14
+ # advantages over creating content anew each time it is placed on
15
+ # the page:
16
+ # i. faster document creation
17
+ # ii. smaller final document
18
+ # iii. faster display on subsequent displays of the repeated
19
+ # element because the viewer application can cache the rendered
20
+ # results
21
+ #
22
+ # Example:
23
+ # pdf.create_stamp("my_stamp") {
24
+ # pdf.fill_circle([10, 15], 5)
25
+ # pdf.draw_text("hello world", :at => [20, 10])
26
+ # }
27
+ # pdf.stamp("my_stamp")
28
+ #
29
+ module Stamp
30
+
31
+ # Renders the stamp named <tt>name</tt> to the page
32
+ # raises <tt>Prawn::Errors::InvalidName</tt> if name.empty?
33
+ # raises <tt>Prawn::Errors::UndefinedObjectName</tt> if no stamp
34
+ # has been created with this name
35
+ #
36
+ # Example:
37
+ # pdf.create_stamp("my_stamp") {
38
+ # pdf.fill_circle([10, 15], 5)
39
+ # pdf.text("hello world", :at => [20, 10])
40
+ # }
41
+ # pdf.stamp("my_stamp")
42
+ #
43
+ def stamp(name)
44
+ dictionary_name, dictionary = stamp_dictionary(name)
45
+ add_content "/#{dictionary_name} Do"
46
+ state.page.xobjects.merge!(dictionary_name => dictionary)
47
+ end
48
+
49
+ # Renders the stamp named <tt>name</tt> at a position offset from
50
+ # the initial coords at which the elements of the stamp was
51
+ # created
52
+ #
53
+ # Example:
54
+ # pdf.create_stamp("circle") do
55
+ # pdf.fill_circle([0, 0], 25)
56
+ # end
57
+ # # draws a circle at 100, 100
58
+ # pdf.stamp_at("circle", [100, 100])
59
+ #
60
+ # See stamp() for exceptions that might be raised
61
+ #
62
+ def stamp_at(name, point)
63
+ translate(point[0], point[1]) { stamp(name) }
64
+ end
65
+
66
+ # Creates a re-usable stamp named <tt>name</tt>
67
+ #
68
+ # raises <tt>Prawn::Errors::NameTaken</tt> if a stamp already
69
+ # exists in this document with this name
70
+ # raises <tt>Prawn::Errors::InvalidName</tt> if name.empty?
71
+ #
72
+ # Example:
73
+ # pdf.create_stamp("my_stamp") {
74
+ # pdf.fill_circle([10, 15], 5)
75
+ # pdf.draw_text("hello world", :at => [20, 10])
76
+ # }
77
+ #
78
+ def create_stamp(name, &block)
79
+ dictionary = create_stamp_dictionary(name)
80
+
81
+ state.page.stamp_stream(dictionary, &block)
82
+ end
83
+
84
+ private
85
+
86
+ def stamp_dictionary_registry
87
+ @stamp_dictionary_registry ||= {}
88
+ end
89
+
90
+ def next_stamp_dictionary_id
91
+ stamp_dictionary_registry.length + 1
92
+ end
93
+
94
+ def stamp_dictionary(name)
95
+ raise Prawn::Errors::InvalidName if name.empty?
96
+ if stamp_dictionary_registry[name].nil?
97
+ raise Prawn::Errors::UndefinedObjectName
98
+ end
99
+
100
+ dict = stamp_dictionary_registry[name]
101
+
102
+ dictionary_name = dict[:stamp_dictionary_name]
103
+ dictionary = dict[:stamp_dictionary]
104
+ [dictionary_name, dictionary]
105
+ end
106
+
107
+ def create_stamp_dictionary(name)
108
+ raise Prawn::Errors::InvalidName if name.empty?
109
+ raise Prawn::Errors::NameTaken unless stamp_dictionary_registry[name].nil?
110
+ # BBox origin is the lower left margin of the page, so we need
111
+ # it to be the full dimension of the page, or else things that
112
+ # should appear near the top or right margin are invisible
113
+ dictionary = ref!(:Type => :XObject,
114
+ :Subtype => :Form,
115
+ :BBox => [0, 0,
116
+ state.page.dimensions[2], state.page.dimensions[3]])
117
+
118
+ dictionary_name = "Stamp#{next_stamp_dictionary_id}"
119
+
120
+ stamp_dictionary_registry[name] = { :stamp_dictionary_name => dictionary_name,
121
+ :stamp_dictionary => dictionary }
122
+ dictionary
123
+ end
124
+
125
+ def freeze_stamp_graphics
126
+ update_colors
127
+ write_line_width
128
+ write_stroke_cap_style
129
+ write_stroke_join_style
130
+ write_stroke_dash
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,609 @@
1
+ # encoding: utf-8
2
+ #
3
+ # table.rb: Table drawing functionality.
4
+ #
5
+ # Copyright December 2009, Brad Ediger. All rights reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require 'prawn/table/cells'
10
+ require 'prawn/table/cell'
11
+ require 'prawn/table/cell/in_table'
12
+ require 'prawn/table/cell/text'
13
+ require 'prawn/table/cell/subtable'
14
+ require 'prawn/table/cell/image'
15
+ require 'prawn/table/cell/span_dummy'
16
+
17
+ module Prawn
18
+
19
+ class Document
20
+
21
+ # Set up and draw a table on this document. A block can be given, which will
22
+ # be run after cell setup but before layout and drawing.
23
+ #
24
+ # See the documentation on Prawn::Table for details on the arguments.
25
+ #
26
+ def table(data, options={}, &block)
27
+ t = Table.new(data, self, options, &block)
28
+ t.draw
29
+ t
30
+ end
31
+
32
+ # Set up, but do not draw, a table. Useful for creating subtables to be
33
+ # inserted into another Table. Call +draw+ on the resulting Table to ink it.
34
+ #
35
+ # See the documentation on Prawn::Table for details on the arguments.
36
+ #
37
+ def make_table(data, options={}, &block)
38
+ Table.new(data, self, options, &block)
39
+ end
40
+
41
+ end
42
+
43
+ # Next-generation table drawing for Prawn.
44
+ #
45
+ # = Data
46
+ #
47
+ # Data, for a Prawn table, is a two-dimensional array of objects that can be
48
+ # converted to cells ("cellable" objects). Cellable objects can be:
49
+ #
50
+ # String::
51
+ # Produces a text cell. This is the most common usage.
52
+ # Prawn::Table::Cell::
53
+ # If you have already built a Cell or have a custom subclass of Cell you
54
+ # want to use in a table, you can pass through Cell objects.
55
+ # Prawn::Table::
56
+ # Creates a subtable (a table within a cell). You can use
57
+ # Prawn::Document#make_table to create a table for use as a subtable
58
+ # without immediately drawing it. See examples/table/bill.rb for a
59
+ # somewhat complex use of subtables.
60
+ # Array::
61
+ # Creates a simple subtable. Create a Table object using make_table (see
62
+ # above) if you need more control over the subtable's styling.
63
+ #
64
+ # = Options
65
+ #
66
+ # Prawn/Layout provides many options to control style and layout of your
67
+ # table. These options are implemented with a uniform interface: the +:foo+
68
+ # option always sets the +foo=+ accessor. See the accessor and method
69
+ # documentation for full details on the options you can pass. Some
70
+ # highlights:
71
+ #
72
+ # +cell_style+::
73
+ # A hash of style options to style all cells. See the documentation on
74
+ # Prawn::Table::Cell for all cell style options.
75
+ # +header+::
76
+ # If set to +true+, the first row will be repeated on every page. The
77
+ # header must be included as the first row of your data. Row numbering
78
+ # (for styling and other row-specific options) always indexes based on
79
+ # your data array. Whether or not you have a header, row(n) always refers
80
+ # to the nth element (starting from 0) of the +data+ array.
81
+ # +column_widths+::
82
+ # Sets widths for individual columns. Manually setting widths can give
83
+ # better results than letting Prawn guess at them, as Prawn's algorithm
84
+ # for defaulting widths is currently pretty boneheaded. If you experience
85
+ # problems like weird column widths or CannotFit errors, try manually
86
+ # setting widths on more columns.
87
+ # +position+::
88
+ # Either :left (the default), :center, :right, or a number. Specifies the
89
+ # horizontal position of the table within its bounding box. If a number is
90
+ # provided, it specifies the distance in points from the left edge.
91
+ #
92
+ # = Initializer Block
93
+ #
94
+ # If a block is passed to methods that initialize a table
95
+ # (Prawn::Table.new, Prawn::Document#table, Prawn::Document#make_table), it
96
+ # will be called after cell setup but before layout. This is a very flexible
97
+ # way to specify styling and layout constraints. This code sets up a table
98
+ # where the second through the fourth rows (1-3, indexed from 0) are each one
99
+ # inch (72 pt) wide:
100
+ #
101
+ # pdf.table(data) do |table|
102
+ # table.rows(1..3).width = 72
103
+ # end
104
+ #
105
+ # As with Prawn::Document#initialize, if the block has no arguments, it will
106
+ # be evaluated in the context of the object itself. The above code could be
107
+ # rewritten as:
108
+ #
109
+ # pdf.table(data) do
110
+ # rows(1..3).width = 72
111
+ # end
112
+ #
113
+ class Table
114
+
115
+ # Set up a table on the given document. Arguments:
116
+ #
117
+ # +data+::
118
+ # A two-dimensional array of cell-like objects. See the "Data" section
119
+ # above for the types of objects that can be put in a table.
120
+ # +document+::
121
+ # The Prawn::Document instance on which to draw the table.
122
+ # +options+::
123
+ # A hash of attributes and values for the table. See the "Options" block
124
+ # above for details on available options.
125
+ #
126
+ def initialize(data, document, options={}, &block)
127
+ @pdf = document
128
+ @cells = make_cells(data)
129
+ @header = false
130
+ @epsilon = 1e-9
131
+ options.each { |k, v| send("#{k}=", v) }
132
+
133
+ if block
134
+ block.arity < 1 ? instance_eval(&block) : block[self]
135
+ end
136
+
137
+ set_column_widths
138
+ set_row_heights
139
+ position_cells
140
+ end
141
+
142
+ # Number of rows in the table.
143
+ #
144
+ attr_reader :row_length
145
+
146
+ # Number of columns in the table.
147
+ #
148
+ attr_reader :column_length
149
+
150
+ # Manually set the width of the table.
151
+ #
152
+ attr_writer :width
153
+
154
+ # Position (:left, :right, :center, or a number indicating distance in
155
+ # points from the left edge) of the table within its parent bounds.
156
+ #
157
+ attr_writer :position
158
+
159
+ # Returns a Prawn::Table::Cells object representing all of the cells in
160
+ # this table.
161
+ #
162
+ attr_reader :cells
163
+
164
+ # Specify a callback to be called before each page of cells is rendered.
165
+ # The block is passed a Cells object containing all cells to be rendered on
166
+ # that page. You can change styling of the cells in this block, but keep in
167
+ # mind that the cells have already been positioned and sized.
168
+ #
169
+ def before_rendering_page(&block)
170
+ @before_rendering_page = block
171
+ end
172
+
173
+ # Returns the width of the table in PDF points.
174
+ #
175
+ def width
176
+ @width ||= [natural_width, @pdf.bounds.width].min
177
+ end
178
+
179
+ # Sets column widths for the table. The argument can be one of the following
180
+ # types:
181
+ #
182
+ # +Array+::
183
+ # <tt>[w0, w1, w2, ...]</tt> (specify a width for each column)
184
+ # +Hash+::
185
+ # <tt>{0 => w0, 1 => w1, ...}</tt> (keys are column names, values are
186
+ # widths)
187
+ # +Numeric+::
188
+ # +72+ (sets width for all columns)
189
+ #
190
+ def column_widths=(widths)
191
+ case widths
192
+ when Array
193
+ widths.each_with_index { |w, i| column(i).width = w }
194
+ when Hash
195
+ widths.each { |i, w| column(i).width = w }
196
+ when Numeric
197
+ cells.width = widths
198
+ else
199
+ raise ArgumentError, "cannot interpret column widths"
200
+ end
201
+ end
202
+
203
+ # Returns the height of the table in PDF points.
204
+ #
205
+ def height
206
+ cells.height
207
+ end
208
+
209
+ # If +true+, designates the first row as a header row to be repeated on
210
+ # every page. Does not change row numbering -- row numbers always index into
211
+ # the data array provided, with no modification.
212
+ #
213
+ attr_writer :header
214
+
215
+ # Accepts an Array of alternating row colors to stripe the table.
216
+ #
217
+ attr_writer :row_colors
218
+
219
+ # Sets styles for all cells.
220
+ #
221
+ # pdf.table(data, :cell_style => { :borders => [:left, :right] })
222
+ #
223
+ def cell_style=(style_hash)
224
+ cells.style(style_hash)
225
+ end
226
+
227
+ # Allows generic stylable content. This is an alternate syntax that some
228
+ # prefer to the attribute-based syntax. This code using style:
229
+ #
230
+ # pdf.table(data) do
231
+ # style(row(0), :background_color => 'ff00ff')
232
+ # style(column(0)) { |c| c.border_width += 1 }
233
+ # end
234
+ #
235
+ # is equivalent to:
236
+ #
237
+ # pdf.table(data) do
238
+ # row(0).style :background_color => 'ff00ff'
239
+ # column(0).style { |c| c.border_width += 1 }
240
+ # end
241
+ #
242
+ def style(stylable, style_hash={}, &block)
243
+ stylable.style(style_hash, &block)
244
+ end
245
+
246
+ # Draws the table onto the document at the document's current y-position.
247
+ #
248
+ def draw
249
+ with_position do
250
+ # The cell y-positions are based on an infinitely long canvas. The offset
251
+ # keeps track of how much we have to add to the original, theoretical
252
+ # y-position to get to the actual position on the current page.
253
+ offset = @pdf.y
254
+
255
+ # Reference bounds are the non-stretchy bounds used to decide when to
256
+ # flow to a new column / page.
257
+ ref_bounds = @pdf.reference_bounds
258
+
259
+ last_y = @pdf.y
260
+
261
+ # Determine whether we're at the top of the current bounds (margin box or
262
+ # bounding box). If we're at the top, we couldn't gain any more room by
263
+ # breaking to the next page -- this means, in particular, that if the
264
+ # first row is taller than the margin box, we will only move to the next
265
+ # page if we're below the top. Some floating-point tolerance is added to
266
+ # the calculation.
267
+ #
268
+ # Note that we use the actual bounds, not the reference bounds. This is
269
+ # because even if we are in a stretchy bounding box, flowing to the next
270
+ # page will not buy us any space if we are at the top.
271
+ if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001
272
+ # we're at the top of our bounds
273
+ started_new_page_at_row = 0
274
+ else
275
+ started_new_page_at_row = -1
276
+
277
+ # If there isn't enough room left on the page to fit the first data row
278
+ # (excluding the header), start the table on the next page.
279
+ needed_height = row(0).height
280
+ needed_height += row(1).height if @header
281
+ if needed_height > @pdf.y - ref_bounds.absolute_bottom
282
+ @pdf.bounds.move_past_bottom
283
+ offset = @pdf.y
284
+ started_new_page_at_row = 0
285
+ end
286
+ end
287
+
288
+ # Duplicate each cell of the header row into @header_row so it can be
289
+ # modified in before_rendering_page callbacks.
290
+ if @header
291
+ @header_row = Cells.new
292
+ row(0).each { |cell| @header_row[cell.row, cell.column] = cell.dup }
293
+ end
294
+
295
+ # Track cells to be drawn on this page. They will all be drawn when this
296
+ # page is finished.
297
+ cells_this_page = []
298
+
299
+ @cells.each do |cell|
300
+ if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom &&
301
+ cell.row > started_new_page_at_row
302
+ # Ink all cells on the current page
303
+ if @before_rendering_page
304
+ c = Cells.new(cells_this_page.map { |c, _| c })
305
+ @before_rendering_page.call(c)
306
+ end
307
+ Cell.draw_cells(cells_this_page)
308
+ cells_this_page = []
309
+
310
+ # start a new page or column
311
+ @pdf.bounds.move_past_bottom
312
+ if cell.row > 0 && @header
313
+ header_height = add_header(cells_this_page, @pdf.cursor, cell.row-1)
314
+ else
315
+ header_height = 0
316
+ end
317
+ offset = @pdf.y - cell.y - header_height
318
+ started_new_page_at_row = cell.row
319
+ end
320
+
321
+ # Don't modify cell.x / cell.y here, as we want to reuse the original
322
+ # values when re-inking the table. #draw should be able to be called
323
+ # multiple times.
324
+ x, y = cell.x, cell.y
325
+ y += offset
326
+
327
+ # Translate coordinates to the bounds we are in, since drawing is
328
+ # relative to the cursor, not ref_bounds.
329
+ x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
330
+ y -= @pdf.bounds.absolute_bottom
331
+
332
+ # Set background color, if any.
333
+ if @row_colors && (!@header || cell.row > 0)
334
+ # Ensure coloring restarts on every page (to make sure the header
335
+ # and first row of a page are not colored the same way).
336
+ index = cell.row - [started_new_page_at_row, @header ? 1 : 0].max
337
+ cell.background_color ||= @row_colors[index % @row_colors.length]
338
+ end
339
+
340
+ cells_this_page << [cell, [x, y]]
341
+ last_y = y
342
+ end
343
+ # Draw the last page of cells
344
+ if @before_rendering_page
345
+ c = Cells.new(cells_this_page.map { |c, _| c })
346
+ @before_rendering_page.call(c)
347
+ end
348
+ Cell.draw_cells(cells_this_page)
349
+
350
+ @pdf.move_cursor_to(last_y - @cells.last.height)
351
+ end
352
+ end
353
+
354
+ # Calculate and return the constrained column widths, taking into account
355
+ # each cell's min_width, max_width, and any user-specified constraints on
356
+ # the table or column size.
357
+ #
358
+ # Because the natural widths can be silly, this does not always work so well
359
+ # at guessing a good size for columns that have vastly different content. If
360
+ # you see weird problems like CannotFit errors or shockingly bad column
361
+ # sizes, you should specify more column widths manually.
362
+ #
363
+ def column_widths
364
+ @column_widths ||= begin
365
+ if width - cells.min_width < -epsilon
366
+ raise Errors::CannotFit,
367
+ "Table's width was set too small to contain its contents " +
368
+ "(min width #{cells.min_width}, requested #{width})"
369
+ end
370
+
371
+ if width - cells.max_width > epsilon
372
+ raise Errors::CannotFit,
373
+ "Table's width was set larger than its contents' maximum width " +
374
+ "(max width #{cells.max_width}, requested #{width})"
375
+ end
376
+
377
+ if width - natural_width < -epsilon
378
+ # Shrink the table to fit the requested width.
379
+ f = (width - cells.min_width).to_f / (natural_width - cells.min_width)
380
+
381
+ (0...column_length).map do |c|
382
+ min, nat = column(c).min_width, natural_column_widths[c]
383
+ (f * (nat - min)) + min
384
+ end
385
+ elsif width - natural_width > epsilon
386
+ # Expand the table to fit the requested width.
387
+ f = (width - cells.width).to_f / (cells.max_width - cells.width)
388
+
389
+ (0...column_length).map do |c|
390
+ nat, max = natural_column_widths[c], column(c).max_width
391
+ (f * (max - nat)) + nat
392
+ end
393
+ else
394
+ natural_column_widths
395
+ end
396
+ end
397
+ end
398
+
399
+ # Returns an array with the height of each row.
400
+ #
401
+ def row_heights
402
+ @natural_row_heights ||=
403
+ begin
404
+ heights_by_row = Hash.new(0)
405
+ cells.each do |cell|
406
+ next if cell.is_a?(Cell::SpanDummy)
407
+
408
+ # Split the height of row-spanned cells evenly by rows
409
+ height_per_row = cell.height.to_f / cell.rowspan
410
+ cell.rowspan.times do |i|
411
+ heights_by_row[cell.row + i] =
412
+ [heights_by_row[cell.row + i], height_per_row].max
413
+ end
414
+ end
415
+ heights_by_row.sort_by { |row, _| row }.map { |_, h| h }
416
+ end
417
+ end
418
+
419
+ protected
420
+
421
+ # Converts the array of cellable objects given into instances of
422
+ # Prawn::Table::Cell, and sets up their in-table properties so that they
423
+ # know their own position in the table.
424
+ #
425
+ def make_cells(data)
426
+ assert_proper_table_data(data)
427
+
428
+ cells = Cells.new
429
+
430
+ row_number = 0
431
+ data.each do |row_cells|
432
+ column_number = 0
433
+ row_cells.each do |cell_data|
434
+ # If we landed on a spanned cell (from a rowspan above), continue
435
+ # until we find an empty spot.
436
+ column_number += 1 until cells[row_number, column_number].nil?
437
+
438
+ # Build the cell and store it in the Cells collection.
439
+ cell = Cell.make(@pdf, cell_data)
440
+ cells[row_number, column_number] = cell
441
+
442
+ # Add dummy cells for the rest of the cells in the span group. This
443
+ # allows Prawn to keep track of the horizontal and vertical space
444
+ # occupied in each column and row spanned by this cell, while still
445
+ # leaving the master (top left) cell in the group responsible for
446
+ # drawing. Dummy cells do not put ink on the page.
447
+ cell.rowspan.times do |i|
448
+ cell.colspan.times do |j|
449
+ next if i == 0 && j == 0
450
+
451
+ # It is an error to specify spans that overlap; catch this here
452
+ if bad_cell = cells[row_number + i, column_number + j]
453
+ raise Prawn::Errors::InvalidTableSpan,
454
+ "Spans overlap at row #{row_number + i}, " +
455
+ "column #{column_number + j}."
456
+ end
457
+
458
+ dummy = Cell::SpanDummy.new(@pdf, cell)
459
+ cells[row_number + i, column_number + j] = dummy
460
+ cell.dummy_cells << dummy
461
+ end
462
+ end
463
+
464
+ column_number += cell.colspan
465
+ end
466
+
467
+ row_number += 1
468
+ end
469
+
470
+ # Calculate the number of rows and columns in the table, taking into
471
+ # account that some cells may span past the end of the physical cells we
472
+ # have.
473
+ @row_length = cells.map do |cell|
474
+ cell.row + cell.rowspan
475
+ end.max
476
+
477
+ @column_length = cells.map do |cell|
478
+ cell.column + cell.colspan
479
+ end.max
480
+
481
+ cells
482
+ end
483
+
484
+ # Add the header row to the given array of cells at the given y-position.
485
+ # Number the row with the given +row+ index, so that the header appears (in
486
+ # any Cells built for this page) immediately prior to the first data row on
487
+ # this page.
488
+ #
489
+ # Return the height of the header.
490
+ #
491
+ def add_header(page_of_cells, y, row)
492
+ @header_row.each do |cell|
493
+ cell.row = row
494
+ page_of_cells << [cell, [cell.x, y]]
495
+ end
496
+ @header_row.height
497
+ end
498
+
499
+ # Raises an error if the data provided cannot be converted into a valid
500
+ # table.
501
+ #
502
+ def assert_proper_table_data(data)
503
+ if data.nil? || data.empty?
504
+ raise Prawn::Errors::EmptyTable,
505
+ "data must be a non-empty, non-nil, two dimensional array " +
506
+ "of cell-convertible objects"
507
+ end
508
+
509
+ unless data.all? { |e| Array === e }
510
+ raise Prawn::Errors::InvalidTableData,
511
+ "data must be a two dimensional array of cellable objects"
512
+ end
513
+ end
514
+
515
+ # Returns an array of each column's natural (unconstrained) width.
516
+ #
517
+ def natural_column_widths
518
+ @natural_column_widths ||=
519
+ begin
520
+ widths_by_column = Hash.new(0)
521
+ cells.each do |cell|
522
+ next if cell.is_a?(Cell::SpanDummy)
523
+
524
+ # Split the width of colspanned cells evenly by columns
525
+ width_per_column = cell.width.to_f / cell.colspan
526
+ cell.colspan.times do |i|
527
+ widths_by_column[cell.column + i] =
528
+ [widths_by_column[cell.column + i], width_per_column].max
529
+ end
530
+ end
531
+ widths_by_column.sort_by { |col, _| col }.map { |_, w| w }
532
+ end
533
+ end
534
+
535
+ # Returns the "natural" (unconstrained) width of the table. This may be
536
+ # extremely silly; for example, the unconstrained width of a paragraph of
537
+ # text is the width it would assume if it were not wrapped at all. Could be
538
+ # a mile long.
539
+ #
540
+ def natural_width
541
+ @natural_width ||= natural_column_widths.inject(0, &:+)
542
+ end
543
+
544
+ # Assigns the calculated column widths to each cell. This ensures that each
545
+ # cell in a column is the same width. After this method is called,
546
+ # subsequent calls to column_widths and width should return the finalized
547
+ # values that will be used to ink the table.
548
+ #
549
+ def set_column_widths
550
+ column_widths.each_with_index do |w, col_num|
551
+ column(col_num).width = w
552
+ end
553
+ end
554
+
555
+ # Assigns the row heights to each cell. This ensures that every cell in a
556
+ # row is the same height.
557
+ #
558
+ def set_row_heights
559
+ row_heights.each_with_index { |h, row_num| row(row_num).height = h }
560
+ end
561
+
562
+ # Set each cell's position based on the widths and heights of cells
563
+ # preceding it.
564
+ #
565
+ def position_cells
566
+ # Calculate x- and y-positions as running sums of widths / heights.
567
+ x_positions = column_widths.inject([0]) { |ary, x|
568
+ ary << (ary.last + x); ary }[0..-2]
569
+ x_positions.each_with_index { |x, i| column(i).x = x }
570
+
571
+ # y-positions assume an infinitely long canvas starting at zero -- this
572
+ # is corrected for in Table#draw, and page breaks are properly inserted.
573
+ y_positions = row_heights.inject([0]) { |ary, y|
574
+ ary << (ary.last - y); ary}[0..-2]
575
+ y_positions.each_with_index { |y, i| row(i).y = y }
576
+ end
577
+
578
+ # Sets up a bounding box to position the table according to the specified
579
+ # :position option, and yields.
580
+ #
581
+ def with_position
582
+ x = case @position || :left
583
+ when :left then return yield
584
+ when :center then (@pdf.bounds.width - width) / 2.0
585
+ when :right then @pdf.bounds.width - width
586
+ when Numeric then @position
587
+ else raise ArgumentError, "unknown position #{@position.inspect}"
588
+ end
589
+ dy = @pdf.bounds.absolute_top - @pdf.y
590
+ final_y = nil
591
+
592
+ @pdf.bounding_box([x, @pdf.bounds.top], :width => width) do
593
+ @pdf.move_down dy
594
+ yield
595
+ final_y = @pdf.y
596
+ end
597
+
598
+ @pdf.y = final_y
599
+ end
600
+
601
+ private
602
+
603
+ def epsilon
604
+ @epsilon
605
+ end
606
+ end
607
+
608
+
609
+ end