prawn 0.15.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/COPYING +2 -2
  2. data/LICENSE +1 -1
  3. data/README.md +96 -0
  4. data/Rakefile +27 -30
  5. data/data/fonts/Action Man.dfont +0 -0
  6. data/data/fonts/Activa.ttf +0 -0
  7. data/data/fonts/Chalkboard.ttf +0 -0
  8. data/data/fonts/DejaVuSans.ttf +0 -0
  9. data/data/fonts/Dustismo_Roman.ttf +0 -0
  10. data/data/fonts/comicsans.ttf +0 -0
  11. data/data/fonts/gkai00mp.ttf +0 -0
  12. data/data/images/16bit.alpha +0 -0
  13. data/data/images/16bit.dat +0 -0
  14. data/data/images/dice.alpha +0 -0
  15. data/data/images/dice.dat +0 -0
  16. data/data/images/page_white_text.alpha +0 -0
  17. data/data/images/page_white_text.dat +0 -0
  18. data/data/images/rails.dat +0 -0
  19. data/data/images/rails.png +0 -0
  20. data/data/pdfs/nested_pages.pdf +13 -13
  21. data/lib/prawn.rb +21 -85
  22. data/lib/prawn/compatibility.rb +51 -0
  23. data/lib/prawn/core.rb +85 -0
  24. data/lib/prawn/core/annotations.rb +61 -0
  25. data/lib/prawn/core/byte_string.rb +9 -0
  26. data/lib/prawn/core/destinations.rb +90 -0
  27. data/lib/prawn/core/document_state.rb +78 -0
  28. data/lib/prawn/core/literal_string.rb +16 -0
  29. data/lib/prawn/core/name_tree.rb +177 -0
  30. data/lib/prawn/core/object_store.rb +264 -0
  31. data/lib/prawn/core/page.rb +215 -0
  32. data/lib/prawn/core/pdf_object.rb +108 -0
  33. data/lib/prawn/core/reference.rb +115 -0
  34. data/lib/prawn/core/text.rb +268 -0
  35. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  36. data/lib/prawn/core/text/formatted/line_wrap.rb +273 -0
  37. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  38. data/lib/prawn/document.rb +122 -155
  39. data/lib/prawn/document/bounding_box.rb +7 -36
  40. data/lib/prawn/document/column_box.rb +10 -38
  41. data/lib/prawn/document/graphics_state.rb +74 -11
  42. data/lib/prawn/document/internals.rb +23 -24
  43. data/lib/prawn/document/page_geometry.rb +136 -0
  44. data/lib/prawn/document/snapshot.rb +6 -7
  45. data/lib/prawn/document/span.rb +10 -12
  46. data/lib/prawn/encoding.rb +10 -9
  47. data/lib/prawn/errors.rb +30 -15
  48. data/lib/prawn/font.rb +104 -136
  49. data/lib/prawn/font/afm.rb +44 -46
  50. data/lib/prawn/font/dfont.rb +3 -4
  51. data/lib/prawn/font/ttf.rb +50 -31
  52. data/lib/prawn/graphics.rb +57 -302
  53. data/lib/prawn/graphics/cap_style.rb +3 -4
  54. data/lib/prawn/graphics/color.rb +5 -13
  55. data/lib/prawn/graphics/dash.rb +31 -53
  56. data/lib/prawn/graphics/gradient.rb +84 -0
  57. data/lib/prawn/graphics/join_style.rb +7 -9
  58. data/lib/prawn/graphics/transformation.rb +9 -10
  59. data/lib/prawn/graphics/transparency.rb +1 -3
  60. data/lib/prawn/images.rb +59 -69
  61. data/lib/prawn/images/image.rb +22 -6
  62. data/lib/prawn/images/jpg.rb +14 -20
  63. data/lib/prawn/images/png.rb +118 -61
  64. data/lib/prawn/layout.rb +15 -10
  65. data/lib/prawn/layout/grid.rb +54 -66
  66. data/lib/prawn/measurement_extensions.rb +6 -10
  67. data/lib/prawn/measurements.rb +21 -27
  68. data/lib/prawn/outline.rb +308 -6
  69. data/lib/prawn/repeater.rb +8 -10
  70. data/lib/prawn/security.rb +33 -55
  71. data/lib/prawn/security/arcfour.rb +0 -1
  72. data/lib/prawn/stamp.rb +3 -5
  73. data/lib/prawn/table.rb +60 -188
  74. data/lib/prawn/table/cell.rb +44 -272
  75. data/lib/prawn/table/cell/image.rb +3 -2
  76. data/lib/prawn/table/cell/in_table.rb +2 -4
  77. data/lib/prawn/table/cell/subtable.rb +2 -2
  78. data/lib/prawn/table/cell/text.rb +18 -41
  79. data/lib/prawn/table/cells.rb +48 -142
  80. data/lib/prawn/text.rb +25 -32
  81. data/lib/prawn/text/box.rb +6 -12
  82. data/lib/prawn/text/formatted.rb +4 -5
  83. data/lib/prawn/text/formatted/box.rb +59 -96
  84. data/lib/prawn/text/formatted/fragment.rb +23 -34
  85. data/lib/prawn/text/formatted/parser.rb +5 -15
  86. data/prawn.gemspec +13 -24
  87. data/spec/annotations_spec.rb +32 -16
  88. data/spec/bounding_box_spec.rb +17 -119
  89. data/spec/cell_spec.rb +42 -112
  90. data/spec/destinations_spec.rb +5 -5
  91. data/spec/document_spec.rb +111 -155
  92. data/spec/extensions/mocha.rb +0 -1
  93. data/spec/font_spec.rb +99 -149
  94. data/spec/formatted_text_arranger_spec.rb +43 -43
  95. data/spec/formatted_text_box_spec.rb +44 -43
  96. data/spec/formatted_text_fragment_spec.rb +8 -8
  97. data/spec/graphics_spec.rb +68 -151
  98. data/spec/grid_spec.rb +15 -26
  99. data/spec/images_spec.rb +30 -51
  100. data/spec/inline_formatted_text_parser_spec.rb +20 -69
  101. data/spec/jpg_spec.rb +4 -4
  102. data/spec/line_wrap_spec.rb +28 -28
  103. data/spec/measurement_units_spec.rb +6 -6
  104. data/spec/name_tree_spec.rb +112 -0
  105. data/spec/object_store_spec.rb +106 -17
  106. data/spec/outline_spec.rb +63 -103
  107. data/spec/pdf_object_spec.rb +170 -0
  108. data/spec/png_spec.rb +25 -25
  109. data/spec/reference_spec.rb +65 -8
  110. data/spec/repeater_spec.rb +10 -10
  111. data/spec/security_spec.rb +12 -44
  112. data/spec/snapshot_spec.rb +7 -7
  113. data/spec/span_spec.rb +15 -10
  114. data/spec/spec_helper.rb +8 -32
  115. data/spec/stamp_spec.rb +30 -29
  116. data/spec/stroke_styles_spec.rb +18 -36
  117. data/spec/table_spec.rb +111 -706
  118. data/spec/template_spec.rb +297 -0
  119. data/spec/text_at_spec.rb +33 -19
  120. data/spec/text_box_spec.rb +64 -100
  121. data/spec/text_rendering_mode_spec.rb +5 -5
  122. data/spec/text_spacing_spec.rb +4 -4
  123. data/spec/text_spec.rb +64 -84
  124. data/spec/transparency_spec.rb +5 -5
  125. metadata +290 -463
  126. checksums.yaml +0 -7
  127. data/.yardopts +0 -10
  128. data/Gemfile +0 -11
  129. data/data/images/16bit.color +0 -0
  130. data/data/images/dice.color +0 -0
  131. data/data/images/indexed_color.dat +0 -0
  132. data/data/images/indexed_color.png +0 -0
  133. data/data/images/page_white_text.color +0 -0
  134. data/lib/prawn/font_metric_cache.rb +0 -47
  135. data/lib/prawn/graphics/patterns.rb +0 -138
  136. data/lib/prawn/image_handler.rb +0 -36
  137. data/lib/prawn/soft_mask.rb +0 -96
  138. data/lib/prawn/table/cell/span_dummy.rb +0 -93
  139. data/lib/prawn/table/column_width_calculator.rb +0 -61
  140. data/lib/prawn/text/formatted/arranger.rb +0 -290
  141. data/lib/prawn/text/formatted/line_wrap.rb +0 -266
  142. data/lib/prawn/text/formatted/wrap.rb +0 -150
  143. data/lib/prawn/utilities.rb +0 -46
  144. data/manual/basic_concepts/adding_pages.rb +0 -27
  145. data/manual/basic_concepts/basic_concepts.rb +0 -34
  146. data/manual/basic_concepts/creation.rb +0 -39
  147. data/manual/basic_concepts/cursor.rb +0 -33
  148. data/manual/basic_concepts/measurement.rb +0 -25
  149. data/manual/basic_concepts/origin.rb +0 -38
  150. data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
  151. data/manual/bounding_box/bounding_box.rb +0 -39
  152. data/manual/bounding_box/bounds.rb +0 -49
  153. data/manual/bounding_box/canvas.rb +0 -24
  154. data/manual/bounding_box/creation.rb +0 -23
  155. data/manual/bounding_box/indentation.rb +0 -46
  156. data/manual/bounding_box/nesting.rb +0 -45
  157. data/manual/bounding_box/russian_boxes.rb +0 -40
  158. data/manual/bounding_box/stretchy.rb +0 -31
  159. data/manual/document_and_page_options/background.rb +0 -27
  160. data/manual/document_and_page_options/document_and_page_options.rb +0 -32
  161. data/manual/document_and_page_options/metadata.rb +0 -23
  162. data/manual/document_and_page_options/page_margins.rb +0 -38
  163. data/manual/document_and_page_options/page_size.rb +0 -34
  164. data/manual/document_and_page_options/print_scaling.rb +0 -20
  165. data/manual/example_file.rb +0 -111
  166. data/manual/example_helper.rb +0 -411
  167. data/manual/example_package.rb +0 -53
  168. data/manual/example_section.rb +0 -46
  169. data/manual/graphics/circle_and_ellipse.rb +0 -22
  170. data/manual/graphics/color.rb +0 -24
  171. data/manual/graphics/common_lines.rb +0 -30
  172. data/manual/graphics/fill_and_stroke.rb +0 -42
  173. data/manual/graphics/fill_rules.rb +0 -37
  174. data/manual/graphics/gradients.rb +0 -37
  175. data/manual/graphics/graphics.rb +0 -58
  176. data/manual/graphics/helper.rb +0 -24
  177. data/manual/graphics/line_width.rb +0 -35
  178. data/manual/graphics/lines_and_curves.rb +0 -41
  179. data/manual/graphics/polygon.rb +0 -29
  180. data/manual/graphics/rectangle.rb +0 -21
  181. data/manual/graphics/rotate.rb +0 -28
  182. data/manual/graphics/scale.rb +0 -41
  183. data/manual/graphics/soft_masks.rb +0 -46
  184. data/manual/graphics/stroke_cap.rb +0 -31
  185. data/manual/graphics/stroke_dash.rb +0 -48
  186. data/manual/graphics/stroke_join.rb +0 -30
  187. data/manual/graphics/translate.rb +0 -29
  188. data/manual/graphics/transparency.rb +0 -35
  189. data/manual/images/absolute_position.rb +0 -23
  190. data/manual/images/fit.rb +0 -21
  191. data/manual/images/horizontal.rb +0 -25
  192. data/manual/images/images.rb +0 -40
  193. data/manual/images/plain_image.rb +0 -18
  194. data/manual/images/scale.rb +0 -22
  195. data/manual/images/vertical.rb +0 -28
  196. data/manual/images/width_and_height.rb +0 -25
  197. data/manual/layout/boxes.rb +0 -27
  198. data/manual/layout/content.rb +0 -25
  199. data/manual/layout/layout.rb +0 -28
  200. data/manual/layout/simple_grid.rb +0 -23
  201. data/manual/manual/cover.rb +0 -36
  202. data/manual/manual/foreword.rb +0 -85
  203. data/manual/manual/how_to_read_this_manual.rb +0 -41
  204. data/manual/manual/manual.rb +0 -34
  205. data/manual/outline/add_subsection_to.rb +0 -61
  206. data/manual/outline/insert_section_after.rb +0 -47
  207. data/manual/outline/outline.rb +0 -32
  208. data/manual/outline/sections_and_pages.rb +0 -67
  209. data/manual/repeatable_content/page_numbering.rb +0 -54
  210. data/manual/repeatable_content/repeatable_content.rb +0 -31
  211. data/manual/repeatable_content/repeater.rb +0 -55
  212. data/manual/repeatable_content/stamp.rb +0 -41
  213. data/manual/security/encryption.rb +0 -31
  214. data/manual/security/permissions.rb +0 -38
  215. data/manual/security/security.rb +0 -28
  216. data/manual/syntax_highlight.rb +0 -52
  217. data/manual/table/basic_block.rb +0 -53
  218. data/manual/table/before_rendering_page.rb +0 -26
  219. data/manual/table/cell_border_lines.rb +0 -24
  220. data/manual/table/cell_borders_and_bg.rb +0 -31
  221. data/manual/table/cell_dimensions.rb +0 -30
  222. data/manual/table/cell_text.rb +0 -38
  223. data/manual/table/column_widths.rb +0 -30
  224. data/manual/table/content_and_subtables.rb +0 -39
  225. data/manual/table/creation.rb +0 -27
  226. data/manual/table/filtering.rb +0 -36
  227. data/manual/table/flow_and_header.rb +0 -17
  228. data/manual/table/image_cells.rb +0 -33
  229. data/manual/table/position.rb +0 -29
  230. data/manual/table/row_colors.rb +0 -20
  231. data/manual/table/span.rb +0 -30
  232. data/manual/table/style.rb +0 -22
  233. data/manual/table/table.rb +0 -52
  234. data/manual/table/width.rb +0 -27
  235. data/manual/text/alignment.rb +0 -44
  236. data/manual/text/color.rb +0 -24
  237. data/manual/text/column_box.rb +0 -32
  238. data/manual/text/fallback_fonts.rb +0 -37
  239. data/manual/text/font.rb +0 -41
  240. data/manual/text/font_size.rb +0 -45
  241. data/manual/text/font_style.rb +0 -23
  242. data/manual/text/formatted_callbacks.rb +0 -60
  243. data/manual/text/formatted_text.rb +0 -54
  244. data/manual/text/free_flowing_text.rb +0 -51
  245. data/manual/text/group.rb +0 -31
  246. data/manual/text/inline.rb +0 -43
  247. data/manual/text/kerning_and_character_spacing.rb +0 -39
  248. data/manual/text/leading.rb +0 -25
  249. data/manual/text/line_wrapping.rb +0 -41
  250. data/manual/text/paragraph_indentation.rb +0 -26
  251. data/manual/text/positioned_text.rb +0 -38
  252. data/manual/text/registering_families.rb +0 -48
  253. data/manual/text/rendering_and_color.rb +0 -37
  254. data/manual/text/right_to_left_text.rb +0 -43
  255. data/manual/text/rotation.rb +0 -43
  256. data/manual/text/single_usage.rb +0 -37
  257. data/manual/text/text.rb +0 -75
  258. data/manual/text/text_box_excess.rb +0 -32
  259. data/manual/text/text_box_extensions.rb +0 -45
  260. data/manual/text/text_box_overflow.rb +0 -44
  261. data/manual/text/utf8.rb +0 -28
  262. data/manual/text/win_ansi_charset.rb +0 -59
  263. data/spec/acceptance/png.rb +0 -23
  264. data/spec/column_box_spec.rb +0 -65
  265. data/spec/extensions/encoding_helpers.rb +0 -9
  266. data/spec/font_metric_cache_spec.rb +0 -52
  267. data/spec/image_handler_spec.rb +0 -54
  268. data/spec/soft_mask_spec.rb +0 -117
  269. data/spec/table/span_dummy_spec.rb +0 -17
