prawn 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (282) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +2 -2
  3. data/Gemfile +18 -0
  4. data/LICENSE +1 -1
  5. data/README.md +17 -4
  6. data/Rakefile +18 -22
  7. data/data/images/indexed_color.dat +0 -0
  8. data/data/images/indexed_color.png +0 -0
  9. data/data/pdfs/nested_pages.pdf +13 -13
  10. data/lib/pdf/core.rb +35 -0
  11. data/lib/{prawn → pdf}/core/annotations.rb +6 -7
  12. data/lib/{prawn → pdf}/core/byte_string.rb +1 -1
  13. data/lib/{prawn → pdf}/core/destinations.rb +23 -23
  14. data/lib/{prawn → pdf}/core/document_state.rb +8 -8
  15. data/lib/pdf/core/filter_list.rb +51 -0
  16. data/lib/pdf/core/filters.rb +36 -0
  17. data/lib/pdf/core/graphics_state.rb +68 -0
  18. data/lib/{prawn → pdf}/core/literal_string.rb +1 -1
  19. data/lib/{prawn → pdf}/core/name_tree.rb +14 -2
  20. data/lib/{prawn → pdf}/core/object_store.rb +80 -24
  21. data/lib/pdf/core/outline.rb +315 -0
  22. data/lib/{prawn → pdf}/core/page.rb +23 -26
  23. data/lib/{prawn/document → pdf/core}/page_geometry.rb +11 -21
  24. data/lib/{prawn → pdf}/core/pdf_object.rb +48 -32
  25. data/lib/{prawn → pdf}/core/reference.rb +35 -44
  26. data/lib/pdf/core/stream.rb +98 -0
  27. data/lib/{prawn → pdf}/core/text.rb +24 -17
  28. data/lib/prawn.rb +95 -17
  29. data/lib/prawn/compatibility.rb +66 -26
  30. data/lib/prawn/document.rb +48 -30
  31. data/lib/prawn/document/bounding_box.rb +3 -3
  32. data/lib/prawn/document/column_box.rb +46 -8
  33. data/lib/prawn/document/graphics_state.rb +10 -73
  34. data/lib/prawn/document/internals.rb +24 -23
  35. data/lib/prawn/document/snapshot.rb +6 -7
  36. data/lib/prawn/document/span.rb +10 -10
  37. data/lib/prawn/encoding.rb +7 -7
  38. data/lib/prawn/errors.rb +18 -29
  39. data/lib/prawn/font.rb +64 -28
  40. data/lib/prawn/font/afm.rb +32 -74
  41. data/lib/prawn/font/dfont.rb +2 -2
  42. data/lib/prawn/font/ttf.rb +28 -57
  43. data/lib/prawn/font_metric_cache.rb +45 -0
  44. data/lib/prawn/graphics.rb +307 -41
  45. data/lib/prawn/graphics/cap_style.rb +3 -3
  46. data/lib/prawn/graphics/color.rb +12 -5
  47. data/lib/prawn/graphics/dash.rb +52 -31
  48. data/lib/prawn/graphics/join_style.rb +7 -7
  49. data/lib/prawn/graphics/patterns.rb +137 -0
  50. data/lib/prawn/graphics/transformation.rb +9 -9
  51. data/lib/prawn/graphics/transparency.rb +1 -1
  52. data/lib/prawn/image_handler.rb +30 -0
  53. data/lib/prawn/images.rb +86 -105
  54. data/lib/prawn/images/image.rb +48 -0
  55. data/lib/prawn/images/jpg.rb +14 -10
  56. data/lib/prawn/images/png.rb +50 -37
  57. data/lib/prawn/layout.rb +2 -2
  58. data/lib/prawn/layout/grid.rb +51 -51
  59. data/lib/prawn/measurement_extensions.rb +5 -5
  60. data/lib/prawn/measurements.rb +25 -21
  61. data/lib/prawn/outline.rb +4 -308
  62. data/lib/prawn/repeater.rb +8 -8
  63. data/lib/prawn/security.rb +50 -36
  64. data/lib/prawn/soft_mask.rb +94 -0
  65. data/lib/prawn/stamp.rb +3 -3
  66. data/lib/prawn/table.rb +292 -118
  67. data/lib/prawn/table/cell.rb +272 -45
  68. data/lib/prawn/table/cell/image.rb +70 -0
  69. data/lib/prawn/table/cell/in_table.rb +2 -2
  70. data/lib/prawn/table/cell/span_dummy.rb +92 -0
  71. data/lib/prawn/table/cell/subtable.rb +2 -2
  72. data/lib/prawn/table/cell/text.rb +42 -24
  73. data/lib/prawn/table/cells.rb +137 -48
  74. data/lib/prawn/text.rb +35 -23
  75. data/lib/prawn/text/box.rb +18 -5
  76. data/lib/prawn/text/formatted.rb +5 -4
  77. data/lib/prawn/text/formatted/arranger.rb +292 -0
  78. data/lib/prawn/text/formatted/box.rb +52 -13
  79. data/lib/prawn/text/formatted/fragment.rb +37 -22
  80. data/lib/prawn/text/formatted/line_wrap.rb +286 -0
  81. data/lib/prawn/text/formatted/parser.rb +14 -6
  82. data/lib/prawn/text/formatted/wrap.rb +151 -0
  83. data/lib/prawn/utilities.rb +44 -0
  84. data/manual/basic_concepts/adding_pages.rb +27 -0
  85. data/manual/basic_concepts/basic_concepts.rb +34 -0
  86. data/manual/basic_concepts/creation.rb +39 -0
  87. data/manual/basic_concepts/cursor.rb +33 -0
  88. data/manual/basic_concepts/measurement.rb +25 -0
  89. data/manual/basic_concepts/origin.rb +38 -0
  90. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  91. data/manual/bounding_box/bounding_box.rb +39 -0
  92. data/manual/bounding_box/bounds.rb +49 -0
  93. data/manual/bounding_box/canvas.rb +24 -0
  94. data/manual/bounding_box/creation.rb +23 -0
  95. data/manual/bounding_box/indentation.rb +46 -0
  96. data/manual/bounding_box/nesting.rb +45 -0
  97. data/manual/bounding_box/russian_boxes.rb +40 -0
  98. data/manual/bounding_box/stretchy.rb +31 -0
  99. data/manual/document_and_page_options/background.rb +27 -0
  100. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  101. data/manual/document_and_page_options/metadata.rb +23 -0
  102. data/manual/document_and_page_options/page_margins.rb +38 -0
  103. data/manual/document_and_page_options/page_size.rb +34 -0
  104. data/manual/example_file.rb +116 -0
  105. data/manual/example_helper.rb +411 -0
  106. data/manual/example_package.rb +53 -0
  107. data/manual/example_section.rb +46 -0
  108. data/manual/graphics/circle_and_ellipse.rb +22 -0
  109. data/manual/graphics/color.rb +24 -0
  110. data/manual/graphics/common_lines.rb +28 -0
  111. data/manual/graphics/fill_and_stroke.rb +42 -0
  112. data/manual/graphics/fill_rules.rb +37 -0
  113. data/manual/graphics/gradients.rb +37 -0
  114. data/manual/graphics/graphics.rb +58 -0
  115. data/manual/graphics/helper.rb +24 -0
  116. data/manual/graphics/line_width.rb +35 -0
  117. data/manual/graphics/lines_and_curves.rb +41 -0
  118. data/manual/graphics/polygon.rb +29 -0
  119. data/manual/graphics/rectangle.rb +21 -0
  120. data/manual/graphics/rotate.rb +28 -0
  121. data/manual/graphics/scale.rb +41 -0
  122. data/manual/graphics/soft_masks.rb +46 -0
  123. data/manual/graphics/stroke_cap.rb +31 -0
  124. data/manual/graphics/stroke_dash.rb +48 -0
  125. data/manual/graphics/stroke_join.rb +30 -0
  126. data/manual/graphics/translate.rb +29 -0
  127. data/manual/graphics/transparency.rb +35 -0
  128. data/manual/images/absolute_position.rb +23 -0
  129. data/manual/images/fit.rb +21 -0
  130. data/manual/images/horizontal.rb +25 -0
  131. data/manual/images/images.rb +40 -0
  132. data/manual/images/plain_image.rb +18 -0
  133. data/manual/images/scale.rb +22 -0
  134. data/manual/images/vertical.rb +28 -0
  135. data/manual/images/width_and_height.rb +25 -0
  136. data/manual/layout/boxes.rb +27 -0
  137. data/manual/layout/content.rb +25 -0
  138. data/manual/layout/layout.rb +28 -0
  139. data/manual/layout/simple_grid.rb +23 -0
  140. data/manual/manual/cover.rb +35 -0
  141. data/manual/manual/foreword.rb +85 -0
  142. data/manual/manual/how_to_read_this_manual.rb +41 -0
  143. data/manual/manual/manual.rb +35 -0
  144. data/manual/outline/add_subsection_to.rb +61 -0
  145. data/manual/outline/insert_section_after.rb +47 -0
  146. data/manual/outline/outline.rb +32 -0
  147. data/manual/outline/sections_and_pages.rb +67 -0
  148. data/manual/repeatable_content/page_numbering.rb +54 -0
  149. data/manual/repeatable_content/repeatable_content.rb +31 -0
  150. data/manual/repeatable_content/repeater.rb +55 -0
  151. data/manual/repeatable_content/stamp.rb +41 -0
  152. data/manual/security/encryption.rb +31 -0
  153. data/manual/security/permissions.rb +38 -0
  154. data/manual/security/security.rb +28 -0
  155. data/manual/syntax_highlight.rb +52 -0
  156. data/manual/table/basic_block.rb +53 -0
  157. data/manual/table/before_rendering_page.rb +26 -0
  158. data/manual/table/cell_border_lines.rb +24 -0
  159. data/manual/table/cell_borders_and_bg.rb +31 -0
  160. data/manual/table/cell_dimensions.rb +30 -0
  161. data/manual/table/cell_text.rb +38 -0
  162. data/manual/table/column_widths.rb +30 -0
  163. data/manual/table/content_and_subtables.rb +39 -0
  164. data/manual/table/creation.rb +27 -0
  165. data/manual/table/filtering.rb +36 -0
  166. data/manual/table/flow_and_header.rb +17 -0
  167. data/manual/table/image_cells.rb +33 -0
  168. data/manual/table/position.rb +29 -0
  169. data/manual/table/row_colors.rb +20 -0
  170. data/manual/table/span.rb +30 -0
  171. data/manual/table/style.rb +22 -0
  172. data/manual/table/table.rb +52 -0
  173. data/manual/table/width.rb +27 -0
  174. data/manual/templates/full_template.rb +25 -0
  175. data/manual/templates/page_template.rb +48 -0
  176. data/manual/templates/templates.rb +27 -0
  177. data/manual/text/alignment.rb +44 -0
  178. data/manual/text/color.rb +24 -0
  179. data/manual/text/column_box.rb +32 -0
  180. data/manual/text/fallback_fonts.rb +37 -0
  181. data/manual/text/font.rb +41 -0
  182. data/manual/text/font_size.rb +45 -0
  183. data/manual/text/font_style.rb +23 -0
  184. data/manual/text/formatted_callbacks.rb +60 -0
  185. data/manual/text/formatted_text.rb +54 -0
  186. data/manual/text/free_flowing_text.rb +51 -0
  187. data/manual/text/group.rb +29 -0
  188. data/manual/text/inline.rb +43 -0
  189. data/manual/text/kerning_and_character_spacing.rb +39 -0
  190. data/manual/text/leading.rb +25 -0
  191. data/manual/text/line_wrapping.rb +41 -0
  192. data/manual/text/paragraph_indentation.rb +26 -0
  193. data/manual/text/positioned_text.rb +38 -0
  194. data/manual/text/registering_families.rb +48 -0
  195. data/manual/text/rendering_and_color.rb +37 -0
  196. data/manual/text/right_to_left_text.rb +43 -0
  197. data/manual/text/rotation.rb +43 -0
  198. data/manual/text/single_usage.rb +37 -0
  199. data/manual/text/text.rb +75 -0
  200. data/manual/text/text_box_excess.rb +32 -0
  201. data/manual/text/text_box_extensions.rb +45 -0
  202. data/manual/text/text_box_overflow.rb +44 -0
  203. data/manual/text/utf8.rb +28 -0
  204. data/{examples/m17n → manual/text}/win_ansi_charset.rb +14 -10
  205. data/prawn.gemspec +18 -12
  206. data/spec/acceptance/png.rb +23 -0
  207. data/spec/annotations_spec.rb +16 -32
  208. data/spec/bounding_box_spec.rb +128 -15
  209. data/spec/cell_spec.rb +169 -38
  210. data/spec/column_box_spec.rb +33 -0
  211. data/spec/destinations_spec.rb +5 -5
  212. data/spec/document_spec.rb +150 -104
  213. data/spec/extensions/encoding_helpers.rb +10 -0
  214. data/spec/extensions/mocha.rb +1 -0
  215. data/spec/filters_spec.rb +34 -0
  216. data/spec/font_metric_cache_spec.rb +52 -0
  217. data/spec/font_spec.rb +183 -97
  218. data/spec/formatted_text_arranger_spec.rb +43 -43
  219. data/spec/formatted_text_box_spec.rb +30 -20
  220. data/spec/formatted_text_fragment_spec.rb +8 -8
  221. data/spec/graphics_spec.rb +158 -69
  222. data/spec/grid_spec.rb +15 -15
  223. data/spec/image_handler_spec.rb +42 -0
  224. data/spec/images_spec.rb +49 -24
  225. data/spec/inline_formatted_text_parser_spec.rb +73 -19
  226. data/spec/jpg_spec.rb +4 -4
  227. data/spec/line_wrap_spec.rb +26 -26
  228. data/spec/measurement_units_spec.rb +6 -6
  229. data/spec/name_tree_spec.rb +21 -21
  230. data/spec/object_store_spec.rb +39 -39
  231. data/spec/outline_spec.rb +93 -53
  232. data/spec/pdf_object_spec.rb +88 -86
  233. data/spec/png_spec.rb +31 -28
  234. data/spec/reference_spec.rb +32 -32
  235. data/spec/repeater_spec.rb +25 -11
  236. data/spec/security_spec.rb +44 -12
  237. data/spec/snapshot_spec.rb +8 -9
  238. data/spec/soft_mask_spec.rb +117 -0
  239. data/spec/span_spec.rb +10 -15
  240. data/spec/spec_helper.rb +25 -8
  241. data/spec/stamp_spec.rb +29 -30
  242. data/spec/stream_spec.rb +58 -0
  243. data/spec/stroke_styles_spec.rb +36 -18
  244. data/spec/table/span_dummy_spec.rb +17 -0
  245. data/spec/table_spec.rb +697 -105
  246. data/spec/template_spec.rb +108 -54
  247. data/spec/text_at_spec.rb +18 -17
  248. data/spec/text_box_spec.rb +111 -62
  249. data/spec/text_rendering_mode_spec.rb +5 -5
  250. data/spec/text_spacing_spec.rb +4 -4
  251. data/spec/text_spec.rb +57 -49
  252. data/spec/transparency_spec.rb +5 -5
  253. metadata +421 -213
  254. data/data/fonts/Action Man.dfont +0 -0
  255. data/data/fonts/Activa.ttf +0 -0
  256. data/data/fonts/Chalkboard.ttf +0 -0
  257. data/data/fonts/DejaVuSans.ttf +0 -0
  258. data/data/fonts/Dustismo_Roman.ttf +0 -0
  259. data/data/fonts/comicsans.ttf +0 -0
  260. data/data/fonts/gkai00mp.ttf +0 -0
  261. data/data/images/rails.dat +0 -0
  262. data/data/images/rails.png +0 -0
  263. data/examples/bounding_box/russian_boxes.rb +0 -37
  264. data/examples/example_helper.rb +0 -11
  265. data/examples/general/context_sensitive_headers.rb +0 -38
  266. data/examples/graphics/cmyk.rb +0 -13
  267. data/examples/graphics/gradient.rb +0 -23
  268. data/examples/graphics/png_types.rb +0 -23
  269. data/examples/graphics/remote_images.rb +0 -13
  270. data/examples/m17n/full_win_ansi_character_list.rb +0 -20
  271. data/examples/m17n/sjis.rb +0 -29
  272. data/examples/table/bill.rb +0 -54
  273. data/examples/table/header.rb +0 -15
  274. data/examples/text/font_calculations.rb +0 -92
  275. data/examples/text/hyphenation.rb +0 -45
  276. data/examples/text/indent_paragraphs.rb +0 -24
  277. data/lib/prawn/core.rb +0 -85
  278. data/lib/prawn/core/text/formatted/arranger.rb +0 -294
  279. data/lib/prawn/core/text/formatted/line_wrap.rb +0 -273
  280. data/lib/prawn/core/text/formatted/wrap.rb +0 -153
  281. data/lib/prawn/graphics/gradient.rb +0 -84
  282. data/lib/prawn/security/arcfour.rb +0 -51
