Class: PDF::Reader::Rectangle
- Inherits:
-
Object
- Object
- PDF::Reader::Rectangle
- Defined in:
- lib/pdf/reader/rectangle.rb
Overview
PDFs represent rectangles all over the place. They’re 4 element arrays, like this:
[A, B, C, D]
Four element arrays are yucky to work with though, so here’s a class that’s better. Initialize it with the 4 elements, and get utility functions (width, height, etc) for free.
By convention the first two elements are x1, y1, the co-ords for the bottom left corner of the rectangle. The third and fourth elements are x2, y2, the co-ords for the top left corner of the rectangle. It’s valid for the alternative corners to be used though, so we don’t assume which is which.
Instance Attribute Summary collapse
-
#bottom_left ⇒ Object
readonly
Returns the value of attribute bottom_left.
-
#bottom_right ⇒ Object
readonly
Returns the value of attribute bottom_right.
-
#top_left ⇒ Object
readonly
Returns the value of attribute top_left.
-
#top_right ⇒ Object
readonly
Returns the value of attribute top_right.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #apply_rotation(degrees) ⇒ Object
- #height ⇒ Object
-
#initialize(x1, y1, x2, y2) ⇒ Rectangle
constructor
A new instance of Rectangle.
-
#to_a ⇒ Object
A pdf-style 4-number array.
- #width ⇒ Object
Constructor Details
#initialize(x1, y1, x2, y2) ⇒ Rectangle
Returns a new instance of Rectangle.
25 26 27 |
# File 'lib/pdf/reader/rectangle.rb', line 25 def initialize(x1, y1, x2, y2) set_corners(x1, y1, x2, y2) end |
Instance Attribute Details
#bottom_left ⇒ Object (readonly)
Returns the value of attribute bottom_left.
23 24 25 |
# File 'lib/pdf/reader/rectangle.rb', line 23 def bottom_left @bottom_left end |
#bottom_right ⇒ Object (readonly)
Returns the value of attribute bottom_right.
23 24 25 |
# File 'lib/pdf/reader/rectangle.rb', line 23 def bottom_right @bottom_right end |
#top_left ⇒ Object (readonly)
Returns the value of attribute top_left.
23 24 25 |
# File 'lib/pdf/reader/rectangle.rb', line 23 def top_left @top_left end |
#top_right ⇒ Object (readonly)
Returns the value of attribute top_right.
23 24 25 |
# File 'lib/pdf/reader/rectangle.rb', line 23 def top_right @top_right end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/pdf/reader/rectangle.rb', line 29 def ==(other) to_a == other.to_a end |
#apply_rotation(degrees) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pdf/reader/rectangle.rb', line 51 def apply_rotation(degrees) return if degrees != 90 && degrees != 180 && degrees != 270 if degrees == 90 new_x1 = bottom_left.x new_y1 = bottom_left.y - width new_x2 = bottom_left.x + height new_y2 = bottom_left.y elsif degrees == 180 new_x1 = bottom_left.x - width new_y1 = bottom_left.y - height new_x2 = bottom_left.x new_y2 = bottom_left.y elsif degrees == 270 new_x1 = bottom_left.x - height new_y1 = bottom_left.y new_x2 = bottom_left.x new_y2 = bottom_left.y + width end set_corners(new_x1, new_y1, new_x2, new_y2) end |
#height ⇒ Object
33 34 35 |
# File 'lib/pdf/reader/rectangle.rb', line 33 def height top_right.y - bottom_right.y end |
#to_a ⇒ Object
A pdf-style 4-number array
42 43 44 45 46 47 48 49 |
# File 'lib/pdf/reader/rectangle.rb', line 42 def to_a [ bottom_left.x, bottom_left.y, top_right.x, top_right.y, ] end |
#width ⇒ Object
37 38 39 |
# File 'lib/pdf/reader/rectangle.rb', line 37 def width bottom_right.x - bottom_left.x end |