lib/pdf/core/page.rb in prawn-templates-0.1.1 vs lib/pdf/core/page.rb in prawn-templates-0.1.2
- old
+ new
@@ -20,20 +20,36 @@
else
init_new_page(options)
end
end
+ # Prepend a content stream containing 'q',
+ # and append a content stream containing 'Q'.
+ # This ensures that prawn has a pristine graphics state
+ # before it starts adding content.
+ def wrap_graphics_state
+ dictionary.data[:Contents] = Array(dictionary.data[:Contents])
+
+ # Save graphics context
+ @content = document.ref({})
+ dictionary.data[:Contents].unshift(document.state.store[@content])
+ document.add_content 'q'
+
+ # Restore graphics context
+ @content = document.ref({})
+ dictionary.data[:Contents] << document.state.store[@content]
+ document.add_content 'Q'
+ end
+
# As per the PDF spec, each page can have multiple content streams. This
# will add a fresh, empty content stream this the page, mainly for use in
# loading template files.
#
def new_content_stream
return if in_stamp_stream?
- unless dictionary.data[:Contents].is_a?(Array)
- dictionary.data[:Contents] = [content]
- end
+ dictionary.data[:Contents] = Array(dictionary.data[:Contents])
@content = document.ref({})
dictionary.data[:Contents] << document.state.store[@content]
document.open_graphics_state
end
@@ -43,11 +59,15 @@
end
end
alias __dimensions dimensions if method_defined? :dimensions
def dimensions
- return inherited_dictionary_value(:MediaBox) if imported_page?
+ if imported_page?
+ media_box = inherited_dictionary_value(:MediaBox)
+ return media_box.data if media_box.is_a?(PDF::Core::Reference)
+ return media_box
+ end
coords = PDF::Core::PageGeometry::SIZES[size] || size
[0, 0] +
case layout
when :portrait
@@ -98,10 +118,10 @@
TrimBox: trim_box,
ArtBox: art_box,
Contents: content
)
- resources[:ProcSet] = [:PDF, :Text, :ImageB, :ImageC, :ImageI]
+ resources[:ProcSet] = %i[PDF Text ImageB ImageC ImageI]
end
end
end
end