alphasights-prawn 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (244) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +50 -0
  3. data/LICENSE +56 -0
  4. data/README +141 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -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/examples/bounding_box/bounding_boxes.rb +43 -0
  66. data/examples/bounding_box/indentation.rb +34 -0
  67. data/examples/bounding_box/russian_boxes.rb +36 -0
  68. data/examples/bounding_box/stretched_nesting.rb +67 -0
  69. data/examples/builder/simple.rb +28 -0
  70. data/examples/example_helper.rb +4 -0
  71. data/examples/general/background.rb +23 -0
  72. data/examples/general/canvas.rb +15 -0
  73. data/examples/general/context_sensitive_headers.rb +37 -0
  74. data/examples/general/float.rb +11 -0
  75. data/examples/general/margin.rb +36 -0
  76. data/examples/general/measurement_units.rb +51 -0
  77. data/examples/general/metadata-info.rb +16 -0
  78. data/examples/general/multi_page_layout.rb +18 -0
  79. data/examples/general/outlines.rb +50 -0
  80. data/examples/general/page_geometry.rb +31 -0
  81. data/examples/general/page_numbering.rb +15 -0
  82. data/examples/general/repeaters.rb +47 -0
  83. data/examples/general/stamp.rb +41 -0
  84. data/examples/general/templates.rb +13 -0
  85. data/examples/graphics/basic_images.rb +23 -0
  86. data/examples/graphics/chunkable.rb +38 -0
  87. data/examples/graphics/cmyk.rb +12 -0
  88. data/examples/graphics/curves.rb +11 -0
  89. data/examples/graphics/hexagon.rb +13 -0
  90. data/examples/graphics/image_fit.rb +15 -0
  91. data/examples/graphics/image_flow.rb +37 -0
  92. data/examples/graphics/image_position.rb +17 -0
  93. data/examples/graphics/line.rb +32 -0
  94. data/examples/graphics/png_types.rb +22 -0
  95. data/examples/graphics/polygons.rb +16 -0
  96. data/examples/graphics/remote_images.rb +12 -0
  97. data/examples/graphics/rounded_polygons.rb +19 -0
  98. data/examples/graphics/rounded_rectangle.rb +20 -0
  99. data/examples/graphics/ruport_style_helpers.rb +19 -0
  100. data/examples/graphics/stroke_bounds.rb +20 -0
  101. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  102. data/examples/graphics/stroke_dash.rb +42 -0
  103. data/examples/graphics/transformations.rb +52 -0
  104. data/examples/graphics/transparency.rb +26 -0
  105. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  106. data/examples/m17n/euro.rb +15 -0
  107. data/examples/m17n/sjis.rb +28 -0
  108. data/examples/m17n/utf8.rb +13 -0
  109. data/examples/m17n/win_ansi_charset.rb +54 -0
  110. data/examples/security/hello_foo.rb +8 -0
  111. data/examples/table/bill.rb +53 -0
  112. data/examples/table/cell.rb +12 -0
  113. data/examples/table/checkerboard.rb +22 -0
  114. data/examples/table/header.rb +14 -0
  115. data/examples/table/inline_format_table.rb +12 -0
  116. data/examples/table/multi_page_table.rb +9 -0
  117. data/examples/table/simple_table.rb +24 -0
  118. data/examples/table/subtable.rb +12 -0
  119. data/examples/table/widths.rb +20 -0
  120. data/examples/text/alignment.rb +18 -0
  121. data/examples/text/character_spacing.rb +12 -0
  122. data/examples/text/dfont.rb +48 -0
  123. data/examples/text/family_based_styling.rb +24 -0
  124. data/examples/text/font_calculations.rb +91 -0
  125. data/examples/text/font_size.rb +33 -0
  126. data/examples/text/hyphenation.rb +45 -0
  127. data/examples/text/indent_paragraphs.rb +22 -0
  128. data/examples/text/inline_format.rb +103 -0
  129. data/examples/text/kerning.rb +30 -0
  130. data/examples/text/rotated.rb +98 -0
  131. data/examples/text/shaped_text_box.rb +31 -0
  132. data/examples/text/simple_text.rb +17 -0
  133. data/examples/text/simple_text_ttf.rb +17 -0
  134. data/examples/text/text_box.rb +88 -0
  135. data/examples/text/text_box_returning_excess.rb +51 -0
  136. data/examples/text/text_flow.rb +67 -0
  137. data/lib/prawn.rb +27 -0
  138. data/lib/prawn/canvas.rb +119 -0
  139. data/lib/prawn/chunkable.rb +37 -0
  140. data/lib/prawn/compatibility.rb +51 -0
  141. data/lib/prawn/core.rb +85 -0
  142. data/lib/prawn/core/annotations.rb +61 -0
  143. data/lib/prawn/core/byte_string.rb +9 -0
  144. data/lib/prawn/core/chunk.rb +36 -0
  145. data/lib/prawn/core/destinations.rb +90 -0
  146. data/lib/prawn/core/document_state.rb +78 -0
  147. data/lib/prawn/core/literal_string.rb +16 -0
  148. data/lib/prawn/core/name_tree.rb +165 -0
  149. data/lib/prawn/core/object_store.rb +236 -0
  150. data/lib/prawn/core/page.rb +179 -0
  151. data/lib/prawn/core/pdf_object.rb +108 -0
  152. data/lib/prawn/core/reference.rb +112 -0
  153. data/lib/prawn/core/text.rb +140 -0
  154. data/lib/prawn/core/text/formatted/arranger.rb +266 -0
  155. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  156. data/lib/prawn/core/text/formatted/wrap.rb +112 -0
  157. data/lib/prawn/core/text/line_wrap.rb +209 -0
  158. data/lib/prawn/core/text/wrap.rb +80 -0
  159. data/lib/prawn/document.rb +573 -0
  160. data/lib/prawn/document/bounding_box.rb +425 -0
  161. data/lib/prawn/document/graphics_state.rb +48 -0
  162. data/lib/prawn/document/internals.rb +170 -0
  163. data/lib/prawn/document/page_geometry.rb +136 -0
  164. data/lib/prawn/document/snapshot.rb +87 -0
  165. data/lib/prawn/document_builder.rb +51 -0
  166. data/lib/prawn/document_builder/command.rb +38 -0
  167. data/lib/prawn/document_builder/constructs.rb +2 -0
  168. data/lib/prawn/document_builder/constructs/flowing_text_construct.rb +18 -0
  169. data/lib/prawn/document_builder/constructs/path_construct.rb +9 -0
  170. data/lib/prawn/document_builder/layout.rb +25 -0
  171. data/lib/prawn/document_builder/modifications.rb +2 -0
  172. data/lib/prawn/document_builder/modifications/layout_modification.rb +9 -0
  173. data/lib/prawn/document_builder/modifications/path_modification.rb +9 -0
  174. data/lib/prawn/encoding.rb +121 -0
  175. data/lib/prawn/errors.rb +94 -0
  176. data/lib/prawn/font.rb +341 -0
  177. data/lib/prawn/font/afm.rb +225 -0
  178. data/lib/prawn/font/dfont.rb +42 -0
  179. data/lib/prawn/font/ttf.rb +350 -0
  180. data/lib/prawn/graphics.rb +325 -0
  181. data/lib/prawn/graphics/cap_style.rb +38 -0
  182. data/lib/prawn/graphics/color.rb +205 -0
  183. data/lib/prawn/graphics/dash.rb +71 -0
  184. data/lib/prawn/graphics/join_style.rb +38 -0
  185. data/lib/prawn/graphics/transformation.rb +156 -0
  186. data/lib/prawn/graphics/transparency.rb +99 -0
  187. data/lib/prawn/images.rb +348 -0
  188. data/lib/prawn/images/jpg.rb +46 -0
  189. data/lib/prawn/images/png.rb +226 -0
  190. data/lib/prawn/measurement_extensions.rb +46 -0
  191. data/lib/prawn/measurements.rb +71 -0
  192. data/lib/prawn/outline.rb +278 -0
  193. data/lib/prawn/repeater.rb +129 -0
  194. data/lib/prawn/security.rb +262 -0
  195. data/lib/prawn/security/arcfour.rb +51 -0
  196. data/lib/prawn/stamp.rb +126 -0
  197. data/lib/prawn/table.rb +421 -0
  198. data/lib/prawn/table/accessors.rb +180 -0
  199. data/lib/prawn/table/cell.rb +350 -0
  200. data/lib/prawn/table/cell/in_table.rb +27 -0
  201. data/lib/prawn/table/cell/subtable.rb +65 -0
  202. data/lib/prawn/table/cell/text.rb +125 -0
  203. data/lib/prawn/text.rb +449 -0
  204. data/lib/prawn/text/box.rb +392 -0
  205. data/lib/prawn/text/formatted.rb +4 -0
  206. data/lib/prawn/text/formatted/box.rb +228 -0
  207. data/lib/prawn/text/formatted/fragment.rb +181 -0
  208. data/lib/prawn/text/formatted/parser.rb +213 -0
  209. data/spec/annotations_spec.rb +90 -0
  210. data/spec/bounding_box_spec.rb +190 -0
  211. data/spec/cell_spec.rb +348 -0
  212. data/spec/destinations_spec.rb +15 -0
  213. data/spec/document_spec.rb +473 -0
  214. data/spec/font_spec.rb +324 -0
  215. data/spec/formatted_text_arranger_spec.rb +426 -0
  216. data/spec/formatted_text_box_spec.rb +756 -0
  217. data/spec/formatted_text_fragment_spec.rb +211 -0
  218. data/spec/graphics_spec.rb +446 -0
  219. data/spec/images_spec.rb +96 -0
  220. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  221. data/spec/jpg_spec.rb +25 -0
  222. data/spec/line_wrap_spec.rb +341 -0
  223. data/spec/measurement_units_spec.rb +23 -0
  224. data/spec/name_tree_spec.rb +112 -0
  225. data/spec/object_store_spec.rb +160 -0
  226. data/spec/outline_spec.rb +269 -0
  227. data/spec/pdf_object_spec.rb +170 -0
  228. data/spec/png_spec.rb +237 -0
  229. data/spec/reference_spec.rb +82 -0
  230. data/spec/repeater_spec.rb +96 -0
  231. data/spec/security_spec.rb +120 -0
  232. data/spec/snapshot_spec.rb +138 -0
  233. data/spec/spec_helper.rb +26 -0
  234. data/spec/stamp_spec.rb +108 -0
  235. data/spec/stroke_styles_spec.rb +163 -0
  236. data/spec/table_spec.rb +598 -0
  237. data/spec/template_spec.rb +158 -0
  238. data/spec/text_at_spec.rb +119 -0
  239. data/spec/text_box_spec.rb +742 -0
  240. data/spec/text_spacing_spec.rb +75 -0
  241. data/spec/text_spec.rb +333 -0
  242. data/spec/text_with_inline_formatting_spec.rb +193 -0
  243. data/spec/transparency_spec.rb +89 -0
  244. metadata +331 -0
