Skip to content

Commit

Permalink
Merge pull request prawnpdf#554 from gettalong/ruby-warning-fixes
Browse files Browse the repository at this point in the history
Fixed many Ruby warnings when running prawn under 'ruby -w'
  • Loading branch information
bradediger committed Oct 8, 2013
2 parents 751ab4c + 54ce8a1 commit 0479610
Show file tree
Hide file tree
Showing 29 changed files with 83 additions and 99 deletions.
4 changes: 4 additions & 0 deletions lib/prawn/compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def codepoints(&block)
unpack("U*")
end
end

def each_codepoint(&block)
unpack("U*").each(&block)
end
end

if "".respond_to?(:encode)
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/core/document_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def normalize_metadata(options)
options[:info][:Creator] ||= "Prawn"
options[:info][:Producer] ||= "Prawn"

info = options[:info]
options[:info]
end

def insert_page(page, page_number)
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/core/filter_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def normalized
alias_method :to_a, :normalized

def names
@list.map do |(name, params)|
@list.map do |(name, _)|
name
end
end

def decode_params
@list.map do |(name, params)|
@list.map do |(_, params)|
params
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/prawn/core/object_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ def load_object_graph(hash, object)
unless @loaded_objects.has_key?(object.id)
@loaded_objects[object.id] = ref(nil)
new_obj = load_object_graph(hash, hash[object])
if new_obj.kind_of?(PDF::Reader::Stream)
stream_dict = load_object_graph(hash, new_obj.hash)
@loaded_objects[object.id].data = stream_dict
@loaded_objects[object.id] << new_obj.data
if new_obj.kind_of?(PDF::Reader::Stream)
stream_dict = load_object_graph(hash, new_obj.hash)
@loaded_objects[object.id].data = stream_dict
@loaded_objects[object.id] << new_obj.data
else
@loaded_objects[object.id].data = new_obj
end
Expand Down
16 changes: 9 additions & 7 deletions lib/prawn/core/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Page #:nodoc:

include Prawn::Core::Page::GraphicsState

attr_accessor :document, :content, :dictionary, :margins, :stack
attr_accessor :document, :margins, :stack
attr_writer :content, :dictionary

def initialize(document, options={})
@document = document
Expand All @@ -32,7 +33,7 @@ def initialize(document, options={})
end

def layout
return @layout if @layout
return @layout if defined?(@layout) && @layout

mb = dictionary.data[:MediaBox]
if mb[3] > mb[2]
Expand All @@ -43,7 +44,7 @@ def layout
end

def size
@size || dimensions[2,2]
defined?(@size) && @size || dimensions[2,2]
end

def in_stamp_stream?
Expand Down Expand Up @@ -89,7 +90,7 @@ def new_content_stream
end

def dictionary
@stamp_dictionary || document.state.store[@dictionary]
defined?(@stamp_dictionary) && @stamp_dictionary || document.state.store[@dictionary]
end

def resources
Expand Down Expand Up @@ -172,6 +173,10 @@ def init_new_page(options)
@size = options[:size] || "LETTER"
@layout = options[:layout] || :portrait

@stamp_stream = nil
@stamp_dictionary = nil
@imported_page = false

@content = document.ref({})
content << "q" << "\n"
@dictionary = document.ref(:Type => :Page,
Expand All @@ -180,9 +185,6 @@ def init_new_page(options)
:Contents => content)

resources[:ProcSet] = [:PDF, :Text, :ImageB, :ImageC, :ImageI]

@stamp_stream = nil
@stamp_dictionary = nil
end

# some entries in the Page dict can be inherited from parent Pages dicts.
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/core/pdf_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Core #:nodoc:
if "".respond_to?(:encode)
# Ruby 1.9+
def utf8_to_utf16(str)
utf16 = "\xFE\xFF".force_encoding("UTF-16BE") + str.encode("UTF-16BE")
"\xFE\xFF".force_encoding("UTF-16BE") + str.encode("UTF-16BE")
end

# encodes any string into a hex representation. The result is a string
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/core/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def replace(other_ref)

# Marks this and all referenced objects live, recursively.
def mark_live
return if @live
return if defined?(@live) && @live
@live = true
referenced_objects.each { |o| o.mark_live }
end
Expand Down
19 changes: 8 additions & 11 deletions lib/prawn/core/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def process_text_options(options)
# Defaults to true
#
def default_kerning?
return true if @default_kerning.nil?
return true if !defined?(@default_kerning)
@default_kerning
end

