Class: Tabula::TextElement

Inherits:
Rectangle show all
Defined in:
lib/tabula/text/text_element.rb

Overview

Represents a single text element (character or glyph) extracted from a PDF. Contains position, dimensions, and font information.

Constant Summary collapse

DIRECTION_LTR =

Text direction constants

0
DIRECTION_RTL =
1

Constants inherited from Rectangle

Rectangle::VERTICAL_COMPARISON_THRESHOLD

Instance Attribute Summary collapse

Attributes inherited from Rectangle

#height, #left, #top, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rectangle

#<=>, #area, #bottom, #bottom=, bounding_box_of, #bounds, #center, #contains?, #contains_point?, #dup, from_bounds, from_points, #horizontal_overlap, #horizontally_overlaps?, #intersection, #intersects?, #merge, #merge!, #overlap_ratio, #points, #right, #right=, #vertical_overlap, #vertically_overlaps?, #x, #x=, #y, #y=

Constructor Details

#initialize(top:, left:, width:, height:, text:, font_name: nil, font_size: nil, width_of_space: nil, direction: DIRECTION_LTR) ⇒ TextElement

Returns a new instance of TextElement.

Parameters:

  • top (Float) —

    top coordinate

  • left (Float) —

    left coordinate

  • width (Float) —

    width

  • height (Float) —

    height

  • text (String) —

    the text content

  • font_name (String) (defaults to: nil) —

    name of the font

  • font_size (Float) (defaults to: nil) —

    font size in points

  • width_of_space (Float) (defaults to: nil) —

    width of space character in this font

  • direction (Integer) (defaults to: DIRECTION_LTR) —

    text direction (LTR or RTL)



22
23
24
25
26
27
28
29
30
# File 'lib/tabula/text/text_element.rb', line 22

def initialize(top:, left:, width:, height:, text:, font_name: nil, font_size: nil,
               width_of_space: nil, direction: DIRECTION_LTR)
  super(top, left, width, height)
  @text = text
  @font_name = font_name
  @font_size = font_size&.to_f
  @width_of_space = width_of_space&.to_f
  @direction = direction
end

Instance Attribute Details

#direction ⇒ Object (readonly)

Returns the value of attribute direction.



11
12
13
# File 'lib/tabula/text/text_element.rb', line 11

def direction
  @direction
end

#font_name ⇒ Object (readonly)

Returns the value of attribute font_name.



11
12
13
# File 'lib/tabula/text/text_element.rb', line 11

def font_name
  @font_name
end

#font_size ⇒ Object (readonly)

Returns the value of attribute font_size.



11
12
13
# File 'lib/tabula/text/text_element.rb', line 11

def font_size
  @font_size
end

#text ⇒ Object (readonly)

Returns the value of attribute text.



11
12
13
# File 'lib/tabula/text/text_element.rb', line 11

def text
  @text
end

#width_of_space ⇒ Object (readonly)

Returns the value of attribute width_of_space.



11
12
13
# File 'lib/tabula/text/text_element.rb', line 11

def width_of_space
  @width_of_space
end

Class Method Details

.merge_words(elements, vertical_rulings: []) ⇒ Array<TextChunk>

Merge text elements into text chunks (words)

Parameters:

  • elements (Array<TextElement>) —

    text elements to merge

  • vertical_rulings (Array<Ruling>) (defaults to: []) —

    vertical rulings that act as word separators

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tabula/text/text_element.rb', line 70

def merge_words(elements, vertical_rulings: [])
  return [] if elements.empty?

  chunks = []
  current_chunk = nil

  # Sort by top first, then by left (RTL sorting handled in text assembly)
  sorted = elements.reject(&:whitespace?).sort_by { |e| [e.top, e.left] }

  sorted.each do |element|
    if current_chunk.nil?
      current_chunk = TextChunk.new(element)
    elsif should_merge?(current_chunk, element, vertical_rulings)
      current_chunk.add(element)
    else
      chunks << current_chunk
      current_chunk = TextChunk.new(element)
    end
  end

  chunks << current_chunk if current_chunk
  chunks
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



53
54
55
56
57
58
# File 'lib/tabula/text/text_element.rb', line 53

def ==(other)
  return false unless other.is_a?(TextElement)

  super && text == other.text && font_name == other.font_name &&
    font_size == other.font_size
end

#hash ⇒ Object



61
62
63
# File 'lib/tabula/text/text_element.rb', line 61

def hash
  [super, text, font_name, font_size].hash
end

#inspect ⇒ Object



49
50
51
# File 'lib/tabula/text/text_element.rb', line 49

def inspect
  to_s
end

#ltr? ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/tabula/text/text_element.rb', line 32

def ltr?
  direction == DIRECTION_LTR
end

#rtl? ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/tabula/text/text_element.rb', line 36

def rtl?
  direction == DIRECTION_RTL
end

#to_s ⇒ Object



45
46
47
# File 'lib/tabula/text/text_element.rb', line 45

def to_s
  "TextElement[#{text.inspect}](#{left}, #{top}, #{width}, #{height})"
end

#whitespace? ⇒ Boolean

Check if this element is whitespace

Returns:

  • (Boolean)


41
42
43
# File 'lib/tabula/text/text_element.rb', line 41

def whitespace?
  text.nil? || text.strip.empty?
end