prawn 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +2 -31
  5. data/lib/prawn.rb +1 -0
  6. data/lib/prawn/document.rb +20 -15
  7. data/lib/prawn/document/bounding_box.rb +10 -2
  8. data/lib/prawn/document/span.rb +2 -1
  9. data/lib/prawn/font.rb +6 -4
  10. data/lib/prawn/font_metric_cache.rb +7 -6
  11. data/lib/prawn/fonts/afm.rb +18 -16
  12. data/lib/prawn/fonts/ttf.rb +55 -29
  13. data/lib/prawn/graphics.rb +11 -11
  14. data/lib/prawn/graphics/color.rb +18 -16
  15. data/lib/prawn/graphics/join_style.rb +1 -1
  16. data/lib/prawn/graphics/patterns.rb +57 -49
  17. data/lib/prawn/graphics/transformation.rb +3 -3
  18. data/lib/prawn/grid.rb +36 -1
  19. data/lib/prawn/images.rb +29 -27
  20. data/lib/prawn/images/jpg.rb +4 -1
  21. data/lib/prawn/images/png.rb +2 -1
  22. data/lib/prawn/outline.rb +6 -5
  23. data/lib/prawn/repeater.rb +1 -1
  24. data/lib/prawn/security.rb +39 -36
  25. data/lib/prawn/text.rb +18 -18
  26. data/lib/prawn/text/box.rb +10 -9
  27. data/lib/prawn/text/formatted/arranger.rb +38 -19
  28. data/lib/prawn/text/formatted/box.rb +20 -15
  29. data/lib/prawn/text/formatted/line_wrap.rb +18 -16
  30. data/lib/prawn/text/formatted/parser.rb +28 -26
  31. data/lib/prawn/text/formatted/wrap.rb +15 -11
  32. data/lib/prawn/version.rb +1 -1
  33. data/lib/prawn/view.rb +2 -2
  34. data/manual/basic_concepts/measurement.rb +1 -1
  35. data/manual/bounding_box/canvas.rb +3 -3
  36. data/manual/bounding_box/nesting.rb +1 -1
  37. data/manual/document_and_page_options/background.rb +6 -2
  38. data/manual/document_and_page_options/document_and_page_options.rb +3 -3
  39. data/manual/document_and_page_options/print_scaling.rb +2 -1
  40. data/manual/graphics/common_lines.rb +1 -1
  41. data/manual/graphics/fill_and_stroke.rb +1 -1
  42. data/manual/graphics/fill_rules.rb +4 -3
  43. data/manual/graphics/helper.rb +11 -4
  44. data/manual/graphics/lines_and_curves.rb +1 -1
  45. data/manual/graphics/stroke_dash.rb +5 -5
  46. data/manual/graphics/translate.rb +2 -1
  47. data/manual/images/horizontal.rb +2 -2
  48. data/manual/images/scale.rb +3 -3
  49. data/manual/images/vertical.rb +6 -3
  50. data/manual/images/width_and_height.rb +3 -3
  51. data/manual/layout/content.rb +2 -2
  52. data/manual/outline/outline.rb +1 -1
  53. data/manual/security/permissions.rb +4 -2
  54. data/manual/security/security.rb +1 -1
  55. data/manual/text/alignment.rb +2 -2
  56. data/manual/text/column_box.rb +2 -2
  57. data/manual/text/font_style.rb +5 -2
  58. data/manual/text/formatted_callbacks.rb +17 -12
  59. data/manual/text/formatted_text.rb +7 -4
  60. data/manual/text/free_flowing_text.rb +3 -3
  61. data/manual/text/kerning_and_character_spacing.rb +4 -4
  62. data/manual/text/paragraph_indentation.rb +4 -5
  63. data/manual/text/rendering_and_color.rb +1 -1
  64. data/manual/text/right_to_left_text.rb +6 -6
  65. data/manual/text/rotation.rb +8 -3
  66. data/manual/text/text_box_extensions.rb +1 -1
  67. data/manual/text/text_box_overflow.rb +11 -9
  68. data/manual/text/win_ansi_charset.rb +9 -9
  69. data/prawn.gemspec +4 -10
  70. data/spec/prawn/document/bounding_box_spec.rb +82 -78
  71. data/spec/prawn/document/security_spec.rb +1 -1
  72. data/spec/prawn/document_span_spec.rb +14 -6
  73. data/spec/prawn/document_spec.rb +29 -26
  74. data/spec/prawn/font_spec.rb +16 -14
  75. data/spec/prawn/graphics_spec.rb +79 -44
  76. data/spec/prawn/images_spec.rb +13 -8
  77. data/spec/prawn/outline_spec.rb +127 -27
  78. data/spec/prawn/repeater_spec.rb +9 -8
  79. data/spec/prawn/soft_mask_spec.rb +1 -1
  80. data/spec/prawn/stamp_spec.rb +3 -2
  81. data/spec/prawn/text/box_spec.rb +57 -59
  82. data/spec/prawn/text/formatted/arranger_spec.rb +10 -10
  83. data/spec/prawn/text/formatted/box_spec.rb +8 -5
  84. data/spec/prawn/text/formatted/line_wrap_spec.rb +2 -1
  85. data/spec/prawn/text_draw_text_spec.rb +9 -8
  86. data/spec/prawn/text_spacing_spec.rb +2 -2
  87. data/spec/prawn/text_spec.rb +9 -9
  88. data/spec/prawn/text_with_inline_formatting_spec.rb +1 -1
  89. data/spec/prawn_manual_spec.rb +4 -4
  90. metadata +14 -98
  91. metadata.gz.sig +0 -0
