+
    f0i                      \   ^ RI t ^ RIHt ^ RIHtHtHt ^RIHtH	t	H
t
Ht ^RIHt R R ltR R	 lt]! R
RRR4      tR R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR  R! ltR" R# ltR$ R% ltR& R' ltR( R) ltR* R+ ltR, R- lt R. R/ lt!R2R0 R1 llt"R# )3    N)
itemgetter)DictIterableOptional)T_bboxT_numT_obj
T_obj_list)cluster_objectsc                h    V ^8  d   QhR\         \        ,          R\        \        \        3,          /#    objectsreturn)r   r	   r   strr   )formats   "_/Users/agent/.openclaw/workspace/venv/lib/python3.14/site-packages/pdfplumber/utils/geometry.py__annotate__r   	   s'     2 2Xe_ 2c5j1A 2    c                *    \        \        V 4      4      # )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   s   &r   objects_to_rectr   	   s     011r   c                F    V ^8  d   QhR\         \        ,          R\        /# r   )r   r	   r   )r   s   "r   r   r      s     3 3Xe_ 3 3r   c                4    \        \        \        V 4      4      # )zX
Given an iterable of objects, return the smallest bounding box that
contains them all.
)merge_bboxesmapbbox_getterr   s   &r   r   r      s    
 K122r   x0topx1bottomc                0    V ^8  d   QhR\         R\        /# r   objr   )r	   r   )r   s   "r   r   r      s      U v r   c                    \        V 4      pV# )z(
Return the bounding box for an object.
)r   )r&   bboxs   & r   obj_to_bboxr)      s     s#DKr   c                R    V ^8  d   QhR\         R\        \        \        3,          /# r   r(   r   )r   r   r   r   )r   s   "r   r   r   %   s'     M Mv M$sEz"2 Mr   c           	     N    RV ^ ,          RV ^,          RV ^,          RV ^,          /# )zX
Return the rectangle (i.e a dict with keys "x0", "top", "x1",
"bottom") for an object.
r    r!   r"   r#    )r(   s   &r   r   r   %   s+    
 $q'5$q'4a(DGLLr   c                F    V ^8  d   QhR\         \        ,          R\        /# )r   bboxesr   )r   r   )r   s   "r   r   r   -   s     5 5&) 5f 5r   c                n    \        V !  w  rr4\        V4      \        V4      \        V4      \        V4      3# )z_
Given an iterable of bounding boxes, return the smallest bounding box
that contains them all.
)zipminmax)r/   r    r!   r"   r#   s   &    r   r   r   -   s1    
 v,BRGSXs2wF44r   c                R    V ^8  d   QhR\         R\         R\        \         ,          /# )r   abr   )r   r   )r   s   "r   r   r   6   s&       6 hv.> r   c                     V w  r#rEVw  rgr\        W&4      p
\        WH4      p\        WY4      p\        W74      pW,
          pW,
          pV^ 8  d   V^ 8  d   W,           ^ 8  d   WW3# R# )r   N)r3   r2   )r5   r6   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_heights   &&              r   get_bbox_overlaprF   6   ss    '($F7'($F7 F'#G8&HEGH1}A(*<q*@w11r   c                0    V ^8  d   QhR\         R\        /# r+   )r   r   )r   s   "r   r   r   E   s     + + +E +r   c                 j    V w  rr4W8  g   W$8  d   \        V  R 24      hW1,
          WB,
          ,          # )z  has a negative width or height.)
ValueError)r(   leftr!   rightr#   s   &    r   calculate_arearL   E   s7    #Du|s|D6!ABCCLV\**r   c                R    V ^8  d   QhR\         R\        R\        \         ,          /# )r   r&   r(   r   )r	   r   r   )r   s   "r   r   r   L   s%      % v (5/ r   c                 X   \        \        V 4      V4      pVf   R # \        V4      p\        V 4      pR F  pW5,          WE&   K  	  VR,          V R,          ,
          pRV9   d   V R,          V,           VR&   VR,          VR,          ,
          VR&   VR,          VR,          ,
          VR&   V# )	Nr    r!   r"   r#   doctopwidthheight)r    r!   r"   r#   )rF   r)   r   dict)r&   r(   overlapdimscopyattrdiffs   &&     r   clip_objrX   L   s    {3/6G D9D-Z
 . ;U#D4X-XJd+DM(^d5k1DNKr   c                R    V ^8  d   QhR\         \        ,          R\        R\        /# r   objsr(   r   r   r	   r   r
   )r   s   "r   r   r   `   s*     Y Y(5/ Y YJ Yr   c                b    V  Uu. uF  p\        \        V4      V4      f   K  VNK   	  up# u upi )z2
