MCPcopy Index your code
hub / github.com/clips/pattern / group_textboxes

Method group_textboxes

pattern/web/pdf/layout.py:554–611  ·  view source on GitHub ↗
(self, laparams, boxes)

Source from the content-addressed store, hash-verified

552 return
553
554 def group_textboxes(self, laparams, boxes):
555 def dist(obj1, obj2):
556 """A distance function between two TextBoxes.
557
558 Consider the bounding rectangle for obj1 and obj2.
559 Return its area less the areas of obj1 and obj2,
560 shown as 'www' below. This value may be negative.
561 +------+..........+ (x1,y1)
562 | obj1 |wwwwwwwwww:
563 +------+www+------+
564 :wwwwwwwwww| obj2 |
565 (x0,y0) +..........+------+
566 """
567 x0 = min(obj1.x0,obj2.x0)
568 y0 = min(obj1.y0,obj2.y0)
569 x1 = max(obj1.x1,obj2.x1)
570 y1 = max(obj1.y1,obj2.y1)
571 return ((x1-x0)*(y1-y0) - obj1.width*obj1.height - obj2.width*obj2.height)
572 def isany(obj1, obj2):
573 """Check if there's any other object between obj1 and obj2.
574 """
575 x0 = min(obj1.x0,obj2.x0)
576 y0 = min(obj1.y0,obj2.y0)
577 x1 = max(obj1.x1,obj2.x1)
578 y1 = max(obj1.y1,obj2.y1)
579 objs = set(plane.find((x0,y0,x1,y1)))
580 return objs.difference((obj1,obj2))
581 # XXX this still takes O(n^2) :(
582 dists = []
583 for i in xrange(len(boxes)):
584 obj1 = boxes[i]
585 for j in xrange(i+1, len(boxes)):
586 obj2 = boxes[j]
587 dists.append((0, dist(obj1, obj2), obj1, obj2))
588 dists.sort()
589 plane = Plane(boxes)
590 while dists:
591 (c,d,obj1,obj2) = dists.pop(0)
592 if c == 0 and isany(obj1, obj2):
593 dists.append((1,d,obj1,obj2))
594 continue
595 if (isinstance(obj1, LTTextBoxVertical) or
596 isinstance(obj1, LTTextGroupTBRL) or
597 isinstance(obj2, LTTextBoxVertical) or
598 isinstance(obj2, LTTextGroupTBRL)):
599 group = LTTextGroupTBRL([obj1,obj2])
600 else:
601 group = LTTextGroupLRTB([obj1,obj2])
602 plane.remove(obj1)
603 plane.remove(obj2)
604 dists = [ (c,d,o1,o2) for (c,d,o1,o2) in dists
605 if o1 in plane and o2 in plane ]
606 for other in plane:
607 dists.append((0, dist(group,other), group, other))
608 dists.sort()
609 plane.add(group)
610 assert len(plane) == 1
611 return list(plane)

Callers 1

analyzeMethod · 0.95

Calls 9

removeMethod · 0.95
addMethod · 0.95
PlaneClass · 0.90
lenFunction · 0.85
LTTextGroupTBRLClass · 0.85
LTTextGroupLRTBClass · 0.85
appendMethod · 0.45
sortMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected