U
    Nf                  
   @   sp  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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-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddlm=Z=m>Z> ej?dkrZddlm@Z@ nddlAm@Z@ eBeCZDdZEeFdZGG dd dee e9ZHG dd de
eef e9ZIG dd deIZJeddddZKG d d! d!eIZLG d"d# d#eLZMG d$d% d%eLZNG d&d' d'eMZOd/eee edePeeP e
eQePf f ee9eQePeOf d(d)d*ZRG d+d, d,eJZSG d-d. d.eJZTdS )0zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)BytesIO)AnyCallableDictIterableListOptionalSequenceSetTupleUnioncast   )PdfReaderProtocolPdfWriterProtocolXmpInformationProtocol)	WHITESPACES
StreamTypeb_deprecate_no_replacementdeprecate_with_replacementlogger_warningread_non_whitespaceread_until_regexskip_over_comment)CheckboxRadioButtonAttributesFieldDictionaryAttributesOutlineFontFlag)FilterTypes)StreamAttributes)TypArguments)TypFitArguments)STREAM_TRUNCATED_PREMATURELYPdfReadErrorPdfStreamError   )	BooleanObjectByteStringObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectTextStringObject)Fit)read_hex_string_from_streamread_string_from_stream)      )Selfs   +-s   [+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]c                   @   s   e Zd Zdeeeeeee	f   d dddZ
ee dddZeee d	d
dZed d	ddZeed	ddZeed	ddZdeedeef ddddZedeee edeee ee	ef f d dddZdS )ArrayObjectF pdf_destforce_duplicateignore_fieldsreturnc                 C   s   z| j j|kr|s| W S W n tk
r.   Y nX td| t ||}| D ]^}t|tr~|||||||}|	|j  qJt
|dr|	|||| qJ|	| qJ|S )Clone object into pdf_dest.r5   clone)indirect_referencepdf	Exceptionr   _reference_cloner5   
isinstanceStreamObjectr=   appendhasattr)selfr8   r9   r:   arrdatadupr6   r6   B/tmp/pip-unpacked-wheel-zevpxvmc/pypdf/generic/_data_structures.pyr=   a   s*    


zArrayObject.cloner;   c                 C   s   t | S )z:Emulate DictionaryObject.items for a list (index, object).)	enumeraterF   r6   r6   rJ   items   s    zArrayObject.items)lstr;   c                 C   sr   t |tttfrn\t |tr$|g}nJt |trR|d dkrFt|g}qnt|g}nt |trht	|g}n|g}|S )Nr   /)
rB   listtuplesetr-   strr*   r.   bytesr'   rF   rO   r6   r6   rJ   _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
        )r5   extendrW   )rF   rO   tempr6   r6   rJ   __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
        )rX   rW   rV   r6   r6   rJ   __iadd__   s    zArrayObject.__iadd__c              	   C   s>   |  |D ].}z| |}| |= W q
 tk
r6   Y q
X q
| S )zAllow to remove items)rW   index
ValueError)rF   rO   xr6   r6   rJ   __isub__   s    

zArrayObject.__isub__Nstreamencryption_keyr;   c                 C   sH   |d k	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)rF   ra   rb   rH   r6   r6   rJ   rh      s     

zArrayObject.write_to_streamra   r?   forced_encodingr;   c                 C   s   t  }| d}|dkr td| d}| r>| d}q*| dd | d}|dkr^q~| dd |t| || q |S )Nr%   re   zCould not read array   ])r5   readr#   isspaceseekrD   read_object)ra   r?   rj   rG   tmptokZ
peek_aheadr6   r6   rJ   read_from_stream   s    


