prawn 0.8.4 → 0.11.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +50 -0
- data/LICENSE +56 -0
- data/README +141 -0
- data/Rakefile +52 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/examples/bounding_box/bounding_boxes.rb +43 -0
- data/examples/bounding_box/indentation.rb +34 -0
- data/examples/bounding_box/russian_boxes.rb +36 -0
- data/examples/bounding_box/stretched_nesting.rb +67 -0
- data/examples/example_helper.rb +8 -0
- data/examples/general/background.rb +23 -0
- data/examples/general/canvas.rb +15 -0
- data/examples/general/context_sensitive_headers.rb +37 -0
- data/examples/general/float.rb +11 -0
- data/examples/general/margin.rb +36 -0
- data/examples/general/measurement_units.rb +51 -0
- data/examples/general/metadata-info.rb +16 -0
- data/examples/general/multi_page_layout.rb +18 -0
- data/examples/general/outlines.rb +66 -0
- data/examples/general/page_geometry.rb +31 -0
- data/examples/general/page_numbering.rb +15 -0
- data/examples/general/repeaters.rb +47 -0
- data/examples/general/stamp.rb +41 -0
- data/examples/general/templates.rb +13 -0
- data/examples/graphics/basic_images.rb +23 -0
- data/examples/graphics/cmyk.rb +12 -0
- data/examples/graphics/curves.rb +11 -0
- data/examples/graphics/hexagon.rb +13 -0
- data/examples/graphics/image_fit.rb +15 -0
- data/examples/graphics/image_flow.rb +37 -0
- data/examples/graphics/image_position.rb +17 -0
- data/examples/graphics/line.rb +32 -0
- data/examples/graphics/png_types.rb +22 -0
- data/examples/graphics/polygons.rb +16 -0
- data/examples/graphics/remote_images.rb +12 -0
- data/examples/graphics/rounded_polygons.rb +19 -0
- data/examples/graphics/rounded_rectangle.rb +20 -0
- data/examples/graphics/ruport_style_helpers.rb +19 -0
- data/examples/graphics/stroke_bounds.rb +20 -0
- data/examples/graphics/stroke_cap_and_join.rb +45 -0
- data/examples/graphics/stroke_dash.rb +42 -0
- data/examples/graphics/transformations.rb +52 -0
- data/examples/graphics/transparency.rb +26 -0
- data/examples/grid/bounding_boxes.rb +21 -0
- data/examples/grid/column_gutter_grid.rb +20 -0
- data/examples/grid/multi_boxes.rb +51 -0
- data/examples/grid/show_grid.rb +13 -0
- data/examples/grid/simple_grid.rb +20 -0
- data/examples/m17n/chinese_text_wrapping.rb +17 -0
- data/examples/m17n/euro.rb +15 -0
- data/examples/m17n/sjis.rb +28 -0
- data/examples/m17n/utf8.rb +13 -0
- data/examples/m17n/win_ansi_charset.rb +54 -0
- data/examples/security/hello_foo.rb +8 -0
- data/examples/table/bill.rb +53 -0
- data/examples/table/cell.rb +12 -0
- data/examples/table/checkerboard.rb +22 -0
- data/examples/table/header.rb +14 -0
- data/examples/table/inline_format_table.rb +12 -0
- data/examples/table/multi_page_table.rb +9 -0
- data/examples/table/simple_table.rb +24 -0
- data/examples/table/subtable.rb +12 -0
- data/examples/table/widths.rb +20 -0
- data/examples/text/alignment.rb +18 -0
- data/examples/text/character_spacing.rb +12 -0
- data/examples/text/dfont.rb +48 -0
- data/examples/text/family_based_styling.rb +24 -0
- data/examples/text/font_calculations.rb +91 -0
- data/examples/text/font_size.rb +33 -0
- data/examples/text/hyphenation.rb +45 -0
- data/examples/text/indent_paragraphs.rb +22 -0
- data/examples/text/inline_format.rb +103 -0
- data/examples/text/kerning.rb +30 -0
- data/examples/text/rotated.rb +98 -0
- data/examples/text/shaped_text_box.rb +31 -0
- data/examples/text/simple_text.rb +17 -0
- data/examples/text/simple_text_ttf.rb +17 -0
- data/examples/text/span.rb +29 -0
- data/examples/text/text_box.rb +88 -0
- data/examples/text/text_box_returning_excess.rb +51 -0
- data/examples/text/text_flow.rb +67 -0
- data/lib/prawn.rb +25 -1
- data/lib/prawn/compatibility.rb +51 -0
- data/lib/prawn/core.rb +85 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +78 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +165 -0
- data/lib/prawn/core/object_store.rb +236 -0
- data/lib/prawn/core/page.rb +199 -0
- data/lib/prawn/core/pdf_object.rb +108 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text.rb +140 -0
- data/lib/prawn/core/text/formatted/arranger.rb +266 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +112 -0
- data/lib/prawn/core/text/line_wrap.rb +211 -0
- data/lib/prawn/core/text/wrap.rb +82 -0
- data/lib/prawn/document.rb +574 -0
- data/lib/prawn/document/bounding_box.rb +425 -0
- data/lib/prawn/document/graphics_state.rb +48 -0
- data/lib/prawn/document/internals.rb +170 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +86 -0
- data/lib/prawn/font.rb +368 -0
- data/lib/prawn/font/afm.rb +225 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +350 -0
- data/lib/prawn/graphics.rb +325 -0
- data/lib/prawn/graphics/cap_style.rb +38 -0
- data/lib/prawn/graphics/color.rb +205 -0
- data/lib/prawn/graphics/dash.rb +71 -0
- data/lib/prawn/graphics/join_style.rb +38 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +361 -0
- data/lib/prawn/images/jpg.rb +46 -0
- data/lib/prawn/images/png.rb +226 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +129 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table.rb +421 -0
- data/lib/prawn/table/cell.rb +360 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +125 -0
- data/lib/prawn/table/cells.rb +189 -0
- data/lib/prawn/text.rb +449 -0
- data/lib/prawn/text/box.rb +385 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +222 -0
- data/lib/prawn/text/formatted/fragment.rb +181 -0
- data/lib/prawn/text/formatted/parser.rb +213 -0
- data/prawn.gemspec +28 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +359 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +476 -0
- data/spec/font_spec.rb +324 -0
- data/spec/formatted_text_arranger_spec.rb +426 -0
- data/spec/formatted_text_box_spec.rb +756 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +117 -0
- data/spec/inline_formatted_text_parser_spec.rb +502 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +341 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +160 -0
- data/spec/outline_spec.rb +404 -0
- data/spec/pdf_object_spec.rb +170 -0
- data/spec/png_spec.rb +237 -0
- data/spec/reference_spec.rb +82 -0
- data/spec/repeater_spec.rb +96 -0
- data/spec/security_spec.rb +120 -0
- data/spec/snapshot_spec.rb +154 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/stamp_spec.rb +108 -0
- data/spec/stroke_styles_spec.rb +163 -0
- data/spec/table_spec.rb +598 -0
- data/spec/template_spec.rb +165 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +742 -0
- data/spec/text_spacing_spec.rb +75 -0
- data/spec/text_spec.rb +342 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +89 -0
- data/vendor/pdf-inspector/README +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
- data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
- data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
- data/vendor/ttfunk/example.rb +45 -0
- data/vendor/ttfunk/lib/ttfunk.rb +102 -0
- data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
- data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
- data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
- data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
- data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
- data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
- data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
- data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
- data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
- data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
- data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
- metadata +337 -48
@@ -0,0 +1,262 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# encryption.rb : Implements encrypted PDF and access permissions.
|
4
|
+
#
|
5
|
+
# Copyright August 2008, Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'digest/md5'
|
10
|
+
require 'prawn/security/arcfour'
|
11
|
+
require 'prawn/core/byte_string'
|
12
|
+
|
13
|
+
module Prawn
|
14
|
+
class Document
|
15
|
+
|
16
|
+
# Implements PDF encryption (password protection and permissions) as
|
17
|
+
# specified in the PDF Reference, version 1.3, section 3.5 "Encryption".
|
18
|
+
module Security
|
19
|
+
include Prawn::Core
|
20
|
+
|
21
|
+
# Encrypts the document, to protect confidential data or control
|
22
|
+
# modifications to the document. The encryption algorithm used is
|
23
|
+
# detailed in the PDF Reference 1.3, section 3.5 "Encryption", and it is
|
24
|
+
# implemented by all major PDF readers.
|
25
|
+
#
|
26
|
+
# +options+ can contain the following:
|
27
|
+
#
|
28
|
+
# <tt>:user_password</tt>:: Password required to open the document. If
|
29
|
+
# this is omitted or empty, no password will be
|
30
|
+
# required. The document will still be
|
31
|
+
# encrypted, but anyone can read it.
|
32
|
+
#
|
33
|
+
# <tt>:owner_password</tt>:: Password required to make modifications to
|
34
|
+
# the document or change or override its
|
35
|
+
# permissions. If this is set to
|
36
|
+
# <tt>:random</tt>, a random password will be
|
37
|
+
# used; this can be useful if you never want
|
38
|
+
# users to be able to override the document
|
39
|
+
# permissions.
|
40
|
+
#
|
41
|
+
# <tt>:permissions</tt>:: A hash mapping permission symbols (see below) to
|
42
|
+
# <tt>true</tt> or <tt>false</tt>. True means
|
43
|
+
# "permitted", and false means "not permitted".
|
44
|
+
# All permissions default to <tt>true</tt>.
|
45
|
+
#
|
46
|
+
# The following permissions can be specified:
|
47
|
+
#
|
48
|
+
# <tt>:print_document</tt>:: Print document.
|
49
|
+
#
|
50
|
+
# <tt>:modify_document</tt>:: Modify contents of document (other than text
|
51
|
+
# annotations and interactive form fields).
|
52
|
+
#
|
53
|
+
# <tt>:copy_contents</tt>:: Copy text and graphics from document.
|
54
|
+
#
|
55
|
+
# <tt>:modify_annotations</tt>:: Add or modify text annotations and
|
56
|
+
# interactive form fields.
|
57
|
+
#
|
58
|
+
# == Examples
|
59
|
+
#
|
60
|
+
# Deny printing to everyone, but allow anyone to open without a password:
|
61
|
+
#
|
62
|
+
# encrypt_document :permissions => { :print_document => false },
|
63
|
+
# :owner_password => :random
|
64
|
+
#
|
65
|
+
# Set a user and owner password on the document, with full permissions for
|
66
|
+
# both the user and the owner:
|
67
|
+
#
|
68
|
+
# encrypt_document :user_password => 'foo', :owner_password => 'bar'
|
69
|
+
#
|
70
|
+
# Set no passwords, grant all permissions (This is useful because the
|
71
|
+
# default in some readers, if no permissions are specified, is "deny"):
|
72
|
+
#
|
73
|
+
# encrypt_document
|
74
|
+
#
|
75
|
+
# == Caveats
|
76
|
+
#
|
77
|
+
# * The encryption used is weak; the key is password-derived and is
|
78
|
+
# limited to 40 bits, due to US export controls in effect at the time
|
79
|
+
# the PDF standard was written.
|
80
|
+
#
|
81
|
+
# * There is nothing technologically requiring PDF readers to respect the
|
82
|
+
# permissions embedded in a document. Many PDF readers do not.
|
83
|
+
#
|
84
|
+
# * In short, you have <b>no security at all</b> against a moderately
|
85
|
+
# motivated person. Don't use this for anything super-serious. This is
|
86
|
+
# not a limitation of Prawn, but is rather a built-in limitation of the
|
87
|
+
# PDF format.
|
88
|
+
#
|
89
|
+
def encrypt_document(options={})
|
90
|
+
Prawn.verify_options [:user_password, :owner_password, :permissions],
|
91
|
+
options
|
92
|
+
@user_password = options.delete(:user_password) || ""
|
93
|
+
|
94
|
+
@owner_password = options.delete(:owner_password) || @user_password
|
95
|
+
if @owner_password == :random
|
96
|
+
# Generate a completely ridiculous password
|
97
|
+
@owner_password = (1..32).map{ rand(256) }.pack("c*")
|
98
|
+
end
|
99
|
+
|
100
|
+
self.permissions = options.delete(:permissions) || {}
|
101
|
+
|
102
|
+
# Shove the necessary entries in the trailer and enable encryption.
|
103
|
+
state.trailer[:Encrypt] = encryption_dictionary
|
104
|
+
state.encrypt = true
|
105
|
+
state.encryption_key = user_encryption_key
|
106
|
+
end
|
107
|
+
|
108
|
+
# Encrypts the given string under the given key, also requiring the
|
109
|
+
# object ID and generation number of the reference.
|
110
|
+
# See Algorithm 3.1.
|
111
|
+
def self.encrypt_string(str, key, id, gen)
|
112
|
+
# Convert ID and Gen number into little-endian truncated byte strings
|
113
|
+
id = [id].pack('V')[0,3]
|
114
|
+
gen = [gen].pack('V')[0,2]
|
115
|
+
extended_key = "#{key}#{id}#{gen}"
|
116
|
+
|
117
|
+
# Compute the RC4 key from the extended key and perform the encryption
|
118
|
+
rc4_key = Digest::MD5.digest(extended_key)[0, 10]
|
119
|
+
Arcfour.new(rc4_key).encrypt(str)
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
# Provides the values for the trailer encryption dictionary.
|
125
|
+
def encryption_dictionary
|
126
|
+
{ :Filter => :Standard, # default PDF security handler
|
127
|
+
:V => 1, # "Algorithm 3.1", PDF reference 1.3
|
128
|
+
:R => 2, # Revision 2 of the algorithm
|
129
|
+
:O => ByteString.new(owner_password_hash),
|
130
|
+
:U => ByteString.new(user_password_hash),
|
131
|
+
:P => permissions_value }
|
132
|
+
end
|
133
|
+
|
134
|
+
# Flags in the permissions word, numbered as LSB = 1
|
135
|
+
PermissionsBits = { :print_document => 3,
|
136
|
+
:modify_contents => 4,
|
137
|
+
:copy_contents => 5,
|
138
|
+
:modify_annotations => 6 }
|
139
|
+
|
140
|
+
FullPermissions = 0b1111_1111_1111_1111_1111_1111_1111_1111
|
141
|
+
|
142
|
+
def permissions=(perms={})
|
143
|
+
@permissions ||= FullPermissions
|
144
|
+
perms.each do |key, value|
|
145
|
+
# 0-based bit number, from LSB
|
146
|
+
bit_position = PermissionsBits[key] - 1
|
147
|
+
|
148
|
+
if value # set bit
|
149
|
+
@permissions |= (1 << bit_position)
|
150
|
+
else # clear bit
|
151
|
+
@permissions &= ~(1 << bit_position)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def permissions_value
|
157
|
+
@permissions || FullPermissions
|
158
|
+
end
|
159
|
+
|
160
|
+
PasswordPadding =
|
161
|
+
"28BF4E5E4E758A4164004E56FFFA01082E2E00B6D0683E802F0CA9FE6453697A".
|
162
|
+
scan(/../).map{|x| x.to_i(16)}.pack("c*")
|
163
|
+
|
164
|
+
# Pads or truncates a password to 32 bytes as per Alg 3.2.
|
165
|
+
def pad_password(password)
|
166
|
+
password = password[0, 32]
|
167
|
+
password + PasswordPadding[0, 32 - password.length]
|
168
|
+
end
|
169
|
+
|
170
|
+
def user_encryption_key
|
171
|
+
@user_encryption_key ||= begin
|
172
|
+
md5 = Digest::MD5.new
|
173
|
+
md5 << pad_password(@user_password)
|
174
|
+
md5 << owner_password_hash
|
175
|
+
md5 << [permissions_value].pack("V")
|
176
|
+
md5.digest[0, 5]
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# The O (owner) value in the encryption dictionary. Algorithm 3.3.
|
181
|
+
def owner_password_hash
|
182
|
+
@owner_password_hash ||= begin
|
183
|
+
key = Digest::MD5.digest(pad_password(@owner_password))[0, 5]
|
184
|
+
Arcfour.new(key).encrypt(pad_password(@user_password))
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# The U (user) value in the encryption dictionary. Algorithm 3.4.
|
189
|
+
def user_password_hash
|
190
|
+
Arcfour.new(user_encryption_key).encrypt(PasswordPadding)
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
module Core #:nodoc:
|
198
|
+
module_function
|
199
|
+
|
200
|
+
# Like PdfObject, but returns an encrypted result if required.
|
201
|
+
# For direct objects, requires the object identifier and generation number
|
202
|
+
# from the indirect object referencing obj.
|
203
|
+
def EncryptedPdfObject(obj, key, id, gen, in_content_stream=false)
|
204
|
+
case obj
|
205
|
+
when Array
|
206
|
+
"[" << obj.map { |e|
|
207
|
+
EncryptedPdfObject(e, key, id, gen, in_content_stream)
|
208
|
+
}.join(' ') << "]"
|
209
|
+
when LiteralString
|
210
|
+
# FIXME: encrypted?
|
211
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
212
|
+
"(#{obj})"
|
213
|
+
when Time
|
214
|
+
# FIXME: encrypted?
|
215
|
+
obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
|
216
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
217
|
+
"(#{obj})"
|
218
|
+
when String
|
219
|
+
PdfObject(
|
220
|
+
ByteString.new(
|
221
|
+
Document::Security.encrypt_string(obj, key, id, gen)),
|
222
|
+
in_content_stream)
|
223
|
+
when Hash
|
224
|
+
output = "<< "
|
225
|
+
obj.each do |k,v|
|
226
|
+
unless String === k || Symbol === k
|
227
|
+
raise Prawn::Errors::FailedObjectConversion,
|
228
|
+
"A PDF Dictionary must be keyed by names"
|
229
|
+
end
|
230
|
+
output << PdfObject(k.to_sym, in_content_stream) << " " <<
|
231
|
+
EncryptedPdfObject(v, key, id, gen, in_content_stream) << "\n"
|
232
|
+
end
|
233
|
+
output << ">>"
|
234
|
+
when NameTree::Value
|
235
|
+
PdfObject(obj.name) + " " +
|
236
|
+
EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
|
237
|
+
else # delegate back to PdfObject
|
238
|
+
PdfObject(obj, in_content_stream)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
class Reference
|
243
|
+
|
244
|
+
# Returns the object definition for the object this references, keyed from
|
245
|
+
# +key+.
|
246
|
+
def encrypted_object(key)
|
247
|
+
@on_encode.call(self) if @on_encode
|
248
|
+
output = "#{@identifier} #{gen} obj\n" <<
|
249
|
+
Prawn::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
|
250
|
+
if @stream
|
251
|
+
output << "stream\n" <<
|
252
|
+
Document::Security.encrypt_string(@stream, key, @identifier, gen) <<
|
253
|
+
"\nendstream\n"
|
254
|
+
end
|
255
|
+
output << "endobj\n"
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Implementation of the "ARCFOUR" algorithm ("alleged RC4 (tm)"). Implemented
|
2
|
+
# as described at:
|
3
|
+
# http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt
|
4
|
+
#
|
5
|
+
# "RC4" is a trademark of RSA Data Security, Inc.
|
6
|
+
#
|
7
|
+
# Copyright August 2009, Brad Ediger. All Rights Reserved.
|
8
|
+
#
|
9
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
10
|
+
|
11
|
+
class Arcfour
|
12
|
+
def initialize(key)
|
13
|
+
# Convert string key to Array of integers
|
14
|
+
key = key.unpack('c*') if key.is_a?(String)
|
15
|
+
|
16
|
+
# 1. Allocate an 256 element array of 8 bit bytes to be used as an S-box
|
17
|
+
# 2. Initialize the S-box. Fill each entry first with it's index
|
18
|
+
@sbox = (0..255).to_a
|
19
|
+
|
20
|
+
# 3. Fill another array of the same size (256) with the key, repeating
|
21
|
+
# bytes as necessary.
|
22
|
+
s2 = []
|
23
|
+
while s2.length < 256
|
24
|
+
s2 += key
|
25
|
+
end
|
26
|
+
s2 = s2[0, 256]
|
27
|
+
|
28
|
+
# 4. Set j to zero and initialize the S-box
|
29
|
+
j = 0
|
30
|
+
(0..255).each do |i|
|
31
|
+
j = (j + @sbox[i] + s2[i]) % 256
|
32
|
+
@sbox[i], @sbox[j] = @sbox[j], @sbox[i]
|
33
|
+
end
|
34
|
+
|
35
|
+
@i = @j = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def encrypt(string)
|
39
|
+
string.unpack('c*').map{|byte| byte ^ key_byte}.pack('c*')
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Produces the next byte of key material in the stream (3.2 Stream Generation)
|
45
|
+
def key_byte
|
46
|
+
@i = (@i + 1) % 256
|
47
|
+
@j = (@j + @sbox[@i]) % 256
|
48
|
+
@sbox[@i], @sbox[@j] = @sbox[@j], @sbox[@i]
|
49
|
+
@sbox[(@sbox[@i] + @sbox[@j]) % 256]
|
50
|
+
end
|
51
|
+
end
|
data/lib/prawn/stamp.rb
ADDED
@@ -0,0 +1,126 @@
|
|
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_at([10, 15], :radius => 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_at([10, 15], :radius => 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_at([0, 0], :radius => 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_at([10, 15], :radius => 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
|
+
end
|
126
|
+
end
|
data/lib/prawn/table.rb
ADDED
@@ -0,0 +1,421 @@
|
|
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
|
+
|
15
|
+
module Prawn
|
16
|
+
|
17
|
+
class Document
|
18
|
+
|
19
|
+
# Set up and draw a table on this document. A block can be given, which will
|
20
|
+
# be run after cell setup but before layout and drawing.
|
21
|
+
#
|
22
|
+
# See the documentation on Prawn::Table for details on the arguments.
|
23
|
+
#
|
24
|
+
def table(data, options={}, &block)
|
25
|
+
t = Table.new(data, self, options, &block)
|
26
|
+
t.draw
|
27
|
+
t
|
28
|
+
end
|
29
|
+
|
30
|
+
# Set up, but do not draw, a table. Useful for creating subtables to be
|
31
|
+
# inserted into another Table. Call +draw+ on the resulting Table to ink it.
|
32
|
+
#
|
33
|
+
# See the documentation on Prawn::Table for details on the arguments.
|
34
|
+
#
|
35
|
+
def make_table(data, options={}, &block)
|
36
|
+
Table.new(data, self, options, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Next-generation table drawing for Prawn.
|
42
|
+
#
|
43
|
+
# = Data
|
44
|
+
#
|
45
|
+
# Data, for a Prawn table, is a two-dimensional array of objects that can be
|
46
|
+
# converted to cells ("cellable" objects). Cellable objects can be:
|
47
|
+
#
|
48
|
+
# String::
|
49
|
+
# Produces a text cell. This is the most common usage.
|
50
|
+
# Prawn::Table::Cell::
|
51
|
+
# 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.
|
53
|
+
# Prawn::Table::
|
54
|
+
# Creates a subtable (a table within a cell). You can use
|
55
|
+
# Prawn::Document#make_table to create a table for use as a subtable
|
56
|
+
# without immediately drawing it. See examples/table/bill.rb for a
|
57
|
+
# somewhat complex use of subtables.
|
58
|
+
# Array::
|
59
|
+
# Creates a simple subtable. Create a Table object using make_table (see
|
60
|
+
# above) if you need more control over the subtable's styling.
|
61
|
+
#
|
62
|
+
# = Options
|
63
|
+
#
|
64
|
+
# Prawn/Layout provides many options to control style and layout of your
|
65
|
+
# table. These options are implemented with a uniform interface: the +:foo+
|
66
|
+
# option always sets the +foo=+ accessor. See the accessor and method
|
67
|
+
# documentation for full details on the options you can pass. Some
|
68
|
+
# highlights:
|
69
|
+
#
|
70
|
+
# +cell_style+::
|
71
|
+
# A hash of style options to style all cells. See the documentation on
|
72
|
+
# Prawn::Table::Cell for all cell style options.
|
73
|
+
# +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+::
|
80
|
+
# Sets widths for individual columns. Manually setting widths can give
|
81
|
+
# better results than letting Prawn guess at them, as Prawn's algorithm
|
82
|
+
# for defaulting widths is currently pretty boneheaded. If you experience
|
83
|
+
# problems like weird column widths or CannotFit errors, try manually
|
84
|
+
# setting widths on more columns.
|
85
|
+
#
|
86
|
+
# = Initializer Block
|
87
|
+
#
|
88
|
+
# If a block is passed to methods that initialize a table
|
89
|
+
# (Prawn::Table.new, Prawn::Document#table, Prawn::Document#make_table), it
|
90
|
+
# will be called after cell setup but before layout. This is a very flexible
|
91
|
+
# way to specify styling and layout constraints. This code sets up a table
|
92
|
+
# where the second through the fourth rows (1-3, indexed from 0) are each one
|
93
|
+
# inch (72 pt) wide:
|
94
|
+
#
|
95
|
+
# pdf.table(data) do |table|
|
96
|
+
# table.rows(1..3).width = 72
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# As with Prawn::Document#initialize, if the block has no arguments, it will
|
100
|
+
# be evaluated in the context of the object itself. The above code could be
|
101
|
+
# rewritten as:
|
102
|
+
#
|
103
|
+
# pdf.table(data) do
|
104
|
+
# rows(1..3).width = 72
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
class Table
|
108
|
+
|
109
|
+
# Set up a table on the given document. Arguments:
|
110
|
+
#
|
111
|
+
# +data+::
|
112
|
+
# A two-dimensional array of cell-like objects. See the "Data" section
|
113
|
+
# above for the types of objects that can be put in a table.
|
114
|
+
# +document+::
|
115
|
+
# The Prawn::Document instance on which to draw the table.
|
116
|
+
# +options+::
|
117
|
+
# A hash of attributes and values for the table. See the "Options" block
|
118
|
+
# above for details on available options.
|
119
|
+
#
|
120
|
+
def initialize(data, document, options={}, &block)
|
121
|
+
@pdf = document
|
122
|
+
@cells = make_cells(data)
|
123
|
+
@header = false
|
124
|
+
options.each { |k, v| send("#{k}=", v) }
|
125
|
+
|
126
|
+
if block
|
127
|
+
block.arity < 1 ? instance_eval(&block) : block[self]
|
128
|
+
end
|
129
|
+
|
130
|
+
set_column_widths
|
131
|
+
set_row_heights
|
132
|
+
position_cells
|
133
|
+
end
|
134
|
+
|
135
|
+
# Number of rows in the table.
|
136
|
+
#
|
137
|
+
attr_reader :row_length
|
138
|
+
|
139
|
+
# Number of columns in the table.
|
140
|
+
#
|
141
|
+
attr_reader :column_length
|
142
|
+
|
143
|
+
# Manually set the width of the table.
|
144
|
+
#
|
145
|
+
attr_writer :width
|
146
|
+
|
147
|
+
# Returns the width of the table in PDF points.
|
148
|
+
#
|
149
|
+
def width
|
150
|
+
@width ||= [natural_width, @pdf.bounds.width].min
|
151
|
+
end
|
152
|
+
|
153
|
+
# Sets column widths for the table. The argument can be one of the following
|
154
|
+
# types:
|
155
|
+
#
|
156
|
+
# +Array+::
|
157
|
+
# <tt>[w0, w1, w2, ...]</tt> (specify a width for each column)
|
158
|
+
# +Hash+::
|
159
|
+
# <tt>{0 => w0, 1 => w1, ...}</tt> (keys are column names, values are
|
160
|
+
# widths)
|
161
|
+
# +Numeric+::
|
162
|
+
# +72+ (sets width for all columns)
|
163
|
+
#
|
164
|
+
def column_widths=(widths)
|
165
|
+
case widths
|
166
|
+
when Array
|
167
|
+
widths.each_with_index { |w, i| column(i).width = w }
|
168
|
+
when Hash
|
169
|
+
widths.each { |i, w| column(i).width = w }
|
170
|
+
when Numeric
|
171
|
+
columns.width = widths
|
172
|
+
else
|
173
|
+
raise ArgumentError, "cannot interpret column widths"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# Returns the height of the table in PDF points.
|
178
|
+
#
|
179
|
+
def height
|
180
|
+
cells.height
|
181
|
+
end
|
182
|
+
|
183
|
+
# If +true+, designates the first row as a header row to be repeated on
|
184
|
+
# every page. Does not change row numbering -- row numbers always index into
|
185
|
+
# the data array provided, with no modification.
|
186
|
+
#
|
187
|
+
attr_writer :header
|
188
|
+
|
189
|
+
# Accepts an Array of alternating row colors to stripe the table.
|
190
|
+
#
|
191
|
+
attr_writer :row_colors
|
192
|
+
|
193
|
+
# Sets styles for all cells.
|
194
|
+
#
|
195
|
+
# pdf.table(data, :cell_style => { :borders => [:left, :right] })
|
196
|
+
#
|
197
|
+
def cell_style=(style_hash)
|
198
|
+
cells.style(style_hash)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Allows generic stylable content. This is an alternate syntax that some
|
202
|
+
# prefer to the attribute-based syntax. This code using style:
|
203
|
+
#
|
204
|
+
# pdf.table(data) do
|
205
|
+
# style(row(0), :background_color => 'ff00ff')
|
206
|
+
# style(column(0)) { |c| c.border_width += 1 }
|
207
|
+
# end
|
208
|
+
#
|
209
|
+
# is equivalent to:
|
210
|
+
#
|
211
|
+
# pdf.table(data) do
|
212
|
+
# row(0).style :background_color => 'ff00ff'
|
213
|
+
# column(0).style { |c| c.border_width += 1 }
|
214
|
+
# end
|
215
|
+
#
|
216
|
+
def style(stylable, style_hash={}, &block)
|
217
|
+
stylable.style(style_hash, &block)
|
218
|
+
end
|
219
|
+
|
220
|
+
# Draws the table onto the document at the document's current y-position.
|
221
|
+
#
|
222
|
+
def draw
|
223
|
+
# The cell y-positions are based on an infinitely long canvas. The offset
|
224
|
+
# keeps track of how much we have to add to the original, theoretical
|
225
|
+
# y-position to get to the actual position on the current page.
|
226
|
+
offset = @pdf.y
|
227
|
+
|
228
|
+
# Reference bounds are the non-stretchy bounds used to decide when to
|
229
|
+
# flow to a new column / page.
|
230
|
+
ref_bounds = @pdf.bounds.stretchy? ? @pdf.margin_box : @pdf.bounds
|
231
|
+
|
232
|
+
last_y = @pdf.y
|
233
|
+
@cells.each do |cell|
|
234
|
+
if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom
|
235
|
+
# start a new page or column
|
236
|
+
@pdf.bounds.move_past_bottom
|
237
|
+
draw_header
|
238
|
+
offset = @pdf.y - cell.y
|
239
|
+
end
|
240
|
+
|
241
|
+
# Don't modify cell.x / cell.y here, as we want to reuse the original
|
242
|
+
# values when re-inking the table. #draw should be able to be called
|
243
|
+
# multiple times.
|
244
|
+
x, y = cell.x, cell.y
|
245
|
+
y += offset
|
246
|
+
|
247
|
+
# Translate coordinates to the bounds we are in, since drawing is
|
248
|
+
# relative to the cursor, not ref_bounds.
|
249
|
+
x += @pdf.bounds.left_side - @pdf.bounds.absolute_left
|
250
|
+
y -= @pdf.bounds.absolute_bottom
|
251
|
+
|
252
|
+
# Set background color, if any.
|
253
|
+
if @row_colors && (!@header || cell.row > 0)
|
254
|
+
index = @header ? (cell.row - 1) : cell.row
|
255
|
+
cell.background_color = @row_colors[index % @row_colors.length]
|
256
|
+
end
|
257
|
+
|
258
|
+
cell.draw([x, y])
|
259
|
+
last_y = y
|
260
|
+
end
|
261
|
+
|
262
|
+
@pdf.move_cursor_to(last_y - @cells.last.height)
|
263
|
+
end
|
264
|
+
|
265
|
+
# Calculate and return the constrained column widths, taking into account
|
266
|
+
# each cell's min_width, max_width, and any user-specified constraints on
|
267
|
+
# the table or column size.
|
268
|
+
#
|
269
|
+
# Because the natural widths can be silly, this does not always work so well
|
270
|
+
# at guessing a good size for columns that have vastly different content. If
|
271
|
+
# you see weird problems like CannotFit errors or shockingly bad column
|
272
|
+
# sizes, you should specify more column widths manually.
|
273
|
+
#
|
274
|
+
def column_widths
|
275
|
+
@column_widths ||= begin
|
276
|
+
if width < cells.min_width
|
277
|
+
raise Errors::CannotFit,
|
278
|
+
"Table's width was set too small to contain its contents"
|
279
|
+
end
|
280
|
+
|
281
|
+
if width > cells.max_width
|
282
|
+
raise Errors::CannotFit,
|
283
|
+
"Table's width was set larger than its contents' maximum width"
|
284
|
+
end
|
285
|
+
|
286
|
+
if width < natural_width
|
287
|
+
# Shrink the table to fit the requested width.
|
288
|
+
f = (width - cells.min_width).to_f / (natural_width - cells.min_width)
|
289
|
+
|
290
|
+
(0...column_length).map do |c|
|
291
|
+
min, nat = column(c).min_width, column(c).width
|
292
|
+
(f * (nat - min)) + min
|
293
|
+
end
|
294
|
+
elsif width > natural_width
|
295
|
+
# Expand the table to fit the requested width.
|
296
|
+
f = (width - cells.width).to_f / (cells.max_width - cells.width)
|
297
|
+
|
298
|
+
(0...column_length).map do |c|
|
299
|
+
nat, max = column(c).width, column(c).max_width
|
300
|
+
(f * (max - nat)) + nat
|
301
|
+
end
|
302
|
+
else
|
303
|
+
natural_column_widths
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# Returns an array with the height of each row.
|
309
|
+
#
|
310
|
+
def row_heights
|
311
|
+
@natural_row_heights ||= (0...row_length).map{ |r| row(r).height }
|
312
|
+
end
|
313
|
+
|
314
|
+
protected
|
315
|
+
|
316
|
+
# Converts the array of cellable objects given into instances of
|
317
|
+
# Prawn::Table::Cell, and sets up their in-table properties so that they
|
318
|
+
# know their own position in the table.
|
319
|
+
#
|
320
|
+
def make_cells(data)
|
321
|
+
assert_proper_table_data(data)
|
322
|
+
|
323
|
+
cells = []
|
324
|
+
|
325
|
+
@row_length = data.length
|
326
|
+
@column_length = data.map{ |r| r.length }.max
|
327
|
+
|
328
|
+
data.each_with_index do |row_cells, row_number|
|
329
|
+
row_cells.each_with_index do |cell_data, column_number|
|
330
|
+
cell = Cell.make(@pdf, cell_data)
|
331
|
+
cell.extend(Cell::InTable)
|
332
|
+
cell.row = row_number
|
333
|
+
cell.column = column_number
|
334
|
+
cells << cell
|
335
|
+
end
|
336
|
+
end
|
337
|
+
cells
|
338
|
+
end
|
339
|
+
|
340
|
+
# Raises an error if the data provided cannot be converted into a valid
|
341
|
+
# table.
|
342
|
+
#
|
343
|
+
def assert_proper_table_data(data)
|
344
|
+
if data.nil? || data.empty?
|
345
|
+
raise Prawn::Errors::EmptyTable,
|
346
|
+
"data must be a non-empty, non-nil, two dimensional array " +
|
347
|
+
"of cell-convertible objects"
|
348
|
+
end
|
349
|
+
|
350
|
+
unless data.all? { |e| Array === e }
|
351
|
+
raise Prawn::Errors::InvalidTableData,
|
352
|
+
"data must be a two dimensional array of cellable objects"
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
# If the table has a header, draw it at the current position.
|
357
|
+
#
|
358
|
+
def draw_header
|
359
|
+
if @header
|
360
|
+
y = @pdf.cursor
|
361
|
+
row(0).each do |cell|
|
362
|
+
cell.y = y
|
363
|
+
cell.draw
|
364
|
+
end
|
365
|
+
@pdf.move_cursor_to(y - row(0).height)
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
# Returns an array of each column's natural (unconstrained) width.
|
370
|
+
#
|
371
|
+
def natural_column_widths
|
372
|
+
@natural_column_widths ||= (0...column_length).map { |c| column(c).width }
|
373
|
+
end
|
374
|
+
|
375
|
+
# Returns the "natural" (unconstrained) width of the table. This may be
|
376
|
+
# extremely silly; for example, the unconstrained width of a paragraph of
|
377
|
+
# text is the width it would assume if it were not wrapped at all. Could be
|
378
|
+
# a mile long.
|
379
|
+
#
|
380
|
+
def natural_width
|
381
|
+
@natural_width ||= natural_column_widths.inject(0) { |sum, w| sum + w }
|
382
|
+
end
|
383
|
+
|
384
|
+
# Assigns the calculated column widths to each cell. This ensures that each
|
385
|
+
# cell in a column is the same width. After this method is called,
|
386
|
+
# subsequent calls to column_widths and width should return the finalized
|
387
|
+
# values that will be used to ink the table.
|
388
|
+
#
|
389
|
+
def set_column_widths
|
390
|
+
column_widths.each_with_index do |w, col_num|
|
391
|
+
column(col_num).width = w
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
# Assigns the row heights to each cell. This ensures that every cell in a
|
396
|
+
# row is the same height.
|
397
|
+
#
|
398
|
+
def set_row_heights
|
399
|
+
row_heights.each_with_index { |h, row_num| row(row_num).height = h }
|
400
|
+
end
|
401
|
+
|
402
|
+
# Set each cell's position based on the widths and heights of cells
|
403
|
+
# preceding it.
|
404
|
+
#
|
405
|
+
def position_cells
|
406
|
+
# Calculate x- and y-positions as running sums of widths / heights.
|
407
|
+
x_positions = column_widths.inject([0]) { |ary, x|
|
408
|
+
ary << (ary.last + x); ary }[0..-2]
|
409
|
+
x_positions.each_with_index { |x, i| column(i).x = x }
|
410
|
+
|
411
|
+
# y-positions assume an infinitely long canvas starting at zero -- this
|
412
|
+
# is corrected for in Table#draw, and page breaks are properly inserted.
|
413
|
+
y_positions = row_heights.inject([0]) { |ary, y|
|
414
|
+
ary << (ary.last - y); ary}[0..-2]
|
415
|
+
y_positions.each_with_index { |y, i| row(i).y = y }
|
416
|
+
end
|
417
|
+
|
418
|
+
end
|
419
|
+
|
420
|
+
|
421
|
+
end
|