o
    ީZh                      @   sN  d dl Z d dlmZ d dlmZmZmZ ddlmZm	Z	m
Z
mZ ddlmZ dee
 d	eee	f fd
dZdee
 d	efddZeddddZde
d	efddZded	eee	f fddZdee d	efddZdeded	ee fddZded	e	fdd Zde
ded	ee
 fd!d"Zd#ee
 ded	efd$d%Zd#ee
 ded	efd&d'Zd#ee
 ded	efd(d)Zd#ee
 ded	efd*d+Zde
d,ed-e	d	e
fd.d/Zd#ee
 d0ed1e	d	efd2d3Zde
d4ed-e	d	e
fd5d6Zd7e
d	efd8d9Zd:e
d	efd;d<Z d=e
d	e
fd>d?Z!de
d	efd@dAZ"			dHdBee
 dCee dDee dEe	d	ef
dFdGZ#dS )I    N)
itemgetter)DictIterableOptional   )T_bboxT_numT_obj
T_obj_list   )cluster_objectsobjectsreturnc                 C   s   t t| S )z
    Given an iterable of objects, return the smallest rectangle (i.e. a
    dict with "x0", "top", "x1", and "bottom" keys) that contains them
    all.
    )bbox_to_rectobjects_to_bboxr    r   P/var/www/html/lang_env/lib/python3.10/site-packages/pdfplumber/utils/geometry.pyobjects_to_rect	   s   r   c                 C   s   t tt| S )zd
    Given an iterable of objects, return the smallest bounding box that
    contains them all.
    )merge_bboxesmapbbox_getterr   r   r   r   r      s   r   x0topx1bottomobjc                 C   s   t | S )z0
    Return the bounding box for an object.
    )r   )r   r   r   r   obj_to_bbox   s   r   bboxc                 C   s   | d | d | d | d dS )zd
    Return the rectangle (i.e a dict with keys "x0", "top", "x1",
    "bottom") for an object.
    r   r   r      r   r   r   r   r   r   r   r   r   r   $   s   r   bboxesc                 C   s,   t |  \}}}}t|t|t|t|fS )zk
    Given an iterable of bounding boxes, return the smallest bounding box
    that contains them all.
    )zipminmax)r"   r   r   r   r   r   r   r   r   ,   s   r   abc                 C   s|   | \}}}}|\}}}}	t ||}
t||}t||	}t ||}||
 }|| }|dkr<|dkr<|| dkr<|
|||fS d S )Nr   )r%   r$   )r&   r'   Za_leftZa_topZa_rightZa_bottomZb_leftZb_topZb_rightZb_bottomZo_leftZo_rightZo_bottomZo_topZo_widthZo_heightr   r   r   get_bbox_overlap5   s   



r(   c                 C   s:   | \}}}}||ks||krt |  d|| ||  S )Nz  has a negative width or height.)
ValueError)r   leftr   rightr   r   r   r   calculate_areaD   s   r,   c                 C   s   t t| |}|d u rd S t|}t| }dD ]}|| ||< q|d | d  }d|v r4| d | |d< |d |d  |d< |d |d  |d< |S )	Nr    r   doctopr   r   widthr   height)r(   r   r   dict)r   r   overlapdimscopyattrdiffr   r   r   clip_objK   s   r6   objsc                        fdd| D S )z:
    Filters objs to only those intersecting the bbox
    c                    s"   g | ]}t t| d ur|qS Nr(   r   .0r   r!   r   r   
<listcomp>c      " z#intersects_bbox.<locals>.<listcomp>r   r7   r   r   r!   r   intersects_bbox_      r@   c                    r8   )z:
    Filters objs to only those fully within the bbox
    c                    s&   g | ]}t t| t|kr|qS r   r:   r;   r!   r   r   r=   j   s
    zwithin_bbox.<locals>.<listcomp>r   r?   r   r!   r   within_bboxf   s   
rB   c                    r8   )z;
    Filters objs to only those fully outside the bbox
    c                    s"   g | ]}t t| d u r|qS r9   r:   r;   r!   r   r   r=   u   r>   z outside_bbox.<locals>.<listcomp>r   r?   r   r!   r   outside_bboxq   rA   rC   c                    s   t td fdd| D S )zp
    Filters objs to only those intersecting the bbox,
    and crops the extent of the objects to the bbox.
    Nc                 3   s    | ]}t | V  qd S r9   )r6   r;   r!   r   r   	<genexpr>}   s    zcrop_to_bbox.<locals>.<genexpr>)listfilterr?   r   r!   r   crop_to_bboxx   s   rG   axisvaluec                 C   s   |dv sJ |dkrd| d | fd| d | fg}|dkrSd| d | fd| d | fg}d| v r=|d| d | fg7 }d	| v rS|d	| d	 | fd