data/spec/outline_spec.rb CHANGED
@@ -16,27 +16,27 @@ describe "Outline" do
16
16
  end
17
17
  end
18
18
  end
19
-
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(Encoding::UTF_16LE)
27
- title.valid_encoding?.should == true
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
28
29
  end
29
30
  end
30
31
  end
31
32
  end
32
-
33
33
  describe "#generate_outline" do
34
34
  before(:each) do
35
35
  render_and_find_objects
36
36
  end
37
37
 
38
38
  it "should create a root outline dictionary item" do
39
- @outline_root.should_not be_nil
39
+ assert_not_nil @outline_root
40
40
  end
41
41
 
42
42
  it "should set the first and last top items of the root outline dictionary item" do
@@ -46,7 +46,7 @@ describe "Outline" do
46
46
 
47
47
  describe "#create_outline_item" do
48
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}
49
+ [@section_1, @page_1, @page_2].each {|item| assert_not_nil item}
50
50
  end
51
51
  end
52
52
 
@@ -70,7 +70,7 @@ describe "Outline" do
70
70
 
71
71
  it "should add the count of all descendant items" do
72
72
  @outline_root[:Count].should == 3
73
- @section_1[:Count].abs.should == 2
73
+ @section_1[:Count].should.abs == 2
74
74
  @page_1[:Count].should == 0
