Class: PDF::Reader::TextRun
- Inherits:
-
Object
- Object
- PDF::Reader::TextRun
- Includes:
- Comparable
- Defined in:
- lib/pdf/reader/text_run.rb
Overview
A value object that represents one or more consecutive characters on a page.
Instance Attribute Summary collapse
-
#font_size ⇒ Object
readonly
Returns the value of attribute font_size.
-
#text ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute text.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #+(other) ⇒ Object
-
#<=>(other) ⇒ Object
Allows collections of TextRun objects to be sorted.
- #endx ⇒ Object
-
#initialize(x, y, width, font_size, text) ⇒ TextRun
constructor
A new instance of TextRun.
- #inspect ⇒ Object
- #mean_character_width ⇒ Object
- #mergable?(other) ⇒ Boolean
Constructor Details
#initialize(x, y, width, font_size, text) ⇒ TextRun
Returns a new instance of TextRun.
12 13 14 15 16 17 18 |
# File 'lib/pdf/reader/text_run.rb', line 12 def initialize(x, y, width, font_size, text) @x = x @y = y @width = width @font_size = font_size.floor @text = text end |
Instance Attribute Details
#font_size ⇒ Object (readonly)
Returns the value of attribute font_size.
8 9 10 |
# File 'lib/pdf/reader/text_run.rb', line 8 def font_size @font_size end |
#text ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute text.
8 9 10 |
# File 'lib/pdf/reader/text_run.rb', line 8 def text @text end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/pdf/reader/text_run.rb', line 8 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
8 9 10 |
# File 'lib/pdf/reader/text_run.rb', line 8 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
8 9 10 |
# File 'lib/pdf/reader/text_run.rb', line 8 def y @y end |
Instance Method Details
#+(other) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/pdf/reader/text_run.rb', line 48 def +(other) raise ArgumentError, "#{other} cannot be merged with this run" unless mergable?(other) if (other.x - endx) <( font_size * 0.2) TextRun.new(x, y, other.endx - x, font_size, text + other.text) else TextRun.new(x, y, other.endx - x, font_size, "#{text} #{other.text}") end end |
#<=>(other) ⇒ Object
Allows collections of TextRun objects to be sorted. They will be sorted in order of their position on a cartesian plain - Top Left to Bottom Right
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pdf/reader/text_run.rb', line 22 def <=>(other) if x == other.x && y == other.y 0 elsif y < other.y 1 elsif y > other.y -1 elsif x < other.x -1 elsif x > other.x 1 end end |
#endx ⇒ Object
36 37 38 |
# File 'lib/pdf/reader/text_run.rb', line 36 def endx @endx ||= x + width end |
#inspect ⇒ Object
58 59 60 |
# File 'lib/pdf/reader/text_run.rb', line 58 def inspect "#{text} w:#{width} f:#{font_size} @#{x},#{y}" end |
#mean_character_width ⇒ Object
40 41 42 |
# File 'lib/pdf/reader/text_run.rb', line 40 def mean_character_width @width / character_count end |
#mergable?(other) ⇒ Boolean
44 45 46 |
# File 'lib/pdf/reader/text_run.rb', line 44 def mergable?(other) y.to_i == other.y.to_i && font_size == other.font_size && mergable_range.include?(other.x) end |