/usr/lib/ruby/vendor_ruby/pdf/reader/text_run.rb is in ruby-pdf-reader 1.3.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | # coding: utf-8
class PDF::Reader
# A value object that represents one or more consecutive characters on a page.
class TextRun
include Comparable
attr_reader :x, :y, :width, :font_size, :text
alias :to_s :text
def initialize(x, y, width, font_size, text)
@x = x
@y = y
@width = width
@font_size = font_size.floor
@text = text
end
# 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
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 endx
@endx ||= x + width
end
def mean_character_width
@width / character_count
end
def mergable?(other)
y.to_i == other.y.to_i && font_size == other.font_size && mergable_range.include?(other.x)
end
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 inspect
"#{text} w:#{width} f:#{font_size} @#{x},#{y}"
end
private
def mergable_range
@mergable_range ||= Range.new(endx - 3, endx + font_size)
end
def character_count
if @text.size == 1
1.0
elsif @text.respond_to?(:bytesize)
# M17N aware VM
# so we can trust String#size to return a character count
@text.size.to_f
else
text.unpack("U*").size.to_f
end
end
end
end
|