Skip to content

Commit

Permalink
don't comparing floating-point values directly because of its errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn authored and bradediger committed Apr 18, 2011
1 parent e2deb80 commit 0c00a3b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/prawn/table.rb
Expand Up @@ -121,6 +121,7 @@ def initialize(data, document, options={}, &block)
@pdf = document
@cells = make_cells(data)
@header = false
@epsilon = 1e-9
options.each { |k, v| send("#{k}=", v) }

if block
Expand Down Expand Up @@ -313,27 +314,27 @@ def draw
#
def column_widths
@column_widths ||= begin
if width < cells.min_width
if width - cells.min_width < -epsilon
raise Errors::CannotFit,
"Table's width was set too small to contain its contents " +
"(min width #{cells.min_width}, requested #{width})"
end

if width > cells.max_width
if width - cells.max_width > epsilon
raise Errors::CannotFit,
"Table's width was set larger than its contents' maximum width " +
"(max width #{cells.max_width}, requested #{width})"
end

if width < natural_width
if width - natural_width < -epsilon
# Shrink the table to fit the requested width.
f = (width - cells.min_width).to_f / (natural_width - cells.min_width)

(0...column_length).map do |c|
min, nat = column(c).min_width, column(c).width
(f * (nat - min)) + min
end
elsif width > natural_width
elsif width - natural_width > epsilon
# Expand the table to fit the requested width.
f = (width - cells.width).to_f / (cells.max_width - cells.width)

Expand Down Expand Up @@ -457,6 +458,11 @@ def position_cells
y_positions.each_with_index { |y, i| row(i).y = y }
end

private

def epsilon
@epsilon
end
end


Expand Down

0 comments on commit 0c00a3b

Please sign in to comment.