File: outline_spec.rb

package info (click to toggle)
ruby-prawn 1.0.0~rc1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: ruby: 17,499; sh: 44; makefile: 17
file content (408 lines) | stat: -rw-r--r-- 12,912 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
# encoding: utf-8
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")

describe "Outline" do
  before(:each) do
    @pdf = Prawn::Document.new() do
      text "Page 1. This is the first Chapter. "
      start_new_page
      text "Page 2. More in the first Chapter. "
      start_new_page
      outline.define do
        section 'Chapter 1', :destination => 1, :closed => true do
          page :destination => 1, :title => 'Page 1'
          page :destination => 2, :title => 'Page 2'
        end
      end
    end
  end
  if RUBY_VERSION >= "1.9"
    describe "outline encoding" do
      it "should store all outline titles as UTF-16" do
        render_and_find_objects
        @hash.values.each do |obj|
          if obj.is_a?(Hash) && obj[:Title]
            title = obj[:Title].dup
            title.force_encoding("UTF-16LE")
            title.valid_encoding?.should == true
          end
        end
      end
    end
  end
  describe "#generate_outline" do
    before(:each) do
      render_and_find_objects
    end

    it "should create a root outline dictionary item" do
      assert_not_nil @outline_root
    end

    it "should set the first and last top items of the root outline dictionary item" do
      referenced_object(@outline_root[:First]).should == @section_1
      referenced_object(@outline_root[:Last]).should == @section_1
    end

    describe "#create_outline_item" do
      it "should create outline items for each section and page" do
        [@section_1, @page_1, @page_2].each {|item| assert_not_nil item}
      end
    end

    describe "#set_relations, #set_variables_for_block, and #reset_parent" do
      it "should link sibling items" do
        referenced_object(@page_1[:Next]).should == @page_2
        referenced_object(@page_2[:Prev]).should == @page_1
      end

      it "should link child items to parent item" do
        [@page_1, @page_2].each {|page| referenced_object(page[:Parent]).should == @section_1 }
      end

      it "should set the first and last child items for parent item" do
        referenced_object(@section_1[:First]).should == @page_1
        referenced_object(@section_1[:Last]).should == @page_2
      end
    end

    describe "#increase_count" do

      it "should add the count of all descendant items" do
        @outline_root[:Count].should == 3
        @section_1[:Count].should.abs == 2
        @page_1[:Count].should == 0
        @page_2[:Count].should == 0
      end

    end

    describe "closed option" do

      it "should set the item's integer count to negative" do
        @section_1[:Count].should == -2
      end

    end

  end

  describe "addding a section later with outline#section" do
    before(:each) do
      @pdf.start_new_page
      @pdf.text "Page 3. An added section "
      @pdf.outline.update do
        section 'Added Section', :destination => 3 do
          page :destination => 3, :title => 'Page 3'
        end
      end
      render_and_find_objects
    end

    it "should add new outline items to document" do
      [@section_2, @page_3].each { |item| assert_not_nil item}
    end

    it "should reset the last items for root outline dictionary" do
      referenced_object(@outline_root[:First]).should == @section_1
      referenced_object(@outline_root[:Last]).should == @section_2
    end

    it "should reset the next relation for the previous last top level item" do
      referenced_object(@section_1[:Next]).should == @section_2
    end

    it "should set the previous relation of the addded to section" do
      referenced_object(@section_2[:Prev]).should == @section_1
    end

    it "should increase the count of root outline dictionary" do
      @outline_root[:Count].should == 5
    end

  end  
  
  describe "#outline.add_subsection_to" do  
    context "positioned last" do
    
      before(:each) do
        @pdf.start_new_page
        @pdf.text "Page 3. An added subsection "
        @pdf.outline.update do
          add_subsection_to 'Chapter 1' do
            section 'Added SubSection', :destination => 3 do
              page :destination => 3, :title => 'Added Page 3'
            end
          end 
        end
        render_and_find_objects
      end
    
      it "should add new outline items to document" do
        [@subsection, @added_page_3].each { |item| assert_not_nil item}
      end
    
      it "should reset the last item for parent item dictionary" do
        referenced_object(@section_1[:First]).should == @page_1
        referenced_object(@section_1[:Last]).should == @subsection
      end
    
      it "should set the prev relation for the new subsection to its parent's old last item" do
        referenced_object(@subsection[:Prev]).should == @page_2
      end
    
    
      it "the subsection should become the next relation for its parent's old last item" do
         referenced_object(@page_2[:Next]).should == @subsection
       end
    
      it "should set the first relation for the new subsection" do
        referenced_object(@subsection[:First]).should == @added_page_3
      end
    
      it "should set the correct last relation of the added to section" do
        referenced_object(@subsection[:Last]).should == @added_page_3
      end
    
      it "should increase the count of root outline dictionary" do
        @outline_root[:Count].should == 5
      end
    
    end  
    
    context "positioned first" do
    
      before(:each) do
        @pdf.start_new_page
        @pdf.text "Page 3. An added subsection "
        @pdf.outline.update do
          add_subsection_to 'Chapter 1', :first do
            section 'Added SubSection', :destination => 3 do
              page :destination => 3, :title => 'Added Page 3'
            end
          end
        end
        render_and_find_objects
      end

      it "should add new outline items to document" do
        [@subsection, @added_page_3].each { |item| assert_not_nil item}
      end

      it "should reset the first item for parent item dictionary" do
        referenced_object(@section_1[:First]).should == @subsection
        referenced_object(@section_1[:Last]).should == @page_2
      end
    
      it "should set the next relation for the new subsection to its parent's old first item" do
        referenced_object(@subsection[:Next]).should == @page_1
      end

      it "the subsection should become the prev relation for its parent's old first item" do
         referenced_object(@page_1[:Prev]).should == @subsection
       end

      it "should set the first relation for the new subsection" do
        referenced_object(@subsection[:First]).should == @added_page_3
      end

      it "should set the correct last relation of the added to section" do
        referenced_object(@subsection[:Last]).should == @added_page_3
      end

      it "should increase the count of root outline dictionary" do
        @outline_root[:Count].should == 5
      end

    end
    
    it "should require an existing title" do
      assert_raise Prawn::Errors::UnknownOutlineTitle do
        @pdf.go_to_page 1
        @pdf.start_new_page
        @pdf.text "Inserted Page"
        @pdf.outline.update do
          add_subsection_to 'Wrong page' do
            page page_number, :title => "Inserted Page"
          end
        end
        render_and_find_objects
      end
    end
  end     

  describe "#outline.insert_section_after" do
    describe "inserting in the middle of another section" do
      before(:each) do
        @pdf.go_to_page 1
        @pdf.start_new_page
        @pdf.text "Inserted Page"
        @pdf.outline.update do
          insert_section_after 'Page 1' do
            page :destination => page_number, :title => "Inserted Page"
          end
        end
      end

      it "should insert new outline items to document" do
        render_and_find_objects
        assert_not_nil @inserted_page
      end
      
      it "should adjust the count of all ancestors" do
        render_and_find_objects
        @outline_root[:Count].should == 4
        @section_1[:Count].should.abs == 3
      end
       
      describe "#adjust_relations" do
      
        it "should reset the sibling relations of adjoining items to inserted item" do
          render_and_find_objects
          referenced_object(@page_1[:Next]).should == @inserted_page
          referenced_object(@page_2[:Prev]).should == @inserted_page
        end
      
        it "should set the sibling relation of added item to adjoining items" do
          render_and_find_objects
          referenced_object(@inserted_page[:Next]).should == @page_2
          referenced_object(@inserted_page[:Prev]).should == @page_1
        end
      
        it "should not affect the first and last relations of parent item" do
          render_and_find_objects
          referenced_object(@section_1[:First]).should == @page_1
          referenced_object(@section_1[:Last]).should == @page_2
        end
      
      end 
      
      
      context "when adding another section afterwards" do 
        it "should have reset the root position so that a new section is added at the end of root sections" do
          @pdf.start_new_page
          @pdf.text "Another Inserted Page"
          @pdf.outline.update do
            section 'Added Section' do
              page :destination => page_number, :title => "Inserted Page"
            end 
          end 
          render_and_find_objects
          referenced_object(@outline_root[:Last]).should == @section_2
          referenced_object(@section_1[:Next]).should == @section_2
        end
      end

   end


    describe "inserting at the end of another section" do

      before(:each) do
        @pdf.go_to_page 2
         @pdf.start_new_page
         @pdf.text "Inserted Page"
         @pdf.outline.update do
           insert_section_after 'Page 2' do
             page :destination => page_number, :title => "Inserted Page"
           end 
         end
         render_and_find_objects
      end

      describe "#adjust_relations" do

        it "should reset the sibling relations of adjoining item to inserted item" do
           referenced_object(@page_2[:Next]).should == @inserted_page
        end

        it "should set the sibling relation of added item to adjoining items" do
          assert_nil referenced_object(@inserted_page[:Next])
          referenced_object(@inserted_page[:Prev]).should == @page_2
        end

        it "should adjust the last relation of parent item" do
          referenced_object(@section_1[:Last]).should == @inserted_page
        end

      end
    end

    it "should require an existing title" do
      assert_raise Prawn::Errors::UnknownOutlineTitle do
        @pdf.go_to_page 1
        @pdf.start_new_page
        @pdf.text "Inserted Page"
        @pdf.outline.update do
          insert_section_after 'Wrong page' do
            page :destination => page_number, :title => "Inserted Page"
          end
        end
        render_and_find_objects
      end
    end

  end

  describe "#page" do
    it "should require a title option to be set" do
      assert_raise Prawn::Errors::RequiredOption do
        @pdf = Prawn::Document.new() do
          text "Page 1. This is the first Chapter. "
          outline.define do
            page :destination => 1, :title => nil
          end
        end
      end
    end
  end
