o
    uh(                     @   s  d Z dZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZ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 ddlm Z m!Z!m"Z" ddlm#Z$ ddlm%Z& ddlm'Z( ddlm)Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZA ddlmBZBmCZC ejDdkrddlmEZE nddlFmEZE eGeHZIeJdZKG dd deLe e7ZMG dd deNeef e7ZOG dd deOZPded dfd!d"ZQG d#d$ d$eOZRG d%d& d&eRZSG d'd( d(eRZTG d)d* d*eSZU	d4d+ed,ee d-edeVeLeV eNeWeVf f d ee7eWeVeUf fd.d/ZXG d0d1 d1ePZYG d2d3 d3ePZZdS )5zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)IterableSequence)BytesIO)ceil)AnyCallableOptionalUnioncast   )PdfReaderProtocolPdfWriterProtocolXmpInformationProtocol)WHITESPACES
StreamTypedeprecation_no_replacementlogger_warningread_non_whitespaceread_until_regexread_until_whitespaceskip_over_comment)CheckboxRadioButtonAttributesFieldDictionaryAttributesOutlineFontFlag)FilterTypes)StreamAttributes)TypArguments)TypFitArguments)STREAM_TRUNCATED_PREMATURELYPdfReadErrorPdfStreamError   )
BooleanObjectByteStringObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectTextStringObjectis_null_or_none)Fit)extract_inline_A85extract_inline_AHxextract_inline_DCTextract_inline_defaultextract_inline_RL)read_hex_string_from_streamread_string_from_stream)      )Selfs   [+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]c                   @   s  e Zd Zdedd fddZ		d!dededeeee	e
f   dd fd	d
Zde
fddZdee fddZdedee fddZdedd fddZdedefddZdedefddZ	d"dedede	ef ddfddZe	d"dedee dede	ee	 ee
e	f f dd fdd ZdS )#ArrayObjectpdf_destreturnc                 C   sJ   t d| t |d}| D ]}t|dr||| q|| q|S )Nr7   F	replicate)r
   _reference_cloner7   hasattrappendr:   )selfr8   arrdata rA   T/var/www/html/bot/env/lib/python3.10/site-packages/pypdf/generic/_data_structures.pyr:   c   s   
zArrayObject.replicateFrA   force_duplicateignore_fieldsc                 C   s   z| j j|kr|s| W S W n	 ty   Y nw td| t ||}| D ]/}t|tr>|||||||}|	|j  q$t
|drN|	|||| q$|	| q$|S )Clone object into pdf_dest.r7   clone)indirect_referencepdf	Exceptionr
   r;   r7   
isinstanceStreamObjectrF   r=   r<   )r>   r8   rC   rD   r?   r@   duprA   rA   rB   rF   r   s.   

zArrayObject.clonec                 C   s   t | jtdd | D fS )q
        Used to detect modified object.

        Returns:
            Hash considering type and value.

        c                 s   s    | ]}|  V  qd S Nhash_bin.0xrA   rA   rB   	<genexpr>   s    z'ArrayObject.hash_bin.<locals>.<genexpr>)hash	__class__tupler>   rA   rA   rB   rP      s   zArrayObject.hash_binc                 C   s   t | S )z:Emulate DictionaryObject.items for a list (index, object).)	enumeraterX   rA   rA   rB   items   s   zArrayObject.itemslstc                 C   s~   t |tttfr	 |S t |tr|g}|S t |tr.|d dkr't|g}|S t|g}|S t |tr:t	|g}|S |g}|S )Nr   /)
rJ   listrW   setr)   strr&   r*   bytesr#   r>   r[   rA   rA   rB   _to_lst   s"   






zArrayObject._to_lstc                 C   s   t | }|| | |S )a  
        Allow extension by adding list or add one element only

        Args:
            lst: any list, tuples are extended the list.
            other types(numbers,...) will be appended.
            if str is passed it will be converted into TextStringObject
            or NameObject (if starting with "/")
            if bytes is passed it will be converted into ByteStringObject

        Returns:
            ArrayObject with all elements

        )r7   extendrb   )r>   r[   temprA   rA   rB   __add__   s   zArrayObject.__add__c                 C   s   |  | | | S )a  
         Allow extension by adding list or add one element only

        Args:
            lst: any list, tuples are extended the list.
            other types(numbers,...) will be appended.
            if str is passed it will be converted into TextStringObject
            or NameObject (if starting with "/")
            if bytes is passed it will be converted into ByteStringObject

        )rc   rb   ra   rA   rA   rB   __iadd__   s   zArrayObject.__iadd__c              	   C   s:   |  |D ]}z
| |}| |= W q ty   Y qw | S )zAllow to remove items)rb   index
ValueError)r>   r[   rS   rA   rA   rB   __isub__   s   

zArrayObject.__isub__Nstreamencryption_keyc                 C   sH   |d ur	t dd |d | D ]}|d || q|d d S )N/the encryption_key parameter of write_to_stream5.0.0   [    s    ])r   writewrite_to_stream)r>   rj   rk   r@   rA   rA   rB   rq      s   