@@ -29,10 +29,10 @@ module Prawn
29
29
  # :even -- repeats on even pages
30
30
  # some_array -- repeats on every page listed in the array
31
31
  # some_range -- repeats on every page included in the range
32
- # some_lambda -- yields page number and repeats for true return values
32
+ # some_lambda -- yields page number and repeats for true return values
33
33
  #
34
- # Also accepts an optional second argument for dynamic content which executes the code
35
- # in the context of the filtered pages without using a Stamp.
34
+ # Also accepts an optional second argument for dynamic content which executes the code
35
+ # in the context of the filtered pages without using a Stamp.
36
36
  #
37
37
  # Example:
38
38
  #
@@ -49,8 +49,8 @@ module Prawn
49
49
  # repeat :even do
50
50
  # draw_text "EVEN", :at => [0,0]
51
51
  # end
52
- #
53
- # repeat [1,2] do
52
+ #
53
+ # repeat [1,2] do
54
54
  # draw_text "[1,2]", :at => [100,0]
55
55
  # end
56
56
  #
@@ -62,11 +62,11 @@ module Prawn
62
62
  # draw_text "Every third", :at => [250, 20]
63
63
  # end
64
64
  #
65
- # 10.times do
65
+ # 10.times do
66
66
  # start_new_page
67
67
  # draw_text "A wonderful page", :at => [400,400]