75
75
  @page_2[:Count].should == 0
76
76
  end
@@ -87,28 +87,6 @@ describe "Outline" do
87
87
 
88
88
  end
89
89
 
90
- describe "adding a custom destination" do
91
- before(:each) do
92
- @pdf.start_new_page
93
- @pdf.text "Page 3 with a destination"
94
- @pdf.add_dest('customdest', @pdf.dest_xyz(200, 200))
95
- pdf = @pdf
96
- @pdf.outline.update do
97
- page :destination => pdf.dest_xyz(200, 200), :title => 'Custom Destination'
98
- end
99
- render_and_find_objects
100
- end
101
-
102
- it "should create an outline item" do
103
- @custom_dest.should_not be_nil
104
- end
105
-
106
- it "should reference the custom destination" do
107
- referenced_object(@custom_dest[:Dest].first).should == referenced_object(@pages.last)
108
- end
109
-
110
- end
111
-
112
90
  describe "addding a section later with outline#section" do
113
91
  before(:each) do
114
92
  @pdf.start_new_page
@@ -122,7 +100,7 @@ describe "Outline" do
122
100
  end
123
101
 
124
102
  it "should add new outline items to document" do
125
- [@section_2, @page_3].each { |item| item.should_not be_nil}
103
+ [@section_2, @page_3].each { |item| assert_not_nil item}
126
104
  end
