podoff 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,415 +0,0 @@
1
-
2
- #
3
- # specifying podoff
4
- #
5
- # Tue Oct 20 13:30:16 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Podoff::Document do
12
-
13
- before :all do
14
-
15
- @d = Podoff.load('pdfs/udocument0.pdf', 'iso-8859-1')
16
- end
17
-
18
- describe '#objs' do
19
-
20
- it 'returns a hash of PDF "obj"' do
21
-
22
- expect(@d.objs.class).to eq(Hash)
23
- expect(@d.objs.values.first.class).to eq(Podoff::Obj)
24
- expect(@d.objs.size).to eq(273)
25
- end
26
- end
27
-
28
- describe '#pages' do
29
-
30
- it 'returns the pages' do
31
-
32
- expect(@d.pages.size).to eq(3)
33
- expect(@d.pages.first.class).to eq(Podoff::Obj)
34
- end
35
- end
36
-
37
- describe '#page' do
38
-
39
- it 'returns a page given an index (starts at 1)' do
40
-
41
- p = @d.page(1)
42
- expect(p.ref).to eq('56 0')
43
- expect(p.class).to eq(Podoff::Obj)
44
- expect(p.type).to eq('/Page')
45
- end
46
-
47
- it 'returns nil if the page doesn\'t exist' do
48
-
49
- expect(@d.page(0)).to eq(nil)
50
- expect(@d.page(9)).to eq(nil)
51
- end
52
-
53
- it 'returns a page given an index (starts at 1) (2)' do
54
-
55
- d = Podoff::Document.load('pdfs/t2.pdf', 'utf-8')
56
-
57
- expect(d.page(1).ref).to eq('3 0')
58
-
59
- expect(d.page(0)).to eq(nil)
60
- expect(d.page(2)).to eq(nil)
61
- end
62
-
63
- it 'returns pages from the last when the index is negative' do
64
-
65
- expect(@d.page(-1).ref).to eq('58 0')
66
- end
67
-
68
- it 'returns pages from the last when the index is negative (2)' do
69
-
70
- d = Podoff::Document.load('pdfs/t2.pdf', 'utf-8')
71
-
72
- expect(d.page(-1).ref).to eq('3 0')
73
- end
74
- end
75
-
76
- describe '#dup' do
77
-
78
- it 'produces a shallow copy of the document' do
79
-
80
- d = @d.dup
81
-
82
- expect(d.class).to eq(Podoff::Document)
83
- expect(d.hash).not_to eq(@d.hash)
84
-
85
- expect(d.encoding).to eq('iso-8859-1')
86
-
87
- expect(d.objs.hash).not_to eq(@d.objs.hash)
88
-
89
- expect(d.objs.values.first.hash).not_to eq(@d.objs.values.first.hash)
90
- expect(d.objs.values.first.class).to eq(Podoff::Obj)
91
- expect(d.objs.values.first.document.class).to eq(Podoff::Document)
92
-
93
- expect(d.objs.values.first.document).to equal(d)
94
- expect(@d.objs.values.first.document).to equal(@d)
95
-
96
- expect(d.root).to eq('1 0')
97
- end
98
-
99
- it 'sports objs with properly recomputed attributes' do
100
-
101
- pa = @d.page(1)
102
-
103
- d = @d.dup
104
-
105
- expect(d.objs[pa.ref].attributes).to eq(pa.attributes)
106
- end
107
- end
108
-
109
- context 'additions' do
110
-
111
- before :each do
112
-
113
- @d = Podoff.load('pdfs/t0.pdf', 'utf-8')
114
- end
115
-
116
- describe '#add_base_font' do
117
-
118
- it 'adds a new /Font obj to the document' do
119
-
120
- fo = @d.add_base_font('Helvetica')
121
-
122
- expect(@d.additions.size).to eq(1)
123
- expect(@d.objs.keys).to eq((1..7).map { |i| "#{i} 0" })
124
- expect(@d.additions.keys).to eq([ '7 0' ])
125
-
126
- expect(fo.document).to eq(@d)
127
- expect(fo.ref).to eq('7 0')
128
-
129
- expect(fo.source).to eq(
130
- '7 0 obj <</Type /Font /Subtype /Type1 /BaseFont /Helvetica>> endobj')
131
-
132
- s = @d.write(:string)
133
- d = Podoff.parse(s, 'utf-8')
134
-
135
- expect(d.xref).to eq(686)
136
-
137
- expect(s).to be_a_valid_pdf
138
- end
139
-
140
- it 'doesn\'t mind a slash in front of the font name' do
141
-
142
- fo = @d.add_base_font('/Helvetica')
143
-
144
- expect(@d.additions.size).to eq(1)
145
- expect(@d.objs.keys).to eq((1..7).map { |i| "#{i} 0" })
146
- expect(@d.additions.keys).to eq([ '7 0' ])
147
-
148
- expect(fo.document).to eq(@d)
149
- expect(fo.ref).to eq('7 0')
150
-
151
- expect(fo.source).to eq(
152
- '7 0 obj <</Type /Font /Subtype /Type1 /BaseFont /Helvetica>> endobj')
153
- end
154
- end
155
-
156
- describe '#add_stream' do
157
-
158
- it 'adds a new obj with a stream to the document' do
159
-
160
- st = @d.add_stream('BT 70 80 Td /Helvetica 35 Tf (Hello!) Tj ET')
161
-
162
- expect(@d.additions.size).to eq(1)
163
- expect(@d.objs.keys).to eq((1..7).map { |i| "#{i} 0" })
164
- expect(@d.additions.keys).to eq([ '7 0' ])
165
-
166
- expect(st.document).to eq(@d)
167
- expect(st.ref).to eq('7 0')
168
-
169
- expect(st.source).to eq(%{
170
- 7 0 obj
171
- << /Length 43 >>
172
- stream
173
- BT 70 80 Td /Helvetica 35 Tf (Hello!) Tj ET
174
- endstream
175
- endobj
176
- }.strip)
177
-
178
- s = @d.write(:string)
179
-
180
- expect(s).to be_a_valid_pdf
181
-
182
- d = Podoff.parse(s, 'utf-8')
183
-
184
- expect(d.xref).to eq(711)
185
- end
186
-
187
- it 'accepts a block' do
188
-
189
- st =
190
- @d.add_stream {
191
- tf '/Helvetica', 35
192
- bt 10, 20, 'thirty here'
193
- bt 40, 50, 'sixty there'
194
- }
195
-
196
- expect(st.obj.document).to eq(@d)
197
- expect(st.obj.ref).to eq('7 0')
198
-
199
- expect(st.to_s).to eq(%{
200
- 7 0 obj
201
- <</Length 97>>
202
- stream
203
- BT /Helvetica 35 Tf 10 20 Td (thirty here) Tj ET
204
- BT /Helvetica 35 Tf 40 50 Td (sixty there) Tj ET
205
- endstream
206
- endobj
207
- }.strip)
208
-
209
- s = @d.write(:string)
210
-
211
- expect(s).to be_a_valid_pdf
212
-
213
- d = Podoff.parse(s, 'utf-8')
214
-
215
- expect(d.source.index('<</Length 97>>')).to eq(625)
216
- expect(d.xref).to eq(763)
217
- end
218
-
219
- it 'returns the open stream when no arg given' do
220
-
221
- st = @d.add_stream
222
-
223
- expect(st.class).to eq(Podoff::Stream)
224
- end
225
- end
226
-
227
- describe '#re_add' do
228
-
229
- it 'replicates an obj and adds the replica to the document' do
230
-
231
- pa = @d.page(1)
232
- re = @d.re_add(pa)
233
-
234
- expect(@d.additions.size).to eq(1)
235
- expect(@d.objs.keys).to eq((1..6).map { |i| "#{i} 0" })
236
- expect(@d.additions.keys).to eq([ '3 0' ])
237
-
238
- expect(re.document).to eq(@d)
239
- expect(re.ref).to eq(pa.ref)
240
- expect(re.source).to eq(pa.source)
241
- expect(re.source).not_to equal(pa.source)
242
- end
243
-
244
- it 'accepts a ref' do
245
-
246
- pa = @d.page(1)
247
- re = @d.re_add(pa.ref)
248
-
249
- expect(@d.additions.size).to eq(1)
250
- expect(@d.objs.keys).to eq((1..6).map { |i| "#{i} 0" })
251
- expect(@d.additions.keys).to eq([ '3 0' ])
252
-
253
- expect(re.document).to eq(@d)
254
- expect(re.ref).to eq(pa.ref)
255
- expect(re.source).to eq(pa.source)
256
- expect(re.source).not_to equal(pa.source)
257
- end
258
-
259
- it 'recomputes the attributes correctly' do
260
-
261
- d = Podoff.load('pdfs/qdocument0.pdf', 'iso-8859-1')
262
-
263
- pa = d.re_add(d.page(1))
264
-
265
- expect(pa.attributes).to eq({ type: '/Page', contents: '151 0 R' })
266
- end
267
- end
268
- end
269
-
270
- describe '#write' do
271
-
272
- it 'writes the document to a given path' do
273
-
274
- @d.write('tmp/out.pdf')
275
-
276
- s = File.open('tmp/out.pdf', 'r:iso8859-1') { |f| f.read }
277
- lines = s.split("\n")
278
-
279
- expect(lines.first).to match(/^%PDF-1.7$/)
280
- expect(lines.last).to match(/^%%EOF$/)
281
- end
282
-
283
- it 'writes open streams as well' do
284
-
285
- d = Podoff.load('pdfs/t0.pdf', 'utf-8')
286
-
287
- pa = d.re_add(d.page(1))
288
- st = d.add_stream
289
- st.bt(10, 20, 'hello open stream')
290
- pa.insert_contents(st)
291
-
292
- s = d.write(:string)
293
-
294
- expect(
295
- s.index(%{
296
- 7 0 obj
297
- <</Length 37>>
298
- stream
299
- BT 10 20 Td (hello open stream) Tj ET
300
- endstream
301
- endobj
302
- }.strip)
303
- ).to eq(729)
304
- end
305
-
306
- it 'writes a proper xref table' do
307
-
308
- d = Podoff.load('pdfs/t0.pdf', 'utf-8')
309
-
310
- pa = d.re_add(d.page(1))
311
- st = d.add_stream
312
- st.bt(10, 20, 'hello open stream')
313
- pa.insert_contents(st)
314
-
315
- s = d.write(:string)
316
-
317
- expect(s).to be_a_valid_pdf
318
-
319
- expect(s[814..-1].strip).to eq(%{
320
- xref
321
- 0 1
322
- 0000000000 65535 f
323
- 3 1
324
- 0000000617 00000 n
325
- 7 1
326
- 0000000729 00000 n
327
- trailer
328
- <<
329
- /Prev 413
330
- /Size 8
331
- /Root 1 0 R
332
- >>
333
- startxref 815
334
- %%EOF
335
- }.strip)
336
- end
337
- end
338
-
339
- describe '#rewrite' do
340
-
341
- it 'rewrites a document in one go' do
342
-
343
- d = Podoff.load('pdfs/t2.pdf', 'utf-8')
344
-
345
- s = d.rewrite(:string)
346
-
347
- expect(s.strip).to eq(%{
348
- %PDF-1.4
349
- 1 0 obj <</Type /Catalog /Pages 2 0 R>>
350
- endobj
351
- 2 0 obj <</Type /Pages /Kids [3 0 R] /Count 1>>
352
- endobj
353
- 3 0 obj <</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents [6 0 R 7 0 R]>>
354
- endobj
355
- 4 0 obj <</Font <</F1 5 0 R>>>>
356
- endobj
357
- 5 0 obj <</Type /Font /Subtype /Type1 /BaseFont /Helvetica>>
358
- endobj
359
- 6 0 obj
360
- <</Length 44>>
361
- stream
362
- BT /F1 24 Tf 175 720 Td (Hello Nadaa!)Tj ET
363
- endstream
364
- endobj
365
- 7 0 obj
366
- <</Length 44>>
367
- stream
368
- BT /F1 24 Tf 175 520 Td (Smurf Megane)Tj ET
369
- endstream
370
- endobj
371
- xref
372
- 0 1
373
- 0000000000 65535 f
374
- 1 7
375
- 0000000009 00000 n
376
- 0000000056 00000 n
377
- 0000000111 00000 n
378
- 0000000221 00000 n
379
- 0000000260 00000 n
380
- 0000000328 00000 n
381
- 0000000419 00000 n
382
- trailer
383
- <<
384
- /Size 8
385
- /Root 1 0 R
386
- >>
387
- startxref 510
388
- %%EOF
389
- }.strip)
390
-
391
- expect(s).to be_a_valid_pdf
392
- end
393
- end
394
-
395
- describe '#extract_refs' do
396
-
397
- it 'extracts a ref' do
398
-
399
- expect(
400
- Podoff::Document.allocate.send(:extract_refs, '17 0 R')
401
- ).to eq([ '17 0' ])
402
- expect(
403
- Podoff::Document.allocate.send(:extract_refs, ' 17 0 R')
404
- ).to eq([ '17 0' ])
405
- end
406
-
407
- it 'extracts a list of ref' do
408
-
409
- expect(
410
- Podoff::Document.allocate.send(:extract_refs, '[17 0 R 6 0 R]')
411
- ).to eq([ '17 0', '6 0' ])
412
- end
413
- end
414
- end
415
-
@@ -1,253 +0,0 @@
1
-
2
- #
3
- # specifying podoff
4
- #
5
- # Tue Oct 20 15:08:59 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Podoff::Obj do
12
-
13
- before :all do
14
-
15
- @d = Podoff.load('pdfs/udocument0.pdf', 'iso-8859-1')
16
- end
17
-
18
- describe '#document' do
19
-
20
- it 'points to the Podoff::Document owning this Obj' do
21
-
22
- expect(@d.objs.values.first.document).to eq(@d)
23
- end
24
- end
25
-
26
- describe '#source' do
27
-
28
- it 'returns the source behind the obj' do
29
-
30
- o = @d.objs['20 0']
31
-
32
- expect(o.source).to eq(%{
33
- 20 0 obj
34
- << /DA (/Calibri,Bold 10 Tf 0 g) /F 4 /FT /Tx /MK << >> /P 58 0 R /Rect [ 448.723 652.574 490.603 667.749 ] /Subtype /Widget /T (State) /TU (State) /Type /Annot >>
35
- endobj
36
- }.strip)
37
- end
38
- end
39
-
40
- # describe '#match' do
41
- #
42
- # it 'returns a MatchData instance if there is a match' do
43
- #
44
- # o = @d.objs['1 0']
45
- #
46
- # m = o.match(/\/Contents ([^\n]+)/)
47
- #
48
- # expect(m).not_to eq(nil)
49
- # expect(m[1]).to eq('3 0 R')
50
- # expect(m.offset(0)).to eq([ 123, 138 ]) # /!\
51
- # end
52
- #
53
- # it 'returns nil if the match exits the obj' do
54
- #
55
- # o = @d.objs['1 0']
56
- #
57
- # m = o.match(/3 0 obj/)
58
- #
59
- # expect(m).to eq(nil)
60
- # end
61
- #
62
- # it 'returns nil if there is no match' do
63
- #
64
- # o = @d.objs['1 0']
65
- #
66
- # m = o.match(/nada/)
67
- #
68
- # expect(m).to eq(nil)
69
- # end
70
- # end
71
- #
72
- # describe '#dmatch' do
73
- #
74
- # it 'matches with the zero offset set to the document' do
75
- #
76
- # o = @d.objs['1 0']
77
- #
78
- # m = o.dmatch(/\/Contents ([^\n]+)/)
79
- #
80
- # expect(m).not_to eq(nil)
81
- # expect(m[1]).to eq('3 0 R')
82
- # expect(m.offset(0)).to eq([ 138, 153 ]) # /!\
83
- # end
84
- # end
85
-
86
- describe '#type' do
87
-
88
- it 'returns the type of the obj' do
89
-
90
- expect(@d.objs['12 0'].type).to eq('/Font')
91
- end
92
-
93
- it 'returns nil if there is no type' do
94
-
95
- expect(@d.objs['59 0'].type).to eq(nil)
96
- end
97
-
98
- it 'works on open streams' do
99
-
100
- st = @d.add_stream
101
-
102
- expect(st.obj.type).to eq(nil)
103
- end
104
- end
105
-
106
- # describe '#parent' do
107
- #
108
- # it 'returns the parent ref if any' do
109
- #
110
- # expect(@d.objs.values.first.parent).to eq('2 0')
111
- # end
112
- #
113
- # it 'returns nil if there is no parent' do
114
- #
115
- # expect(@d.objs['2 0'].parent).to eq(nil)
116
- # end
117
- # end
118
-
119
- # describe '#kids' do
120
- #
121
- # it 'returns a list of refs' do
122
- #
123
- # expect(@d.objs['2 0'].kids).to eq([ '1 0', '16 0', '33 0' ])
124
- # end
125
- #
126
- # it 'returns an empty list if there are no kids' do
127
- #
128
- # expect(@d.objs['224 0'].kids).to eq([])
129
- # end
130
- # end
131
- #
132
- # describe '#contents' do
133
- #
134
- # it 'returns the Contents references (single)' do
135
- #
136
- # expect(@d.objs['1 0'].contents).to eq([ '3 0' ])
137
- # end
138
- #
139
- # it 'returns the Contents references (array)' do
140
- #
141
- # expect(@d.objs['16 0'].contents).to eq([ '17 0' ])
142
- # end
143
- #
144
- # it 'returns an empty list if none' do
145
- #
146
- # expect(@d.objs['224 0'].contents).to eq([])
147
- # end
148
- # end
149
-
150
- context 'insertions' do
151
-
152
- before :each do
153
-
154
- @d = Podoff.load('pdfs/udocument0.pdf', 'iso-8859-1')
155
- end
156
-
157
- describe '#insert_contents' do
158
-
159
- it 'fails if the target hasn\'t been replicated' do
160
-
161
- expect {
162
- @d.objs['23 0'].insert_contents('-1 0')
163
- }.to raise_error(ArgumentError, "target '23 0' not a replica")
164
- end
165
-
166
- it 'fails if the target doesn\'t have /Contents' do
167
-
168
- expect {
169
- ta = @d.re_add('23 0')
170
- ta.insert_contents('-1 0')
171
- }.to raise_error(ArgumentError, "target '23 0' doesn't have /Contents")
172
- end
173
-
174
- it 'accepts an obj' do
175
-
176
- pa = @d.re_add(@d.page(1))
177
-
178
- st = @d.add_stream('BT 70 80 Td /Font0 35 Tf (content is king!) Tj ET')
179
-
180
- pa.insert_contents(st)
181
-
182
- expect(pa.source).to match(/\/Contents \[151 0 R #{st.ref} R\]/)
183
- end
184
-
185
- it 'accepts an obj ref' do
186
-
187
- pa = @d.re_add(@d.page(1))
188
-
189
- st = @d.add_stream('BT 70 80 Td /Font0 35 Tf (content is king!) Tj ET')
190
-
191
- pa.insert_contents(st.ref)
192
-
193
- expect(pa.source).to match(/\/Contents \[151 0 R #{st.ref} R\]/)
194
- end
195
- end
196
-
197
- describe '#insert_font' do
198
-
199
- it 'fails if the target hasn\'t been replicated' do
200
-
201
- expect {
202
- @d.objs['23 0'].insert_font('/Helvetica', '-1 0')
203
- }.to raise_error(ArgumentError, "target '23 0' not a replica")
204
- end
205
-
206
- it 'accepts name, obj' do
207
-
208
- fo = @d.add_base_font('/Helvetica')
209
- pa = @d.re_add(@d.page(1))
210
-
211
- pa.insert_font('MyHelv', fo)
212
-
213
- expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
214
- end
215
-
216
- it 'accepts name, obj ref' do
217
-
218
- fo = @d.add_base_font('/Helvetica')
219
- pa = @d.re_add(@d.page(1))
220
-
221
- pa.insert_font('MyHelv', fo.ref)
222
-
223
- expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
224
- end
225
-
226
- it 'accepts a slash in front of the name' do
227
-
228
- fo = @d.add_base_font('/Helvetica')
229
- pa = @d.re_add(@d.page(1))
230
-
231
- pa.insert_font('/MyHelv', fo.ref)
232
-
233
- expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
234
- end
235
- end
236
-
237
- describe '#add_to_attribute' do
238
-
239
- it 'adds to a list of references' do
240
-
241
- d = Podoff.load('pdfs/qdocument0.pdf', 'iso-8859-1')
242
-
243
- o = d.re_add('56 0')
244
-
245
- o.send(:add_to_attribute, :contents, '9999 0')
246
-
247
- expect(o.attributes).to eq(
248
- { type: '/Page', contents: '[151 0 R 9999 0 R]' })
249
- end
250
- end
251
- end
252
- end
253
-