68
68
  # end
69
- #
69
+ #
70
70
  # repeat(:all, :dynamic => true) do
71
71
  # text page_number, :at => [500, 0]
72
72
  # end
@@ -108,7 +108,7 @@ module Prawn
108
108
  def run(page_number)
109
109
  if !@dynamic
110
110
  @document.stamp(@stamp_name) if match?(page_number)
111
- elsif @block
111
+ elsif @block && match?(page_number)
112
112
  @document.save_graphics_state(@graphic_state) do
113
113
  @document.send(:freeze_stamp_graphics)
114
114
  @block.call
@@ -7,30 +7,31 @@
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
 
9
9
  require 'digest/md5'
10
- require 'prawn/security/arcfour'
11
- require 'prawn/core/byte_string'
10
+ require 'rc4'
11
+
12
+ require_relative '../pdf/core/byte_string'
12
13
 
13
14
  module Prawn
14
15
  class Document
15
-
16
+
16
17
  # Implements PDF encryption (password protection and permissions) as
17
18
  # specified in the PDF Reference, version 1.3, section 3.5 "Encryption".
18
19
  module Security
19
- include Prawn::Core
20
-
20
+ include PDF::Core
21
+
21
22
  # Encrypts the document, to protect confidential data or control
22
23
  # modifications to the document. The encryption algorithm used is
23
24
  # detailed in the PDF Reference 1.3, section 3.5 "Encryption", and it is