127
105
 
128
106
  it "should reset the last items for root outline dictionary" do
@@ -142,11 +120,11 @@ describe "Outline" do
142
120
  @outline_root[:Count].should == 5
143
121
  end
144
122
 
145
- end
146
-
147
- describe "#outline.add_subsection_to" do
123
+ end
124
+
125
+ describe "#outline.add_subsection_to" do
148
126
  context "positioned last" do
149
-
127
+
150
128
  before(:each) do
151
129
  @pdf.start_new_page
152
130
  @pdf.text "Page 3. An added subsection "
@@ -155,45 +133,45 @@ describe "Outline" do
155
133
  section 'Added SubSection', :destination => 3 do
156
134
  page :destination => 3, :title => 'Added Page 3'
157
135
  end
158
- end
136
+ end
159
137
  end
160
138
  render_and_find_objects
161
139
  end
162
-
140
+
163
141
  it "should add new outline items to document" do
164
- [@subsection, @added_page_3].each { |item| item.should_not be_nil}
142
+ [@subsection, @added_page_3].each { |item| assert_not_nil item}
165
143
  end
166
-
144
+
167
145
  it "should reset the last item for parent item dictionary" do
168
146
  referenced_object(@section_1[:First]).should == @page_1
169
147
  referenced_object(@section_1[:Last]).should == @subsection
