U
    Bb+                 
   @   sf  d Z dZdZddlZddlZddl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 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$m%Z%m&Z&m'Z' ddl(m)Z)m*Z* ddl(m+Z, ddl(m-Z. ddl(m/Z0 ddl(m1Z2 ddl3m4Z4m5Z5m6Z6m7Z7 e8e9Z:dZ;dZ<e=dZ>G dd dZ?G dd de?Z@G dd de?ZAG dd deBe?ZCG dd de?ZDG dd dejEe?ZFG d d! d!eGe?ZHeed" d#d$d%ZId_eedeJeeJ eeGeJf f ed" d&d'd(ZKd`eedeJeeJ eeGeJf f ed" d&d)d*ZLdaeedeJeeJ eeGeJf f ed" d&d+d,ZMG d-d. d.eNe?ZOG d/d0 d0eJe?ZPG d1d2 d2eJe?ZQG d3d4 d4eRe?ZSG d5d6 d6eSZTG d7d8 d8eSZUG d9d: d:eUZVG d;d< d<eUZWG d=d> d>eVZXdbeeedeJeeJ eeGeJf f ee?eGeJeXf d?d@dAZYG dBdC dCeCZZG dDdE dEeTZ[G dFdG dGe
Z\G dHdI dIeTZ]G dJdK dKe]Z^dceeJeNf edeJeeJ eeGeJf f eePeOf dLdMdNZ_ddeeJeNf edeJeeJ eeGeJf f eePeOf dLdOdPZ`eDeJeeeaeaeaf  ebebeTdQdRdSZceJeNdTdUdVZdeNeJdWdXdYZeeJeeaeaeaf dZd[d\ZfG d]d^ d^ZgdS )ezHImplementation of generic PDF objects (dictionary, number, string, ...).zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)IntFlag)BytesIO)	AnyCallableDictIterableListOptionalTupleUnioncast   )_pdfdoc_encoding_pdfdoc_encoding_revrev_encoding)WHITESPACES
StreamTypeb_deprecate_no_replacementdeprecate_with_replacementhex_str	hexencodelogger_warningread_non_whitespaceread_until_regexskip_over_commentstr_)CheckboxRadioButtonAttributesFieldDictionaryAttributes)FilterTypes)StreamAttributes)TypArguments)TypFitArguments)STREAM_TRUNCATED_PREMATURELYPdfReadErrorPdfReadWarningPdfStreamErrors   /<[tf(n%s   +-s   [+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]c                   @   s|   e Zd ZU ejZed ed< edddZ	edddZ
ed  ddd	Zed  dd
dZeedeef ddddZdS )	PdfObject).zhashlib._Hash	hash_funcreturnc                 C   s   d|    S )Nz%s)encodeself r.   2/tmp/pip-unpacked-wheel-weujb6sz/PyPDF2/generic.pyhash_value_data^   s    zPdfObject.hash_value_datac                 C   s"   d| j j| |   f  S )Nz%s:%s)	__class____name__r(   r0   	hexdigestr+   r,   r.   r.   r/   
hash_valuea   s    zPdfObject.hash_valuec                 C   s   | S )zResolve indirect references.r.   r,   r.   r.   r/   
get_objectj   s    zPdfObject.get_objectc                 C   s   t dd |  S )N	getObjectr5   )r   r5   r,   r.   r.   r/   r6   n   s    
zPdfObject.getObjectNstreamencryption_keyr*   c                 C   s   t d S N)NotImplementedErrorr-   r8   r9   r.   r.   r/   write_to_streamr   s    zPdfObject.write_to_stream)r2   
__module____qualname__hashlibsha1r(   r   __annotations__bytesr0   r4   r	   r5   r6   r   r   strr=   r.   r.   r.   r/   r'   Z   s   
	 r'   c                   @   sz   e Zd Zeedeef ddddZeed dddZ	eedeef dddd	Z
ed
ddZeed dddZdS )
NullObjectNr7   c                 C   s   | d d S )N   null)writer<   r.   r.   r/   r=   y   s    zNullObject.write_to_streamr8   r*   c                 C   s    |  d}|dkrtdt S )N   rF   zCould not read Null object)readr$   rE   )r8   Znulltxtr.   r.   r/   read_from_stream~   s    
zNullObject.read_from_streamc                 C   s   t dd | || d S NwriteToStreamr=   r   r=   r<   r.   r.   r/   rM      s    
zNullObject.writeToStreamr)   c                 C   s   dS )NrE   r.   r,   r.   r.   r/   __repr__   s    zNullObject.__repr__c                 C   s   t dd t| S NreadFromStreamrK   )r   rE   rK   r8   r.   r.   r/   rQ      s    
zNullObject.readFromStream)r2   r>   r?   r   r   rD   rC   r=   staticmethodrK   rM   rO   rQ   r.   r.   r.   r/   rE   x   s     rE   c                   @   s   e Zd ZeddddZeedddZedd	d
Z	e
edeef ddddZe
edeef ddddZee
d dddZee
d dddZdS )BooleanObjectNvaluer*   c                 C   s
   || _ d S r:   rV   r-   rV   r.   r.   r/   __init__   s    zBooleanObject.__init__)_BooleanObject__or*   c                 C   s2   t |tr| j|jkS t |tr*| j|kS dS d S )NF)
isinstancerT   rV   bool)r-   rZ   r.   r.   r/   __eq__   s
    


zBooleanObject.__eq__r)   c                 C   s   | j r
dS dS )NTrueFalserW   r,   r.   r.   r/   rO      s    zBooleanObject.__repr__r7   c                 C   s    | j r|d n
|d d S )N   trues   false)rV   rG   r<   r.   r.   r/   r=      s    zBooleanObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM      s    
zBooleanObject.writeToStreamrH   c                 C   s@   |  d}|dkrtdS |dkr4|  d tdS tdd S )NrI   r`   Ts   falsr   FzCould not read Boolean object)rJ   rT   r$   )r8   wordr.   r.   r/   rK      s    

zBooleanObject.read_from_streamc                 C   s   t dd t| S rP   )r   rT   rK   rR   r.   r.   r/   rQ      s    
zBooleanObject.readFromStream)r2   r>   r?   r   rY   objectr\   r]   rD   rO   r   r   rC   r=   rM   rS   rK   rQ   r.   r.   r.   r/   rT      s    	 
rT   c                   @   s   e Zd ZedddZeedeef ddddZ	eedeef dddd	Z
edeeedeee eeef f d d
ddZeeed dddZdS )ArrayObjectr)   c                 C   s   t | S )zS
        Emulate DictionaryObject.items for a list
        (index, object)
        )	enumerater,   r.   r.   r/   items   s    zArrayObject.itemsNr7   c                 C   s8   | d | D ]}| d ||| q| d d S )N   [    s    ])rG   r=   )r-   r8   r9   datar.   r.   r/   r=      s
    

zArrayObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM      s    
zArrayObject.writeToStreamr8   pdf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   rf   zCould not read array   ])rc   rJ   r$   isspaceseekappendread_object)r8   rj   rk   arrtmptokZ	peekaheadr.   r.   r/   rK      s    