zArrayObject.read_from_stream)Fr6   )N)N)__name__
__module____qualname__r   boolr   r	   r   rT   intr=   r   r   rN   r   rW   rZ   r4   r[   r_   r   rU   rh   staticmethodr   r   rs   r6   r6   r6   rJ   r5   `   s6        r5   c                   @   s.  e Zd Zd eeeeeee	f   d dddZ
d eeeeeee	f   eee	e	f  dddd	Zeed
ddZd!eeedddZeeedddZd"eee edddZeed
ddZeee dddZd#eedeef ddddZed$eee edeee ee	ef f d dddZdS )%DictionaryObjectFr6   r7   c                 C   s   z| j j|kr|s| W S W n tk
r.   Y nX t }td| |  ||}|dkrZg }t| dkr||	| |||| |S )r<   rz   Nr   )
r>   r?   r@   rS   r   rA   	__class__lenkeys_clonerF   r8   r9   r:   visitedZd__r6   r6   rJ   r=      s    
zDictionaryObject.cloneNsrcr8   r9   r:   r   r;   c              
      s  d} dk	st t  |t k rnt | trdtt | dkrT |=  |= qn |  d8  < |d7 }qt fdddD r0t  dD ]}|D ]}g }	|kr|| krt|trt| t	r
dddks&tt	| 
dddks&
ddtt	| 
ddkrtd	| }
| }|
dk	r
td	|
|
 ||}|jdk	r|jj}|jj}||f|krd}
q
|||f |	|
|f |dk	st |j|t|< |}z"|
krd}
ntd	|
| }
W n tk
r   d}
Y nX q8|	D ]\}}|||| | qqq D ]\}}| kr8t|trt|d
shd|_||| }|jdk	st |j| ||< n0|| kr8t|dr||| n|| t|< q8dS )z
        Update the object from src.

        Args:
            src: "DictionaryObject":
            pdf_dest:
            force_duplicate:
            ignore_fields:
        r   Nr%   c                 3   s   | ]}| ko~|ko~t |to~t | to~d ddkp~tt| d ddkp~d dtt| d dkV  qdS )/TypeN)rB   raw_getr)   rz   getr   ).0fieldr:   r   r6   rJ   	<genexpr>  s   
z*DictionaryObject._clone.<locals>.<genexpr>)/Next/Prev/N/V))r   r   )r   r   r   rz   r>   r=   )AssertionErrorrQ   r|   rB   rx   r   anyr   r)   rz   r   rA   r{   r>   idnum
generationaddrD   r*   r@   r~   rN   rC   rE   r=   )rF   r   r8   r9   r:   r   r^   rO   kobjsZcur_objZprev_objZclonr   r   scvvvr6   r   rJ   r~      s    



	

 
  
    

zDictionaryObject._clone)keyr;   c                 C   s   t | |S N)dict__getitem__rF   r   r6   r6   rJ   r   o  s    zDictionaryObject.raw_get)r   defaultr;   c                 C   s^   || kr| | S zd| kr |W S t dW n. t k
rX   td| d  || Y S X dS )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 presentrz   N)KeyErrorr   
get_objectget_inherited)rF   r   r   r6   r6   rJ   r   r  s     zDictionaryObject.get_inherited)r   valuer;   c                 C   s2   t |tstdt |ts$tdt| ||S Nzkey must be PdfObjectzvalue must be PdfObject)rB   r-   r]   r   __setitem__rF   r   r   r6   r6   rJ   r     s
    

zDictionaryObject.__setitem__c                 C   s2   t |tstdt |ts$tdt| ||S r   )rB   r-   r]   r   
setdefaultr   r6   r6   rJ   r     s
    

zDictionaryObject.setdefaultc                 C   s   t | | S r   )r   r   r   r   r6   r6   rJ   r     s    zDictionaryObject.__getitem__rK   c                 C   sN   ddl m} | dd}|dkr$dS | }t||sJ||}|| 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)xmpr   r   r   rB   r*   )rF   r   metadatar6   r6   rJ   xmp_metadata  s    
zDictionaryObject.xmp_metadatar`   c                 C   s   |d k	rt dd |d t|  D ]X\}}t|dkrV|d dkrV|d dkrVq(||| |d || |d	 q(|d
 d S )Nrc   rd      <<
r   r%   %rk   rf      
   >>)r   rg   rQ   rN   r|   rh   )rF   ra   rb   r   r   r6   r6   rJ   rh     s     
$

z DictionaryObject.write_to_streamri   c              
      s   t t tt  tt ddd tttd fdd}| d}|dkrZtd	t|   d
