+
    f0iM                        ^ RI t ^ RIt^ 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HtHtHtHtHtHtHt ^ RIHt ^ RIHt ^ RIHtHt ^ RIHt ^R	IHtH t  ^R
I!H"t"H#t# ]PH                  ! ]%4      t&]
'       d   ^RI'H(t( ^RI)H*t* ]R.]+3,          t,R R lt- ! R R4      t.] ! R R].4      4       t/ ! R R]04      t1 ! R R].4      t2R# )    N)deque)asdict	dataclassfield)TYPE_CHECKINGAnyCallableDictIterableIteratorListOptionalPatternTupleUnion)
NumberTree)	PDFParser)	PDFObjRefresolve1)	PSLiteral)T_bboxT_obj)decode_textgeometry)Page)PDFPDFStructElementc                    V ^8  d   QhR\         R,          R\        \        \        \        ,          \        3,          R\
        R,          /# )   elementsr   matcherreturn)r   r   strr   	MatchFuncr   )formats   "Z/Users/agent/.openclaw/workspace/venv/lib/python3.14/site-packages/pdfplumber/structure.py__annotate__r'   '   sB     , ,)*,3i/0,  !,    c              #  f  a"   R V3R llpR V3R llp\        S\        4      '       d   TpM%\        S\        P                  4      '       d   TpMSp\	        V 4      pV'       dI   VP                  4       pV! V4      '       d   Vx  VP                  \        VP                  4      4       KP  R# 5i)z5
