davebenvenuti-prawn 0.11.1.pre

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