lib/hexapdf/task/optimize.rb in hexapdf-0.11.6 vs lib/hexapdf/task/optimize.rb in hexapdf-0.11.7
- old
+ new
@@ -125,31 +125,37 @@
def self.process_object_streams(doc, method, xref_streams)
case method
when :delete
doc.revisions.each_with_index do |rev, rev_index|
xref_stream = false
+ objects_to_delete = []
rev.each do |obj|
- if obj.type == :ObjStm || (obj.type == :XRef && xref_streams == :delete)
- rev.delete(obj)
+ if obj.type == :ObjStm
+ objects_to_delete << obj
+ elsif obj.type == :XRef
+ xref_stream = true
+ objects_to_delete << obj if xref_streams == :delete
else
delete_fields_with_defaults(obj)
end
end
+ objects_to_delete.each {|obj| rev.delete(obj) }
if xref_streams == :generate && !xref_stream
doc.add({Type: :XRef}, revision: rev_index)
end
end
when :generate
doc.revisions.each_with_index do |rev, rev_index|
xref_stream = false
count = 0
objstms = [doc.wrap({Type: :ObjStm})]
+ old_objstms = []
rev.each do |obj|
if obj.type == :XRef
xref_stream = true
elsif obj.type == :ObjStm
- rev.delete(obj)
+ old_objstms << obj
end
delete_fields_with_defaults(obj)
next if obj.respond_to?(:stream)
@@ -158,9 +164,10 @@
if count == 200
objstms << doc.wrap({Type: :ObjStm})
count = 0
end
end
+ old_objstms.each {|objstm| rev.delete(objstm) }
objstms.each {|objstm| doc.add(objstm, revision: rev_index) }
doc.add({Type: :XRef}, revision: rev_index) unless xref_stream
end
end
end