170
148
  end
171
-
149
+
172
150
  it "should set the prev relation for the new subsection to its parent's old last item" do
173
151
  referenced_object(@subsection[:Prev]).should == @page_2
174
152
  end
175
-
176
-
153
+
154
+
177
155
  it "the subsection should become the next relation for its parent's old last item" do
178
156
  referenced_object(@page_2[:Next]).should == @subsection
179
157
  end
180
-
158
+
181
159
  it "should set the first relation for the new subsection" do
182
160
  referenced_object(@subsection[:First]).should == @added_page_3
183
161
  end
184
-
162
+
185
163
  it "should set the correct last relation of the added to section" do
186
164
  referenced_object(@subsection[:Last]).should == @added_page_3
187
165
  end
188
-
166
+
189
167
  it "should increase the count of root outline dictionary" do
190
168
  @outline_root[:Count].should == 5
191
169
  end
192
-
193
- end
194
-
170
+
171
+ end
172
+
195
173
  context "positioned first" do
196
-
174
+
197
175
  before(:each) do
198
176
  @pdf.start_new_page
199
177
  @pdf.text "Page 3. An added subsection "
@@ -208,14 +186,14 @@ describe "Outline" do
208
186
  end
209
187
 
210
188
  it "should add new outline items to document" do
211
- [@subsection, @added_page_3].each { |item| item.should_not be_nil}
189
+ [@subsection, @added_page_3].each { |item| assert_not_nil item}
212
190
  end
213
191
 
214
192
  it "should reset the first item for parent item dictionary" do
215
193
  referenced_object(@section_1[:First]).should == @subsection
216
194
  referenced_object(@section_1[:Last]).should == @page_2
217
195
  end
218
-
196
+
219
197
  it "should set the next relation for the new subsection to its parent's old first item" do
220
198
  referenced_object(@subsection[:Next]).should == @page_1
221
199
  end
@@ -237,9 +215,9 @@ describe "Outline" do
237
215
  end
238
216
 
239
217
  end
240
-
218
+
241
219
  it "should require an existing title" do
242
- lambda do
220
+ assert_raise Prawn::Errors::UnknownOutlineTitle do
243
221
  @pdf.go_to_page 1
244
222
  @pdf.start_new_page
245
223
  @pdf.text "Inserted Page"
@@ -249,9 +227,9 @@ describe "Outline" do
249
227
  end
250
228
  end
251
229
  render_and_find_objects
252
- end.should raise_error(Prawn::Errors::UnknownOutlineTitle)
230
+ end
253
231
  end
254
- end
232
+ end
255
233
 
256
234
  describe "#outline.insert_section_after" do
257
235
  describe "inserting in the middle of another section" do
@@ -268,47 +246,47 @@ describe "Outline" do
268
246
 
269
247
  it "should insert new outline items to document" do
270
248
  render_and_find_objects
271
- @inserted_page.should_not be_nil
249
+ assert_not_nil @inserted_page
272
250
  end
273
-
251
+
274
252
  it "should adjust the count of all ancestors" do
275
253
  render_and_find_objects
276
254
  @outline_root[:Count].should == 4
277
- @section_1[:Count].abs.should == 3
255
+ @section_1[:Count].should.abs == 3
278
256
  end