i }t	| }|dkrrq^n|dkr| 
dd t|  q^|stt|dkr| d q| 
dd z.t| |}t	| }| 
dd t| ||}W nh tk
rT }	 zH|d k	r|jrt|	 t|	 t t }
|
| |
 W Y S d }	~	X Y nX ||sl|||< q^dt|   d| }|d k	r|jrt|t|t q^|  }t	| }|dkr| ddkr| d}|dkr| d}q|dkrtd|dkr.| ddkr.| 
dd tj|kr||d k	rV|jrVtdntd|   t td|ttj< |tj }t|tr|  }|d k	st||}| 
|d |d k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< nf|d k	r|js| 
|d || ||d< |  }n0| 
|d td"t|   d#|d$|d%n| 
|d d|krt"#|S t }
|
| |
S d S )&N)pp1rem_gensr?   r;   c              	      sX   }|D ]J}|j | }z&t|t fdd| D }W q tk
rP   Y qX q|S )Nc                    s(   g | ] } |  k rkrn q|qS r6   r6   r   r^   r   r   r6   rJ   
<listcomp>  s
      
  zODictionaryObject.read_from_stream.<locals>.get_next_obj_pos.<locals>.<listcomp>)xrefminvaluesr]   )r   r   r   r?   outgenlocr6   r   rJ   get_next_obj_pos  s    
&z;DictionaryObject.read_from_stream.<locals>.get_next_obj_pos)ra   r?   r;   c                    sz    |   dt|j|d }|   }| ||    }|d}|dk rXtd| d| || d  |d |d  S )Nl        r%   	   endstreamr   z6Unable to find 'endstream' marker for obj starting at .	   )tellrQ   r   rm   findr#   ro   )ra   r?   Zeoncurrrwr   r   r6   rJ   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 '<<'       %rk   r%      >z+Multiple definitions in dictionary at byte z	 for key    s   s   treamrf   )r      z)Stream data must be followed by a newliner   r   zStream length not definedzStream length not defined @pos=r   __streamdata__r      ir   z7Unable to find 'endstream' marker after stream at byte z (nd='z', end='z').)$rx   r   r   r   rU   rm   r#   hexr   r   ro   r   r$   r"   rp   r@   strict__repr__r   rt   rz   updater   SALENGTHr,   r*   rB   r)   r   r   r   recompilerC   initialize_from_dictionary)ra   r?   rj   r   rq   rH   rr   r   r   excretvalmsgposr   eollengthtZpstarteZndstreamendr6   r   rJ   rs     s        








 



 






z!DictionaryObject.read_from_stream)Fr6   )N)N)N)N) rt   ru   rv   r   rw   r   r	   r   rT   rx   r=   r
   r   r~   r   r   r   r   r   r-   r   propertyr   r   r   rU   rh   ry   r   r   r   rs   r6   r6   r6   rJ   rz      sH     q   rz   c                   @   s  e Zd Zd&ee ddddZedddZeddd	Ze	dd
