lib/prawn/document.rb in prawn-2.0.1 vs lib/prawn/document.rb in prawn-2.0.2
- old
+ new
@@ -12,11 +12,10 @@
require_relative "document/column_box"
require_relative "document/internals"
require_relative "document/span"
module Prawn
-
# The Prawn::Document class is how you start creating a PDF document.
#
# There are three basic ways you can instantiate PDF Documents in Prawn, they
# are through assignment, implicit block or explicit block. Below is an exmple
# of each type, each example does exactly the same thing, makes a PDF document
@@ -100,11 +99,11 @@
end
# @group Stable Attributes
attr_accessor :margin_box
- attr_reader :margins, :y
+ attr_reader :margins, :y
attr_accessor :page_number
# @group Extension Attributes
attr_accessor :text_formatter
@@ -136,12 +135,12 @@
# # self here is left alone
# pdf.font "Times-Roman"
# pdf.draw_text content, :at => [200,720], :size => 32
# end
#
- def self.generate(filename,options={},&block)
- pdf = new(options,&block)
+ def self.generate(filename, options = {}, &block)
+ pdf = new(options, &block)
pdf.render_file(filename)
end
# Creates a new PDF Document. The following options are available (with
# the default values marked in [])
@@ -187,21 +186,21 @@
# pdf = Prawn::Document.new(:page_size => [200, 300])
#
# # New document, with background
# pdf = Prawn::Document.new(:background => "#{Prawn::DATADIR}/images/pigs.jpg")
#
- def initialize(options={},&block)
+ def initialize(options = {}, &block)
options = options.dup
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)
+ self.state = PDF::Core::DocumentState.new(options)
+ self.state.populate_pages_from_store(self)
renderer.min_version(state.store.min_version) if state.store.min_version
renderer.min_version(1.6) if options[:print_scaling] == :none
@background = options[:background]
@@ -244,17 +243,21 @@
last_page_size = last_page.size
last_page_layout = last_page.layout
last_page_margins = last_page.margins.dup
end
- page_options = {:size => options[:size] || last_page_size,
- :layout => options[:layout] || last_page_layout,
- :margins => last_page_margins}
+ page_options = {
+ :size => options[:size] || last_page_size,
+ :layout => options[:layout] || last_page_layout,
+ :margins => last_page_margins
+ }
if last_page
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
+
+ # 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
state.page = PDF::Core::Page.new(self, page_options)
@@ -298,11 +301,11 @@
#
# 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]
+ state.page = state.pages[k - 1]
generate_margin_box
@y = @bounding_box.absolute_top
end
def y=(new_y)
@@ -464,11 +467,10 @@
move_down(y)
yield
move_down(y)
end
-
# Indents the specified number of PDF points for the duration of the block
#
# pdf.text "some text"
# pdf.indent(20) do
# pdf.text "This is indented 20 points"
@@ -516,22 +518,24 @@
# :at => [bounds.right - 50, 0],
# :align => :right,
# :size => 14}
# end
#
- def number_pages(string, options={})
+ def number_pages(string, options = {})
opts = options.dup
start_count_at = opts.delete(:start_count_at).to_i
- page_filter = if opts.has_key?(:page_filter)
- opts.delete(:page_filter)
+
+ if opts.key?(:page_filter)
+ page_filter = opts.delete(:page_filter)
else
- :all
+ page_filter = :all
end
+
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 unless opts.has_key?(:height)
+ opts[:height] = 50 unless opts.key?(:height)
start_count = false
pseudopage = 0
(1..page_count).each do |p|
unless start_count
@@ -545,11 +549,11 @@
if page_match?(page_filter, p)
go_to_page(p)
# have to use fill_color here otherwise text reverts back to default fill color
fill_color txtcolor unless txtcolor.nil?
total_pages = total_pages.nil? ? page_count : total_pages
- str = string.gsub("<page>","#{pseudopage}").gsub("<total>","#{total_pages}")
+ str = string.gsub("<page>", "#{pseudopage}").gsub("<total>", "#{total_pages}")
text_box str, opts
start_count = true # increment page count as soon as first match found
end
pseudopage += 1 if start_count
end
@@ -566,24 +570,24 @@
# Raises CannotGroup if the provided content is too large to fit alone in
# the current page or column.
#
# @private
def group(*a, &b)
- 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"
+ fail 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"
end
# @private
def transaction
- raise NotImplementedError,
- "Document#transaction 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"
+ fail NotImplementedError,
+ "Document#transaction 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"
end
# Provides a way to execute a block of code repeatedly based on a
# page_filter.
#
@@ -597,26 +601,26 @@
def page_match?(page_filter, page_number)
case page_filter
when :all
true
when :odd
- page_number % 2 == 1
+ page_number.odd?
when :even
- page_number % 2 == 0
+ page_number.even?
when Range, Array
page_filter.include?(page_number)
when Proc
page_filter.call(page_number)
end
end
# @private
def mask(*fields)
- # Stores the current state of the named attributes, executes the block, and
- # then restores the original values after the block has executed.
- # -- I will remove the nodoc if/when this feature is a little less hacky
+ # Stores the current state of the named attributes, executes the block, and
+ # then restores the original values after the block has executed.
+ # -- I will remove the nodoc if/when this feature is a little less hacky
stored = {}
fields.each { |f| stored[f] = send(f) }
yield
fields.each { |f| send("#{f}=", stored[f]) }
end
@@ -632,22 +636,19 @@
end
## Internals. Don't depend on them!
# @private
- def state
- @internal_state
- end
+ attr_accessor :state
# @private
def page
state.page
end
private
-
# setting override_settings to true ensures that a new graphic state does not end up using
# 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
@@ -682,25 +683,25 @@
def apply_margin_options(options)
if options[:margin]
# Treat :margin as CSS shorthand with 1-4 values.
margin = Array(options[:margin])
- positions = { 4 => [0,1,2,3], 3 => [0,1,2,1],
- 2 => [0,1,0,1], 1 => [0,0,0,0] }[margin.length]
+ positions = { 4 => [0, 1, 2, 3], 3 => [0, 1, 2, 1],
+ 2 => [0, 1, 0, 1], 1 => [0, 0, 0, 0] }[margin.length]
- [:top, :right, :bottom, :left].zip(positions).each do |p,i|
+ [:top, :right, :bottom, :left].zip(positions).each do |p, i|
options[:"#{p}_margin"] ||= margin[i]
end
end
- [:left,:right,:top,:bottom].each do |side|
- if margin = options[:"#{side}_margin"]
- state.page.margins[side] = margin
- end
+ [:left, :right, :top, :bottom].each do |side|
+ if margin = options[:"#{side}_margin"]
+ state.page.margins[side] = margin
+ end
end
end
def font_metric_cache #:nodoc:
- @font_metric_cache ||= FontMetricCache.new( self )
+ @font_metric_cache ||= FontMetricCache.new(self)
end
end
end