zArrayObject.write_to_streamrH   forced_encodingc                 C   s   t  }| d}|dkrtd	 | d}| r#| d}| s|dkr*	 |S |dkr9| dd t|  q| dd | d}|dkrK	 |S | dd |t| || q)	Nr!   rn   zCould not read arrayT       %   ])r7   readr   isspaceseekr   r=   read_object)rj   rH   rr   r?   tmptok
peek_aheadrA   rA   rB   read_from_stream   s0   



zArrayObject.read_from_streamFrA   rN   )__name__
__module____qualname__r   r:   boolr   r   r	   r_   intrF   rP   r   r   rZ   r]   rb   re   r6   rf   ri   r   r`   rq   staticmethodr   dictr~   rA   rA   rA   rB   r7   b   sT    



r7   c                   @   s  e Zd Zdedd fddZ		d)dededeeee	e
f   dd fd	d
Zdd dededeeee	e
f   deee
e
f  ddfddZde
fddZdedefddZd*de	dedefddZdededefddZd*dedee defddZdedefddZedee fdd Z	d*d!ed"ede	ef ddfd#d$Ze	d*d!ed%ee d&ede	ee	 e e
e	f f dd fd'd(Z!dS )+DictionaryObjectr8   r9   c                 C   sP   t d| |  |d}|  D ]\}}t|dr||n||||< q|S )Nr   Fr:   )r
   r;   rV   rZ   r<   r:   )r>   r8   d__kvrA   rA   rB   r:   	  s   zDictionaryObject.replicateFrA   rC   rD   c                 C   s~   z| j j|kr|s| W S W n	 ty   Y nw t }td| |  ||}|du r,g }t| dkr=|	| |||| |S )rE   r   Nr   )
rG   rH   rI   r^   r
   r;   rV   lenkeys_cloner>   r8   rC   rD   visitedr   rA   rA   rB   rF     s"   zDictionaryObject.clonesrcr   Nc              
      s  d} dusJ t   |t k r;t | tr1tt | dkr) |=  |= q |  d8  < |d7 }|t k st fdddD rt   dD ]}|D ]}g }	|v r|| vrt|trt| tr	dddu stt| 	dddu s	ddtt| 	ddkrtd	| }
| }|
durtd	|

|
 ||}|jdur|jj}|jj}||f|v rd}
n>|||f |	|
|f |dusJ |j|t|< |}z|
krd}
ntd	|
| }
W n ty   d}
Y nw |
dus|	D ]\}}|||| | qqRqN D ]J\}}| vrft|trNt|d
s5d|_||| }|jdusDJ |j| ||< q|| vrft|dr`||| n|| t|< qdS )z
        Update the object from src.

        Args:
            src: "DictionaryObject":
            pdf_dest:
            force_duplicate:
            ignore_fields:

        r   Nr!   c                 3   s    | ]@}| vo@|v o@t |to@t | to@d ddu p@tt| d ddu p@d dtt| d dkV  qdS )/TypeN)rJ   raw_getr%   r   getr
   )rR   fieldrD   r   rA   rB   rT   O  s"    

z*DictionaryObject._clone.<locals>.<genexpr>)/Next/Prev/N/V))r   r   )r   r   r   r   rG   rF   )r]   r   rJ   r   r
   anyr   r%   r   r   r;   rV   rG   idnum
generationaddr=   r&   rI   r   rZ   rK   r<   rF   )r>   r   r8   rC   rD   r   rS   r[   r   objscur_objprev_objclonr   r   scr   vvrA   r   rB   r   /  s   	







3


zDictionaryObject._clonec                 C   s    t | jtdd |  D fS )rM   c                 s   s     | ]\}}||  fV  qd S rN   rO   )rR   r   r   rA   rA   rB   rT     s    z,DictionaryObject.hash_bin.<locals>.<genexpr>)rU   rV   rW   rZ   rX   rA   rA   rB   rP     s   zDictionaryObject.hash_binkeyc                 C   s   t | |S rN   )r   __getitem__r>   r   rA   rA   rB   r     s   zDictionaryObject.raw_getdefaultc                 C   sT   || v r| | S zd| vr|W S t d t y)   td| d  || Y S w )a>  
        Returns the value of a key or from the parent if not found.
        If not found returns default.

        Args:
            key: string identifying the field to return

            default: default value to return

        Returns:
            Current key or inherited one, otherwise default value.

        /ParentzNot presentr   )KeyErrorr
   
get_objectget_inherited)r>   r   r   rA   rA   rB   r     s   zDictionaryObject.get_inheritedvaluec                 C   2   t |ts	tdt |tstdt| ||S NzKey must be a PdfObjectzValue must be a PdfObject)rJ   r)   rh   r   __setitem__r>   r   r   rA   rA   rB   r     
   

