Skip to content

Commit

Permalink
Fix box layouting to take small floating point differences into account
Browse files Browse the repository at this point in the history
When fitting or splitting boxes their width/height is often compared to
other widths/heights. Due to small floating point differences a box that
fits might be treated as one that doesn't fit.

This can be fixed by using a special float comparision method in such
cases.
  • Loading branch information
gettalong committed Aug 30, 2022
1 parent b67b450 commit 73ff347
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
the document's root page tree node
* HexaPDF::Type::Page#perform_validation to only do the validation if the page
is part of the document's page tree
* Box layouting to take small floating point differences into account


## 0.24.1 - 2022-08-11
Expand Down
7 changes: 6 additions & 1 deletion lib/hexapdf/layout/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# commercial licenses are available at <https://gettalong.at/hexapdf/>.
#++
require 'hexapdf/layout/style'
require 'geom2d/utils'

module HexaPDF
module Layout
Expand Down Expand Up @@ -76,6 +77,8 @@ module Layout
# which should just draw the content.
class Box

include Geom2D::Utils

# Creates a new Box object, using the provided block as drawing block (see ::new).
#
# If +content_box+ is +true+, the width and height are taken to mean the content width and
Expand Down Expand Up @@ -183,7 +186,9 @@ def fit(available_width, available_height, _frame)
def split(available_width, available_height, frame)
if @fit_successful
[self, nil]
elsif (style.position != :flow && (@width > available_width || @height > available_height)) ||
elsif (style.position != :flow &&
(float_compare(@width, available_width) > 0 ||
float_compare(@height, available_height) > 0)) ||
content_height == 0 || content_width == 0
[nil, self]
else
Expand Down
3 changes: 2 additions & 1 deletion lib/hexapdf/layout/image_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def fit(available_width, available_height, _frame)
@height = image_height * ratio + rh
end

@width <= available_width && @height <= available_height
@fit_successful = float_compare(@width, available_width) <= 0 &&
float_compare(@height, available_height) <= 0
end

private
Expand Down

0 comments on commit 73ff347

Please sign in to comment.