def calc_coords( text, layout, pos, clamp=1 ):
    closest = None
    y = 0
    for line_layout in layout:
        x = 0
        for seg in line_layout:
            s = LayoutSegment(seg)
            if s.offs is None:
                x += s.sc
                continue
            if s.offs == pos:
                return x,y
            if s.end is not None and s.offs<=pos and s.end>pos:
                x += calc_width( text, s.offs, pos )
                return x,y
            distance = abs(s.offs - pos)
            if s.end is not None and s.end<pos:
                distance = pos - (s.end-1)
            if closest is None or distance < closest[0]:
                closest = distance, (x,y)
            x += s.sc
        y += 1
    
    if closest:
        return closest[1]
    return 0,0
Calculate the coordinates closest to position pos in text with layout.
dataset/ETHPy150Open AnyMesh/anyMesh-Python/example/urwid/text_layout.py/calc_coords