class PDF::Reader::TextRun
A value object that represents one or more consecutive characters on a page.
def +(other)
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
def <=>(other)
Allows collections of TextRun objects to be sorted. They will be sorted
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
def area
def area (endx - x) * (endy - y) end
def character_count
Assume string encoding is marked correctly and we can trust String#size to return a
def character_count @text.size.to_f end
def endx
def endx @endx ||= @origin.x + width end
def endy
def endy @endy ||= @origin.y + font_size end
def initialize(x, y, width, font_size, text)
def initialize(x, y, width, font_size, text) @origin = PDF::Reader::Point.new(x, y) @width = width @font_size = font_size @text = text end
def inspect
def inspect "#{text} w:#{width} f:#{font_size} @#{x},#{y}" end
def intersect?(other_run)
def intersect?(other_run) x <= other_run.endx && endx >= other_run.x && endy >= other_run.y && y <= other_run.endy end
def intersection_area_percent(other_run)
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
def mean_character_width
def mean_character_width @width / character_count end
def mergable?(other)
def mergable?(other) y.to_i == other.y.to_i && font_size == other.font_size && mergable_range.include?(other.x) end
def mergable_range
def mergable_range @mergable_range ||= Range.new(endx - 3, endx + font_size) end
def x
def x @origin.x end
def y
def y @origin.y end