o
    Zh{                     @   s  d Z dZddlZddlZddlZddlmZ ddl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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" 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/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZBmCZCmDZDmEZE ddlmFZFmGZG ejHdkrddl	mIZI nddlJmIZI eKeLZMeNdZOG dd dee
 e;ZPG dd dee
e
f e;ZQG dd deQZRde
ddfd d!ZSG d"d# d#eQZTG d$d% d%eTZUG d&d' d'eTZVG d(d) d)eUZW	d3d*ed+ee d,edeXeeX eeYeXf f dee;eYeXeWf fd-d.ZZG d/d0 d0eRZ[G d1d2 d2eRZ\dS )4zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)BytesIO)ceil)AnyCallableDictIterableListOptionalSequenceSetTupleUnioncast   )PdfReaderProtocolPdfWriterProtocolXmpInformationProtocol)	WHITESPACES
StreamTypedeprecation_no_replacementdeprecation_with_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 )Nr<   F	replicate)r   _reference_cloner<   hasattrappendr?   )selfr=   arrdata rF   U/var/www/html/lang_env/lib/python3.10/site-packages/pypdf/generic/_data_structures.pyr?   i   s   
zArrayObject.replicateFrF   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.r<   clone)indirect_referencepdf	Exceptionr   r@   r<   
isinstanceStreamObjectrK   rB   rA   )rC   r=   rH   rI   rD   rE   duprF   rF   rG   rK   x   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xrF   rF   rG   	<genexpr>   s    z'ArrayObject.hash_bin.<locals>.<genexpr>)hash	__class__tuplerC   rF   rF   rG   rU      s   zArrayObject.hash_binc                 C   s   t | S )z:Emulate DictionaryObject.items for a list (index, object).)	enumerater]   rF   rF   rG   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   /)
rO   listr\   setr.   strr+   r/   bytesr(   rC   r`   rF   rF   rG   _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

        )r<   extendrg   )rC   r`   temprF   rF   rG   __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

        )rh   rg   rf   rF   rF   rG   __iadd__   s   zArrayObject.__iadd__c              	   C   s:   |  |D ]}z
| |}| |= W q ty   Y qw | S )zAllow to remove items)rg   index
ValueError)rC   r`   rX   rF   rF   rG   __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)rC   ro   rp   rE   rF   rF   rG   rv      s   

zArrayObject.write_to_streamrM   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&   rs   zCould not read arrayT       %   ])r<   readr$   isspaceseekr   rB   read_object)ro   rM   rw   rD   tmptokZ
peek_aheadrF   rF   rG   read_from_stream   s0   



zArrayObject.read_from_streamFrF   rS   )__name__
__module____qualname__r   r?   boolr	   r
   r   rd   intrK   rU   r   r   r_   r   rg   rj   r;   rk   rn   r   re   rv   staticmethodr   r   r   rF   rF   rF   rG   r<   h   sT    



r<   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 )+DictionaryObjectr=   r>   c                 C   sP   t d| |  |d}|  D ]\}}t|dr||n||||< q|S )Nr   Fr?   )r   r@   r[   r_   rA   r?   )rC   r=   d__kvrF   rF   rG   r?     s   zDictionaryObject.replicateFrF   rH   rI   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 )rJ   r   Nr   )
rL   rM   rN   rc   r   r@   r[   lenkeys_clonerC   r=   rH   rI   visitedr   rF   rF   rG   rK     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)rO   raw_getr*   r   getr   )rW   fieldrI   r   rF   rG   rY   U  s"    

z*DictionaryObject._clone.<locals>.<genexpr>)/Next/Prev/N/V))r   r   )r   r   r   r   rL   rK   )rb   r   rO   r   r   anyr   r*   r   r   r@   r[   rL   idnum
generationaddrB   r+   rN   r   r_   rP   rA   rK   )rC   r   r=   rH   rI   r   rX   r`   r   ZobjsZcur_objZprev_objZclonr   r   scr   vvrF   r   rG   r   5  s   	







3


