2010-10-19 09:57:01 +00:00
|
|
|
#!/usr/bin/env python2
|
2009-03-28 17:23:53 +00:00
|
|
|
import sys
|
2011-03-01 11:47:20 +00:00
|
|
|
from utils import INF, Plane, get_bound, uniq, csort, fsplit
|
|
|
|
from utils import bbox2str, matrix2str, apply_matrix_pt
|
2009-03-28 17:23:53 +00:00
|
|
|
|
|
|
|
|
2009-07-21 07:55:19 +00:00
|
|
|
## LAParams
|
2009-05-04 08:29:36 +00:00
|
|
|
##
|
2009-07-21 07:55:19 +00:00
|
|
|
class LAParams(object):
|
2009-10-24 04:41:59 +00:00
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
line_overlap=0.5,
|
2010-10-17 05:13:39 +00:00
|
|
|
char_margin=2.0,
|
2009-10-24 04:41:59 +00:00
|
|
|
line_margin=0.5,
|
2010-04-10 11:29:30 +00:00
|
|
|
word_margin=0.1,
|
2011-02-14 14:41:23 +00:00
|
|
|
boxes_flow=0.5,
|
2011-02-02 14:09:34 +00:00
|
|
|
detect_vertical=False,
|
2010-04-10 11:29:30 +00:00
|
|
|
all_texts=False):
|
2009-10-24 04:41:59 +00:00
|
|
|
self.line_overlap = line_overlap
|
|
|
|
self.char_margin = char_margin
|
|
|
|
self.line_margin = line_margin
|
|
|
|
self.word_margin = word_margin
|
2010-12-26 08:26:39 +00:00
|
|
|
self.boxes_flow = boxes_flow
|
2011-02-02 14:09:34 +00:00
|
|
|
self.detect_vertical = detect_vertical
|
2010-04-10 11:29:30 +00:00
|
|
|
self.all_texts = all_texts
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<LAParams: char_margin=%.1f, line_margin=%.1f, word_margin=%.1f all_texts=%r>' %
|
|
|
|
(self.char_margin, self.line_margin, self.word_margin, self.all_texts))
|
2009-05-04 08:29:36 +00:00
|
|
|
|
|
|
|
|
2010-04-10 11:29:30 +00:00
|
|
|
## LTItem
|
2009-05-05 12:26:29 +00:00
|
|
|
##
|
2010-04-10 11:29:30 +00:00
|
|
|
class LTItem(object):
|
2009-05-05 12:26:29 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __init__(self, bbox):
|
|
|
|
self.set_bbox(bbox)
|
|
|
|
return
|
|
|
|
|
2009-11-06 15:06:59 +00:00
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s %s>' %
|
|
|
|
(self.__class__.__name__, bbox2str(self.bbox)))
|
2009-11-06 15:06:59 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def set_bbox(self, (x0,y0,x1,y1)):
|
|
|
|
self.x0 = x0
|
|
|
|
self.y0 = y0
|
|
|
|
self.x1 = x1
|
|
|
|
self.y1 = y1
|
|
|
|
self.width = x1-x0
|
|
|
|
self.height = y1-y0
|
2010-01-31 02:09:28 +00:00
|
|
|
self.bbox = (x0, y0, x1, y1)
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2011-03-01 11:47:20 +00:00
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
return self.width <= 0 or self.height <= 0
|
2010-11-09 10:40:05 +00:00
|
|
|
|
2009-11-14 11:29:40 +00:00
|
|
|
def is_hoverlap(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
return obj.x0 <= self.x1 and self.x0 <= obj.x1
|
2009-11-14 11:29:40 +00:00
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
def hdistance(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
if self.is_hoverlap(obj):
|
2009-10-24 04:41:59 +00:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
|
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
def hoverlap(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
if self.is_hoverlap(obj):
|
|
|
|
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
|
2009-11-14 11:29:40 +00:00
|
|
|
else:
|
2010-03-20 05:43:34 +00:00
|
|
|
return 0
|
2009-11-14 11:29:40 +00:00
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
def is_voverlap(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
return obj.y0 <= self.y1 and self.y0 <= obj.y1
|
|
|
|
|
|
|
|
def vdistance(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
if self.is_voverlap(obj):
|
2009-10-24 04:41:59 +00:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
|
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
def voverlap(self, obj):
|
2010-04-10 11:29:30 +00:00
|
|
|
assert isinstance(obj, LTItem)
|
2010-03-20 05:43:34 +00:00
|
|
|
if self.is_voverlap(obj):
|
|
|
|
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2009-06-20 10:44:00 +00:00
|
|
|
|
2011-04-20 13:05:25 +00:00
|
|
|
## LTCurve
|
2009-05-15 14:25:32 +00:00
|
|
|
##
|
2011-04-20 13:05:25 +00:00
|
|
|
class LTCurve(LTItem):
|
2009-05-15 14:25:32 +00:00
|
|
|
|
2009-12-20 02:38:01 +00:00
|
|
|
def __init__(self, linewidth, pts):
|
|
|
|
self.pts = pts
|
2009-10-24 04:41:59 +00:00
|
|
|
self.linewidth = linewidth
|
2010-10-17 05:13:39 +00:00
|
|
|
LTItem.__init__(self, get_bound(pts))
|
2009-12-20 02:38:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def get_pts(self):
|
|
|
|
return ','.join( '%.3f,%.3f' % p for p in self.pts )
|
|
|
|
|
|
|
|
|
|
|
|
## LTLine
|
|
|
|
##
|
2011-04-20 13:05:25 +00:00
|
|
|
class LTLine(LTCurve):
|
2009-12-20 02:38:01 +00:00
|
|
|
|
|
|
|
def __init__(self, linewidth, p0, p1):
|
2011-04-20 13:05:25 +00:00
|
|
|
LTCurve.__init__(self, linewidth, [p0, p1])
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2009-05-15 14:25:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
## LTRect
|
2009-05-05 12:26:29 +00:00
|
|
|
##
|
2011-04-20 13:05:25 +00:00
|
|
|
class LTRect(LTCurve):
|
2009-05-15 14:25:32 +00:00
|
|
|
|
2009-12-20 02:38:01 +00:00
|
|
|
def __init__(self, linewidth, (x0,y0,x1,y1)):
|
2011-04-20 13:05:25 +00:00
|
|
|
LTCurve.__init__(self, linewidth, [(x0,y0), (x1,y0), (x1,y1), (x0,y1)])
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
2009-05-15 14:25:32 +00:00
|
|
|
|
2010-01-30 07:30:01 +00:00
|
|
|
## LTImage
|
|
|
|
##
|
2010-04-10 11:29:30 +00:00
|
|
|
class LTImage(LTItem):
|
2010-01-30 07:30:01 +00:00
|
|
|
|
2010-04-10 11:05:02 +00:00
|
|
|
def __init__(self, name, stream, bbox):
|
2010-04-10 11:29:30 +00:00
|
|
|
LTItem.__init__(self, bbox)
|
2010-01-30 07:30:01 +00:00
|
|
|
self.name = name
|
2010-04-10 11:05:02 +00:00
|
|
|
self.stream = stream
|
|
|
|
self.srcsize = (stream.get_any(('W', 'Width')),
|
|
|
|
stream.get_any(('H', 'Height')))
|
|
|
|
self.imagemask = stream.get_any(('IM', 'ImageMask'))
|
|
|
|
self.bits = stream.get_any(('BPC', 'BitsPerComponent'), 1)
|
|
|
|
self.colorspace = stream.get_any(('CS', 'ColorSpace'))
|
|
|
|
if not isinstance(self.colorspace, list):
|
2010-04-10 11:30:03 +00:00
|
|
|
self.colorspace = [self.colorspace]
|
2010-01-30 07:30:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
(w,h) = self.srcsize
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s(%s) %s %dx%d>' %
|
|
|
|
(self.__class__.__name__, self.name,
|
|
|
|
bbox2str(self.bbox), w, h))
|
2010-01-30 07:30:01 +00:00
|
|
|
|
|
|
|
|
2009-07-21 07:55:19 +00:00
|
|
|
## LTText
|
2009-06-20 10:44:00 +00:00
|
|
|
##
|
2009-07-21 07:55:19 +00:00
|
|
|
class LTText(object):
|
2009-06-20 10:44:00 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __init__(self, text):
|
|
|
|
self.text = text
|
|
|
|
return
|
2009-06-20 10:44:00 +00:00
|
|
|
|
2009-10-24 04:41:59 +00:00
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s %r>' %
|
2010-11-09 10:40:05 +00:00
|
|
|
(self.__class__.__name__, self.text))
|
2009-07-21 07:55:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
## LTAnon
|
|
|
|
##
|
|
|
|
class LTAnon(LTText):
|
|
|
|
|
2010-03-21 02:21:37 +00:00
|
|
|
pass
|
2009-06-20 10:44:00 +00:00
|
|
|
|
|
|
|
|
2010-02-27 03:59:25 +00:00
|
|
|
## LTChar
|
2009-05-05 12:26:29 +00:00
|
|
|
##
|
2010-04-10 11:29:30 +00:00
|
|
|
class LTChar(LTItem, LTText):
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-03-26 11:11:35 +00:00
|
|
|
debug = 0
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-11-14 10:07:41 +00:00
|
|
|
def __init__(self, matrix, font, fontsize, scaling, rise, text, textwidth, textdisp):
|
2010-02-27 03:59:25 +00:00
|
|
|
LTText.__init__(self, text)
|
2010-11-14 10:07:41 +00:00
|
|
|
self.matrix = matrix
|
|
|
|
self.fontname = font.fontname
|
|
|
|
self.adv = textwidth * fontsize * scaling
|
2010-02-27 03:59:25 +00:00
|
|
|
# compute the boundary rectangle.
|
2010-11-14 10:07:41 +00:00
|
|
|
if font.is_vertical():
|
2010-02-27 03:59:25 +00:00
|
|
|
# vertical
|
2010-08-29 06:39:31 +00:00
|
|
|
width = font.get_width() * fontsize
|
2010-11-14 10:07:41 +00:00
|
|
|
(vx,vy) = textdisp
|
2010-08-29 06:39:31 +00:00
|
|
|
if vx is None:
|
|
|
|
vx = width/2
|
|
|
|
else:
|
|
|
|
vx = vx * fontsize * .001
|
|
|
|
vy = (1000 - vy) * fontsize * .001
|
|
|
|
tx = -vx
|
|
|
|
ty = vy + rise
|
|
|
|
bll = (tx, ty+self.adv)
|
|
|
|
bur = (tx+width, ty)
|
2010-02-27 03:59:25 +00:00
|
|
|
else:
|
|
|
|
# horizontal
|
2010-08-29 06:39:31 +00:00
|
|
|
height = font.get_height() * fontsize
|
2010-02-27 03:59:25 +00:00
|
|
|
descent = font.get_descent() * fontsize
|
2010-08-29 06:39:31 +00:00
|
|
|
ty = descent + rise
|
|
|
|
bll = (0, ty)
|
|
|
|
bur = (self.adv, ty+height)
|
|
|
|
(a,b,c,d,e,f) = self.matrix
|
|
|
|
self.upright = (0 < a*d*scaling and b*c <= 0)
|
2010-11-09 10:40:05 +00:00
|
|
|
(x0,y0) = apply_matrix_pt(self.matrix, bll)
|
|
|
|
(x1,y1) = apply_matrix_pt(self.matrix, bur)
|
|
|
|
if x1 < x0:
|
|
|
|
(x0,x1) = (x1,x0)
|
|
|
|
if y1 < y0:
|
|
|
|
(y0,y1) = (y1,y0)
|
|
|
|
LTItem.__init__(self, (x0,y0,x1,y1))
|
2010-11-14 10:07:41 +00:00
|
|
|
if font.is_vertical():
|
2010-11-09 10:39:48 +00:00
|
|
|
self.size = self.width
|
|
|
|
else:
|
|
|
|
self.size = self.height
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.debug:
|
2010-11-14 10:07:41 +00:00
|
|
|
return ('<%s %s matrix=%s font=%r adv=%s text=%r>' %
|
2010-10-17 05:13:39 +00:00
|
|
|
(self.__class__.__name__, bbox2str(self.bbox),
|
2010-11-14 10:07:41 +00:00
|
|
|
matrix2str(self.matrix), self.fontname,
|
2010-11-09 10:40:05 +00:00
|
|
|
self.adv, self.text))
|
2009-10-24 04:41:59 +00:00
|
|
|
else:
|
2010-02-27 03:59:25 +00:00
|
|
|
return '<char %r>' % self.text
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-10-17 05:13:39 +00:00
|
|
|
def is_compatible(self, obj):
|
2010-11-09 10:40:05 +00:00
|
|
|
"""Returns True if two characters can coexist in the same line."""
|
2010-10-17 05:13:39 +00:00
|
|
|
return True
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-02-27 03:59:25 +00:00
|
|
|
|
2010-04-10 11:29:30 +00:00
|
|
|
## LTContainer
|
2009-05-16 04:16:00 +00:00
|
|
|
##
|
2010-04-10 11:29:30 +00:00
|
|
|
class LTContainer(LTItem):
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def __init__(self, bbox):
|
2010-04-10 11:29:30 +00:00
|
|
|
LTItem.__init__(self, bbox)
|
2010-11-09 10:40:05 +00:00
|
|
|
self._objs = []
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
2010-04-10 11:29:30 +00:00
|
|
|
def __iter__(self):
|
2010-11-09 10:40:05 +00:00
|
|
|
return iter(self._objs)
|
2010-04-10 11:29:30 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2010-11-09 10:40:05 +00:00
|
|
|
return len(self._objs)
|
2010-04-10 11:29:30 +00:00
|
|
|
|
|
|
|
def add(self, obj):
|
2010-10-17 05:13:33 +00:00
|
|
|
self._objs.append(obj)
|
2010-04-10 11:29:30 +00:00
|
|
|
return
|
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def extend(self, objs):
|
|
|
|
for obj in objs:
|
|
|
|
self.add(obj)
|
2010-04-10 11:29:30 +00:00
|
|
|
return
|
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
|
|
|
|
## LTExpandableContainer
|
|
|
|
##
|
|
|
|
class LTExpandableContainer(LTContainer):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
LTContainer.__init__(self, (+INF,+INF,-INF,-INF))
|
2010-04-10 11:29:30 +00:00
|
|
|
return
|
2009-05-16 04:16:00 +00:00
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def add(self, obj):
|
|
|
|
LTContainer.add(self, obj)
|
|
|
|
self.set_bbox((min(self.x0, obj.x0), min(self.y0, obj.y0),
|
|
|
|
max(self.x1, obj.x1), max(self.y1, obj.y1)))
|
|
|
|
return
|
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
|
|
|
"""Perform the layout analysis."""
|
2010-11-09 10:40:05 +00:00
|
|
|
return self
|
|
|
|
|
2009-05-16 04:16:00 +00:00
|
|
|
|
2009-07-21 07:55:19 +00:00
|
|
|
## LTTextLine
|
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTTextLine(LTExpandableContainer, LTText):
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def __init__(self, word_margin):
|
|
|
|
LTExpandableContainer.__init__(self)
|
|
|
|
self.word_margin = word_margin
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s %s %r>' %
|
2010-11-09 10:40:05 +00:00
|
|
|
(self.__class__.__name__, bbox2str(self.bbox), self.text))
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-11-09 10:40:05 +00:00
|
|
|
LTContainer.add(self, LTAnon('\n'))
|
|
|
|
self.text = ''.join( obj.text for obj in self if isinstance(obj, LTText) )
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTExpandableContainer.analyze(self, laparams)
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-03-21 02:21:37 +00:00
|
|
|
def find_neighbors(self, plane, ratio):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
class LTTextLineHorizontal(LTTextLine):
|
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def __init__(self, word_margin):
|
|
|
|
LTTextLine.__init__(self, word_margin)
|
|
|
|
self._x1 = +INF
|
|
|
|
return
|
|
|
|
|
|
|
|
def add(self, obj):
|
|
|
|
if isinstance(obj, LTChar) and self.word_margin:
|
|
|
|
margin = self.word_margin * obj.width
|
|
|
|
if self._x1 < obj.x0-margin:
|
|
|
|
LTContainer.add(self, LTAnon(' '))
|
|
|
|
self._x1 = obj.x1
|
|
|
|
LTTextLine.add(self, obj)
|
2010-03-20 05:43:34 +00:00
|
|
|
return
|
|
|
|
|
2010-03-21 02:21:37 +00:00
|
|
|
def find_neighbors(self, plane, ratio):
|
|
|
|
h = ratio*self.height
|
2010-10-17 05:13:33 +00:00
|
|
|
objs = plane.find((self.x0, self.y0-h, self.x1, self.y1+h))
|
|
|
|
return [ obj for obj in objs if isinstance(obj, LTTextLineHorizontal) ]
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
class LTTextLineVertical(LTTextLine):
|
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def __init__(self, word_margin):
|
|
|
|
LTTextLine.__init__(self, word_margin)
|
|
|
|
self._y0 = -INF
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def add(self, obj):
|
|
|
|
if isinstance(obj, LTChar) and self.word_margin:
|
|
|
|
margin = self.word_margin * obj.height
|
|
|
|
if obj.y1+margin < self._y0:
|
|
|
|
LTContainer.add(self, LTAnon(' '))
|
|
|
|
self._y0 = obj.y0
|
|
|
|
LTTextLine.add(self, obj)
|
|
|
|
return
|
|
|
|
|
2010-03-21 02:21:37 +00:00
|
|
|
def find_neighbors(self, plane, ratio):
|
|
|
|
w = ratio*self.width
|
2010-10-17 05:13:33 +00:00
|
|
|
objs = plane.find((self.x0-w, self.y0, self.x1+w, self.y1))
|
|
|
|
return [ obj for obj in objs if isinstance(obj, LTTextLineVertical) ]
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2009-05-15 14:25:32 +00:00
|
|
|
## LTTextBox
|
2009-05-05 12:26:29 +00:00
|
|
|
##
|
2009-05-15 14:25:32 +00:00
|
|
|
## A set of text objects that are grouped within
|
2009-05-05 12:26:29 +00:00
|
|
|
## a certain rectangular area.
|
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTTextBox(LTExpandableContainer):
|
2009-05-05 12:26:29 +00:00
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
def __init__(self):
|
|
|
|
LTExpandableContainer.__init__(self)
|
2010-03-22 04:00:18 +00:00
|
|
|
self.index = None
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s(%s) %s %r...>' %
|
|
|
|
(self.__class__.__name__, self.index,
|
2010-11-09 10:40:05 +00:00
|
|
|
bbox2str(self.bbox), self.text[:20]))
|
2009-05-05 12:26:29 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-11-09 10:40:05 +00:00
|
|
|
self.text = ''.join( obj.text for obj in self if isinstance(obj, LTTextLine) )
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTExpandableContainer.analyze(self, laparams)
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
class LTTextBoxHorizontal(LTTextBox):
|
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-11-09 10:40:05 +00:00
|
|
|
self._objs = csort(self._objs, key=lambda obj: -obj.y1)
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTTextBox.analyze(self, laparams)
|
2009-05-04 08:29:36 +00:00
|
|
|
|
2010-11-14 10:07:41 +00:00
|
|
|
def get_writing_mode(self):
|
|
|
|
return 'lr-tb'
|
|
|
|
|
2010-03-20 05:43:34 +00:00
|
|
|
class LTTextBoxVertical(LTTextBox):
|
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-11-09 10:40:05 +00:00
|
|
|
self._objs = csort(self._objs, key=lambda obj: -obj.x1)
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTTextBox.analyze(self, laparams)
|
2009-05-05 12:26:29 +00:00
|
|
|
|
2010-11-14 10:07:41 +00:00
|
|
|
def get_writing_mode(self):
|
|
|
|
return 'tb-rl'
|
|
|
|
|
2009-07-21 07:55:19 +00:00
|
|
|
|
2010-03-22 04:00:18 +00:00
|
|
|
## LTTextGroup
|
2010-03-21 02:21:37 +00:00
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTTextGroup(LTExpandableContainer):
|
2010-03-21 02:21:37 +00:00
|
|
|
|
|
|
|
def __init__(self, objs):
|
2010-11-09 10:40:05 +00:00
|
|
|
LTExpandableContainer.__init__(self)
|
|
|
|
self.extend(objs)
|
2010-03-21 02:21:37 +00:00
|
|
|
return
|
|
|
|
|
2010-03-25 11:38:47 +00:00
|
|
|
class LTTextGroupLRTB(LTTextGroup):
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-03-21 02:21:37 +00:00
|
|
|
# reorder the objects from top-left to bottom-right.
|
2010-12-26 08:26:39 +00:00
|
|
|
self._objs = csort(self._objs, key=lambda obj:
|
2011-02-14 13:32:55 +00:00
|
|
|
(1-laparams.boxes_flow)*(obj.x0) -
|
2010-12-26 08:26:39 +00:00
|
|
|
(1+laparams.boxes_flow)*(obj.y0+obj.y1))
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTTextGroup.analyze(self, laparams)
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2010-03-25 11:38:47 +00:00
|
|
|
class LTTextGroupTBRL(LTTextGroup):
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-03-21 02:21:37 +00:00
|
|
|
# reorder the objects from top-right to bottom-left.
|
2010-12-26 08:26:39 +00:00
|
|
|
self._objs = csort(self._objs, key=lambda obj:
|
|
|
|
-(1+laparams.boxes_flow)*(obj.x0+obj.x1)
|
2011-02-14 13:32:55 +00:00
|
|
|
-(1-laparams.boxes_flow)*(obj.y1))
|
2010-12-26 07:56:21 +00:00
|
|
|
return LTTextGroup.analyze(self, laparams)
|
2010-03-21 02:21:37 +00:00
|
|
|
|
|
|
|
|
2010-11-09 10:40:05 +00:00
|
|
|
## LTLayoutContainer
|
2010-03-21 02:21:37 +00:00
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTLayoutContainer(LTContainer):
|
2010-03-21 02:21:37 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def __init__(self, bbox):
|
2010-11-09 10:40:05 +00:00
|
|
|
LTContainer.__init__(self, bbox)
|
|
|
|
self.layout = None
|
2010-03-21 02:21:37 +00:00
|
|
|
return
|
2010-11-09 10:40:05 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
2010-06-19 03:56:50 +00:00
|
|
|
# textobjs is a list of LTChar objects, i.e.
|
|
|
|
# it has all the individual characters in the page.
|
2011-03-01 11:47:20 +00:00
|
|
|
(textobjs, otherobjs) = fsplit(lambda obj: isinstance(obj, LTChar), self._objs)
|
2010-10-17 05:13:33 +00:00
|
|
|
if not textobjs: return
|
2010-12-26 07:56:21 +00:00
|
|
|
textlines = list(self.get_textlines(laparams, textobjs))
|
2010-11-09 10:40:05 +00:00
|
|
|
assert len(textobjs) <= sum( len(line._objs) for line in textlines )
|
2011-03-01 11:47:20 +00:00
|
|
|
(empties, textlines) = fsplit(lambda obj: obj.is_empty(), textlines)
|
2010-12-26 07:56:21 +00:00
|
|
|
textboxes = list(self.get_textboxes(laparams, textlines))
|
2010-11-09 10:40:05 +00:00
|
|
|
assert len(textlines) == sum( len(box._objs) for box in textboxes )
|
2010-12-26 07:56:21 +00:00
|
|
|
top = self.group_textboxes(laparams, textboxes)
|
2010-03-22 04:00:18 +00:00
|
|
|
def assign_index(obj, i):
|
|
|
|
if isinstance(obj, LTTextBox):
|
|
|
|
obj.index = i
|
|
|
|
i += 1
|
|
|
|
elif isinstance(obj, LTTextGroup):
|
|
|
|
for x in obj:
|
|
|
|
i = assign_index(x, i)
|
|
|
|
return i
|
|
|
|
assign_index(top, 0)
|
|
|
|
textboxes.sort(key=lambda box:box.index)
|
2011-03-01 11:47:20 +00:00
|
|
|
self._objs = textboxes + otherobjs + empties
|
2010-03-22 04:00:18 +00:00
|
|
|
self.layout = top
|
2010-11-09 10:40:05 +00:00
|
|
|
return self
|
2009-10-24 04:41:59 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def get_textlines(self, laparams, objs):
|
2010-10-17 05:13:33 +00:00
|
|
|
obj0 = None
|
|
|
|
line = None
|
|
|
|
for obj1 in objs:
|
2010-10-17 05:13:39 +00:00
|
|
|
if obj0 is not None:
|
2010-10-17 05:13:33 +00:00
|
|
|
k = 0
|
2010-10-17 05:13:39 +00:00
|
|
|
if (obj0.is_compatible(obj1) and obj0.is_voverlap(obj1) and
|
2010-12-26 07:56:21 +00:00
|
|
|
min(obj0.height, obj1.height) * laparams.line_overlap < obj0.voverlap(obj1) and
|
|
|
|
obj0.hdistance(obj1) < max(obj0.width, obj1.width) * laparams.char_margin):
|
2010-10-17 05:13:33 +00:00
|
|
|
# obj0 and obj1 is horizontally aligned:
|
|
|
|
#
|
|
|
|
# +------+ - - -
|
|
|
|
# | obj0 | - - +------+ -
|
|
|
|
# | | | obj1 | | (line_overlap)
|
|
|
|
# +------+ - - | | -
|
|
|
|
# - - - +------+
|
|
|
|
#
|
|
|
|
# |<--->|
|
|
|
|
# (char_margin)
|
|
|
|
k |= 1
|
2011-02-02 14:09:34 +00:00
|
|
|
if (laparams.detect_vertical and
|
|
|
|
obj0.is_compatible(obj1) and obj0.is_hoverlap(obj1) and
|
2010-12-26 07:56:21 +00:00
|
|
|
min(obj0.width, obj1.width) * laparams.line_overlap < obj0.hoverlap(obj1) and
|
|
|
|
obj0.vdistance(obj1) < max(obj0.height, obj1.height) * laparams.char_margin):
|
2010-10-17 05:13:33 +00:00
|
|
|
# obj0 and obj1 is vertically aligned:
|
|
|
|
#
|
|
|
|
# +------+
|
|
|
|
# | obj0 |
|
|
|
|
# | |
|
|
|
|
# +------+ - - -
|
|
|
|
# | | | (char_margin)
|
|
|
|
# +------+ - -
|
|
|
|
# | obj1 |
|
|
|
|
# | |
|
|
|
|
# +------+
|
|
|
|
#
|
|
|
|
# |<-->|
|
|
|
|
# (line_overlap)
|
|
|
|
k |= 2
|
|
|
|
if ( (k & 1 and isinstance(line, LTTextLineHorizontal)) or
|
|
|
|
(k & 2 and isinstance(line, LTTextLineVertical)) ):
|
|
|
|
line.add(obj1)
|
2010-10-17 05:13:39 +00:00
|
|
|
elif line is not None:
|
2010-12-26 07:56:21 +00:00
|
|
|
yield line.analyze(laparams)
|
2010-10-17 05:13:33 +00:00
|
|
|
line = None
|
2010-10-17 05:13:39 +00:00
|
|
|
else:
|
|
|
|
if k == 2:
|
2010-12-26 07:56:21 +00:00
|
|
|
line = LTTextLineVertical(laparams.word_margin)
|
2010-10-17 05:13:39 +00:00
|
|
|
line.add(obj0)
|
|
|
|
line.add(obj1)
|
|
|
|
elif k == 1:
|
2010-12-26 07:56:21 +00:00
|
|
|
line = LTTextLineHorizontal(laparams.word_margin)
|
2010-10-17 05:13:39 +00:00
|
|
|
line.add(obj0)
|
|
|
|
line.add(obj1)
|
|
|
|
else:
|
2010-12-26 07:56:21 +00:00
|
|
|
line = LTTextLineHorizontal(laparams.word_margin)
|
2010-10-17 05:13:39 +00:00
|
|
|
line.add(obj0)
|
2010-12-26 07:56:21 +00:00
|
|
|
yield line.analyze(laparams)
|
2010-10-17 05:13:39 +00:00
|
|
|
line = None
|
|
|
|
obj0 = obj1
|
2010-10-17 05:13:33 +00:00
|
|
|
if line is None:
|
2010-12-26 07:56:21 +00:00
|
|
|
line = LTTextLineHorizontal(laparams.word_margin)
|
2010-10-17 05:13:39 +00:00
|
|
|
line.add(obj0)
|
2010-12-26 07:56:21 +00:00
|
|
|
yield line.analyze(laparams)
|
2010-10-17 05:13:33 +00:00
|
|
|
return
|
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def get_textboxes(self, laparams, lines):
|
2010-10-17 05:13:33 +00:00
|
|
|
plane = Plane(lines)
|
2010-11-09 10:40:05 +00:00
|
|
|
boxes = {}
|
|
|
|
for line in lines:
|
2010-12-26 07:56:21 +00:00
|
|
|
neighbors = line.find_neighbors(plane, laparams.line_margin)
|
2010-10-17 05:13:33 +00:00
|
|
|
assert line in neighbors, line
|
2010-11-09 10:40:05 +00:00
|
|
|
members = []
|
2010-10-17 05:13:33 +00:00
|
|
|
for obj1 in neighbors:
|
2010-11-09 10:40:05 +00:00
|
|
|
members.append(obj1)
|
|
|
|
if obj1 in boxes:
|
|
|
|
members.extend(boxes.pop(obj1))
|
2010-10-17 05:13:33 +00:00
|
|
|
if isinstance(line, LTTextLineHorizontal):
|
2010-11-09 10:40:05 +00:00
|
|
|
box = LTTextBoxHorizontal()
|
2010-10-17 05:13:33 +00:00
|
|
|
else:
|
2010-11-09 10:40:05 +00:00
|
|
|
box = LTTextBoxVertical()
|
|
|
|
for obj in uniq(members):
|
|
|
|
box.add(obj)
|
|
|
|
boxes[obj] = box
|
2010-10-17 05:13:33 +00:00
|
|
|
done = set()
|
|
|
|
for line in lines:
|
2010-11-09 10:40:05 +00:00
|
|
|
box = boxes[line]
|
|
|
|
if box in done: continue
|
|
|
|
done.add(box)
|
2010-12-26 07:56:21 +00:00
|
|
|
yield box.analyze(laparams)
|
2010-10-17 05:13:33 +00:00
|
|
|
return
|
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def group_textboxes(self, laparams, boxes):
|
2011-02-14 14:41:23 +00:00
|
|
|
def dist((x0,y0,x1,y1), obj1, obj2):
|
2010-06-19 03:56:50 +00:00
|
|
|
"""A distance function between two TextBoxes.
|
|
|
|
|
|
|
|
Consider the bounding rectangle for obj1 and obj2.
|
|
|
|
Return its area less the areas of obj1 and obj2,
|
|
|
|
shown as 'www' below. This value may be negative.
|
2011-02-14 14:41:23 +00:00
|
|
|
+------+..........+ (x1,y1)
|
|
|
|
| obj1 |wwwwwwwwww:
|
|
|
|
+------+www+------+
|
|
|
|
:wwwwwwwwww| obj2 |
|
|
|
|
(x0,y0) +..........+------+
|
2010-06-19 03:56:50 +00:00
|
|
|
"""
|
2011-02-14 14:41:23 +00:00
|
|
|
return ((x1-x0)*(y1-y0) - obj1.width*obj1.height - obj2.width*obj2.height)
|
2010-11-09 10:40:05 +00:00
|
|
|
boxes = boxes[:]
|
2011-04-21 13:07:04 +00:00
|
|
|
plane = Plane(boxes)
|
2011-02-27 03:56:28 +00:00
|
|
|
# XXX this is very slow when there're many textboxes.
|
2010-11-09 10:40:05 +00:00
|
|
|
while 2 <= len(boxes):
|
2011-02-27 03:56:28 +00:00
|
|
|
mindist = (INF,0)
|
2010-10-17 05:13:33 +00:00
|
|
|
minpair = None
|
2010-11-09 10:40:05 +00:00
|
|
|
boxes = csort(boxes, key=lambda obj: obj.width*obj.height)
|
|
|
|
for i in xrange(len(boxes)):
|
|
|
|
for j in xrange(i+1, len(boxes)):
|
|
|
|
(obj1, obj2) = (boxes[i], boxes[j])
|
2011-02-14 14:41:23 +00:00
|
|
|
b = (min(obj1.x0,obj2.x0), min(obj1.y0,obj2.y0),
|
|
|
|
max(obj1.x1,obj2.x1), max(obj1.y1,obj2.y1))
|
|
|
|
others = set(plane.find(b)).difference((obj1,obj2))
|
|
|
|
d = dist(b, obj1, obj2)
|
|
|
|
# disregard if there's any other object in between.
|
|
|
|
if 0 < d and others:
|
2011-02-27 03:56:28 +00:00
|
|
|
d = (1,d)
|
|
|
|
else:
|
|
|
|
d = (0,d)
|
2011-02-14 14:41:23 +00:00
|
|
|
if mindist <= d: continue
|
|
|
|
mindist = d
|
|
|
|
minpair = (obj1, obj2)
|
|
|
|
assert minpair is not None, boxes
|
2010-10-17 05:13:33 +00:00
|
|
|
(obj1, obj2) = minpair
|
2010-11-09 10:40:05 +00:00
|
|
|
boxes.remove(obj1)
|
|
|
|
boxes.remove(obj2)
|
2011-04-21 13:07:04 +00:00
|
|
|
plane.remove(obj1)
|
|
|
|
plane.remove(obj2)
|
2010-11-09 10:40:05 +00:00
|
|
|
if (isinstance(obj1, LTTextBoxVertical) or
|
2010-11-09 10:40:14 +00:00
|
|
|
isinstance(obj2, LTTextBoxVertical) or
|
|
|
|
isinstance(obj1, LTTextGroupTBRL) or
|
|
|
|
isinstance(obj2, LTTextGroupTBRL)):
|
2010-10-17 05:13:33 +00:00
|
|
|
group = LTTextGroupTBRL([obj1, obj2])
|
2010-11-09 10:40:05 +00:00
|
|
|
else:
|
|
|
|
group = LTTextGroupLRTB([obj1, obj2])
|
2011-04-21 13:07:04 +00:00
|
|
|
group.analyze(laparams)
|
|
|
|
boxes.append(group)
|
|
|
|
plane.add(group)
|
2010-11-09 10:40:05 +00:00
|
|
|
assert len(boxes) == 1
|
|
|
|
return boxes.pop()
|
2010-04-10 11:30:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
## LTFigure
|
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTFigure(LTLayoutContainer):
|
2010-04-10 11:30:03 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def __init__(self, name, bbox, matrix):
|
2010-08-29 06:39:31 +00:00
|
|
|
self.name = name
|
|
|
|
self.matrix = matrix
|
2010-04-10 11:30:03 +00:00
|
|
|
(x,y,w,h) = bbox
|
2010-10-17 05:13:39 +00:00
|
|
|
bbox = get_bound( apply_matrix_pt(matrix, (p,q))
|
|
|
|
for (p,q) in ((x,y), (x+w,y), (x,y+h), (x+w,y+h)) )
|
2010-12-26 07:56:21 +00:00
|
|
|
LTLayoutContainer.__init__(self, bbox)
|
2010-04-10 11:30:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s(%s) %s matrix=%s>' %
|
|
|
|
(self.__class__.__name__, self.name,
|
|
|
|
bbox2str(self.bbox), matrix2str(self.matrix)))
|
2010-04-10 11:30:03 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def analyze(self, laparams):
|
|
|
|
if not laparams.all_texts: return
|
|
|
|
return LTLayoutContainer.analyze(self, laparams)
|
2010-04-10 11:30:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
## LTPage
|
|
|
|
##
|
2010-11-09 10:40:05 +00:00
|
|
|
class LTPage(LTLayoutContainer):
|
2010-04-10 11:30:03 +00:00
|
|
|
|
2010-12-26 07:56:21 +00:00
|
|
|
def __init__(self, pageid, bbox, rotate=0):
|
|
|
|
LTLayoutContainer.__init__(self, bbox)
|
2010-04-10 11:30:03 +00:00
|
|
|
self.pageid = pageid
|
|
|
|
self.rotate = rotate
|
|
|
|
return
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-10-17 05:13:39 +00:00
|
|
|
return ('<%s(%r) %s rotate=%r>' %
|
|
|
|
(self.__class__.__name__, self.pageid,
|
|
|
|
bbox2str(self.bbox), self.rotate))
|