o
    ¾ÙøiM  ã                   @   sf  d dl Z d dlZ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mZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d	d
lmZm Z  d	dl!m"Z"m#Z# e $e%¡Z&e
rsd	dl'm(Z( d	dl)m*Z* edge+f Z,ded dee-ee- e,f ded fdd„Z.G dd„ dƒZ/eG dd„ de/ƒƒZ0G dd„ de1ƒZ2G dd„ de/ƒZ3dS )é    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ÚPDFStructElementÚelementsÚmatcherÚreturnc                 #   s’   � dddt f‡ fdd„}dddt f‡ fdd„}tˆ tƒr|}ntˆ tjƒr(|}nˆ }t| ƒ}|rG| ¡ }||ƒr;|V  | t|j	ƒ¡ |s0dS dS )	z=
    Common code for `find_all()` in trees and elements.
    Úxr   r    c                    s
   | j ˆ kS )zMatch an element name.)Útype©r!   ©r   © úa/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/pdfplumber/structure.pyÚ	match_tag/   s   
z_find_all.<locals>.match_tagc                    s   ˆ   | j¡S )z,Match an element name by regular expression.)Úmatchr"   r#   r$   r%   r&   Úmatch_regex3   s   z_find_all.<locals>.match_regexN)
ÚboolÚ
isinstanceÚstrÚrer   r   ÚpopleftÚ
extendleftÚreversedÚchildren)r   r   r'   r)   Ú
match_funcÚdÚelr%   r$   r&   Ú	_find_all'   s   €
ür5   c                   @   sf   e Zd ZU dZed ed< deeee e	f de
d fdd„Zdeeee e	f ded fdd	„Zd
S )ÚFindablezRfind() and find_all() methods that can be inherited to avoid
    repeating oneselfr   r1   r   r    c                 C   s   t | j|ƒS )z÷Iterate 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.
        )r5   r1   ©Úselfr   r%   r%   r&   Úfind_allK   s   	zFindable.find_allc                 C   s(   z	t t| j|ƒƒW S  ty   Y dS w )zìFind 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)Únextr5   r1   ÚStopIterationr7   r%   r%   r&   ÚfindV   s
   	ÿzFindable.findN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r,   r   Ú	MatchFuncr   r9   r   r<   r%   r%   r%   r&   r6   E   s   
 ÿ
þÿþr6   c                   @   sö   e Zd ZU eed< ee ed< ee ed< ee ed< ee ed< ee ed< ee ed< ee ed< eed	�Z	e
eef ed
< eed	�Zee ed< eed	�Zed  ed< ded  fdd„Zdeeee ef  fdd„Zde
eef fdd„ZdS )r   r"   ÚrevisionÚidÚlangÚalt_textÚactual_textÚtitleÚpage_number)Údefault_factoryÚ
attributesÚmcidsr1   r    c                 C   ó
   t | jƒS ©N©Úiterr1   ©r8   r%   r%   r&   Ú__iter__s   ó   
zPDFStructElement.__iter__c                 c   sd   � | j D ]}| j|fV  qt| jƒ}|r0| ¡ }|j D ]}|j|fV  q| t|jƒ¡ |sdS dS )z�Collect all MCIDs (with their page numbers, if there are
        multiple pages in the tree) inside a structure element.
        N)rL   rI   r   r1   r.   r/   r0   )r8   Úmcidr3   r4   r%   r%   r&   Ú	all_mcidsv   s   €


üzPDFStructElement.all_mcidsc                 C   sx   t | ƒ}t|gƒ}|r:| ¡ }t| ¡ ƒD ]}|| du s)|| g ks)|| i kr,||= qd|v r8| |d ¡ |s|S )z'Return a compacted dict representation.Nr1   )r   r   r.   ÚlistÚkeysÚextend)r8   Úrr3   r4   Úkr%   r%   r&   Úto_dict„   s   
$€úzPDFStructElement.to_dictN)r=   r>   r?   r,   rA   r   Úintr   ÚdictrK   r	   r   rV   rL   r   r1   r   rR   r   rU   r[   r%   r%   r%   r&   r   e   s   
 c                   @   s   e Zd ZdS )ÚStructTreeMissingN)r=   r>   r?   r%   r%   r%   r&   r^   “   s    r^   c                   @   sú   e Zd ZU dZed ed< dddded fdd„Zd	eee	f d