Expand Down Expand Up @@ -81,8 +81,7 @@ def default_kerning(boolean)
#
def default_leading(number=nil)
if number.nil?
return 0 if @default_leading.nil?
@default_leading
defined?(@default_leading) && @default_leading || 0
else
@default_leading = number
end
Expand Down Expand Up @@ -112,8 +111,7 @@ def default_leading(number=nil)
#
def text_direction(direction=nil)
if direction.nil?
return :ltr if @text_direction.nil?
@text_direction
defined?(@text_direction) && @text_direction || :ltr
else
@text_direction = direction
end
Expand Down Expand Up @@ -155,8 +153,7 @@ def text_direction(direction=nil)
#
def fallback_fonts(fallback_fonts=nil)
if fallback_fonts.nil?
return [] if @fallback_fonts.nil?
@fallback_fonts
defined?(@fallback_fonts) && @fallback_fonts || []
else
@fallback_fonts = fallback_fonts
end
Expand Down Expand Up @@ -188,11 +185,11 @@ def fallback_fonts(fallback_fonts=nil)
# with templates. If left in :unknown, the first text command will force
# an assertion to :fill.
def text_rendering_mode(mode=nil)
return @text_rendering_mode || :fill if mode.nil?
return (defined?(@text_rendering_mode) && @text_rendering_mode || :fill) if mode.nil?
unless MODES.key?(mode)
raise ArgumentError, "mode must be between one of #{MODES.keys.join(', ')} (#{mode})"
end
original_mode = @text_rendering_mode || :fill
original_mode = self.text_rendering_mode
if original_mode == :unknown
original_mode = :fill
add_content "\n#{MODES[:fill]} Tr"
Expand All @@ -217,7 +214,7 @@ def forget_text_rendering_mode!
# For veritical text, a positive value will decrease the space.
#
def character_spacing(amount=nil)
return @character_spacing || 0 if amount.nil?
return defined?(@character_spacing) && @character_spacing || 0 if amount.nil?
original_character_spacing = character_spacing
if original_character_spacing == amount
yield
Expand All @@ -235,7 +232,7 @@ def character_spacing(amount=nil)
# For veritical text, a positive value will decrease the space.
#
def word_spacing(amount=nil)
return @word_spacing || 0 if amount.nil?
return defined?(@word_spacing) && @word_spacing || 0 if amount.nil?
original_word_spacing = word_spacing
if original_word_spacing == amount
yield
Expand Down
6 changes: 3 additions & 3 deletions lib/prawn/core/text/formatted/arranger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ def set_fragment_measurements(fragment)
end

def set_line_measurement_maximums(fragment)
@max_line_height = [@max_line_height, fragment.line_height].compact.max
@max_descender = [@max_descender, fragment.descender].compact.max
@max_ascender = [@max_ascender, fragment.ascender].compact.max
@max_line_height = [defined?(@max_line_height) && @max_line_height, fragment.line_height].compact.max
@max_descender = [defined?(@max_descender) && @max_descender, fragment.descender].compact.max
@max_ascender = [defined?(@max_ascender) && @max_ascender, fragment.ascender].compact.max
end

end
Expand Down
8 changes: 4 additions & 4 deletions lib/prawn/core/text/formatted/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def print_line

accumulated_width = 0
fragments_this_line.reverse! if @direction == :rtl
fragments_this_line.each do |fragment|
fragment.default_direction = @direction
format_and_draw_fragment(fragment, accumulated_width,
fragments_this_line.each do |fragment_this_line|
fragment_this_line.default_direction = @direction
format_and_draw_fragment(fragment_this_line, accumulated_width,
@line_wrap.width, word_spacing)
accumulated_width += fragment.width
accumulated_width += fragment_this_line.width
end

if "".respond_to?(:force_encoding)
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/document/page_geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ module PageGeometry
"LETTER" => [612.00, 792.00],
"TABLOID" => [792.00, 1224.00] }