24
25
  # implemented by all major PDF readers.
25
26
  #
26
27
  # +options+ can contain the following:
27
- #
28
- # <tt>:user_password</tt>:: Password required to open the document. If
28
+ #
29
+ # <tt>:user_password</tt>:: Password required to open the document. If
29
30
  # this is omitted or empty, no password will be
30
31
  # required. The document will still be
31
32
  # encrypted, but anyone can read it.
32
33
  #
33
- # <tt>:owner_password</tt>:: Password required to make modifications to
34
+ # <tt>:owner_password</tt>:: Password required to make modifications to
34
35
  # the document or change or override its
35
36
  # permissions. If this is set to
36
37
  # <tt>:random</tt>, a random password will be
@@ -52,7 +53,7 @@ module Prawn
52
53
  #
53
54
  # <tt>:copy_contents</tt>:: Copy text and graphics from document.
54
55
  #
55
- # <tt>:modify_annotations</tt>:: Add or modify text annotations and
56
+ # <tt>:modify_annotations</tt>:: Add or modify text annotations and
56
57
  # interactive form fields.
57
58
  #
58
59
  # == Examples
@@ -66,8 +67,8 @@ module Prawn
66
67
  # both the user and the owner:
67
68
  #
68
69
  # encrypt_document :user_password => 'foo', :owner_password => 'bar'
69
- #
70
- # Set no passwords, grant all permissions (This is useful because the
70
+ #
71
+ # Set no passwords, grant all permissions (This is useful because the
71
72
  # default in some readers, if no permissions are specified, is "deny"):
72
73
  #
73
74
  # encrypt_document
@@ -77,10 +78,10 @@ module Prawn
77
78
  # * The encryption used is weak; the key is password-derived and is
78
79
  # limited to 40 bits, due to US export controls in effect at the time
79
80
  # the PDF standard was written.
80
- #
81
+ #
81
82
  # * There is nothing technologically requiring PDF readers to respect the
82
83
  # permissions embedded in a document. Many PDF readers do not.
83
- #
84
+ #
84
85
  # * In short, you have <b>no security at all</b> against a moderately
85
86
  # motivated person. Don't use this for anything super-serious. This is
86
87
  # not a limitation of Prawn, but is rather a built-in limitation of the
@@ -104,7 +105,7 @@ module Prawn
104
105
  state.encrypt = true
105
106
  state.encryption_key = user_encryption_key
106
107
  end
107
-
108
+
108
109
  # Encrypts the given string under the given key, also requiring the
109
110
  # object ID and generation number of the reference.
110
111
  # See Algorithm 3.1.
@@ -116,7 +117,7 @@ module Prawn
116
117
 
117
118
  # Compute the RC4 key from the extended key and perform the encryption
118
119
  rc4_key = Digest::MD5.digest(extended_key)[0, 10]
119
- Arcfour.new(rc4_key).encrypt(str)
120
+ RC4.new(rc4_key).encrypt(str)
120
121
  end
121
122
 
122
123
  private
@@ -136,7 +137,7 @@ module Prawn
136
137
  :modify_contents => 4,
137
138
  :copy_contents => 5,
138
139
  :modify_annotations => 6 }
139
-
140
+
140
141
  FullPermissions = 0b1111_1111_1111_1111_1111_1111_1111_1111
141
142
 
142
143
  def permissions=(perms={})
@@ -162,10 +163,10 @@ module Prawn
162
163
  @permissions || FullPermissions
163
164
  end
164
165
 
165
- PasswordPadding =
166
+ PasswordPadding =
166
167
  "28BF4E5E4E758A4164004E56FFFA01082E2E00B6D0683E802F0CA9FE6453697A".
167
168
  scan(/../).map{|x| x.to_i(16)}.pack("c*")
168
-
169
+
169
170
  # Pads or truncates a password to 32 bytes as per Alg 3.2.
170
171
  def pad_password(password)
171
172
  password = password[0, 32]
@@ -186,20 +187,22 @@ module Prawn
186
187
  def owner_password_hash
187
188
  @owner_password_hash ||= begin
188
189
  key = Digest::MD5.digest(pad_password(@owner_password))[0, 5]
189
- Arcfour.new(key).encrypt(pad_password(@user_password))
190
+ RC4.new(key).encrypt(pad_password(@user_password))
190
191
  end
191
192
  end
192
193
 
193
194
  # The U (user) value in the encryption dictionary. Algorithm 3.4.
194
195
  def user_password_hash
195
- Arcfour.new(user_encryption_key).encrypt(PasswordPadding)
196
+ RC4.new(user_encryption_key).encrypt(PasswordPadding)
196
197
  end
197
198
 
198
199
  end
199
200
 
200
201
  end
202
+ end
201
203
 
202
- module Core #:nodoc:
204
+ module PDF #:nodoc:
205
+ module Core
203
206
  module_function
204
207
 
205
208
  # Like PdfObject, but returns an encrypted result if required.
@@ -212,24 +215,22 @@ module Prawn
212
215
  EncryptedPdfObject(e, key, id, gen, in_content_stream)
213
216
  }.join(' ') << "]"
214
217
  when LiteralString
215
- # FIXME: encrypted?
216
- obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
218
+ obj = ByteString.new(Prawn::Document::Security.encrypt_string(obj, key, id, gen)).gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
217
219
  "(#{obj})"
218
220
  when Time
219
- # FIXME: encrypted?
220
221
  obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
221
- obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
222
+ obj = ByteString.new(Prawn::Document::Security.encrypt_string(obj, key, id, gen)).gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
222
223
  "(#{obj})"
223
224
  when String
224
225
  PdfObject(
225
226
  ByteString.new(
226
- Document::Security.encrypt_string(obj, key, id, gen)),
227
+ Prawn::Document::Security.encrypt_string(obj, key, id, gen)),
227
228
  in_content_stream)
228
- when Hash
229
+ when ::Hash
229
230
  output = "<< "
230
231
  obj.each do |k,v|
231
232
  unless String === k || Symbol === k
232
- raise Prawn::Errors::FailedObjectConversion,
233
+ raise PDF::Core::Errors::FailedObjectConversion,
233
234
  "A PDF Dictionary must be keyed by names"
234
235
  end
235
236
  output << PdfObject(k.to_sym, in_content_stream) << " " <<
@@ -239,29 +240,42 @@ module Prawn
239
240
  when NameTree::Value
240
241
  PdfObject(obj.name) + " " +
241
242
  EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
243
+ when PDF::Core::OutlineRoot, PDF::Core::OutlineItem
244
+ EncryptedPdfObject(obj.to_hash, key, id, gen, in_content_stream)
242
245
  else # delegate back to PdfObject
243
246
  PdfObject(obj, in_content_stream)