ee
 deee	f fdd„Zd	e	deee ee	 f fdd„Zdee	 ddfdd„Zd	eee	f defdd„Zd dd„Zdeee	f ddfdd„Zdee fdd„Zdedefdd„ZdS )!ÚPDFStructTreeaz  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`.

    r   ÚpageNÚdocr   c                    s(  |j | _ d| j jvrtdƒ‚t| j jd ƒ| _t| j di ¡ƒ| _t| j di ¡ƒ| _g | _|d urw|| _	|j
|i| _d | _| j d¡}|d u rN|  ¡  d S t|ƒ}d| j	jjvr[d S | j	jjd ‰ tt‡ fdd„|jD ƒƒƒ}|  |¡ d S d | _	d	d
„ |jD ƒ| _dd
„ | j ¡ D ƒ| _|  ¡  d S )NÚStructTreeRootzPDF has no structureÚRoleMapÚClassMapÚ
ParentTreeÚStructParentsc                 3   s    � | ]\}}|ˆ kr|V  qd S rN   r%   )Ú.0ÚnumÚarray©Ú	parent_idr%   r&   Ú	<genexpr>Í   s   € z)PDFStructTree.__init__.<locals>.<genexpr>c                 S   s   i | ]}|j |“qS r%   )rI   ©rg   r`   r%   r%   r&   Ú
<dictcomp>Ó   s    z*PDFStructTree.__init__.<locals>.<dictcomp>c                 S   s   i | ]}|j j|j“qS r%   )Úpage_objÚpageidrI   rm   r%   r%   r&   rn   Ô   s    ÿ)ra   Úcatalogr^   r   ÚrootÚgetÚrole_mapÚ	class_mapr1   r`   rI   ÚpagesÚ	page_dictÚ_parse_struct_treer   ro   Úattrsr:   ÚvaluesÚ_parse_parent_tree)r8   ra   r`   Úparent_tree_objÚparent_treeÚparent_arrayr%   rj   r&   Ú__init__«   s8   ÿÿzPDFStructTree.__init__ÚobjrC   r    c                 C   s,  g }dD ]}||vrqt || ƒ}t|tƒr| |¡ q| |¡ qg }d }|D ]$}t|tƒr?||kr<|d ur<| |¡ d }q(|d urH| |¡ t |ƒ}q(|d urV| |¡ i }	|D ]9}t|tƒrwt|jƒ}|| j	vrrt
 d|¡ qZ| j	| }| ¡ D ]\}
}t|tƒrŒt|jƒ|	|
< q{||
 |	|
< q{qZ|	S )N)ÚCÚAzUnknown attribute class %s)r   r+   rV   rX   Úappendr\   r   r   Únameru   ÚloggerÚwarningÚitems)r8   r€   rC   Úattr_obj_listÚkeyÚattr_objÚ	attr_objsÚprev_objÚarefÚattrrZ   Úvr%   r%   r&   Ú_make_attributesÙ   sD   










üzPDFStructTree._make_attributesc                 C   sž  d|vs
J d| ƒ‚d|vsJ d| ƒ‚d }| j d ur4d|v r4|d j}|| j v s/J d| ƒ‚| j | }d}d|v rNt|d jƒ}|| jv rNt| j| jƒ}d	|v rXt|d	 ƒng }t|tƒrc|g}n
t|tƒrm|d	 g}| 	d
¡}|  
||¡}d|v r„tt|d ƒƒnd }d|v r’tt|d ƒƒnd }	d|v r tt|d ƒƒnd }
d|v r®tt|d ƒƒnd }d|v r¼tt|d ƒƒnd }t|||||
|	|||d�	}||fS )NÚMCIDzUncaught MCR: %sÚObjzUncaught OBJR: %sÚPgzObject on unparsed page: %sÚ ÚSÚKÚRÚIDÚTÚLangÚAltÚ
ActualText)	r"   rD   rI   rC   rE   rH   rF   rG   rK   )rw   Úobjidr   r„   rt   r   r+   r\   r]   rs   r�   r   )r8   r€   rI   Ú
page_objidÚobj_tagr1   rC   rK   Ú
element_idrH   rE   rF   rG   Úelementr%   r%   r&   Ú_make_element
  sJ   






ÿ÷zPDFStructTree._make_elementr~   c           	      C   s°   t |ƒ}i }d}|rM| ¡ }|tjkrqt|ƒ|v rqt|ƒ}d|v r/t|d jƒdkr/d}n|  |¡\}}|dus<J ‚||f|t|ƒ< | 	|d ¡ |s
|sQJ ‚|  
|¡ dS )zYPopulate the structure tree using the leaves of the parent tree for
        a given page.FÚTyperb   TNÚP)r   r.   r   ÚKEYWORD_NULLÚreprr   r   r„   r¢   rƒ   Ú_resolve_children)	r8   r~   r3   ÚsÚ
found_rootÚrefr€   r¡   r1   r%   r%   r&   r{   4  s&   
íz PDFStructTree._parse_parent_treec                 C   sJ   d|vrdS |d j }| jd ur|| jv S | jd ur#|| jjjkr#dS dS )Nr“   TF)r�   rw   r`   ro   rp   )r8   r€   rž   r%   r%   r&   Úon_parsed_pageS  s   