zDictionaryObject._clonec                 C   s    t | jtdd |  D fS )rR   c                 s   s     | ]\}}||  fV  qd S rS   rT   )rW   r   r   rF   rF   rG   rY     s    z,DictionaryObject.hash_bin.<locals>.<genexpr>)rZ   r[   r\   r_   r]   rF   rF   rG   rU     s   zDictionaryObject.hash_binkeyc                 C   s   t | |S rS   )dict__getitem__rC   r   rF   rF   rG   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)rC   r   r   rF   rF   rG   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)rO   r.   rm   r   __setitem__rC   r   r   rF   rF   rG   r     
   

zDictionaryObject.__setitem__c                 C   r   r   )rO   r.   rm   r   
setdefaultr   rF   rF   rG   r     r   zDictionaryObject.setdefaultc                 C   s   t | | S rS   )r   r   r   r   rF   rF   rG   r     s   zDictionaryObject.__getitem__c                 C   sN   ddl m} | dd}t|rdS | }t||s%||}|| td< |S )u  
        Retrieve XMP (Extensible Metadata Platform) data relevant to the 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)Zxmpr   r   r0   r   rO   r+   )rC   r   metadatarF   rF   rG   xmp_metadata  s   
zDictionaryObject.xmp_metadataro   rp   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 )Nrq   rr      <<
r   r&   %rz   rt      
   >>)r   ru   r_   r   rv   )rC   ro   rp   r   r   rF   rF   rG   rv     s   
$

z DictionaryObject.write_to_streamrM   rw   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_gensrM   r>   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 rF   rF   rV   r   r   rF   rG   
<listcomp>  s   ( zODictionaryObject.read_from_stream.<locals>.get_next_obj_pos.<locals>.<listcomp>)xrefvaluesminrm   )r   r   r   rM   outgenlocr   rF   r   rG   get_next_obj_pos  s   
z;DictionaryObject.read_from_stream.<locals>.get_next_obj_posro   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 .	   )tellrb   r   r|   findr$   r~   )ro   rM   Zeoncurrrwr   r   rF   rG   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    ry   rz   r&      >z)Expecting a NameObject for key but found z+Multiple definitions in dictionary at byte z	 for key    s   s   treamrt   )r      z)Stream data must be followed by a newliner   r   zStream length not definedzStream length not defined @pos=mypyr   __streamdata__r      ir   z7Unable to find 'endstream' marker after stream at byte z (nd='z', end='z').)$r   r   r   r   re   r|   r$   hexr   r   r~   r   r%   r#   r   rO   r,   r+   strictr   __repr__r   rN   r   updater   SALENGTHr-   r*   r   r   recompilerP   initialize_from_dictionary)ro   rM   rw   r   r   rE   r   r   excr   retvalmsgposr   eollengthtZpstarteZndstreamendrF   r   rG   r   
  s  










4

















z!DictionaryObject.read_from_streamr   rS   )"r   r   r   r   r?   r   r	   r
   r   rd   r   rK   r   r   r   rU   r   r   r   r   r   r.   r   propertyr   r   r   re   rv   r   r   r   r   r   rF   rF   rF   rG   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dctr>   c                 C   s    t |  |r| | d S d S rS   )r   __init__r   )rC   r   rF   rF   rG   r     s   
zTreeObject.__init__c                 C   s   d| v S )N/FirstrF   r]   rF   rF   rG   has_children     zTreeObject.has_childrenc                 C   s   |   S rS   )childrenr]   rF   rF   rG   __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   r0   )rC   Z	child_refchildrF   rF   rG   r     s   zTreeObject.childrenr   rM   c                 C   s   |  |d | d S rS   )insert_child)rC   r   rM   rF   rF   rG   	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   )	r0   r   r   r-   maxr   r+   inc_parent_counter_defaultr   )rC   r   r   rF   rF   rG   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&   rz   r   )	r0   r   r   r   r   absr-   r+   inc_parent_counter_outline)rC   r   r   Zopnr   rF   rF   rG   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   rL   r+   r-   r   r   rO   r   rN   )rC   r   r   rM   r   	child_objprevrF   rF   rG   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-   )rC   r  r  r  r  Znext_refZnext_objrF   rF   rG   _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   rL   r+   rm   r  _reset_node_tree_relationship)
rC   r   r  foundr  r  Zcur_refr  Zlast_refr  rF   rF   rG   remove_childF  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+   rm   r   r
  r]   rF   rF   rG   remove_from_treek  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+   )rC   r   r  rF   rF   rG   
empty_treeq  s   