244
247
  end
245
248
  end
246
249
 
250
+
251
+ class Stream
252
+ def encrypted_object(key, id, gen)
253
+ if filtered_stream
254
+ "stream\n#{Prawn::Document::Security.encrypt_string filtered_stream, key, id, gen}\nendstream\n"
255
+ else
256
+ ''
257
+ end
258
+ end
259
+ end
260
+
247
261
  class Reference
248
262
 
249
263
  # Returns the object definition for the object this references, keyed from
250
264
  # +key+.
251
265
  def encrypted_object(key)
252
266
  @on_encode.call(self) if @on_encode
253
- output = "#{@identifier} #{gen} obj\n" <<
254
- Prawn::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
255
- if @stream
256
- output << "stream\n" <<
257
- Document::Security.encrypt_string(@stream, key, @identifier, gen) <<
258
- "\nendstream\n"
267
+
268
+ output = "#{@identifier} #{gen} obj\n"
269
+ unless @stream.empty?
270
+ output << PDF::Core::EncryptedPdfObject(data.merge(@stream.data), key, @identifier, gen) << "\n" <<
271
+ @stream.encrypted_object(key, @identifier, gen)
272
+ else
273
+ output << PDF::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
259
274
  end
275
+
260
276
  output << "endobj\n"
261
277
  end
262
278
 
263
279
  end
264
280
  end
265
-
266
281
  end
267
-
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ #
3
+ # soft_mask.rb : Implements soft-masking
4
+ #
5
+ # Copyright September 2012, Alexander Mankuta. 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::SoftMask module is used to create arbitrary transparency in
13
+ # document. Using a soft mask allows creaing more visually rich documents.
14
+ #
15
+ # You must group soft mask and graphics it's applied to under
16
+ # save_graphics_state because soft mask is a part of graphic state in PDF.
17
+ #
18
+ # Example:
19
+ # pdf.save_graphics_state do
20
+ # pdf.soft_mask do
21
+ # pdf.fill_color "444444"
22
+ # pdf.fill_polygon [0, 40], [60, 10], [120, 40], [60, 68]
23
+ # end
24
+ # pdf.fill_color '000000'
25
+ # pdf.fill_rectangle [0, 50], 120, 68
26
+ # end
27
+ #
28
+ module SoftMask
29
+ def soft_mask(&block)
30
+ min_version(1.4)
31
+
32
+ group_attrs = ref!({
33
+ :Type => :Group,
34
+ :S => :Transparency,
35
+ :CS => :DeviceRGB,
36
+ :I => false,
37
+ :K => false
38
+ })
39
+
40
+ group = ref!({
41
+ :Type => :XObject,
42
+ :Subtype => :Form,
43
+ :BBox => state.page.dimensions,
44
+ :Group => group_attrs,
45
+ })
46
+
47
+ state.page.stamp_stream(group, &block)
48
+
49
+ mask = ref!({
50
+ :Type => :Mask,
51
+ :S => :Luminosity,
52
+ :G => group
53
+ })
54
+
55
+ g_state = ref!({
56
+ :Type => :ExtGState,
57
+ :SMask => mask,
58
+
59
+ :AIS => false,
60
+ :BM => :Normal,
61
+ :OP => false,
62
+ :op => false,
63
+ :OPM => 1,
64
+ :SA => true,
65
+ })
66
+
67
+ registry_key = {
68
+ :bbox => state.page.dimensions,
69
+ :mask => [group.stream.filters.normalized, group.stream.filtered_stream],
70
+ :page => state.page_count,
71
+ }.hash
72
+
73
+ if soft_mask_registry[registry_key]
74
+ [g_state, mask, group, group_attrs].each { |ref| ref.live = false }
75
+
76
+ add_content "/#{soft_mask_registry[registry_key]} gs"
77
+ else
78
+ masks = page.resources[:ExtGState] ||= {}
79
+ id = masks.empty? ? 'GS1' : masks.keys.sort.last.succ
80
+ masks[id] = g_state
81
+
82
+ soft_mask_registry[registry_key] = id
83
+
84
+ add_content "/#{id} gs"
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def soft_mask_registry
91
+ @soft_mask_registry ||= {}
92
+ end
93
+ end
94
+ end
@@ -80,7 +80,7 @@ module Prawn
80
80
 
81
81
  state.page.stamp_stream(dictionary, &block)
82
82
  end
83
-
83
+
84
84
  private
85
85
 
86
86
  def stamp_dictionary_registry
@@ -121,12 +121,12 @@ module Prawn
121
121
  :stamp_dictionary => dictionary }
122
122
  dictionary
123
123
  end
124
-
124
+
125
125
  def freeze_stamp_graphics
126
126
  update_colors
127
127
  write_line_width
128
128
  write_stroke_cap_style
129
- write_stroke_join_style
129
+ write_stroke_join_style
130
130
  write_stroke_dash
131
131
  end
132
132
 
@@ -11,11 +11,13 @@ require 'prawn/table/cell'
11
11
  require 'prawn/table/cell/in_table'
12
12
  require 'prawn/table/cell/text'
13
13
  require 'prawn/table/cell/subtable'
14
+ require 'prawn/table/cell/image'
15
+ require 'prawn/table/cell/span_dummy'
14
16
 
15
17
  module Prawn
16
18
 
17
19
  class Document
18
-
20
+
19
21
  # Set up and draw a table on this document. A block can be given, which will
20
22
  # be run after cell setup but before layout and drawing.
21
23
  #
@@ -49,7 +51,7 @@ module Prawn
49
51
  # Produces a text cell. This is the most common usage.
50
52
  # Prawn::Table::Cell::
51
53
  # If you have already built a Cell or have a custom subclass of Cell you
52
- # want to use in a table, you can pass through Cell objects.
54
+ # want to use in a table, you can pass through Cell objects.
53
55
  # Prawn::Table::
54
56
  # Creates a subtable (a table within a cell). You can use
55
57
  # Prawn::Document#make_table to create a table for use as a subtable
@@ -71,17 +73,21 @@ module Prawn
71
73
  # A hash of style options to style all cells. See the documentation on
72
74
  # Prawn::Table::Cell for all cell style options.
73
75
  # +header+::
74
- # If set to +true+, the first row will be repeated on every page. The
75
- # header must be included as the first row of your data. Row numbering
76
- # (for styling and other row-specific options) always indexes based on
77
- # your data array. Whether or not you have a header, row(n) always refers
78
- # to the nth element (starting from 0) of the +data+ array.
79
- # +column_widths+::
76
+ # If set to +true+, the first row will be repeated on every page. If set
77
+ # to an Integer, the first +x+ rows will be repeated on every page. Row
78
+ # numbering (for styling and other row-specific options) always indexes
79
+ # based on your data array. Whether or not you have a header, row(n) always
80
+ # refers to the nth element (starting from 0) of the +data+ array.
81
+ # +column_widths+::
80
82
  # Sets widths for individual columns. Manually setting widths can give