zArrayObject.read_from_streamr8   rj   r*   c                 C   s   t dd t| |S rP   )r   rc   rK   r8   rj   r.   r.   r/   rQ      s    
zArrayObject.readFromStream)N)r2   r>   r?   r   re   r   r   rD   rC   r=   rM   rS   r   r   r   intrK   rQ   r.   r.   r.   r/   rc      s,    
   rc   c                   @   s   e Zd ZeeeddddZee dddZe	ddd	Z
eed
ddZeed
ddZeede	ef ddddZeede	ef ddddZeeed dddZeeed dddZdS )IndirectObjectN)idnum
generationrj   r*   c                 C   s   || _ || _|| _d S r:   )ry   rz   rj   )r-   ry   rz   rj   r.   r.   r/   rY      s    zIndirectObject.__init__r)   c                 C   s    | j | }|d krd S | S r:   )rj   r5   )r-   objr.   r.   r/   r5      s    zIndirectObject.get_objectc                 C   s"   d| j d| jdt| j dS )NzIndirectObject(z, ))ry   rz   idrj   r,   r.   r.   r/   rO     s    zIndirectObject.__repr__)otherr*   c                 C   s6   |d k	o4t |to4| j|jko4| j|jko4| j|jkS r:   )r[   rx   ry   rz   rj   r-   r~   r.   r.   r/   r]   	  s    


zIndirectObject.__eq__c                 C   s   |  | S r:   )r]   r   r.   r.   r/   __ne__  s    zIndirectObject.__ne__r7   c                 C   s"   | t| j d| j d d S )N z R)rG   r   ry   rz   r<   r.   r.   r/   r=     s    zIndirectObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM     s    
zIndirectObject.writeToStreamru   c                 C   s   d}|  d}|stt| r$q.||7 }qd}|  d}|sHtt| rX|sbq2qb||7 }q2t| }|dkrtdt|   tt	|t	||S )N    r      Rz0Error reading indirect object reference at byte )
rJ   r&   r#   rn   r   r$   r   tellrx   rw   )r8   rj   ry   rt   rz   rr.   r.   r/   rK      s,    



