Skip to content

Commit

Permalink
page numbers need to pass an explicit height to text box or else page…
Browse files Browse the repository at this point in the history
… numbers will only be able to be printed within the bounds, not out in the margin. updated the manual to match the new page numbering api
  • Loading branch information
Daniel Nelson committed Mar 29, 2011
1 parent 9e6a865 commit 70b7b4a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
4 changes: 3 additions & 1 deletion lib/prawn/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def @bounding_box.move_past_bottom
# Parameters are:
#
# <tt>string</tt>:: Template string for page number wording.
# Should include '<page>' and '<total>'.
# Should include '<page>' and, optionally, '<total>'.
# <tt>options</tt>:: A hash for page numbering and text box options.
# <tt>:page_filter</tt>:: A filter to specify which pages to place page numbers on.
# Refer to the method 'page_match?'
Expand Down Expand Up @@ -551,6 +551,8 @@ def number_pages(string, options={})
page_filter = opts.delete(:page_filter)
total_pages = opts.delete(:total_pages)
txtcolor = opts.delete(:color)
# An explicit height so that we can draw page numbers in the margins
opts[:height] = 50

start_count = false
pseudopage = 0
Expand Down
38 changes: 32 additions & 6 deletions manual/repeatable_content/page_numbering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
# your document. It should be called towards the end of the document since
# pages created after the call won't be numbered.
#
# It accepts a string with two optional tags: <code><page></code> will be
# replaced by the page number and <code>total</code> by the total number of
# pages at the moment of the call. The string will be rendered starting at
# the array coordinates provided by the second argument.
# It accepts a string and a hash of options:
#
# <code>start_count_at</code> is the value from which to start numbering pages
#
# <code>total_pages</code> If provided, will replace <code>total</code> with
# the value given. Useful for overriding the total number of pages when using the
# start_count_at option.
#
# <code>page_filter</code>, which is one of: <code>:all</code>, <code>:odd</code>,
# <code>:even</code>, an array, a range, or a Proc that receives the page number
# as an argument and should return true if the page number should be printed on
# that page.
#
# <code>color</code> which accepts the same values as <code>fill_color</code>
#
# As well as any option accepted by <code>text_box</code>
#
require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))
Expand All @@ -20,8 +32,22 @@
start_new_page
text "Here comes yet another page."
end

number_pages("<page> in a total of <total>", [bounds.right - 100, 0])

string = "page <page> of <total>"
# Green page numbers 1 to 7
options = { :at => [bounds.right - 150, 0],
:width => 150,
:align => :right,
:page_filter => (1..7),
:start_count_at => 1,
:color => "007700" }
number_pages string, options

# Gray page numbers from 8 on up
options[:page_filter] = lambda{ |pg| pg > 7}
options[:start_count_at] = 8
options[:color] = "333333"
number_pages string, options

start_new_page
text "See. This page isn't numbered and doesn't count towards the total."
Expand Down
44 changes: 22 additions & 22 deletions spec/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@

it "replaces the '<page>' string with the proper page number" do
@pdf.start_new_page
@pdf.expects(:text_box).with("1, test", {})
@pdf.expects(:text_box).with("1, test", { :height => 50 })
@pdf.number_pages "<page>, test", {:page_filter => :all}
end

it "replaces the '<total>' string with the total page count" do
@pdf.start_new_page
@pdf.expects(:text_box).with("test, 1", {})
@pdf.expects(:text_box).with("test, 1", { :height => 50 })
@pdf.number_pages "test, <total>", {:page_filter => :all}
end