zTreeObject.empty_treerS   r>   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  rF   rF   rF   rG   r     s\    


2
+
%r   r  r>   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  rF   rF   rG   r  ~  s   


r  c                       s8  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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 )*rP   r>   Nc                 C   s   d| _ d | _d S Nrx   )_datadecoded_selfr]   rF   rF   rG   r     s   
zStreamObject.__init__r=   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 )NrP   FDecodedStreamObjectr?   )	r   r@   r[   r  r  r?   rN   r_   rA   rC   r=   r   r  r   r   rF   rF   rG   r?     s(   
zStreamObject.replicater   rH   rI   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:

        rP   Nr  )r   r  r  rK   rN   superr   )rC   r   r=   rH   rI   r   r  r[   rF   rG   r     s   zStreamObject._clonec                    s   t t  | jfS )rR   )rZ   r  rU   r  r]   r  rF   rG   rU     s   	zStreamObject.hash_binc                 C   s   | j S rS   r  r]   rF   rF   rG   get_data  s   zStreamObject.get_datarE   c                 C   s
   || _ d S rS   r  rC   rE   rF   rF   rG   set_data     
zStreamObject.set_datac                    s   t   }||  7 }|S rS   )r  hash_value_datar  r  r  rF   rG   r    s   
zStreamObject.hash_value_dataro   rp   c                 C   sb   |d ur	t dd tt| j| ttj< t| | | tj= |	d |	| j |	d d S )Nrq   rr   s   
stream
s
   
endstream)
r   r-   r   r  r+   r   r   r   rv   ru   rC   ro   rp   rF   rF   rG   rv     s   
zStreamObject.write_to_streamc                 C   s   t ddd d S )NinitializeFromDictionaryr   rr   )r   )rE   rF   rF   rG   r    s   z%StreamObject.initializeFromDictionary)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   )rE   r   rF   rF   rG   r     s   



z'StreamObject.initialize_from_dictionaryrz   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  rO   r<   r+   FTFLATE_DECODEr,   r   DECODE_PARMS	TypeErrorr  r   encoder  )rC   r   r"  fparamsr   rF   rF   rG   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#  r,  r   rL   AttributeErrorr   r   r   )rC   r,  r   	extensionZbyte_streamZimgrF   rF   rG   decode_as_image'  s   
zStreamObject.decode_as_imager  rS   )rz   )r   r   r   r   r   r?   r   r   r	   r
   r   rd   r   r   r   r   rU   re   r  r  r  r   rv   r   r   r   r  r   r+  r0  __classcell__rF   rF   r  rG   rP     sT    

 

 rP   c                   @   s   e Zd ZdS )r  N)r   r   r   rF   rF   rF   rG   r  B  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  r>   Nc                 C   s
   d | _ d S rS   )r  r]   rF   rF   rG   r   G  r  zEncodedStreamObject.__init__c                 C   st   ddl m} | jd ur| j S t }|||  t|  D ]\}}|tj	tj
tjfvr2|||< q || _| S )Nr   )decode_stream_data)r#  r2  r  r  r  r  rb   r_   r   r   r  r&  )rC   r2  decodedr   r   rF   rF   rG   r  K  s   

zEncodedStreamObject.get_datarE   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!  r-  zData must be bytesr   zJStreams encoded with a filter different from FlateDecode are not supported)r#  r"  r   r   r  r$  r%  rO   re   r'  r  r  r  r  r(  r$   )rC   rE   r"  r  rF   rG   r  \  s   

zEncodedStreamObject.set_datar  )r   r   r   r   re   r  r  r1  rF   rF   r  rG   r  F  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.
    Nro   rM   rw   r>   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 )Nrx   zExpected StreamObject, got z instead. Data might be wrong.r   rz   r   )rM   _operationsr  r  r   rO   r<   r,   rP   r   typer   r  r   re   rw   )rC   ro   rM   rw   rE   r   Z
s_resolvedZstream_datar  rF   rG   r     s2   



