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

Returns a new instance of TextRun.



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

def initialize(x, y, width, font_size, text)
  @origin = PDF::Reader::Point.new(x, y)
  @width = width
  @font_size = font_size
  @text = text
end

Instance Attribute Details

#font_sizeObject (readonly)

Returns the value of attribute font_size.



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

def font_size
  @font_size
end

#originObject (readonly)

Returns the value of attribute origin.



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

def origin
  @origin
end

#textObject (readonly) Also known as: to_s

Returns the value of attribute text.



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

def text
  @text
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
# File 'lib/pdf/reader/text_run.rb', line 61

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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdf/reader/text_run.rb', line 23

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

#endxObject



45
46
47
# File 'lib/pdf/reader/text_run.rb', line 45

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

#endyObject



49
50
51
# File 'lib/pdf/reader/text_run.rb', line 49

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

#inspectObject



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

def inspect
  "#{text} w:#{width} f:#{font_size} @#{x},#{y}"
end

#intersect?(other_run) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/pdf/reader/text_run.rb', line 75

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



81
82
83
84
85
86
87
88
89
# File 'lib/pdf/reader/text_run.rb', line 81

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



53
54
55
# File 'lib/pdf/reader/text_run.rb', line 53

def mean_character_width
  @width / character_count
end

#mergable?(other) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pdf/reader/text_run.rb', line 57

def mergable?(other)
  y.to_i == other.y.to_i && font_size == other.font_size && mergable_range.include?(other.x)
end

#xObject



37
38
39
# File 'lib/pdf/reader/text_run.rb', line 37

def x
  @origin.x
end

#yObject



41
42
43
# File 'lib/pdf/reader/text_run.rb', line 41

def y
  @origin.y
end