zIndirectObject.read_from_streamc                 C   s   t dd t| |S rP   )r   rx   rK   rv   r.   r.   r/   rQ   ;  s    
zIndirectObject.readFromStream)r2   r>   r?   rw   r   rY   r	   r'   r5   rD   rO   r\   r]   r   r   r   rC   r=   rM   rS   rK   rQ   r.   r.   r.   r/   rx      s(   	   rx   c                   @   s   e Zd Zdeeef ee d dddZedddZe	dd	d
Z
eedeef ddddZeedeef ddddZdS )FloatObject0N)rV   contextr*   c                 C   s   zt j| t||W S  tk
r|   zt j| t|W  Y S  t jk
rv   td| t t j| d Y  Y S X Y nX d S )NzInvalid FloatObject r   )	decimalDecimal__new__r   	ExceptionrD   InvalidOperationr   r2   )clsrV   r   r.   r.   r/   r   D  s    zFloatObject.__new__r)   c                 C   sN   | |   kr t| tdS | d}|rF|d dkrF|d d }q(|S d S )Nr   z.5frl   r   )to_integralrD   quantizer   r   )r-   or.   r.   r/   rO   R  s    zFloatObject.__repr__c                 C   s   t t| dS Nutf8)floatreprr+   r,   r.   r.   r/   
as_numeric]  s    zFloatObject.as_numericr7   c                 C   s   | t| d d S r   rG   r   r+   r<   r.   r.   r/   r=   `  s    zFloatObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM   e  s    
zFloatObject.writeToStream)r   N)r2   r>   r?   r   rD   r   r	   r   rO   r   r   r   rC   r=   rM   r.   r.   r.   r/   r   C  s"      
   r   c                   @   s   e Zd ZedZed dddZedddZ	e
edeef dd	d
dZe
edeef dd	ddZee
ed ef dddZee
ed ef dddZdS )NumberObjects	   [^+-.0-9]rU   c                 C   s<   t |}zt | |W S  tk
r6   t | d Y S X d S Nr   )rw   r   OverflowError)r   rV   valr.   r.   r/   r   o  s
    zNumberObject.__new__r)   c                 C   s   t t| dS r   )rw   r   r+   r,   r.   r.   r/   r   v  s    zNumberObject.as_numericNr7   c                 C   s   | t| d d S r   r   r<   r.   r.   r/   r=   y  s    zNumberObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM   ~  s    
zNumberObject.writeToStreamrH   c                 C   s*   t | tj}|ddkr"t|S t|S )N   .rl   )r   r   NumberPatternfindr   )r8   numr.   r.   r/   rK     s    zNumberObject.read_from_streamc                 C   s   t dd t| S rP   )r   r   rK   rR   r.   r.   r/   rQ     s    
zNumberObject.readFromStream)r2   r>   r?   recompiler   r   r   rw   r   r   r   rD   rC   r=   rM   rS   r   rK   rQ   r.   r.   r.   r/   r   l  s"   
  
r   )TextStringObjectByteStringObjectrH   c                 C   s   t ddd t| S )NreadHexStringFromStreamread_hex_string_from_stream4.0.0)r   r   rR   r.   r.   r/   r     s      r   )r8   rk   r*   c                 C   s   |  d d}d}t| }|s&tt|dkr0q^||7 }t|dkr|tt|dd7 }d}qt|dkrr|d7 }t|dkr|tt|dd7 }tt||S )	Nr    r      >      base   0)	rJ   r   r&   r#   lenchrrw   create_string_objectr   )r8   rk   txtxrt   r.   r.   r/   r     s"    
r   c                 C   s   t ddd t| |S )NreadStringFromStreamread_string_from_streamr   )r   r   )r8   rk   r.   r.   r/   r     s      r   c           	      C   sl  |  d}d}d}|  d}|s(tt|dkr<|d7 }n|dkrZ|d8 }|dkrXqbn|dkrX|  d}ddd	d
ddddddddddddddddd}z|| }W n tk
rV   | rtdD ]$}|  d}| r||7 }q qqttt|dd}nJ|dkr8|  d}|dkr2| 	dd d}nd|
d }t|t Y nX ||7 }qt||S ) Nr   r      (   )r      \   
      	      s   \c   /rg      %   <r   rf   rm      #   _   &   $)   n   r   t   b   f   cr   r   r   r   rg   r   r   r   rf   rm   r   r   r   r   r      r   s   
rl   zUnexpected escaped string: r   )rJ   r&   r#   KeyErrorisdigitranger   r   rw   ro   decoder   r2   r   )	r8   rk   rt   parensr   Zescape_dict_Zntokmsgr.   r.   r/   r     sl    










r   c                   @   sZ   e Zd ZdZeedddZeede	ef ddddZ
eede	ef ddd	d
ZdS )r   a=  
    Represents a string object where the text encoding could not be determined.
    This occurs quite often, as the PDF spec doesn't provide an alternate way to
    represent strings -- for example, the encryption data stored in files (like
    /O) is clearly not text, but is still stored in a "String" object.
    r)   c                 C   s   | S )z7For compatibility with TextStringObject.original_bytes.r.   r,   r.   r.   r/   original_bytes  s    zByteStringObject.original_bytesNr7   c                 C   sD   | }|rddl m} |||}|d |t| |d d S )Nr   RC4_encryptr   r   )	_securityr   rG   r   )r-   r8   r9   bytearrr   r.   r.   r/   r=     s    

z ByteStringObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM   !  s    
zByteStringObject.writeToStream)r2   r>   r?   __doc__propertyrC   r   r   r   rD   r=   rM   r.   r.   r.   r/   r     s     r   c                   @   sp   e Zd ZdZdZdZeedddZedddZ	e
edeef dd	d
dZe
edeef dd	ddZdS )r   z
    Represents a string object that has been decoded into a real unicode string.
    If read from a PDF document, this string appeared to match the
    PDFDocEncoding, or contained a UTF-16BE BOM mark to cause UTF-16 decoding to
    occur.
    Fr)   c                 C   s   |   S )a-  
        It is occasionally possible that a text string object gets created where
        a byte string object was expected due to the autodetection mechanism --
        if that occurs, this "original_bytes" property can be used to
        back-calculate what the original encoded bytes were.
        )get_original_bytesr,   r.   r.   r/   r   3  s    zTextStringObject.original_bytesc                 C   s0   | j rtj| d S | jr$t| S tdd S )Nutf-16bez#no information about original bytes)autodetect_utf16codecsBOM_UTF16_BEr+   autodetect_pdfdocencodingencode_pdfdocencodingr   r,   r.   r.   r/   r   =  s
    z#TextStringObject.get_original_bytesNr7   c                 C   s   zt | }W n$ tk
r0   tj| d }Y nX |rbddlm} |||}t|}||d  nX|	d |D ]>}t
| s|dkr|	td|  qp|	tt
| qp|	d d S )Nr   r   r   r   rg   z\%03or   )r   UnicodeEncodeErrorr   r   r+   r   r   r   r=   rG   r   isalnumr   )r-   r8   r9   r   r   r{   cr.   r.   r/   r=   J  s    

z TextStringObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM   g  s    
zTextStringObject.writeToStream)r2   r>   r?   r   r   r   r   rC   r   r   r   r   rD   r=   rM   r.   r.   r.   r/   r   (  s   	  r   c                   @   s~   e Zd ZedZdZeede	e
f ddddZeede	e
f ddddZeeed d	d
dZeeed d	ddZdS )
NameObjects   \s+|[\(\)<>\[\]{}/%]r   Nr7   c                 C   s   | t|  d S r:   )rG   r   r<   r.   r.   r/   r=   r  s    zNameObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM   w  s    
zNameObject.writeToStreamru   c              
   C   s   |  d}|tjkrtd|t| tjdd7 }z<z|d}W n" ttfk
rb   |d}Y nX t|W S  ttfk
r } z.|j	st
dt t| W Y S td|W 5 d }~X Y nX d S )Nr   zname read errorT)Z
ignore_eofzutf-8gbkz Illegal character in Name Object)rJ   r   surfixr$   r   delimiter_patternr   r   UnicodeDecodeErrorstrictr   r2   )r8   rj   namereter.   r.   r/   rK   }  s    



zNameObject.read_from_streamc                 C   s   t dd t| |S rP   )r   r   rK   rv   r.   r.   r/   rQ     s    
zNameObject.readFromStream)r2   r>   r?   r   r   r   r   r   r   rD   rC   r=   rM   rS   r   rK   rQ   r.   r.   r.   r/   r   n  s"   
   r   c                   @   s  e Zd Z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ee dddZe
ee dddZeedeef ddddZeedeef ddddZedeeedeee eeef f d dddZeeed dddZdS ) DictionaryObject)keyr*   c                 C   s   t | |S r:   )dict__getitem__r-   r   r.   r.   r/   raw_get  s    zDictionaryObject.raw_get)r   rV   r*   c                 C   s2   t |tstdt |ts$tdt| ||S Nzkey must be PdfObjectzvalue must be PdfObject)r[   r'   
ValueErrorr   __setitem__r-   r   rV   r.   r.   r/   r     s
    

zDictionaryObject.__setitem__Nc                 C   s2   t |tstdt |ts$tdt| ||S r   )r[   r'   r   r   
setdefaultr   r.   r.   r/   r     s
    

zDictionaryObject.setdefaultc                 C   s   t | | S r:   )r   r   r5   r   r.   r.   r/   r     s    zDictionaryObject.__getitem__r)   c                 C   sN   ddl m} | dd}|dkr$dS | }t||sJ||}|| td< |S )a  
        Retrieve XMP (Extensible Metadata Platform) data relevant to the
        this object, if available.

        Stability: Added in v1.12, will exist for all future v1.x releases.
        @return Returns a {@link #xmp.XmpInformation XmlInformation} 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   getr5   r[   r   )r-   r   metadatar.   r.   r/   xmp_metadata  s    
zDictionaryObject.xmp_metadatac                 C   s   t dd | jS )W
        .. deprecated:: 1.28.3

            Use :meth:`xmp_metadata` instead.
        getXmpMetadatar   r   r   r,   r.   r.   r/   r     s    
zDictionaryObject.getXmpMetadatac                 C   s   t dd | jS )r   xmpMetadatar   r   r,   r.   r.   r/   r     s    
zDictionaryObject.xmpMetadatar7   c                 C   sZ   | d t|  D ]4\}}||| | d ||| | d q| d d S )N   <<
rg   r      >>)rG   listre   r=   r-   r8   r9   r   rV   r.   r.   r/   r=     s    

z DictionaryObject.write_to_streamc                 C   s   t dd | || d S rL   rN   r<   r.   r.   r/   rM     s    
zDictionaryObject.writeToStreamri   c                    s  t t tt  tt d fdd tttd fdd}| d}|dkr^td	t|   d
i }t	| }|dkrvqbn|dkr| 
dd t|  qb|stt|dkr| d qD| 
dd t| |}t	| }| 
dd t| ||}||s|||< qbdt|   d| }	|d k	r6|jr6t|	qbt|	t qb|  }
t	| }|dkr| ddkr| d}|dkr| d}qx|dkrtd|dkr| ddkr| 
dd tj|krtd|tj }t|tr|  }||}| 
|d |  }| ||d< t	| }| d}|| dkr|  }
| 
dd | d }|dkr|d d d |d< n\|js| 
|d || ||d< |  }
n0| 
|
d td!t|   d"|d#|d$n| 
|
d d|krt|S t }|| |S d S )%N)pp1rem_gensrj   r*   c                    sb   |j |d  }|D ]$}||| kr| || k r|| }qt|dkrH|S  | ||dd  |S d S )Nr   r   )xrefr   )r  r  r  rj   lr   get_next_obj_posr.   r/   r
    s    
z;DictionaryObject.read_from_stream.<locals>.get_next_obj_posru   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 .	   )r   r  r  rJ   r   r$   ro   )r8   rj   Zeoncurrrwr  r	  r.   r/   read_unsized_from_steam  s    

zBDictionaryObject.read_from_stream.<locals>.read_unsized_from_steamr      <<zDictionary read error at byte z: stream must begin with '<<'    r   rl   r   r   z+Multiple definitions in dictionary at byte z	 for key    s   s   treamrg   )r   r   z)Stream data must be followed by a newliner   r   zStream length not definedr   __streamdata__r   r  ir  z7Unable to find 'endstream' marker after stream at byte z (nd='z', end='z').)rw   r   r   r   rC   rJ   r$   r   r   r   ro   r   r&   r#   rq   r   r   warningswarnr%   SALENGTHr[   rx   r5   StreamObjectinitialize_from_dictionaryr   update)r8   rj   rk   r  rs   rh   rt   r   rV   r   posseollengthtZpstartr   Zndstreamendretvalr.   r	  r/   rK     s       

















z!DictionaryObject.read_from_streamru   c                 C   s   t dd t| |S rP   )r   r   rK   rv   r.   r.   r/   rQ   l  s    
zDictionaryObject.readFromStream)N)N)r2   r>   r?   r   r   r   r	   r   r'   r   r   r   r   r   r   r   rD   rC   r=   rM   rS   r   r   rw   rK   rQ   r.   r.   r.   r/   r     s>   
    r   c                   @   s   e Zd Z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eddddZeddddZeddddZddddZddddZdS )
TreeObjectNr)   c                 C   s   t |  d S r:   )r   rY   r,   r.   r.   r/   rY   u  s    zTreeObject.__init__c                 C   s   t ddd |  S )NhasChildrenhas_childrenr   )r   r&  r,   r.   r.   r/   r%  x  s    zTreeObject.hasChildrenc                 C   s   d| kS )N/Firstr.   r,   r.   r.   r/   r&  |  s    zTreeObject.has_childrenc                 C   s   |   S r:   )childrenr,   r.   r.   r/   __iter__  s    zTreeObject.__iter__c                 c   s8   |   sd S | d }|V  || d kr*d S |d }qd S )Nr'  /Last/Next)r&  r-   childr.   r.   r/   r(    s    zTreeObject.children)r-  rj   r*   c                 C   s   t dd | || d S )NaddChild	add_child)r   r/  )r-   r-  rj   r.   r.   r/   r.    s    
zTreeObject.addChildc                 C   s   |  }||}t|ts td| krJ|| td< td| td< d }ntt| d }|| td< t| td d | td< |r||}t|tst||td< ||td< || }t|tst||td< d S )	Nr'  r   /Countr*  r   /Prevr+  /Parent)	r5   Zget_referencer[   rx   AssertionErrorr   r   r   r   )r-   r-  rj   	child_objprevprev_refZ
parent_refr.   r.   r/   r/    s*    
 

zTreeObject.add_child)r-  r*   c                 C   s   t dd | | d S )NremoveChildremove_child)r   r8  r,  r.   r.   r/   r7    s    
zTreeObject.removeChildc                 C   s(  |  }td|krtdn|td | kr6tdd}d }d }| td }|  }| td }|  }	|d k	r||kr|d krtd|kr|td }
|
  }|td= |
| td< | td	  d
8  < n@| td	 d
kst| td	= | td= td| kr| td= ntd|krf|td }
|
  }||td< |
|td< | td	  d
8  < n8||	kstt|td= || td< | td	  d
8  < d}q|}|}td|kr|td }|  }qjd }d }qj|std|td= td|kr|td= td|kr$|td= d S )Nr2  z/Removed child does not appear to be a tree itemz*Removed child is not a member of this treeFr'  r*  r+  r1  r0  r   Tz"Removal couldn't find item in tree)r5   r   r   r3  )r-   r-  r4  foundr6  r5  Zcur_refcurZlast_reflastZnext_refZnext_objr.   r.   r/   r8    sh    










zTreeObject.remove_childc                 C   s   t ddd |   d S )N	emptyTree
empty_treer   )r   r=  r,   r.   r.   r/   r<    s    zTreeObject.emptyTreec                 C   s   | D ]B}|  }|td= td|kr0|td= td|kr|td= qtd| kr^| td= td| krt| td= td| kr| td= d S )Nr2  r+  r1  r0  r'  r*  )r5   r   )r-   r-  r4  r.   r.   r/   r=    s    



zTreeObject.empty_tree)r2   r>   r?   rY   r\   r%  r&  r   r)  r	   r(  r.  r/  r7  r8  r<  r=  r.   r.   r.   r/   r$  t  s   Dr$  c                       s   e Zd ZddddZed fddZeed ddd	Zej	ddd
dd	Zee
dddZej	e
dd
ddZ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dddZddddZ  ZS )r  Nr)   c                 C   s   d | _ d | _d S r:   )_StreamObject__datadecoded_selfr,   r.   r.   r/   rY     s    zStreamObject.__init__c                    s   t   }|t| j7 }|S r:   )superr0   r   _datar-   rh   r1   r.   r/   r0     s    
zStreamObject.hash_value_dataDecodedStreamObjectc                 C   s   t dd | jS NdecodedSelfr?  r   r?  r,   r.   r.   r/   rF    s    
zStreamObject.decodedSelfrU   c                 C   s   t dd || _d S rE  rG  rX   r.   r.   r/   rF    s    
c                 C   s   | j S r:   r>  r,   r.   r.   r/   rA    s    zStreamObject._datac                 C   s
   || _ d S r:   rH  rX   r.   r.   r/   rA  "  s    r7   c                 C   sp   t t| j| ttj< t| || | tj= |d | j}|rXddl	m
} |||}|| |d d S )Ns   
stream
r   r   s
   
endstream)r   r   rA  r   r  r  r   r=   rG   r   r   )r-   r8   r9   rh   r   r.   r.   r/   r=   &  s    


zStreamObject.write_to_stream)EncodedStreamObjectrD  rh   r*   c                 C   s
   t | S r:   )r  r  )rh   r.   r.   r/   initializeFromDictionary5  s    z%StreamObject.initializeFromDictionaryc                 C   s>   t j| krt }nt }| d |_| d= | t j= ||  |S )Nr  )r  FILTERrI  rD  rA  r  r  )rh   r#  r.   r.   r/   r  ;  s    


z'StreamObject.initialize_from_dictionaryrI  c                 C   s   t dd |  S )NflateEncodeflate_encode)r   rN  r,   r.   r.   r/   rM  J  s    
zStreamObject.flateEncodec                 C   s   ddl m} tj| krb| tj }t|tr>|dttj	 qjt }|
td |
| |}ntd}t }||ttj< || j|_|S )Nr   )FlateDecoder   z/FlateDecode)filtersrO  r  rL  r[   rc   insertr   FTZFLATE_DECODErp   rI  r+   rA  )r-   rO  fZnewfr#  r.   r.   r/   rN  N  s    



zStreamObject.flate_encode)r2   r>   r?   rY   rC   r0   r   r	   rF  setterr   rA  r   r   rD   r=   rS   r   rK  r  rM  rN  __classcell__r.   r.   rC  r/   r  
  s0    

r  c                   @   sH   e Zd ZedddZeedddZedddZed	dd
dZd	S )rD  r)   c                 C   s   | j S r:   rA  r,   r.   r.   r/   get_datac  s    zDecodedStreamObject.get_datarJ  c                 C   s
   || _ d S r:   rV  rB  r.   r.   r/   set_dataf  s    zDecodedStreamObject.set_datac                 C   s   t dd | jS NgetDatarW  )r   rA  r,   r.   r.   r/   rZ  i  s    
zDecodedStreamObject.getDataNc                 C   s   t dd | | d S NsetDatarX  r   rX  rB  r.   r.   r/   r\  m  s    
zDecodedStreamObject.setData)r2   r>   r?   r   rW  rX  rZ  r\  r.   r.   r.   r/   rD  b  s   rD  c                   @   s   e Zd ZddddZeed dddZejeddd	dZe	de
ef dd
dZe	de
ef dddZeddddZeddddZdS )rI  Nr)   c                 C   s
   d | _ d S r:   )r?  r,   r.   r.   r/   rY   s  s    zEncodedStreamObject.__init__rD  c                 C   s   t dd | jS rE  rG  r,   r.   r.   r/   rF  v  s    
zEncodedStreamObject.decodedSelfrU   c                 C   s   t dd || _d S rE  rG  rX   r.   r.   r/   rF  {  s    
c                 C   sr   ddl m} | jd k	r | j S t }|| |_t|  D ]$\}}|tj	tj
tjfkr<|||< q<|| _|jS d S )Nr   )decode_stream_data)rP  r^  r?  rW  rD  rA  r  re   r  r  rL  ZDECODE_PARMS)r-   r^  decodedr   rV   r.   r.   r/   rW    s    



zEncodedStreamObject.get_datac                 C   s   t dd |  S rY  )r   rW  r,   r.   r.   r/   rZ    s    
zEncodedStreamObject.getDatarJ  c                 C   s   t dd S )Nz7Creating EncodedStreamObject is not currently supported)r$   rB  r.   r.   r/   rX    s    zEncodedStreamObject.set_datac                 C   s   t dd | |S r[  r]  rB  r.   r.   r/   r\    s    
zEncodedStreamObject.setData)r2   r>   r?   rY   r   r	   rF  rT  rD  r   rD   rC   rW  rZ  r   rX  r\  r.   r.   r.   r/   rI  r  s   rI  c                
   @   s   e Zd Zdeeedeee eeef f ddddZ	e
ddddZe
eeef ddd	Zeed
ddZejeeef ddddZdS )ContentStreamNri   c           	      C   s   || _ g | _| }t|trJd}|D ]}|t|  7 }q&t|}n$| }|d k	s^tt|}t|}|| _	| 
| d S )Nr   )rj   
operationsr5   r[   rc   r   rW  r   r3  rk   $_ContentStream__parse_content_stream)	r-   r8   rj   rk   rh   r  Zstream_bytesZstream_dataZstream_data_bytesr.   r.   r/   rY     s    

zContentStream.__init__rH   c                 C   s   | dd g }t|}|dks|dkr*q| dd | sF|dkrt|tjd}|dkr|g ksht| |}| j	|df q| j	||f g }q|d	kr|d
kr|
d}qq|	t|d | j qd S )Nr   r   rl   r   )   '   "T   BI   INLINE IMAGEr   r   r   )ro   r   isalphar   r   r   r3  _read_inline_imagera  rp   rJ   rq   rk   )r-   r8   operandspeekoperatoriir.   r.   r/   Z__parse_content_stream  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rj|d}||
 }d}|tkr0d}||7 }|d}q|dkrR|rR|dd qn|dd |
| q~|dd |
| q~|| dS )Nrl   r      I   r   s   IDi    zUnexpected end of stream   Er   FT   Q)settingsrh   )r   r   ro   rq   rj   rJ   r3  r   r$   r   rG   r   r   getvalue)r-   r8   rr  rt   r   rV   rs   rh   buflocZtok2Ztok3infoZhas_q_whitespacer.   r.   r/   ri    sN    








z ContentStream._read_inline_imager)   c                 C   s   t  }| jD ]\}}|dkrv|d t  }|d |d  || dd  |d ||d  |d n.|D ]}||d  |d	 qz|t| |d
 q| S )Nrf  re  rr  r   s   ID rh   s   EIrg   r   )r   ra  rG   r=   rs  r   )r-   newdatarj  rl  Zdicttextopr.   r.   r/   rA    s     

zContentStream._datarU   c                 C   s   |  tt| d S r:   )rb  r   r   rX   r.   r.   r/   rA  )  s    )N)r2   r>   r?   r   r   rD   r   r   rw   rY   r   rb  ri  r   rC   rA  rT  r.   r.   r.   r/   r`    s    <r`  ri   c                 C   sb  |  d}| dd t|}|dkr4t| |S |dkrt|  d}| dd |dkrht| ||S t| |S n|dkrt| ||S |dks|dkrt	| S |d	krt
| |S |d
krt| S |dkr|dk r|  d}t|dkrtdqt| }| dd t| ||S |  d}| t| d t|d k	rTt| |S t| S d S )Nr   rl   r   r   rw  r  ro  rI   r        rg  zFile ended unexpectedly.   )rJ   ro   ObjectPrefixr   r   rK   r   r   rc   rT   r   rE   r   r&   r   rq   IndirectPatternmatchrx   r   )r8   rj   rk   rt   idxrk  r.   r.   r/   rq   .  s@    










rq   c                   @   s$  e Zd ZdZeeeeef ddddZeee	e
f dddZeed d	d
dZeee	e
f dddZedddZee	dddZee	dddZee	dddZee	dddZe	dddZe	dddZe	dddZe	ddd Ze	dd!d"Ze	dd#d$Ze	dd%d&Ze	dd'd(Zeeejejf dd)d*Zej e!e ddd+d*Zeeejejf dd,d-Z"e"j e!e ddd.d-Z"eeejejf dd/d0Z#e#j e!e ddd1d0Z#eeejejf dd2d3Z$e$j e!e ddd4d3Z$eejejf dd5d6Z%eejejf dd7d8Z&eejejf dd9d:Z'eejejf dd;d<Z(eeef ddd=d>Z)eeef ddd?d@Z*eeef dddAdBZ+eeef dddCdDZ,eejddEdFZ-ejddGdHZ.eejddIdJZ/ejddKdLZ0eeejejf ddMdNZ1e1j eejejf dddOdNZ1eeejejf ddPdQZ2e2j eejejf dddRdQZ2eeejejf ddSdTZ3e3j eejejf dddUdTZ3eeejejf ddVdWZ4e4j eejejf dddXdWZ4dS )YRectangleObjecta  
    This class is used to represent *page boxes* in PyPDF2. These boxes include:
        * :attr:`artbox <PyPDF2._page.PageObject.artbox>`
        * :attr:`bleedbox <PyPDF2._page.PageObject.bleedbox>`
        * :attr:`cropbox <PyPDF2._page.PageObject.cropbox>`
        * :attr:`mediabox <PyPDF2._page.PageObject.mediabox>`
        * :attr:`trimbox <PyPDF2._page.PageObject.trimbox>`
    N)rr   r*   c                    s.   t |dkstt  fdd|D  d S )NrI   c                    s   g | ]}  |qS r.   _ensure_is_number.0r   r,   r.   r/   
<listcomp>l  s     z,RectangleObject.__init__.<locals>.<listcomp>)r   r3  rc   rY   )r-   rr   r.   r,   r/   rY   h  s    zRectangleObject.__init__rU   c                 C   s   t |ttfst|}|S r:   )r[   r   r   rX   r.   r.   r/   r  n  s    z!RectangleObject._ensure_is_number)sxsyr*   c                 C   s8   t t| j| t| j| t| j| t| j| fS r:   )r  r   leftbottomrighttop)r-   r  r  r.   r.   r/   scales  s    zRectangleObject.scalec                 C   s   t d | |S )NensureIsNumber)r   r  rX   r.   r.   r/   r  }  s    zRectangleObject.ensureIsNumberr)   c                 C   s   dt t|  dS )NzRectangleObject(r|   )r   r  r,   r.   r.   r/   rO     s    zRectangleObject.__repr__c                 C   s   | d S r   r.   r,   r.   r.   r/   r    s    zRectangleObject.leftc                 C   s   | d S )Nr   r.   r,   r.   r.   r/   r    s    zRectangleObject.bottomc                 C   s   | d S )Nr   r.   r,   r.   r.   r/   r    s    zRectangleObject.rightc                 C   s   | d S )Nro  r.   r,   r.   r.   r/   r    s    zRectangleObject.topc                 C   s   t dd | jS )NgetLowerLeft_xr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerLeft_xc                 C   s   t dd | jS )NgetLowerLeft_yr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerLeft_yc                 C   s   t dd | jS )NgetUpperRight_xr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperRight_xc                 C   s   t dd | jS )NgetUpperRight_yr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperRight_yc                 C   s   t dd | jS )NgetUpperLeft_xr  r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperLeft_xc                 C   s   t dd | jS )NgetUpperLeft_yr  r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperLeft_yc                 C   s   t dd | jS )NgetLowerRight_xr  r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerRight_xc                 C   s   t dd | jS )NgetLowerRight_yr  r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerRight_yc                 C   s   | j | jfS )zj
        Property to read and modify the lower left coordinate of this box
        in (x,y) form.
        )r  r  r,   r.   r.   r/   
lower_left  s    zRectangleObject.lower_leftc                    s"    fdd|D \ d<  d< d S )Nc                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   	<genexpr>  s     z-RectangleObject.lower_left.<locals>.<genexpr>r   r   r.   rX   r.   r,   r/   r    s    c                 C   s   | j | jfS )zk
        Property to read and modify the lower right coordinate of this box
        in (x,y) form.
        )r  r  r,   r.   r.   r/   lower_right  s    zRectangleObject.lower_rightc                    s"    fdd|D \ d<  d< d S )Nc                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z.RectangleObject.lower_right.<locals>.<genexpr>r   r   r.   rX   r.   r,   r/   r    s    c                 C   s   | j | jfS )zj
        Property to read and modify the upper left coordinate of this box
        in (x,y) form.
        )r  r  r,   r.   r.   r/   
upper_left  s    zRectangleObject.upper_leftc                    s"    fdd|D \ d<  d< d S )Nc                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z-RectangleObject.upper_left.<locals>.<genexpr>r   ro  r.   rX   r.   r,   r/   r    s    c                 C   s   | j | jfS )zk
        Property to read and modify the upper right coordinate of this box
        in (x,y) form.
        )r  r  r,   r.   r.   r/   upper_right  s    zRectangleObject.upper_rightc                    s"    fdd|D \ d<  d< d S )Nc                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z.RectangleObject.upper_right.<locals>.<genexpr>r   ro  r.   rX   r.   r,   r/   r    s    c                 C   s   t dd | jS )NgetLowerLeftr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerLeftc                 C   s   t dd | jS )NgetLowerRightr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getLowerRightc                 C   s   t dd | jS )NgetUpperLeftr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperLeftc                 C   s   t dd | jS )NgetUpperRightr  r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getUpperRightc                 C   s   t dd || _d S )NsetLowerLeftr  r  rX   r.   r.   r/   r    s    
zRectangleObject.setLowerLeftc                    s,   t dd  fdd|D \ d<  d< d S )NsetLowerRightr  c                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z0RectangleObject.setLowerRight.<locals>.<genexpr>r   r   r   rX   r.   r,   r/   r    s    
zRectangleObject.setLowerRightc                    s,   t dd  fdd|D \ d<  d< d S )NsetUpperLeftr  c                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z/RectangleObject.setUpperLeft.<locals>.<genexpr>r   ro  r  rX   r.   r,   r/   r    s    
zRectangleObject.setUpperLeftc                    s,   t dd  fdd|D \ d<  d< d S )NsetUpperRightr  c                 3   s   | ]}  |V  qd S r:   r  r  r,   r.   r/   r    s     z0RectangleObject.setUpperRight.<locals>.<genexpr>r   ro  r  rX   r.   r,   r/   r  
  s    
zRectangleObject.setUpperRightc                 C   s   | j | j S r:   )r  r  r,   r.   r.   r/   width  s    zRectangleObject.widthc                 C   s   t dd | jS )NgetWidthr  )r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getWidthc                 C   s   | j | j S r:   )r  r  r,   r.   r.   r/   height  s    zRectangleObject.heightc                 C   s   t dd | jS )N	getHeightr  )r   r  r,   r.   r.   r/   r    s    
zRectangleObject.getHeightc                 C   s   t dd | jS N	lowerLeftr  r  r,   r.   r.   r/   r    s    
zRectangleObject.lowerLeftc                 C   s   t dd || _d S r  r  rX   r.   r.   r/   r  #  s    
c                 C   s   t dd | jS N
lowerRightr  r  r,   r.   r.   r/   r  *  s    
zRectangleObject.lowerRightc                 C   s   t dd || _d S r  r  rX   r.   r.   r/   r  /  s    
c                 C   s   t dd | jS N	upperLeftr  r  r,   r.   r.   r/   r  6  s    
zRectangleObject.upperLeftc                 C   s   t dd || _d S r  r  rX   r.   r.   r/   r  ;  s    
c                 C   s   t dd | jS N
upperRightr  r  r,   r.   r.   r/   r  B  s    
zRectangleObject.upperRightc                 C   s   t dd || _d S r  r  rX   r.   r.   r/   r  G  s    
)5r2   r>   r?   r   r
   r   rY   r   r   r   r   r  r  r  rD   rO   r   r  r  r  r  r  r  r  r  r  r  r  r  r   r   r  rT  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  ^  s   	
r  c                   @   sr  e Zd ZdZe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	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()<PyPDF2.PdfReader.get_fields>`
    NrJ  c              	   C   sR   t |  t t  }|D ].}z|| | t|< W q tk