zDictionaryObject.__setitem__c                 C   r   r   )rJ   r)   rh   r   
setdefaultr   rA   rA   rB   r     r   zDictionaryObject.setdefaultc                 C   s   t | | S rN   )r   r   r   r   rA   rA   rB   r     s   zDictionaryObject.__getitem__c                 C   sD   ddl m} | dd}t|rdS |dusJ d| }||S )u  
        Retrieve XMP (Extensible Metadata Platform) data relevant to this
        object, if available.

        See Table 347 — Additional entries in a metadata stream dictionary.

        Returns:
          Returns a :class:`~pypdf.xmp.XmpInformation` instance
          that can be used to access XMP metadata from the document. Can also
          return None if no metadata was found on the document root.

        r   )XmpInformationz	/MetadataNmypy)xmpr   r   r+   r   )r>   r   metadatarA   rA   rB   xmp_metadata  s   zDictionaryObject.xmp_metadatarj   rk   c                 C   s   |d ur	t dd |d |  D ],\}}t|dkr)|d dkr)|d dkr)q||| |d || |d	 q|d
 d S )Nrl   rm      <<
r   r!   %ru   ro      
   >>)r   rp   rZ   r   rq   )r>   rj   rk   r   r   rA   rA   rB   rq     s   
$

z DictionaryObject.write_to_streamrH   rr   c              
      sv  dt dt dtt  dtdt f
dd dtdtdtf fd	d
}| d}|dkr3tdt|   di }	 t	| }|dkr?q5|dkrN| 
dd t|  q5|sTtt|dkr^| d n| 
dd zPzt| |}t|trsW W nt|tstd|W n" ty } z|d ur|jr t| t W Y d }~W q5d }~ww t	| }| 
dd t| ||}	W n0 ty } z$|d ur|jrt| t| t t }
|
| |
W  Y d }~S d }~ww ||s|	||< ndt|   d| }|d ur|jrt|t|t q6|  }t	| }|dkr!| ddkr!| d}|dkr7| d}|dks-|dvr@td|dkrS| ddkrS| 
dd tj|vry|d urf|jrftd td!|   t td|ttj< |tj }t|tr|  }|d usJ d"||}| 
|d# |d u rd}|  }|d#kr| ||d$< n
t| t !d%|d$< t	| }| d&}|| d%kr |  }| 
d'd | d(}|d%kr|d$ d d |d$< n:|d ur|js| 
|d# || ||d$< |  }n| 
|d# td)t|   d*|d+|d,n| 
|d# d$|v r1t"#|S t }
|
| |
S )-Npp1rem_gensrH   r9   c              	      s^   }|D ](}|j | }z fdd| D }|r"t|g|R  }W q ty,   Y qw |S )Nc                    s(   g | ]} |  k rkrn n|qS rA   rA   rQ   r   r   rA   rB   
<listcomp>  s   ( zODictionaryObject.read_from_stream.<locals>.get_next_obj_pos.<locals>.<listcomp>)xrefvaluesminrh   )r   r   r   rH   outgenlocr   rA   r   rB   get_next_obj_pos  s   
z;DictionaryObject.read_from_stream.<locals>.get_next_obj_posrj   c                    sz    |   dt|j|d }|   }| ||    }|d}|dk r,td| d| || d  |d |d  S )Nl        r!   	   endstreamr   z6Unable to find 'endstream' marker for obj starting at .	   )tellr]   r   rw   findr   ry   )rj   rH   eoncurrrwr   r   rA   rB   read_unsized_from_stream  s   

zCDictionaryObject.read_from_stream.<locals>.read_unsized_from_streamr      <<zDictionary read error at byte z: stream must begin with '<<'T    rt   ru   r!      >z)Expecting a NameObject for key but found z+Multiple definitions in dictionary at byte z	 for key    s   s   treamro   )r      z)Stream data must be followed by a newliner   r   zStream length not definedzStream length not defined @pos=r   r   __streamdata__r      ir   z7Unable to find 'endstream' marker after stream at byte z (nd='z', end='z').)$r   r]   r   r   r`   rw   r   hexr   r   ry   r   r    r   rz   rJ   r'   r&   strictr   __repr__r   rI   r   updater   SALENGTHr(   r%   r   r   recompilerK   initialize_from_dictionary)rj   rH   rr   r   r{   r@   r|   r   excr   retvalmsgposr   eollengthtpstartendstreamendrA   r   rB   r~     s  










4

















z!DictionaryObject.read_from_streamr   rN   )"r   r   r   r   r:   r   r   r   r	   r_   r   rF   r^   rW   r   rP   r   r   r   r   r   r)   r   propertyr   r   r   r`   rq   r   r   r]   r   r~   rA   rA   rA   rB   r     sr    