@@ -0,0 +1,573 @@
1
+ # encoding: utf-8
2
+
3
+ # document.rb : Implements PDF document generation for Prawn
4
+ #
5
+ # Copyright April 2008, Gregory Brown. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require "stringio"
10
+ require "prawn/document/page_geometry"
11
+ require "prawn/document/bounding_box"
12
+ require "prawn/document/internals"
13
+ require "prawn/document/snapshot"
14
+ require "prawn/document/graphics_state"
15
+
16
+ module Prawn
17
+
18
+ # The Prawn::Document class is how you start creating a PDF document.
19
+ #
20
+ # There are three basic ways you can instantiate PDF Documents in Prawn, they
21
+ # are through assignment, implicit block or explicit block. Below is an exmple
22
+ # of each type, each example does exactly the same thing, makes a PDF document
23
+ # with all the defaults and puts in the default font "Hello There" and then
24
+ # saves it to the current directory as "example.pdf"
25
+ #
26
+ # For example, assignment can be like this:
27
+ #
28
+ # pdf = Prawn::Document.new
29
+ # pdf.text "Hello There"
30
+ # pdf.render_file "example.pdf"
31
+ #
32
+ # Or you can do an implied block form:
33
+ #
34
+ # Prawn::Document.generate "example.pdf" do
35
+ # text "Hello There"
36
+ # end
37
+ #
38
+ # Or if you need to access a variable outside the scope of the block, the
39
+ # explicit block form:
40
+ #
41
+ # words = "Hello There"
42
+ # Prawn::Document.generate "example.pdf" do |pdf|
43
+ # pdf.text words
44
+ # end
45
+ #
46
+ # Usually, the block forms are used when you are simply creating a PDF document
47
+ # that you want to immediately save or render out.
48
+ #
49
+ # See the new and generate methods for further details on the above.
50
+ #
51
+ class Document
52
+ include Prawn::Document::Internals
53
+ include Prawn::Core::Annotations
54
+ include Prawn::Core::Destinations
55
+ include Prawn::Document::Snapshot
56
+ include Prawn::Document::GraphicsState
57
+ include Prawn::Document::Security
58
+ include Prawn::Text
59
+ include Prawn::Graphics
60
+ include Prawn::Images
61
+ include Prawn::Stamp
62
+
63
+ # Any module added to this array will be included into instances of
64
+ # Prawn::Document at the per-object level. These will also be inherited by
65
+ # any subclasses.
66
+ #
67
+ # Example:
68
+ #
69
+ # module MyFancyModule
70
+ #
71
+ # def party!
72
+ # text "It's a big party!"
73
+ # end
74
+ #
75
+ # end
76
+ #
77
+ # Prawn::Document.extensions << MyFancyModule
78
+ #
79
+ # Prawn::Document.generate("foo.pdf") do
80
+ # party!
81
+ # end
82
+ #
83
+ def self.extensions
84
+ @extensions ||= []
85
+ end
86
+
87
+ def self.inherited(base) #:nodoc:
88
+ extensions.each { |e| base.extensions << e }
89
+ end
90
+
91
+ # Creates and renders a PDF document.
92
+ #
93
+ # When using the implicit block form, Prawn will evaluate the block
94
+ # within an instance of Prawn::Document, simplifying your syntax.
95
+ # However, please note that you will not be able to reference variables
96
+ # from the enclosing scope within this block.
97
+ #
98
+ # # Using implicit block form and rendering to a file
99
+ # Prawn::Document.generate "example.pdf" do
100
+ # # self here is set to the newly instantiated Prawn::Document
101
+ # # and so any variables in the outside scope are unavailable
102
+ # font "Times-Roman"
103
+ # draw_text "Hello World", :at => [200,720], :size => 32
104
+ # end
105
+ #
106
+ # If you need to access your local and instance variables, use the explicit
107
+ # block form shown below. In this case, Prawn yields an instance of
108
+ # PDF::Document and the block is an ordinary closure:
109
+ #
110
+ # # Using explicit block form and rendering to a file
111
+ # content = "Hello World"
112
+ # Prawn::Document.generate "example.pdf" do |pdf|
113
+ # # self here is left alone
114
+ # pdf.font "Times-Roman"
115
+ # pdf.draw_text content, :at => [200,720], :size => 32
116
+ # end
117
+ #
118
+ def self.generate(filename,options={},&block)
119
+ pdf = new(options,&block)
120
+ pdf.render_file(filename)
121
+ end
122
+
123
+ # Creates a new PDF Document. The following options are available (with
124
+ # the default values marked in [])
125
+ #
126
+ # <tt>:page_size</tt>:: One of the Document::PageGeometry sizes [LETTER]
127
+ # <tt>:page_layout</tt>:: Either <tt>:portrait</tt> or <tt>:landscape</tt>
128
+ # <tt>:margin</tt>:: Sets the margin on all sides in points [0.5 inch]
129
+ # <tt>:left_margin</tt>:: Sets the left margin in points [0.5 inch]
130
+ # <tt>:right_margin</tt>:: Sets the right margin in points [0.5 inch]
131
+ # <tt>:top_margin</tt>:: Sets the top margin in points [0.5 inch]
132
+ # <tt>:bottom_margin</tt>:: Sets the bottom margin in points [0.5 inch]
133
+ # <tt>:skip_page_creation</tt>:: Creates a document without starting the first page [false]
134
+ # <tt>:compress</tt>:: Compresses content streams before rendering them [false]
135
+ # <tt>:optimize_objects</tt>:: Reduce number of PDF objects in output, at expense of render time [false]
136
+ # <tt>:background</tt>:: An image path to be used as background on all pages [nil]
137
+ # <tt>:info</tt>:: Generic hash allowing for custom metadata properties [nil]
138
+ # <tt>:template</tt>:: The path to an existing PDF file to use as a template [nil]
139
+ #
140
+ # Setting e.g. the :margin to 100 points and the :left_margin to 50 will result in margins
141
+ # of 100 points on every side except for the left, where it will be 50.
142
+ #
143
+ # The :margin can also be an array much like CSS shorthand:
144
+ #
145
+ # # Top and bottom are 20, left and right are 100.
146
+ # :margin => [20, 100]
147
+ # # Top is 50, left and right are 100, bottom is 20.
148
+ # :margin => [50, 100, 20]
149
+ # # Top is 10, right is 20, bottom is 30, left is 40.
150
+ # :margin => [10, 20, 30, 40]
151
+ #
152
+ # Additionally, :page_size can be specified as a simple two value array giving
153
+ # the width and height of the document you need in PDF Points.
154
+ #
155
+ # Usage:
156
+ #
157
+ # # New document, US Letter paper, portrait orientation
158
+ # pdf = Prawn::Document.new
159
+ #
160
+ # # New document, A4 paper, landscaped
161
+ # pdf = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)
162
+ #
163
+ # # New document, Custom size
164
+ # pdf = Prawn::Document.new(:page_size => [200, 300])
165
+ #
166
+ # # New document, with background
167
+ # pdf = Prawn::Document.new(:background => "#{Prawn::BASEDIR}/data/images/pigs.jpg")
168
+ #
169
+ def initialize(options={},&block)
170
+ Prawn.verify_options [:page_size, :page_layout, :margin, :left_margin,
171
+ :right_margin, :top_margin, :bottom_margin, :skip_page_creation,
172
+ :compress, :skip_encoding, :background, :info,
173
+ :optimize_objects, :template], options
174
+
175
+ # need to fix, as the refactoring breaks this
176
+ # raise NotImplementedError if options[:skip_page_creation]
177
+
178
+ self.class.extensions.reverse_each { |e| extend e }
179
+ @internal_state = Prawn::Core::DocumentState.new(options)
180
+ @internal_state.populate_pages_from_store(self)
181
+ min_version(state.store.min_version) if state.store.min_version
182
+
183
+ @background = options[:background]
184
+ @font_size = 12
185
+
186
+ @bounding_box = nil
187
+ @margin_box = nil
188
+
189
+ @page_number = 0
190
+
191
+ options[:size] = options.delete(:page_size)
192
+ options[:layout] = options.delete(:page_layout)
193
+
194
+ if options[:template]
195
+ fresh_content_streams(options)
196
+ go_to_page(1)
197
+ else
198
+ if options[:skip_page_creation] || options[:template]
199
+ start_new_page(options.merge(:orphan => true))
200
+ else
201
+ start_new_page(options)
202
+ end
203
+ end
204
+
205
+ @bounding_box = @margin_box
206
+
207
+ if block
208
+ block.arity < 1 ? instance_eval(&block) : block[self]
209
+ end
210
+ end
211
+
212
+ attr_accessor :margin_box
213
+ attr_reader :margins, :y
214
+ attr_writer :font_size
215
+ attr_accessor :page_number
216
+
217
+ def state
218
+ @internal_state
219
+ end
220
+
221
+ def page
222
+ state.page
223
+ end
224
+
225
+ # Creates and advances to a new page in the document.
226
+ #
227
+ # Page size, margins, and layout can also be set when generating a
228
+ # new page. These values will become the new defaults for page creation
229
+ #
230
+ # pdf.start_new_page #=> Starts new page keeping current values
231
+ # pdf.start_new_page(:size => "LEGAL", :layout => :landscape)
232
+ # pdf.start_new_page(:left_margin => 50, :right_margin => 50)
233
+ # pdf.start_new_page(:margin => 100)
234
+ #
235
+ def start_new_page(options = {})
236
+ if last_page = state.page
237
+ last_page_size = last_page.size
238
+ last_page_layout = last_page.layout
239
+ last_page_margins = last_page.margins
240
+ end
241
+
242
+ state.page = Prawn::Core::Page.new(self,
243
+ :size => options[:size] || last_page_size,
244
+ :layout => options[:layout] || last_page_layout,
245
+ :margins => last_page_margins )
246
+
247
+ apply_margin_options(options)
248
+
249
+ use_graphic_settings
250
+
251
+ unless options[:orphan]
252
+ state.insert_page(state.page, @page_number)
253
+ @page_number += 1
254
+
255
+ save_graphics_state
256
+
257
+ canvas { image(@background, :at => bounds.top_left) } if @background
258
+ @y = @bounding_box.absolute_top
259
+
260
+ float do
261
+ state.on_page_create_action(self)
262
+ end
263
+ end
264
+ end
265
+
266
+ # Returns the number of pages in the document
267
+ #
268
+ # pdf = Prawn::Document.new
269
+ # pdf.page_count #=> 1
270
+ # 3.times { pdf.start_new_page }
271
+ # pdf.page_count #=> 4
272
+ #
273
+ def page_count
274
+ state.page_count
275
+ end
276
+
277
+ # Re-opens the page with the given (1-based) page number so that you can
278
+ # draw on it.
279
+ #
280
+ # See Prawn::Document#number_pages for a sample usage of this capability.
281
+ #
282
+ def go_to_page(k)
283
+ @page_number = k
284
+ state.page = state.pages[k-1]
285
+ generate_margin_box
286
+ @y = @bounding_box.absolute_top
287
+ end
288
+
289
+ def y=(new_y)
290
+ @y = new_y
291
+ bounds.update_height
292
+ end
293
+
294
+ # The current y drawing position relative to the innermost bounding box,
295
+ # or to the page margins at the top level.
296
+ #
297
+ def cursor
298
+ y - bounds.absolute_bottom
299
+ end
300
+
301
+ # Moves to the specified y position in relative terms to the bottom margin.
302
+ #
303
+ def move_cursor_to(new_y)
304
+ self.y = new_y + bounds.absolute_bottom
305
+ end
306
+
307
+ # Executes a block and then restores the original y position
308
+ #
309
+ # pdf.text "A"
310
+ #
311
+ # pdf.float do
312
+ # pdf.move_down 100
313
+ # pdf.text "C"
314
+ # end
315
+ #
316
+ # pdf.text "B"
317
+ #
318
+ def float
319
+ mask(:y) { yield }
320
+ end
321
+
322
+ # Renders the PDF document to string
323
+ #
324
+ def render
325
+ output = StringIO.new
326
+ finalize_all_page_contents
327
+
328
+ render_header(output)
329
+ render_body(output)
330
+ render_xref(output)
331
+ render_trailer(output)
332
+ str = output.string
333
+ str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
334
+ str
335
+ end
336
+
337
+ # Renders the PDF document to file.
338
+ #
339
+ # pdf.render_file "foo.pdf"
340
+ #
341
+ def render_file(filename)
342
+ Kernel.const_defined?("Encoding") ? mode = "wb:ASCII-8BIT" : mode = "wb"
343
+ File.open(filename,mode) { |f| f << render }
344
+ end
345
+
346
+ # The bounds method returns the current bounding box you are currently in,
347
+ # which is by default the box represented by the margin box on the
348
+ # document itself. When called from within a created <tt>bounding_box</tt>
349
+ # block, the box defined by that call will be returned instead of the
350
+ # document margin box.
351
+ #
352
+ # Another important point about bounding boxes is that all x and y measurements
353
+ # within a bounding box code block are relative to the bottom left corner of the
354
+ # bounding box.
355
+ #
356
+ # For example:
357
+ #
358
+ # Prawn::Document.new do
359
+ # # In the default "margin box" of a Prawn document of 0.5in along each edge
360
+ #
361
+ # # Draw a border around the page (the manual way)
362
+ # stroke do
363
+ # line(bounds.bottom_left, bounds.bottom_right)
364
+ # line(bounds.bottom_right, bounds.top_right)
365
+ # line(bounds.top_right, bounds.top_left)
366
+ # line(bounds.top_left, bounds.bottom_left)
367
+ # end
368
+ #
369
+ # # Draw a border around the page (the easy way)
370
+ # stroke_bounds
371
+ # end
372
+ #
373
+ def bounds
374
+ @bounding_box
375
+ end
376
+
377
+ # Sets Document#bounds to the BoundingBox provided. See above for a brief
378
+ # description of what a bounding box is. This function is useful if you
379
+ # really need to change the bounding box manually, but usually, just entering
380
+ # and exiting bounding box code blocks is good enough.
381
+ #
382
+ def bounds=(bounding_box)
383
+ @bounding_box = bounding_box
384
+ end
385
+
386
+ # Moves up the document by n points relative to the current position inside
387
+ # the current bounding box.
388
+ #
389
+ def move_up(n)
390
+ self.y += n
391
+ end
392
+
393
+ # Moves down the document by n points relative to the current position inside
394
+ # the current bounding box.
395
+ #
396
+ def move_down(n)
397
+ self.y -= n
398
+ end
399
+
400
+ # Moves down the document and then executes a block.
401
+ #
402
+ # pdf.text "some text"
403
+ # pdf.pad_top(100) do
404
+ # pdf.text "This is 100 points below the previous line of text"
405
+ # end
406
+ # pdf.text "This text appears right below the previous line of text"
407
+ #
408
+ def pad_top(y)
409
+ move_down(y)
410
+ yield
411
+ end
412
+
413
+ # Executes a block then moves down the document
414
+ #
415
+ # pdf.text "some text"
416
+ # pdf.pad_bottom(100) do
417
+ # pdf.text "This text appears right below the previous line of text"
418
+ # end
419
+ # pdf.text "This is 100 points below the previous line of text"
420
+ #
421
+ def pad_bottom(y)
422
+ yield
423
+ move_down(y)
424
+ end
425
+
426
+ # Moves down the document by y, executes a block, then moves down the
427
+ # document by y again.
428
+ #
429
+ # pdf.text "some text"
430
+ # pdf.pad(100) do
431
+ # pdf.text "This is 100 points below the previous line of text"
432
+ # end
433
+ # pdf.text "This is 100 points below the previous line of text"
434
+ #
435
+ def pad(y)
436
+ move_down(y)
437
+ yield
438
+ move_down(y)
439
+ end
440
+
441
+
442
+ # Indents the specified number of PDF points for the duration of the block
443
+ #
444
+ # pdf.text "some text"
445
+ # pdf.indent(20) do
446
+ # pdf.text "This is indented 20 points"
447
+ # end
448
+ # pdf.text "This starts 20 points left of the above line " +
449
+ # "and is flush with the first line"
450
+ #
451
+ def indent(x, &block)
452
+ bounds.indent(x, &block)
453
+ end
454
+
455
+
456
+ def mask(*fields) # :nodoc:
457
+ # Stores the current state of the named attributes, executes the block, and
458
+ # then restores the original values after the block has executed.
459
+ # -- I will remove the nodoc if/when this feature is a little less hacky
460
+ stored = {}
461
+ fields.each { |f| stored[f] = send(f) }
462
+ yield
463
+ fields.each { |f| send("#{f}=", stored[f]) }
464
+ end
465
+
466
+ # Attempts to group the given block vertically within the current context.
467
+ # First attempts to render it in the current position on the current page.
468
+ # If that attempt overflows, it is tried anew after starting a new context
469
+ # (page or column).
470
+ #
471
+ # Raises CannotGroup if the provided content is too large to fit alone in
472
+ # the current page or column.
473
+ #
474
+ def group(second_attempt=false)
475
+ old_bounding_box = @bounding_box
476
+ @bounding_box = SimpleDelegator.new(@bounding_box)
477
+
478
+ def @bounding_box.move_past_bottom
479
+ raise RollbackTransaction
480
+ end
481
+
482
+ success = transaction { yield }
483
+
484
+ unless success
485
+ raise Prawn::Errors::CannotGroup if second_attempt
486
+ old_bounding_box.move_past_bottom
487
+ group(second_attempt=true) { yield }
488
+ end
489
+
490
+ @bounding_box = old_bounding_box
491
+ end
492
+
493
+ # Specify a template for page numbering. This should be called
494
+ # towards the end of document creation, after all your content is already in
495
+ # place. In your template string, <page> refers to the current page, and
496
+ # <total> refers to the total amount of pages in the doucment.
497
+ #
498
+ # Example:
499
+ #
500
+ # Prawn::Document.generate("page_with_numbering.pdf") do
501
+ # text "Hai"
502
+ # start_new_page
503
+ # text "bai"
504
+ # start_new_page
505
+ # text "-- Hai again"
506
+ # number_pages "<page> in a total of <total>", [bounds.right - 50, 0]
507
+ # end
508
+ #
509
+ def number_pages(string, position)
510
+ page_count.times do |i|
511
+ go_to_page(i+1)
512
+ str = string.gsub("<page>","#{i+1}").gsub("<total>","#{page_count}")
513
+ draw_text str, :at => position
514
+ end
515
+ end
516
+
517
+ # Returns true if content streams will be compressed before rendering,
518
+ # false otherwise
519
+ #
520
+ def compression_enabled?
521
+ !!state.compress
522
+ end
523
+
524
+ private
525
+
526
+ def use_graphic_settings
527
+ update_colors
528
+ line_width(line_width) unless line_width == 1
529
+ cap_style(cap_style) unless cap_style == :butt
530
+ join_style(join_style) unless join_style == :miter
531
+ dash(dash[:dash], dash) if dashed?
532
+ end
533
+
534
+ def generate_margin_box
535
+ old_margin_box = @margin_box
536
+ page = state.page
537
+
538
+ @margin_box = BoundingBox.new(
539
+ self,
540
+ [ page.margins[:left], page.dimensions[-1] - page.margins[:top] ] ,
541
+ :width => page.dimensions[-2] - (page.margins[:left] + page.margins[:right]),
542
+ :height => page.dimensions[-1] - (page.margins[:top] + page.margins[:bottom])
543
+ )
544
+
545
+ # we must update bounding box if not flowing from the previous page
546
+ #
547
+ # FIXME: This may have a bug where the old margin is restored
548
+ # when the bounding box exits.
549
+ @bounding_box = @margin_box if old_margin_box == @bounding_box
550
+ end
551
+
552
+ def apply_margin_options(options)
553
+ if options[:margin]
554
+ # Treat :margin as CSS shorthand with 1-4 values.
555
+ margin = Array(options[:margin])
556
+ positions = { 4 => [0,1,2,3], 3 => [0,1,2,1],
557
+ 2 => [0,1,0,1], 1 => [0,0,0,0] }[margin.length]
558
+
559
+ [:top, :right, :bottom, :left].zip(positions).each do |p,i|
560
+ options[:"#{p}_margin"] ||= margin[i]
561
+ end
562
+ end
563
+
564
+ [:left,:right,:top,:bottom].each do |side|
565
+ if margin = options[:"#{side}_margin"]
566
+ state.page.margins[side] = margin
567
+ end
568
+ end
569
+
570
+ generate_margin_box
571
+ end
572
+ end
573
+ end