Class: Tabula::Extraction::LineExtractor::AppendRectangleToPathOperator

Inherits:
OperatorProcessor
  • Object
show all
Defined in:
lib/tabula/pdf_line_extractor.rb

Instance Method Summary collapse

Instance Method Details

#process(operator, arguments) ⇒ Object



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
136
# File 'lib/tabula/pdf_line_extractor.rb', line 106

def process(operator, arguments)

  drawer = self.context
  finalX, finalY, finalW, finalH = arguments.to_array.map(&:floatValue)

  ppos = drawer.TransformedPoint(finalX, finalY)
  psize = drawer.ScaledPoint(finalW, finalH)

  finalY = ppos.getY - psize.getY
  if finalY < 0
    finalY = 0
  end

  width = psize.getX.abs
  height = psize.getY.abs

  lines = if width > height && height < 2 # horizontal line, "thin" rectangle.
            [java.awt.geom.Line2D::Float.new(ppos.getX, finalY + psize.getY/2, ppos.getX + psize.getX, finalY + psize.getY/2)]
          elsif width < height && width < 2 # vertical line, "thin" rectangle
            [java.awt.geom.Line2D::Float.new(ppos.getX + psize.getX/2, finalY, ppos.getX + psize.getX/2, finalY + psize.getY)]
          else
            # add every edge of the rectangle to drawer.rulings
            [java.awt.geom.Line2D::Float.new(ppos.getX, finalY, ppos.getX + psize.getX, finalY),
             java.awt.geom.Line2D::Float.new(ppos.getX, finalY, ppos.getX, finalY + psize.getY),
             java.awt.geom.Line2D::Float.new(ppos.getX+psize.getX, finalY, ppos.getX + psize.getX, finalY + psize.getY),
             java.awt.geom.Line2D::Float.new(ppos.getX, finalY+psize.getY, ppos.getX + psize.getX, finalY + psize.getY)]
          end

  drawer.currentPath += lines.select { |l| l.horizontal? or l.vertical? }

end