81
83
  # better results than letting Prawn guess at them, as Prawn's algorithm
82
84
  # for defaulting widths is currently pretty boneheaded. If you experience
83
85
  # problems like weird column widths or CannotFit errors, try manually
84
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.
85
91
  #
86
92
  # = Initializer Block
87
93
  #
@@ -95,7 +101,7 @@ module Prawn
95
101
  # pdf.table(data) do |table|
96
102
  # table.rows(1..3).width = 72
97
103
  # end
98
- #
104
+ #
99
105
  # As with Prawn::Document#initialize, if the block has no arguments, it will
100
106
  # be evaluated in the context of the object itself. The above code could be
101
107
  # rewritten as:
@@ -104,7 +110,7 @@ module Prawn
104
110
  # rows(1..3).width = 72
105
111
  # end
106
112
  #
107
- class Table
113
+ class Table
108
114
 
109
115
  # Set up a table on the given document. Arguments:
110
116
  #
@@ -131,7 +137,7 @@ module Prawn
131
137
  set_column_widths
132
138
  set_row_heights
133
139
  position_cells
134
- end
140
+ end
135
141
 
136
142
  # Number of rows in the table.
137
143
  #
@@ -145,6 +151,25 @@ module Prawn
145
151
  #
146
152
  attr_writer :width
147
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
+
148
173
  # Returns the width of the table in PDF points.
149
174
  #
150
175
  def width
@@ -154,9 +179,9 @@ module Prawn
154
179
  # Sets column widths for the table. The argument can be one of the following
155
180
  # types:
156
181
  #
157
- # +Array+::
182
+ # +Array+::
158
183
  # <tt>[w0, w1, w2, ...]</tt> (specify a width for each column)
159
- # +Hash+::
184
+ # +Hash+::
160
185
  # <tt>{0 => w0, 1 => w1, ...}</tt> (keys are column names, values are
161
186
  # widths)
162
187
  # +Numeric+::
@@ -182,8 +207,9 @@ module Prawn
182
207
  end
183
208
 
184
209
  # If +true+, designates the first row as a header row to be repeated on
185
- # every page. Does not change row numbering -- row numbers always index into
186
- # the data array provided, with no modification.
210
+ # every page. If an integer, designates the number of rows to be treated
211
+ # as a header Does not change row numbering -- row numbers always index
212
+ # into the data array provided, with no modification.
187
213
  #
188
214
  attr_writer :header
189
215
 
@@ -221,86 +247,139 @@ module Prawn
221
247
  # Draws the table onto the document at the document's current y-position.
222
248
  #
223
249
  def draw
224
- # The cell y-positions are based on an infinitely long canvas. The offset
225
- # keeps track of how much we have to add to the original, theoretical
226
- # y-position to get to the actual position on the current page.
227
- offset = @pdf.y
228
-
229
- # Reference bounds are the non-stretchy bounds used to decide when to
230
- # flow to a new column / page.
231
- ref_bounds = @pdf.reference_bounds
232
-
233
- last_y = @pdf.y
234
-
235
- # Determine whether we're at the top of the current bounds (margin box or
236
- # bounding box). If we're at the top, we couldn't gain any more room by
237
- # breaking to the next page -- this means, in particular, that if the
238
- # first row is taller than the margin box, we will only move to the next
239
- # page if we're below the top. Some floating-point tolerance is added to
240
- # the calculation.
241
- #
242
- # Note that we use the actual bounds, not the reference bounds. This is
243
- # because even if we are in a stretchy bounding box, flowing to the next
244
- # page will not buy us any space if we are at the top.
245
- if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001
246
- # we're at the top of our bounds
247
- started_new_page_at_row = 0
248
- else
249
- started_new_page_at_row = -1
250
-
251
- # If there isn't enough room left on the page to fit the first data row
252
- # (excluding the header), start the table on the next page.
253
- needed_height = row(0).height
254
- needed_height += row(1).height if @header
255
- if needed_height > @pdf.y - ref_bounds.absolute_bottom
256
- @pdf.bounds.move_past_bottom
257
- offset = @pdf.y
250
+ with_position do
251
+ # The cell y-positions are based on an infinitely long canvas. The offset
252
+ # keeps track of how much we have to add to the original, theoretical
253
+ # y-position to get to the actual position on the current page.
254
+ offset = @pdf.y
255
+
256
+ # Reference bounds are the non-stretchy bounds used to decide when to
257
+ # flow to a new column / page.
258
+ ref_bounds = @pdf.reference_bounds
259
+
260
+ last_y = @pdf.y
261
+
262
+ # Determine whether we're at the top of the current bounds (margin box or
263
+ # bounding box). If we're at the top, we couldn't gain any more room by
264
+ # breaking to the next page -- this means, in particular, that if the
265
+ # first row is taller than the margin box, we will only move to the next
266
+ # page if we're below the top. Some floating-point tolerance is added to
267
+ # the calculation.
268
+ #
269
+ # Note that we use the actual bounds, not the reference bounds. This is
270
+ # because even if we are in a stretchy bounding box, flowing to the next
271
+ # page will not buy us any space if we are at the top.
272
+ if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001
273
+ # we're at the top of our bounds
258
274
  started_new_page_at_row = 0
275
+ else
276
+ started_new_page_at_row = -1
277
+
278
+ # If there isn't enough room left on the page to fit the first data row
279
+ # (excluding the header), start the table on the next page.
280
+ needed_height = row(0).height
281
+ if @header
282
+ if @header.is_a? Integer
283
+ needed_height += row(1..@header).height
284
+ else
285
+ needed_height += row(1).height
286
+ end
287
+ end
288
+ if needed_height > @pdf.y - ref_bounds.absolute_bottom
289
+ @pdf.bounds.move_past_bottom
290
+ offset = @pdf.y
291
+ started_new_page_at_row = 0
292
+ end
259
293
  end
260
- end
261
294
 
262
- # Track cells to be drawn on this page. They will all be drawn when this
263
- # page is finished.
264
- cells_this_page = []
265
-
266
- @cells.each do |cell|
267
- if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom &&
268
- cell.row > started_new_page_at_row
269
- # Ink all cells on the current page
270
- Cell.draw_cells(cells_this_page)
271
- cells_this_page = []
272
-
273
- # start a new page or column
274
- @pdf.bounds.move_past_bottom
275
- draw_header unless cell.row == 0
276
- offset = @pdf.y - cell.y
277
- started_new_page_at_row = cell.row
295
+ # Duplicate each cell of the header row into @header_row so it can be
296
+ # modified in before_rendering_page callbacks.
297
+ if @header
298
+ @header_row = Cells.new
299
+ if @header.is_a? Integer
300
+ @header.times do |r|
301
+ row(r).each { |cell| @header_row[cell.row, cell.column] = cell.dup }
302
+ end
303
+ else
304
+ row(0).each { |cell| @header_row[cell.row, cell.column] = cell.dup }
305
+ end
278
306
  end
