a
    ˜ëj¼  ã                   @   sâ   d dl mZmZmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ erlddlmZ ddlmZ ddlmZ G d	d
„ d
ƒZG dd„ dƒZeeef Zddeeeef  dœdd„Ze
dee dœdd„Zedee dœdd„ZdS )é    )ÚTYPE_CHECKINGÚOptionalÚUnionÚcasté   )Úlogger_warningé   )ÚArrayObjectÚDictionaryObjectÚIndirectObjectÚ	PdfObjectÚTextStringObjectÚis_null_or_none)Ú
PageObject)Ú	PdfReader)Ú	PdfWriterc                   @   sZ   e Zd ZdZedddœdd„Zeedf dœdd	„Zdd
œdee	e
e  ddœdd„ZdS )ÚNamedReferenceLinkzGNamed reference link being preserved until we can resolve it correctly.r   N©Ú	referenceÚ
source_pdfÚreturnc                 C   s   || _ || _dS )z0reference: TextStringObject with named referenceN)Ú
_referenceÚ_source_pdf)Úselfr   r   © r   úR/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/generic/_link.pyÚ__init__-   s    zNamedReferenceLink.__init__©r   c                 C   s"   | j j t| jƒ¡}|r|jS d S ©N)r   Únamed_destinationsÚgetÚstrr   Úpage)r   Zdestinationr   r   r   Úfind_referenced_page2   s    z'NamedReferenceLink.find_referenced_page)Úknown_namesr   )Ú
target_pdfÚnew_pager$   r   c                C   s@   t | jƒ}|du rt|jƒ}||vr<| ||j¡ | |¡ dS )aä  
        Add the named destination of the link to the new page, unless it exists.

        Args:
            target_pdf: PdfWriter which the new link went into.
            new_page: IndirectObject representing the new page in the target PDF.
            known_names: Names of the named destinations of target_pdf, kept up to
                date by this method. Collecting them from target_pdf is expensive,
                thus callers patching many links should pass them.
        N)r!   r   Úsetr   Zadd_named_destinationZpage_numberÚadd)r   r%   r&   r$   Únamer   r   r   Úpatch_reference6   s    

z"NamedReferenceLink.patch_reference)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r#   r   r'   r!   r*   r   r   r   r   r   *   s   	û
úr   c                   @   sH   e Zd ZdZeddœdd„Zeedf dœdd„Zd	edd
œdd„Z	dS )ÚDirectReferenceLinkzHDirect reference link being preserved until we can resolve it correctly.N)r   r   c                 C   s
   || _ dS )zIreference: an ArrayObject whose first element is the Page indirect objectN©r   )r   r   r   r   r   r   S   s    zDirectReferenceLink.__init__r   c                 C   s   | j s
d S tt| j d ƒS )Nr   )r   r   r   )r   r   r   r   r#   W   s    z(DirectReferenceLink.find_referenced_pager   )r%   r&   r   c                 C   s   || j d< dS )z2target_pdf: PdfWriter which the new link went intor   Nr0   )r   r%   r&   r   r   r   r*   \   s    z#DirectReferenceLink.patch_reference)
r+   r,   r-   r.   r	   r   r   r   r#   r*   r   r   r   r   r/   P   s   r/   r   )r&   Úold_pager   c                    sÆ   ˆ  dtƒ ¡ ¡ }ˆ  dtƒ ¡ ¡ }t|ƒr2tƒ }t|ƒr@tƒ }t|tƒrTt|tƒshtdt||d� g S ‡ ‡fdd„|D ƒ}‡ ‡fdd„|D ƒ}t|ƒt|ƒkr¸tdtt|ƒt|ƒd� tt	||ƒƒS )	a!  Extracts links from two pages on the assumption that the two pages are
    the same. Produces one list of (new link, old link) tuples.

    Non-link annotations are ignored before pairing to avoid dropping valid
    links when one page includes additional non-link annotation entries.
    z/AnnotszZExpected annotation arrays: %(old_annotations)s %(new_annotations)s. Ignoring annotations.)ÚsourceÚold_annotationsÚnew_annotationsc                    s"   g | ]}t |ˆƒ ‰ d urˆ ‘qS r   ©Ú_build_link©Ú.0Ú
annotation)Úlinkr&   r   r   Ú
<listcomp>y   s   þz!extract_links.<locals>.<listcomp>c                    s"   g | ]}t |ˆƒ ‰ d urˆ ‘qS r   r5   r7   )r:   r1   r   r   r;   ~   s   þz8Annotation sizes differ: %(old_count)d vs. %(new_count)d)r2   Z	old_countZ	new_count)
r    r	   Ú
get_objectr   Ú
isinstancer   r+   ÚlenÚlistÚzip)r&   r1   r4   r3   Z	new_linksZ	old_linksr   )r:   r&   r1   r   Úextract_linksd   s8    üþþürA   )Úindirect_objectr"   r   c                 C   s’   t d|jƒ}t t|  ¡ ƒ}t|tƒr2| d¡dkr6d S d|v rxt t|d ƒ}| d¡dkr^d S d|vrjd S t|d |ƒS d|v rŽt|d |ƒS d S )	Nr   z/Subtypez/Linkz/Az/Sz/GoToz/Dz/Dest)r   Zpdfr
   r<   r=   r    Ú_create_link)rB   r"   Úsrcr:   Úactionr   r   r   r6   �   s    r6   r   r   c                 C   s*   t | tƒrt| |ƒS t | tƒr&t| ƒS d S r   )r=   r   r   r	   r/   )r   r   r   r   r   rC   ¤   s
    


rC   N)Útypingr   r   r   r   Ú_utilsr   Ú r	   r
   r   r   r   r   Z_pager   Z_readerr   Z_writerr   r   r/   ZReferenceLinkr?   ÚtuplerA   r6   rC   r   r   r   r   Ú<module>   s    &+