Class: PDF::Reader::Turtletext::Textangle

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/turtletext/textangle.rb

Overview

A DSL syntax for text extraction.

textangle = PDF::Reader::Turtletext::Textangle.new(reader) do |r|

r.page = 1
r.below = "Electricity Services"
r.above = "Gas Services by City Gas Pte Ltd"
r.right_of = 240.0
r.left_of = "Total ($)"

end textangle.text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(turtletext_reader, &block) ⇒ Textangle

turtletext_reader is a PDF::Reader::Turtletext



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pdf/reader/turtletext/textangle.rb', line 18

def initialize(turtletext_reader,&block)
  @reader = turtletext_reader
  @page = 1
  @inclusive = false
  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval &block
    end
  end
end

Instance Attribute Details

#above(*args) ⇒ Object



59
60
61
62
63
64
# File 'lib/pdf/reader/turtletext/textangle.rb', line 59

def above(*args)
  if value = args.first
    @above = value
  end
  @above
end

#below(*args) ⇒ Object



67
68
69
70
71
72
# File 'lib/pdf/reader/turtletext/textangle.rb', line 67

def below(*args)
  if value = args.first
    @below = value
  end
  @below
end

#inclusive(*args) ⇒ Object



33
34
35
36
37
38
# File 'lib/pdf/reader/turtletext/textangle.rb', line 33

def inclusive(*args)
  if value = args.first
    @inclusive = value
  end
  @inclusive
end

#left_of(*args) ⇒ Object



75
76
77
78
79
80
# File 'lib/pdf/reader/turtletext/textangle.rb', line 75

def left_of(*args)
  if value = args.first
    @left_of = value
  end
  @left_of
end

#page(*args) ⇒ Object



51
52
53
54
55
56
# File 'lib/pdf/reader/turtletext/textangle.rb', line 51

def page(*args)
  if value = args.first
    @page = value
  end
  @page
end

#readerObject (readonly)

Returns the value of attribute reader.



15
16
17
# File 'lib/pdf/reader/turtletext/textangle.rb', line 15

def reader
  @reader
end

#right_of(*args) ⇒ Object



83
84
85
86
87
88
# File 'lib/pdf/reader/turtletext/textangle.rb', line 83

def right_of(*args)
  if value = args.first
    @right_of = value
  end
  @right_of
end

Instance Method Details

#exclusive!Object

Command: sets +inclusive false



46
47
48
# File 'lib/pdf/reader/turtletext/textangle.rb', line 46

def exclusive!
  @inclusive = false
end

#inclusive!Object

Command: sets +inclusive true



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

def inclusive!
  @inclusive = true
end

#textObject

Returns the text array found within the defined region. Each line of text is an array of the seperate text elements found on that line.

[["first line first text", "first line last text"],["second line text"]]


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pdf/reader/turtletext/textangle.rb', line 93

def text
  return unless reader

  xmin = if right_of
    if [Fixnum,Float].include?(right_of.class)
      right_of
    elsif xy = reader.text_position(right_of,page)
      xy[:x]
    end
  else
    0
  end
  xmax = if left_of
    if [Fixnum,Float].include?(left_of.class)
      left_of
    elsif xy = reader.text_position(left_of,page)
      xy[:x]
    end
  else
    99999 # TODO: figure out the actual limit?
  end

  ymin = if above
    if [Fixnum,Float].include?(above.class)
      above
    elsif xy = reader.text_position(above,page)
      xy[:y]
    end
  else
    0
  end
  ymax = if below
    if [Fixnum,Float].include?(below.class)
      below
    elsif xy = reader.text_position(below,page)
      xy[:y]
    end
  else
    99999 # TODO: figure out the actual limit?
  end

  reader.text_in_region(xmin,xmax,ymin,ymax,page,inclusive)
end