def METHOD_NAME(size, p1, p2):
    """Calculate position of the text relative to a line defined by points p1
    and p2.
    Parameters:
     - size: text size, a (width, height) tuple
     - p1:      beginning of line segment
     - p2:      end of line segment
    """
    ofs = 5
    dx = float(p2[0]) - float(p1[0])
    dy = float(p2[1]) - float(p1[1])
    name_w, name_h = size
    rc = 1000.0 if dy == 0 else dx / dy
    abs_rc = abs(rc)
    h = dx > 0  # right side of the box
    v = dy > 0  # bottom side
    if abs_rc > 6:
        # horizontal line
        name_dx = ofs if h else -ofs - name_w
        name_dy = -ofs - name_h
    elif 0 <= abs_rc <= 0.2:
        name_dx = -ofs - name_w
        # vertical line
        name_dy = ofs if v else -ofs - name_h
    else:
        # Should both items be placed on the same side of the line?
        r = abs_rc < 1.0
        # Find out alignment of text (depends on the direction of the line)
        align_left = h ^ r
        align_bottom = v ^ r
        name_dx = ofs if align_left else -ofs - name_w
        name_dy = -ofs - name_h if align_bottom else ofs
    return p1[0] + name_dx, p1[1] + name_dy