q
r   c                   @   s  e Zd Zd'dee ddfddZdefddZdefdd	Z	de
e fd
dZdededdfddZdeded f deddfddZdeded f deddfddZ	d'dedededeedef  def
ddZdededededdf
dd Zdeddfd!d"Zd(d#d$Zd(d%d&ZdS ))
TreeObjectNdctr9   c                 C   s    t |  |r| | d S d S rN   )r   __init__r   )r>   r   rA   rA   rB   r     s   
zTreeObject.__init__c                 C   s   d| v S )N/FirstrA   rX   rA   rA   rB   has_children     zTreeObject.has_childrenc                 C   s   |   S rN   )childrenrX   rA   rA   rB   __iter__  r   zTreeObject.__iter__c                 c   sb    |   sd S | td }| }	 |V  || td krd S |td}t|r,d S | }q)Nr   T/Lastr   )r   r&   r   r   r+   )r>   	child_refchildrA   rA   rB   r     s   zTreeObject.childrenr   rH   c                 C   s   |  |d | d S rN   )insert_child)r>   r   rH   rA   rA   rB   	add_child  s   zTreeObject.add_childparentnc                 C   sv   t |rd S |d usJ dtd| }d|v r9ttdtt|td | |td< | |dd | d S d S )Nr   r   /Countr   r   )	r+   r
   r   r(   maxr   r&   inc_parent_counter_defaultr   )r>   r   r   rA   rA   rB   r    s   z%TreeObject.inc_parent_counter_defaultc                 C   s   t |rd S |d usJ dtd| }|dddk}tt|dd}|dk r.t|}t|| |r6dnd |td< |sBd S | |d	d | d S )
Nr   r   z
/%is_open%Tr   r   r!   ru   r   )	r+   r
   r   r   r   absr(   r&   inc_parent_counter_outline)r>   r   r   opnr   rA   rA   rB   r    s    z%TreeObject.inc_parent_counter_outlinebeforeinc_parent_counter.c                 C   s  |d u r| j }| }|j}d| vrF|| td< td| td< || td< | j|td< || |dd d|v r=|d= d|v rD|d= |S td	| d }|j|krd|v r^td
|d }n/td
||td< |j|td< | j|td< d|v r||d= || td< || |dd |S |j|ksRzt|d tsJ ||d td< |d |td< W n t	y   |d= Y nw ||td< ||td< | j|td< || |dd |S )Nr   r   r   r   r   r!   r   r   r   r   )
r  r   rG   r&   r(   r   r
   rJ   r   rI   )r>   r   r  rH   r  	child_objprevrA   rA   rB   r     sP   


zTreeObject.insert_childr  prev_refcurlastc                 C   s   | tdd}|du rI|r-| }|td= || td< t| td d | td< dS td| td< | td= td| v rG| td= dS dS |r\| }||td< ||td< n||ksbJ |td= || td< t| td d | td< dS )	z
        Adjust the pointers of the linked list and tree node count.

        Args:
            prev:
            prev_ref:
            cur:
            last:

        r   Nr   r   r   r!   r   r   )r   r&   r   r(   )r>   r  r	  r
  r  next_refnext_objrA   rA   rB   _remove_node_from_tree  s,   


 z!TreeObject._remove_node_from_treec           
      C   s   |  }|j}td|vrtd|td | krtdd}d }d }| td }|  }| td }|  }	|d urg||krJ| ||||	 d}n|}|}td|v r_|td }|  }nd }d }|d us;|smtd	t| d S )
Nr   /Removed child does not appear to be a tree itemz*Removed child is not a member of this treeFr   r   Tr   z"Removal couldn't find item in tree)r   rG   r&   rh   r  _reset_node_tree_relationship)
r>   r   r  foundr	  r  cur_refr
  last_refr  rA   rA   rB   remove_child=  s:   
zTreeObject.remove_childc                 C   s,   t d| vr
tdtd| d |  dS )z)Remove the object from the tree it is in.r   r  r   N)r&   rh   r
   r  rX   rA   rA   rB   remove_from_treeb  s   zTreeObject.remove_from_treec                 C   sd   | D ]
}|  }t| qtd| v r| td= td| v r#| td= td| v r0| td= d S d S )Nr   r   r   )r   r  r&   )r>   r   r  rA   rA   rB   
empty_treeh  s   


zTreeObject.empty_treerN   r9   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r%   r   r  r  r   r   r  r  r  r  rA   rA   rA   rB   r     s\    


2
+
%r   r  r9   c                 C   s>   | t d= t d| v r| t d= t d| v r| t d= dS dS )z
    Call this after a node has been removed from a tree.

    This resets the nodes attributes in respect to that tree.

    Args:
        child_obj:

    r   r   r   N)r&   )r  rA   rA   rB   r  u  s   


