o
     e                      @   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   L/var/www/html/venv/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'   a_lefta_topa_righta_bottomb_leftb_topb_rightb_bottomo_lefto_righto_bottomo_topo_widtho_heightr   r   r   get_bbox_overlap5   s   



r6   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 | |d< |d |d  |d< |d |d  |d< |S )	Nr    r   doctopr   r   widthr   height)r6   r   r   dict)r   r   overlapdimscopyattrdiffr   r   r   clip_objK   s   rD   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r6   r   .0r   r!   r   r   
<listcomp>b      " z#intersects_bbox.<locals>.<listcomp>r   rE   r   r   r!   r   intersects_bbox^      rN   c                    rF   )z:
    Filters objs to only those fully within the bbox
    c                    s&   g | ]}t t| t|kr|qS r   rH   rI   r!   r   r   rK   i   s
    zwithin_bbox.<locals>.<listcomp>r   rM   r   r!   r   within_bboxe   s   
rP   c                    rF   )z;
    Filters objs to only those fully outside the bbox
    c                    s"   g | ]}t t| d u r|qS rG   rH   rI   r!   r   r   rK   t   rL   z outside_bbox.<locals>.<listcomp>r   rM   r   r!   r   outside_bboxp   rO   rQ   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 rG   )rD   rI   r!   r   r   	<genexpr>|   s    zcrop_to_bbox.<locals>.<genexpr>)listfilterrM   r   r!   r   crop_to_bboxw   s   rU   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vrX   r   r   rY   r   r   r;   y0y1)	__class__tupleitems)r   rV   rW   	new_itemsr   r   r   move_object   s"   r`   rB   	tolerancec                    sd   ddddd  t | }t|t |} fdd|D } fddt||D }t tj| S )NrX   rY   r   r   r   r   c                    s&   g | ]}t tt |t| qS r   )sumr   r   lenrJ   cluster)rB   r   r   rK      s   & z snap_objects.<locals>.<listcomp>c                    s&   g | ]\}  fd d|D qS )c                    s    g | ]}t ||   qS r   )r`   rI   )rB   avgrV   r   r   rK      s     z+snap_objects.<locals>.<listcomp>.<listcomp>r   re   rB   rV   )rg   r   rK      s    )rS   r   r   r#   	itertoolschain)rE   rB   ra   	list_objsclustersavgssnapped_clustersr   rh   r   snap_objects   s   ro   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 )Nrb   r   r   r<   r   r   r;   r=   r[   rZ   )appendr\   r]   r^   )r   rp   rW   	old_valuerC   r_   r   r   r   resize_object   s0   rs   curvec                    s,   t  d  d dd  } fdd|D S )N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 )	
curve_edger   r   r;   r   rY   rX   N)	object_typer   r   r   r;   r   r<   r=   orientation)r$   r%   abs)rJ   p0p1rt   r   r   rK      s     *z"curve_to_edges.<locals>.<listcomp>)r#   )rt   point_pairsr   r|   r   curve_to_edges   s   
r~   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   r>   )rJ   xr   r   r   rK      s    z!rect_to_edges.<locals>.<listcomp>   	rect_edger   r[   r   rX   )rw   r=   rZ   r   rx   rZ   r=   r;   )rw   r=   r[   r   r;   rx   r   rY   )rw   r<   r   rx   r   )rw   r<   r   rx   )rangeupdate)r   r   r   r8   r9   r   r   r   rect_to_edges   sB   	
r   linec                 C   s(   t | }| d | d krdnd|d< |S )Nr   r   rX   rY   rx   r   )r   edger   r   r   line_to_edge   s   r   c                 C   s:   | d }d|v r| gS |dkrt | gS ttd| | S )Nrw   _edger   )r   rt   )r   r   r~   )r   tr   r   r   obj_to_edges   s   
r   edgesrx   	edge_type
min_lengthc                    s8   dvrt ddtdtf fdd}tt|| S )N)rY   rX   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 )Nrx   rY   r=   r<   rw   T)bool)r   dim
et_correctorient_correctr   r   rx   r   r   test  s   zfilter_edges.<locals>.test)r7   r	   r   rS   rT   )r   rx   r   r   r   r   r   r   filter_edges  s   r   )NNr   )$ri   operatorr   typingr   r   r   _typingr   r   r	   r
   
clusteringr   strr   r   r   r   r   r   r6   r:   rD   rN   rP   rQ   rU   r`   ro   rs   r~   r   r   r   r   r   r   r   r   <module>   sN    		(