279
-
257
+
280
258
  describe "#adjust_relations" do
281
-
259
+
282
260
  it "should reset the sibling relations of adjoining items to inserted item" do
283
261
  render_and_find_objects
284
262
  referenced_object(@page_1[:Next]).should == @inserted_page
285
263
  referenced_object(@page_2[:Prev]).should == @inserted_page
286
264
  end
287
-
265
+
288
266
  it "should set the sibling relation of added item to adjoining items" do
289
267
  render_and_find_objects
290
268
  referenced_object(@inserted_page[:Next]).should == @page_2
291
269
  referenced_object(@inserted_page[:Prev]).should == @page_1
292
270
  end
293
-
271
+
294
272
  it "should not affect the first and last relations of parent item" do
295
273
  render_and_find_objects
296
274
  referenced_object(@section_1[:First]).should == @page_1
297
275
  referenced_object(@section_1[:Last]).should == @page_2
298
276
  end
299
-
300
- end
301
-
302
-
303
- context "when adding another section afterwards" do
277
+
278
+ end
279
+
280
+
281
+ context "when adding another section afterwards" do
304
282
  it "should have reset the root position so that a new section is added at the end of root sections" do
305
283
  @pdf.start_new_page
306
284
  @pdf.text "Another Inserted Page"
307
285
  @pdf.outline.update do
308
286
  section 'Added Section' do
309
287
  page :destination => page_number, :title => "Inserted Page"
310
- end
311
- end
288
+ end
289
+ end
312
290
  render_and_find_objects
313
291
  referenced_object(@outline_root[:Last]).should == @section_2
314
292
  referenced_object(@section_1[:Next]).should == @section_2
@@ -327,7 +305,7 @@ describe "Outline" do
327
305
  @pdf.outline.update do
328
306
  insert_section_after 'Page 2' do
329
307
  page :destination => page_number, :title => "Inserted Page"
330
- end
308
+ end
331
309
  end
332
310
  render_and_find_objects
333
311
  end
@@ -339,7 +317,7 @@ describe "Outline" do
339
317
  end
340
318
 
341
319
  it "should set the sibling relation of added item to adjoining items" do
342
- referenced_object(@inserted_page[:Next]).should be_nil
320
+ assert_nil referenced_object(@inserted_page[:Next])
343
321
  referenced_object(@inserted_page[:Prev]).should == @page_2
344
322
  end
345
323
 
@@ -351,7 +329,7 @@ describe "Outline" do
351
329
  end
352
330
 
353
331
  it "should require an existing title" do
354
- lambda do
332
+ assert_raise Prawn::Errors::UnknownOutlineTitle do
355
333
  @pdf.go_to_page 1
356
334
  @pdf.start_new_page
357
335
  @pdf.text "Inserted Page"
@@ -361,26 +339,26 @@ describe "Outline" do
361
339
  end
362
340
  end
363
341
  render_and_find_objects
364
- end.should raise_error(Prawn::Errors::UnknownOutlineTitle)
342
+ end
365
343
  end
366
344
 
367
345
  end
368
346
 
369
347
  describe "#page" do
370
348
  it "should require a title option to be set" do
371
- lambda do
349
+ assert_raise Prawn::Errors::RequiredOption do
372
350
  @pdf = Prawn::Document.new() do
373
351
  text "Page 1. This is the first Chapter. "
374
352
  outline.define do
375
353
  page :destination => 1, :title => nil
376
354
  end
377
355
  end
378
- end.should raise_error(Prawn::Errors::RequiredOption)
356
+ end
379
357
  end
380
358
  end
381
359
  end
382
-
383
- describe "foreign character encoding" do
360
+
361
+ context "foreign character encoding" do
384
362
  before(:each) do
385
363
  pdf = Prawn::Document.new() do
386
364
  outline.define do
@@ -392,25 +370,7 @@ describe "foreign character encoding" do
392
370
 
393
371
  it "should handle other encodings for the title" do
394
372
  object = find_by_title('La pomme croquée')
395
- object.should_not == nil
396
- end
397
- end
398
-
399
- describe "with optimize_objects option" do
400
- before(:each) do
401
- @pdf = Prawn::Document.new(:optimize_objects => true) do
402
- outline.define do
403
- section 'Chapter 1', :destination => 1, :closed => true do
404
- page :destination => 1, :title => 'Page 1'
405
- end
406
- end
407
- end
408
- render_and_find_objects
409
- end
410
-
411
- it "should generate an outline" do
412
- @section_1.should_not be_nil
413
- @page_1.should_not be_nil
373
+ object.should.not == nil
414
374
  end
415
375
  end
416
376
 
@@ -424,10 +384,9 @@ def render_and_find_objects
424
384
  @page_2 = find_by_title('Page 2')