zPDFStructTree.on_parsed_pagec                    s2  t ˆjd ƒ}t|tƒrˆjd g}t|ƒ}i ‰|r}| ¡ }t|ƒˆv r%qt |ƒ}t|tƒr@d|v r@ˆ |¡s8q|d }t |ƒ}ˆ |¡\}}||fˆt|ƒ< |D ])}t |ƒ}t|tƒrpˆ |¡sbqQd|v rk|d }nd|v rpqQt|t	ƒrz| 
|¡ qQ|sdtt dtt f‡ ‡‡fdd„‰ ˆ |ƒ ˆ ˆ¡ dS )	zgPopulate the structure tree starting from the root, skipping
        unparsed pages and empty elements.r–   r’   r‘   r   r    c                    s¸   g }| D ]U}t |ƒ}t|tƒr| |¡ qt|tƒr4ˆ |¡s qd|v r,| |d ¡ qd|v r4|d }ˆt|ƒ \}}ˆ |ƒ}|d u sF|sLˆt|ƒ= q||fˆt|ƒ< | |¡ q|S )Nr‘   r’   )r   r+   r\   rƒ   r]   r«   r¦   )r   Únext_elementsrª   r€   r¡   r1   ©Úpruner¨   r8   r%   r&   r®   ‡  s*   



z/PDFStructTree._parse_struct_tree.<locals>.pruneN)r   rr   r+   r]   r   r.   r¦   r«   r¢   r   rƒ   r   r   r§   )r8   rr   r3   rª   r€   r¡   r1   Úchildr%   r­   r&   rx   _  sB   






€æ"z PDFStructTree._parse_struct_treeÚseenc                    sr  t | jd ƒ}t|tƒr| jd g}g | _g }|D ]$}t |ƒ}t|tƒr2d|v r2|  |¡s.q|d }t|ƒˆ v r=| |¡ qt|ƒ}|r­| 	¡ }ˆ t|ƒ \}}|dusXJ dƒ‚|D ]P}	t |	ƒ}t|t
ƒrl|j |¡ n t|tƒrŒ|  |¡swqZd|v r„|j |d ¡ nd|v rŒ|d }	t|	tƒrªˆ  t|	ƒd¡\}
}|
durª|j |
¡ | |	¡ qZ|sD‡ fdd„|D ƒ| _dS )	z|Resolve children starting from the tree root based on references we
        saw when traversing the structure tree.
        r–   r’   NzUnparsed elementr‘   )NNc                    s   g | ]
}ˆ t |ƒ d  ‘qS )r   )r¦   )rg   rª   ©r°   r%   r&   Ú
<listcomp>Í  s    z3PDFStructTree._resolve_children.<locals>.<listcomp>)r   rr   r+   r]   r1   r«   r¦   rƒ   r   r.   r\   rL   r   rs   )r8   r°   rr   Úparsed_rootrª   r€   r3   r¡   r1   r¯   Úchild_elementÚ_r%   r±   r&   r§   £  sL   


€




€ëzPDFStructTree._resolve_childrenc                 C   rM   rN   rO   rQ   r%   r%   r&   rR   Ï  rS   zPDFStructTree.__iter__r4   c                 C   sH  d}| j dur| j }n|jdur| j|j }|j dd¡}|dur]|dur]ddl m}m}m} |||ƒ|jd |jd  ƒ}t	||ƒr[t
 |¡}| |g¡}|sTtdƒ‚t
 |d ¡S |S g }	| ¡ D ]5\}
}|
du r||durytj |j ¡ ¡}ng }ntj | j|
 j ¡ ¡}|D ]}|d |kr—|	 |¡ qŠqc|	sŸtd	ƒ‚t
 |	¡S )
z9Get the bounding box for an element for visual debugging.NÚBBoxr   )ÚCroppedPageÚ_invert_boxÚ_normalize_boxé   zElement no longer on pager   rT   zNo objects found)r`   rI   rv   rK   rs   r·   r¸   r¹   Úmediaboxr+   r   Úbbox_to_rectÚ_crop_fnÚ
IndexErrorÚobj_to_bboxrU   Ú	itertoolsÚchainÚfrom_iterableÚobjectsrz   rƒ   Úobjects_to_bbox)r8   r4   r`   Úbboxr·   r¸   r¹   ÚrectÚrectsÚ	mcid_objsrI   rT   rÃ   Úcr%   r%   r&   Úelement_bboxÒ  sF   

ÿ

ÿ
€þ
zPDFStructTree.element_bboxrN   )r    N)r=   r>   r?   r@   r   rA   r   r	   r,   r   r\   r�   r   r   r   r¢   r{   r*   r«   rx   r§   r   rR   r   rÊ   r%   r%   r%   r&   r_   —   s$   
 .
ÿÿ

þ"1*
D,r_   )4rÀ   Ú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   Ú	getLoggerr=   r…   r`   r   Úpdfr   r*   rB   r,   r5   r6   r   Ú
ValueErrorr^   r_   r%   r%   r%   r&   Ú<module>   s:    4
ÿþ
ý -