Class: PDF::Reader::TextRun

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_sizeObject (readonly)

: Numeric



17
18
19
# File 'lib/pdf/reader/text_run.rb', line 17

def font_size
  @font_size
end

#originObject (readonly)

: PDF::Reader::Point



11
12
13
# File 'lib/pdf/reader/text_run.rb', line 11

def origin
  @origin
end

#textObject (readonly) Also known as: to_s

: String



20
21
22
# File 'lib/pdf/reader/text_run.rb', line 20

def text
  @text
end

#widthObject (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

Raises:

  • (ArgumentError)


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

#endxObject

: () -> Numeric



65
66
67
# File 'lib/pdf/reader/text_run.rb', line 65

def endx
  @endx ||= @origin.x + width
end

#endyObject

: () -> Numeric



70
71
72
# File 'lib/pdf/reader/text_run.rb', line 70

def endy
  @endy ||= @origin.y + font_size
end

#inspectObject

: () -> 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

Returns:

  • (Boolean)


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_widthObject

: () -> 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

Returns:

  • (Boolean)


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

#xObject

: () -> Numeric



55
56
57
# File 'lib/pdf/reader/text_run.rb', line 55

def x
  @origin.x
end

#yObject

: () -> Numeric



60
61
62
# File 'lib/pdf/reader/text_run.rb', line 60

def y
  @origin.y
end