@@ -29,25 +29,30 @@ describe Prawn::Images do
29
29
  end
30
30
 
31
31
  it 'accepts IO objects' do
32
- file = File.open(filename, 'rb')
33
- info = pdf.image(file)
32
+ info =
33
+ File.open(filename, 'rb') do |file|
34
+ pdf.image(file)
35
+ end
34
36
 
35
37
  expect(info.height).to eq(453)
36
38
  end
37
39
 
38
40
  it 'rewinds IO objects to be able to embed them multiply' do
39
- file = File.open(filename, 'rb')
41
+ info =
42
+ File.open(filename, 'rb') do |file|
43
+ pdf.image(file)
44
+ pdf.image(file)
45
+ end
40
46
 
41
- pdf.image(file)
42
- info = pdf.image(file)
43
47
  expect(info.height).to eq(453)
44
48
  end
45
49
 
46
50
  it 'does not close passed-in IO objects' do
47
- file = File.open(filename, 'rb')
48
- _info = pdf.image(file)
51
+ File.open(filename, 'rb') do |file|
52
+ pdf.image(file)
49
53
 
50
- expect(file).to_not be_closed
54
+ expect(file).to_not be_closed
55
+ end
51
56
  end
52
57
 
53
58
  it 'accepts Pathname objects' do
@@ -18,27 +18,15 @@ describe Prawn::Outline do
18
18
  end
19
19
  end
20
20
 
21
- let(:output) { StringIO.new(pdf.render, 'r+') }
22
- let(:hash) { PDF::Reader::ObjectHash.new(output) }
21
+ let(:hash) do
22
+ output = StringIO.new(pdf.render, 'r+')
23
+ PDF::Reader::ObjectHash.new(output)
24
+ end
23
25
  let(:outline_root) do
24
26
  hash.values.find do |obj|
25
27
  obj.is_a?(Hash) && obj[:Type] == :Outlines
26
28
  end
27
29
  end
28
- let(:pages) do
29
- hash.values.find do |obj|
30
- obj.is_a?(Hash) && obj[:Type] == :Pages
31
- end[:Kids]
32
- end
33
- let(:section1) { find_by_title('Chapter 1') }
34
- let(:page1) { find_by_title('Page 1') }
35
- let(:page2) { find_by_title('Page 2') }
36
- let(:section2) { find_by_title('Added Section') }
37
- let(:page3) { find_by_title('Page 3') }
38
- let(:inserted_page) { find_by_title('Inserted Page') }
39
- let(:subsection) { find_by_title('Added SubSection') }
40
- let(:added_page3) { find_by_title('Added Page 3') }
41
- let(:custom_dest) { find_by_title('Custom Destination') }
42
30
 
43
31
  describe 'outline encoding' do
44
32
  it 'stores all outline titles as UTF-16' do
@@ -59,12 +47,18 @@ describe Prawn::Outline do
59
47
 
60
48
  it 'sets the first and last top items of the root outline dictionary '\
61
49
  'item' do
