spec/obj_spec.rb in podoff-0.9.0 vs spec/obj_spec.rb in podoff-1.0.0
- old
+ new
@@ -21,163 +21,217 @@
expect(@d.objs.values.first.document).to eq(@d)
end
end
- describe '#type' do
+ describe '#source' do
- it 'returns the type of the obj' do
+ it 'returns the source behind the obj' do
- expect(@d.objs['23 0'].type).to eq('Font')
- end
+ o = @d.objs['20 0']
- it 'returns nil if there is no type' do
-
- expect(@d.objs['17 0'].type).to eq(nil)
+ expect(o.source).to eq(%{
+20 0 obj [21 0 R]
+endobj
+ }.strip)
end
end
- describe '#parent' do
+# describe '#match' do
+#
+# it 'returns a MatchData instance if there is a match' do
+#
+# o = @d.objs['1 0']
+#
+# m = o.match(/\/Contents ([^\n]+)/)
+#
+# expect(m).not_to eq(nil)
+# expect(m[1]).to eq('3 0 R')
+# expect(m.offset(0)).to eq([ 123, 138 ]) # /!\
+# end
+#
+# it 'returns nil if the match exits the obj' do
+#
+# o = @d.objs['1 0']
+#
+# m = o.match(/3 0 obj/)
+#
+# expect(m).to eq(nil)
+# end
+#
+# it 'returns nil if there is no match' do
+#
+# o = @d.objs['1 0']
+#
+# m = o.match(/nada/)
+#
+# expect(m).to eq(nil)
+# end
+# end
+#
+# describe '#dmatch' do
+#
+# it 'matches with the zero offset set to the document' do
+#
+# o = @d.objs['1 0']
+#
+# m = o.dmatch(/\/Contents ([^\n]+)/)
+#
+# expect(m).not_to eq(nil)
+# expect(m[1]).to eq('3 0 R')
+# expect(m.offset(0)).to eq([ 138, 153 ]) # /!\
+# end
+# end
- it 'returns the parent ref if any' do
+ describe '#type' do
- expect(@d.objs.values.first.parent).to eq('2 0')
- end
+ it 'returns the type of the obj' do
- it 'returns nil if there is no parent' do
-
- expect(@d.objs['2 0'].parent).to eq(nil)
+ expect(@d.objs['23 0'].type).to eq('/Font')
end
- end
- describe '#kids' do
+ it 'returns nil if there is no type' do
- it 'returns a list of refs' do
-
- expect(@d.objs['2 0'].kids).to eq([ '1 0', '16 0', '33 0' ])
+ expect(@d.objs['17 0'].type).to eq(nil)
end
- it 'returns an empty list if there are no kids' do
+ it 'works on open streams' do
- expect(@d.objs['224 0'].kids).to eq([])
- end
- end
+ st = @d.add_stream
- describe '#contents' do
-
- it 'returns the Contents reference' do
-
- expect(@d.objs['1 0'].contents).to eq('3 0')
+ expect(st.obj.type).to eq(nil)
end
-
- it 'returns nil if none' do
-
- expect(@d.objs['224 0'].contents).to eq(nil)
- end
end
- describe '#font_names' do
+# describe '#parent' do
+#
+# it 'returns the parent ref if any' do
+#
+# expect(@d.objs.values.first.parent).to eq('2 0')
+# end
+#
+# it 'returns nil if there is no parent' do
+#
+# expect(@d.objs['2 0'].parent).to eq(nil)
+# end
+# end
- it 'returns a list of font names visible in this obj' do
+# describe '#kids' do
+#
+# it 'returns a list of refs' do
+#
+# expect(@d.objs['2 0'].kids).to eq([ '1 0', '16 0', '33 0' ])
+# end
+#
+# it 'returns an empty list if there are no kids' do
+#
+# expect(@d.objs['224 0'].kids).to eq([])
+# end
+# end
+#
+# describe '#contents' do
+#
+# it 'returns the Contents references (single)' do
+#
+# expect(@d.objs['1 0'].contents).to eq([ '3 0' ])
+# end
+#
+# it 'returns the Contents references (array)' do
+#
+# expect(@d.objs['16 0'].contents).to eq([ '17 0' ])
+# end
+#
+# it 'returns an empty list if none' do
+#
+# expect(@d.objs['224 0'].contents).to eq([])
+# end
+# end
- expect(
- @d.objs.values.first.font_names
- ).to eq(%w[
- C2_0 TT2 TT1 TT0 C2_2 C2_1 Helv
- ])
- end
- end
+ context 'insertions' do
- describe '#index' do
+ before :each do
- it 'returns the for a given line' do
-
- o = @d.objs['3 0']
-
- expect(o.index('stream')).to eq(4)
- expect(o.index('BT')).to eq(14)
+ @d = Podoff.load('pdfs/udocument0.pdf')
end
- it 'returns nil when it doesn\'t find' do
+ describe '#insert_contents' do
- o = @d.objs['3 0']
+ it 'fails if the target hasn\'t been replicated' do
- expect(o.index('nada')).to eq(nil)
- end
+ expect {
+ @d.objs['23 0'].insert_contents('-1 0')
+ }.to raise_error(ArgumentError, "target '23 0' not a replica")
+ end
- it 'accepts regexes' do
+ it 'fails if the target doesn\'t have /Contents' do
- o = @d.objs['1 0']
+ expect {
+ ta = @d.re_add('23 0')
+ ta.insert_contents('-1 0')
+ }.to raise_error(ArgumentError, "target '23 0' doesn't have /Contents")
+ end
- i = o.index(/^\/B.+Box /)
+ it 'accepts an obj' do
- expect(i).to eq(40)
- expect(o.lines[i]).to eq('/BleedBox [0.0 0.0 612.0 792.0]')
- end
+ pa = @d.re_add(@d.page(1))
- it 'accepts a start index' do
+ st = @d.add_stream('BT 70 80 Td /Font0 35 Tf (content is king!) Tj ET')
- o = @d.objs['1 0']
+ pa.insert_contents(st)
- i = o.index(/^\/.+Box /, 3)
+ expect(pa.source).to match(/\/Contents \[3 0 R #{st.ref} R\]\n/)
+ end
- expect(i).to eq(5)
- expect(o.lines[i]).to eq('/TrimBox [0.0 0.0 612.0 792.0]')
- end
- end
+ it 'accepts an obj ref' do
- describe '#find' do
+ pa = @d.re_add(@d.page(1))
- it 'returns the first sub obj that matches the given block' do
+ st = @d.add_stream('BT 70 80 Td /Font0 35 Tf (content is king!) Tj ET')
- o = @d.objs['1 0']
+ pa.insert_contents(st.ref)
- o1 = o.find { |o| o.index('stream') }
-
- expect(o1).not_to eq(nil)
- expect(o1.lines.first).to eq('3 0 obj ')
+ expect(pa.source).to match(/\/Contents \[3 0 R #{st.ref} R\]\n/)
+ end
end
- it 'accept a :skip_root option'
- end
+ describe '#insert_font' do
- describe '#gather' do
+ it 'fails if the target hasn\'t been replicated' do
- it 'returns a list of sub obj that match the given block'
- it 'accept a :skip_root option'
- end
+ expect {
+ @d.objs['23 0'].insert_font('/Helvetica', '-1 0')
+ }.to raise_error(ArgumentError, "target '23 0' not a replica")
+ end
- describe '#crop_box' do
+ it 'accepts name, obj' do
- it 'returns the [ x, y, w, h ] box for the obj' do
+ fo = @d.add_base_font('/Helvetica')
+ pa = @d.re_add(@d.page(1))
- o = @d.objs['1 0']
+ pa.insert_font('MyHelv', fo)
- expect(o.crop_box).to eq([ 0.0, 0.0, 612.0, 792.0 ])
- end
+ expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
+ end
- it 'defaults to the MediaBox' do
+ it 'accepts name, obj ref' do
- o = @d.objs['16 0']
+ fo = @d.add_base_font('/Helvetica')
+ pa = @d.re_add(@d.page(1))
- expect(o.crop_box).to eq([ 0.0, 0.0, 612.0, 792.0 ])
- end
- end
+ pa.insert_font('MyHelv', fo.ref)
- describe '#crop_dims' do
+ expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
+ end
- it 'returns [ w, h ]' do
+ it 'accepts a slash in front of the name' do
- o = @d.objs['1 0']
+ fo = @d.add_base_font('/Helvetica')
+ pa = @d.re_add(@d.page(1))
- expect(o.crop_dims).to eq([ 612.0, 792.0 ])
- end
+ pa.insert_font('/MyHelv', fo.ref)
- it 'defaults to the MediaBox' do
-
- o = @d.objs['16 0']
-
- expect(o.crop_dims).to eq([ 612.0, 792.0 ])
+ expect(pa.source).to match(/\/Font\s+<<\s+\/MyHelv #{fo.ref} R\s+/)
+ end
end
end
end