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.

Signature:

  • (Numeric, Numeric, Numeric, Numeric, String) -> void



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

def initialize(x, y, width, font_size, text)
  @x = x #: Numeric
  @y = y #: Numeric
  @width = width
  @font_size = font_size
  @text = text
  @origin = nil #: PDF::Reader::Point | nil
  @endx = nil #: Numeric | nil
  @endy = nil #: Numeric | nil
  @mergable_range = nil #: Range[Numeric] | nil
end

Instance Attribute Details

#font_sizeObject (readonly)

Signature:

  • Numeric



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

def font_size
  @font_size
end

#textObject (readonly) Also known as: to_s

Signature:

  • String



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

def text
  @text
end

#widthObject (readonly)

Signature:

  • Numeric



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

def width
  @width
end

Instance Method Details

#+(other) ⇒ Object

Signature:

  • (PDF::Reader::TextRun) -> PDF::Reader::TextRun

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
# File 'lib/pdf/reader/text_run.rb', line 90

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

Signature:

  • (PDF::Reader::Point) -> Numeric



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pdf/reader/text_run.rb', line 43

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

Signature:

  • () -> Numeric



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

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

#endyObject

Signature:

  • () -> Numeric



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

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

#inspectObject

Signature:

  • () -> String



101
102
103
# File 'lib/pdf/reader/text_run.rb', line 101

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

#intersect?(other_run) ⇒ Boolean

Signature:

  • (PDF::Reader::TextRun) -> bool

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/pdf/reader/text_run.rb', line 106

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

Signature:

  • (PDF::Reader::TextRun) -> Numeric



113
114
115
116
117
118
119
120
121
# File 'lib/pdf/reader/text_run.rb', line 113

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

Signature:

  • () -> Numeric



80
81
82
# File 'lib/pdf/reader/text_run.rb', line 80

def mean_character_width
  @width / character_count
end

#mergable?(other) ⇒ Boolean

Signature:

  • (PDF::Reader::TextRun) -> bool

Returns:

  • (Boolean)


85
86
87
# File 'lib/pdf/reader/text_run.rb', line 85

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

#originObject

Lazily constructed to avoid allocating a Point on every TextRun

Signature:

  • () -> PDF::Reader::Point



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

def origin
  @origin ||= PDF::Reader::Point.new(@x, @y)
end

#xObject

Signature:

  • () -> Numeric



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

def x
  @x
end

#yObject

Signature:

  • () -> Numeric



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

def y
  @y
end