rJ   Y qX qd S r:   )r   rY   r   
attributesr   r   r   )r-   rh   Zfield_attributesattrr.   r.   r/   rY   W  s    
zField.__init__r)   c                 C   s   |  tjS )z4Read-only property accessing the type of this field.)r   r   rR  r,   r.   r.   r/   
field_typed  s    zField.field_typec                 C   s   t dd | jS )zX
        .. deprecated:: 1.28.3

            Use :py:attr:`field_type` instead.
        	fieldTyper  )r   r  r,   r.   r.   r/   r  i  s    
zField.fieldTypec                 C   s   |  tjS )z6Read-only property accessing the parent of this field.)r   r   ZParentr,   r.   r.   r/   parents  s    zField.parentc                 C   s   |  tjS )z4Read-only property accessing the kids of this field.)r   r   ZKidsr,   r.   r.   r/   kidsx  s    z
Field.kidsc                 C   s   |  tjS )z4Read-only property accessing the name of this field.)r   r   Tr,   r.   r.   r/   r   }  s    z
Field.namec                 C   s   |  tjS )z>Read-only property accessing the alternate name of this field.)r   r   ZTUr,   r.   r.   r/   alternate_name  s    zField.alternate_namec                 C   s   t dd | jS )z\
        .. deprecated:: 1.28.3

            Use :py:attr:`alternate_name` instead.
        altNamer  )r   r  r,   r.   r.   r/   r    s    
