nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,170 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "Prawn::ObjectStore" do
6
+ before(:each) do
7
+ @store = Prawn::Core::ObjectStore.new
8
+ end
9
+
10
+ it "should create required roots by default, including info passed to new" do
11
+ store = Prawn::Core::ObjectStore.new(:info => {:Test => 3})
12
+ store.size.should == 3 # 3 default roots
13
+ store.info.data[:Test].should == 3
14
+ store.pages.data[:Count].should == 0
15
+ store.root.data[:Pages].should == store.pages
16
+ end
17
+
18
+ it "should import objects from an existing PDF" do
19
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
20
+ store = Prawn::Core::ObjectStore.new(:template => filename)
21
+ store.size.should == 5
22
+ end
23
+
24
+ it "should point to existing roots when importing objects from an existing PDF" do
25
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
26
+ store = Prawn::Core::ObjectStore.new(:template => filename)
27
+ store.info.class.should == Prawn::Core::Reference
28
+ store.root.class.should == Prawn::Core::Reference
29
+ end
30
+
31
+ it "should initialize with pages when importing objects from an existing PDF" do
32
+ filename = "#{Prawn::BASEDIR}/spec/data/curves.pdf"
33
+ store = Prawn::Core::ObjectStore.new(:template => filename)
34
+ store.pages.data[:Count].should == 1
35
+ end
36
+
37
+ it "should import all objects from a PDF that has an indirect reference in a stream dict" do
38
+ filename = "#{Prawn::DATADIR}/pdfs/indirect_reference.pdf"
39
+ store = Prawn::Core::ObjectStore.new(:template => filename)
40
+ store.size.should == 8
41
+ end
42
+
43
+ it "should raise_error ArgumentError when given a file that doesn exist as a template" do
44
+ filename = "not_really_there.pdf"
45
+
46
+ lambda { Prawn::Core::ObjectStore.new(:template => filename) }.should raise_error(ArgumentError)
47
+ end
48
+
49
+ it "should raise_error Prawn::Errors::TemplateError when given a non PDF as a template" do
50
+ filename = "#{Prawn::DATADIR}/images/dice.png"
51
+
52
+ lambda { Prawn::Core::ObjectStore.new(:template => filename) }.should raise_error(Prawn::Errors::TemplateError)
53
+ end
54
+
55
+ it "should raise_error Prawn::Errors::TemplateError when given an encrypted PDF as a template" do
56
+ filename = "#{Prawn::DATADIR}/pdfs/encrypted.pdf"
57
+
58
+ lambda { Prawn::Core::ObjectStore.new(:template => filename) }.should raise_error(Prawn::Errors::TemplateError)
59
+ end
60
+
61
+ it "should add to its objects when ref() is called" do
62
+ count = @store.size
63
+ @store.ref("blah")
64
+ @store.size.should == count + 1
65
+ end
66
+
67
+ it "should accept push with a Prawn::Reference" do
68
+ r = Prawn::Core::Reference(123, "blah")
69
+ @store.push(r)
70
+ @store[r.identifier].should == r
71
+ end
72
+
73
+ it "should accept arbitrary data and use it to create a Prawn::Reference" do
74
+ @store.push(123, "blahblah")
75
+ @store[123].data.should == "blahblah"
76
+ end
77
+
78
+ it "should be Enumerable, yielding in order of submission" do
79
+ # higher IDs to bypass the default roots
80
+ [10, 11, 12].each do |id|
81
+ @store.push(id, "some data #{id}")
82
+ end
83
+ @store.map{|ref| ref.identifier}[-3..-1].should == [10, 11, 12]
84
+ end
85
+ end
86
+
87
+ describe "Prawn::ObjectStore#compact" do
88
+ it "should do nothing to an ObjectStore with all live refs" do
89
+ store = Prawn::Core::ObjectStore.new
90
+ store.info.data[:Blah] = store.ref(:some => "structure")
91
+ old_size = store.size
92
+ store.compact
93
+
94
+ store.size.should == old_size
95
+ end
96
+
97
+ it "should remove dead objects, renumbering live objects from 1" do
98
+ store = Prawn::Core::ObjectStore.new
99
+ store.ref(:some => "structure")
100
+ old_size = store.size
101
+ store.compact
102
+
103
+ store.size.should be < old_size
104
+ store.map{ |o| o.identifier }.should == (1..store.size).to_a
105
+ end
106
+
107
+ it "should detect and remove dead objects that were once live" do
108
+ store = Prawn::Core::ObjectStore.new
109
+ store.info.data[:Blah] = store.ref(:some => "structure")
110
+ store.info.data[:Blah] = :overwritten
111
+ old_size = store.size
112
+ store.compact
113
+
114
+ store.size.should be < old_size
115
+ store.map{ |o| o.identifier }.should == (1..store.size).to_a
116
+ end
117
+ end
118
+
119
+ describe "Prawn::ObjectStorie#object_id_for_page" do
120
+ it "should return the object ID of an imported template page" do
121
+ filename = "#{Prawn::DATADIR}/pdfs/hexagon.pdf"
122
+ store = Prawn::Core::ObjectStore.new(:template => filename)
123
+ store.object_id_for_page(0).should == 4
124
+ end
125
+
126
+ it "should return the object ID of the first imported template page" do
127
+ filename = "#{Prawn::DATADIR}/pdfs/two_hexagons.pdf"
128
+ store = Prawn::Core::ObjectStore.new(:template => filename)
129
+ store.object_id_for_page(1).should == 4
130
+ end
131
+
132
+ it "should return the object ID of the last imported template page" do
133
+ filename = "#{Prawn::DATADIR}/pdfs/two_hexagons.pdf"
134
+ store = Prawn::Core::ObjectStore.new(:template => filename)
135
+ store.object_id_for_page(-1).should == 6
136
+ end
137
+
138
+ it "should return the object ID of the first page of a template that uses nested Pages" do
139
+ filename = "#{Prawn::DATADIR}/pdfs/nested_pages.pdf"
140
+ store = Prawn::Core::ObjectStore.new(:template => filename)
141
+ store.object_id_for_page(1).should == 5
142
+ end
143
+
144
+ it "should return the object ID of the last page of a template that uses nested Pages" do
145
+ filename = "#{Prawn::DATADIR}/pdfs/nested_pages.pdf"
146
+ store = Prawn::Core::ObjectStore.new(:template => filename)
147
+ store.object_id_for_page(-1).should == 8
148
+ end
149
+
150
+ it "should return nil if given an invalid page number" do
151
+ filename = "#{Prawn::DATADIR}/pdfs/hexagon.pdf"
152
+ store = Prawn::Core::ObjectStore.new(:template => filename)
153
+ store.object_id_for_page(10).should == nil
154
+ end
155
+
156
+ it "should return nil if given an invalid page number" do
157
+ store = Prawn::Core::ObjectStore.new
158
+ store.object_id_for_page(10).should == nil
159
+ end
160
+
161
+ it "should accept a stream instead of a filename" do
162
+ example = Prawn::Document.new()
163
+ example.text "An example doc, created in memory"
164
+ example.start_new_page
165
+ StringIO.open(example.render) do |stream|
166
+ @pdf = Prawn::Core::ObjectStore.new(:template => stream)
167
+ end
168
+ @pdf.page_count.should == 2
169
+ end
170
+ end
@@ -0,0 +1,426 @@
1
+ # encoding: utf-8
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
3
+
4
+ describe "Outline" do
5
+ before(:each) do
6
+ @pdf = Prawn::Document.new() do
7
+ text "Page 1. This is the first Chapter. "
8
+ start_new_page
9
+ text "Page 2. More in the first Chapter. "
10
+ start_new_page
11
+ outline.define do
12
+ section 'Chapter 1', :destination => 1, :closed => true do
13
+ page :destination => 1, :title => 'Page 1'
14
+ page :destination => 2, :title => 'Page 2'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ if RUBY_VERSION >= "1.9"
20
+ describe "outline encoding" do
21
+ it "should store all outline titles as UTF-16" do
22
+ render_and_find_objects
23
+ @hash.values.each do |obj|
24
+ if obj.is_a?(Hash) && obj[:Title]
25
+ title = obj[:Title].dup
26
+ title.force_encoding("UTF-16LE")
27
+ title.valid_encoding?.should == true
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ describe "#generate_outline" do
34
+ before(:each) do
35
+ render_and_find_objects
36
+ end
37
+
38
+ it "should create a root outline dictionary item" do
39
+ @outline_root.should_not be_nil
40
+ end
41
+
42
+ it "should set the first and last top items of the root outline dictionary item" do
43
+ referenced_object(@outline_root[:First]).should == @section_1
44
+ referenced_object(@outline_root[:Last]).should == @section_1
45
+ end
46
+
47
+ describe "#create_outline_item" do
48
+ it "should create outline items for each section and page" do
49
+ [@section_1, @page_1, @page_2].each {|item| item.should_not be_nil}
50
+ end
51
+ end
52
+
53
+ describe "#set_relations, #set_variables_for_block, and #reset_parent" do
54
+ it "should link sibling items" do
55
+ referenced_object(@page_1[:Next]).should == @page_2
56
+ referenced_object(@page_2[:Prev]).should == @page_1
57
+ end
58
+
59
+ it "should link child items to parent item" do
60
+ [@page_1, @page_2].each {|page| referenced_object(page[:Parent]).should == @section_1 }
61
+ end
62
+
63
+ it "should set the first and last child items for parent item" do
64
+ referenced_object(@section_1[:First]).should == @page_1
65
+ referenced_object(@section_1[:Last]).should == @page_2
66
+ end
67
+ end
68
+
69
+ describe "#increase_count" do
70
+
71
+ it "should add the count of all descendant items" do
72
+ @outline_root[:Count].should == 3
73
+ @section_1[:Count].abs.should == 2
74
+ @page_1[:Count].should == 0
75
+ @page_2[:Count].should == 0
76
+ end
77
+
78
+ end
79
+
80
+ describe "closed option" do
81
+
82
+ it "should set the item's integer count to negative" do
83
+ @section_1[:Count].should == -2
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
90
+ describe "addding a section later with outline#section" do
91
+ before(:each) do
92
+ @pdf.start_new_page
93
+ @pdf.text "Page 3. An added section "
94
+ @pdf.outline.update do
95
+ section 'Added Section', :destination => 3 do
96
+ page :destination => 3, :title => 'Page 3'
97
+ end
98
+ end
99
+ render_and_find_objects
100
+ end
101
+
102
+ it "should add new outline items to document" do
103
+ [@section_2, @page_3].each { |item| item.should_not be_nil}
104
+ end
105
+
106
+ it "should reset the last items for root outline dictionary" do
107
+ referenced_object(@outline_root[:First]).should == @section_1
108
+ referenced_object(@outline_root[:Last]).should == @section_2
109
+ end
110
+
111
+ it "should reset the next relation for the previous last top level item" do
112
+ referenced_object(@section_1[:Next]).should == @section_2
113
+ end
114
+
115
+ it "should set the previous relation of the addded to section" do
116
+ referenced_object(@section_2[:Prev]).should == @section_1
117
+ end
118
+
119
+ it "should increase the count of root outline dictionary" do
120
+ @outline_root[:Count].should == 5
121
+ end
122
+
123
+ end
124
+
125
+ describe "#outline.add_subsection_to" do
126
+ context "positioned last" do
127
+
128
+ before(:each) do
129
+ @pdf.start_new_page
130
+ @pdf.text "Page 3. An added subsection "
131
+ @pdf.outline.update do
132
+ add_subsection_to 'Chapter 1' do
133
+ section 'Added SubSection', :destination => 3 do
134
+ page :destination => 3, :title => 'Added Page 3'
135
+ end
136
+ end
137
+ end
138
+ render_and_find_objects
139
+ end
140
+
141
+ it "should add new outline items to document" do
142
+ [@subsection, @added_page_3].each { |item| item.should_not be_nil}
143
+ end
144
+
145
+ it "should reset the last item for parent item dictionary" do
146
+ referenced_object(@section_1[:First]).should == @page_1
147
+ referenced_object(@section_1[:Last]).should == @subsection
148
+ end
149
+
150
+ it "should set the prev relation for the new subsection to its parent's old last item" do
151
+ referenced_object(@subsection[:Prev]).should == @page_2
152
+ end
153
+
154
+
155
+ it "the subsection should become the next relation for its parent's old last item" do
156
+ referenced_object(@page_2[:Next]).should == @subsection
157
+ end
158
+
159
+ it "should set the first relation for the new subsection" do
160
+ referenced_object(@subsection[:First]).should == @added_page_3
161
+ end
162
+
163
+ it "should set the correct last relation of the added to section" do
164
+ referenced_object(@subsection[:Last]).should == @added_page_3
165
+ end
166
+
167
+ it "should increase the count of root outline dictionary" do
168
+ @outline_root[:Count].should == 5
169
+ end
170
+
171
+ end
172
+
173
+ context "positioned first" do
174
+
175
+ before(:each) do
176
+ @pdf.start_new_page
177
+ @pdf.text "Page 3. An added subsection "
178
+ @pdf.outline.update do
179
+ add_subsection_to 'Chapter 1', :first do
180
+ section 'Added SubSection', :destination => 3 do
181
+ page :destination => 3, :title => 'Added Page 3'
182
+ end
183
+ end
184
+ end
185
+ render_and_find_objects
186
+ end
187
+
188
+ it "should add new outline items to document" do
189
+ [@subsection, @added_page_3].each { |item| item.should_not be_nil}
190
+ end
191
+
192
+ it "should reset the first item for parent item dictionary" do
193
+ referenced_object(@section_1[:First]).should == @subsection
194
+ referenced_object(@section_1[:Last]).should == @page_2
195
+ end
196
+
197
+ it "should set the next relation for the new subsection to its parent's old first item" do
198
+ referenced_object(@subsection[:Next]).should == @page_1
199
+ end
200
+
201
+ it "the subsection should become the prev relation for its parent's old first item" do
202
+ referenced_object(@page_1[:Prev]).should == @subsection
203
+ end
204
+
205
+ it "should set the first relation for the new subsection" do
206
+ referenced_object(@subsection[:First]).should == @added_page_3
207
+ end
208
+
209
+ it "should set the correct last relation of the added to section" do
210
+ referenced_object(@subsection[:Last]).should == @added_page_3
211
+ end
212
+
213
+ it "should increase the count of root outline dictionary" do
214
+ @outline_root[:Count].should == 5
215
+ end
216
+
217
+ end
218
+
219
+ it "should require an existing title" do
220
+ lambda do
221
+ @pdf.go_to_page 1
222
+ @pdf.start_new_page
223
+ @pdf.text "Inserted Page"
224
+ @pdf.outline.update do
225
+ add_subsection_to 'Wrong page' do
226
+ page page_number, :title => "Inserted Page"
227
+ end
228
+ end
229
+ render_and_find_objects
230
+ end.should raise_error(Prawn::Errors::UnknownOutlineTitle)
231
+ end
232
+ end
233
+
234
+ describe "#outline.insert_section_after" do
235
+ describe "inserting in the middle of another section" do
236
+ before(:each) do
237
+ @pdf.go_to_page 1
238
+ @pdf.start_new_page
239
+ @pdf.text "Inserted Page"
240
+ @pdf.outline.update do
241
+ insert_section_after 'Page 1' do
242
+ page :destination => page_number, :title => "Inserted Page"
243
+ end
244
+ end
245
+ end
246
+
247
+ it "should insert new outline items to document" do
248
+ render_and_find_objects
249
+ @inserted_page.should_not be_nil
250
+ end
251
+
252
+ it "should adjust the count of all ancestors" do
253
+ render_and_find_objects
254
+ @outline_root[:Count].should == 4
255
+ @section_1[:Count].abs.should == 3
256
+ end
257
+
258
+ describe "#adjust_relations" do
259
+
260
+ it "should reset the sibling relations of adjoining items to inserted item" do
261
+ render_and_find_objects
262
+ referenced_object(@page_1[:Next]).should == @inserted_page
263
+ referenced_object(@page_2[:Prev]).should == @inserted_page
264
+ end
265
+
266
+ it "should set the sibling relation of added item to adjoining items" do
267
+ render_and_find_objects
268
+ referenced_object(@inserted_page[:Next]).should == @page_2
269
+ referenced_object(@inserted_page[:Prev]).should == @page_1
270
+ end
271
+
272
+ it "should not affect the first and last relations of parent item" do
273
+ render_and_find_objects
274
+ referenced_object(@section_1[:First]).should == @page_1
275
+ referenced_object(@section_1[:Last]).should == @page_2
276
+ end
277
+
278
+ end
279
+
280
+
281
+ context "when adding another section afterwards" do
282
+ it "should have reset the root position so that a new section is added at the end of root sections" do
283
+ @pdf.start_new_page
284
+ @pdf.text "Another Inserted Page"
285
+ @pdf.outline.update do
286
+ section 'Added Section' do
287
+ page :destination => page_number, :title => "Inserted Page"
288
+ end
289
+ end
290
+ render_and_find_objects
291
+ referenced_object(@outline_root[:Last]).should == @section_2
292
+ referenced_object(@section_1[:Next]).should == @section_2
293
+ end
294
+ end
295
+
296
+ end
297
+
298
+
299
+ describe "inserting at the end of another section" do
300
+
301
+ before(:each) do
302
+ @pdf.go_to_page 2
303
+ @pdf.start_new_page
304
+ @pdf.text "Inserted Page"
305
+ @pdf.outline.update do
306
+ insert_section_after 'Page 2' do
307
+ page :destination => page_number, :title => "Inserted Page"
308
+ end
309
+ end
310
+ render_and_find_objects
311
+ end
312
+
313
+ describe "#adjust_relations" do
314
+
315
+ it "should reset the sibling relations of adjoining item to inserted item" do
316
+ referenced_object(@page_2[:Next]).should == @inserted_page
317
+ end
318
+
319
+ it "should set the sibling relation of added item to adjoining items" do
320
+ referenced_object(@inserted_page[:Next]).should be_nil
321
+ referenced_object(@inserted_page[:Prev]).should == @page_2
322
+ end
323
+
324
+ it "should adjust the last relation of parent item" do
325
+ referenced_object(@section_1[:Last]).should == @inserted_page
326
+ end
327
+
328
+ end
329
+ end
330
+
331
+ it "should require an existing title" do
332
+ lambda do
333
+ @pdf.go_to_page 1
334
+ @pdf.start_new_page
335
+ @pdf.text "Inserted Page"
336
+ @pdf.outline.update do
337
+ insert_section_after 'Wrong page' do
338
+ page :destination => page_number, :title => "Inserted Page"
339
+ end
340
+ end
341
+ render_and_find_objects
342
+ end.should raise_error(Prawn::Errors::UnknownOutlineTitle)
343
+ end
344
+
345
+ end
346
+
347
+ describe "#page" do
348
+ it "should require a title option to be set" do
349
+ lambda do
350
+ @pdf = Prawn::Document.new() do
351
+ text "Page 1. This is the first Chapter. "
352
+ outline.define do
353
+ page :destination => 1, :title => nil
354
+ end
355
+ end
356
+ end.should raise_error(Prawn::Errors::RequiredOption)
357
+ end
358
+ end
359
+ end
360
+
361
+ describe "foreign character encoding" do
362
+ before(:each) do
363
+ pdf = Prawn::Document.new() do
364
+ outline.define do
365
+ section 'La pomme croquée', :destination => 1, :closed => true
366
+ end
367
+ end
368
+ @hash = PDF::Reader::ObjectHash.new(StringIO.new(pdf.render, 'r+'))
369
+ end
370
+
371
+ it "should handle other encodings for the title" do
372
+ object = find_by_title('La pomme croquée')
373
+ object.should_not == nil
374
+ end
375
+ end
376
+
377
+ describe "with optimize_objects option" do
378
+ before(:each) do
379
+ @pdf = Prawn::Document.new(:optimize_objects => true) do
380
+ outline.define do
381
+ section 'Chapter 1', :destination => 1, :closed => true do
382
+ page :destination => 1, :title => 'Page 1'
383
+ end
384
+ end
385
+ end
386
+ render_and_find_objects
387
+ end
388
+
389
+ it "should generate an outline" do
390
+ @section_1.should_not be_nil
391
+ @page_1.should_not be_nil
392
+ end
393
+ end
394
+
395
+ def render_and_find_objects
396
+ output = StringIO.new(@pdf.render, 'r+')
397
+ @hash = PDF::Reader::ObjectHash.new(output)
398
+ @outline_root = @hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Outlines}
399
+ @pages = @hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Pages}[:Kids]
400
+ @section_1 = find_by_title('Chapter 1')
401
+ @page_1 = find_by_title('Page 1')
402
+ @page_2 = find_by_title('Page 2')
403
+ @section_2 = find_by_title('Added Section')
404
+ @page_3 = find_by_title('Page 3')
405
+ @inserted_page = find_by_title('Inserted Page')
406
+ @subsection = find_by_title('Added SubSection')
407
+ @added_page_3 = find_by_title('Added Page 3')
408
+ end
409
+
410
+ # Outline titles are stored as UTF-16. This method accepts a UTF-8 outline title
411
+ # and returns the PDF Object that contains an outline with that name
412
+ def find_by_title(title)
413
+ @hash.values.find {|obj|
414
+ if obj.is_a?(Hash) && obj[:Title]
415
+ title_codepoints = obj[:Title].unpack("n*")
416
+ title_codepoints.shift
417
+ utf8_title = title_codepoints.pack("U*")
418
+ utf8_title == title ? obj : nil
419
+ end
420
+ }
421
+ end
422
+
423
+ def referenced_object(reference)
424
+ @hash[reference]
425
+ end
426
+