Permalink
Browse files

don't comparing floating-point values directly because of its errors.

  • Loading branch information...
1 parent e2deb80 commit 0c00a3baf80379ddc99f6e76727453876bfdfafe @mrkn mrkn committed with bradediger Apr 12, 2011
Showing with 10 additions and 4 deletions.
  1. +10 −4 lib/prawn/table.rb
View
@@ -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
@@ -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)
@@ -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

0 comments on commit 0c00a3b

Please sign in to comment.