279
-
280
- # Don't modify cell.x / cell.y here, as we want to reuse the original
281
- # values when re-inking the table. #draw should be able to be called
282
- # multiple times.
283
- x, y = cell.x, cell.y
284
- y += offset
285
-
286
- # Translate coordinates to the bounds we are in, since drawing is
287
- # relative to the cursor, not ref_bounds.
288
- x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
289
- y -= @pdf.bounds.absolute_bottom
290
-
291
- # Set background color, if any.
292
- if @row_colors && (!@header || cell.row > 0)
293
- index = @header ? (cell.row - 1) : cell.row
294
- cell.background_color = @row_colors[index % @row_colors.length]
307
+
308
+ # Track cells to be drawn on this page. They will all be drawn when this
309
+ # page is finished.
310
+ cells_this_page = []
311
+
312
+ @cells.each do |cell|
313
+ if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom &&
314
+ cell.row > started_new_page_at_row
315
+ # Ink all cells on the current page
316
+ if defined?(@before_rendering_page) && @before_rendering_page
317
+ c = Cells.new(cells_this_page.map { |ci, _| ci })
318
+ @before_rendering_page.call(c)
319
+ end
320
+ Cell.draw_cells(cells_this_page)
321
+ cells_this_page = []
322
+
323
+ # start a new page or column
324
+ @pdf.bounds.move_past_bottom
325
+ x_offset = @pdf.bounds.left_side - @pdf.bounds.absolute_left
326
+ if cell.row > 0 && @header
327
+ if @header.is_a? Integer
328
+ header_height = 0
329
+ y_coord = @pdf.cursor
330
+ @header.times do |h|
331
+ additional_header_height = add_header(cells_this_page, x_offset, y_coord-header_height, cell.row-1, h)
332
+ header_height += additional_header_height
333
+ end
334
+ else
335
+ header_height = add_header(cells_this_page, x_offset, @pdf.cursor, cell.row-1)
336
+ end
337
+ else
338
+ header_height = 0
339
+ end
340
+ offset = @pdf.y - cell.y - header_height
341
+ started_new_page_at_row = cell.row
342
+ end
343
+
344
+ # Don't modify cell.x / cell.y here, as we want to reuse the original
345
+ # values when re-inking the table. #draw should be able to be called
346
+ # multiple times.
347
+ x, y = cell.x, cell.y
348
+ y += offset
349
+
350
+ # Translate coordinates to the bounds we are in, since drawing is
351
+ # relative to the cursor, not ref_bounds.
352
+ x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
353
+ y -= @pdf.bounds.absolute_bottom
354
+
355
+ # Set background color, if any.
356
+ if defined?(@row_colors) && @row_colors && (!@header || cell.row > 0)
357
+ # Ensure coloring restarts on every page (to make sure the header
358
+ # and first row of a page are not colored the same way).
359
+ if @header.is_a? Integer
360
+ rows = @header
361
+ elsif @header
362
+ rows = 1
363
+ else
364
+ rows = 0
365
+ end
366
+ index = cell.row - [started_new_page_at_row, rows].max
367
+
368
+ cell.background_color ||= @row_colors[index % @row_colors.length]
369
+ end
370
+
371
+ cells_this_page << [cell, [x, y]]
372
+ last_y = y
373
+ end
374
+ # Draw the last page of cells
375
+ if defined?(@before_rendering_page) && @before_rendering_page
376
+ c = Cells.new(cells_this_page.map { |ci, _| ci })
377
+ @before_rendering_page.call(c)
295
378
  end
379
+ Cell.draw_cells(cells_this_page)
296
380
 
297
- cells_this_page << [cell, [x, y]]
298
- last_y = y
381
+ @pdf.move_cursor_to(last_y - @cells.last.height)
299
382
  end
300
- # Draw the last page of cells
301
- Cell.draw_cells(cells_this_page)
302
-
303
- @pdf.move_cursor_to(last_y - @cells.last.height)
304
383
  end
305
384
 
306
385
  # Calculate and return the constrained column widths, taking into account
@@ -331,7 +410,7 @@ module Prawn
331
410
  f = (width - cells.min_width).to_f / (natural_width - cells.min_width)
332
411
 
333
412
  (0...column_length).map do |c|
334
- min, nat = column(c).min_width, column(c).width
413
+ min, nat = column(c).min_width, natural_column_widths[c]
335
414
  (f * (nat - min)) + min
336
415
  end
337
416
  elsif width - natural_width > epsilon
@@ -339,7 +418,7 @@ module Prawn
339
418
  f = (width - cells.width).to_f / (cells.max_width - cells.width)
340
419
 
341
420
  (0...column_length).map do |c|
342
- nat, max = column(c).width, column(c).max_width
421
+ nat, max = natural_column_widths[c], column(c).max_width
343
422
  (f * (max - nat)) + nat
344
423
  end
345
424
  else
@@ -351,7 +430,21 @@ module Prawn
351
430
  # Returns an array with the height of each row.
352
431
  #
353
432
  def row_heights
354
- @natural_row_heights ||= (0...row_length).map{ |r| row(r).height }
433
+ @natural_row_heights ||=
434
+ begin
435
+ heights_by_row = Hash.new(0)
436
+ cells.each do |cell|
437
+ next if cell.is_a?(Cell::SpanDummy)
438
+
439
+ # Split the height of row-spanned cells evenly by rows
440
+ height_per_row = cell.height.to_f / cell.rowspan
441
+ cell.rowspan.times do |i|
442
+ heights_by_row[cell.row + i] =
443
+ [heights_by_row[cell.row + i], height_per_row].max
444
+ end
445
+ end
446
+ heights_by_row.sort_by { |row, _| row }.map { |_, h| h }
447
+ end
355
448
  end
356
449
 
357
450
  protected
@@ -363,23 +456,80 @@ module Prawn
363
456
  def make_cells(data)
364
457
  assert_proper_table_data(data)
365
458
 
366
- cells = []
367
-
368
- @row_length = data.length
369
- @column_length = data.map{ |r| r.length }.max
459
+ cells = Cells.new
460
+
461
+ row_number = 0
462
+ data.each do |row_cells|
463
+ column_number = 0
464
+ row_cells.each do |cell_data|
465
+ # If we landed on a spanned cell (from a rowspan above), continue
466
+ # until we find an empty spot.
467
+ column_number += 1 until cells[row_number, column_number].nil?
370
468
 