r  c                       s  e Zd Zd%ddZdedd fddZdeded	ed
ee	e
eef   deeeef  ddf fddZdef fddZdefddZdeddfddZdef fddZ	d&dede
deef ddfddZedeeef de
d fddZd'dedd fd!d"Zdefd#d$Z  ZS )(rK   r9   Nc                 C   s   d| _ d | _d S Nrs   )_datadecoded_selfrX   rA   rA   rB   r     s   
zStreamObject.__init__r8   c                 C   s   t d| |  |d}| j|_z| j}|d u rd | _n	t d||| _W n	 ty/   Y nw |  D ]\}}t|drB||n||||< q4|S )NrK   FDecodedStreamObjectr:   )	r
   r;   rV   r  r  r:   rI   rZ   r<   r>   r8   r   r  r   r   rA   rA   rB   r:     s(   
zStreamObject.replicater   rC   rD   r   c                    sp   t d|j| _zt d|j}|du rd| _nt d||||| _W n	 ty+   Y nw t ||||| dS )
        Update the object from src.

        Args:
            src:
            pdf_dest:
            force_duplicate:
            ignore_fields:

        rK   Nr  )r
   r  r  rF   rI   superr   )r>   r   r8   rC   rD   r   r  rV   rA   rB   r     s   zStreamObject._clonec                    s   t t  | jfS )rM   )rU   r  rP   r  rX   r  rA   rB   rP     s   	zStreamObject.hash_binc                 C   s   | j S rN   r  rX   rA   rA   rB   get_data  s   zStreamObject.get_datar@   c                 C   s
   || _ d S rN   r   r>   r@   rA   rA   rB   set_data     
zStreamObject.set_datac                    s   t   }||  7 }|S rN   )r  hash_value_datar!  r"  r  rA   rB   r%    s   
zStreamObject.hash_value_datarj   rk   c                 C   sb   |d ur	t dd tt| j| ttj< t| | | tj= |	d |	| j |	d d S )Nrl   rm   s   
stream
s
   
endstream)
r   r(   r   r  r&   r   r   r   rq   rp   r>   rj   rk   rA   rA   rB   rq     s   
zStreamObject.write_to_stream)EncodedStreamObjectr  c                 C   sH   t j| v r	t }nt }| d |_| d= t j| v r| t j= ||  |S )Nr   )r   FILTERr'  r  r  r   r   )r@   r   rA   rA   rB   r     s   



z'StreamObject.initialize_from_dictionaryru   levelr'  c              	   C   s  ddl m} tj| v r]| tj }t|trFtttjg|}ztt	 g| 
tjt }W n4 tyE   tt	 | 
tjt g}Y nw tttj|g}tt	 | 
tjt	 g}nttj}d }t }||  ||ttj< |d ur~||ttj< || j||_|S )Nr   FlateDecode)filtersr+  r   r(  rJ   r7   r&   FTFLATE_DECODEr'   r   DECODE_PARMS	TypeErrorr'  r   encoder  )r>   r)  r+  fparamsr   rA   rA   rB   flate_encode  s6   




zStreamObject.flate_encodec                 C   sv   ddl m} | dddkr,z| j d}W n ty&   |   d}Y nw t|t || \}}}|du r9dS |S )	a~  
        Try to decode the stream object as an image

        Returns:
            a PIL image if proper decoding has been found
        Raises:
            Exception: (any)during decoding to to invalid object or
                errors during decoding will be reported
                It is recommended to catch exceptions to prevent
                stops in your program.

        r   )_xobj_to_imagez/Subtype z/Imagez does not seem to be an Imagez$ object does not seem to be an ImageN)r,  r5  r   rG   AttributeErrorr   r   r   )r>   r5  r   	extensionbyte_streamimgrA   rA   rB   decode_as_image  s   
zStreamObject.decode_as_imager  rN   )ru   )r   r   r   r   r   r:   r   r   r   r   r	   r_   r   r^   rW   r   rP   r`   r!  r#  r%  r   rq   r   r   r   r   r4  r;  __classcell__rA   rA   r  rB   rK     sP    

 

 rK   c                   @   s   e Zd ZdS )r  N)r   r   r   rA   rA   rA   rB   r  3  s    r  c                       s>   e Zd Zd
ddZdefddZdeddf fdd	Z  ZS )r'  r9   Nc                 C   s
   d | _ d S rN   )r  rX   rA   rA   rB   r   8  r$  zEncodedStreamObject.__init__c                 C   sp   ddl m} | jd ur| j S t }|||  |  D ]\}}|tjtj	tj
fvr0|||< q|| _| S )Nr   )decode_stream_data)r,  r=  r  r!  r  r#  rZ   r   r   r(  r/  )r>   r=  decodedr   r   rA   rA   rB   r!  <  s   

zEncodedStreamObject.get_datar@   c                    s   ddl m} | tjdtjtjgfv r@t|tst	d| j
d u r&|   | j
d us/J d| j
| t || d S td)Nr   r*  r6  zData must be bytesr   zJStreams encoded with a filter different from FlateDecode are not supported)r,  r+  r   r   r(  r-  r.  rJ   r`   r0  r  r!  r#  r  r1  r   )r>   r@   r+  r  rA   rB   r#  M  s   