425
385
  @section_2 = find_by_title('Added Section')
426
386
  @page_3 = find_by_title('Page 3')
427
- @inserted_page = find_by_title('Inserted Page')
387
+ @inserted_page = find_by_title('Inserted Page')
428
388
  @subsection = find_by_title('Added SubSection')
429
389
  @added_page_3 = find_by_title('Added Page 3')
430
- @custom_dest = find_by_title('Custom Destination')
431
390
  end
432
391
 
433
392
  # Outline titles are stored as UTF-16. This method accepts a UTF-8 outline title
@@ -446,3 +405,4 @@ end
446
405
  def referenced_object(reference)
447
406
  @hash[reference]
448
407
  end
408
+
@@ -0,0 +1,170 @@
1
+ # encoding: ASCII-8BIT
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ # See PDF Reference, Sixth Edition (1.7) pp51-60 for details
6
+ describe "PDF Object Serialization" do
7
+
8
+ it "should convert Ruby's nil to PDF null" do
9
+ Prawn::Core::PdfObject(nil).should == "null"
10
+ end
11
+
12
+ it "should convert Ruby booleans to PDF booleans" do
13
+ Prawn::Core::PdfObject(true).should == "true"
14
+ Prawn::Core::PdfObject(false).should == "false"
15
+ end
16
+
17
+ it "should convert a Ruby number to PDF number" do
18
+ Prawn::Core::PdfObject(1).should == "1"
19
+ Prawn::Core::PdfObject(1.214112421).should == "1.214112421"
20
+ end
21
+
22
+ it "should convert a Ruby time object to a PDF timestamp" do
23
+ t = Time.now
24
+ Prawn::Core::PdfObject(t).should == t.strftime("(D:%Y%m%d%H%M%S%z").chop.chop + "'00')"
25
+ end
26
+
27
+ it "should convert a Ruby string to PDF string when inside a content stream" do
28
+ s = "I can has a string"
29
+ PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
30
+ end
31
+
32
+ it "should convert a Ruby string to a UTF-16 PDF string when outside a content stream" do
33
+ s = "I can has a string"
34
+ s_utf16 = "\xFE\xFF" + s.unpack("U*").pack("n*")
35
+ PDF::Inspector.parse(Prawn::Core::PdfObject(s, false)).should == s_utf16
36
+ end
37
+
38
+ it "should convert a Ruby string with characters outside the BMP to its " +
39
+ "UTF-16 representation with a BOM" do
40
+ # U+10192 ROMAN SEMUNCIA SIGN
41
+ semuncia = [65938].pack("U")
42
+ Prawn::Core::PdfObject(semuncia, false).upcase.should == "<FEFFD800DD92>"
43
+ end
44
+
45
+ it "should pass through bytes regardless of content stream status for ByteString" do
46
+ Prawn::Core::PdfObject(Prawn::Core::ByteString.new("\xDE\xAD\xBE\xEF")).upcase.
47
+ should == "<DEADBEEF>"
48
+ end
49
+
50
+ it "should escape parens when converting from Ruby string to PDF" do
51
+ s = 'I )(can has a string'
52
+ PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
53
+ end
54
+
55
+ it "should handle ruby escaped parens when converting to PDF string" do
56
+ s = 'I can \\)( has string'
57
+ PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
58
+ end
59
+
60
+ it "should escape various strings correctly when converting a LiteralString" do
61
+ ls = Prawn::Core::LiteralString.new("abc")
62
+ Prawn::Core::PdfObject(ls).should == "(abc)"
63
+
64
+ ls = Prawn::Core::LiteralString.new("abc\x0Ade") # should escape \n
65
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Ade)"
66
+
67
+ ls = Prawn::Core::LiteralString.new("abc\x0Dde") # should escape \r
68
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Dde)"
69
+
70
+ ls = Prawn::Core::LiteralString.new("abc\x09de") # should escape \t
71
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x09de)"
72
+
73
+ ls = Prawn::Core::LiteralString.new("abc\x08de") # should escape \b
74
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x08de)"
75
+
76
+ ls = Prawn::Core::LiteralString.new("abc\x0Cde") # should escape \f
77
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Cde)"
78
+
79
+ ls = Prawn::Core::LiteralString.new("abc(de") # should escape \(
80
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C(de)"
81
+
82
+ ls = Prawn::Core::LiteralString.new("abc)de") # should escape \)
83
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C)de)"
84
+
85
+ ls = Prawn::Core::LiteralString.new("abc\x5Cde") # should escape \\
86
+ Prawn::Core::PdfObject(ls).should == "(abc\x5C\x5Cde)"
87
+ Prawn::Core::PdfObject(ls).size.should == 9
88
+ end
89
+
90
+ it "should escape strings correctly when converting a LiteralString that is not utf-8" do
91
+ data = "\x43\xaf\xc9\x7f\xef\xf\xe6\xa8\xcb\x5c\xaf\xd0"
92
+ ls = Prawn::Core::LiteralString.new(data)
93
+ Prawn::Core::PdfObject(ls).should == "(\x43\xaf\xc9\x7f\xef\xf\xe6\xa8\xcb\x5c\x5c\xaf\xd0)"
94
+ end
95
+
96
+ it "should convert a Ruby symbol to PDF name" do
97
+ Prawn::Core::PdfObject(:my_symbol).should == "/my_symbol"
98
+ Prawn::Core::PdfObject(:"A;Name_With-Various***Characters?").should ==
99
+ "/A;Name_With-Various***Characters?"
100
+ end
101
+
102
+ it "should convert a whitespace or delimiter containing Ruby symbol to a PDF name" do
103
+ Prawn::Core::PdfObject(:"my symbol").should == "/my#20symbol"
104
+ Prawn::Core::PdfObject(:"my#symbol").should == "/my#23symbol"
105
+ Prawn::Core::PdfObject(:"my/symbol").should == "/my#2Fsymbol"
106
+ Prawn::Core::PdfObject(:"my(symbol").should == "/my#28symbol"
107
+ Prawn::Core::PdfObject(:"my)symbol").should == "/my#29symbol"
108
+ Prawn::Core::PdfObject(:"my<symbol").should == "/my#3Csymbol"
109
+ Prawn::Core::PdfObject(:"my>symbol").should == "/my#3Esymbol"
110
+ end
111
+
112
+ it "should convert a Ruby array to PDF Array when inside a content stream" do
113
+ Prawn::Core::PdfObject([1,2,3]).should == "[1 2 3]"
114
+ PDF::Inspector.parse(Prawn::Core::PdfObject([[1,2],:foo,"Bar"], true)).should ==
115
+ [[1,2],:foo, "Bar"]
116
+ end
117
+
118
+ it "should convert a Ruby array to PDF Array when outside a content stream" do
119
+ bar = "\xFE\xFF" + "Bar".unpack("U*").pack("n*")
120
+ Prawn::Core::PdfObject([1,2,3]).should == "[1 2 3]"
121
+ PDF::Inspector.parse(Prawn::Core::PdfObject([[1,2],:foo,"Bar"], false)).should ==
122
+ [[1,2],:foo, bar]
123
+ end
124
+
125
+ it "should convert a Ruby hash to a PDF Dictionary when inside a content stream" do
126
+ dict = Prawn::Core::PdfObject( {:foo => :bar,
127
+ "baz" => [1,2,3],
128
+ :bang => {:a => "what", :b => [:you, :say] }}, true )
129
+
130
+ res = PDF::Inspector.parse(dict)
131
+
132
+ res[:foo].should == :bar
133
+ res[:baz].should == [1,2,3]
134
+ res[:bang].should == { :a => "what", :b => [:you, :say] }
135
+
136
+ end
137
+
138
+ it "should convert a Ruby hash to a PDF Dictionary when outside a content stream" do
139
+ what = "\xFE\xFF" + "what".unpack("U*").pack("n*")
140
+ dict = Prawn::Core::PdfObject( {:foo => :bar,
141
+ "baz" => [1,2,3],
142
+ :bang => {:a => "what", :b => [:you, :say] }}, false )
143
+
144
+ res = PDF::Inspector.parse(dict)
145
+
146
+ res[:foo].should == :bar
147
+ res[:baz].should == [1,2,3]
148
+ res[:bang].should == { :a => what, :b => [:you, :say] }
149
+
150
+ end
151
+
152
+ it "should not allow keys other than strings or symbols for PDF dicts" do
153
+ lambda { Prawn::Core::PdfObject(:foo => :bar, :baz => :bang, 1 => 4) }.
154
+ should.raise(Prawn::Errors::FailedObjectConversion)
155
+ end
156
+
157
+ it "should convert a Prawn::Reference to a PDF indirect object reference" do
158
+ ref = Prawn::Core::Reference(1,true)
159
+ Prawn::Core::PdfObject(ref).should == ref.to_s
160
+ end
161
+
162
+ it "should convert a NameTree::Node to a PDF hash" do
163
+ node = Prawn::Core::NameTree::Node.new(Prawn::Document.new, 10)
164
+ node.add "hello", 1.0
165
+ node.add "world", 2.0
166
+ data = Prawn::Core::PdfObject(node)
167
+ res = PDF::Inspector.parse(data)
168
+ res.should == {:Names => ["hello", 1.0, "world", 2.0]}
169
+ end
170
+ end