Filters objs to only those intersecting the bbox
rF   r)   r[   r(   r&   s   && r   intersects_bboxr`   `   s-      X4C#3K4Dd#KCC4XXX   ,,c                R    V ^8  d   QhR\         \        ,          R\        R\        /# rZ   r\   )r   s   "r   r   r   g   s%      huo V 
 r   c                z    V  Uu. uF*  p\        \        V4      V4      \        V4      8X  g   K(  VNK,  	  up# u upi )z2
Filters objs to only those fully within the bbox
r^   r_   s   && r   within_bboxrd   g   sD     CK,d3{37GG 	  s   %88c                R    V ^8  d   QhR\         \        ,          R\        R\        /# rZ   r\   )r   s   "r   r   r   r   s*     U Ux Uf U Ur   c                b    V  Uu. uF  p\        \        V4      V4      e   K  VNK   	  up# u upi )z3
Filters objs to only those fully outside the bbox
r^   r_   s   && r   outside_bboxrg   r   s-      T4C#3K4Dd#KCC4TTTra   c                R    V ^8  d   QhR\         \        ,          R\        R\        /# rZ   r\   )r   s   "r   r   r   y   s*     E Ex Ef E Er   c                B   a \        \        RV3R lV  4       4      4      # )zd
Filters objs to only those intersecting the bbox,
and crops the extent of the objects to the bbox.
Nc              3   <   <"   T F  p\        VS4      x  K  	  R # 5i)N)rX   ).0r&   r(   s   & r   	<genexpr>crop_to_bbox.<locals>.<genexpr>~   s     BTchsD11Ts   )listfilter)r[   r(   s   &fr   crop_to_bboxrp   y   s    
 tBTBCDDr   c                H    V ^8  d   QhR\         R\        R\        R\         /# )r   r&   axisvaluer   r	   r   r   )r   s   "r   r   r      s.     @ @U @# @e @ @r   c                    VR	9   g   Q hVR 8X  d%   RV R,          V,           3RV R,          V,           3.pVR8X  dx   RV R,          V,           3RV R,          V,           3.pRV 9   d   VRV R,          V,           3.,          pRV 9   d,   VRV R,          V,
          3RV R,          V,
          3.,          pV P                  \        V P                  4       4      \        X4      ,           4      # )
hvr    r"   r!   r#   rO   y0y1)rv   rw   )	__class__tupleitems)r&   rr   rs   	new_itemss   &&& r   move_objectr~      s    :s{3t9u$%3t9u$%
	 s{CJ&'s8}u,-
	 s?8S]U%:;<<I3;s4y5()s4y5() I ==syy{+eI.>>??r   c                ^    V ^8  d   QhR\         \        ,          R\        R\        R\        /# )r   r[   rV   	tolerancer   )r   r	   r   r   r
   )r   s   "r   r   r      s,     	4 	4x 	4c 	4e 	4
 	4r   c                    R RRRRRRR/V,          p\        V 4      p\        V\        V4      V4      pV Uu. uF1  p\        \	        \        V4      V4      4      \        V4      ,          NK3  	  pp\        WW4       UUU	u. uF.  w  rhV U	u. uF  p	\        WWV,          ,
          4      NK  	  up	NK0  	  p
ppp	\        \        P                  ! V
!  4      # u upi u up	i u up	ppi )r    rv   r"   r!   rw   r#   )
rn   r   r   sumr   lenr1   r~   	itertoolschain)r[   rV   r   rr   	list_objsclustersclusteravgsavgr&   snapped_clusterss   &&&        r   snap_objectsr      s    #tS%h<TBDT
Iy*T*:IFHNVWh7CJt$g./#g,>>hDW  //LG =DDGSS$i	0GD/   	!1233 XDs   7C?C
"C,CCc                H    V ^8  d   QhR\         R\        R\        R\         /# )r   r&   keyrs   r   rt   )r   s   "r   r   r      s.     @ @u @3 @u @ @r   c                 L   VR	9   g   Q hW,          pW#,
          pW3.pVR 8X  d3   W R,          8:  g   Q hVP                  RV R,          V,
          34       EMVR8X  d1   W R ,          8  g   Q hVP                  RW R ,          ,
          34       MVR8X  d{   W R,          8:  g   Q hVP                  RV R,          V,           34       VP                  RV R,          V,
          34       RV 9   d"   VP                  RV R,          V,
          34       M_VR8X  dY   W R,          8  g   Q hVP                  RV R,          V,           34       RV 9   d"   VP                  RV R,          V,
          34       V P                  \        V P                  4       4      \        V4      ,           4      # )
r    r"   r!   r#   rP   rO   rQ   ry   rx   )r    r"   r!   r#   )appendrz   r{   r|   )r&   r   rs   	old_valuerW   r}   s   &&&   r   resize_objectr      s   ////ID	I d{D	!!!'3t9u#456	D	!!!'5t9#456	H%%%(CMD$89:(CMD$89:3;dCI$456	E
"""(CMD$89:3;dCI$456==syy{+eI.>>??r   c                0    V ^8  d   QhR\         R\        /# )r   curver   r	   r
   )r   s   "r   r   r      s      % J r   c                    \        V R ,          V R ,          R,          4      pV UUu. uEF  w  r#RRR\        V^ ,          V^ ,          4      R\        V^ ,          V^ ,          4      R\        V^,          V^,          4      R\        V^,          V^,          4      V R,          V R,          ,
          ,           R\        V^,          V^,          4      R	\        V^ ,          V^ ,          ,
          4      R
\        V^,          V^,          ,
          4      RV^ ,          V^ ,          8X  d   RMV^,          V^,          8X  d   RMR/	NEK  	  upp# u uppi )pts:   NNobject_type
curve_edger    r"   r!   rO   r#   rP   rQ   orientationrw   rv   N)r1   r2   r3   abs)r   point_pairsp0p1s   &   r   curve_to_edgesr      s   eElE%L$45K " "FB <#beRU##beRU#3r!ube$c"Q%A'5?U5\+IJc"Q%A'SAA'c"Q%"Q%-("Q%2a5.3ber!unsRV
	
 "  s   DE
c                0    V ^8  d   QhR\         R\        /# )r   rectr   r   )r   s   "r   r   r      s     %& %& %&* %&r   c                    \        ^4       Uu. uF  p\        V 4      NK  	  upw  r#rEVP                  RRR^ RV R,          RV R,          RR	/4       VP                  RRR^ RV R,          RV R,          V R,          ,           R
V R
,          V R,          ,           RR	/4       VP                  RRR^ RV R,          RR/4       VP                  RRR^ RV R,          RR/4       W#WE.# u upi )   r   	rect_edgerQ   rx   ry   r#   r!   r   rv   rO   rP   r"   r    rw   )rangerR   update)r   xr!   r#   rJ   rK   s   &     r   rect_to_edgesr      s   49!H=HqT
H=CJJ;a$t*d5k3	
 MM;a$t*4;h/d8ntH~53	
	 	KK;Q$t*3		
 
LL;Q$t*3		
 %%I  >s   C#c                0    V ^8  d   QhR\         R\         /# )r   liner   )r	   )r   s   "r   r   r      s      u  r   c                 T    \        V 4      pV R ,          V R,          8X  d   RMRVR&   V# )r!   r#   rv   rw   r   )rR   )r   edges   & r   line_to_edger      s+    :D"&u+h"?#cDKr   c                0    V ^8  d   QhR\         R\        /# r%   r   )r   s   "r   r   r      s     H He H
 Hr   c                     V R ,          pRV9   d   V .# VR8X  d   \        V 4      .# R\        R\        /V,          ! V 4      # )r   _edger   r   r   )r   r   r   )r&   ts   & r   obj_to_edgesr      sH    MA!|u	
fS!""w?B3GGr   c          
          V ^8  d   QhR\         \        ,          R\        \        ,          R\        \        ,          R\        R\
        /# )r   edgesr   	edge_type
min_lengthr   )r   r	   r   r   r   r
   )r   s   "r   r   r     sF     % %E?%#% }% 	%
 %r   c                 j   aaa SR9  d   \        R4      hR VVV3R llp\        \        W@4      4      # )rw   zOrientation must be 'v' or 'h'c                0    V ^8  d   QhR\         R\        /# )r   er   )r	   bool)r   s   "r   r   "filter_edges.<locals>.__annotate__  s     N N N$ Nr   c                    < V R ,          R8X  d   RMRpSe   V R,          S8H  MRpSRJ ;'       g    V R ,          S8H  p\        T;'       d    T;'       d    W,          S8  4      # )r   rw   rQ   rP   Nr   T)r   )r   dim
et_correctorient_correctr   r   r   s   &   r   testfilter_edges.<locals>.test  sk    M*c1hw6?6KQ}%2QU
$,OO-0@K0OJLL>LLqv7KMMr   )rw   rv   N)rI   rn   ro   )r   r   r   r   r   s   &fff r   filter_edgesr     s8     **9::N N t#$$r   )NNr   )#r   operatorr   typingr   r   r   _typingr   r   r	   r
   
clusteringr   r   r   r   r)   r   r   rF   rL   rX   r`   rd   rg   rp   r~   r   r   r   r   r   r   r   r-   r   r   <module>r      s      + + 6 6 '23 udH5M5+(YUE@,	4@6$%&PH% %r   