zContentStream.__init__r=   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 )Nr4  Fr  r?   )r   r@   r[   r  r  r?   rN   r_   rA   r  rM   rb   r5  rw   r  rF   rF   rG   r?     s(   
zContentStream.replicateFrF   rH   rI   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

        r4  N)rL   rM   rN   rc   r   r@   r[   r   r   rF   rF   rG   rK     s$   zContentStream.cloner   r   c                    s6   t d|}t |j || _t|j| _|j| _dS )r  r4  N)r   r  r  r  rM   rb   r5  rw   )rC   r   r=   rH   rI   r   Zsrc_csr  rF   rG   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)rx   r   rz   r&   )   '   "   BI   INLINE IMAGEry   )r   r   rx   )r~   r   isalphar   r+   Zdelimiter_pattern_read_inline_imager5  rB   r|   r   rw   )rC   ro   operandspeekoperatoriirF   rF   rG   _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 )"NTrz   r&      Ir9   r   s   ID/Fz/Filterznot setr   ZAHxZASCIIHexDecodeZA85ZASCII85DecodeZRLZRunLengthDecodeZDCTZ	DCTDecodez/CSr-  RGBZCMYK   z/BPC>   z/Iz/Indexedz/Gz/DeviceGrayr   g       @z/Wz/H   EIzHCould not extract inline image, even using fallback. Expected 'EI', got )settingsrE   )r   r   r~   r   rM   r|   r   r   rO   rb   r3   r2   r6   r4   r5   r   r   r   r   r%   )rC   ro   rG  r   r   r   r   ZfiltrZsavposrE   csZlcsbitseirF   rF   rG   r<  $  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 )Nr:  r9  rG  r   s   ID rE   rF  rt   r   )r  r   r5  ru   rv   getvalue)rC   Znew_datar=  r?  Z	dict_textoprF   rF   rG   r  p  s$   




zContentStream.get_datarE   c                    s   t  | g | _d S rS   )r  r  r5  r  r  rF   rG   r    s   
zContentStream.set_datac                 C   s(   | j s| jr| t| j d| _| j S r  )r5  r  rA  r   r]   rF   rF   rG   
operations  s   zContentStream.operationsrN  c                 C   s   || _ d| _d S r  )r5  r  )rC   rN  rF   rF   rG   rN    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
)r5  insertrB   r  r]   rF   rF   rG   isolate_graphics_state  s   z$ContentStream.isolate_graphics_staterp   c                    s&   | j s
| jr
|   t || d S rS   )r  r5  r  r  rv   r  r  rF   rG   rv     s   zContentStream.write_to_streamrS   r   r  )!r   r   r   __doc__r   r   rd   r   r   r   r   r   r?   r   r	   r
   rK   r   r   r   r   r   rA  r<  re   r  r  r   rN  setterrR  rv   r1  rF   rF   r  rG   r4  m  st    '
