piglop-prawn 0.10.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +49 -0
  3. data/LICENSE +56 -0
  4. data/README +140 -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/example_helper.rb +4 -0
  70. data/examples/general/background.rb +23 -0
  71. data/examples/general/canvas.rb +15 -0
  72. data/examples/general/context_sensitive_headers.rb +37 -0
  73. data/examples/general/float.rb +11 -0
  74. data/examples/general/margin.rb +36 -0
  75. data/examples/general/measurement_units.rb +51 -0
  76. data/examples/general/metadata-info.rb +16 -0
  77. data/examples/general/multi_page_layout.rb +18 -0
  78. data/examples/general/outlines.rb +50 -0
  79. data/examples/general/page_geometry.rb +31 -0
  80. data/examples/general/page_numbering.rb +15 -0
  81. data/examples/general/repeaters.rb +47 -0
  82. data/examples/general/stamp.rb +41 -0
  83. data/examples/general/templates.rb +13 -0
  84. data/examples/graphics/basic_images.rb +23 -0
  85. data/examples/graphics/cmyk.rb +12 -0
  86. data/examples/graphics/curves.rb +11 -0
  87. data/examples/graphics/hexagon.rb +13 -0
  88. data/examples/graphics/image_fit.rb +15 -0
  89. data/examples/graphics/image_flow.rb +37 -0
  90. data/examples/graphics/image_position.rb +17 -0
  91. data/examples/graphics/line.rb +32 -0
  92. data/examples/graphics/png_types.rb +22 -0
  93. data/examples/graphics/polygons.rb +16 -0
  94. data/examples/graphics/remote_images.rb +12 -0
  95. data/examples/graphics/rounded_polygons.rb +19 -0
  96. data/examples/graphics/rounded_rectangle.rb +20 -0
  97. data/examples/graphics/ruport_style_helpers.rb +19 -0
  98. data/examples/graphics/stroke_bounds.rb +20 -0
  99. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  100. data/examples/graphics/stroke_dash.rb +42 -0
  101. data/examples/graphics/transformations.rb +52 -0
  102. data/examples/graphics/transparency.rb +26 -0
  103. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  104. data/examples/m17n/euro.rb +15 -0
  105. data/examples/m17n/sjis.rb +28 -0
  106. data/examples/m17n/utf8.rb +13 -0
  107. data/examples/m17n/win_ansi_charset.rb +54 -0
  108. data/examples/security/hello_foo.rb +8 -0
  109. data/examples/table/bill.rb +53 -0
  110. data/examples/table/cell.rb +12 -0
  111. data/examples/table/checkerboard.rb +22 -0
  112. data/examples/table/header.rb +14 -0
  113. data/examples/table/inline_format_table.rb +12 -0
  114. data/examples/table/multi_page_table.rb +9 -0
  115. data/examples/table/simple_table.rb +24 -0
  116. data/examples/table/subtable.rb +12 -0
  117. data/examples/table/widths.rb +20 -0
  118. data/examples/text/alignment.rb +18 -0
  119. data/examples/text/dfont.rb +48 -0
  120. data/examples/text/family_based_styling.rb +24 -0
  121. data/examples/text/font_calculations.rb +91 -0
  122. data/examples/text/font_size.rb +33 -0
  123. data/examples/text/hyphenation.rb +45 -0
  124. data/examples/text/indent_paragraphs.rb +18 -0
  125. data/examples/text/inline_format.rb +72 -0
  126. data/examples/text/kerning.rb +30 -0
  127. data/examples/text/rotated.rb +98 -0
  128. data/examples/text/shaped_text_box.rb +31 -0
  129. data/examples/text/simple_text.rb +17 -0
  130. data/examples/text/simple_text_ttf.rb +17 -0
  131. data/examples/text/text_box.rb +88 -0
  132. data/examples/text/text_box_returning_excess.rb +51 -0
  133. data/examples/text/text_flow.rb +67 -0
  134. data/lib/prawn/compatibility.rb +51 -0
  135. data/lib/prawn/core/annotations.rb +61 -0
  136. data/lib/prawn/core/byte_string.rb +9 -0
  137. data/lib/prawn/core/destinations.rb +90 -0
  138. data/lib/prawn/core/document_state.rb +84 -0
  139. data/lib/prawn/core/literal_string.rb +16 -0
  140. data/lib/prawn/core/name_tree.rb +165 -0
  141. data/lib/prawn/core/object_store.rb +219 -0
  142. data/lib/prawn/core/page.rb +179 -0
  143. data/lib/prawn/core/pdf_object.rb +83 -0
  144. data/lib/prawn/core/reference.rb +112 -0
  145. data/lib/prawn/core/text/formatted/arranger.rb +260 -0
  146. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  147. data/lib/prawn/core/text/formatted/wrap.rb +113 -0
  148. data/lib/prawn/core/text/line_wrap.rb +198 -0
  149. data/lib/prawn/core/text/wrap.rb +80 -0
  150. data/lib/prawn/core/text.rb +115 -0
  151. data/lib/prawn/core.rb +89 -0
  152. data/lib/prawn/document/bounding_box.rb +425 -0
  153. data/lib/prawn/document/graphics_state.rb +48 -0
  154. data/lib/prawn/document/internals.rb +176 -0
  155. data/lib/prawn/document/page_geometry.rb +136 -0
  156. data/lib/prawn/document/snapshot.rb +87 -0
  157. data/lib/prawn/document.rb +573 -0
  158. data/lib/prawn/encoding.rb +121 -0
  159. data/lib/prawn/errors.rb +94 -0
  160. data/lib/prawn/font/afm.rb +225 -0
  161. data/lib/prawn/font/dfont.rb +42 -0
  162. data/lib/prawn/font/ttf.rb +350 -0
  163. data/lib/prawn/font.rb +342 -0
  164. data/lib/prawn/graphics/cap_style.rb +38 -0
  165. data/lib/prawn/graphics/color.rb +205 -0
  166. data/lib/prawn/graphics/dash.rb +71 -0
  167. data/lib/prawn/graphics/join_style.rb +38 -0
  168. data/lib/prawn/graphics/transformation.rb +156 -0
  169. data/lib/prawn/graphics/transparency.rb +99 -0
  170. data/lib/prawn/graphics.rb +321 -0
  171. data/lib/prawn/images/jpg.rb +46 -0
  172. data/lib/prawn/images/png.rb +226 -0
  173. data/lib/prawn/images.rb +348 -0
  174. data/lib/prawn/measurement_extensions.rb +46 -0
  175. data/lib/prawn/measurements.rb +71 -0
  176. data/lib/prawn/outline.rb +278 -0
  177. data/lib/prawn/repeater.rb +129 -0
  178. data/lib/prawn/security/arcfour.rb +51 -0
  179. data/lib/prawn/security.rb +262 -0
  180. data/lib/prawn/stamp.rb +126 -0
  181. data/lib/prawn/table/accessors.rb +180 -0
  182. data/lib/prawn/table/cell/in_table.rb +27 -0
  183. data/lib/prawn/table/cell/subtable.rb +65 -0
  184. data/lib/prawn/table/cell/text.rb +128 -0
  185. data/lib/prawn/table/cell.rb +330 -0
  186. data/lib/prawn/table.rb +421 -0
  187. data/lib/prawn/text/box.rb +380 -0
  188. data/lib/prawn/text/formatted/box.rb +215 -0
  189. data/lib/prawn/text/formatted/fragment.rb +127 -0
  190. data/lib/prawn/text/formatted/parser.rb +202 -0
  191. data/lib/prawn/text/formatted.rb +4 -0
  192. data/lib/prawn/text.rb +442 -0
  193. data/lib/prawn.rb +25 -0
  194. data/spec/annotations_spec.rb +90 -0
  195. data/spec/bounding_box_spec.rb +190 -0
  196. data/spec/cell_spec.rb +337 -0
  197. data/spec/destinations_spec.rb +15 -0
  198. data/spec/document_spec.rb +461 -0
  199. data/spec/font_spec.rb +314 -0
  200. data/spec/formatted_text_arranger_spec.rb +400 -0
  201. data/spec/formatted_text_box_spec.rb +659 -0
  202. data/spec/formatted_text_fragment_spec.rb +211 -0
  203. data/spec/graphics_spec.rb +446 -0
  204. data/spec/images_spec.rb +96 -0
  205. data/spec/inline_formatted_text_parser_spec.rb +446 -0
  206. data/spec/jpg_spec.rb +25 -0
  207. data/spec/line_wrap_spec.rb +341 -0
  208. data/spec/measurement_units_spec.rb +23 -0
  209. data/spec/name_tree_spec.rb +112 -0
  210. data/spec/object_store_spec.rb +160 -0
  211. data/spec/outline_spec.rb +229 -0
  212. data/spec/pdf_object_spec.rb +127 -0
  213. data/spec/png_spec.rb +237 -0
  214. data/spec/reference_spec.rb +82 -0
  215. data/spec/repeater_spec.rb +96 -0
  216. data/spec/security_spec.rb +120 -0
  217. data/spec/snapshot_spec.rb +138 -0
  218. data/spec/spec_helper.rb +26 -0
  219. data/spec/stamp_spec.rb +108 -0
  220. data/spec/stroke_styles_spec.rb +163 -0
  221. data/spec/table_spec.rb +583 -0
  222. data/spec/template_spec.rb +158 -0
  223. data/spec/text_at_spec.rb +119 -0
  224. data/spec/text_box_spec.rb +675 -0
  225. data/spec/text_spacing_spec.rb +57 -0
  226. data/spec/text_spec.rb +328 -0
  227. data/spec/text_with_inline_formatting_spec.rb +193 -0
  228. data/spec/transparency_spec.rb +75 -0
  229. data/vendor/pdf-inspector/README +18 -0
  230. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  231. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  232. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  233. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  234. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  235. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  236. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  237. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  238. data/vendor/ttfunk/example.rb +45 -0
  239. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  240. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  241. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  242. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  243. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  244. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  245. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  246. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  247. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  248. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  249. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  250. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  251. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  252. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  253. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  254. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  255. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  256. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  257. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  258. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  260. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  261. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  262. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  263. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  264. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  265. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  266. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  274. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  275. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  276. metadata +364 -0
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates transformations
4
+ #
5
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
+
7
+ Prawn::Document.generate "rotated_text.pdf" do |pdf|
8
+ pdf.line_width = 1
9
+ width = 150
10
+ height = 200
11
+ half_width = width / 2
12
+ half_height = height / 2
13
+ angle = 30
14
+
15
+
16
+ # AROUND THE CENTER
17
+
18
+ x = pdf.bounds.width / 2 - half_width
19
+ y = pdf.bounds.height / 2 + half_height
20
+
21
+ pdf.stroke_rectangle([x, y], width, height)
22
+ pdf.rotate(angle, :origin => [x + half_width, y - half_height]) do
23
+ pdf.stroke_rectangle([x, y], width, height)
24
+ end
25
+ pdf.text_box("rotated around the center " * 10,
26
+ :at => [x, y],
27
+ :width => width,
28
+ :height => height,
29
+ :rotate => angle,
30
+ :rotate_around => :center)
31
+
32
+
33
+ # AROUND THE UPPER_LEFT_CORNER
34
+
35
+ x = pdf.bounds.width - width
36
+ y = height
37
+
38
+ pdf.stroke_rectangle([x, y], width, height)
39
+ pdf.rotate(angle, :origin => [x, y]) do
40
+ pdf.stroke_rectangle([x, y], width, height)
41
+ end
42
+ pdf.text_box("rotated around upper left corner " * 10,
43
+ :at => [x, y],
44
+ :width => width,
45
+ :height => height,
46
+ :rotate => angle)
47
+
48
+
49
+ # AROUND THE UPPER_RIGHT_CORNER
50
+
51
+ x = 0
52
+ y = height
53
+
54
+ pdf.stroke_rectangle([x, y], width, height)
55
+ pdf.rotate(angle, :origin => [x + width, y]) do
56
+ pdf.stroke_rectangle([x, y], width, height)
57
+ end
58
+ pdf.text_box("rotated around upper right corner " * 10,
59
+ :at => [x, y],
60
+ :width => width,
61
+ :height => height,
62
+ :rotate => angle,
63
+ :rotate_around => :upper_right)
64
+
65
+
66
+ # AROUND THE LOWER_RIGHT_CORNER
67
+
68
+ x = 0
69
+ y = pdf.bounds.height
70
+
71
+ pdf.stroke_rectangle([x, y], width, height)
72
+ pdf.rotate(angle, :origin => [x + width, y - height]) do
73
+ pdf.stroke_rectangle([x, y], width, height)
74
+ end
75
+ pdf.text_box("rotated around lower right corner " * 10,
76
+ :at => [x, y],
77
+ :width => width,
78
+ :height => height,
79
+ :rotate => angle,
80
+ :rotate_around => :lower_right)
81
+
82
+
83
+ # AROUND THE LOWER_LEFT_CORNER
84
+
85
+ x = pdf.bounds.width - width
86
+ y = pdf.bounds.height
87
+
88
+ pdf.stroke_rectangle([x, y], width, height)
89
+ pdf.rotate(angle, :origin => [x, y - height]) do
90
+ pdf.stroke_rectangle([x, y], width, height)
91
+ end
92
+ pdf.text_box("rotated around lower left corner " * 10,
93
+ :at => [x, y],
94
+ :width => width,
95
+ :height => height,
96
+ :rotate => angle,
97
+ :rotate_around => :lower_left)
98
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Demonstrates extending Text::Box
4
+ #
5
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
+
7
+ Prawn::Document.generate "shaped_text_box.pdf" do |pdf|
8
+ module ShapedBox
9
+ def available_width
10
+ height + 25
11
+ end
12
+ end
13
+
14
+ Prawn::Text::Box.extensions << ShapedBox
15
+ pdf.stroke_rectangle([10, pdf.bounds.top - 10], 300, 300)
16
+ pdf.text_box("A" * 500,
17
+ :at => [10, pdf.bounds.top - 10],
18
+ :width => 300,
19
+ :height => 300,
20
+ :align => :center)
21
+
22
+ Prawn::Text::Formatted::Box.extensions << ShapedBox
23
+ pdf.stroke_rectangle([10, pdf.bounds.top - 330], 300, 300)
24
+ pdf.formatted_text_box([:text => "A" * 500,
25
+ :color => "009900"],
26
+ :at => [10, pdf.bounds.top - 330],
27
+ :width => 300,
28
+ :height => 300,
29
+ :align => :center)
30
+ end
31
+
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ #
3
+ # An early example of basic text generation at absolute positions.
4
+ # Mostly kept for nostalgia.
5
+ #
6
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
+
8
+ Prawn::Document.generate "simple_text.pdf" do
9
+ fill_color "0000ff"
10
+ draw_text "Hello World", :at => [200,420], :size => 32, :rotate => 45
11
+ font "Times-Roman"
12
+ fill_color "ff0000"
13
+ draw_text "Using Another Font", :at => [5,5]
14
+ start_new_page
15
+ font "Courier"
16
+ draw_text "Goodbye World", :at => [288,50]
17
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ #
3
+ # An early example of TTF font embedding. Mostly kept for nostalgia's sake.
4
+ #
5
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
6
+
7
+ Prawn::Document.generate "simple_text_ttf.pdf" do
8
+ fill_color "0000ff"
9
+ font "#{Prawn::BASEDIR}/data/fonts/comicsans.ttf"
10
+ draw_text "Hello World", :at => [200,720], :size => 32
11
+
12
+ font "#{Prawn::BASEDIR}/data/fonts/Chalkboard.ttf"
13
+
14
+ pad(20) do
15
+ text "This is chalkboard wrapping " * 20
16
+ end
17
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ #
3
+
4
+ require 'examples/example_helper'
5
+
6
+ Prawn::Document.generate("text_box.pdf") do
7
+ def get_string(i, j)
8
+ case i
9
+ when 0
10
+ text = "this is left text " * 30
11
+ text.insert(48, "\n\n")
12
+ when 1
13
+ text = "this is center text " * 30
14
+ text.insert(54, "\n\n")
15
+ when 2
16
+ text = "this is right text " * 30
17
+ text.insert(51, "\n\n")
18
+ end
19
+
20
+ case j
21
+ when 0
22
+ text.split(" ").slice(0..47).join(" ")
23
+ when 3
24
+ text.delete(" ")
25
+ else
26
+ text
27
+ end
28
+ end
29
+
30
+ def get_options(i, j)
31
+ options = {
32
+ :width => bounds.width * 0.3,
33
+ :height => bounds.width * 0.3,
34
+ :overflow => :ellipses,
35
+ :at => [0, 0],
36
+ :align => :left,
37
+ :document => self
38
+ }
39
+
40
+ case i
41
+ when 0
42
+ options[:valign] = :top if j == 0
43
+ when 1
44
+ options[:align] = :center
45
+ options[:valign] = :center if j == 0
46
+ when 2
47
+ options[:align] = :right
48
+ options[:valign] = :bottom if j == 0
49
+ end
50
+
51
+ case j
52
+ when 1
53
+ options[:overflow] = :shrink_to_fit
54
+ when 2
55
+ options[:leading] = font.height * 0.5
56
+ options[:overflow] = :truncate
57
+ end
58
+ options
59
+ end
60
+
61
+ stroke_color("555555")
62
+ 3.times do |i|
63
+ 4.times do |j|
64
+ options = get_options(i, j)
65
+ options[:at][0] = (bounds.width - options[:width]) * 0.5 * i
66
+ options[:at][1] = bounds.top - (bounds.height - options[:height]) * 0.33 * j
67
+ box = Prawn::Text::Box.new(get_string(i, j), options)
68
+
69
+ fill_color("ffeeee")
70
+ if i == 1
71
+ # bound with a box of a particular size, regardless of how
72
+ # much text it contains
73
+ fill_and_stroke_rectangle(options[:at],
74
+ options[:width],
75
+ options[:height])
76
+ else
77
+ # bound with a box that exactly fits the printed text using
78
+ # dry_run look-ahead
79
+ box.render(:dry_run => true)
80
+ fill_and_stroke_rectangle(options[:at],
81
+ options[:width],
82
+ box.height)
83
+ end
84
+ fill_color("000000")
85
+ box.render
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ #
3
+ # A text box is positioned by a top-left corner, width, and height and is
4
+ # essentially an invisible rectangle that the text will flow within. If the
5
+ # text exceeds the boundaries, it is either truncated, replaced with some
6
+ # ellipses, or set to expand beyond the bottom boundary.
7
+ #
8
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
9
+
10
+ Prawn::Document.generate("text_box_returning_excess.pdf") do
11
+
12
+ # Note that without the initial space in p_break, newlines may be eaten by
13
+ # the wrap/unwrap process that happens inside the text box.
14
+ p_break = " \n\n"
15
+ callout = "Lorem ipsum dolor sit amet"
16
+ lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.#{p_break}Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.#{p_break}Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
17
+
18
+ box_height = font.height * 5
19
+
20
+ # Add a callout box that the rest of the text should flow around
21
+ font_size(18) do
22
+ text_box callout, {
23
+ :width => 100,
24
+ :height => font.height * 3,
25
+ :overflow => :truncate,
26
+ :at => [100, bounds.top - box_height - 4]
27
+ }
28
+ end
29
+
30
+ excess_text = text_box lorem + p_break + lorem, {
31
+ :width => 300,
32
+ :height => box_height,
33
+ :overflow => :truncate,
34
+ :at => [100, bounds.top],
35
+ }
36
+
37
+ excess_text = text_box excess_text, {
38
+ :width => 200,
39
+ :height => box_height,
40
+ :overflow => :truncate,
41
+ :at => [200, bounds.top - box_height],
42
+ }
43
+
44
+ text_box excess_text, {
45
+ :width => 300,
46
+ :height => box_height,
47
+ :overflow => :expand,
48
+ :at => [100, bounds.top - box_height * 2],
49
+ }
50
+
51
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Basic text flowing example including the use of bounding boxes. A somewhat
4
+ # old example, mostly retained for nostalgia.
5
+ #
6
+ require "#{File.dirname(__FILE__)}/../example_helper.rb"
7
+
8
+ content = <<-EOS
9
+ How does
10
+ Prawn deal with
11
+ white
12
+ space
13
+
14
+ and
15
+
16
+ line
17
+ breaks?
18
+ EOS
19
+
20
+ poem = <<-EOS
21
+ GOOD-BYE
22
+
23
+ Good-bye, proud world! I'm going home: Thou art not my friend, and I'm not thine. Long through thy weary crowds I roam; A river-ark on the ocean brine, Long I've been tossed like the driven foam: But now, proud world! I'm going home.
24
+
25
+ Good-bye to Flattery's fawning face; To Grandeur with his wise grimace; To upstart Wealth's averted eye; To supple Office, low and high; To crowded halls, to court and street; To frozen hearts and hasting feet; To those who go, and those who come; Good-bye, proud world! I'm going home.
26
+
27
+ I am going to my own hearth-stone, Bosomed in yon green hills alone,-- secret nook in a pleasant land, Whose groves the frolic fairies planned; Where arches green, the livelong day, Echo the blackbird's roundelay, And vulgar feet have never trod A spot that is sacred to thought and God.
28
+
29
+ O, when I am safe in my sylvan home, I tread on the pride of Greece and Rome; And when I am stretched beneath the pines, Where the evening star so holy shines, I laugh at the lore and the pride of man, At the sophist schools and the learned clan; For what are they all, in their high conceit, When man in the bush with God may meet?
30
+ EOS
31
+
32
+ overflow = "This text should flow gracefully onto the next page, like a stream"+
33
+ " flows elegantly from a mountain lake down into the village below."
34
+
35
+ Prawn::Document.generate("flow.pdf") do |pdf|
36
+
37
+ pdf.font "Times-Roman"
38
+ pdf.stroke_line [pdf.bounds.left, pdf.bounds.top],
39
+ [pdf.bounds.right, pdf.bounds.top]
40
+
41
+ pdf.text content, :size => 10
42
+
43
+ pdf.bounding_box([100,600], :width => 200, :height => 525) do
44
+ pdf.stroke_line [pdf.bounds.left, pdf.bounds.top],
45
+ [pdf.bounds.right, pdf.bounds.top]
46
+ pdf.text poem, :size => 12
47
+ end
48
+
49
+ pdf.bounding_box([325,600], :width => 200, :height => 525) do
50
+ pdf.stroke_line [pdf.bounds.left, pdf.bounds.top],
51
+ [pdf.bounds.right, pdf.bounds.top]
52
+ pdf.text poem.reverse, :size => 12
53
+ end
54
+
55
+ pdf.text overflow * 10, :size => 14
56
+
57
+ pdf.text "Hooray! We've conquered the evil PDF gods", :size => 36
58
+
59
+ pdf.bounding_box([100,450], :width => 300) do
60
+ pdf.stroke_line [pdf.bounds.left, pdf.bounds.top],
61
+ [pdf.bounds.right, pdf.bounds.top]
62
+ pdf.text poem, :size => 10, :leading => 5
63
+ end
64
+
65
+ pdf.text "And this text automatically goes below the poem", :size => 18
66
+
67
+ end
@@ -0,0 +1,51 @@
1
+ # coding: utf-8
2
+ #
3
+ # Why would we ever use Ruby 1.8.7 when we can backport with something
4
+ # as simple as this?
5
+ #
6
+ class String #:nodoc:
7
+ def first_line
8
+ self.each_line { |line| return line }
9
+ end
10
+ unless "".respond_to?(:lines)
11
+ alias_method :lines, :to_a
12
+ end
13
+ unless "".respond_to?(:each_char)
14
+ def each_char #:nodoc:
15
+ # copied from jcode
16
+ if block_given?
17
+ scan(/./m) { |x| yield x }
18
+ else
19
+ scan(/./m)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ unless File.respond_to?(:binread)
26
+ def File.binread(file) #:nodoc:
27
+ File.open(file,"rb") { |f| f.read }
28
+ end
29
+ end
30
+
31
+ if RUBY_VERSION < "1.9"
32
+
33
+ def ruby_18 #:nodoc:
34
+ yield
35
+ end
36
+
37
+ def ruby_19 #:nodoc:
38
+ false
39
+ end
40
+
41
+ else
42
+
43
+ def ruby_18 #:nodoc:
44
+ false
45
+ end
46
+
47
+ def ruby_19 #:nodoc:
48
+ yield
49
+ end
50
+
51
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+
3
+ # annotations.rb : Implements low-level annotation support for PDF
4
+ #
5
+ # Copyright November 2008, Jamis Buck. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ #
9
+ module Prawn
10
+ module Core
11
+
12
+ # Provides very low-level support for annotations.
13
+ #
14
+ module Annotations #:nodoc:
15
+
16
+ # Adds a new annotation (section 8.4 in PDF spec) to the current page.
17
+ # +options+ must be a Hash describing the annotation.
18
+ #
19
+ def annotate(options)
20
+ state.page.dictionary.data[:Annots] ||= []
21
+ options = sanitize_annotation_hash(options)
22
+ state.page.dictionary.data[:Annots] << ref!(options)
23
+ return options
24
+ end
25
+
26
+ # A convenience method for creating Text annotations. +rect+ must be an array
27
+ # of four numbers, describing the bounds of the annotation. +contents+ should
28
+ # be a string, to be shown when the annotation is activated.
29
+ #
30
+ def text_annotation(rect, contents, options={})
31
+ options = options.merge(:Subtype => :Text, :Rect => rect, :Contents => contents)
32
+ annotate(options)
33
+ end
34
+
35
+ # A convenience method for creating Link annotations. +rect+ must be an array
36
+ # of four numbers, describing the bounds of the annotation. The +options+ hash
37
+ # should include either :Dest (describing the target destination, usually as a
38
+ # string that has been recorded in the document's Dests tree), or :A (describing
39
+ # an action to perform on clicking the link), or :PA (for describing a URL to
40
+ # link to).
41
+ #
42
+ def link_annotation(rect, options={})
43
+ options = options.merge(:Subtype => :Link, :Rect => rect)
44
+ annotate(options)
45
+ end
46
+
47
+ private
48
+
49
+ def sanitize_annotation_hash(options)
50
+ options = options.merge(:Type => :Annot)
51
+
52
+ if options[:Dest].is_a?(String)
53
+ options[:Dest] = Prawn::Core::LiteralString.new(options[:Dest])
54
+ end
55
+
56
+ options
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ module Prawn
3
+ module Core
4
+ # This is used to differentiate strings that must be encoded as
5
+ # a byte string, such as binary data from encrypted strings.
6
+ class ByteString < String #:nodoc:
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ # prawn/core/destinations.rb : Implements destination support for PDF
4
+ #
5
+ # Copyright November 2008, Jamis Buck. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ module Prawn
10
+ module Core
11
+ module Destinations #:nodoc:
12
+
13
+ # The maximum number of children to fit into a single node in the Dests tree.
14
+ NAME_TREE_CHILDREN_LIMIT = 20 #:nodoc:
15
+
16
+ # The Dests name tree in the Name dictionary (see Prawn::Document::Internal#names).
17
+ # This name tree is used to store named destinations (PDF spec 8.2.1).
18
+ # (For more on name trees, see section 3.8.4 in the PDF spec.)
19
+ #
20
+ def dests
21
+ names.data[:Dests] ||= ref!(Prawn::Core::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
22
+ end
23
+
24
+ # Adds a new destination to the dests name tree (see #dests). The
25
+ # +reference+ parameter will be converted into a Prawn::Reference if
26
+ # it is not already one.
27
+ #
28
+ def add_dest(name, reference)
29
+ reference = ref!(reference) unless reference.is_a?(Prawn::Core::Reference)
30
+ dests.data.add(name, reference)
31
+ end
32
+
33
+ # Return a Dest specification for a specific location (and optional zoom
34
+ # level).
35
+ #
36
+ def dest_xyz(left, top, zoom=nil, page=current_page)
37
+ [page, :XYZ, left, top, zoom]
38
+ end
39
+
40
+ # Return a Dest specification that will fit the given page into the
41
+ # viewport.
42
+ #
43
+ def dest_fit(page=current_page)
44
+ [page, :Fit]
45
+ end
46
+
47
+ # Return a Dest specification that will fit the given page horizontally
48
+ # into the viewport, aligned vertically at the given top coordinate.
49
+ #
50
+ def dest_fit_horizontally(top, page=current_page)
51
+ [page, :FitH, top]
52
+ end
53
+
54
+ # Return a Dest specification that will fit the given page vertically
55
+ # into the viewport, aligned horizontally at the given left coordinate.
56
+ #
57
+ def dest_fit_vertically(left, page=current_page)
58
+ [page, :FitV, left]
59
+ end
60
+
61
+ # Return a Dest specification that will fit the given rectangle into the
62
+ # viewport, for the given page.
63
+ #
64
+ def dest_fit_rect(left, bottom, right, top, page=current_page)
65
+ [page, :FitR, left, bottom, right, top]
66
+ end
67
+
68
+ # Return a Dest specfication that will fit the given page's bounding box
69
+ # into the viewport.
70
+ #
71
+ def dest_fit_bounds(page=current_page)
72
+ [page, :FitB]
73
+ end
74
+
75
+ # Same as #dest_fit_horizontally, but works on the page's bounding box
76
+ # instead of the entire page.
77
+ #
78
+ def dest_fit_bounds_horizontally(top, page=current_page)
79
+ [page, :FitBH, top]
80
+ end
81
+
82
+ # Same as #dest_fit_vertically, but works on the page's bounding box
83
+ # instead of the entire page.
84
+ #
85
+ def dest_fit_bounds_vertically(left, page=current_page)
86
+ [page, :FitBV, left]
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,84 @@
1
+ module Prawn
2
+ module Core
3
+ class DocumentState #:nodoc:
4
+ def initialize(options)
5
+ normalize_metadata(options)
6
+
7
+ if options[:template]
8
+ @store = Prawn::Core::ObjectStore.new(:template => options[:template])
9
+ else
10
+ @store = Prawn::Core::ObjectStore.new(:info => options[:info])
11
+ end
12
+
13
+ @version = 1.3
14
+ @pages = []
15
+ @page = nil
16
+ @trailer = {}
17
+ @compress = options.fetch(:compress, false)
18
+ @encrypt = options.fetch(:encrypt, false)
19
+ @encryption_key = options[:encryption_key]
20
+ @optimize_objects = options.fetch(:optimize_objects, false)
21
+ @skip_encoding = options.fetch(:skip_encoding, false)
22
+ @before_render_callbacks = []
23
+ @on_page_create_callback = nil
24
+ end
25
+
26
+ attr_accessor :store, :version, :pages, :page, :trailer, :compress,
27
+ :encrypt, :encryption_key, :optimize_objects, :skip_encoding,
28
+ :before_render_callbacks, :on_page_create_callback
29
+
30
+ def populate_pages_from_store(document)
31
+ return 0 if @store.page_count <= 0 || @pages.size > 0
32
+
33
+ count = (1..@store.page_count)
34
+ @pages = count.map do |index|
35
+ orig_dict_id = @store.object_id_for_page(index)
36
+ Prawn::Core::Page.new(document, :object_id => orig_dict_id)
37
+ end
38
+
39
+ end
40
+
41
+ def normalize_metadata(options)
42
+ options[:info] ||= {}
43
+ options[:info][:Creator] ||= "Prawn"
44
+ options[:info][:Producer] = "Prawn"
45
+
46
+ info = options[:info]
47
+
48
+ info.keys.each do |key|
49
+ if info[key].kind_of?(String)
50
+ info[key] = Prawn::Core::LiteralString.new(info[key])
51
+ end
52
+ end
53
+ end
54
+
55
+ def insert_page(page, page_number)
56
+ pages.insert(page_number, page)
57
+ store.pages.data[:Kids].insert(page_number, page.dictionary)
58
+ store.pages.data[:Count] += 1
59
+ end
60
+
61
+ def on_page_create_action(doc)
62
+ on_page_create_callback[doc] if on_page_create_callback
63
+ end
64
+
65
+ def before_render_actions(doc)
66
+ before_render_callbacks.each{ |c| c.call(self) }
67
+ end
68
+
69
+ def page_count
70
+ pages.length
71
+ end
72
+
73
+ def render_body(output)
74
+ store.compact if optimize_objects
75
+ store.each do |ref|
76
+ ref.offset = output.size
77
+ output << (@encrypt ? ref.encrypted_object(@encryption_key) :
78
+ ref.object)
79
+ end
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module Prawn
3
+ module Core
4
+ # This is used to differentiate strings that must be encoded as
5
+ # a *literal* string, versus those that can be encoded in
6
+ # the PDF hexadecimal format.
7
+ #
8
+ # Some features of the PDF format appear to require that literal
9
+ # strings be used. One such feature is the /Dest key of a link
10
+ # annotation; if a hex encoded string is used there, the links
11
+ # do not work (as tested in Mac OS X Preview, and Adobe Acrobat
12
+ # Reader).
13
+ class LiteralString < String #:nodoc:
14
+ end
15
+ end
16
+ end