zField.altNamec                 C   s   |  tjS )z
        Read-only property accessing the mapping name of this field. This
        name is used by PyPDF2 as a key in the dictionary returned by
        :meth:`get_fields()<PyPDF2.PdfReader.get_fields>`
        )r   r   ZTMr,   r.   r.   r/   mapping_name  s    zField.mapping_namec                 C   s   t dd | jS )zZ
        .. deprecated:: 1.28.3

            Use :py:attr:`mapping_name` instead.
        mappingNamer  )r   r  r,   r.   r.   r/   r    s    
zField.mappingNamec                 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Ffr,   r.   r.   r/   flags  s    zField.flagsc                 C   s   |  tjS )zr
        Read-only property accessing the value of this field. Format
        varies based on field type.
        )r   r   Vr,   r.   r.   r/   rV     s    zField.valuec                 C   s   |  tjS )z=Read-only property accessing the default value of this field.)r   r   ZDVr,   r.   r.   r/   default_value  s    zField.default_valuec                 C   s   t dd | jS )z[
        .. deprecated:: 1.28.3

            Use :py:attr:`default_value` instead.
        defaultValuer  )r   r  r,   r.   r.   r/   r    s    
zField.defaultValuec                 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AAr,   r.   r.   r/   additional_actions  s    zField.additional_actionsc                 C   s   t dd | jS )z`
        .. deprecated:: 1.28.3

            Use :py:attr:`additional_actions` instead.
        additionalActionsr  )r   r  r,   r.   r.   r/   r    s    
zField.additionalActions)r2   r>   r?   r   r   rD   r   rY   r   r	   r   r  r  r   r  rc   r  r   r  r  r  r  rw   r  rV   r  r  r  r  r.   r.   r.   r/   r  O  s@   				r  c                   @   s   e Zd ZdZdZdZdS )OutlineFontFlagzK
    A class used as an enumerable flag for formatting an outline font
    r   r   N)r2   r>   r?   r   italicboldr.   r.   r.   r/   r    s   r  c                   @   sh  e Zd ZdZeeeeee	f eeef e
ddddZeedddZedd	d
Z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e 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.

    :param str title: Title of this destination.
    :param IndirectObject page: Reference to the page of this destination. Should
        be an instance of :class:`IndirectObject<PyPDF2.generic.IndirectObject>`.
    :param str typ: How the destination is displayed.
    :param args: Additional arguments may be necessary depending on the type.
    :raises PdfReadError: If destination type is invalid.

    .. list-table:: Valid ``typ`` arguments (see PDF spec for details)
       :widths: 50 50

       * - /Fit
         - No additional arguments
       * - /XYZ
         - [left] [top] [zoomFactor]
       * - /FitH
         - [top]
       * - /FitV
         - [left]
       * - /FitR
         - [left] [bottom] [right] [top]
       * - /FitB
         - No additional arguments
       * - /FitBH
         - [top]
       * - /FitBV
         - [left]
    N)titlepagetypargsr*   c                 G   sb  t |  || td< || td< || td< |dkr`|\| ttj< | ttj< | td<  n|tjkr|\| ttj< | ttj< | ttj	< | ttj< n|tj
