File: bounding_box_spec.rb

package info (click to toggle)
ruby-prawn 1.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,936 kB
  • ctags: 802
  • sloc: ruby: 13,906; sh: 43; makefile: 15
file content (510 lines) | stat: -rw-r--r-- 15,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# encoding: utf-8

require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")

describe "A bounding box" do

  before(:each) do
    @x      = 100
    @y      = 125
    @width  = 50
    @height = 75
    @box = Prawn::Document::BoundingBox.new(nil, nil, [@x,@y],
      :width  => @width, :height => @height )
  end

  it "should have an anchor at (x, y - height)" do
    @box.anchor.should == [@x,@y-@height]
  end

  it "should have a left boundary of 0" do
    @box.left.should == 0
  end

  it "should have a right boundary equal to the width" do
    @box.right.should == @width
  end

  it "should have a top boundary of height" do
    @box.top.should == @height
  end

  it "should have a bottom boundary of 0" do
    @box.bottom.should == 0
  end

  it "should have a top-left of [0,height]" do
    @box.top_left.should == [0,@height]
  end

  it "should have a top-right of [width,height]" do
    @box.top_right.should == [@width,@height]
  end

  it "should have a bottom-left of [0,0]" do
    @box.bottom_left.should == [0,0]
  end

  it "should have a bottom-right of [width,0]" do
    @box.bottom_right.should == [@width,0]
  end

  it "should have an absolute left boundary of x" do
    @box.absolute_left.should == @x
  end

  it "should have an absolute right boundary of x + width" do
    @box.absolute_right.should == @x + @width
  end

  it "should have an absolute top boundary of y" do
    @box.absolute_top.should == @y
  end

  it "should have an absolute bottom boundary of y - height" do
    @box.absolute_bottom.should == @y - @height
  end

  it "should have an absolute bottom-left of [x,y-height]" do
    @box.absolute_bottom_left.should == [@x, @y - @height]
  end

  it "should have an absolute bottom-right of [x+width,y-height]" do
    @box.absolute_bottom_right.should == [@x + @width , @y - @height]
  end

  it "should have an absolute top-left of [x,y]" do
    @box.absolute_top_left.should == [@x, @y]
  end

  it "should have an absolute top-right of [x+width,y]" do
    @box.absolute_top_right.should == [@x + @width, @y]
  end

  it "should require width to be set" do
    lambda do
      Prawn::Document::BoundingBox.new(nil, nil, [100,100])
    end.should raise_error(ArgumentError)
  end

  it "should raise_error an ArgumentError if a block is not passed" do
    pdf = Prawn::Document.new
    lambda do
      pdf.bounding_box([0, 0], :width => 200)
    end.should raise_error(ArgumentError)
  end

end

describe "drawing bounding boxes" do

  before(:each) { create_pdf }

  it "should not stomp on the arguments to bounding_box" do
    pdf = Prawn::Document.new
    x = [100, 500]
    pdf.bounding_box x, :width => 100 do
      pdf.text "bork-bork-bork"
    end
    x.should == [100, 500]
  end

  it "should restore Document#bounds to the correct margin box on exit" do
    pdf = Prawn::Document.new(:margin => 200)

    # add a multi-page bounding box
    pdf.bounding_box([100, pdf.bounds.top], :width => 400) do
      pdf.text "The rain in spain falls mainly in the plains.\n" * 30
    end

    pdf.start_new_page(:margin => 0)

    x_min, y_min, x_max, y_max = pdf.page.dimensions

    pdf.bounds.absolute_top_left.should == [x_min, y_max]
    pdf.bounds.absolute_bottom_right.should == [x_max, y_min]
  end

  it "should restore the parent bounding box when calls are nested" do
    @pdf.bounding_box [100,500], :width => 300, :height => 300 do

      @pdf.bounds.absolute_top.should  == 500 + @pdf.margin_box.absolute_bottom
      @pdf.bounds.absolute_left.should == 100 + @pdf.margin_box.absolute_left

      parent_box = @pdf.bounds

      @pdf.bounding_box [50,200], :width => 100, :height => 100 do
        @pdf.bounds.absolute_top.should == 200 + parent_box.absolute_bottom
        @pdf.bounds.absolute_left.should == 50 + parent_box.absolute_left
      end

      @pdf.bounds.absolute_top.should  == 500 + @pdf.margin_box.absolute_bottom
      @pdf.bounds.absolute_left.should == 100 + @pdf.margin_box.absolute_left

    end
  end

  it "should calculate a height if none is specified" do
    @pdf.bounding_box([100, 500], :width => 100) do
      @pdf.text "The rain in Spain falls mainly on the plains."
    end

    @pdf.y.should be_within(0.001).of(458.384)
  end

  it "should keep track of the max height the box was stretched to" do
    box = @pdf.bounding_box(@pdf.bounds.top_left, :width => 100) do
      @pdf.move_down 100
      @pdf.move_up 15
    end

    box.height.should == 100
  end

  it "should advance the y-position by bbox.height by default" do
    orig_y = @pdf.y
    @pdf.bounding_box [0, @pdf.cursor], :width => @pdf.bounds.width,
        :height => 30 do
      @pdf.text "hello"
    end
    @pdf.y.should be_within(0.001).of(orig_y - 30)
  end

  it "should not advance y-position if passed :hold_position => true" do
    orig_y = @pdf.y
    @pdf.bounding_box [0, @pdf.cursor], :width => @pdf.bounds.width,
        :hold_position => true do
      @pdf.text "hello"
    end
    # y only advances by height of one line ("hello")
    @pdf.y.should be_within(0.001).of(orig_y - @pdf.height_of("hello"))
  end

  it "should not advance y-position of a stretchy bbox if it would stretch " +
     "the bbox further" do
    bottom = @pdf.y = @pdf.margin_box.absolute_bottom
    @pdf.bounding_box [0, @pdf.margin_box.top], :width => @pdf.bounds.width do
      @pdf.y = bottom
      @pdf.text "hello" # starts a new page
    end
    @pdf.page_count.should == 2

    # Restoring the position (to the absolute bottom) would stretch the bbox to
    # the bottom of the page, which we don't want. This should be equivalent to
    # a bbox with :hold_position => true, where we only advance by the amount
    # that was actually drawn.
    @pdf.y.should be_within(0.001).of(
      @pdf.margin_box.absolute_top - @pdf.height_of("hello")
    )
  end