!
$L 
	r4  ro   rM   rw   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&   rz      /   <r   rK  r   rs   )   t   f   (   e   s   endobj   nry   s   0123456789+-.   r   iP   z(Invalid Elementary Object starting with z @z: )r|   r~   r+   r   r   r7   r<   r'   r8   r,   r   r   r   r   IndirectPatternmatchr*   r-   r   r   r$   )ro   rM   rw   r   r>  r   Zstream_extractrF   rF   rG   r     sP   









r   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>`
    rE   r>   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   r-  zShould never happen)r   r   r   
attributesr   rL   r+   r   rO   r   r  r   r  re   decoderN   r/   )rC   rE   Zfield_attributesattrdZd_strrF   rF   rG   r     s*   


zField.__init__c                 C      |  tjS )z4Read-only property accessing the type of this field.)r   r   r$  r]   rF   rF   rG   
field_type     zField.field_typec                 C   rf  )z6Read-only property accessing the parent of this field.)r   r   ZParentr]   rF   rF   rG   r     rh  zField.parentr<   c                 C   rf  )z4Read-only property accessing the kids of this field.)r   r   ZKidsr]   rF   rF   rG   kids  rh  z
Field.kidsc                 C   rf  )z4Read-only property accessing the name of this field.)r   r   Tr]   rF   rF   rG   name  rh  z
Field.namec                 C   rf  )z>Read-only property accessing the alternate name of this field.)r   r   ZTUr]   rF   rF   rG   alternate_name  rh  zField.alternate_namec                 C   rf  )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   ZTMr]   rF   rF   rG   mapping_name     zField.mapping_namec                 C   rf  )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   ZFfr]   rF   rF   rG   flags  s   zField.flagsc                 C   rf  )zs
        Read-only property accessing the value of this field.

        Format varies based on field type.
        )r   r   Vr]   rF   rF   rG   r   "     zField.valuec                 C   rf  )z=Read-only property accessing the default value of this field.)r   r   ZDVr]   rF   rF   rG   default_value+  rh  zField.default_valuec                 C   rf  )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   ZAAr]   rF   rF   rG   additional_actions0  rn  zField.additional_actions)r   r   r   rS  r   r   r   r	   r+   rg  r   ri  rd   rk  rl  rm  r   ro  r   r   rr  rs  rF   rF   rF   rG   ra    s.    	ra  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fitr>   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   r9   /Zoomr   zUnknown Destination Type: )Z_filtered_childrenZfit_typeZfit_argsr   r   r/   r+   r   rB   r-   TAZLEFTZTOPTFZFIT_RZBOTTOMZRIGHTZFIT_HZFIT_BHrN   r,   ZFIT_VZFIT_BVZFITZFIT_Br$   )rC   rv  rw  rx  typargsrF   rF   rG   r   P  sV   

zDestination.__init__r<   c                    s(   t  d d g fdddD  S )Nrz  r   c                    s   g | ]
}| v r | qS rF   rF   rV   r]   rF   rG   r     s
    z*Destination.dest_array.<locals>.<listcomp>)/Left/Bottom/Right/Topr{  )r<   r   r]   rF   r]   rG   
dest_array  s   
zDestination.dest_arrayro   rp   c                 C   s   |d ur	t dd |d td}|| |d | j}|| td}|| |d td}|| |d |d	 d S )
Nrq   rr   r   z/Drt   z/Sz/GoTor   r   )r   ru   r+   rv   r  )rC   ro   rp   r   r   Zvalue_srF   rF   rG   rv     s"   







zDestination.write_to_streamc                 C   
   |  dS )z3Read-only property accessing the destination title.ry  r   r]   rF   rF   rG   rv       
zDestination.titlec                 C   r  )z9Read-only property accessing the destination page number.rz  r  r]   rF   rF   rG   rw    r  zDestination.pagec                 C   r  )z2Read-only property accessing the destination type.r   r  r]   rF   rF   rG   r~    r  zDestination.typc                 C      |  ddS )z-Read-only property accessing the zoom factor.r{  Nr  r]   rF   rF   rG   zoom  rh  zDestination.zoomc                 C   r  )z<Read-only property accessing the left horizontal coordinate.r  Nr  r]   rF   rF   rG   left  rh  zDestination.leftc                 C   r  )z=Read-only property accessing the right horizontal coordinate.r  Nr  r]   rF   rF   rG   right  rh  zDestination.rightc                 C   r  )z9Read-only property accessing the top vertical coordinate.r  Nr  r]   rF   rF   rG   top  rh  zDestination.topc                 C   r  )z<Read-only property accessing the bottom vertical coordinate.r  Nr  r]   rF   rF   rG   bottom  rh  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   r<   r)   r]   rF   rF   rG   color  s   zDestination.colorc                 C   r  )z_
        Read-only property accessing the font type.

        1=italic, 2=bold, 3=both
        rC  r   r  r]   rF   rF   rG   font_format  rq  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  r]   rF   rF   rG   outline_count  s   	zDestination.outline_count)r>   r<   rS   )"r   r   r   rS  ru  r	   r   __annotations__rd   r   r-   r*   r,   r1   r   r   r  r   re   rv   rv  r   rw  r~  r  r)   r  r  r  r  r  r   r  r  rF   rF   rF   rG   rt  ;  s^   
 
5
rt  rS   )]
__author____author_email__loggingr   sysior   mathr   typingr   r   r   r   r   r	   r
   r   r   r   r   Z
_protocolsr   r   r   Z_utilsr   r   r   r   r   r   r   r   r   	constantsr   r   r   r   r$  r    r   r!   r|  r"   r}  errorsr#   r$   r%   Z_baser'   r(   r)   r*   r+   r,   r-   r.   r/   r0   Z_fitr1   Z_image_inliner2   r3   r4   r5   r6   r7   r8   version_infor;   typing_extensions	getLoggerr   loggerr   r_  r<   r   r   r  rP   r  r  r4  rd   r   r   ra  rt  rF   rF   rF   rG   <module>   sj   4,0


 '   " R 4'  >
2c