50
+ section1 = find_by_title('Chapter 1')
51
+
62
52
  expect(referenced_object(outline_root[:First])).to eq(section1)
63
53
  expect(referenced_object(outline_root[:Last])).to eq(section1)
64
54
  end
65
55
 
66
56
  describe '#create_outline_item' do
67
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
+
68
62
  expect(section1).to_not be_nil
69
63
  expect(page1).to_not be_nil
70
64
  expect(page2).to_not be_nil
@@ -73,16 +67,27 @@ describe Prawn::Outline do
73
67
 
74
68
  describe '#set_relations, #set_variables_for_block, and #reset_parent' do
75
69
  it 'links sibling items' do
70
+ page1 = find_by_title('Page 1')
71
+ page2 = find_by_title('Page 2')
72
+
76
73
  expect(referenced_object(page1[:Next])).to eq(page2)
77
74
  expect(referenced_object(page2[:Prev])).to eq(page1)
78
75
  end
79
76
 
80
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
+
81
82
  expect(referenced_object(page1[:Parent])).to eq(section1)
82
83
  expect(referenced_object(page2[:Parent])).to eq(section1)
83
84
  end
84
85
 
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
+
86
91
  expect(referenced_object(section1[:First])).to eq(page1)
87
92
  expect(referenced_object(section1[:Last])).to eq(page2)
88
93
  end
@@ -90,6 +95,10 @@ describe Prawn::Outline do
90
95
 
91
96
  describe '#increase_count' do
92
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
+
93
102
  expect(outline_root[:Count]).to eq(3)
94
103
  expect(section1[:Count].abs).to eq(2)
95
104
  expect(page1[:Count]).to eq(0)
@@ -99,6 +108,8 @@ describe Prawn::Outline do
99
108
 
100
109
  describe 'closed option' do
101
110
  it "sets the item's integer count to negative" do
111
+ section1 = find_by_title('Chapter 1')
112
+
102
113
  expect(section1[:Count]).to eq(-2)
103
114
  end
104
115
  end
@@ -116,12 +127,21 @@ describe Prawn::Outline do
116
127
  end
117
128
 
118
129
  it 'creates an outline item' do
130
+ custom_dest = find_by_title('Custom Destination')
131
+
119
132
  expect(custom_dest).to_not be_nil
120
133
  end
121
134
 
122
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
+
123
143
  expect(referenced_object(custom_dest[:Dest].first))
124
- .to eq(referenced_object(pages.last))
144
+ .to eq(referenced_object(last_page))
125
145
  end
126
146
  end
127
147
 
@@ -137,20 +157,32 @@ describe Prawn::Outline do
137
157
  end
138
158
 
139
159
  it 'adds new outline items to document' do
160
+ section2 = find_by_title('Added Section')
161
+ page3 = find_by_title('Page 3')
162
+
140
163
  expect(section2).to_not be_nil
141
164
  expect(page3).to_not be_nil
142
165
  end
143
166
 
144
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
+
145
171
  expect(referenced_object(outline_root[:First])).to eq(section1)
146
172
  expect(referenced_object(outline_root[:Last])).to eq(section2)
147
173
  end
148
174
 
149
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
+
150
179
  expect(referenced_object(section1[:Next])).to eq(section2)
151
180
  end
152
181
 
153
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
+
154
186
  expect(referenced_object(section2[:Prev])).to eq(section1)
155
187
  end
156
188
 
@@ -174,31 +206,50 @@ describe Prawn::Outline do
174
206
  end
175
207
 
176
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
+
177
212
  expect(subsection).to_not be_nil
178
- expect(added_page3).to_not be_nil
213
+ expect(added_page).to_not be_nil
179
214
  end
180
215
 
181
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
+
182
221
  expect(referenced_object(section1[:First])).to eq(page1)
183
222
  expect(referenced_object(section1[:Last])).to eq(subsection)
184
223
  end
185
224
 
186
225
  it "sets the prev relation for the new subsection to its parent's old "\
187
226
  'last item' do
227
+ subsection = find_by_title('Added SubSection')
228
+ page2 = find_by_title('Page 2')
229
+
188
230
  expect(referenced_object(subsection[:Prev])).to eq(page2)
189
231
  end
190
232
 
191
233
  it "the subsection should become the next relation for its parent's old "\
192
234
  'last item' do