end

describe "Indentation" do
  before(:each) { create_pdf }

  it "should temporarily shift the x coordinate and width" do
    @pdf.bounding_box([100,100], :width => 200) do
      @pdf.indent(20) do
        @pdf.bounds.absolute_left.should == 120
        @pdf.bounds.width.should == 180
      end
    end
  end

  it "should restore the x coordinate and width after block exits" do
    @pdf.bounding_box([100,100], :width => 200) do
      @pdf.indent(20) do
        # no-op
      end
      @pdf.bounds.absolute_left.should == 100
      @pdf.bounds.width.should == 200
    end
  end

  it "should restore the x coordinate and width on error" do
    @pdf.bounding_box([100,100], :width => 200) do
      begin
        @pdf.indent(20) { raise }
      rescue
        @pdf.bounds.absolute_left.should == 100
        @pdf.bounds.width.should == 200
      end
    end
  end

  it "should maintain left indentation across a page break" do
    original_left = @pdf.bounds.absolute_left

    @pdf.indent(20) do
      @pdf.bounds.absolute_left.should == original_left + 20
      @pdf.start_new_page
      @pdf.bounds.absolute_left.should == original_left + 20
    end

    @pdf.bounds.absolute_left.should == original_left
  end

  it "should maintain right indentation across a page break" do
    original_width = @pdf.bounds.width

    @pdf.indent(0, 20) do
      @pdf.bounds.width.should == original_width - 20
      @pdf.start_new_page
      @pdf.bounds.width.should == original_width - 20
    end

    @pdf.bounds.width.should == original_width
  end

  it "optionally allows adjustment of the right bound as well" do
    @pdf.bounding_box([100,100], :width => 200) do
      @pdf.indent(20, 30) do
        @pdf.bounds.absolute_left.should == 120
        @pdf.bounds.width.should == 150
      end
      @pdf.bounds.absolute_left.should == 100
      @pdf.bounds.width.should == 200
    end
  end

  describe "in a ColumnBox" do
    it "should subtract the given indentation from the available width" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width,
                      :height => 200, :columns => 2, :spacer => 20) do
        width = @pdf.bounds.width
        @pdf.indent(20) do
          @pdf.bounds.width.should be_within(0.01).of(width - 20)
        end
      end
    end

    it "should subtract right padding from the available width" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width,
                      :height => 200, :columns => 2, :spacer => 20) do
        width = @pdf.bounds.width
        @pdf.indent(20, 30) do
          @pdf.bounds.width.should be_within(0.01).of(width - 50)
        end
      end
    end

    it "should maintain the same left indentation across column breaks" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          x = @pdf.bounds.left_side
          @pdf.indent(20) do
            @pdf.bounds.left_side.should == x+20
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end

    it "should not change the right margin if only left indentation is requested" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          x = @pdf.bounds.right_side
          @pdf.indent(20) do
            @pdf.bounds.right_side.should == x
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end

    it "should maintain the same right indentation across columns" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          x = @pdf.bounds.right_side
          @pdf.indent(20, 10) do
            @pdf.bounds.right_side.should == x-10
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end

    it "should keep the right indentation after nesting indents" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          # I am giving a right indent of 10...
          @pdf.indent(20, 10) do
            x = @pdf.bounds.right_side
            # ...and no right indent here...
            @pdf.indent(20) do
              # right indent is inherited from the parent!
              @pdf.bounds.right_side.should == x
            end
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end

    it "should revert the right indentation if negative indent is given in nested indent" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          x = @pdf.bounds.right_side
          @pdf.indent(20, 10) do
            # requesting a negative right-indent of equivalent size...
            @pdf.indent(20, -10) do
              # ...resets the right margin to that of the column!
              @pdf.bounds.right_side.should == x
            end
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end

    it "should reduce the available column width by the sum of all nested indents" do
      @pdf.column_box([0, @pdf.cursor], :width => @pdf.bounds.width, :columns => 3, :spacer => 15) do
        3.times do |column|
          w = @pdf.bounds.width
          @pdf.indent(20, 10) do
            @pdf.indent(20, 10) do
              @pdf.bounds.width.should == w - 60
            end
          end
          @pdf.bounds.move_past_bottom
        end
      end
    end
  end