zEncodedStreamObject.set_datar  )r   r   r   r   r`   r!  r#  r<  rA   rA   r  rB   r'  7  s    
r'  c                       s  e Zd ZdZ	d(dedededeee ee	ef f ddf fddZ
d	edd fd
dZ		d)d	ededeeeee	f   dd fddZded	ededeeeee	f   deee	e	f  ddf fddZdeddfddZdedeeef fddZdefddZdeddf fddZedeeeef  fdd Zejd!eeeef  ddfd"d Zd*d#d$Z	d(ded%edeef ddf fd&d'Z  Z S )+ContentStreama  
    In order to be fast, this data structure can contain either:

    * raw data in ._data
    * parsed stream operations in ._operations.

    At any time, ContentStream object can either have both of those fields defined,
    or one field defined and the other set to None.

    These fields are "rebuilt" lazily, when accessed:

    * when .get_data() is called, if ._data is None, it is rebuilt from ._operations.
    * when .operations is called, if ._operations is None, it is rebuilt from ._data.

    Conversely, these fields can be invalidated:

    * when .set_data() is called, ._operations is set to None.
    * when .operations is set, ._data is set to None.
    Nrj   rH   rr   r9   c                    s   || _ g | _|d u rt d n[| }t|tr\d}|D ]4}| }t|tr*qt|ts<t	dt
|j dt n|| 7 }t|dksN|d dkrR|d7 }qt t| n| }|d usfJ t | || _d S )Nrs   zExpected StreamObject, got z instead. Data might be wrong.r   ru   r   )rH   _operationsr  r#  r   rJ   r7   r'   rK   r   typer   r!  r   r`   rr   )r>   rj   rH   rr   r@   r   
s_resolvedstream_datar  rA   rB   r   s  s2   



zContentStream.__init__r8   c                 C   s   t d| | d d |d}| j|_z| j}|d u rd | _n	t d||| _W n	 ty1   Y nw |  D ]\}}t|drD||n||||< q6|S )Nr?  Fr  r:   )r
   r;   rV   r  r  r:   rI   rZ   r<   r#  rH   r]   r@  rr   r  rA   rA   rB   r:     s(   
zContentStream.replicateFrA   rC   rD   c                 C   sr   z| j j|kr|s| W S W n	 ty   Y nw t }td| | dd||}|du r.g }|| |||| |S )z
        Clone object into pdf_dest.

        Args:
            pdf_dest:
            force_duplicate:
            ignore_fields:

        Returns:
            The cloned ContentStream

        r?  N)rG   rH   rI   r^   r
   r;   rV   r   r   rA   rA   rB   rF     s$   zContentStream.cloner   r   c                    s6   t d|}t |j || _t|j| _|j| _dS )r  r?  N)r
   r  r#  r  rH   r]   r@  rr   )r>   r   r8   rC   rD   r   src_csr  rA   rB   r     s
   
zContentStream._clonec                 C   s   | dd g }	 t|}|dv rd S | dd | s!|dv rJt|tj}|dkr?|g ks1J | |}| j|df n'| j||f g }n|d	kr\|d
vr[|	d}|d
vsRn
|t
|d | j q	)Nr   T)rs   r   ru   r!   )   '   "   BI   INLINE IMAGErt   )r   r   rs   )ry   r   isalphar   r&   delimiter_pattern_read_inline_imager@  r=   rw   rz   rr   )r>   rj   operandspeekoperatoriirA   rA   rB   _parse_content_stream  s,   

z#ContentStream._parse_content_streamc                 C   sx  t  }	 t|}|dd |dkrnt|| j}t|}|dd t|| j}|||< q|d}|d d dks=J |d|d	d
}| }t|t	rT|d }d|v s\d|v rat
|}	nd|v sid|v rnt|}	n~d|v svd|v r{t|}	nqd|v sd|v rt|}	nd|d
kr|dd}
t|
t	r|
d }
d|
v rd}n"d|
v rd}n|d|
dv rdnd}|dkr|d }nt|}	d}|dkr|ttt|d | tt|d  }	t|}|dd nt|}	|d}|dd |d d dks	|dd tvr7||d t|}	|d}|dd |d d dks0|dd tvr7td |||	d!S )"NTru   r!      Ir4   r   s   ID/Fz/Filterznot setr   AHxASCIIHexDecodeA85ASCII85DecodeRLRunLengthDecodeDCT	DCTDecodez/CSr6  RGBCMYK   z/BPC>   /G/I/Indexed/DeviceGrayr   g       @z/Wz/H   EIzHCould not extract inline image, even using fallback. Expected 'EI', got )settingsr@   )r   r   ry   rz   rH   rw   r   r   rJ   r]   r.   r-   r1   r/   r0   r   r
   r   r   r    )r>   rj   rc  r|   r   r   r{   filtrsavposr@   cslcsbitseirA   rA   rB   rK    sz   







"
$
$
z ContentStream._read_inline_imagec                 C   s   | j s\t }| jD ]M\}}|dkr=|d t }|d | || dd  |d ||d  |d n|D ]}|| |d	 q?|| |d
 q	| | _ | j S )NrH  rG  rc  r   s   ID r@   rb  ro   r   )r  r   r@  rp   rq   getvalue)r>   new_datarL  rN  	dict_textoprA   rA   rB   r!  a  s$   