235
+ subsection = find_by_title('Added SubSection')
236
+ page2 = find_by_title('Page 2')
237
+
193
238
  expect(referenced_object(page2[:Next])).to eq(subsection)
194
239
  end
195
240
 
196
241
  it 'sets the first relation for the new subsection' do
197
- expect(referenced_object(subsection[:First])).to eq(added_page3)
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)
198
246
  end
199
247
 
200
248
  it 'sets the correct last relation of the added to section' do
201
- expect(referenced_object(subsection[:Last])).to eq(added_page3)
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)
202
253
  end
203
254
 
204
255
  it 'increases the count of root outline dictionary' do
@@ -220,31 +271,50 @@ describe Prawn::Outline do
220
271
  end
221
272
 
222
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
+
223
277
  expect(subsection).to_not be_nil
224
- expect(added_page3).to_not be_nil
278
+ expect(added_page).to_not be_nil
225
279
  end
226
280
 
227
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
+
228
286
  expect(referenced_object(section1[:First])).to eq(subsection)
229
287
  expect(referenced_object(section1[:Last])).to eq(page2)
230
288
  end
231
289
 
232
290
  it "sets the next relation for the new subsection to its parent's old "\
233
291
  'first item' do
292
+ subsection = find_by_title('Added SubSection')
293
+ page1 = find_by_title('Page 1')
294
+
234
295
  expect(referenced_object(subsection[:Next])).to eq(page1)
235
296
  end
236
297
 
237
298
  it "the subsection should become the prev relation for its parent's old "\
238
299
  'first item' do
300
+ subsection = find_by_title('Added SubSection')
301
+ page1 = find_by_title('Page 1')
302
+
239
303
  expect(referenced_object(page1[:Prev])).to eq(subsection)
240
304
  end
241
305
 
242
306
  it 'sets the first relation for the new subsection' do
243
- expect(referenced_object(subsection[:First])).to eq(added_page3)
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)
244
311
  end
245
312
 
246
313
  it 'sets the correct last relation of the added to section' do
247
- expect(referenced_object(subsection[:Last])).to eq(added_page3)
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)
248
318
  end
249
319
 
250
320
  it 'increases the count of root outline dictionary' do
@@ -281,26 +351,42 @@ describe Prawn::Outline do
281
351
  end
282
352
 
283
353
  it 'inserts new outline items to document' do
354
+ inserted_page = find_by_title('Inserted Page')
355
+
284
356
  expect(inserted_page).to_not be_nil
285
357
  end
286
358
 
287
359
  it 'adjusts the count of all ancestors' do
360
+ section1 = find_by_title('Chapter 1')
361
+
288
362
  expect(outline_root[:Count]).to eq(4)
289
363
  expect(section1[:Count].abs).to eq(3)
290
364
  end
291
365
 
292
366
  describe '#adjust_relations' do
293
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
+
294
372
  expect(referenced_object(page1[:Next])).to eq(inserted_page)
295
373
  expect(referenced_object(page2[:Prev])).to eq(inserted_page)
296
374
  end
297
375
 
298
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
+
299
381
  expect(referenced_object(inserted_page[:Next])).to eq(page2)
300
382
  expect(referenced_object(inserted_page[:Prev])).to eq(page1)
301
383
  end
302
384
 
303
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
+
304
390
  expect(referenced_object(section1[:First])).to eq(page1)
305
391
  expect(referenced_object(section1[:Last])).to eq(page2)
306
392
  end
@@ -316,6 +402,10 @@ describe Prawn::Outline do
316
402
  page destination: page_number, title: 'Inserted Page'
317
403
  end
318
404
  end
405
+
406
+ section1 = find_by_title('Chapter 1')
407
+ section2 = find_by_title('Added Section')
408
+
319
409
  expect(referenced_object(outline_root[:Last])).to eq(section2)
320
410
  expect(referenced_object(section1[:Next])).to eq(section2)
321
411
  end
@@ -336,15 +426,24 @@ describe Prawn::Outline do
336
426
 
337
427
  describe '#adjust_relations' do
338
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
+
339
432
  expect(referenced_object(page2[:Next])).to eq(inserted_page)
340
433
  end
341
434
 
342
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
+
343
439
  expect(referenced_object(inserted_page[:Next])).to be_nil
344
440
  expect(referenced_object(inserted_page[:Prev])).to eq(page2)