dZ
ee	 dddZe	eddddZeded f eddddZeded f eddddZd'e	e	eeede	f  edddZe	e	e	e	ddddZe	ddddZddd d!Zddd"d#Zddd$d%ZdS )(
TreeObjectN)dctr;   c                 C   s   t |  |r| | d S r   )rz   __init__r   )rF   r   r6   r6   rJ   r   ]  s    
zTreeObject.__init__rK   c                 C   s   t ddd |  S )NhasChildrenhas_children4.0.0)r   r   rM   r6   r6   rJ   r   b  s    zTreeObject.hasChildrenc                 C   s   d| kS )N/Firstr6   rM   r6   r6   rJ   r   f  s    zTreeObject.has_childrenc                 C   s   |   S r   )childrenrM   r6   r6   rJ   __iter__i  s    zTreeObject.__iter__c                 c   sb   |   sd S | td }| }|V  || td kr:d S |td}|d krTd S | }q d S )Nr   /Lastr   )r   r*   r   r   )rF   Z	child_refchildr6   r6   rJ   r   l  s    zTreeObject.children)r   r?   r;   c                 C   s   |  |d | d S r   )insert_child)rF   r   r?   r6   r6   rJ   	add_child{  s    zTreeObject.add_child)parentnr;   c                 C   sb   |d krd S t d| }d|kr^ttdt t|td | |td< | |dd | d S )Nr   /Countr   r   )r   r   r,   maxrx   r*   inc_parent_counter_defaultr   )rF   r   r   r6   r6   rJ   r   ~  s    z%TreeObject.inc_parent_counter_defaultc                 C   s   |d krd S t d| }|dddk}t t|dd}|dk rLt|}t|| |r\dnd |td< |std S | |dd | d S )	Nr   z
/%is_open%Tr   r   r%   rk   r   )r   r   r   rx   absr,   r*   inc_parent_counter_outline)rF   r   r   Zopnr   r6   r6   rJ   r     s     z%TreeObject.inc_parent_counter_outline.)r   beforer?   inc_parent_counterr;   c                 C   s  |d kr| j }| }|j}d| kr|| td< td| td< || td< | j|td< || |dd d|krz|d= d|kr|d= |S td	| d }|j|krd|krtd
|d }qtd
||td< |j|td< | j|td< d|kr|d= || td< || |dd |S qz8t|d ts4t	||d td< |d |td< W n t
k
rr   |d= Y nX ||td< ||td< | j|td< || |dd |S )Nr   r   r   r   r   r%   r   r   rz   r   )r   r   r>   r*   r,   r   r   rB   rz   r   r@   )rF   r   r   r?   r   	child_objprevr6   r6   rJ   r     sL    zTreeObject.insert_child)r   prev_refcurlastr;   c                 C   s   | tdd}|dkr|rX| }|td= || td< t| td d | td< qtd| td< | td= td| kr| td= nd|r| }||td< ||td< n"||kst|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   )rF   r   r   r   r   Znext_refZnext_objr6   r6   rJ   _remove_node_from_tree  s*    


z!TreeObject._remove_node_from_tree)r   r;   c           
      C   s   |  }|j}td|kr$tdn|td | kr<tdd}d }d }| td }|  }| td }|  }	|d k	r||kr| ||||	 d}q|}|}td|kr|td }|  }qpd }d }qp|st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   r>   r*   r]   r   _reset_node_tree_relationship)
rF   r   r   foundr   r   Zcur_refr   Zlast_refr   r6   r6   rJ   remove_child  s8    

zTreeObject.remove_childc                 C   s.   t d| krtdntd| d |  dS )z)Remove the object from the tree it is in.r   r   r   N)r*   r]   r   r  rM   r6   r6   rJ   remove_from_tree  s    
zTreeObject.remove_from_treec                 C   s   t ddd |   d S )N	emptyTree
empty_treer   )r   r  rM   r6   r6   rJ   r  #  s    zTreeObject.emptyTreec                 C   s`   | D ]}|  }t| qtd| kr0| td= td| krF| td= td| kr\| td= d S )Nr   r   r   )r   r   r*   )rF   r   r   r6   r6   rJ   r  '  s    


zTreeObject.empty_tree)N)N)rt   ru   rv   r   rz   r   rw   r   r   r   r   r   r   r   r   r   r)   rx   r   r   r   r   r   r  r  r  r  r6   r6   r6   rJ   r   \  s@      4   *%r   )r   r;   c                 C   s:   | t d= t d| kr | t d= t d| kr6| t d= 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   r6   r6   rJ   r   4  s
    	

r   c                       s   e Zd ZddddZeeeeee	e
ef   eeeef  dd fddZe	ee
f ddd	Zedd
ddZed fddZdee	de
ef ddddZeee
ef e	d d
ddZeee
ef e	d d
ddZdeddddZ  ZS )rC   NrK   c                 C   s   d| _ d | _d S N    )_datadecoded_selfrM   r6   r6   rJ   r   E  s    zStreamObject.__init__r   c                    sr   t d|j| _z6t d|j}|dkr,d| _nt d||||| _W n tk
rX   Y nX t ||||| dS )
        Update the object from src.

        Args:
            src:
            pdf_dest:
            force_duplicate:
            ignore_fields:
        rC   NDecodedStreamObject)r   r  r  r=   r@   superr~   )rF   r   r8   r9   r:   r   r  r{   r6   rJ   r~   I  s    
zStreamObject._clonec                 C   s   | j S r   r  rM   r6   r6   rJ   get_datah  s    zStreamObject.get_datarH   r;   c                 C   s
   || _ d S r   r  rF   rH   r6   r6   rJ   set_datak  s    zStreamObject.set_datac                    s   t   }|t| j7 }|S r   )r  hash_value_datar   r  r  r  r6   rJ   r  n  s    
