prawn 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/prawn/document/bounding_box.rb +213 -141
  4. data/lib/prawn/document/column_box.rb +61 -26
  5. data/lib/prawn/document/internals.rb +25 -16
  6. data/lib/prawn/document/span.rb +20 -18
  7. data/lib/prawn/document.rb +257 -171
  8. data/lib/prawn/encoding.rb +2 -5
  9. data/lib/prawn/errors.rb +23 -34
  10. data/lib/prawn/font.rb +248 -135
  11. data/lib/prawn/font_metric_cache.rb +11 -10
  12. data/lib/prawn/fonts/afm.rb +85 -45
  13. data/lib/prawn/fonts/dfont.rb +7 -1
  14. data/lib/prawn/fonts/otf.rb +4 -1
  15. data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
  16. data/lib/prawn/fonts/ttc.rb +7 -2
  17. data/lib/prawn/fonts/ttf.rb +305 -93
  18. data/lib/prawn/fonts.rb +14 -0
  19. data/lib/prawn/graphics/blend_mode.rb +25 -28
  20. data/lib/prawn/graphics/cap_style.rb +9 -12
  21. data/lib/prawn/graphics/color.rb +57 -34
  22. data/lib/prawn/graphics/dash.rb +45 -42
  23. data/lib/prawn/graphics/join_style.rb +17 -11
  24. data/lib/prawn/graphics/patterns.rb +190 -69
  25. data/lib/prawn/graphics/transformation.rb +48 -41
  26. data/lib/prawn/graphics/transparency.rb +16 -40
  27. data/lib/prawn/graphics.rb +363 -253
  28. data/lib/prawn/grid.rb +184 -57
  29. data/lib/prawn/image_handler.rb +27 -10
  30. data/lib/prawn/images/image.rb +8 -10
  31. data/lib/prawn/images/jpg.rb +42 -19
  32. data/lib/prawn/images/png.rb +92 -41
  33. data/lib/prawn/images.rb +44 -57
  34. data/lib/prawn/measurement_extensions.rb +39 -8
  35. data/lib/prawn/measurements.rb +60 -5
  36. data/lib/prawn/outline.rb +114 -108
  37. data/lib/prawn/repeater.rb +51 -35
  38. data/lib/prawn/security/arcfour.rb +4 -4
  39. data/lib/prawn/security.rb +75 -70
  40. data/lib/prawn/soft_mask.rb +42 -30
  41. data/lib/prawn/stamp.rb +38 -42
  42. data/lib/prawn/text/box.rb +146 -96
  43. data/lib/prawn/text/formatted/arranger.rb +87 -26
  44. data/lib/prawn/text/formatted/box.rb +221 -150
  45. data/lib/prawn/text/formatted/fragment.rb +130 -14
  46. data/lib/prawn/text/formatted/line_wrap.rb +33 -24
  47. data/lib/prawn/text/formatted/parser.rb +112 -72
  48. data/lib/prawn/text/formatted/wrap.rb +12 -17
  49. data/lib/prawn/text/formatted.rb +75 -0
  50. data/lib/prawn/text.rb +441 -196
  51. data/lib/prawn/transformation_stack.rb +29 -10
  52. data/lib/prawn/utilities.rb +13 -13
  53. data/lib/prawn/version.rb +2 -1
  54. data/lib/prawn/view.rb +68 -53
  55. data/lib/prawn.rb +23 -18
  56. data.tar.gz.sig +0 -0
  57. metadata +54 -177
  58. metadata.gz.sig +0 -0
  59. data/.yardopts +0 -10
  60. data/Gemfile +0 -5
  61. data/Rakefile +0 -25
  62. data/manual/absolute_position.pdf +0 -0
  63. data/manual/basic_concepts/adding_pages.rb +0 -26
  64. data/manual/basic_concepts/basic_concepts.rb +0 -43
  65. data/manual/basic_concepts/creation.rb +0 -38
  66. data/manual/basic_concepts/cursor.rb +0 -32
  67. data/manual/basic_concepts/measurement.rb +0 -24
  68. data/manual/basic_concepts/origin.rb +0 -37
  69. data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
  70. data/manual/basic_concepts/view.rb +0 -48
  71. data/manual/bounding_box/bounding_box.rb +0 -41
  72. data/manual/bounding_box/bounds.rb +0 -48
  73. data/manual/bounding_box/canvas.rb +0 -23
  74. data/manual/bounding_box/creation.rb +0 -22
  75. data/manual/bounding_box/indentation.rb +0 -45
  76. data/manual/bounding_box/nesting.rb +0 -52
  77. data/manual/bounding_box/russian_boxes.rb +0 -40
  78. data/manual/bounding_box/stretchy.rb +0 -29
  79. data/manual/contents.rb +0 -35
  80. data/manual/cover.rb +0 -43
  81. data/manual/document_and_page_options/background.rb +0 -29
  82. data/manual/document_and_page_options/document_and_page_options.rb +0 -34
  83. data/manual/document_and_page_options/metadata.rb +0 -25
  84. data/manual/document_and_page_options/page_margins.rb +0 -36
  85. data/manual/document_and_page_options/page_size.rb +0 -34
  86. data/manual/document_and_page_options/print_scaling.rb +0 -23
  87. data/manual/example_helper.rb +0 -8
  88. data/manual/graphics/blend_mode.rb +0 -52
  89. data/manual/graphics/circle_and_ellipse.rb +0 -21
  90. data/manual/graphics/color.rb +0 -22
  91. data/manual/graphics/common_lines.rb +0 -29
  92. data/manual/graphics/fill_and_stroke.rb +0 -41
  93. data/manual/graphics/fill_rules.rb +0 -38
  94. data/manual/graphics/gradients.rb +0 -43
  95. data/manual/graphics/graphics.rb +0 -64
  96. data/manual/graphics/helper.rb +0 -34
  97. data/manual/graphics/line_width.rb +0 -36
  98. data/manual/graphics/lines_and_curves.rb +0 -40
  99. data/manual/graphics/polygon.rb +0 -27
  100. data/manual/graphics/rectangle.rb +0 -20
  101. data/manual/graphics/rotate.rb +0 -25
  102. data/manual/graphics/scale.rb +0 -42
  103. data/manual/graphics/soft_masks.rb +0 -44
  104. data/manual/graphics/stroke_cap.rb +0 -30
  105. data/manual/graphics/stroke_dash.rb +0 -47
  106. data/manual/graphics/stroke_join.rb +0 -29
  107. data/manual/graphics/translate.rb +0 -29
  108. data/manual/graphics/transparency.rb +0 -33
  109. data/manual/how_to_read_this_manual.rb +0 -39
  110. data/manual/images/absolute_position.rb +0 -22
  111. data/manual/images/fit.rb +0 -20
  112. data/manual/images/horizontal.rb +0 -24
  113. data/manual/images/images.rb +0 -41
  114. data/manual/images/plain_image.rb +0 -17
  115. data/manual/images/scale.rb +0 -21
  116. data/manual/images/vertical.rb +0 -30
  117. data/manual/images/width_and_height.rb +0 -24
  118. data/manual/layout/boxes.rb +0 -26
  119. data/manual/layout/content.rb +0 -24
  120. data/manual/layout/layout.rb +0 -27
  121. data/manual/layout/simple_grid.rb +0 -22
  122. data/manual/outline/add_subsection_to.rb +0 -60
  123. data/manual/outline/insert_section_after.rb +0 -46
  124. data/manual/outline/outline.rb +0 -33
  125. data/manual/outline/sections_and_pages.rb +0 -66
  126. data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
  127. data/manual/repeatable_content/page_numbering.rb +0 -55
  128. data/manual/repeatable_content/repeatable_content.rb +0 -35
  129. data/manual/repeatable_content/repeater.rb +0 -54
  130. data/manual/repeatable_content/stamp.rb +0 -40
  131. data/manual/security/encryption.rb +0 -28
  132. data/manual/security/permissions.rb +0 -43
  133. data/manual/security/security.rb +0 -28
  134. data/manual/table.rb +0 -16
  135. data/manual/text/alignment.rb +0 -43
  136. data/manual/text/color.rb +0 -24
  137. data/manual/text/column_box.rb +0 -30
  138. data/manual/text/fallback_fonts.rb +0 -41
  139. data/manual/text/font.rb +0 -40
  140. data/manual/text/font_size.rb +0 -44
  141. data/manual/text/font_style.rb +0 -25
  142. data/manual/text/formatted_callbacks.rb +0 -70
  143. data/manual/text/formatted_text.rb +0 -61
  144. data/manual/text/free_flowing_text.rb +0 -50
  145. data/manual/text/inline.rb +0 -40
  146. data/manual/text/kerning_and_character_spacing.rb +0 -38
  147. data/manual/text/leading.rb +0 -24
  148. data/manual/text/line_wrapping.rb +0 -60
  149. data/manual/text/paragraph_indentation.rb +0 -31
  150. data/manual/text/positioned_text.rb +0 -37
  151. data/manual/text/registering_families.rb +0 -51
  152. data/manual/text/rendering_and_color.rb +0 -36
  153. data/manual/text/right_to_left_text.rb +0 -54
  154. data/manual/text/rotation.rb +0 -52
  155. data/manual/text/single_usage.rb +0 -36
  156. data/manual/text/text.rb +0 -75
  157. data/manual/text/text_box_excess.rb +0 -35
  158. data/manual/text/text_box_extensions.rb +0 -48
  159. data/manual/text/text_box_overflow.rb +0 -51
  160. data/manual/text/utf8.rb +0 -27
  161. data/manual/text/win_ansi_charset.rb +0 -62
  162. data/prawn.gemspec +0 -51
  163. data/spec/data/curves.pdf +0 -66
  164. data/spec/extensions/encoding_helpers.rb +0 -11
  165. data/spec/prawn/document/bounding_box_spec.rb +0 -550
  166. data/spec/prawn/document/column_box_spec.rb +0 -75
  167. data/spec/prawn/document/security_spec.rb +0 -176
  168. data/spec/prawn/document_annotations_spec.rb +0 -76
  169. data/spec/prawn/document_destinations_spec.rb +0 -15
  170. data/spec/prawn/document_grid_spec.rb +0 -99
  171. data/spec/prawn/document_reference_spec.rb +0 -27
  172. data/spec/prawn/document_span_spec.rb +0 -44
  173. data/spec/prawn/document_spec.rb +0 -805
  174. data/spec/prawn/font_metric_cache_spec.rb +0 -54
  175. data/spec/prawn/font_spec.rb +0 -544
  176. data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
  177. data/spec/prawn/graphics/transparency_spec.rb +0 -81
  178. data/spec/prawn/graphics_spec.rb +0 -872
  179. data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
  180. data/spec/prawn/image_handler_spec.rb +0 -53
  181. data/spec/prawn/images/jpg_spec.rb +0 -20
  182. data/spec/prawn/images/png_spec.rb +0 -283
  183. data/spec/prawn/images_spec.rb +0 -229
  184. data/spec/prawn/measurements_extensions_spec.rb +0 -24
  185. data/spec/prawn/outline_spec.rb +0 -512
  186. data/spec/prawn/repeater_spec.rb +0 -166
  187. data/spec/prawn/soft_mask_spec.rb +0 -74
  188. data/spec/prawn/stamp_spec.rb +0 -173
  189. data/spec/prawn/text/box_spec.rb +0 -1110
  190. data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
  191. data/spec/prawn/text/formatted/box_spec.rb +0 -849
  192. data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
  193. data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -495
  194. data/spec/prawn/text/formatted/parser_spec.rb +0 -697
  195. data/spec/prawn/text_draw_text_spec.rb +0 -150
  196. data/spec/prawn/text_rendering_mode_spec.rb +0 -48
  197. data/spec/prawn/text_spacing_spec.rb +0 -95
  198. data/spec/prawn/text_spec.rb +0 -603
  199. data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
  200. data/spec/prawn/transformation_stack_spec.rb +0 -66
  201. data/spec/prawn/view_spec.rb +0 -63
  202. data/spec/prawn_manual_spec.rb +0 -35
  203. data/spec/spec_helper.rb +0 -48