Expand All @@ -547,8 +547,8 @@
it "increments the pages" do
2.times { @pdf.start_new_page }
options = {:page_filter => :all, :start_count_at => startat}
@pdf.expects(:text_box).with("#{startat} 2", {})
@pdf.expects(:text_box).with("#{startat+1} 2", {})
@pdf.expects(:text_box).with("#{startat} 2", { :height => 50 })
@pdf.expects(:text_box).with("#{startat+1} 2", { :height => 50 })
@pdf.number_pages "<page> <total>", options
end
end
Expand All @@ -559,9 +559,9 @@
it "defaults to start at page 1" do
3.times { @pdf.start_new_page }
options = {:page_filter => :all, :start_count_at => val}
@pdf.expects(:text_box).with("1 3", {})
@pdf.expects(:text_box).with("2 3", {})
@pdf.expects(:text_box).with("3 3", {})
@pdf.expects(:text_box).with("1 3", { :height => 50 })
@pdf.expects(:text_box).with("2 3", { :height => 50 })
@pdf.expects(:text_box).with("3 3", { :height => 50 })
@pdf.number_pages "<page> <total>", options
end
end
Expand All @@ -571,8 +571,8 @@
context "total_pages option" do
it "allows the total pages count to be overridden" do
2.times { @pdf.start_new_page }
@pdf.expects(:text_box).with("1 10", {})
@pdf.expects(:text_box).with("2 10", {})
@pdf.expects(:text_box).with("1 10", { :height => 50 })
@pdf.expects(:text_box).with("2 10", { :height => 50 })
@pdf.number_pages "<page> <total>", :page_filter => :all, :total_pages => 10
end
end
Expand All @@ -581,9 +581,9 @@
context "such as :odd" do
it "increments the pages" do
3.times { @pdf.start_new_page }
@pdf.expects(:text_box).with("1 3", {})
@pdf.expects(:text_box).with("3 3", {})
@pdf.expects(:text_box).with("2 3", {}).never
@pdf.expects(:text_box).with("1 3", { :height => 50 })
@pdf.expects(:text_box).with("3 3", { :height => 50 })
@pdf.expects(:text_box).with("2 3", { :height => 50 }).never
@pdf.number_pages "<page> <total>", :page_filter => :odd
end
end
Expand All @@ -600,23 +600,23 @@
context "such as :odd and 7" do
it "increments the pages" do
3.times { @pdf.start_new_page }
@pdf.expects(:text_box).with("1 3", {}).never
@pdf.expects(:text_box).with("5 3", {}) # page 1
@pdf.expects(:text_box).with("6 3", {}).never # page 2
@pdf.expects(:text_box).with("7 3", {}) # page 3
@pdf.expects(:text_box).with("1 3", { :height => 50 }).never
@pdf.expects(:text_box).with("5 3", { :height => 50 }) # page 1
@pdf.expects(:text_box).with("6 3", { :height => 50 }).never # page 2
@pdf.expects(:text_box).with("7 3", { :height => 50 }) # page 3
@pdf.number_pages "<page> <total>", :page_filter => :odd, :start_count_at => 5
end
end
context "some crazy proc and 2" do
it "increments the pages" do
6.times { @pdf.start_new_page }
options = {:page_filter => lambda {|p| p != 2 && p != 5}, :start_count_at => 4}
@pdf.expects(:text_box).with("4 6", {}) # page 1
@pdf.expects(:text_box).with("5 6", {}).never # page 2
@pdf.expects(:text_box).with("6 6", {}) # page 3
@pdf.expects(:text_box).with("7 6", {}) # page 4
@pdf.expects(:text_box).with("8 6", {}).never # page 5
@pdf.expects(:text_box).with("9 6", {}) # page 6
@pdf.expects(:text_box).with("4 6", { :height => 50 }) # page 1
@pdf.expects(:text_box).with("5 6", { :height => 50 }).never # page 2
@pdf.expects(:text_box).with("6 6", { :height => 50 }) # page 3
@pdf.expects(:text_box).with("7 6", { :height => 50 }) # page 4
@pdf.expects(:text_box).with("8 6", { :height => 50 }).never # page 5
@pdf.expects(:text_box).with("9 6", { :height => 50 }) # page 6
@pdf.number_pages "<page> <total>", options
end
end
Expand Down

0 comments on commit 70b7b4a

Please sign in to comment.