end
end
end
end
2 changes: 1 addition & 1 deletion lib/prawn/document/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def take_snapshot
# monotonically as data is added to the document, so we share that
# between the old and new copies.
{:page_content => state.page.content.deep_copy,
:current_page => state.page.dictionary.deep_copy(share=[:Parent]),
:current_page => state.page.dictionary.deep_copy([:Parent]),
:bounds => bounds.deep_copy,
:page_number => page_number,
:page_kids => state.store.pages.data[:Kids].compact.map{|kid| kid.identifier},
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/document/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def span(width, options={})
when :left
margin_box.absolute_left
when :center
margin_box.absolute_left + margin_box.width / 2.0 - width /2.0
margin_box.absolute_left + margin_box.width / 2.0 - width / 2.0
when :right
margin_box.absolute_right - width
when Numeric
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def load_mapping
RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
File.open(@mapping_file, mode) do |f|
f.each do |l|
m, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
_, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
self.class.mapping["0x#{unicode}".hex] = "0x#{single_byte}".hex if single_byte
end
end
Expand Down
8 changes: 0 additions & 8 deletions lib/prawn/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,6 @@ def line_gap
@line_gap / 1000.0 * size
end

def identifier_for(subset)
"#{@identifier}.#{subset}".to_sym
end

def inspect
"#{self.class.name}< #{name}: #{size} >"
end

# Normalizes the encoding of the string to an encoding supported by the
# font. The string is expected to be UTF-8 going in. It will be re-encoded
# and the new string will be returned. For an in-place (destructive)
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/font/afm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def kern(string)
kerned = [[]]
last_byte = nil

string.bytes do |byte|
string.each_byte do |byte|
if k = last_byte && @kern_pair_table[[last_byte, byte]]
kerned << -k << [byte]
else
Expand Down
4 changes: 2 additions & 2 deletions lib/prawn/font/ttf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def cmap
def kern(string)
a = []

string.codepoints do |r|
string.each_codepoint do |r|
if a.empty?
a << [r]
elsif (kern = kern_pairs_table[[cmap[a.last.last], cmap[r]]])
Expand Down Expand Up @@ -287,7 +287,7 @@ def embed(reference, subset)
map = @subsets[subset].to_unicode_map

ranges = [[]]
lines = map.keys.sort.inject("") do |s, code|
map.keys.sort.inject("") do |s, code|
ranges << [] if ranges.last.length >= 100
unicode = map[code]
ranges.last << "<%02x><%04x>" % [code, unicode]
Expand Down
1 change: 0 additions & 1 deletion lib/prawn/graphics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ def rounded_polygon(radius, *points)
# requires a radius to define bezier curve and three points. The first two points define
# the line segment and the third point helps define the curve for the vertex.
def rounded_vertex(radius, *points)
x0,y0,x1,y1,x2,y2 = points.flatten
radial_point_1 = point_on_line(radius, points[0], points[1])
bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
radial_point_2 = point_on_line(radius, points[2], points[1])
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/graphics/patterns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def gradient(*args)
:Extend => [true, true],
})

shading_pattern = ref!({
ref!({
:PatternType => 2, # shading pattern
:Shading => shading,
:Matrix => [1, 0,
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/images/jpg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def initialize(data)
break
end

buffer = data.read(length - 2)
data.read(length - 2)
end
end

Expand Down
17 changes: 8 additions & 9 deletions lib/prawn/images/png.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,28 +290,27 @@ def unfilter_image_data
case filter
when 0 # None
when 1 # Sub
row_data.each_with_index do |byte, index|
row_data.each_with_index do |row_byte, index|
left = index < pixel_bytes ? 0 : row_data[index - pixel_bytes]
row_data[index] = (byte + left) % 256
#p [byte, left, row_data[index]]
row_data[index] = (row_byte + left) % 256
end
when 2 # Up
row_data.each_with_index do |byte, index|
row_data.each_with_index do |row_byte, index|
col = (index / pixel_bytes).floor
upper = row == 0 ? 0 : pixels[row-1][col][index % pixel_bytes]
row_data[index] = (upper + byte) % 256
row_data[index] = (upper + row_byte) % 256
end
when 3 # Average
row_data.each_with_index do |byte, index|
row_data.each_with_index do |row_byte, index|
col = (index / pixel_bytes).floor
upper = row == 0 ? 0 : pixels[row-1][col][index % pixel_bytes]
left = index < pixel_bytes ? 0 : row_data[index - pixel_bytes]

row_data[index] = (byte + ((left + upper)/2).floor) % 256
row_data[index] = (row_byte + ((left + upper)/2).floor) % 256
end
when 4 # Paeth
left = upper = upper_left = nil
row_data.each_with_index do |byte, index|
row_data.each_with_index do |row_byte, index|
col = (index / pixel_bytes).floor

left = index < pixel_bytes ? 0 : row_data[index - pixel_bytes]
Expand All @@ -336,7 +335,7 @@ def unfilter_image_data
upper_left
end

row_data[index] = (byte + paeth) % 256
row_data[index] = (row_byte + paeth) % 256
end
else
raise ArgumentError, "Invalid filter algorithm #{filter}"
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/outline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def to_hash
hash = { :Title => title,
:Parent => parent,
:Count => closed ? -count : count }
[{:First => first}, {:Last => last}, {:Next => @next},
[{:First => first}, {:Last => last}, {:Next => defined?(@next) && @next},
{:Prev => prev}, {:Dest => dest}].each do |h|
unless h.values.first.nil?
hash.merge!(h)
Expand Down
Loading

0 comments on commit 0479610

Please sign in to comment.