@@ -1,512 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Prawn::Outline do
6
- let(:pdf) do
7
- Prawn::Document.new do
8
- text 'Page 1. This is the first Chapter. '
9
- start_new_page
10
- text 'Page 2. More in the first Chapter. '
11
- start_new_page
12
- outline.define do
13
- section 'Chapter 1', destination: 1, closed: true do
14
- page destination: 1, title: 'Page 1'
15
- page destination: 2, title: 'Page 2'
16
- end
17
- end
18
- end
19
- end
20
-
21
- let(:hash) do
22
- output = StringIO.new(pdf.render, 'r+')
23
- PDF::Reader::ObjectHash.new(output)
24
- end
25
- let(:outline_root) do
26
- hash.values.find do |obj|
27
- obj.is_a?(Hash) && obj[:Type] == :Outlines
28
- end
29
- end
30
-
31
- describe 'outline encoding' do
32
- it 'stores all outline titles as UTF-16' do
33
- hash.each_value do |obj|
34
- next unless obj.is_a?(Hash) && obj[:Title]
35
-
36
- title = obj[:Title].dup
37
- title.force_encoding(Encoding::UTF_16LE)
38
- expect(title.valid_encoding?).to eq(true)
39
- end
40
- end
41
- end
42
-
43
- describe '#generate_outline' do
44
- it 'creates a root outline dictionary item' do
45
- expect(outline_root).to_not be_nil
46
- end
47
-
48
- it 'sets the first and last top items of the root outline dictionary '\
49
- 'item' do
50
- section1 = find_by_title('Chapter 1')
51
-
52
- expect(referenced_object(outline_root[:First])).to eq(section1)
53
- expect(referenced_object(outline_root[:Last])).to eq(section1)
54
- end
55
-
56
- describe '#create_outline_item' do
57
- it 'creates outline items for each section and page' do
58
- section1 = find_by_title('Chapter 1')
59
- page1 = find_by_title('Page 1')
60
- page2 = find_by_title('Page 2')
61
-
62
- expect(section1).to_not be_nil
63
- expect(page1).to_not be_nil
64
- expect(page2).to_not be_nil
65
- end
66
- end
67
-
68
- describe '#set_relations, #set_variables_for_block, and #reset_parent' do
69
- it 'links sibling items' do
70
- page1 = find_by_title('Page 1')
71
- page2 = find_by_title('Page 2')
72
-
73
- expect(referenced_object(page1[:Next])).to eq(page2)
74
- expect(referenced_object(page2[:Prev])).to eq(page1)
75
- end
76
-
77
- it 'links child items to parent item' do
78
- section1 = find_by_title('Chapter 1')
79
- page1 = find_by_title('Page 1')
80
- page2 = find_by_title('Page 2')
81
-
82
- expect(referenced_object(page1[:Parent])).to eq(section1)
83
- expect(referenced_object(page2[:Parent])).to eq(section1)
84
- end
85
-
86
- it 'sets the first and last child items for parent item' do
87
- section1 = find_by_title('Chapter 1')
88
- page1 = find_by_title('Page 1')
89
- page2 = find_by_title('Page 2')
90
-
91
- expect(referenced_object(section1[:First])).to eq(page1)
92
- expect(referenced_object(section1[:Last])).to eq(page2)
93
- end
94
- end
95
-
96
- describe '#increase_count' do
97
- it 'adds the count of all descendant items' do
98
- section1 = find_by_title('Chapter 1')
99
- page1 = find_by_title('Page 1')
100
- page2 = find_by_title('Page 2')
101
-
102
- expect(outline_root[:Count]).to eq(3)
103
- expect(section1[:Count].abs).to eq(2)
104
- expect(page1[:Count]).to eq(0)
105
- expect(page2[:Count]).to eq(0)
106
- end
107
- end
108
-
109
- describe 'closed option' do
110
- it "sets the item's integer count to negative" do
111
- section1 = find_by_title('Chapter 1')
112
-
113
- expect(section1[:Count]).to eq(-2)
114
- end
115
- end
116
- end
117
-
118
- describe 'adding a custom destination' do
119
- before do
120
- pdf.start_new_page
121
- pdf.text 'Page 3 with a destination'
122
- pdf.add_dest('customdest', pdf.dest_xyz(200, 200))
123
- destination = pdf.dest_xyz(200, 200)
124
- pdf.outline.update do
125
- page destination: destination, title: 'Custom Destination'
126
- end
127
- end
128
-
129
- it 'creates an outline item' do
130
- custom_dest = find_by_title('Custom Destination')
131
-
132
- expect(custom_dest).to_not be_nil
133
- end
134
-
135
- it 'references the custom destination' do
136
- custom_dest = find_by_title('Custom Destination')
137
- last_page =
138
- hash.values.find do |obj|
139
- obj.is_a?(Hash) && obj[:Type] == :Pages
140
- end[:Kids]
141
- .last
142
-
143
- expect(referenced_object(custom_dest[:Dest].first))
144
- .to eq(referenced_object(last_page))
145
- end
146
- end
147
-
148
- describe 'addding a section later with outline#section' do
149
- before do
150
- pdf.start_new_page
151
- pdf.text 'Page 3. An added section '
152
- pdf.outline.update do
153
- section 'Added Section', destination: 3 do
154
- page destination: 3, title: 'Page 3'
155
- end
156
- end
157
- end
158
-
159
- it 'adds new outline items to document' do
160
- section2 = find_by_title('Added Section')
161
- page3 = find_by_title('Page 3')
162
-
163
- expect(section2).to_not be_nil
164
- expect(page3).to_not be_nil
165
- end
166
-
167
- it 'resets the last items for root outline dictionary' do
168
- section1 = find_by_title('Chapter 1')
169
- section2 = find_by_title('Added Section')
170
-
171
- expect(referenced_object(outline_root[:First])).to eq(section1)
172
- expect(referenced_object(outline_root[:Last])).to eq(section2)
173
- end
174
-
175
- it 'resets the next relation for the previous last top level item' do
176
- section1 = find_by_title('Chapter 1')
177
- section2 = find_by_title('Added Section')
178
-
179
- expect(referenced_object(section1[:Next])).to eq(section2)
180
- end
181
-
182
- it 'sets the previous relation of the addded to section' do
183
- section1 = find_by_title('Chapter 1')
184
- section2 = find_by_title('Added Section')
185
-
186
- expect(referenced_object(section2[:Prev])).to eq(section1)
187
- end
188
-
189
- it 'increases the count of root outline dictionary' do
190
- expect(outline_root[:Count]).to eq(5)
191
- end
192
- end
193
-
194
- describe '#outline.add_subsection_to' do
195
- context 'when positioned last' do
196
- before do
197
- pdf.start_new_page
198
- pdf.text 'Page 3. An added subsection '
199
- pdf.outline.update do
200
- add_subsection_to 'Chapter 1' do
201
- section 'Added SubSection', destination: 3 do
202
- page destination: 3, title: 'Added Page 3'
203
- end
204
- end
205
- end
206
- end
207
-
208
- it 'adds new outline items to document' do
209
- subsection = find_by_title('Added SubSection')
210
- added_page = find_by_title('Added Page 3')
211
-
212
- expect(subsection).to_not be_nil
213
- expect(added_page).to_not be_nil
214
- end
215
-
216
- it 'resets the last item for parent item dictionary' do
217
- section1 = find_by_title('Chapter 1')
218
- page1 = find_by_title('Page 1')
219
- subsection = find_by_title('Added SubSection')
220
-
221
- expect(referenced_object(section1[:First])).to eq(page1)
222
- expect(referenced_object(section1[:Last])).to eq(subsection)
223
- end
224
-
225
- it "sets the prev relation for the new subsection to its parent's old "\
226
- 'last item' do
227
- subsection = find_by_title('Added SubSection')
228
- page2 = find_by_title('Page 2')
229
-
230
- expect(referenced_object(subsection[:Prev])).to eq(page2)
231
- end
232
-
233
- it "the subsection should become the next relation for its parent's old "\
234
- 'last item' do
235
- subsection = find_by_title('Added SubSection')
236
- page2 = find_by_title('Page 2')
237
-
238
- expect(referenced_object(page2[:Next])).to eq(subsection)
239
- end
240
-
241
- it 'sets the first relation for the new subsection' do
242
- subsection = find_by_title('Added SubSection')
243
- added_page = find_by_title('Added Page 3')
244
-
245
- expect(referenced_object(subsection[:First])).to eq(added_page)
246
- end
247
-
248
- it 'sets the correct last relation of the added to section' do
249
- subsection = find_by_title('Added SubSection')
250
- added_page = find_by_title('Added Page 3')
251
-
252
- expect(referenced_object(subsection[:Last])).to eq(added_page)
253
- end
254
-
255
- it 'increases the count of root outline dictionary' do
256
- expect(outline_root[:Count]).to eq(5)
257
- end
258
- end
259
-
260
- context 'when positioned first' do
261
- before do
262
- pdf.start_new_page
263
- pdf.text 'Page 3. An added subsection '
264
- pdf.outline.update do
265
- add_subsection_to 'Chapter 1', :first do
266
- section 'Added SubSection', destination: 3 do
267
- page destination: 3, title: 'Added Page 3'
268
- end
269
- end
270
- end
271
- end
272
-
273
- it 'adds new outline items to document' do
274
- subsection = find_by_title('Added SubSection')
275
- added_page = find_by_title('Added Page 3')
276
-
277
- expect(subsection).to_not be_nil
278
- expect(added_page).to_not be_nil
279
- end
280
-
281
- it 'resets the first item for parent item dictionary' do
282
- section1 = find_by_title('Chapter 1')
283
- subsection = find_by_title('Added SubSection')
284
- page2 = find_by_title('Page 2')
285
-
286
- expect(referenced_object(section1[:First])).to eq(subsection)
287
- expect(referenced_object(section1[:Last])).to eq(page2)
288
- end
289
-
290
- it "sets the next relation for the new subsection to its parent's old "\
291
- 'first item' do
292
- subsection = find_by_title('Added SubSection')
293
- page1 = find_by_title('Page 1')
294
-
295
- expect(referenced_object(subsection[:Next])).to eq(page1)
296
- end
297
-
298
- it "the subsection should become the prev relation for its parent's old "\
299
- 'first item' do
300
- subsection = find_by_title('Added SubSection')
301
- page1 = find_by_title('Page 1')
302
-
303
- expect(referenced_object(page1[:Prev])).to eq(subsection)
304
- end
305
-
306
- it 'sets the first relation for the new subsection' do
307
- subsection = find_by_title('Added SubSection')
308
- added_page = find_by_title('Added Page 3')
309
-
310
- expect(referenced_object(subsection[:First])).to eq(added_page)
311
- end
312
-
313
- it 'sets the correct last relation of the added to section' do
314
- subsection = find_by_title('Added SubSection')
315
- added_page = find_by_title('Added Page 3')
316
-
317
- expect(referenced_object(subsection[:Last])).to eq(added_page)
318
- end
319
-
320
- it 'increases the count of root outline dictionary' do
321
- expect(outline_root[:Count]).to eq(5)
322
- end
323
- end
324
-
325
- it 'requires an existing title' do
326
- expect do
327
- pdf.go_to_page 1
328
- pdf.start_new_page
329
- pdf.text 'Inserted Page'
330
- pdf.outline.update do
331
- add_subsection_to 'Wrong page' do
332
- page page_number, title: 'Inserted Page'
333
- end
334
- end
335
- render_and_find_objects
336
- end.to raise_error(Prawn::Errors::UnknownOutlineTitle)
337
- end
338
- end
339
-
340
- describe '#outline.insert_section_after' do
341
- describe 'inserting in the middle of another section' do
342
- before do
343
- pdf.go_to_page 1
344
- pdf.start_new_page
345
- pdf.text 'Inserted Page'
346
- pdf.outline.update do
347
- insert_section_after 'Page 1' do
348
- page destination: page_number, title: 'Inserted Page'
349
- end
350
- end
351
- end
352
-
353
- it 'inserts new outline items to document' do
354
- inserted_page = find_by_title('Inserted Page')
355
-
356
- expect(inserted_page).to_not be_nil
357
- end
358
-
359
- it 'adjusts the count of all ancestors' do
360
- section1 = find_by_title('Chapter 1')
361
-
362
- expect(outline_root[:Count]).to eq(4)
363
- expect(section1[:Count].abs).to eq(3)
364
- end
365
-
366
- describe '#adjust_relations' do
367
- it 'resets the sibling relations of adjoining items to inserted item' do
368
- inserted_page = find_by_title('Inserted Page')
369
- page1 = find_by_title('Page 1')
370
- page2 = find_by_title('Page 2')
371
-
372
- expect(referenced_object(page1[:Next])).to eq(inserted_page)
373
- expect(referenced_object(page2[:Prev])).to eq(inserted_page)
374
- end
375
-
376
- it 'sets the sibling relation of added item to adjoining items' do
377
- inserted_page = find_by_title('Inserted Page')
378
- page1 = find_by_title('Page 1')
379
- page2 = find_by_title('Page 2')
380
-
381
- expect(referenced_object(inserted_page[:Next])).to eq(page2)
382
- expect(referenced_object(inserted_page[:Prev])).to eq(page1)
383
- end
384
-
385
- it 'does not affect the first and last relations of parent item' do
386
- section1 = find_by_title('Chapter 1')
387
- page1 = find_by_title('Page 1')
388
- page2 = find_by_title('Page 2')
389
-
390
- expect(referenced_object(section1[:First])).to eq(page1)
391
- expect(referenced_object(section1[:Last])).to eq(page2)
392
- end
393
- end
394
-
395
- context 'when adding another section afterwards' do
396
- it 'has reset the root position so that a new section is added at '\
397
- 'the end of root sections' do
398
- pdf.start_new_page
399
- pdf.text 'Another Inserted Page'
400
- pdf.outline.update do
401
- section 'Added Section' do
402
- page destination: page_number, title: 'Inserted Page'
403
- end
404
- end
405
-
406
- section1 = find_by_title('Chapter 1')
407
- section2 = find_by_title('Added Section')
408
-
409
- expect(referenced_object(outline_root[:Last])).to eq(section2)
410
- expect(referenced_object(section1[:Next])).to eq(section2)
411
- end
412
- end
413
- end
414
-
415
- describe 'inserting at the end of another section' do
416
- before do
417
- pdf.go_to_page 2
418
- pdf.start_new_page
419
- pdf.text 'Inserted Page'
420
- pdf.outline.update do
421
- insert_section_after 'Page 2' do
422
- page destination: page_number, title: 'Inserted Page'
423
- end
424
- end
425
- end
426
-
427
- describe '#adjust_relations' do
428
- it 'resets the sibling relations of adjoining item to inserted item' do
429
- page2 = find_by_title('Page 2')
430
- inserted_page = find_by_title('Inserted Page')
431
-
432
- expect(referenced_object(page2[:Next])).to eq(inserted_page)
433
- end
434
-
435
- it 'sets the sibling relation of added item to adjoining items' do
436
- page2 = find_by_title('Page 2')
437
- inserted_page = find_by_title('Inserted Page')
438
-
439
- expect(referenced_object(inserted_page[:Next])).to be_nil
440
- expect(referenced_object(inserted_page[:Prev])).to eq(page2)
441
- end
442
-
443
- it 'adjusts the last relation of parent item' do
444
- section1 = find_by_title('Chapter 1')
445
- inserted_page = find_by_title('Inserted Page')
446
-
447
- expect(referenced_object(section1[:Last])).to eq(inserted_page)
448
- end
449
- end
450
- end
451
-
452
- it 'requires an existing title' do
453
- expect do
454
- pdf.go_to_page 1
455
- pdf.start_new_page
456
- pdf.text 'Inserted Page'
457
- pdf.outline.update do
458
- insert_section_after 'Wrong page' do
459
- page destination: page_number, title: 'Inserted Page'
460
- end
461
- end
462
- end.to raise_error(Prawn::Errors::UnknownOutlineTitle)
463
- end
464
- end
465
-
466
- describe '#page' do
467
- it 'requires a title option to be set' do
468
- expect do
469
- Prawn::Document.new do
470
- text 'Page 1. This is the first Chapter. '
471
- outline.define do
472
- page destination: 1, title: nil
473
- end
474
- end
475
- end.to raise_error(Prawn::Errors::RequiredOption)
476
- end
477
- end
478
-
479
- describe 'foreign character encoding' do
480
- let(:hash) do
481
- pdf =
482
- Prawn::Document.new do
483
- outline.define do
484
- section 'La pomme croquée', destination: 1, closed: true
485
- end
486
- end
487
- PDF::Reader::ObjectHash.new(StringIO.new(pdf.render, 'r+'))
488
- end
489
-
490
- it 'handles other encodings for the title' do
491
- object = find_by_title('La pomme croquée')
492
- expect(object).to_not be_nil
493
- end
494
- end
495
-
496
- # Outline titles are stored as UTF-16. This method accepts a UTF-8 outline
497
- # title and returns the PDF Object that contains an outline with that name
498
- def find_by_title(title)
499
- hash.values.find do |obj|
500
- next unless obj.is_a?(Hash) && obj[:Title]
501
-
502
- title_codepoints = obj[:Title].unpack('n*')
503
- title_codepoints.shift
504
- utf8_title = title_codepoints.pack('U*')
505
- utf8_title == title ? obj : nil
506
- end
507
- end
508
-
509
- def referenced_object(reference)
510
- hash[reference]
511
- end
512
- end
@@ -1,166 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Prawn::Repeater do
6
- it 'creates a stamp and increments Prawn::Repeater.count on initialize' do
7
- orig_count = described_class.count
8
-
9
- doc = sample_document
10
- allow(doc).to receive(:create_stamp).with("prawn_repeater(#{orig_count})")
11
-
12
- repeater(doc, :all) { :do_nothing }
13
-
14
- expect(doc).to have_received(:create_stamp)
15
- .with("prawn_repeater(#{orig_count})")
16
-
17
- expect(described_class.count).to eq(orig_count + 1)
18
- end
19
-
20
- it 'must provide an :all filter' do
21
- doc = sample_document
22
- r = repeater(doc, :all) { :do_nothing }
23
-
24
- expect((1..doc.page_count).all? { |i| r.match?(i) }).to eq true
25
- end
26
-
27
- it 'must provide an :odd filter' do
28
- doc = sample_document
29
- r = repeater(doc, :odd) { :do_nothing }
30
-
31
- odd, even = (1..doc.page_count).partition(&:odd?)
32
-
33
- expect(odd.all? { |i| r.match?(i) }).to eq true
34
- expect(even.any? { |i| r.match?(i) }).to eq false
35
- end
36
-
37
- it 'must be able to filter by an array of page numbers' do
38
- doc = sample_document
39
- r = repeater(doc, [1, 2, 7]) { :do_nothing }
40
-
41
- expect((1..10).select { |i| r.match?(i) }).to eq([1, 2, 7])
42
- end
43
-
44
- it 'must be able to filter by a range of page numbers' do
45
- doc = sample_document
46
- r = repeater(doc, 2..4) { :do_nothing }
47
-
48
- expect((1..10).select { |i| r.match?(i) }).to eq([2, 3, 4])
49
- end
50
-
51
- it 'must be able to filter by an arbitrary proc' do
52
- doc = sample_document
53
- r = repeater(doc, ->(x) { x == 1 || x % 3 == 0 })
54
-
55
- expect((1..10).select { |i| r.match?(i) }).to eq([1, 3, 6, 9])
56
- end
57
-
58
- it 'must try to run a stamp if the page number matches' do
59
- doc = sample_document
60
- allow(doc).to receive(:stamp)
61
-
62
- repeater(doc, :odd).run(3)
63
- expect(doc).to have_received(:stamp)
64
- end
65
-
66
- it 'must not try to run a stamp unless the page number matches' do
67
- doc = sample_document
68
-
69
- allow(doc).to receive(:stamp)
70
- repeater(doc, :odd).run(2)
71
- expect(doc).to_not have_received(:stamp)
72
- end
73
-
74
- it 'must not try to run a stamp if dynamic is selected' do
75
- doc = sample_document
76
-
77
- allow(doc).to receive(:stamp)
78
- (1..10).each { |p| repeater(doc, :all, true) { :do_nothing }.run(p) }
79
- expect(doc).to_not have_received(:stamp)
80
- end
81
-
82
- it 'must try to run a block if the page number matches' do
83
- doc = sample_document
84
-
85
- allow(doc).to receive(:draw_text)
86
- (1..10).each do |p|
87
- repeater(doc, [1, 2], true) { doc.draw_text 'foo' }.run(p)
88
- end
89
- expect(doc).to have_received(:draw_text).twice
90
- end
91
-
92
- it 'must not try to run a block unless the page number matches' do
93
- doc = sample_document
94
-
95
- allow(doc).to receive(:draw_text)
96
- repeater(doc, :odd, true) { doc.draw_text 'foo' }.run(2)
97
- expect(doc).to_not have_received(:draw_text)
98
- end
99
-
100
- it 'must treat any block as a closure' do
101
- doc = sample_document
102
-
103
- page = 'Page' # ensure access to ivars
104
- doc.repeat(:all, dynamic: true) do
105
- doc.draw_text "#{page} #{doc.page_number}", at: [500, 0]
106
- end
107
-
108
- text = PDF::Inspector::Text.analyze(doc.render)
109
- expect(text.strings).to eq((1..10).to_a.map { |p| "Page #{p}" })
110
- end
111
-
112
- it 'must treat any block as a closure (Document.new instance_eval form)' do
113
- doc =
114
- Prawn::Document.new(skip_page_creation: true) do
115
- 10.times { start_new_page }
116
-
117
- page = 'Page'
118
- repeat(:all, dynamic: true) do
119
- # ensure self is accessible here
120
- draw_text "#{page} #{page_number}", at: [500, 0]
121
- end
122
- end
123
-
124
- text = PDF::Inspector::Text.analyze(doc.render)
125
- expect(text.strings).to eq((1..10).to_a.map { |p| "Page #{p}" })
126
- end
127
-
128
- def sample_document
129
- doc = Prawn::Document.new(skip_page_creation: true)
130
- 10.times { |_e| doc.start_new_page }
131
- doc
132
- end
133
-
134
- def repeater(*args, &block)
135
- Prawn::Repeater.new(*args, &block)
136
- end
137
-
138
- describe 'graphic state' do
139
- let(:pdf) { create_pdf }
140
-
141
- it 'does not alter the graphic state stack color space' do
142
- starting_color_space = pdf.state.page.graphic_state.color_space.dup
143
- pdf.repeat :all do
144
- pdf.text 'Testing', size: 24, style: :bold
145
- end
146
- expect(pdf.state.page.graphic_state.color_space)
147
- .to eq(starting_color_space)
148
- end
149
-
150
- context 'with dynamic repeaters' do
151
- it 'preserves the graphic state at creation time' do
152
- pdf.repeat :all, dynamic: true do
153
- pdf.text "fill_color: #{pdf.graphic_state.fill_color}"
154
- pdf.text "cap_style: #{pdf.graphic_state.cap_style}"
155
- end
156
- pdf.fill_color '666666'
157
- pdf.cap_style :round
158
- text = PDF::Inspector::Text.analyze(pdf.render)
159
- expect(text.strings.include?('fill_color: 666666')).to eq(false)
160
- expect(text.strings.include?('fill_color: 000000')).to eq(true)
161
- expect(text.strings.include?('cap_style: round')).to eq(false)
162
- expect(text.strings.include?('cap_style: butt')).to eq(true)
163
- end
164
- end
165
- end
166
- end