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

==(other) click to toggle source

Compares this rectangle to other like in Object#== but also allows comparison to simple arrays if the rectangle is a direct object.

Calls superclass method HexaPDF::Object#==
# File lib/hexapdf/rectangle.rb, line 118
def ==(other)
  super || (other.kind_of?(Array) && !indirect? && other == data.value)
end
bottom() click to toggle source

Returns the y-coordinate of the bottom-left corner.

# File lib/hexapdf/rectangle.rb, line 77
def bottom
  self[1]
end
bottom=(y) click to toggle source

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
height() click to toggle source

Returns the height of the rectangle.

# File lib/hexapdf/rectangle.rb, line 107
def height
  self[3] - self[1]
end
height=(val) click to toggle source

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
left() click to toggle source

Returns the x-coordinate of the bottom-left corner.

# File lib/hexapdf/rectangle.rb, line 57
def left
  self[0]
end
left=(x) click to toggle source

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
right() click to toggle source

Returns the x-coordinate of the top-right corner.

# File lib/hexapdf/rectangle.rb, line 67
def right
  self[2]
end
right=(x) click to toggle source

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
top() click to toggle source

Returns the y-coordinate of the top-right corner.

# File lib/hexapdf/rectangle.rb, line 87
def top
  self[3]
end
top=(y) click to toggle source

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
width() click to toggle source

Returns the width of the rectangle.

# File lib/hexapdf/rectangle.rb, line 97
def width
  self[2] - self[0]
end
width=(val) click to toggle source

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

after_data_change() click to toggle source

Ensures that the value is an array containing four numbers that specify the bottom left and top right corner.

Calls superclass method 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