end
  
context "foreign character encoding" do
  before(:each) do
    pdf = Prawn::Document.new() do
      outline.define do
        section 'La pomme croquée', :destination => 1, :closed => true
      end
    end
    @hash = PDF::Reader::ObjectHash.new(StringIO.new(pdf.render, 'r+'))
  end

  it "should handle other encodings for the title" do
    object = find_by_title('La pomme croquée')
    object.should.not == nil
  end
end

def render_and_find_objects
  output = StringIO.new(@pdf.render, 'r+')
  @hash = PDF::Reader::ObjectHash.new(output)
  @outline_root = @hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Outlines}
  @pages = @hash.values.find {|obj| obj.is_a?(Hash) && obj[:Type] == :Pages}[:Kids]
  @section_1 = find_by_title('Chapter 1')
  @page_1 = find_by_title('Page 1')
  @page_2 = find_by_title('Page 2')
  @section_2 = find_by_title('Added Section')
  @page_3 = find_by_title('Page 3')
  @inserted_page = find_by_title('Inserted Page') 
  @subsection = find_by_title('Added SubSection')
  @added_page_3 = find_by_title('Added Page 3')
end

# Outline titles are stored as UTF-16. This method accepts a UTF-8 outline title
# and returns the PDF Object that contains an outline with that name
def find_by_title(title)
  @hash.values.find {|obj|
    if obj.is_a?(Hash) && obj[:Title]
      title_codepoints = obj[:Title].unpack("n*")
      title_codepoints.shift
      utf8_title = title_codepoints.pack("U*")
      utf8_title == title ? obj : nil
    end
  }
end

def referenced_object(reference)
  @hash[reference]
end