prawn-git 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +10 -0
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +11 -0
- data/LICENSE +56 -0
- data/Rakefile +55 -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/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/images/16bit.alpha +0 -0
- data/data/images/16bit.color +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/dice.alpha +0 -0
- data/data/images/dice.color +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/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/license.md +8 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pal_bk.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.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/form.pdf +820 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/multipage_template.pdf +127 -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/lib/prawn.rb +89 -0
- data/lib/prawn/document.rb +706 -0
- data/lib/prawn/document/bounding_box.rb +539 -0
- data/lib/prawn/document/column_box.rb +144 -0
- data/lib/prawn/document/internals.rb +58 -0
- data/lib/prawn/document/span.rb +57 -0
- data/lib/prawn/encoding.rb +87 -0
- data/lib/prawn/errors.rb +80 -0
- data/lib/prawn/font.rb +413 -0
- data/lib/prawn/font/afm.rb +256 -0
- data/lib/prawn/font/dfont.rb +43 -0
- data/lib/prawn/font/ttf.rb +355 -0
- data/lib/prawn/font_metric_cache.rb +46 -0
- data/lib/prawn/graphics.rb +646 -0
- data/lib/prawn/graphics/cap_style.rb +47 -0
- data/lib/prawn/graphics/color.rb +232 -0
- data/lib/prawn/graphics/dash.rb +109 -0
- data/lib/prawn/graphics/join_style.rb +49 -0
- data/lib/prawn/graphics/patterns.rb +126 -0
- data/lib/prawn/graphics/transformation.rb +157 -0
- data/lib/prawn/graphics/transparency.rb +101 -0
- data/lib/prawn/grid.rb +279 -0
- data/lib/prawn/image_handler.rb +44 -0
- data/lib/prawn/images.rb +199 -0
- data/lib/prawn/images/image.rb +49 -0
- data/lib/prawn/images/jpg.rb +91 -0
- data/lib/prawn/images/png.rb +290 -0
- data/lib/prawn/measurement_extensions.rb +50 -0
- data/lib/prawn/measurements.rb +77 -0
- data/lib/prawn/outline.rb +289 -0
- data/lib/prawn/repeater.rb +124 -0
- data/lib/prawn/security.rb +288 -0
- data/lib/prawn/security/arcfour.rb +54 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +136 -0
- data/lib/prawn/text.rb +437 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +7 -0
- data/lib/prawn/text/formatted/arranger.rb +290 -0
- data/lib/prawn/text/formatted/box.rb +614 -0
- data/lib/prawn/text/formatted/fragment.rb +264 -0
- data/lib/prawn/text/formatted/line_wrap.rb +277 -0
- data/lib/prawn/text/formatted/parser.rb +224 -0
- data/lib/prawn/text/formatted/wrap.rb +160 -0
- data/lib/prawn/utilities.rb +46 -0
- data/lib/prawn/version.rb +5 -0
- data/lib/prawn/view.rb +91 -0
- data/manual/absolute_position.pdf +0 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +36 -0
- data/manual/basic_concepts/creation.rb +39 -0
- data/manual/basic_concepts/cursor.rb +33 -0
- data/manual/basic_concepts/measurement.rb +25 -0
- data/manual/basic_concepts/origin.rb +38 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
- data/manual/basic_concepts/view.rb +42 -0
- data/manual/bounding_box/bounding_box.rb +39 -0
- data/manual/bounding_box/bounds.rb +49 -0
- data/manual/bounding_box/canvas.rb +24 -0
- data/manual/bounding_box/creation.rb +23 -0
- data/manual/bounding_box/indentation.rb +46 -0
- data/manual/bounding_box/nesting.rb +45 -0
- data/manual/bounding_box/russian_boxes.rb +40 -0
- data/manual/bounding_box/stretchy.rb +31 -0
- data/manual/contents.rb +29 -0
- data/manual/cover.rb +39 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +32 -0
- data/manual/document_and_page_options/metadata.rb +23 -0
- data/manual/document_and_page_options/page_margins.rb +38 -0
- data/manual/document_and_page_options/page_size.rb +34 -0
- data/manual/document_and_page_options/print_scaling.rb +20 -0
- data/manual/example_helper.rb +7 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +30 -0
- data/manual/graphics/fill_and_stroke.rb +42 -0
- data/manual/graphics/fill_rules.rb +37 -0
- data/manual/graphics/gradients.rb +37 -0
- data/manual/graphics/graphics.rb +58 -0
- data/manual/graphics/helper.rb +24 -0
- data/manual/graphics/line_width.rb +35 -0
- data/manual/graphics/lines_and_curves.rb +41 -0
- data/manual/graphics/polygon.rb +29 -0
- data/manual/graphics/rectangle.rb +21 -0
- data/manual/graphics/rotate.rb +28 -0
- data/manual/graphics/scale.rb +41 -0
- data/manual/graphics/soft_masks.rb +46 -0
- data/manual/graphics/stroke_cap.rb +31 -0
- data/manual/graphics/stroke_dash.rb +48 -0
- data/manual/graphics/stroke_join.rb +30 -0
- data/manual/graphics/translate.rb +29 -0
- data/manual/graphics/transparency.rb +35 -0
- data/manual/how_to_read_this_manual.rb +40 -0
- data/manual/images/absolute_position.rb +23 -0
- data/manual/images/fit.rb +21 -0
- data/manual/images/horizontal.rb +25 -0
- data/manual/images/images.rb +40 -0
- data/manual/images/plain_image.rb +18 -0
- data/manual/images/scale.rb +22 -0
- data/manual/images/vertical.rb +28 -0
- data/manual/images/width_and_height.rb +25 -0
- data/manual/layout/boxes.rb +27 -0
- data/manual/layout/content.rb +25 -0
- data/manual/layout/layout.rb +28 -0
- data/manual/layout/simple_grid.rb +23 -0
- data/manual/outline/add_subsection_to.rb +61 -0
- data/manual/outline/insert_section_after.rb +47 -0
- data/manual/outline/outline.rb +32 -0
- data/manual/outline/sections_and_pages.rb +67 -0
- data/manual/repeatable_content/alternate_page_numbering.rb +32 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +32 -0
- data/manual/repeatable_content/repeater.rb +55 -0
- data/manual/repeatable_content/stamp.rb +41 -0
- data/manual/security/encryption.rb +31 -0
- data/manual/security/permissions.rb +38 -0
- data/manual/security/security.rb +28 -0
- data/manual/table.rb +16 -0
- data/manual/text/alignment.rb +44 -0
- data/manual/text/color.rb +24 -0
- data/manual/text/column_box.rb +32 -0
- data/manual/text/fallback_fonts.rb +37 -0
- data/manual/text/font.rb +41 -0
- data/manual/text/font_size.rb +45 -0
- data/manual/text/font_style.rb +23 -0
- data/manual/text/formatted_callbacks.rb +60 -0
- data/manual/text/formatted_text.rb +50 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/inline.rb +41 -0
- data/manual/text/kerning_and_character_spacing.rb +39 -0
- data/manual/text/leading.rb +25 -0
- data/manual/text/line_wrapping.rb +41 -0
- data/manual/text/paragraph_indentation.rb +34 -0
- data/manual/text/positioned_text.rb +38 -0
- data/manual/text/registering_families.rb +48 -0
- data/manual/text/rendering_and_color.rb +37 -0
- data/manual/text/right_to_left_text.rb +47 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +73 -0
- data/manual/text/text_box_excess.rb +32 -0
- data/manual/text/text_box_extensions.rb +45 -0
- data/manual/text/text_box_overflow.rb +48 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +60 -0
- data/prawn.gemspec +45 -0
- data/spec/acceptance/png.rb +25 -0
- data/spec/annotations_spec.rb +74 -0
- data/spec/bounding_box_spec.rb +510 -0
- data/spec/column_box_spec.rb +65 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +748 -0
- data/spec/extensions/encoding_helpers.rb +11 -0
- data/spec/extensions/mocha.rb +46 -0
- data/spec/font_metric_cache_spec.rb +52 -0
- data/spec/font_spec.rb +474 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +705 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +683 -0
- data/spec/grid_spec.rb +96 -0
- data/spec/image_handler_spec.rb +54 -0
- data/spec/images_spec.rb +153 -0
- data/spec/inline_formatted_text_parser_spec.rb +564 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +367 -0
- data/spec/measurement_units_spec.rb +25 -0
- data/spec/outline_spec.rb +430 -0
- data/spec/png_spec.rb +245 -0
- data/spec/reference_spec.rb +25 -0
- data/spec/repeater_spec.rb +160 -0
- data/spec/security_spec.rb +158 -0
- data/spec/soft_mask_spec.rb +79 -0
- data/spec/span_spec.rb +44 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/stamp_spec.rb +160 -0
- data/spec/stroke_styles_spec.rb +211 -0
- data/spec/text_at_spec.rb +143 -0
- data/spec/text_box_spec.rb +1043 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +557 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +91 -0
- data/spec/view_spec.rb +43 -0
- metadata +509 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# measurement_extensions.rb: Core extensions for Prawn::Measurements
|
3
|
+
#
|
4
|
+
# Copyright December 2008, Florian Witteler. All Rights Reserved.
|
5
|
+
#
|
6
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
7
|
+
|
8
|
+
require_relative 'measurements'
|
9
|
+
|
10
|
+
# @group Stable API
|
11
|
+
|
12
|
+
class Numeric
|
13
|
+
include Prawn::Measurements
|
14
|
+
# prawns' basic unit is PostScript-Point
|
15
|
+
# 72 points per inch
|
16
|
+
|
17
|
+
# @group Experimental API
|
18
|
+
|
19
|
+
def mm
|
20
|
+
return mm2pt(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def cm
|
24
|
+
return cm2pt(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
def dm
|
28
|
+
return dm2pt(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def m
|
32
|
+
return m2pt(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def in
|
36
|
+
return in2pt(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def yd
|
40
|
+
return yd2pt(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def ft
|
44
|
+
return ft2pt(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
def pt
|
48
|
+
return pt2pt(self)
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# measurements.rb: Conversions from other measurements to PDF points
|
3
|
+
#
|
4
|
+
# Copyright December 2008, Florian Witteler. All Rights Reserved.
|
5
|
+
#
|
6
|
+
module Prawn
|
7
|
+
# @group Stable API
|
8
|
+
|
9
|
+
module Measurements
|
10
|
+
|
11
|
+
# ============================================================================
|
12
|
+
#metric conversions
|
13
|
+
def cm2mm(cm)
|
14
|
+
return cm*10
|
15
|
+
end
|
16
|
+
|
17
|
+
def dm2mm(dm)
|
18
|
+
return dm*100
|
19
|
+
end
|
20
|
+
|
21
|
+
def m2mm(m)
|
22
|
+
return m*1000
|
23
|
+
end
|
24
|
+
|
25
|
+
# ============================================================================
|
26
|
+
# imperial conversions
|
27
|
+
# from http://en.wikipedia.org/wiki/Imperial_units
|
28
|
+
|
29
|
+
def ft2in(ft)
|
30
|
+
return ft * 12
|
31
|
+
end
|
32
|
+
|
33
|
+
def yd2in(yd)
|
34
|
+
return yd*36
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# ============================================================================
|
39
|
+
# PostscriptPoint-converisons
|
40
|
+
|
41
|
+
def pt2pt(pt)
|
42
|
+
return pt
|
43
|
+
end
|
44
|
+
|
45
|
+
def in2pt(inch)
|
46
|
+
return inch * 72
|
47
|
+
end
|
48
|
+
|
49
|
+
def ft2pt(ft)
|
50
|
+
return in2pt(ft2in(ft))
|
51
|
+
end
|
52
|
+
|
53
|
+
def yd2pt(yd)
|
54
|
+
return in2pt(yd2in(yd))
|
55
|
+
end
|
56
|
+
|
57
|
+
def mm2pt(mm)
|
58
|
+
return mm*(72 / 25.4)
|
59
|
+
end
|
60
|
+
|
61
|
+
def cm2pt(cm)
|
62
|
+
return mm2pt(cm2mm(cm))
|
63
|
+
end
|
64
|
+
|
65
|
+
def dm2pt(dm)
|
66
|
+
return mm2pt(dm2mm(dm))
|
67
|
+
end
|
68
|
+
|
69
|
+
def m2pt(m)
|
70
|
+
return mm2pt(m2mm(m))
|
71
|
+
end
|
72
|
+
|
73
|
+
def pt2mm(pt)
|
74
|
+
return pt * 1 / mm2pt(1)# (25.4 / 72)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Prawn
|
4
|
+
class Document
|
5
|
+
# @group Stable API
|
6
|
+
|
7
|
+
# Lazily instantiates a Prawn::Outline object for document. This is used as point of entry
|
8
|
+
# to methods to build the outline tree for a document's table of contents.
|
9
|
+
def outline
|
10
|
+
@outline ||= Outline.new(self)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# The Outline class organizes the outline tree items for the document.
|
15
|
+
# Note that the prev and parent instance variables are adjusted while navigating
|
16
|
+
# through the nested blocks. These variables along with the presence or absense
|
17
|
+
# of blocks are the primary means by which the relations for the various
|
18
|
+
# OutlineItems and the OutlineRoot are set. Unfortunately, the best way to
|
19
|
+
# understand how this works is to follow the method calls through a real example.
|
20
|
+
#
|
21
|
+
# Some ideas for the organization of this class were gleaned from name_tree. In
|
22
|
+
# particular the way in which the OutlineItems are finally rendered into document
|
23
|
+
# objects in PdfObject through a hash.
|
24
|
+
#
|
25
|
+
class Outline
|
26
|
+
# @private
|
27
|
+
attr_accessor :parent, :prev, :document, :items
|
28
|
+
|
29
|
+
def initialize(document)
|
30
|
+
@document = document
|
31
|
+
@parent = root
|
32
|
+
@prev = nil
|
33
|
+
@items = {}
|
34
|
+
end
|
35
|
+
|
36
|
+
# @group Stable API
|
37
|
+
|
38
|
+
# Returns the current page number of the document
|
39
|
+
def page_number
|
40
|
+
@document.page_number
|
41
|
+
end
|
42
|
+
|
43
|
+
# Defines/Updates an outline for the document.
|
44
|
+
# The outline is an optional nested index that appears on the side of a PDF
|
45
|
+
# document usually with direct links to pages. The outline DSL is defined by nested
|
46
|
+
# blocks involving two methods: section and page; see the documentation on those methods
|
47
|
+
# for their arguments and options. Note that one can also use outline#update
|
48
|
+
# to add more sections to the end of the outline tree using the same syntax and scope.
|
49
|
+
#
|
50
|
+
# The syntax is best illustrated with an example:
|
51
|
+
#
|
52
|
+
# Prawn::Document.generate(outlined_document.pdf) do
|
53
|
+
# text "Page 1. This is the first Chapter. "
|
54
|
+
# start_new_page
|
55
|
+
# text "Page 2. More in the first Chapter. "
|
56
|
+
# start_new_page
|
57
|
+
# outline.define do
|
58
|
+
# section 'Chapter 1', :destination => 1, :closed => true do
|
59
|
+
# page :destination => 1, :title => 'Page 1'
|
60
|
+
# page :destination => 2, :title => 'Page 2'
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
# start_new_page do
|
64
|
+
# outline.update do
|
65
|
+
# section 'Chapter 2', :destination => 2, do
|
66
|
+
# page :destination => 3, :title => 'Page 3'
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
def define(&block)
|
72
|
+
instance_eval(&block) if block
|
73
|
+
end
|
74
|
+
|
75
|
+
alias :update :define
|
76
|
+
|
77
|
+
# Inserts an outline section to the outline tree (see outline#define).
|
78
|
+
# Although you will probably choose to exclusively use outline#define so
|
79
|
+
# that your outline tree is contained and easy to manage, this method
|
80
|
+
# gives you the option to insert sections to the outline tree at any point
|
81
|
+
# during document generation. This method allows you to add a child subsection
|
82
|
+
# to any other item at any level in the outline tree.
|
83
|
+
# Currently the only way to locate the place of entry is with the title for the
|
84
|
+
# item. If your title names are not unique consider using define_outline.
|
85
|
+
# The method takes the following arguments:
|
86
|
+
# title: a string that must match an outline title to add the subsection to
|
87
|
+
# position: either :first or :last(the default) where the subsection will be placed relative
|
88
|
+
# to other child elements. If you need to position your subsection in between
|
89
|
+
# other elements then consider using #insert_section_after
|
90
|
+
# block: uses the same DSL syntax as outline#define, for example:
|
91
|
+
#
|
92
|
+
# Consider using this method inside of outline.update if you want to have the outline object
|
93
|
+
# to be scoped as self (see #insert_section_after example).
|
94
|
+
#
|
95
|
+
# go_to_page 2
|
96
|
+
# start_new_page
|
97
|
+
# text "Inserted Page"
|
98
|
+
# outline.add_subsection_to :title => 'Page 2', :first do
|
99
|
+
# outline.page :destination => page_number, :title => "Inserted Page"
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
def add_subsection_to(title, position = :last, &block)
|
103
|
+
@parent = items[title]
|
104
|
+
raise Prawn::Errors::UnknownOutlineTitle,
|
105
|
+
"\n No outline item with title: '#{title}' exists in the outline tree" unless @parent
|
106
|
+
@prev = position == :first ? nil : @parent.data.last
|
107
|
+
nxt = position == :first ? @parent.data.first : nil
|
108
|
+
insert_section(nxt, &block)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Inserts an outline section to the outline tree (see outline#define).
|
112
|
+
# Although you will probably choose to exclusively use outline#define so
|
113
|
+
# that your outline tree is contained and easy to manage, this method
|
114
|
+
# gives you the option to insert sections to the outline tree at any point
|
115
|
+
# during document generation. Unlike outline.add_section, this method allows
|
116
|
+
# you to enter a section after any other item at any level in the outline tree.
|
117
|
+
# Currently the only way to locate the place of entry is with the title for the
|
118
|
+
# item. If your title names are not unique consider using define_outline.
|
119
|
+
# The method takes the following arguments:
|
120
|
+
# title: the title of other section or page to insert new section after
|
121
|
+
# block: uses the same DSL syntax as outline#define, for example:
|
122
|
+
#
|
123
|
+
# go_to_page 2
|
124
|
+
# start_new_page
|
125
|
+
# text "Inserted Page"
|
126
|
+
# update_outline do
|
127
|
+
# insert_section_after :title => 'Page 2' do
|
128
|
+
# page :destination => page_number, :title => "Inserted Page"
|
129
|
+
# end
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
def insert_section_after(title, &block)
|
133
|
+
@prev = items[title]
|
134
|
+
raise Prawn::Errors::UnknownOutlineTitle,
|
135
|
+
"\n No outline item with title: '#{title}' exists in the outline tree" unless @prev
|
136
|
+
@parent = @prev.data.parent
|
137
|
+
nxt = @prev.data.next
|
138
|
+
insert_section(nxt, &block)
|
139
|
+
end
|
140
|
+
|
141
|
+
# See outline#define above for documentation on how this is used in that context
|
142
|
+
#
|
143
|
+
# Adds an outine section to the outline tree.
|
144
|
+
# Although you will probably choose to exclusively use outline#define so
|
145
|
+
# that your outline tree is contained and easy to manage, this method
|
146
|
+
# gives you the option to add sections to the outline tree at any point
|
147
|
+
# during document generation. When not being called from within another #section block
|
148
|
+
# the section will be added at the top level after the other root elements of the outline.
|
149
|
+
# For more flexible placement try using outline#insert_section_after and/or
|
150
|
+
# outline#add_subsection_to
|
151
|
+
# Takes the following arguments:
|
152
|
+
# title: the outline text that appears for the section.
|
153
|
+
# options: destination - optional integer defining the page number for a destination link
|
154
|
+
# to the top of the page (using a :FIT destination).
|
155
|
+
# - or an array with a custom destination (see the #dest_* methods of the
|
156
|
+
# PDF::Destination module)
|
157
|
+
# closed - whether the section should show its nested outline elements.
|
158
|
+
# - defaults to false.
|
159
|
+
# block: more nested subsections and/or page blocks
|
160
|
+
#
|
161
|
+
# example usage:
|
162
|
+
#
|
163
|
+
# outline.section 'Added Section', :destination => 3 do
|
164
|
+
# outline.page :destionation => 3, :title => 'Page 3'
|
165
|
+
# end
|
166
|
+
def section(title, options = {}, &block)
|
167
|
+
add_outline_item(title, options, &block)
|
168
|
+
end
|
169
|
+
|
170
|
+
# See Outline#define above for more documentation on how it is used in that context
|
171
|
+
#
|
172
|
+
# Adds a page to the outline.
|
173
|
+
# Although you will probably choose to exclusively use outline#define so
|
174
|
+
# that your outline tree is contained and easy to manage, this method also
|
175
|
+
# gives you the option to add pages to the root of outline tree at any point
|
176
|
+
# during document generation. Note that the page will be added at the
|
177
|
+
# top level after the other root outline elements. For more flexible placement try
|
178
|
+
# using outline#insert_section_after and/or outline#add_subsection_to.
|
179
|
+
#
|
180
|
+
# Takes the following arguments:
|
181
|
+
# options:
|
182
|
+
# title - REQUIRED. The outline text that appears for the page.
|
183
|
+
# destination - optional integer defining the page number for a destination link
|
184
|
+
# to the top of the page (using a :FIT destination).
|
185
|
+
# - or an array with a custom destination (see the #dest_* methods of the
|
186
|
+
# PDF::Destination module)
|
187
|
+
# closed - whether the section should show its nested outline elements.
|
188
|
+
# - defaults to false.
|
189
|
+
# example usage:
|
190
|
+
#
|
191
|
+
# outline.page :title => "Very Last Page"
|
192
|
+
# Note: this method is almost identical to section except that it does not accept a block
|
193
|
+
# thereby defining the outline item as a leaf on the outline tree structure.
|
194
|
+
def page(options = {})
|
195
|
+
if options[:title]
|
196
|
+
title = options[:title]
|
197
|
+
else
|
198
|
+
raise Prawn::Errors::RequiredOption,
|
199
|
+
"\nTitle is a required option for page"
|
200
|
+
end
|
201
|
+
add_outline_item(title, options)
|
202
|
+
end
|
203
|
+
|
204
|
+
private
|
205
|
+
|
206
|
+
# The Outline dictionary (12.3.3) for this document. It is
|
207
|
+
# lazily initialized, so that documents that do not have an outline
|
208
|
+
# do not incur the additional overhead.
|
209
|
+
def root
|
210
|
+
document.state.store.root.data[:Outlines] ||= document.ref!(PDF::Core::OutlineRoot.new)
|
211
|
+
end
|
212
|
+
|
213
|
+
def add_outline_item(title, options, &block)
|
214
|
+
outline_item = create_outline_item(title, options)
|
215
|
+
set_relations(outline_item)
|
216
|
+
increase_count
|
217
|
+
set_variables_for_block(outline_item, block)
|
218
|
+
block.call if block
|
219
|
+
reset_parent(outline_item)
|
220
|
+
end
|
221
|
+
|
222
|
+
def create_outline_item(title, options)
|
223
|
+
outline_item = PDF::Core::OutlineItem.new(title, parent, options)
|
224
|
+
|
225
|
+
case options[:destination]
|
226
|
+
when Integer
|
227
|
+
page_index = options[:destination] - 1
|
228
|
+
outline_item.dest = [document.state.pages[page_index].dictionary, :Fit]
|
229
|
+
when Array
|
230
|
+
outline_item.dest = options[:destination]
|
231
|
+
end
|
232
|
+
|
233
|
+
outline_item.prev = prev if @prev
|
234
|
+
items[title] = document.ref!(outline_item)
|
235
|
+
end
|
236
|
+
|
237
|
+
def set_relations(outline_item)
|
238
|
+
prev.data.next = outline_item if prev
|
239
|
+
parent.data.first = outline_item unless prev
|
240
|
+
parent.data.last = outline_item
|
241
|
+
end
|
242
|
+
|
243
|
+
def increase_count
|
244
|
+
counting_parent = parent
|
245
|
+
while counting_parent
|
246
|
+
counting_parent.data.count += 1
|
247
|
+
if counting_parent == root
|
248
|
+
counting_parent = nil
|
249
|
+
else
|
250
|
+
counting_parent = counting_parent.data.parent
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def set_variables_for_block(outline_item, block)
|
256
|
+
self.prev = block ? nil : outline_item
|
257
|
+
self.parent = outline_item if block
|
258
|
+
end
|
259
|
+
|
260
|
+
def reset_parent(outline_item)
|
261
|
+
if parent == outline_item
|
262
|
+
self.prev = outline_item
|
263
|
+
self.parent = outline_item.data.parent
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def insert_section(nxt, &block)
|
268
|
+
last = @parent.data.last
|
269
|
+
if block
|
270
|
+
block.call
|
271
|
+
end
|
272
|
+
adjust_relations(nxt, last)
|
273
|
+
reset_root_positioning
|
274
|
+
end
|
275
|
+
|
276
|
+
def adjust_relations(nxt, last)
|
277
|
+
if nxt
|
278
|
+
nxt.data.prev = @prev
|
279
|
+
@prev.data.next = nxt
|
280
|
+
@parent.data.last = last
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def reset_root_positioning
|
285
|
+
@parent = root
|
286
|
+
@prev = root.data.last
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# repeater.rb : Implements repeated page elements.
|
4
|
+
# Heavy inspired by repeating_element() in PDF::Wrapper
|
5
|
+
# http://pdf-wrapper.rubyforge.org/
|
6
|
+
#
|
7
|
+
# Copyright November 2009, Gregory Brown. All Rights Reserved.
|
8
|
+
#
|
9
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
10
|
+
|
11
|
+
module Prawn
|
12
|
+
|
13
|
+
class Document
|
14
|
+
# A list of all repeaters in the document.
|
15
|
+
# See Document#repeat for details
|
16
|
+
#
|
17
|
+
# @private
|
18
|
+
def repeaters
|
19
|
+
@repeaters ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
# @group Experimental API
|
23
|
+
|
24
|
+
# Provides a way to execute a block of code repeatedly based on a
|
25
|
+
# page_filter. Since Stamp is used under the hood, this method is very space
|
26
|
+
# efficient.
|
27
|
+
#
|
28
|
+
# Available page filters are:
|
29
|
+
# :all -- repeats on every page
|
30
|
+
# :odd -- repeats on odd pages
|
31
|
+
# :even -- repeats on even pages
|
32
|
+
# some_array -- repeats on every page listed in the array
|
33
|
+
# some_range -- repeats on every page included in the range
|
34
|
+
# some_lambda -- yields page number and repeats for true return values
|
35
|
+
#
|
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.
|
38
|
+
#
|
39
|
+
# Example:
|
40
|
+
#
|
41
|
+
# Prawn::Document.generate("repeat.pdf", :skip_page_creation => true) do
|
42
|
+
#
|
43
|
+
# repeat :all do
|
44
|
+
# draw_text "ALLLLLL", :at => bounds.top_left
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# repeat :odd do
|
48
|
+
# draw_text "ODD", :at => [0,0]
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# repeat :even do
|
52
|
+
# draw_text "EVEN", :at => [0,0]
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# repeat [1,2] do
|
56
|
+
# draw_text "[1,2]", :at => [100,0]
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# repeat 2..4 do
|
60
|
+
# draw_text "2..4", :at => [200,0]
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# repeat(lambda { |pg| pg % 3 == 0 }) do
|
64
|
+
# draw_text "Every third", :at => [250, 20]
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# 10.times do
|
68
|
+
# start_new_page
|
69
|
+
# draw_text "A wonderful page", :at => [400,400]
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# repeat(:all, :dynamic => true) do
|
73
|
+
# text page_number, :at => [500, 0]
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
def repeat(page_filter, options={}, &block)
|
79
|
+
repeaters << Prawn::Repeater.new(self, page_filter, !!options[:dynamic], &block)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class Repeater #:nodoc:
|
84
|
+
class << self
|
85
|
+
attr_writer :count
|
86
|
+
|
87
|
+
def count
|
88
|
+
@count ||= 0
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
attr_reader :name
|
93
|
+
|
94
|
+
def initialize(document, page_filter, dynamic = false, &block)
|
95
|
+
@document = document
|
96
|
+
@page_filter = page_filter
|
97
|
+
@dynamic = dynamic
|
98
|
+
@stamp_name = "prawn_repeater(#{Repeater.count})"
|
99
|
+
@document.create_stamp(@stamp_name, &block) unless dynamic
|
100
|
+
@block = block if dynamic
|
101
|
+
@graphic_state = document.state.page.graphic_state.dup
|
102
|
+
|
103
|
+
Repeater.count += 1
|
104
|
+
end
|
105
|
+
|
106
|
+
def match?(page_number)
|
107
|
+
@document.page_match?(@page_filter, page_number)
|
108
|
+
end
|
109
|
+
|
110
|
+
def run(page_number)
|
111
|
+
if !@dynamic
|
112
|
+
@document.stamp(@stamp_name) if match?(page_number)
|
113
|
+
elsif @block && match?(page_number)
|
114
|
+
@document.save_graphics_state(@graphic_state) do
|
115
|
+
@document.send(:freeze_stamp_graphics)
|
116
|
+
@block.call
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
|