Common code for `find_all()` in trees and elements.
c                (    V ^8  d   QhRRR\         /# r   xr   r"   bool)r%   s   "r&   r'   _find_all.<locals>.__annotate__/   s     ! !' !D !r(   c                "   < V P                   S8H  # )zMatch an element name.)typer,   r!   s   &r&   	match_tag_find_all.<locals>.match_tag/   s    vv  r(   c                (    V ^8  d   QhRRR\         /# r+   r-   )r%   s   "r&   r'   r/   3   s     % %) %d %r(   c                :   < SP                  V P                  4      # )z,Match an element name by regular expression.)matchr1   r2   s   &r&   match_regex_find_all.<locals>.match_regex3   s    }}QVV$$r(   N)	
isinstancer#   rer   r   popleft
extendleftreversedchildren)r    r!   r3   r8   
match_funcdels   &f     r&   	_find_allrC   '   s     ! !% % '3
	GRZZ	(	( 

hA
YY[b>>H	Xbkk*+	 s   A#B1'B1,B1c                   T   a  ] tR t^Et o RtV 3R lR ltV 3R lR ltV 3R ltRtV t	R# )	FindablezNfind() and find_all() methods that can be inherited to avoid
repeating oneselfc                ^   < V ^8  d   QhRS[ S[S[S[,          S[3,          RS[R,          /# r   r!   r"   r   )r   r#   r   r$   r   )r%   __classdict__s   "r&   r'   Findable.__annotate__K   s3     	1 	1S'#,	9:	1	$	%	1r(   c                .    \        V P                  V4      # )zIterate depth-first over matching elements in subtree.

The `matcher` argument is either an element name, a regular
expression, or a function taking a `PDFStructElement` and
returning `True` if the element matches.
)rC   r?   selfr!   s   &&r&   find_allFindable.find_allK   s     00r(   c                ^   < V ^8  d   QhRS[ S[S[S[,          S[3,          RS[R,          /# rG   )r   r#   r   r$   r   )r%   rH   s   "r&   r'   rI   V   s3      S'#,	9:	$	%r(   c                f     \        \        V P                  V4      4      #   \         d     R# i ; i)zFind the first matching element in subtree.

The `matcher` argument is either an element name, a regular
expression, or a function taking a `PDFStructElement` and
returning `True` if the element matches.
N)nextrC   r?   StopIterationrK   s   &&r&   findFindable.findV   s/    		$--9:: 		s   ! 00c                4   < V ^8  d   Qh/ S[ R,          ;R&   # )r   r   r?   )r   )r%   rH   s   "r&   r'   rI   E   s      %&&	 r(    N)
__name__
__module____qualname____firstlineno____doc__rM   rS   __annotate_func____static_attributes____classdictcell__rH   s   @r&   rE   rE   E   s%     
	1 	1 #  r(   rE   c                      a  ] tR t^et o ]! ]R7      t]! ]R7      t]! ]R7      t	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R ltR	tV tR
# )r   )default_factoryc                .   < V ^8  d   QhRS[ R,          /# )r   r"   r   )r   )r%   rH   s   "r&   r'   PDFStructElement.__annotate__s   s     # #(#56 #r(   c                ,    \        V P                  4      # Niterr?   rL   s   &r&   __iter__PDFStructElement.__iter__s       DMM""r(   c                V   < V ^8  d   QhRS[ S[S[S[,          S[3,          ,          /# r   r"   )r   r   r   int)r%   rH   s   "r&   r'   rc   v   s'     0 08E(3-*<$=> 0r(   c              #  @  "   V P                    F  pV P                  V3x  K  	  \        V P                  4      pV'       dZ   VP	                  4       pVP                    F  pVP                  V3x  K  	  VP                  \        VP                  4      4       Ka  R# 5i)zqCollect all MCIDs (with their page numbers, if there are
multiple pages in the tree) inside a structure element.
N)mcidspage_numberr   r?   r<   r=   r>   )rL   mcidrA   rB   s   &   r&   	all_mcidsPDFStructElement.all_mcidsv   sy     
 JJD""D(( $-- Bnnd** !LL"++./	 s   A BABc                6   < V ^8  d   QhRS[ S[S[3,          /# rm   r
   r#   r   )r%   rH   s   "r&   r'   rc      s      c3h r(   c                :   \        V 4      p\        V.4      pV'       d|   VP                  4       p\        VP	                  4       4       F+  pW4,          e   W4,          . 8X  g   W4,          / 8X  g   K)  W4 K-  	  RV9   g   Ki  VP                  VR,          4       K  V# )z'Return a compacted dict representation.r?   )r   r   r<   listkeysextend)rL   rrA   rB   ks   &    r&   to_dictPDFStructElement.to_dict   ss    4L1#JB"'')_5=BERK25B; % RJ(r(   c                B  < V ^8  d   Qh/ S[ ;R&   S[S[,          ;R&   S[S[ ,          ;R&   S[S[ ,          ;R&   S[S[ ,          ;R&   S[S[ ,          ;R&   S[S[ ,          ;R&   S[S[,          ;R&   S[S[ S[3,          ;R	&   S[S[,          ;R
&   S[R,          ;R&   # )r   r1   revisionidlangalt_textactual_texttitlerq   
attributesrp   r   r?   )r#   r   rn   r
   r   r   )r%   rH   s   "r&   r'   rc   e   s     
I  sm  		 
 3-  sm  #  C=  #  S#X<  92  %&D r(   rV   N)rW   rX   rY   rZ   r   dictr   rx   rp   r?   ri   rs   r}   r\   r]   r^   r_   s   @r&   r   r   e   sJ      "'t!<JT2E).t)DH# #0 0 ?  r(   c                       ] tR t^tRtR# )StructTreeMissingrV   N)rW   rX   rY   rZ   r]   rV   r(   r&   r   r      s    r(   r   c                      a  ] tR t^t o RtRV 3R lR lltV 3R lR ltV 3R lR ltV 3R	 lR
 ltV 3R lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltV 3R ltRtV tR# )PDFStructTreeaN  Parse the structure tree of a PDF.

The constructor takes a `pdfplumber.PDF` and optionally a
`pdfplumber.Page`.  To avoid creating the entire tree for a large
document it is recommended to provide a page.

This class creates a representation of the portion of the
structure tree that reaches marked content sections, either for a
single page, or for the whole document.  Note that this is slightly
different from the behaviour of other PDF libraries which will
also include structure elements with no content.

If the PDF has no structure, the constructor will raise
`StructTreeMissing`.

Nc                2   < V ^8  d   QhRRRS[ R,          /# )r   docr   pager   r   )r%   rH   s   "r&   r'   PDFStructTree.__annotate__   s      ,& ,&E ,&&)9 ,&r(   c                  a VP                   V n         R V P                   P                  9  d   \        R4      h\        V P                   P                  R ,          4      V n        \        V P                  P                  R/ 4      4      V n        \        V P                  P                  R/ 4      4      V n        . V n        Ve   W n	        VP                  V/V n        RV n        V P                  P                  R4      pVf   V P                  4        R# \        V4      pRV P                  P                  P                   9  d   R# V P                  P                  P                   R,          o\        \#        V3R lVP$                   4       4      4      pV P'                  V4       R# RV n	        VP                   Uu/ uF  q"P                  VbK  	  upV n        V P                  P%                  4        Uu/ uF#  q"P                  P(                  VP                  bK%  	  upV n        V P                  4        R# u upi u upi )StructTreeRootzPDF has no structureRoleMapClassMapN
ParentTreeStructParentsc              3   >   <"   T F  w  rVS8X  g   K  Vx  K  	  R # 5ire   rV   ).0numarray	parent_ids   &  r&   	<genexpr>)PDFStructTree.__init__.<locals>.<genexpr>   s     X1C:3siGW1Cs   
)r   catalogr   r   rootgetrole_map	class_mapr?   r   rq   pages	page_dict_parse_struct_treer   page_objattrsrQ   values_parse_parent_treepageid)rL   r   r   parent_tree_objparent_treeparent_arrayr   s   &&&   @r&   __init__PDFStructTree.__init__   s   77488#3#33#$:;;TXX--.>?@	 y"!=>!$))--
B"?@02 I**D1DJ!DN #iimmL9O&'')(9 #$))*<*<*B*BB II..44_E	'X1C1CXX  ''5DI=@YYGYT**D0YGDJCG::CTCTCVCV4$$d&6&66CVDN ##%	 Hs   =H96)H>c                h   < V ^8  d   QhRS[ S[S[3,          RS[S[,          RS[ S[S[3,          /# )r   objr   r"   )r
   r#   r   r   rn   )r%   rH   s   "r&   r'   r      s9     / /S>/-5c]/	c3h/r(   c                6   . pR FV  pWA9  d   K  \        W,          4      p\        V\        4      '       d   VP                  V4       KE  VP	                  V4       KX  	  . pRpV FX  p\        V\
        4      '       d    W8X  d   Ve   VP	                  V4       RpK8  Ve   VP	                  V4       \        V4      pKZ  	  Ve   VP	                  V4       / p	V F  p\        V\        4      '       dQ   \        VP                  4      pW@P                  9  d   \        P                  RV4       KV  V P                  V,          pVP                  4        F>  w  r\        V\        4      '       d   \        VP                  4      W&   K4  W,          W&   K@  	  K  	  V	# )CNzUnknown attribute class %s)r   A)r   r:   rx   rz   appendrn   r   r   namer   loggerwarningitems)rL   r   r   attr_obj_listkeyattr_obj	attr_objsprev_objarefattrr|   vs   &&&         r&   _make_attributesPDFStructTree._make_attributes   s[    C~)H(D))$$X.$$X.  	!D $$$#(<$$X.'$$X.#D> " X& C#y))!#((+nn,NN#?EnnS)		a++)!&&1DG!fDG	 $  r(   c                \   < V ^8  d   QhRS[ RS[S[S[,          S[S[ ,          3,          /# r   r   r"   )r   r   r   r   r   )r%   rH   s   "r&   r'   r   
  s0     (! (! (!x8H/I4PS9/T)U (!r(   c                F   R V9  g   Q RV,          4       hRV9  g   Q RV,          4       hRpV P                   eL   RV9   dE   VR,          P                  pW0P                   9   g   Q RV,          4       hV P                   V,          pRpRV9   dS   \        VR,          P                  4      pW@P                  9   d'   \        V P                  V,          P                  4      pR	V9   d   \        VR	,          4      M. p\        V\        4      '       d   V.pM \        V\        4      '       d   VR	,          .pVP                  R
4      pV P                  W4      pRV9   d   \        \        VR,          4      4      MRpRV9   d   \        \        VR,          4      4      MRp	RV9   d   \        \        VR,          4      4      MRp
RV9   d   \        \        VR,          4      4      MRpRV9   d   \        \        VR,          4      4      MRp\        VVVVV
V	VVVR7	      pW3# )MCIDzUncaught MCR: %sObjzUncaught OBJR: %sNPgzObject on unparsed page: %s SKRIDTLangAlt
ActualText)	r1   r   rq   r   r   r   r   r   r   )r   objidr   r   r   r   r:   rn   r   r   r   r   )rL   r   rq   
page_objidobj_tagr?   r   r   
element_idr   r   r   r   elements   &&            r&   _make_elementPDFStructTree._make_element
  s   S :"4s":: C:!4s!::>>%$#+TJ/T1NQT1TT/..4K#:!#c(--0G--'%dmmG&<&A&AB),8CH%h$$ zH$''CzH773<**39
9=[#d)!45$
36#:HSX./45;s]{8CK018=;xE
34$8D8KK\!234QU 	 ###!

   r(   c                4   < V ^8  d   QhRS[ S[,          RR/# )r   r   r"   Nr   r   )r%   rH   s   "r&   r'   r   4  s     " "tCy "T "r(   c                   \        V4      p/ pRpV'       d   VP                  4       pV\        P                  8X  d   K/  \	        V4      V9   d   KA  \        V4      pRV9   d&   \        VR,          P                  4      R8X  d   RpKx  V P                  V4      w  rxVf   Q hWx3V\	        V4      &   VP                  VR,          4       K  V'       g   Q hV P                  V4       R# )zQPopulate the structure tree using the leaves of the parent tree for
a given page.FTyper   TNP)r   r<   r   KEYWORD_NULLreprr   r   r   r   r   _resolve_children)	rL   r   rA   s
found_rootrefr   r   r?   s	   &&       r&   r    PDFStructTree._parse_parent_tree4  s     ,
))+C i,,,CyA~3-C}S[-=-=!>BR!R!
 %)$6$6s$;!***&0$s)S"zq!r(   c                <   < V ^8  d   QhRS[ S[S[3,          RS[/# r   )r
   r#   r   r.   )r%   rH   s   "r&   r'   r   S  s#     
 
$sCx. 
T 
r(   c                    R V9  d   R# VR ,          P                   pV P                  e   W P                  9   # V P                  e'   W P                  P                  P                  8w  d   R# R# )r   TF)r   r   r   r   r   )rL   r   r   s   && r&   on_parsed_pagePDFStructTree.on_parsed_pageS  sV    s?Y__
>>%//99 YY//666r(   c                   < V ^8  d   QhRR/# )r   r"   NrV   )r%   rH   s   "r&   r'   r   _  s     B" B"D B"r(   c                P  a aa	 \        S P                  R,          4      p\        V\        4      '       d   S P                  R,          .p\	        V4      p/ o	V'       Ed#   VP                  4       p\        V4      S	9   d   K+  \        V4      p\        V\        4      '       d5   RV9   d.   S P                  V4      '       g   Kl  VR,          p\        V4      pS P                  V4      w  rVWV3S	\        V4      &   V F  p\        V4      p\        V\        4      '       d4   S P                  V4      '       g   K=  RV9   d   VR,          pM	RV9   d   KW  \        V\        4      '       g   Ko  VP                  V4       K  	  EK+  R VV	V 3R lloS! V4       S P                  S	4       R# )z_Populate the structure tree starting from the root, skipping
unparsed pages and empty elements.r   r   r   c                \    V ^8  d   QhR\         \        ,          R\         \        ,          /# )r   r    r"   r   )r%   s   "r&   r'   6PDFStructTree._parse_struct_tree.<locals>.__annotate__  s"     	! 	!DI 	!$s) 	!r(   c                   < . pV  F  p\        V4      p\        V\        4      '       d   VP                  V4       K7  \        V\        4      '       dK   SP                  V4      '       g   Kf  R V9   d   VP                  VR ,          4       K  RV9   d
   VR,          pS\        V4      ,          w  rES! V4      pVe	   V'       g   S\        V4       K  WE3S\        V4      &   VP                  V4       K  	  V# )r   r   )r   r:   rn   r   r   r   r   )	r    next_elementsr   r   r   r?   pruner   rL   s	   &     r&   r   /PDFStructTree._parse_struct_tree.<locals>.prune  s    Msmc3''!((-T**..s33 }%,,S[9 #!%j$%d3iL! ??($s)#*#4Ad3iL!((-)  * ! r(   N)r   r   r:   r   r   r<   r   r   r   r   r   r   )
rL   r   rA   r   r   r   r?   childr   r   s
   f       @@r&   r    PDFStructTree._parse_struct_tree_  sP    		#' dD!!IIcN#D$Ka))+CCyA~3-C#t$$#**3//%jsm $ 2 23 7G #,Ad3iL!uoc4((..s33 | #E
3 eY//HHUO "	! 	!2 	dq!r(   c                :   < V ^8  d   QhRS[ S[S[3,          RR/# )r   seenr"   Nrv   )r%   rH   s   "r&   r'   r     s'     *D *Dd38n *D *Dr(   c                   \        V P                  R,          4      p\        V\        4      '       d   V P                  R,          .p. V n        . pV Fp  p\        V4      p\        V\        4      '       d*   RV9   d#   V P                  V4      '       g   KD  VR,          p\        V4      V9   g   K_  VP                  V4       Kr  	  \        V4      pV'       EdK   VP                  4       pV\        V4      ,          w  rxVf   Q R4       hV EF  p	\        V	4      p\        V\        4      '       d   VP                  P                  V4       Mi\        V\        4      '       dT   V P                  V4      '       g   Kp  RV9   d$   VP                  P                  VR,          4       MRV9   d
   VR,          p	\        V	\        4      '       g   K  VP                  \        V	4      R4      w  rV
f   K  VP                  P                  V
4       VP                  V	4       EK  	  EKS  V Uu. uF  qA\        V4      ,          ^ ,          NK  	  upV n        R# u upi )zlResolve children starting from the tree root based on references we
saw when traversing the structure tree.
r   r   NzUnparsed elementr   )NN)r   r   r:   r   r?   r   r   r   r   r<   rn   rp   r   r   )rL   r   r   parsed_rootr   r   rA   r   r?   r   child_element_s   &&          r&   r   PDFStructTree._resolve_children  s    		#'dD!!IIcN#DC3-C#t$$#**3//%jCyD ""3'  +a))+C $T#YG&:(::&!uoc3''MM((-T**..s33 },,S[9# #E
eY//'+xxU\'J$M$0((//># "$ 8CC{d3i++{CCs   (!Ic                0   < V ^8  d   QhRS[ S[,          /# rm   )r   r   )r%   rH   s   "r&   r'   r     s     # #(#34 #r(   c                ,    \        V P                  4      # re   rf   rh   s   &r&   ri   PDFStructTree.__iter__  rk   r(   c                &   < V ^8  d   QhRS[ RS[/# )r   rB   r"   )r   r   )r%   rH   s   "r&   r'   r     s     -7 -7/ -7F -7r(   c                &   RpV P                   e   V P                   pM+VP                  e   V P                  VP                  ,          pVP                  P	                  RR4      pVe   Ve   ^RI HpHpHp V! V! V4      VP                  ^,          VP                  ^,          ,
          4      p\        W$4      '       dY   \        P                  ! V4      pVP                  V.4      pV'       g   \        R4      h\        P                  ! V^ ,          4      # V# . p	VP                  4        F  w  rV
f@   Ve9   \         P"                  P%                  VP&                  P)                  4       4      pMK. pMH\         P"                  P%                  V P                  V
,          P&                  P)                  4       4      pV F$  pVR,          V8X  g   K  V	P+                  V4       K&  	  K  	  V	'       g   \        R4      h\        P,                  ! V	4      # )z9Get the bounding box for an element for visual debugging.NBBox)CroppedPage_invert_box_normalize_boxzElement no longer on pagerr   zNo objects found)r   rq   r   r   r   r  r  r  mediaboxr:   r   bbox_to_rect_crop_fn
IndexErrorobj_to_bboxrs   	itertoolschainfrom_iterableobjectsr   r   objects_to_bbox)rL   rB   r   bboxr  r  r  rectrects	mcid_objsrq   rr   r  cs   &&            r&   element_bboxPDFStructTree.element_bbox  s   99 99D^^'::bnn-D}}  . 0FF
 t$dmmA&6q9I&ID $,,,,T2tf-$%@AA++E!H55 I%'\\^!&'"+//"?"?@S@S@U"V"$'oo;;

;/77>>@G !AyD(!((+ ! &4  !344++I66r(   c                4   < V ^8  d   Qh/ S[ R,          ;R&   # )r   r   r   r   )r%   rH   s   "r&   r'   r      s     $ 6
% r(   )r?   r   r   r   r   r   r   r   re   )rW   rX   rY   rZ   r[   r   r   r   r   r   r   r   ri   r  r\   r]   r^   r_   s   @r&   r   r      su     &,& ,&\/ /b(! (!T" ">
 
B" B"H*D *DX# #-7 -7w	  r(   r   )3r	  loggingr;   collectionsr   dataclassesr   r   r   typingr   r   r	   r
   r   r   r   r   r   r   r   pdfminer.data_structuresr   pdfminer.pdfparserr   pdfminer.pdftypesr   r   pdfminer.psparserr   _typingr   r   utilsr   r   	getLoggerrW   r   r   r   pdfr   r.   r$   rC   rE   r   
ValueErrorr   r   rV   r(   r&   <module>r#     s      	  0 0    0 ( 1 ' " (			8	$  ()4/0	,< @ *x * *Z	
 	h7H h7r(   