345
441
  end
346
442
 
347
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
+
348
447
  expect(referenced_object(section1[:Last])).to eq(inserted_page)
349
448
  end
350
449
  end
@@ -379,11 +478,12 @@ describe Prawn::Outline do
379
478
 
380
479
  describe 'foreign character encoding' do
381
480
  let(:hash) do
382
- pdf = Prawn::Document.new do
383
- outline.define do
384
- section 'La pomme croquée', destination: 1, closed: true
481
+ pdf =
482
+ Prawn::Document.new do
483
+ outline.define do
484
+ section 'La pomme croquée', destination: 1, closed: true
485
+ end
385
486
  end
386
- end
387
487
  PDF::Reader::ObjectHash.new(StringIO.new(pdf.render, 'r+'))
388
488
  end
389
489
 
@@ -110,15 +110,16 @@ describe Prawn::Repeater do
110
110
  end
111
111
 
112
112
  it 'must treat any block as a closure (Document.new instance_eval form)' do
113
- doc = Prawn::Document.new(skip_page_creation: true) do
114
- 10.times { start_new_page }
115
-
116
- page = 'Page'
117
- repeat(:all, dynamic: true) do
118
- # ensure self is accessible here
119
- draw_text "#{page} #{page_number}", at: [500, 0]
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
120
122
  end
121
- end
122
123
 
123
124
  text = PDF::Inspector::Text.analyze(doc.render)
124
125
  expect(text.strings).to eq((1..10).to_a.map { |p| "Page #{p}" })
@@ -52,7 +52,7 @@ describe Prawn::SoftMask do
52
52
  extgstate = PDF::Inspector::ExtGState.analyze(pdf.render).extgstates.first
53
53
  expect(extgstate[:soft_mask][:G].data).to eq(
54
54
  "q\n/DeviceRGB cs\n0.0 0.0 0.0 scn\n/DeviceRGB CS\n0.0 0.0 0.0 SCN\n"\
55
- "1 w\n0 J\n0 j\n[] 0 d\n/DeviceRGB cs\n0.502 0.502 0.502 scn\n"\
55
+ "1 w\n0 J\n0 j\n[] 0 d\n/DeviceRGB cs\n0.50196 0.50196 0.50196 scn\n"\
56
56
  "100.0 -100.0 200.0 200.0 re\nf\nQ\n"
57
57
  )
58
58
  end
@@ -116,9 +116,10 @@ describe Prawn::Stamp do
116
116
  output = pdf.render
117
117
  objects = output.split('endobj')
118
118
  objects.each do |object|
119
- if %r{/Type /Page$}.match?(object)
119
+ case object
120
+ when %r{/Type /Page$}
120
121
  expect(object).to_not match(%r{/ExtGState})
121
- elsif %r{/Type /XObject$}.match?(object)
122
+ when %r{/Type /XObject$}
122
123
  expect(object).to match(%r{/ExtGState})
123
124
  end
124
125
  end
@@ -183,7 +183,8 @@ describe Prawn::Text::Box do
183
183
  draw_block = instance_spy('Draw block')
184
184
 
185
185
  pdf.text_box 'this text has <b>fancy</b> formatting',
186
- inline_format: true, width: 500,
186
+ inline_format: true,
187
+ width: 500,
187
188
  draw_text_callback: ->(text, _) { draw_block.kick(text) }
188
189
 
189
190
  expect(draw_block).to have_received(:kick).with('this text has ')
@@ -193,8 +194,9 @@ describe Prawn::Text::Box do
193
194
 
194
195
  it 'does not call #draw_text!' do
195
196
  allow(pdf).to receive(:draw_text!)
196
- pdf.text_box 'some text', width: 500,
197
- draw_text_callback: ->(_, _) {}
197
+ pdf.text_box 'some text',
198
+ width: 500,
199
+ draw_text_callback: ->(_, _) {}
198
200
  expect(pdf).to_not have_received(:draw_text!)
199
201
  end
200
202
  end
@@ -310,21 +312,16 @@ describe Prawn::Text::Box do
310
312
  end
311
313
 
312
314
  describe '#render with :rotate option of 30)' do
