prawn 0.15.0 → 1.0.0.rc1

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