zStreamObject.hash_value_datar`   c                 C   sb   |d k	rt dd tt| j| ttj< t| | | tj= |	d |	| j |	d d S )Nrc   rd   s   
stream
s
   
endstream)
r   r,   r|   r  r*   r   r   rz   rh   rg   rF   ra   rb   r6   r6   rJ   rh   s  s     
zStreamObject.write_to_stream)EncodedStreamObjectr
  c                 C   s
   t | S r   )rC   r   )rH   r6   r6   rJ   initializeFromDictionary  s    z%StreamObject.initializeFromDictionaryc                 C   s>   t j| krt }nt }| d |_| d= | t j= ||  |S )Nr   )r   FILTERr  r
  r  r   r   )rH   r   r6   r6   rJ   r     s    


z'StreamObject.initialize_from_dictionaryrk   r  )levelr;   c              	   C   s  ddl m} tj| kr| tj }t|trtttjf|}z tt	 f| 
tjt }W q tk
r   tt	 | 
tjt g}Y qX qtttj|g}tt	 | 
tjt	 g}nttj}d }t }||  ||ttj< |d k	r||ttj< |t| j||_|S )Nr   FlateDecode)filtersr  r   r  rB   r5   r*   FTFLATE_DECODEr+   r   DECODE_PARMS	TypeErrorr  r   encoder   r  )rF   r  r  fparamsr   r6   r6   rJ   flate_encode  s4    





zStreamObject.flate_encode)N)rk   )rt   ru   rv   r   rz   r   rw   r   r	   r   rT   rx   r
   r   r~   rU   r  r  r  r   rh   ry   r   r   r  r   r"  __classcell__r6   r6   r  rJ   rC   D  s4     

rC   c                   @   s   e Zd ZdS )r
  N)rt   ru   rv   r6   r6   r6   rJ   r
    s   r
  c                       sH   e Zd ZddddZeeef dddZedd fdd	Z  Z	S )
r  NrK   c                 C   s
   d | _ d S r   )r  rM   r6   r6   rJ   r     s    zEncodedStreamObject.__init__c                 C   s|   ddl m} | jd k	r | j S t }|t||  t|  D ]$\}}|t	j
t	jt	jfkrD|||< qD|| _| S d S )Nr   )decode_stream_data)r  r$  r  r  r
  r  r   rQ   rN   r   r   r  r  )rF   r$  decodedr   r   r6   r6   rJ   r    s    


zEncodedStreamObject.get_datar  c                    sl   ddl m} | tjdtjkr`t|ts2t	d| j
d k	s@t| j
| t || ntdd S )Nr   r   zdata must be byteszLStreams encoded with different filter from only FlateDecode is not supported)r  r  r   r   r  r  r  rB   rU   r  r  r   r  r  r  r#   )rF   rH   r  r  r6   rJ   r    s    
zEncodedStreamObject.set_data)
rt   ru   rv   r   r   rU   rT   r  r  r#  r6   r6   r  rJ   r    s   r  c                
       s^  e Zd ZdZd"eeedeee ee	ef f dd fddZ
d#eeeeeee	f   d dd	d
Zeeeeeeee	f   eee	e	f  dd fddZeddddZeeeef dddZedddZedd fddZeeeeef  dddZejeeeef  ddddZddddZd$eedeef dd 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.
    Nri   c                    s   || _ g | _|d kr"t d n| }t|trd}|D ]8}|t|  7 }t	|dksl|d dkr<|d7 }q<t t
| n$| }|d k	stt t| || _d S )Nr  r   rk   r   )r?   _operationsr  r  r   rB   r5   r   r  r|   rU   r   rj   )rF   ra   r?   rj   rH   r   Zstream_datar  r6   rJ   r     s     

