class HexaPDF::Rectangle
Implementation of the PDF rectangle data structure.
Rectangles are used for describing page and bounding boxes. They are represented by arrays of four numbers specifying the (x,y) coordinates of any diagonally opposite corners.
This class simplifies the usage of rectangles by automatically normalizing the coordinates so that they are in the order:
[left, bottom, right, top]
where left
is the bottom left x-coordinate, bottom
is the bottom left y-coordinate, right
is the top right x-coordinate and top
is the top right y-coordinate.
See: PDF1.7 s7.9.5
Public Instance Methods
Compares this rectangle to other
like in Object#== but also allows comparison to simple arrays if the rectangle is a direct object.
# File lib/hexapdf/rectangle.rb, line 118 def ==(other) super || (other.kind_of?(Array) && !indirect? && other == data.value) end
Returns the y-coordinate of the bottom-left corner.
# File lib/hexapdf/rectangle.rb, line 77 def bottom self[1] end
Sets the y-coordinate of the bottom-left corner to the given value.
# File lib/hexapdf/rectangle.rb, line 82 def bottom=(y) value[1] = y end
Returns the height of the rectangle.
# File lib/hexapdf/rectangle.rb, line 107 def height self[3] - self[1] end
Sets the height of the rectangle to the given value.
# File lib/hexapdf/rectangle.rb, line 112 def height=(val) self[3] = self[1] + val end
Returns the x-coordinate of the bottom-left corner.
# File lib/hexapdf/rectangle.rb, line 57 def left self[0] end
Sets the x-coordinate of the bottom-left corner to the given value.
# File lib/hexapdf/rectangle.rb, line 62 def left=(x) value[0] = x end
Returns the x-coordinate of the top-right corner.
# File lib/hexapdf/rectangle.rb, line 67 def right self[2] end
Sets the x-coordinate of the top-right corner to the given value.
# File lib/hexapdf/rectangle.rb, line 72 def right=(x) value[2] = x end
Returns the y-coordinate of the top-right corner.
# File lib/hexapdf/rectangle.rb, line 87 def top self[3] end
Sets the y-coordinate of the top-right corner to the given value.
# File lib/hexapdf/rectangle.rb, line 92 def top=(y) value[3] = y end
Returns the width of the rectangle.
# File lib/hexapdf/rectangle.rb, line 97 def width self[2] - self[0] end
Sets the width of the rectangle to the given value.
# File lib/hexapdf/rectangle.rb, line 102 def width=(val) self[2] = self[0] + val end
Private Instance Methods
Ensures that the value is an array containing four numbers that specify the bottom left and top right corner.
HexaPDF::Object#after_data_change
# File lib/hexapdf/rectangle.rb, line 126 def after_data_change super unless value.size == 4 && all? {|v| v.kind_of?(Numeric) } raise ArgumentError, "A PDF rectangle structure must contain an array of four numbers" end self[0], self[2] = self[2], self[0] if self[0] > self[2] self[1], self[3] = self[3], self[1] if self[1] > self[3] end