zContentStream.get_datar@   c                    s   t  | g | _d S rN   )r  r#  r@  r"  r  rA   rB   r#  w  s   
zContentStream.set_datac                 C   s(   | j s| jr| t| j d| _| j S r  )r@  r  rP  r   rX   rA   rA   rB   
operations{  s   zContentStream.operationsro  c                 C   s   || _ d| _d S r  )r@  r  )r>   ro  rA   rA   rB   ro    s   
c                 C   sJ   | j r| j dg df | j g df d S | jr#d| j d | _d S d S )Nr      q   Qs   q
s   
Q
)r@  insertr=   r  rX   rA   rA   rB   isolate_graphics_state  s   z$ContentStream.isolate_graphics_staterk   c                    s&   | j s
| jr
|   t || d S rN   )r  r@  r!  r  rq   r&  r  rA   rB   rq     s   zContentStream.write_to_streamrN   r   r  )!r   r   r   __doc__r   r	   r_   r]   r   r   r   r   r:   r   r   r   rF   r   r^   rW   r   r   rP  rK  r`   r!  r#  r   ro  setterrs  rq   r<  rA   rA   r  rB   r?  ^  st    '
!
$L 
	r?  rj   rH   rr   c                 C   s  |  d}| dd |dkrt| |S |dkr4|  d}| dd |dkr/t| ||S t| |S |dkr?t| ||S |d	v rHt| S |d
krQt| |S |dkr_|  ddkr_t	 S |dkrht	| S |dkrt
|  t| }| dd t| ||S |dv r|  d}| t| d t|d ur|d usJ dt| |S t| S |  }| dd |  d}| | t|  td|d| d|)Nr!   ru      /   <r   rj  r   rn   )   t   f   (   e   s   endobj   nrt   s   0123456789+-.   r   iP   z(Invalid Elementary Object starting with z @z: )rw   ry   r&   r~   r   r2   r7   r"   r3   r'   r   r   rz   r   IndirectPatternmatchr%   r(   r   r   r   )rj   rH   rr   r|   rM  r   stream_extractrA   rA   rB   rz     sP   









rz   c                   @   s   e Zd ZdZdeddfddZedee fddZ	edee fd	d
Z
eded fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZdS )Fieldz
    A class representing a field dictionary.

    This class is accessed through
    :meth:`get_fields()<pypdf.PdfReader.get_fields>`
    r@   r9   Nc              	   C   s   t |  t t  }|j| _|D ]}z
|| | t|< W q ty(   Y qw t| 	dt
r[tt
| td  }t|trF| }n|d u rMd}ntdt|| td< d S d S )Nr   r6  zShould never happen)r   r   r   
attributesr   rG   r&   r   rJ   r   r'  r
   r!  r`   decoderI   r*   )r>   r@   field_attributesattrdd_strrA   rA   rB   r     s*   


zField.__init__c                 C      |  tjS )z4Read-only property accessing the type of this field.)r   r   r-  rX   rA   rA   rB   
field_type     zField.field_typec                 C   r  )z6Read-only property accessing the parent of this field.)r   r   ParentrX   rA   rA   rB   r     r  zField.parentr7   c                 C   r  )z4Read-only property accessing the kids of this field.)r   r   KidsrX   rA   rA   rB   kids  r  z
Field.kidsc                 C   r  )z4Read-only property accessing the name of this field.)r   r   TrX   rA   rA   rB   name  r  z
Field.namec                 C   r  )z>Read-only property accessing the alternate name of this field.)r   r   TUrX   rA   rA   rB   alternate_name  r  zField.alternate_namec                 C   r  )z
        Read-only property accessing the mapping name of this field.

        This name is used by pypdf as a key in the dictionary returned by
        :meth:`get_fields()<pypdf.PdfReader.get_fields>`
        )r   r   TMrX   rA   rA   rB   mapping_name     zField.mapping_namec                 C   r  )z
        Read-only property accessing the field flags, specifying various
        characteristics of the field (see Table 8.70 of the PDF 1.7 reference).
        )r   r   FfrX   rA   rA   rB   flags  s   zField.flagsc                 C   r  )zs
        Read-only property accessing the value of this field.

        Format varies based on field type.
        )r   r   VrX   rA   rA   rB   r        zField.valuec                 C   r  )z=Read-only property accessing the default value of this field.)r   r   DVrX   rA   rA   rB   default_value  r  zField.default_valuec                 C   r  )z
        Read-only property accessing the additional actions dictionary.

        This dictionary defines the field's behavior in response to trigger
        events. See Section 8.5.2 of the PDF 1.7 reference.
        )r   r   AArX   rA   rA   rB   additional_actions!  r  zField.additional_actions)r   r   r   rt  r   r   r   r   r&   r  r   r  r_   r  r  r  r   r  r   r   r  r  rA   rA   rA   rB   r    s.    	r  c                   @   sl  e Zd ZU dZdZee ed< dede	e