| d
 | fg7 }|  t|  t| S )N)hvrJ   r   r   rK   r   r   r-   y0y1)	__class__tupleitems)r   rH   rI   	new_itemsr   r   r   move_object   s"   rR   r4   	tolerancec                    sd   ddddd  t | }t|t |} fdd|D } fddt||D }t tj| S )NrJ   rK   r   r   r   r   c                    s&   g | ]}t tt |t| qS r   )sumr   r   lenr<   Zcluster)r4   r   r   r=      s   & z snap_objects.<locals>.<listcomp>c                    s&   g | ]\}  fd d|D qS )c                    s    g | ]}t ||   qS r   )rR   r;   )r4   avgrH   r   r   r=      s     z+snap_objects.<locals>.<listcomp>.<listcomp>r   rW   r4   rH   )rX   r   r=      s    )rE   r   r   r#   	itertoolschain)r7   r4   rS   Z	list_objsZclustersZavgsZsnapped_clustersr   rY   r   snap_objects   s   r\   keyc                 C   sP  |dv sJ | | }|| }||fg}|dkr+|| d ksJ | d| d | f np|dkrC|| d ks7J | d|| d  f nX|dkru|| d ksOJ | d| d | f | d| d | f d	| v rt| d	| d	 | f n&|dkr|| d ksJ | d| d | f d
| v r| d
| d
 | f | t|  t| S )NrT   r   r   r.   r   r   r-   r/   rM   rL   )appendrN   rO   rP   )r   r]   rI   	old_valuer5   rQ   r   r   r   resize_object   s0   r`   curvec                    s,   t  d  d dd  } fdd|D S )NZptsr   c                    s   g | ]_\}}d t |d |d t|d |d t |d |d t |d |d  d  d   t|d |d t|d |d  t|d |d  |d |d krSdn|d |d kr]dndd	qS )	Z
curve_edger   r   r-   r   rK   rJ   N)	object_typer   r   r   r-   r   r.   r/   orientation)r$   r%   abs)r<   p0p1ra   r   r   r=      s     *z"curve_to_edges.<locals>.<listcomp>)r#   )ra   Zpoint_pairsr   rg   r   curve_to_edges   s   
rh   rectc              	      s    fddt dD \}}}}|dd d  d dd	 |dd d
  d  d   d  d  dd |dd d dd |dd d dd ||||gS )Nc                    s   g | ]}t  qS r   r0   )r<   xri   r   r   r=      s    z!rect_to_edges.<locals>.<listcomp>   Z	rect_edger   rM   r   rJ   )rb   r/   rL   r   rc   rL   r/   r-   )rb   r/   rM   r   r-   rc   r   rK   )rb   r.   r   rc   r   )rb   r.   r   rc   )rangeupdate)ri   r   r   r*   r+   r   rl   r   rect_to_edges   sB   	
rp   linec                 C   s(   t | }| d | d krdnd|d< |S )Nr   r   rJ   rK   rc   rj   )rq   edger   r   r   line_to_edge   s   rs   c                 C   s:   | d }d|v r| gS |dkrt | gS ttd| | S )Nrb   Z_edgerq   )ri   ra   )rs   rp   rh   )r   tr   r   r   obj_to_edges   s   
ru   edgesrc   	edge_type
min_lengthc                    s8   dvrt ddtdtf fdd}tt|| S )N)rK   rJ   NzOrientation must be 'v' or 'h'er   c                    sX   | d dkrdnd} d ur| d  knd}d u p| d k}t |o*|o*| | kS )Nrc   rK   r/   r.   rb   T)bool)ry   dimZ
et_correctZorient_correctrw   rx   rc   r   r   test  s   zfilter_edges.<locals>.test)r)   r	   rz   rE   rF   )rv   rc   rw   rx   r}   r   r|   r   filter_edges  s   r~   )NNr   )$rZ   operatorr   typingr   r   r   Z_typingr   r   r	   r
   Z
clusteringr   strr   r   r   r   r   r   r(   r,   r6   r@   rB   rC   rG   rR   r\   r`   rh   rp   rs   ru   r~   r   r   r   r   <module>   sN    		(