313
- let(:angle) { 30 }
314
- let(:x) { 300 }
315
- let(:y) { 70 }
316
- let(:width) { 100 }
317
- let(:height) { 50 }
318
- let(:cos) { Math.cos(angle * Math::PI / 180) }
319
- let(:sin) { Math.sin(angle * Math::PI / 180) }
315
+ let(:cos) { Math.cos(30 * Math::PI / 180) }
316
+ let(:sin) { Math.sin(30 * Math::PI / 180) }
320
317
  let(:text) { 'Oh hai text rect. ' * 10 }
321
318
  let(:options) do
322
319
  {
323
320
  document: pdf,
324
- rotate: angle,
325
- at: [x, y],
326
- width: width,
327
- height: height
321
+ rotate: 30,
322
+ at: [300, 70],
323
+ width: 100,
324
+ height: 50
328
325
  }
329
326
  end
330
327
 
@@ -335,14 +332,14 @@ describe Prawn::Text::Box do
335
332
  text_box.render
336
333
 
337
334
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
338
- x_ = x + width / 2
339
- y_ = y - height / 2
340
- x_prime = x_ * cos - y_ * sin
341
- y_prime = x_ * sin + y_ * cos
335
+ x = 350
336
+ y = 45
337
+ x_prime = x * cos - y * sin
338
+ y_prime = x * sin + y * cos
342
339
  expect(matrices.matrices[0]).to eq([
343
340
  1, 0, 0, 1,
344
- reduce_precision(x_ - x_prime),
345
- reduce_precision(y_ - y_prime)
341
+ reduce_precision(x - x_prime),
342
+ reduce_precision(y - y_prime)
346
343
  ])