tjfkrz|\| ttj< W n$ tk
r   t | ttj< Y nX nr|tjtjfkr<z|\| ttj< W n& tk
r8   t | ttj< Y nX n"|tjtjfkrPntd|d S )N/Title/Page/Typez/XYZ/ZoomzUnknown Destination Type: )r   rY   r   TALEFTZTOPTFZFIT_RZBOTTOMRIGHTZFIT_HZFIT_BHr   rE   ZFIT_VZFIT_BVZFITZFIT_Br$   )r-   r  r  r  r  r.   r.   r/   rY     s@    


zDestination.__init__r)   c                    s(   t  d d g fdddD  S )Nr  r  c                    s   g | ]}| kr | qS r.   r.   r  r,   r.   r/   r  .  s   z*Destination.dest_array.<locals>.<listcomp>)/Left/Bottom/Right/Topr  )rc   r   r,   r.   r,   r/   
dest_array*  s    
zDestination.dest_arrayc                 C   s   t dd | jS )zX
        .. deprecated:: 1.28.3

            Use :py:attr:`dest_array` instead.
        getDestArrayr  )r   r  r,   r.   r.   r/   r  5  s    
zDestination.getDestArrayr7   c                 C   s   | d td}||| | d | j}||| td}||| | d td}||| | d | d d S )Nr   z/Drg   z/Sz/GoTor   r  )rG   r   r=   r  )r-   r8   r9   r   rV   Zvalue_sr.   r.   r/   r=   >  s    



