Class: HOCRBox

Inherits:
Object
  • Object
show all
Defined in:
lib/hocr_box.rb

Direct Known Subclasses

OCRElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*coordinates) ⇒ HOCRBox

Returns a new instance of HOCRBox.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hocr_box.rb', line 7

def initialize(* coordinates)
    
    @left, @top, @right, @bottom = coordinates.flatten.collect { |x| x.to_i}
    
    @height  = @bottom - @top
    @width   = @right - @left
    @coordinates = [ @left, @top,@right, @bottom ]
    
    if left > right || top > bottom then
        raise " Negative dimensions of OCRBox ar not allowed. left #{@left} / right #{@right} - top #{@top} / bottom #{@bottom}"
    end
    
end

Instance Attribute Details

#bottomObject (readonly)

Returns the value of attribute bottom.



5
6
7
# File 'lib/hocr_box.rb', line 5

def bottom
  @bottom
end

#coordinatesObject (readonly)

Returns the value of attribute coordinates.



5
6
7
# File 'lib/hocr_box.rb', line 5

def coordinates
  @coordinates
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/hocr_box.rb', line 5

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



5
6
7
# File 'lib/hocr_box.rb', line 5

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



5
6
7
# File 'lib/hocr_box.rb', line 5

def right
  @right
end

#topObject (readonly)

Returns the value of attribute top.



5
6
7
# File 'lib/hocr_box.rb', line 5

def top
  @top
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/hocr_box.rb', line 5

def width
  @width
end

Instance Method Details

#bottom_distance_to(other) ⇒ Object



52
53
54
# File 'lib/hocr_box.rb', line 52

def bottom_distance_to(other)
    other.top_distance_to(self)
end

#coordinates_to_sObject



60
61
62
# File 'lib/hocr_box.rb', line 60

def coordinates_to_s
    "(#{@left},#{@top})/(#{@right},#{@bottom})" 
end

#enclosed_by?(other) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/hocr_box.rb', line 28

def enclosed_by?(other)
    return other.encloses? self
end

#encloses?(other) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/hocr_box.rb', line 21

def encloses?(other)
    @left <= other.left and 
    @right >= other.right and
    @top <= other.top and 
    @bottom >= other.bottom
end

#left_distance_to(other) ⇒ Object



40
41
42
# File 'lib/hocr_box.rb', line 40

def left_distance_to(other)
    @left - other.right
end

#left_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/hocr_box.rb', line 32

def left_of?(other)
    @right < other.left
end

#right_distance_to(other) ⇒ Object



44
45
46
# File 'lib/hocr_box.rb', line 44

def right_distance_to(other)
    other.left_distance_to(self)
end

#right_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hocr_box.rb', line 36

def right_of?(other)
    @left > other.right
end

#to_css_style(zoom = 1) ⇒ Object



64
65
66
# File 'lib/hocr_box.rb', line 64

def to_css_style(zoom = 1)
    "position:absolute; top:#{ @top * zoom }px; left:#{ @left * zoom }px; height:#{ @height * zoom }px; width:#{ @width * zoom }px;"
end

#to_image_html(options = {}) ⇒ Object



68
69
70
71
72
# File 'lib/hocr_box.rb', line 68

def to_image_html(options = {})
    css_class = options[:css_class] || 'hocr_box'
    zoom = options[:zoom] || 1
    "<span style='#{ to_css_style(zoom) }' class='#{css_class}'></span>"
end

#to_sObject



56
57
58
# File 'lib/hocr_box.rb', line 56

def to_s
    coordinates_to_s
end

#top_distance_to(other) ⇒ Object



48
49
50
# File 'lib/hocr_box.rb', line 48

def top_distance_to(other)
    @top - other.bottom
end