File: page_spec.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (360 lines) | stat: -rw-r--r-- 11,527 bytes parent folder | download | duplicates (2)
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
# typed: false
# coding: utf-8

describe PDF::Reader::Page do
  describe "#initialize" do
    it "raises InvalidPageError when an invalid page number is provided" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      expect {
        PDF::Reader::Page.new(@browser.objects, 10)
      }.to raise_error(PDF::Reader::InvalidPageError)
    end
  end

  describe "#raw_content" do
    it "returns a string from raw_content() from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.raw_content).to be_a_kind_of(String)
    end
  end

  describe "#text" do
    # only do a very basic test here. Detailed testing of text extraction is
    # done by testing the PageTextReceiver class
    it "returns the text content from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.text).to eql("Hello James")
    end

  end

  describe "#boxes" do
    let!(:page)    { browser.page(1) }
    let!(:browser) { PDF::Reader.new(pdf_spec_file("all_page_boxes_exist")) }

    it "returns a hash of all the different boxes" do
      expect(page.attributes[:ArtBox]).to_not be_empty
      expect(page.attributes[:BleedBox]).to_not be_empty
      expect(page.attributes[:CropBox]).to_not be_empty
      expect(page.attributes[:MediaBox]).to_not be_empty
      expect(page.attributes[:TrimBox]).to_not be_empty

      expect(page.boxes).to eq(
        {
          ArtBox: [0, 0, 612, 792],
          BleedBox: [0, 0, 612, 792],
          CropBox: [0, 0, 612, 792],
          MediaBox: [0, 0, 612, 792],
          TrimBox: [0, 0, 612, 792],
        }
      )
    end

    context "mediabox and cropbox are references" do
      let!(:browser) { PDF::Reader.new(pdf_spec_file("mediabox_and_cropbox_are_references")) }

      it "returns a non-reference for the dimensions of the boxes" do
        expect(page.boxes).to eq(
          {
            ArtBox: [0, 0, 612, 792],
            BleedBox: [0, 0, 612, 792],
            CropBox: [0, 0, 612, 792],
            MediaBox: [0, 0, 612, 792],
            TrimBox: [0, 0, 612, 792],
          }
        )
      end
    end
  end

  describe "#rectangles" do
    let!(:page)    { browser.page(1) }
    let!(:browser) { PDF::Reader.new(pdf_spec_file("all_page_boxes_exist")) }

    it "returns a hash of all the different boxes" do
      expect(page.attributes[:ArtBox]).to_not be_empty
      expect(page.attributes[:BleedBox]).to_not be_empty
      expect(page.attributes[:CropBox]).to_not be_empty
      expect(page.attributes[:MediaBox]).to_not be_empty
      expect(page.attributes[:TrimBox]).to_not be_empty

      expect(page.rectangles).to eq(
        {
          ArtBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
          BleedBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
          CropBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
          MediaBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
          TrimBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
        }
      )
    end

    context "mediabox and cropbox are references" do
      let!(:browser) { PDF::Reader.new(pdf_spec_file("mediabox_and_cropbox_are_references")) }

      it "returns a non-reference for the dimensions of the boxes" do
        expect(page.rectangles).to eq(
          {
            ArtBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
            BleedBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
            CropBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
            MediaBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
            TrimBox: PDF::Reader::Rectangle.new(0, 0, 612, 792),
          }
        )
      end
    end
  end

  describe "#walk" do

    context "with page 1 of cairo-basic.pdf" do
      let!(:browser) { PDF::Reader.new(pdf_spec_file("cairo-basic")) }
      let!(:page)    { browser.page(1) }

      it "calls the special page= callback while walking content stream" do
        receiver = PDF::Reader::RegisterReceiver.new
        page.walk(receiver)

        callbacks = receiver.callbacks.map { |cb| cb[:name] }

        expect(callbacks.first).to eql(:page=)
      end

      it "runs callbacks while walking content stream" do
        receiver = PDF::Reader::RegisterReceiver.new
        page.walk(receiver)

        callbacks = receiver.callbacks.map { |cb| cb[:name] }

        expect(callbacks.size).to eql(16)
        expect(callbacks[0]).to eql(:page=)
        expect(callbacks[1]).to eql(:save_graphics_state)
      end

      it "runs callbacks on multiple receivers while walking content stream" do
        receiver_one = PDF::Reader::RegisterReceiver.new
        receiver_two = PDF::Reader::RegisterReceiver.new
        page.walk(receiver_one, receiver_two)

        callbacks = receiver_one.callbacks.map { |cb| cb[:name] }

        expect(callbacks.size).to eql(16)
        expect(callbacks.first).to eql(:page=)

        callbacks = receiver_two.callbacks.map { |cb| cb[:name] }

        expect(callbacks.size).to eql(16)
        expect(callbacks.first).to eql(:page=)
      end

      context "with a receiver that implements a method not type checked by ValidatingReceiver" do

        before do
          # This test is speecifically for confirming a receiver method is called, even if that
          # method isn't one that ValidatingReceiver is type checking. If the following assertion
          # ever fails, then we should update this spec to a different method not mentioned in
          # ValidatingReceiver
          expect(PDF::Reader::ValidatingReceiver.methods).to_not include(
            :set_rgb_color_for_nonstroking
          )
        end

        it "calls the receiver method as expected" do
          receiver = double(set_rgb_color_for_nonstroking: true)
          page.walk(receiver)
          expect(receiver).to have_received(:set_rgb_color_for_nonstroking).twice
        end
      end

    end
  end

  describe "#number" do

    it "returns the correct number for the current page" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.number).to eql(1)
    end

  end

  describe "#attributes" do

    it "contains attributes from the Page object" do
      @browser = PDF::Reader.new(pdf_spec_file("inherited_page_attributes"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:Resources]).to      be_a_kind_of(Hash)
      expect(attribs[:Resources].size).to eql(2)
    end

    it "contains inherited attributes" do
      @browser = PDF::Reader.new(pdf_spec_file("inherited_page_attributes"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:MediaBox]).to eql([0.0, 0.0, 595.276, 841.89])
    end

    it "allows Page to override inherited attributes" do
      @browser = PDF::Reader.new(pdf_spec_file("override_inherited_attributes"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:MediaBox]).to eql([0, 0, 200, 200])
    end

    it "does not include attributes from the Pages object that don't belong on a Page" do
      @browser = PDF::Reader.new(pdf_spec_file("inherited_page_attributes"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:Kids]).to be_nil
    end

    it "does not include attributes from the Pages object that don't belong on a Page" do
      @browser = PDF::Reader.new(pdf_spec_file("inherited_trimbox"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:TrimBox]).to be_nil
    end

    it "always includes Type => Page" do
      @browser = PDF::Reader.new(pdf_spec_file("inherited_page_attributes"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:Type]).to eql(:Page)
    end

    it 'assumes 8.5" x 11" if MediaBox is missing (matches Acrobat behaviour)' do
      @browser = PDF::Reader.new(pdf_spec_file("mediabox_missing"))
      @page    = @browser.page(1)

      attribs = @page.attributes
      expect(attribs[:MediaBox]).to eql([0,0,612,792])
    end
  end


  describe "#fonts" do

    it "returns a hash with the correct size from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.fonts).to      be_a_kind_of(Hash)
      expect(@page.fonts.size).to eql(1)
      expect(@page.fonts.keys).to eql([:"CairoFont-0-0"])
    end

    it "contains inherited resources" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.fonts).to      be_a_kind_of(Hash)
      expect(@page.fonts.size).to eql(1)
      expect(@page.fonts.keys).to eql([:"CairoFont-0-0"])
    end

  end

  describe "#color_spaces" do

    it "returns an empty hash from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.color_spaces).to      be_a_kind_of(Hash)
      expect(@page.color_spaces.size).to eql(0)
    end
  end

  describe "#graphic_states" do

    it "returns an hash with 1 entry from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.graphic_states).to      be_a_kind_of(Hash)
      expect(@page.graphic_states.size).to eql(1)
    end
  end

  describe "#orientation" do

    # this just checks that Page calls the PageOrientation class correctly. Extended specs
    # to check the different orientations are correctly detected are over in the
    # PageOrientation unit specs
    it "returns the orientation of portrait.pdf page 1 as 'portrait'" do
      @browser = PDF::Reader.new(pdf_spec_file("portrait"))
      @page    = @browser.page(1)
      expect(@page.orientation).to eql("portrait")
    end

  end

  describe "#patterns" do

    it "returns an empty hash from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.patterns).to      be_a_kind_of(Hash)
      expect(@page.patterns.size).to eql(0)
    end
  end

  describe "#procedure_sets" do

    it "returns an empty array from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.procedure_sets).to      be_a_kind_of(Array)
      expect(@page.procedure_sets.size).to eql(0)
    end
  end

  describe "#properties" do

    it "returns an empty hash from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.properties).to      be_a_kind_of(Hash)
      expect(@page.properties.size).to eql(0)
    end
  end

  describe "#shadings" do

    it "returns an empty hash from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.shadings).to      be_a_kind_of(Hash)
      expect(@page.shadings.size).to eql(0)
    end
  end

  describe "#xobjects" do

    it "returns an empty hash from cairo-basic.pdf page 1" do
      @browser = PDF::Reader.new(pdf_spec_file("cairo-basic"))
      @page    = @browser.page(1)

      expect(@page.xobjects).to      be_a_kind_of(Hash)
      expect(@page.xobjects.size).to eql(0)
    end
  end
end