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
- #bottom_right ⇒ Object readonly
- #top_left ⇒ Object readonly
- #top_right ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #apply_rotation(degrees) ⇒ Object
-
#contains?(point) ⇒ Boolean
Is the point inside this rectangle? Nicer to read and use than contains_xy?(), but allocates more objects so be careful using on hot code paths.
-
#contains_xy?(x, y) ⇒ Boolean
Is the point inside this rectangle? Worse to read and use than contains?(), but allocates fewer objects so may be preferrable on hot code paths.
- #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.
36 37 38 39 40 41 42 |
# File 'lib/pdf/reader/rectangle.rb', line 36 def initialize(x1, y1, x2, y2) @bottom_left = Point.new(0,0) #: PDF::Reader::Point @bottom_right = Point.new(0,0) #: PDF::Reader::Point @top_left = Point.new(0,0) #: PDF::Reader::Point @top_right = Point.new(0,0) #: PDF::Reader::Point set_corners(x1, y1, x2, y2) end |
Instance Attribute Details
#bottom_left ⇒ Object (readonly)
24 25 26 |
# File 'lib/pdf/reader/rectangle.rb', line 24 def bottom_left @bottom_left end |
#bottom_right ⇒ Object (readonly)
27 28 29 |
# File 'lib/pdf/reader/rectangle.rb', line 27 def bottom_right @bottom_right end |
#top_left ⇒ Object (readonly)
30 31 32 |
# File 'lib/pdf/reader/rectangle.rb', line 30 def top_left @top_left end |
#top_right ⇒ Object (readonly)
33 34 35 |
# File 'lib/pdf/reader/rectangle.rb', line 33 def top_right @top_right end |
Class Method Details
.from_array(arr) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pdf/reader/rectangle.rb', line 45 def self.from_array(arr) if arr.size != 4 raise ArgumentError, "Only 4-element Arrays can be converted to a Rectangle" end PDF::Reader::Rectangle.new( arr[0].to_f, arr[1].to_f, arr[2].to_f, arr[3].to_f, ) end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/pdf/reader/rectangle.rb', line 59 def ==(other) to_a == other.to_a end |
#apply_rotation(degrees) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/pdf/reader/rectangle.rb', line 102 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 || 0, new_y1 || 0, new_x2 || 0, new_y2 || 0) end |
#contains?(point) ⇒ Boolean
Is the point inside this rectangle? Nicer to read and use than contains_xy?(), but allocates more objects so be careful using on hot code paths.
77 78 79 |
# File 'lib/pdf/reader/rectangle.rb', line 77 def contains?(point) contains_xy?(point.x, point.y) end |
#contains_xy?(x, y) ⇒ Boolean
Is the point inside this rectangle? Worse to read and use than contains?(), but allocates fewer objects so may be preferrable on hot code paths.
85 86 87 88 |
# File 'lib/pdf/reader/rectangle.rb', line 85 def contains_xy?(x, y) x >= bottom_left.x && x <= top_right.x && y >= bottom_left.y && y <= top_right.y end |
#height ⇒ Object
64 65 66 |
# File 'lib/pdf/reader/rectangle.rb', line 64 def height top_right.y - bottom_right.y end |
#to_a ⇒ Object
A pdf-style 4-number array
92 93 94 95 96 97 98 99 |
# File 'lib/pdf/reader/rectangle.rb', line 92 def to_a [ bottom_left.x, bottom_left.y, top_right.x, top_right.y, ] end |
#width ⇒ Object
69 70 71 |
# File 'lib/pdf/reader/rectangle.rb', line 69 def width bottom_right.x - bottom_left.x end |