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
: Numeric.
-
#origin ⇒ Object
readonly
: PDF::Reader::Point.
-
#text ⇒ Object
(also: #to_s)
readonly
: String.
-
#width ⇒ Object
readonly
: Numeric.
Instance Method Summary collapse
-
#+(other) ⇒ Object
: (PDF::Reader::TextRun) -> PDF::Reader::TextRun.
-
#<=>(other) ⇒ Object
Allows collections of TextRun objects to be sorted.
-
#endx ⇒ Object
: () -> Numeric.
-
#endy ⇒ Object
: () -> Numeric.
-
#initialize(x, y, width, font_size, text) ⇒ TextRun
constructor
: (Numeric, Numeric, Numeric, Numeric, String) -> void.
-
#inspect ⇒ Object
: () -> String.
-
#intersect?(other_run) ⇒ Boolean
: (PDF::Reader::TextRun) -> bool.
-
#intersection_area_percent(other_run) ⇒ Object
return what percentage of this text run is overlapped by another run : (PDF::Reader::TextRun) -> Numeric.
-
#mean_character_width ⇒ Object
: () -> Numeric.
-
#mergable?(other) ⇒ Boolean
: (PDF::Reader::TextRun) -> bool.
-
#x ⇒ Object
: () -> Numeric.
-
#y ⇒ Object
: () -> Numeric.
Constructor Details
#initialize(x, y, width, font_size, text) ⇒ TextRun
: (Numeric, Numeric, Numeric, Numeric, String) -> void
25 26 27 28 29 30 31 32 33 |
# File 'lib/pdf/reader/text_run.rb', line 25 def initialize(x, y, width, font_size, text) @origin = PDF::Reader::Point.new(x, y) #: PDF::Reader::Point @width = width @font_size = font_size @text = text @endx = nil #: Numeric | nil @endy = nil #: Numeric | nil @mergable_range = nil #: Range[Numeric] | nil end |
Instance Attribute Details
#font_size ⇒ Object (readonly)
: Numeric
17 18 19 |
# File 'lib/pdf/reader/text_run.rb', line 17 def font_size @font_size end |
#origin ⇒ Object (readonly)
: PDF::Reader::Point
11 12 13 |
# File 'lib/pdf/reader/text_run.rb', line 11 def origin @origin end |
#text ⇒ Object (readonly) Also known as: to_s
: String
20 21 22 |
# File 'lib/pdf/reader/text_run.rb', line 20 def text @text end |
#width ⇒ Object (readonly)
: Numeric
14 15 16 |
# File 'lib/pdf/reader/text_run.rb', line 14 def width @width end |
Instance Method Details
#+(other) ⇒ Object
: (PDF::Reader::TextRun) -> PDF::Reader::TextRun
85 86 87 88 89 90 91 92 93 |
# File 'lib/pdf/reader/text_run.rb', line 85 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 : (PDF::Reader::Point) -> Numeric
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pdf/reader/text_run.rb', line 38 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 else 0 # Unreachable? end end |
#endx ⇒ Object
: () -> Numeric
65 66 67 |
# File 'lib/pdf/reader/text_run.rb', line 65 def endx @endx ||= @origin.x + width end |
#endy ⇒ Object
: () -> Numeric
70 71 72 |
# File 'lib/pdf/reader/text_run.rb', line 70 def endy @endy ||= @origin.y + font_size end |
#inspect ⇒ Object
: () -> String
96 97 98 |
# File 'lib/pdf/reader/text_run.rb', line 96 def inspect "#{text} w:#{width} f:#{font_size} @#{x},#{y}" end |
#intersect?(other_run) ⇒ Boolean
: (PDF::Reader::TextRun) -> bool
101 102 103 104 |
# File 'lib/pdf/reader/text_run.rb', line 101 def intersect?(other_run) x <= other_run.endx && endx >= other_run.x && endy >= other_run.y && y <= other_run.endy end |
#intersection_area_percent(other_run) ⇒ Object
return what percentage of this text run is overlapped by another run : (PDF::Reader::TextRun) -> Numeric
108 109 110 111 112 113 114 115 116 |
# File 'lib/pdf/reader/text_run.rb', line 108 def intersection_area_percent(other_run) return 0 unless intersect?(other_run) dx = [endx, other_run.endx].min - [x, other_run.x].max dy = [endy, other_run.endy].min - [y, other_run.y].max intersection_area = dx*dy intersection_area.to_f / area end |
#mean_character_width ⇒ Object
: () -> Numeric
75 76 77 |
# File 'lib/pdf/reader/text_run.rb', line 75 def mean_character_width @width / character_count end |
#mergable?(other) ⇒ Boolean
: (PDF::Reader::TextRun) -> bool
80 81 82 |
# File 'lib/pdf/reader/text_run.rb', line 80 def mergable?(other) y.to_i == other.y.to_i && font_size == other.font_size && mergable_range.include?(other.x) end |
#x ⇒ Object
: () -> Numeric
55 56 57 |
# File 'lib/pdf/reader/text_run.rb', line 55 def x @origin.x end |
#y ⇒ Object
: () -> Numeric
60 61 62 |
# File 'lib/pdf/reader/text_run.rb', line 60 def y @origin.y end |