lib/prawn/document.rb in prawn-0.13.2 vs lib/prawn/document.rb in prawn-0.14.0

- old
+ new

@@ -60,10 +60,18 @@ include Prawn::Graphics include Prawn::Images include Prawn::Stamp include Prawn::SoftMask + # NOTE: We probably need to rethink the options validation system, but this + # constant temporarily allows for extensions to modify the list. + + VALID_OPTIONS = [:page_size, :page_layout, :margin, :left_margin, + :right_margin, :top_margin, :bottom_margin, :skip_page_creation, + :compress, :skip_encoding, :background, :info, + :optimize_objects, :text_formatter, :print_scaling] + # Any module added to this array will be included into instances of # Prawn::Document at the per-object level. These will also be inherited by # any subclasses. # # Example: @@ -136,11 +144,10 @@ # <tt>:compress</tt>:: Compresses content streams before rendering them [false] # <tt>:optimize_objects</tt>:: Reduce number of PDF objects in output, at expense of render time [false] # <tt>:background</tt>:: An image path to be used as background on all pages [nil] # <tt>:background_scale</tt>:: Backgound image scale [1] [nil] # <tt>:info</tt>:: Generic hash allowing for custom metadata properties [nil] - # <tt>:template</tt>:: The path to an existing PDF file to use as a template [nil] # <tt>:text_formatter</tt>: The text formatter to use for <tt>:inline_format</tt>ted text [Prawn::Text::Formatted::Parser] # # Setting e.g. the :margin to 100 points and the :left_margin to 50 will result in margins # of 100 points on every side except for the left, where it will be 50. # @@ -171,23 +178,22 @@ # pdf = Prawn::Document.new(:background => "#{Prawn::DATADIR}/images/pigs.jpg") # def initialize(options={},&block) options = options.dup - Prawn.verify_options [:page_size, :page_layout, :margin, :left_margin, - :right_margin, :top_margin, :bottom_margin, :skip_page_creation, - :compress, :skip_encoding, :background, :info, - :optimize_objects, :template, :text_formatter], options + Prawn.verify_options VALID_OPTIONS, options # need to fix, as the refactoring breaks this # raise NotImplementedError if options[:skip_page_creation] self.class.extensions.reverse_each { |e| extend e } @internal_state = PDF::Core::DocumentState.new(options) @internal_state.populate_pages_from_store(self) min_version(state.store.min_version) if state.store.min_version + min_version(1.6) if options[:print_scaling] == :none + @background = options[:background] @background_scale = options[:background_scale] || 1 @font_size = 12 @bounding_box = nil @@ -198,20 +204,11 @@ @text_formatter = options.delete(:text_formatter) || Text::Formatted::Parser options[:size] = options.delete(:page_size) options[:layout] = options.delete(:page_layout) - if options[:template] - fresh_content_streams(options) - go_to_page(1) - else - if options[:skip_page_creation] || options[:template] - start_new_page(options.merge(:orphan => true)) - else - start_new_page(options) - end - end + initialize_first_page(options) @bounding_box = @margin_box if block block.arity < 1 ? instance_eval(&block) : block[self] @@ -230,28 +227,28 @@ def page state.page end + def initialize_first_page(options) + if options[:skip_page_creation] + start_new_page(options.merge(:orphan => true)) + else + start_new_page(options) + end + end + # Creates and advances to a new page in the document. # # Page size, margins, and layout can also be set when generating a # new page. These values will become the new defaults for page creation # # pdf.start_new_page #=> Starts new page keeping current values # pdf.start_new_page(:size => "LEGAL", :layout => :landscape) # pdf.start_new_page(:left_margin => 50, :right_margin => 50) # pdf.start_new_page(:margin => 100) # - # A template for a page can be specified by pointing to the path of and existing pdf. - # One can also specify which page of the template which defaults otherwise to 1. - # - # pdf.start_new_page(:template => multipage_template.pdf, :template_page => 2) - # - # Note: templates get indexed by either the object_id of the filename or stream - # entered so that if you reuse the same template multiple times be sure to use the - # same instance for more efficient use of resources and smaller rendered pdfs. def start_new_page(options = {}) if last_page = state.page last_page_size = last_page.size last_page_layout = last_page.layout last_page_margins = last_page.margins @@ -264,11 +261,10 @@ new_graphic_state = last_page.graphic_state.dup if last_page.graphic_state #erase the color space so that it gets reset on new page for fussy pdf-readers new_graphic_state.color_space = {} if new_graphic_state page_options.merge!(:graphic_state => new_graphic_state) end - merge_template_options(page_options, options) if options[:template] state.page = PDF::Core::Page.new(self, page_options) apply_margin_options(options) generate_margin_box @@ -277,13 +273,11 @@ if last_page && (last_page.size != state.page.size || last_page.layout != state.page.layout) @bounding_box = @margin_box end - state.page.new_content_stream if options[:template] - use_graphic_settings(options[:template]) - forget_text_rendering_mode! if options[:template] + use_graphic_settings unless options[:orphan] state.insert_page(state.page, @page_number) @page_number += 1 @@ -649,16 +643,11 @@ !!state.compress end private - def merge_template_options(page_options, options) - object_id = state.store.import_page(options[:template], options[:template_page] || 1) - page_options.merge!(:object_id => object_id, :page_template => true) - end - # setting override_settings to true ensures that a new graphic state does not end up using - # previous settings especially from imported template streams + # previous settings. def use_graphic_settings(override_settings = false) set_fill_color if current_fill_color != "000000" || override_settings set_stroke_color if current_stroke_color != "000000" || override_settings write_line_width if line_width != 1 || override_settings write_stroke_cap_style if cap_style != :butt || override_settings