zDestination.write_to_streamc                 C   s
   |  dS )z3Read-only property accessing the destination title.r  r   r,   r.   r.   r/   r  Q  s    zDestination.titlec                 C   s
   |  dS )z9Read-only property accessing the destination page number.r  r  r,   r.   r.   r/   r  V  s    zDestination.pagec                 C   s
   |  dS )z2Read-only property accessing the destination type.r  r  r,   r.   r.   r/   r  [  s    zDestination.typc                 C   s   |  ddS )z-Read-only property accessing the zoom factor.r  Nr  r,   r.   r.   r/   zoom`  s    zDestination.zoomc                 C   s   |  ddS )z<Read-only property accessing the left horizontal coordinate.r  Nr  r,   r.   r.   r/   r  e  s    zDestination.leftc                 C   s   |  ddS )z=Read-only property accessing the right horizontal coordinate.r  Nr  r,   r.   r.   r/   r  j  s    zDestination.rightc                 C   s   |  ddS )z9Read-only property accessing the top vertical coordinate.r  Nr  r,   r.   r.   r/   r  o  s    zDestination.topc                 C   s   |  ddS )z<Read-only property accessing the bottom vertical coordinate.r  Nr  r,   r.   r.   r/   r  t  s    zDestination.bottomc                 C   s"   |  dttdtdtdgS )zGRead-only property accessing the color in (R, G, B) with values 0.0-1.0/Cr   )r   rc   r   r,   r.   r.   r/   colory  s     zDestination.colorc                 C   s   |  ddS )zDRead-only property accessing the font type. 1=italic, 2=bold, 3=both/Fr   r  r,   r.   r.   r/   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 descendents at all levels
        r0  Nr  r,   r.   r.   r/   outline_count  s    zDestination.outline_count)"r2   r>   r?   r   rD   r   r   rx   rE   r   r   rY   r   rc   r  r  r   rC   r=   r	   r  rw   r  r  r  r   r  r  r  r  r  r  r  r  r.   r.   r.   r/   r    sH   "
)

 r  c                   @   s(   e Zd Zeedeef ddddZdS )BookmarkNr7   c                    s   | d  fdddD D ]:}||| | d  |}||| | d qtd}||| | d  j}||| | d | d d S )	Nr   c                    s   g | ]}| krt |qS r.   )r   r  r,   r.   r/   r    s   z,Bookmark.write_to_stream.<locals>.<listcomp>)r  r2  r'  r*  r+  r1  rg   r   z/Destr  )rG   r=   r   r   r  r  r.   r,   r/   r=     s     






zBookmark.write_to_stream)r2   r>   r?   r   r   rD   rC   r=   r.   r.   r.   r/   r    s    r  )stringrk   r*   c                 C   s   t ddd t| |S )NcreateStringObjectr   r   )r   r   )r  rk   r.   r.   r/   r    s    r  c              	   C   s  t | trt| S t | trt |ttfr|d}| D ]>}z||| 7 }W q4 tk
rp   |t|fd7 }Y q4X q4t|S t |tr|dkrt| S t| |S zB| 	t
jrt| d}d|_|W S tt| }d|_|W S W n tk
r   t|  Y S X ntddS )z
    Create a ByteStringObject or a TextStringObject from a string to represent the string.

    :param string: A string

    :raises TypeError: If string is not of type str or bytes.
    r   charmaprC   zutf-16Tz3create_string_object should have str or unicode argN)r[   rD   r   rC   r  r   r   r   r   
startswithr   r   r   decode_pdfdocencodingr   r   	TypeError)r  rk   outr   r#  r.   r.   r/   r     s4    


r   )
action_refr  r  r  r  r*   c                 C   s   t  }|td| tdt|i |d k	rL|tdtdd |D i d}|r\|d7 }|rh|d7 }|r|td	t|i |S )
Nz/Ar  r  c                 S   s   g | ]}t |qS r.   r   )r  r   r.   r.   r/   r    s     z$_create_bookmark.<locals>.<listcomp>r   r   r   r  )r$  r  r   r   rc   r   )r  r  r  r  r  bookmarkZformat_flagr.   r.   r/   _create_bookmark  s(      r  )unicode_stringr*   c              
   C   sP   d}| D ]B}z|t tt| 7 }W q tk
rH   td|dddY qX q|S )Nr   pdfdocencodingrl   #does not exist in translation table)r   r   r   r   r   )r  r#  r   r.   r.   r/   r     s        r   )
byte_arrayr*   c                 C   s>   d}| D ]0}t | }|dkr0tdt|ddd||7 }q|S )Nr    r  rl   r  )r   r   	bytearray)r  r#  br   r.   r.   r/   r  	  s    
r  rU   c                    s   t  fdddD S )Nc                 3   s(   | ] }t  ||d   dd V  qdS )r   r   g     o@N)rw   )r  irW   r.   r/   r    s     zhex_to_rgb.<locals>.<genexpr>)r   r   rI   )tuplerW   r.   rW   r/   
hex_to_rgb  s    r  c                   @   sx   e Zd Zedeeeeeef eeeeeeeed
ddZ	edeeef eeef eeeeef eeed
ddZ
dS )AnnotationBuilder	HelveticaF14pt000000ffffff)
textrectfontr  r  	font_size
font_colorborder_colorbackground_colorr*   c	                 C   s   d}	|dkr|	d }	|dkr$|	d }	|	| d | }	|	d | }	d}
t |D ]}|
t| d }
qL|
d }
t }|td	td
tdtdtdt|tdt| tdt|	tdt|
tdtdd t |D i |S )z"Add text in a rectangle to a page.zfont: Tzbold zitalic r   z;text-align:left;color:#r   Zrgr  /Annot/Subtypez	/FreeText/Rect	/Contentsz/DSz/DAr  c                 S   s   g | ]}t |qS r.   r  )r  nr.   r.   r/   r  E  s     z/AnnotationBuilder.free_text.<locals>.<listcomp>)r  rD   r   r  r   r  r   rc   )r  r  r  r  r  r  r  r  r	  Zfont_strZbg_color_strst	free_textr.   r.   r/   r    s@           zAnnotationBuilder.free_textr   )r  p2r  r  	title_barr*   c                 C   s   t tdtdtdtdtdt|tdt|tdtt| d t| d	 t|d t|d	 gtd
ttdtdgtdttdtdtdgtdt|i}|S )aU  
        Draw a line on the PDF.

        :param p1: First point
        :param p2: Second point
        :param rect: Rectangle
        :param text: Text to be displayed as the line annotation
        :param title_bar: Text to be displayed in the title bar of the
            annotation; by convention this is the name of the author
        r  r
  r  z/Liner  z/Tz/Lr   r   z/LENz/ICg      ?r  )r   r   r  r   rc   r   )r  r  r  r  r  Zline_objr.   r.   r/   lineK  sF         



   zAnnotationBuilder.lineN)r  FFr   r  r  r  )r   r   )r2   r>   r?   rS   rD   r
   r   r\   r   r  r  r.   r.   r.   r/   r    s>          ,  

r  )N)N)N)N)N)N)hr   
__author____author_email__r   r   r@   loggingr   r  enumr   ior   typingr   r   r   r   r   r	   r
   r   r   _codecsr   r   r   _utilsr   r   r   r   r   r   r   r   r   r   r   r   	constantsr   r   r   rR  r    r  r!   r  r"   r  errorsr#   r$   r%   r&   	getLoggerr2   loggerr}  ZNumberSignsr   r~  r'   rE   rT   r  rc   rx   r   r   rw   r   r   rD   r   r   r   rC   r   r   r   r   r   r$  r  rD  rI  r`  rq   r  r  r  r  r  r  r   r   r\   r  r   r  r  r  r.   r.   r.   r/   <module>   s   ,8

.8I)(   J F, [ X+  0 r 		 1 


 

/