end

describe "A canvas" do
  before(:each) { create_pdf }

  it "should use whatever the last set y position is" do
    @pdf.canvas do
      @pdf.bounding_box([100,500],:width => 200) { @pdf.move_down 50 }
    end
    @pdf.y.should == 450
  end

  it "should restore the original ypos after execution", :issue => 523 do
    doc = Prawn::Document.new(:skip_page_creation => true)
    doc.start_new_page

    original_ypos = doc.y

    doc.canvas {}

    doc.y.should == original_ypos
  end

end

describe "Deep-copying" do
  it "should create a new object that does not copy @document" do
    Prawn::Document.new do |pdf|
      orig = pdf.bounds
      copy = orig.deep_copy

      copy.should_not == pdf.bounds
      copy.document.should be_nil
    end
  end

  it "should deep-copy parent bounds" do
    Prawn::Document.new do |pdf|
      outside = pdf.bounds
      pdf.bounding_box [100, 100], :width => 100 do
        copy = pdf.bounds.deep_copy

        # the parent bounds should have the same parameters
        copy.parent.width.should  == outside.width
        copy.parent.height.should == outside.height

        # but should not be the same object
        copy.parent.should_not == outside
      end
    end
  end
end

describe "Prawn::Document#reference_bounds" do
  before(:each) { create_pdf }

  it "should return self for non-stretchy bounds" do
    @pdf.bounding_box([0, @pdf.cursor], :width => 100, :height => 100) do
      @pdf.reference_bounds.should == @pdf.bounds
    end
  end

  it "should return the parent bounds if in a stretchy box" do
    @pdf.bounding_box([0, @pdf.cursor], :width => 100, :height => 100) do
      correct_bounds = @pdf.bounds
      @pdf.bounding_box([0, @pdf.cursor], :width => 100) do
        @pdf.reference_bounds.should == correct_bounds
      end
    end
  end

  it "should find the non-stretchy box through 2 levels" do
    @pdf.bounding_box([0, @pdf.cursor], :width => 100, :height => 100) do
      correct_bounds = @pdf.bounds
      @pdf.bounding_box([0, @pdf.cursor], :width => 100) do
        @pdf.bounding_box([0, @pdf.cursor], :width => 100) do
          @pdf.reference_bounds.should == correct_bounds
        end
      end
    end
  end

  it "should return the margin box if there's no explicit bbox" do
    @pdf.reference_bounds.should == @pdf.margin_box

    @pdf.bounding_box([0, @pdf.cursor], :width => 100) do
      @pdf.reference_bounds.should == @pdf.margin_box
    end
  end

  it "should return the canvas box if we're in a canvas" do
    @pdf.canvas do
      canvas_box = @pdf.bounds

      @pdf.reference_bounds.should == canvas_box

      @pdf.bounding_box([0, @pdf.cursor], :width => 100) do
        @pdf.reference_bounds.should == canvas_box
      end
    end
  end

end

describe "BoundingBox#move_past_bottom" do
  before(:each) { create_pdf }

  it "should ordinarily start a new page" do
    @pdf.bounds.move_past_bottom
    @pdf.text "Foo"

    pages = PDF::Inspector::Page.analyze(@pdf.render).pages
    pages.size.should == 2
    pages[0][:strings].should == []
    pages[1][:strings].should == ["Foo"]
  end

  it "should move to the top of the next page if it exists already" do
    # save away the y-position at the top of a page
    top_y = @pdf.y

    # create a blank page but go to the page before it
    @pdf.start_new_page
    @pdf.go_to_page 1
    @pdf.text "Foo"

    @pdf.bounds.move_past_bottom
    @pdf.y.should be_within(0.001).of(top_y)
    @pdf.text "Bar"

    pages = PDF::Inspector::Page.analyze(@pdf.render).pages
    pages.size.should == 2
    pages[0][:strings].should == ["Foo"]
    pages[1][:strings].should == ["Bar"]
  end
end