zContentStream.__init__Fr6   r7   c                 C   st   z| j j|kr|s| W S W n tk
r.   Y nX t }td| | dd||}|dkr^g }|| |||| |S )z
        Clone object into pdf_dest.

        Args:
            pdf_dest:
            force_duplicate:
            ignore_fields:

        Returns:
            The cloned ContentStream
        r'  N)r>   r?   r@   rS   r   rA   r{   r~   r   r6   r6   rJ   r=     s$    

  zContentStream.cloner   c                    s:   t d|}t t|j || _t|j| _|j| _dS )r	  r'  N)	r   r  r  r   r  r?   rQ   r(  rj   )rF   r   r8   r9   r:   r   Zsrc_csr  r6   rJ   r~   9  s
    
zContentStream._clone)ra   r;   c                 C   s   | dd g }t|}|dks|dkr*q| dd | sF|dkrt|tj}|dkr|g ksft| |}| j	|df q| j	||f g }q|dkr|d	kr|
d}qq|	t|d | j qd S )
Nr   r  rk   r%   )   '   "   BI   INLINE IMAGEr   )r   r   r  )ro   r   isalphar   r*   Zdelimiter_patternr   _read_inline_imager(  rD   rm   rp   rj   )rF   ra   operandspeekoperatoriir6   r6   rJ   _parse_content_streamR  s$    
z#ContentStream._parse_content_streamc                 C   s  t  }t|}|dd |dkr$qZt|| j}t|}|dd t|| j}|||< q|d}|d d dksxtt }|d}|std|	d	}	|	dkr|
| q~|
|d
|	  ||	t| d |d}|d}
|
dkr|dd |
| q~||
 }|d}|tkrB|dd |
| q~||	d |	 tkr|tkrp|d}qX|dd qq~|tkr||7 }|d}q|dd |dkrqq~|d	kr|d}|dd |dkrqq~|
| q~|| dS )Nrk   r%      Ir2   r   s   IDi    zUnexpected end of stream   Er      Qs   EMC)settingsrH   )rz   r   ro   rp   r?   rm   r   r   r#   r   rg   r|   r   getvalue)rF   ra   r9  rr   r   r   rq   rH   bufr   Ztok2infoZtok3Zoper6   r6   rJ   r.  q  sl    













z ContentStream._read_inline_imagerK   c                 C   s   | j st }| jD ]\}}|dkrz|d t }|d | || dd  |d ||d  |d n,|D ]}|| |d	 q~|t| |d
 q| | _ t| j S )Nr,  r+  r9  r   r6  s   ID rH   s   EIrf   r   )r  r   r(  rg   rh   r:  r   )rF   Znew_datar/  r1  Z	dict_textopr6   r6   rJ   r    s$    



