o
    uÃhW  ã                	   @   sÞ   d dl mZmZmZmZ ddlmZmZmZm	Z	m
Z
 er.d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dddeeeef  fdd„Zdedddee fdd„Zde	dddee fdd„ZdS )é    )ÚTYPE_CHECKINGÚOptionalÚUnionÚcasté   )ÚArrayObjectÚDictionaryObjectÚIndirectObjectÚ	PdfObjectÚTextStringObjecté   )Ú
PageObject)Ú	PdfReader)Ú	PdfWriterc                   @   sR   e Zd ZdZdeddddfdd„Zdeedf fd	d
„Zdddeddfdd„Z	dS )ÚNamedReferenceLinkzGNamed reference link being preserved until we can resolve it correctly.Ú	referenceÚ
source_pdfr   ÚreturnNc                 C   s   || _ || _dS )z0reference: TextStringObject with named referenceN)Ú
_referenceÚ_source_pdf)Úselfr   r   © r   úI/var/www/html/bot/env/lib/python3.10/site-packages/pypdf/generic/_link.pyÚ__init__,   s   
zNamedReferenceLink.__init__c                 C   s"   | j j t| jƒ¡}|r|jS d S ©N)r   Únamed_destinationsÚgetÚstrr   Úpage)r   Údestinationr   r   r   Úfind_referenced_page1   s   z'NamedReferenceLink.find_referenced_pageÚ
target_pdfr   Únew_pagec                 C   s,   t | jƒ|jvr| t | jƒ|j¡ dS dS )ú2target_pdf: PdfWriter which the new link went intoN)r   r   r   Úadd_named_destinationÚpage_number©r   r!   r"   r   r   r   Úpatch_reference5   s   ÿz"NamedReferenceLink.patch_reference)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r    r'   r   r   r   r   r   )   s
    r   c                   @   sF   e Zd ZdZdeddfdd„Zdefdd„Zd	d
deddfdd„ZdS )ÚDirectReferenceLinkzHDirect reference link being preserved until we can resolve it correctly.r   r   Nc                 C   s
   || _ dS )zIreference: an ArrayObject whose first element is the Page indirect objectN©r   )r   r   r   r   r   r   ?   s   
zDirectReferenceLink.__init__c                 C   s
   | j d S )Nr   r-   )r   r   r   r   r    C   s   
z(DirectReferenceLink.find_referenced_pager!   r   r"   c                 C   s   || j d< dS )r#   r   Nr-   r&   r   r   r   r'   F   s   z#DirectReferenceLink.patch_reference)	r(   r)   r*   r+   r   r   r	   r    r'   r   r   r   r   r,   <   s
    r,   r"   r   Úold_pager   c                    sH   ‡ fdd„ˆ   dg ¡D ƒ}‡fdd„ˆ  dg ¡D ƒ}dd„ t||ƒD ƒS )z‹Extracts links from two pages on the assumption that the two pages are
    the same. Produces one list of (new link, old link) tuples.
    c                    ó   g | ]}t |ˆ ƒ‘qS r   ©Ú_build_link©Ú.0Úlink)r"   r   r   Ú
<listcomp>R   ó    z!extract_links.<locals>.<listcomp>z/Annotsc                    r/   r   r0   r2   )r.   r   r   r5   S   r6   c                 S   s    g | ]\}}|r|r||f‘qS r   r   )r3   Únew_linkÚold_linkr   r   r   r5   U   s    ýýÿ)r   Úzip)r"   r.   Ú	new_linksÚ	old_linksr   )r"   r.   r   Úextract_linksN   s
   þr<   Úindirect_objectr   c                 C   s’   t d|jƒ}t t|  ¡ ƒ}t|tƒr| d¡dkrd S d|v r<t t|d ƒ}| d¡dkr/d S d|vr5d S t|d |ƒS d|v rGt|d |ƒS d S )	Nr   z/Subtypez/Linkz/Az/Sz/GoToz/Dz/Dest)r   Úpdfr   Ú
get_objectÚ
isinstancer   Ú_create_link)r=   r   Úsrcr4   Úactionr   r   r   r1   \   s   r1   r   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   rA   q   s
   


rA   N)Útypingr   r   r   r   Ú r   r   r	   r
   r   Ú_pager   Ú_readerr   Ú_writerr   r   r,   ÚReferenceLinkÚlistÚtupler<   r1   rA   r   r   r   r   Ú<module>   s   "