347
344
  expect(matrices.matrices[1]).to eq([
348
345
  reduce_precision(cos),
@@ -365,12 +362,12 @@ describe Prawn::Text::Box do
365
362
  text_box.render
366
363
 
367
364
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
368
- x_prime = x * cos - y * sin
369
- y_prime = x * sin + y * cos
365
+ x_prime = 300 * cos - 70 * sin
366
+ y_prime = 300 * sin + 70 * cos
370
367
  expect(matrices.matrices[0]).to eq([
371
368
  1, 0, 0, 1,
372
- reduce_precision(x - x_prime),
373
- reduce_precision(y - y_prime)
369
+ reduce_precision(300 - x_prime),
370
+ reduce_precision(70 - y_prime)
374
371
  ])
375
372
  expect(matrices.matrices[1]).to eq([
376
373
  reduce_precision(cos),
@@ -392,12 +389,12 @@ describe Prawn::Text::Box do
392
389
  text_box.render
393
390
 
394
391
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
395
- x_prime = x * cos - y * sin
396
- y_prime = x * sin + y * cos
392
+ x_prime = 300 * cos - 70 * sin
393
+ y_prime = 300 * sin + 70 * cos
397
394
  expect(matrices.matrices[0]).to eq([
398
395
  1, 0, 0, 1,
399
- reduce_precision(x - x_prime),
400
- reduce_precision(y - y_prime)
396
+ reduce_precision(300 - x_prime),
397
+ reduce_precision(70 - y_prime)
401
398
  ])
402
399
  expect(matrices.matrices[1]).to eq([
403
400
  reduce_precision(cos),
@@ -420,14 +417,14 @@ describe Prawn::Text::Box do
420
417
  text_box.render
421
418
 
422
419
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
423
- x_ = x + width
424
- y_ = y
425
- x_prime = x_ * cos - y_ * sin
426
- y_prime = x_ * sin + y_ * cos
420
+ x = 400
421
+ y = 70
422
+ x_prime = x * cos - y * sin
423
+ y_prime = x * sin + y * cos
427
424
  expect(matrices.matrices[0]).to eq([
428
425
  1, 0, 0, 1,
429
- reduce_precision(x_ - x_prime),
430
- reduce_precision(y_ - y_prime)
426
+ reduce_precision(x - x_prime),
427
+ reduce_precision(y - y_prime)
431
428
  ])
432
429
  expect(matrices.matrices[1]).to eq([
433
430
  reduce_precision(cos),
@@ -450,14 +447,14 @@ describe Prawn::Text::Box do
450
447
  text_box.render
451
448
 
452
449
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
453
- x_ = x + width
454
- y_ = y - height
455
- x_prime = x_ * cos - y_ * sin
456
- y_prime = x_ * sin + y_ * cos
450
+ x = 400
451
+ y = 20
452
+ x_prime = x * cos - y * sin
453
+ y_prime = x * sin + y * cos
457
454
  expect(matrices.matrices[0]).to eq([
458
455
  1, 0, 0, 1,
459
- reduce_precision(x_ - x_prime),
460
- reduce_precision(y_ - y_prime)
456
+ reduce_precision(x - x_prime),
457
+ reduce_precision(y - y_prime)
461
458
  ])
462
459
  expect(matrices.matrices[1]).to eq([
463
460
  reduce_precision(cos),
@@ -480,14 +477,14 @@ describe Prawn::Text::Box do
480
477
  text_box.render
481
478
 
482
479
  matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
483
- x_ = x
484
- y_ = y - height
485
- x_prime = x_ * cos - y_ * sin
486
- y_prime = x_ * sin + y_ * cos
480
+ x = 300
481
+ y = 20
482
+ x_prime = x * cos - y * sin
483
+ y_prime = x * sin + y * cos
487
484
  expect(matrices.matrices[0]).to eq([
488
485
  1, 0, 0, 1,
489
- reduce_precision(x_ - x_prime),
490
- reduce_precision(y_ - y_prime)
486
+ reduce_precision(x - x_prime),
487
+ reduce_precision(y - y_prime)
491
488
  ])
492
489
  expect(matrices.matrices[1]).to eq([
493
490
  reduce_precision(cos),
@@ -578,7 +575,7 @@ describe Prawn::Text::Box do
578
575
  it 'printed text should match requested text, except that preceding and ' \
579
576
  'trailing white space will be stripped from each line, and newlines ' \
580
577
  'may be inserted' do
581
- text_box = described_class.new(' ' + text, options)
578
+ text_box = described_class.new(" #{text}", options)
582
579
  text_box.render
583
580
  expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
584
581
  end
@@ -746,11 +743,9 @@ describe Prawn::Text::Box do
746
743
  remaining_text = text_box.render
747
744
 
748
745
  rotate = 30
749
- x = 300
750
- y = 70
751
746
  options[:document] = pdf
752
747
  options[:rotate] = rotate
753
- options[:at] = [x, y]
748
+ options[:at] = [300, 70]
754
749
  rotated_text_box = described_class.new(text, options)
755
750
  expect(rotated_text_box.render).to eq(remaining_text)
756
751
  end
@@ -1023,14 +1018,15 @@ describe Prawn::Text::Box do
1023
1018
 
1024
1019
  pdf.font 'Courier'
1025
1020
  text_box = described_class.new(
1026
- text, width: 180,
1027
- overflow: :expand,
1028
- document: pdf
1021
+ text,
1022
+ width: 180,
1023
+ overflow: :expand,
1024
+ document: pdf
1029
1025
  )
1030
1026
 
1031
1027
  text_box.render
1032
1028
 
1033
- expected = +'©' * 25 + "\n" + '©' * 5
1029
+ expected = "#{'©' * 25}\n#{'©' * 5}"
1034
1030
  expected = pdf.font.normalize_encoding(expected)
1035
1031
  expected = expected.force_encoding(Encoding::UTF_8)
1036
1032
  expect(text_box.text).to eq(expected)
@@ -1039,16 +1035,18 @@ describe Prawn::Text::Box do
1039
1035
  it 'wraps non-unicode strings using single-byte word-wrapping' do
1040
1036
  text = 'continúa esforzandote ' * 5
1041
1037
  text_box = described_class.new(
1042
- text, width: 180,
1043
- document: pdf
1038
+ text,
1039
+ width: 180,
1040
+ document: pdf
1044
1041
  )
1045
1042
  text_box.render
1046
1043
  results_with_accent = text_box.text
1047
1044
 
1048
1045
  text = 'continua esforzandote ' * 5
1049
1046
  text_box = described_class.new(
1050
- text, width: 180,
1051
- document: pdf
1047
+ text,
1048
+ width: 180,
1049
+ document: pdf
1052
1050
  )
1053
1051
  text_box.render
1054
1052
  results_without_accent = text_box.text
@@ -1107,6 +1105,6 @@ describe Prawn::Text::Box do
1107
1105
  end
1108
1106
 
1109
1107
  def first_line(str)
1110
- str.each_line { |line| return line }
1108
+ str.lines.first
1111
1109
  end
1112
1110
  end