eeef deddfdd	Zed'ddZ	d(dede	deef ddfddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fdd Zeded
 fd!d"Zedee fd#d$Z edee fd%d&Z!dS ))Destinationa  
    A class representing a destination within a PDF file.

    See section 12.3.2 of the PDF 2.0 reference.

    Args:
        title: Title of this destination.
        page: Reference to the page of this destination. Should
            be an instance of :class:`IndirectObject<pypdf.generic.IndirectObject>`.
        fit: How the destination is displayed.

    Raises:
        PdfReadError: If destination type is invalid.

    Nnodetitlepagefitr9   c                 C   s  g | _ |j}|j}t|  t|| td< || td< || td< |dkrbt|dk r3|t	d t|dk r@|t	d t|dk rM|t	d |\| tt
j< | tt
j< | td	< d S t|d
krjd S |tjkr|\| tt
j< | tt
j< | tt
j< | tt
j< d S |tjtjfv rz|\| tt
j< W d S  ty   t | tt
j< Y d S w |tjtjfv rz|\| tt
j< W d S  ty   t | tt
j< Y d S w |tjtjfv rd S td|)N/Title/Pager   z/XYZr!   g        r   r4   /Zoomr   zUnknown Destination Type: )_filtered_childrenfit_typefit_argsr   r   r*   r&   r   r=   r(   TALEFTTOPTFFIT_RBOTTOMRIGHTFIT_HFIT_BHrI   r'   FIT_VFIT_BVFITFIT_Br   )r>   r  r  r  typargsrA   rA   rB   r   A  sV   

zDestination.__init__r7   c                    s(   t  d d g fdddD  S )Nr  r   c                    s   g | ]
}| v r | qS rA   rA   rQ   rX   rA   rB   r   z  s
    z*Destination.dest_array.<locals>.<listcomp>)/Left/Bottom/Right/Topr  )r7   r   rX   rA   rX   rB   
dest_arrayv  s   
zDestination.dest_arrayrj   rk   c                 C   s   |d ur	t dd |d td}|| |d | j}|| td}|| |d td}|| |d |d	 d S )
Nrl   rm   r   z/Dro   z/Sz/GoTor   r   )r   rp   r&   rq   r  )r>   rj   rk   r   r   value_srA   rA   rB   rq     s"   







zDestination.write_to_streamc                 C   
   |  dS )z3Read-only property accessing the destination title.r  r   rX   rA   rA   rB   r       
zDestination.titlec                 C   r  )zHRead-only property accessing the IndirectObject of the destination page.r  r  rX   rA   rA   rB   r    r  zDestination.pagec                 C   r  )z2Read-only property accessing the destination type.r   r  rX   rA   rA   rB   r    r  zDestination.typc                 C      |  ddS )z-Read-only property accessing the zoom factor.r  Nr  rX   rA   rA   rB   zoom  r  zDestination.zoomc                 C   r  )z<Read-only property accessing the left horizontal coordinate.r  Nr  rX   rA   rA   rB   left  r  zDestination.leftc                 C   r  )z=Read-only property accessing the right horizontal coordinate.r  Nr  rX   rA   rA   rB   right  r  zDestination.rightc                 C   r  )z9Read-only property accessing the top vertical coordinate.r  Nr  rX   rA   rA   rB   top  r  zDestination.topc                 C   r  )z<Read-only property accessing the bottom vertical coordinate.r  Nr  rX   rA   rA   rB   bottom  r  zDestination.bottomc                 C   s"   |  dttdtdtdgS )zHRead-only property accessing the color in (R, G, B) with values 0.0-1.0.z/Cr   )r   r7   r$   rX   rA   rA   rB   color  s   zDestination.colorc                 C   r  )z_
        Read-only property accessing the font type.

        1=italic, 2=bold, 3=both
        rR  r   r  rX   rA   rA   rB   font_format  r  zDestination.font_formatc                 C   r  )z
        Read-only property accessing the outline count.

        positive = expanded
        negative = collapsed
        absolute value = number of visible descendants at all levels
        r   Nr  rX   rA   rA   rB   outline_count  s   	zDestination.outline_count)r9   r7   rN   )"r   r   r   rt  r  r   r   __annotations__r_   r	   r(   r%   r'   r,   r   r   r  r   r`   rq   r  r  r  r   r  r$   r  r  r  r  r  r   r  r  rA   rA   rA   rB   r  ,  s^   
 
5
r  rN   )[
__author____author_email__loggingr   syscollections.abcr   r   ior   mathr   typingr   r   r   r	   r
   
_protocolsr   r   r   _utilsr   r   r   r   r   r   r   r   	constantsr   r   r   r   r-  r   r   r   r  r   r  errorsr   r   r    _baser"   r#   r$   r%   r&   r'   r(   r)   r*   r+   _fitr,   _image_inliner-   r.   r/   r0   r1   r2   r3   version_infor6   typing_extensions	getLoggerr   loggerr   r  r]   r7   r   r   r   r  rK   r  r'  r?  r_   r   rz   r  r  rA   rA   rA   rB   <module>   sl   (
0


 '    R .'  >
2c