lib/prawn/document.rb in prawn-2.2.2 vs lib/prawn/document.rb in prawn-2.3.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
# document.rb : Implements PDF document generation for Prawn
#
# Copyright April 2008, Gregory Brown. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
@@ -60,15 +62,15 @@
# @group Extension API
# 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, :background, :info,
- :text_formatter, :print_scaling
+ VALID_OPTIONS = %i[
+ page_size page_layout margin left_margin
+ right_margin top_margin bottom_margin skip_page_creation
+ compress background info
+ text_formatter print_scaling
].freeze
# 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.
@@ -267,11 +269,11 @@
new_graphic_state = last_page.graphic_state.dup
end
# 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
+ new_graphic_state&.color_space = {}
page_options[:graphic_state] = new_graphic_state
end
state.page = PDF::Core::Page.new(self, page_options)
@@ -302,10 +304,30 @@
state.on_page_create_action(self)
end
end
end
+ # Remove page of the document by index
+ #
+ # pdf = Prawn::Document.new
+ # pdf.page_count #=> 1
+ # 3.times { pdf.start_new_page }
+ # pdf.page_count #=> 4
+ # pdf.delete_page(-1)
+ # pdf.page_count #=> 3
+ #
+ def delete_page(index)
+ return false if index.abs > (state.pages.count - 1)
+
+ state.pages.delete_at(index)
+
+ state.store.pages.data[:Kids].delete_at(index)
+ state.store.pages.data[:Count] -= 1
+ @page_number -= 1
+ true
+ end
+
# Returns the number of pages in the document
#
# pdf = Prawn::Document.new
# pdf.page_count #=> 1
# 3.times { pdf.start_new_page }
@@ -318,13 +340,13 @@
# Re-opens the page with the given (1-based) page number so that you can
# draw on it.
#
# See Prawn::Document#number_pages for a sample usage of this capability.
#
- def go_to_page(k)
- @page_number = k
- state.page = state.pages[k - 1]
+ def go_to_page(page_number)
+ @page_number = page_number
+ state.page = state.pages[page_number - 1]
generate_margin_box
@y = @bounding_box.absolute_top
end
def y=(new_y)
@@ -367,17 +389,17 @@
end
# Renders the PDF document to string.
# Pass an open file descriptor to render to file.
#
- def render(*a, &b)
+ def render(*arguments, &block)
(1..page_count).each do |i|
go_to_page i
repeaters.each { |r| r.run(i) }
end
- renderer.render(*a, &b)
+ renderer.render(*arguments, &block)
end
# Renders the PDF document to file.
#
# pdf.render_file "foo.pdf"
@@ -435,19 +457,19 @@
end
# Moves up the document by n points relative to the current position inside
# the current bounding box.
#
- def move_up(n)
- self.y += n
+ def move_up(amount)
+ self.y += amount
end
# Moves down the document by n points relative to the current position
# inside the current bounding box.
#
- def move_down(n)
- self.y -= n
+ def move_down(amount)
+ self.y -= amount
end
# Moves down the document and then executes a block.
#
# pdf.text "some text"
@@ -595,11 +617,11 @@
#
# Raises CannotGroup if the provided content is too large to fit alone in
# the current page or column.
#
# @private
- def group(*_a)
+ def group(*_arguments)
raise NotImplementedError,
'Document#group has been disabled because its implementation ' \
'lead to corrupted documents whenever a page boundary was ' \
'crossed. We will try to work on reimplementing it in a ' \
'future release'
@@ -704,14 +726,14 @@
@margin_box.add_right_padding(old_margin_box.total_right_padding)
end
# we must update bounding box if not flowing from the previous page
#
- @bounding_box = @margin_box unless @bounding_box && @bounding_box.parent
+ @bounding_box = @margin_box unless @bounding_box&.parent
end
def apply_margin_options(options)
- sides = [:top, :right, :bottom, :left]
+ sides = %i[top right bottom left]
margin = Array(options[:margin])
# Treat :margin as CSS shorthand with 1-4 values.
positions = {
4 => [0, 1, 2, 3], 3 => [0, 1, 2, 1],