zContentStream.get_datar  c                    s   t  | g | _d S r   )r  r  r(  r  r  r6   rJ   r    s    zContentStream.set_datac                 C   s,   | j s&| jr&| tt| j d| _| j S r  )r(  r  r3  r   r   rM   r6   r6   rJ   
operations  s    zContentStream.operations)r>  r;   c                 C   s   || _ d| _d S r  )r(  r  )rF   r>  r6   r6   rJ   r>    s    c                 C   sH   | j r*| j dg df | j g df n| jrDdt| j d | _d S )Nr   qQs   q
s   
Q
)r(  insertrD   r  r   rM   r6   r6   rJ   isolate_graphics_state  s
    z$ContentStream.isolate_graphics_stater`   c                    s&   | j s| jr|   t || d S r   )r  r(  r  r  rh   r  r  r6   rJ   rh     s    zContentStream.write_to_stream)N)Fr6   )N) rt   ru   rv   __doc__r   r   rT   r   r   rx   r   rw   r   r	   r=   rz   r   r
   r   r~   r   r3  r.  rU   r  r  r   r>  setterrB  rh   r#  r6   r6   r  rJ   r'    sL    #  %O	  r'  ri   c              	   C   s  |  d}| dd |dkr*t| |S |dkrl|  d}| dd |dkr^t| ||S t| |S nf|dkrt| ||S |d	ks|d
krt| S |dkrt| |S |dkr|  ddkr| dd t	 S |dk rt	| S |dkrB|dkr"|  d}t
|dk rtd qt| }| dd t| ||S |dkr|  d}| t
| d t|d k	r|d k	stt| |S t| S n4| dd td|d|   d|  d  d S )Nr%   rk      /   <r   r6  r   re      t   f   (   e   s   endobji   nr   )r   r   r   zFile ended unexpectedly.s   0123456789+-.   iz(Invalid Elementary Object starting with z @z: P   )rm   ro   r*   rs   rz   r0   r5   r&   r1   r+   r|   r$   r   rp   IndirectPatternmatchr   r)   r,   r#   r   r   )ra   r?   rj   rr   r0  r6   r6   rJ   rp     sP    










"rp   c                   @   s   e Zd ZdZeddddZeee dddZ	eee dd	d
Z
eed dddZeee dddZeee dddZeee dddZeee dddZeee dddZeee dddZeee dddZdS )Fieldz
    A class representing a field dictionary.

    This class is accessed through
    :meth:`get_fields()<pypdf.PdfReader.get_fields>`
    Nr  c              	   C   s   t |  t t  }|j| _|D ].}z|| | t|< W q& tk
rR   Y q&X q&t| 	dt
rtt
| td  }t|tr| }n|d krd}ntdt|| td< d S )Nr   r&  zShould never happen)rz   r   r   
attributesr   r>   r*   r   rB   r   r  r   r  rU   decoder@   r.   )rF   rH   Zfield_attributesattrdZd_strr6   r6   rJ   r   5  s&    


zField.__init__rK   c                 C   s   |  tjS )z4Read-only property accessing the type of this field.)r   r   r  rM   r6   r6   rJ   
field_typeL  s    zField.field_typec                 C   s   |  tjS )z6Read-only property accessing the parent of this field.)r   r   ZParentrM   r6   r6   rJ   r   Q  s    zField.parentr5   c                 C   s   |  tjS )z4Read-only property accessing the kids of this field.)r   r   ZKidsrM   r6   r6   rJ   kidsV  s    z
Field.kidsc                 C   s   |  tjS )z4Read-only property accessing the name of this field.)r   r   TrM   r6   r6   rJ   name[  s    z
Field.namec                 C   s   |  tjS )z>Read-only property accessing the alternate name of this field.)r   r   ZTUrM   r6   r6   rJ   alternate_name`  s    zField.alternate_namec                 C   s   |  tjS )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TMrM   r6   r6   rJ   mapping_namee  s    zField.mapping_namec                 C   s   |  tjS )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FfrM   r6   r6   rJ   flagso  s    zField.flagsc                 C   s   |  tjS )zs
        Read-only property accessing the value of this field.

        Format varies based on field type.
        )r   r   VrM   r6   r6   rJ   r   w  s    zField.valuec                 C   s   |  tjS )z=Read-only property accessing the default value of this field.)r   r   ZDVrM   r6   r6   rJ   default_value  s    zField.default_valuec                 C   s   |  tjS )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AArM   r6   r6   rJ   additional_actions  s    zField.additional_actions)rt   ru   rv   rC  rz   r   r   r   r*   rV  r   rW  rT   rY  rZ  r[  rx   r\  r   r   r^  r_  r6   r6   r6   rJ   rQ  -  s,   	rQ  c                   @   sd  e Zd ZU dZdZee ed< ee	e
eeef eddddZeddd	d
Zd$ee	deef ddddZeee dddZeee dddZeee dddZeee dddZeee dddZeee dddZeee dddZeee dddZeed dddZeee dd d!Z eee dd"d#Z!dS )%Destinationa  
    A class representing a destination within a PDF file.

    See section 8.2.1 of the PDF 1.6 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rt|dk rf|t	d t|dk r|t	d t|dk r|t	d |\| tt
j< | tt
j< | td	< nt|d
krԐn|tjkr|\| tt
j< | tt
j< | tt
j< | tt
j< n|tjtjfkrfz|\| tt
j< W n& tk
rb   t | tt
j< Y nX nr|tjtjfkrz|\| tt
j< W n& tk
r   t | tt
j< Y nX n"|tjtjfkrntd|d S )N/Title/Pager   z/XYZr%   g        r   r2   /Zoomr   zUnknown Destination Type: )Z_filtered_childrenZfit_typeZfit_argsrz   r   r.   r*   r|   rD   r,   TALEFTZTOPTFZFIT_RZBOTTOMRIGHTZFIT_HZFIT_BHr@   r+   ZFIT_VZFIT_BVZFITZFIT_Br#   )rF   rb  rc  rd  typargsr6   r6   rJ   r     sV    

zDestination.__init__r5   rK   c                    s(   t  d d g fdddD  S )Nrf  r   c                    s   g | ]}| kr | qS r6   r6   r   rM   r6   rJ   r     s   z*Destination.dest_array.<locals>.<listcomp>)/Left/Bottom/Right/Toprg  )r5   r   rM   r6   rM   rJ   
dest_array  s    
zDestination.dest_arrayr`   c                 C   s   |d k	rt dd |d td}|| |d | j}|| td}|| |d td}|| |d |d	 d S )
Nrc   rd   r   z/Drf   z/Sz/GoTor   r   )r   rg   r*   rh   rr  )rF   ra   rb   r   r   Zvalue_sr6   r6   rJ   rh     s$     







zDestination.write_to_streamc                 C   s
   |  dS )z3Read-only property accessing the destination title.re  r   rM   r6   r6   rJ   rb    s    zDestination.titlec                 C   s
   |  dS )z9Read-only property accessing the destination page number.rf  rs  rM   r6   r6   rJ   rc     s    zDestination.pagec                 C   s
   |  dS )z2Read-only property accessing the destination type.r   rs  rM   r6   r6   rJ   rl    s    zDestination.typc                 C   s   |  ddS )z-Read-only property accessing the zoom factor.rg  Nrs  rM   r6   r6   rJ   zoom
  s    zDestination.zoomc                 C   s   |  ddS )z<Read-only property accessing the left horizontal coordinate.rn  Nrs  rM   r6   r6   rJ   left  s    zDestination.leftc                 C   s   |  ddS )z=Read-only property accessing the right horizontal coordinate.rp  Nrs  rM   r6   r6   rJ   right  s    zDestination.rightc                 C   s   |  ddS )z9Read-only property accessing the top vertical coordinate.rq  Nrs  rM   r6   r6   rJ   top  s    zDestination.topc                 C   s   |  ddS )z<Read-only property accessing the bottom vertical coordinate.ro  Nrs  rM   r6   r6   rJ   bottom  s    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   r5   r(   rM   r6   r6   rJ   color#  s     zDestination.colorc                 C   s   |  ddS )z_
        Read-only property accessing the font type.

        1=italic, 2=bold, 3=both
        z/Fr   rs  rM   r6   r6   rJ   font_format*  s    zDestination.font_formatc                 C   s   |  ddS )z
        Read-only property accessing the outline count.

        positive = expanded
        negative = collapsed
        absolute value = number of visible descendants at all levels
        r   Nrs  rM   r6   r6   rJ   outline_count3  s    	zDestination.outline_count)N)"rt   ru   rv   rC  ra  r   rz   __annotations__rT   r   r,   r)   r+   r/   r   r   rr  r   rU   rh   rb  rx   rc  rl  rt  r(   ru  rv  rw  rx  ry  r   rz  r{  r6   r6   r6   rJ   r`    sP   
5  r`  )N)U
__author____author_email__loggingr   sysior   typingr   r   r   r   r   r   r	   r
   r   r   r   Z
_protocolsr   r   r   _utilsr   r   r   r   r   r   r   r   r   	constantsr   r   r   r   r  r   r   r    rh  r!   rj  errorsr"   r#   r$   _baser&   r'   r(   r)   r*   r+   r,   r-   r.   Z_fitr/   r0   r1   version_infor4   Ztyping_extensions	getLoggerrt   loggerZNumberSignsr   rO  r5   rz   r   r   rC   r
  r  r'  rT   rx   rp   rQ  r`  r6   r6   r6   rJ   <module>   s\   4,,

   y Ys&   7c