prawn-git 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (252) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +10 -0
  3. data/COPYING +2 -0
  4. data/GPLv2 +340 -0
  5. data/GPLv3 +674 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE +56 -0
  8. data/Rakefile +55 -0
  9. data/data/fonts/Courier-Bold.afm +342 -0
  10. data/data/fonts/Courier-BoldOblique.afm +342 -0
  11. data/data/fonts/Courier-Oblique.afm +342 -0
  12. data/data/fonts/Courier.afm +342 -0
  13. data/data/fonts/Helvetica-Bold.afm +2827 -0
  14. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  15. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  16. data/data/fonts/Helvetica.afm +3051 -0
  17. data/data/fonts/MustRead.html +19 -0
  18. data/data/fonts/Symbol.afm +213 -0
  19. data/data/fonts/Times-Bold.afm +2588 -0
  20. data/data/fonts/Times-BoldItalic.afm +2384 -0
  21. data/data/fonts/Times-Italic.afm +2667 -0
  22. data/data/fonts/Times-Roman.afm +2419 -0
  23. data/data/fonts/ZapfDingbats.afm +225 -0
  24. data/data/images/16bit.alpha +0 -0
  25. data/data/images/16bit.color +0 -0
  26. data/data/images/16bit.png +0 -0
  27. data/data/images/arrow.png +0 -0
  28. data/data/images/arrow2.png +0 -0
  29. data/data/images/dice.alpha +0 -0
  30. data/data/images/dice.color +0 -0
  31. data/data/images/dice.png +0 -0
  32. data/data/images/dice_interlaced.png +0 -0
  33. data/data/images/fractal.jpg +0 -0
  34. data/data/images/indexed_color.dat +0 -0
  35. data/data/images/indexed_color.png +0 -0
  36. data/data/images/letterhead.jpg +0 -0
  37. data/data/images/license.md +8 -0
  38. data/data/images/page_white_text.alpha +0 -0
  39. data/data/images/page_white_text.color +0 -0
  40. data/data/images/page_white_text.png +0 -0
  41. data/data/images/pal_bk.png +0 -0
  42. data/data/images/pigs.jpg +0 -0
  43. data/data/images/prawn.png +0 -0
  44. data/data/images/ruport.png +0 -0
  45. data/data/images/ruport_data.dat +0 -0
  46. data/data/images/ruport_transparent.png +0 -0
  47. data/data/images/ruport_type0.png +0 -0
  48. data/data/images/stef.jpg +0 -0
  49. data/data/images/tru256.bmp +0 -0
  50. data/data/images/web-links.dat +1 -0
  51. data/data/images/web-links.png +0 -0
  52. data/data/pdfs/complex_template.pdf +0 -0
  53. data/data/pdfs/contains_ttf_font.pdf +0 -0
  54. data/data/pdfs/encrypted.pdf +0 -0
  55. data/data/pdfs/form.pdf +820 -0
  56. data/data/pdfs/hexagon.pdf +61 -0
  57. data/data/pdfs/indirect_reference.pdf +86 -0
  58. data/data/pdfs/multipage_template.pdf +127 -0
  59. data/data/pdfs/nested_pages.pdf +118 -0
  60. data/data/pdfs/page_without_mediabox.pdf +193 -0
  61. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  62. data/data/pdfs/two_hexagons.pdf +90 -0
  63. data/data/pdfs/version_1_6.pdf +61 -0
  64. data/data/shift_jis_text.txt +1 -0
  65. data/lib/prawn.rb +89 -0
  66. data/lib/prawn/document.rb +706 -0
  67. data/lib/prawn/document/bounding_box.rb +539 -0
  68. data/lib/prawn/document/column_box.rb +144 -0
  69. data/lib/prawn/document/internals.rb +58 -0
  70. data/lib/prawn/document/span.rb +57 -0
  71. data/lib/prawn/encoding.rb +87 -0
  72. data/lib/prawn/errors.rb +80 -0
  73. data/lib/prawn/font.rb +413 -0
  74. data/lib/prawn/font/afm.rb +256 -0
  75. data/lib/prawn/font/dfont.rb +43 -0
  76. data/lib/prawn/font/ttf.rb +355 -0
  77. data/lib/prawn/font_metric_cache.rb +46 -0
  78. data/lib/prawn/graphics.rb +646 -0
  79. data/lib/prawn/graphics/cap_style.rb +47 -0
  80. data/lib/prawn/graphics/color.rb +232 -0
  81. data/lib/prawn/graphics/dash.rb +109 -0
  82. data/lib/prawn/graphics/join_style.rb +49 -0
  83. data/lib/prawn/graphics/patterns.rb +126 -0
  84. data/lib/prawn/graphics/transformation.rb +157 -0
  85. data/lib/prawn/graphics/transparency.rb +101 -0
  86. data/lib/prawn/grid.rb +279 -0
  87. data/lib/prawn/image_handler.rb +44 -0
  88. data/lib/prawn/images.rb +199 -0
  89. data/lib/prawn/images/image.rb +49 -0
  90. data/lib/prawn/images/jpg.rb +91 -0
  91. data/lib/prawn/images/png.rb +290 -0
  92. data/lib/prawn/measurement_extensions.rb +50 -0
  93. data/lib/prawn/measurements.rb +77 -0
  94. data/lib/prawn/outline.rb +289 -0
  95. data/lib/prawn/repeater.rb +124 -0
  96. data/lib/prawn/security.rb +288 -0
  97. data/lib/prawn/security/arcfour.rb +54 -0
  98. data/lib/prawn/soft_mask.rb +94 -0
  99. data/lib/prawn/stamp.rb +136 -0
  100. data/lib/prawn/text.rb +437 -0
  101. data/lib/prawn/text/box.rb +141 -0
  102. data/lib/prawn/text/formatted.rb +7 -0
  103. data/lib/prawn/text/formatted/arranger.rb +290 -0
  104. data/lib/prawn/text/formatted/box.rb +614 -0
  105. data/lib/prawn/text/formatted/fragment.rb +264 -0
  106. data/lib/prawn/text/formatted/line_wrap.rb +277 -0
  107. data/lib/prawn/text/formatted/parser.rb +224 -0
  108. data/lib/prawn/text/formatted/wrap.rb +160 -0
  109. data/lib/prawn/utilities.rb +46 -0
  110. data/lib/prawn/version.rb +5 -0
  111. data/lib/prawn/view.rb +91 -0
  112. data/manual/absolute_position.pdf +0 -0
  113. data/manual/basic_concepts/adding_pages.rb +27 -0
  114. data/manual/basic_concepts/basic_concepts.rb +36 -0
  115. data/manual/basic_concepts/creation.rb +39 -0
  116. data/manual/basic_concepts/cursor.rb +33 -0
  117. data/manual/basic_concepts/measurement.rb +25 -0
  118. data/manual/basic_concepts/origin.rb +38 -0
  119. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  120. data/manual/basic_concepts/view.rb +42 -0
  121. data/manual/bounding_box/bounding_box.rb +39 -0
  122. data/manual/bounding_box/bounds.rb +49 -0
  123. data/manual/bounding_box/canvas.rb +24 -0
  124. data/manual/bounding_box/creation.rb +23 -0
  125. data/manual/bounding_box/indentation.rb +46 -0
  126. data/manual/bounding_box/nesting.rb +45 -0
  127. data/manual/bounding_box/russian_boxes.rb +40 -0
  128. data/manual/bounding_box/stretchy.rb +31 -0
  129. data/manual/contents.rb +29 -0
  130. data/manual/cover.rb +39 -0
  131. data/manual/document_and_page_options/background.rb +27 -0
  132. data/manual/document_and_page_options/document_and_page_options.rb +32 -0
  133. data/manual/document_and_page_options/metadata.rb +23 -0
  134. data/manual/document_and_page_options/page_margins.rb +38 -0
  135. data/manual/document_and_page_options/page_size.rb +34 -0
  136. data/manual/document_and_page_options/print_scaling.rb +20 -0
  137. data/manual/example_helper.rb +7 -0
  138. data/manual/graphics/circle_and_ellipse.rb +22 -0
  139. data/manual/graphics/color.rb +24 -0
  140. data/manual/graphics/common_lines.rb +30 -0
  141. data/manual/graphics/fill_and_stroke.rb +42 -0
  142. data/manual/graphics/fill_rules.rb +37 -0
  143. data/manual/graphics/gradients.rb +37 -0
  144. data/manual/graphics/graphics.rb +58 -0
  145. data/manual/graphics/helper.rb +24 -0
  146. data/manual/graphics/line_width.rb +35 -0
  147. data/manual/graphics/lines_and_curves.rb +41 -0
  148. data/manual/graphics/polygon.rb +29 -0
  149. data/manual/graphics/rectangle.rb +21 -0
  150. data/manual/graphics/rotate.rb +28 -0
  151. data/manual/graphics/scale.rb +41 -0
  152. data/manual/graphics/soft_masks.rb +46 -0
  153. data/manual/graphics/stroke_cap.rb +31 -0
  154. data/manual/graphics/stroke_dash.rb +48 -0
  155. data/manual/graphics/stroke_join.rb +30 -0
  156. data/manual/graphics/translate.rb +29 -0
  157. data/manual/graphics/transparency.rb +35 -0
  158. data/manual/how_to_read_this_manual.rb +40 -0
  159. data/manual/images/absolute_position.rb +23 -0
  160. data/manual/images/fit.rb +21 -0
  161. data/manual/images/horizontal.rb +25 -0
  162. data/manual/images/images.rb +40 -0
  163. data/manual/images/plain_image.rb +18 -0
  164. data/manual/images/scale.rb +22 -0
  165. data/manual/images/vertical.rb +28 -0
  166. data/manual/images/width_and_height.rb +25 -0
  167. data/manual/layout/boxes.rb +27 -0
  168. data/manual/layout/content.rb +25 -0
  169. data/manual/layout/layout.rb +28 -0
  170. data/manual/layout/simple_grid.rb +23 -0
  171. data/manual/outline/add_subsection_to.rb +61 -0
  172. data/manual/outline/insert_section_after.rb +47 -0
  173. data/manual/outline/outline.rb +32 -0
  174. data/manual/outline/sections_and_pages.rb +67 -0
  175. data/manual/repeatable_content/alternate_page_numbering.rb +32 -0
  176. data/manual/repeatable_content/page_numbering.rb +54 -0
  177. data/manual/repeatable_content/repeatable_content.rb +32 -0
  178. data/manual/repeatable_content/repeater.rb +55 -0
  179. data/manual/repeatable_content/stamp.rb +41 -0
  180. data/manual/security/encryption.rb +31 -0
  181. data/manual/security/permissions.rb +38 -0
  182. data/manual/security/security.rb +28 -0
  183. data/manual/table.rb +16 -0
  184. data/manual/text/alignment.rb +44 -0
  185. data/manual/text/color.rb +24 -0
  186. data/manual/text/column_box.rb +32 -0
  187. data/manual/text/fallback_fonts.rb +37 -0
  188. data/manual/text/font.rb +41 -0
  189. data/manual/text/font_size.rb +45 -0
  190. data/manual/text/font_style.rb +23 -0
  191. data/manual/text/formatted_callbacks.rb +60 -0
  192. data/manual/text/formatted_text.rb +50 -0
  193. data/manual/text/free_flowing_text.rb +51 -0
  194. data/manual/text/inline.rb +41 -0
  195. data/manual/text/kerning_and_character_spacing.rb +39 -0
  196. data/manual/text/leading.rb +25 -0
  197. data/manual/text/line_wrapping.rb +41 -0
  198. data/manual/text/paragraph_indentation.rb +34 -0
  199. data/manual/text/positioned_text.rb +38 -0
  200. data/manual/text/registering_families.rb +48 -0
  201. data/manual/text/rendering_and_color.rb +37 -0
  202. data/manual/text/right_to_left_text.rb +47 -0
  203. data/manual/text/rotation.rb +43 -0
  204. data/manual/text/single_usage.rb +37 -0
  205. data/manual/text/text.rb +73 -0
  206. data/manual/text/text_box_excess.rb +32 -0
  207. data/manual/text/text_box_extensions.rb +45 -0
  208. data/manual/text/text_box_overflow.rb +48 -0
  209. data/manual/text/utf8.rb +28 -0
  210. data/manual/text/win_ansi_charset.rb +60 -0
  211. data/prawn.gemspec +45 -0
  212. data/spec/acceptance/png.rb +25 -0
  213. data/spec/annotations_spec.rb +74 -0
  214. data/spec/bounding_box_spec.rb +510 -0
  215. data/spec/column_box_spec.rb +65 -0
  216. data/spec/data/curves.pdf +66 -0
  217. data/spec/destinations_spec.rb +15 -0
  218. data/spec/document_spec.rb +748 -0
  219. data/spec/extensions/encoding_helpers.rb +11 -0
  220. data/spec/extensions/mocha.rb +46 -0
  221. data/spec/font_metric_cache_spec.rb +52 -0
  222. data/spec/font_spec.rb +474 -0
  223. data/spec/formatted_text_arranger_spec.rb +421 -0
  224. data/spec/formatted_text_box_spec.rb +705 -0
  225. data/spec/formatted_text_fragment_spec.rb +298 -0
  226. data/spec/graphics_spec.rb +683 -0
  227. data/spec/grid_spec.rb +96 -0
  228. data/spec/image_handler_spec.rb +54 -0
  229. data/spec/images_spec.rb +153 -0
  230. data/spec/inline_formatted_text_parser_spec.rb +564 -0
  231. data/spec/jpg_spec.rb +25 -0
  232. data/spec/line_wrap_spec.rb +367 -0
  233. data/spec/measurement_units_spec.rb +25 -0
  234. data/spec/outline_spec.rb +430 -0
  235. data/spec/png_spec.rb +245 -0
  236. data/spec/reference_spec.rb +25 -0
  237. data/spec/repeater_spec.rb +160 -0
  238. data/spec/security_spec.rb +158 -0
  239. data/spec/soft_mask_spec.rb +79 -0
  240. data/spec/span_spec.rb +44 -0
  241. data/spec/spec_helper.rb +54 -0
  242. data/spec/stamp_spec.rb +160 -0
  243. data/spec/stroke_styles_spec.rb +211 -0
  244. data/spec/text_at_spec.rb +143 -0
  245. data/spec/text_box_spec.rb +1043 -0
  246. data/spec/text_rendering_mode_spec.rb +45 -0
  247. data/spec/text_spacing_spec.rb +93 -0
  248. data/spec/text_spec.rb +557 -0
  249. data/spec/text_with_inline_formatting_spec.rb +35 -0
  250. data/spec/transparency_spec.rb +91 -0
  251. data/spec/view_spec.rb +43 -0
  252. 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
+