371
- data.each_with_index do |row_cells, row_number|
372
- row_cells.each_with_index do |cell_data, column_number|
469
+ # Build the cell and store it in the Cells collection.
373
470
  cell = Cell.make(@pdf, cell_data)
374
- cell.extend(Cell::InTable)
375
- cell.row = row_number
376
- cell.column = column_number
377
- cells << cell
471
+ cells[row_number, column_number] = cell
472
+
473
+ # Add dummy cells for the rest of the cells in the span group. This
474
+ # allows Prawn to keep track of the horizontal and vertical space
475
+ # occupied in each column and row spanned by this cell, while still
476
+ # leaving the master (top left) cell in the group responsible for
477
+ # drawing. Dummy cells do not put ink on the page.
478
+ cell.rowspan.times do |i|
479
+ cell.colspan.times do |j|
480
+ next if i == 0 && j == 0
481
+
482
+ # It is an error to specify spans that overlap; catch this here
483
+ if cells[row_number + i, column_number + j]
484
+ raise Prawn::Errors::InvalidTableSpan,
485
+ "Spans overlap at row #{row_number + i}, " +
486
+ "column #{column_number + j}."
487
+ end
488
+
489
+ dummy = Cell::SpanDummy.new(@pdf, cell)
490
+ cells[row_number + i, column_number + j] = dummy
491
+ cell.dummy_cells << dummy
492
+ end
493
+ end
494
+
495
+ column_number += cell.colspan
378
496
  end
497
+
498
+ row_number += 1
379
499
  end
500
+
501
+ # Calculate the number of rows and columns in the table, taking into
502
+ # account that some cells may span past the end of the physical cells we
503
+ # have.
504
+ @row_length = cells.map do |cell|
505
+ cell.row + cell.rowspan
506
+ end.max
507
+
508
+ @column_length = cells.map do |cell|
509
+ cell.column + cell.colspan
510
+ end.max
511
+
380
512
  cells
381
513
  end
382
514
 
515
+ # Add the header row(s) to the given array of cells at the given y-position.
516
+ # Number the row with the given +row+ index, so that the header appears (in
517
+ # any Cells built for this page) immediately prior to the first data row on
518
+ # this page.
519
+ #
520
+ # Return the height of the header.
521
+ #
522
+ def add_header(page_of_cells, x_offset, y, row, row_of_header=nil)
523
+ rows_to_operate_on = @header_row
524
+ rows_to_operate_on = @header_row.rows(row_of_header) if row_of_header
525
+ rows_to_operate_on.each do |cell|
526
+ cell.row = row
527
+ cell.dummy_cells.each {|c| c.row = row }
528
+ page_of_cells << [cell, [cell.x + x_offset, y]]
529
+ end
530
+ rows_to_operate_on.height
531
+ end
532
+
383
533
  # Raises an error if the data provided cannot be converted into a valid
384
534
  # table.
385
535
  #
@@ -396,23 +546,24 @@ module Prawn
396
546
  end
397
547
  end
398
548
 
399
- # If the table has a header, draw it at the current position.
400
- #
401
- def draw_header
402
- if @header
403
- y = @pdf.cursor
404
- row(0).each do |cell|
405
- cell.y = y
406
- cell.draw
407
- end
408
- @pdf.move_cursor_to(y - row(0).height)
409
- end
410
- end
411
-
412
549
  # Returns an array of each column's natural (unconstrained) width.
413
550
  #
414
551
  def natural_column_widths
415
- @natural_column_widths ||= (0...column_length).map { |c| column(c).width }
552
+ @natural_column_widths ||=
553
+ begin
554
+ widths_by_column = Hash.new(0)
555
+ cells.each do |cell|
556
+ next if cell.is_a?(Cell::SpanDummy)
557
+
558
+ # Split the width of colspanned cells evenly by columns
559
+ width_per_column = cell.width.to_f / cell.colspan
560
+ cell.colspan.times do |i|
561
+ widths_by_column[cell.column + i] =
562
+ [widths_by_column[cell.column + i], width_per_column].max
563
+ end
564
+ end
565
+ widths_by_column.sort_by { |col, _| col }.map { |_, w| w }
566
+ end
416
567
  end
417
568
 
418
569
  # Returns the "natural" (unconstrained) width of the table. This may be
@@ -421,7 +572,7 @@ module Prawn
421
572
  # a mile long.
422
573
  #
423
574
  def natural_width
424
- @natural_width ||= natural_column_widths.inject(0) { |sum, w| sum + w }
575
+ @natural_width ||= natural_column_widths.inject(0, &:+)
425
576
  end
426
577
 
427
578
  # Assigns the calculated column widths to each cell. This ensures that each
@@ -430,7 +581,7 @@ module Prawn
430
581
  # values that will be used to ink the table.
431
582
  #
432
583
  def set_column_widths
433
- column_widths.each_with_index do |w, col_num|
584
+ column_widths.each_with_index do |w, col_num|
434
585
  column(col_num).width = w
435
586
  end
436
587
  end
@@ -447,7 +598,7 @@ module Prawn
447
598
  #
448
599
  def position_cells
449
600
  # Calculate x- and y-positions as running sums of widths / heights.
450
- x_positions = column_widths.inject([0]) { |ary, x|
601
+ x_positions = column_widths.inject([0]) { |ary, x|
451
602
  ary << (ary.last + x); ary }[0..-2]
452
603
  x_positions.each_with_index { |x, i| column(i).x = x }
453
604
 
@@ -458,6 +609,29 @@ module Prawn
458
609
  y_positions.each_with_index { |y, i| row(i).y = y }
459
610
  end
460
611
 
612
+ # Sets up a bounding box to position the table according to the specified
613
+ # :position option, and yields.
614
+ #
615
+ def with_position
616
+ x = case defined?(@position) && @position || :left
617
+ when :left then return yield
618
+ when :center then (@pdf.bounds.width - width) / 2.0
619
+ when :right then @pdf.bounds.width - width
620
+ when Numeric then @position
621
+ else raise ArgumentError, "unknown position #{@position.inspect}"
622
+ end
623
+ dy = @pdf.bounds.absolute_top - @pdf.y
624
+ final_y = nil
625
+
626
+ @pdf.bounding_box([x, @pdf.bounds.top], :width => width) do
627
+ @pdf.move_down dy
628
+ yield
629
+ final_y = @pdf.y
630
+ end
631
+
632
+ @pdf.y = final_y
633
+ end
634
+
461
635
  private
462
636
 
463
637
  def epsilon