File size: 760 Bytes
f65b994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Read normalized baseline or contour coordinates from PAGE XML."""
import math

def points(node,width,height,polygon):
    if node is None:
        return "missing",()
    try:
        pts=[tuple(map(float,p.split(","))) for p in node.get("points","").split()]
        if any(len(p)!=2 or not all(map(math.isfinite,p)) for p in pts):
            return "invalid",()
    except ValueError:
        return "invalid",()
    if any(not (0<=x<=width and 0<=y<=height) for x,y in pts):
        return "outside",()
    if len(set(pts))<(3 if polygon else 2):
        return "degenerate",()
    if polygon and sum(a[0]*b[1]-a[1]*b[0] for a,b in zip(pts,pts[1:]+pts[:1]))==0:
        return "zero_area",()
    return "usable",tuple((x/width,y/height) for x,y in pts)