o
    jm                    @   s  d Z dZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZ dd	lmZmZmZ dd
lmZmZmZmZmZmZmZmZmZ ddl m!Z!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,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZB ddlmCZCmDZD ejEdkrddlmFZF nddlGmFZF eHeIZJeKdZLG dd deMe e8ZNG dd deOeef e8ZPG dd dePZQdeddfd d!ZRG d"d# d#ePZSG d$d% d%eSZTG d&d' d'eSZUd(ZVG d)d* d*eTZW	d4d+ed,ee d-edeXeMeX eOeYeXf f dee8eYeXeWf fd.d/ZZG d0d1 d1eQZ[G d2d3 d3eQZ\dS )5zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)IterableSequence)BytesIO)ceil)AnyCallableOptionalUnioncast   )PdfReaderProtocolPdfWriterProtocolXmpInformationProtocol)	WHITESPACESBinaryStreamType
StreamTypedeprecation_no_replacementlogger_warningread_non_whitespaceread_until_regexread_until_whitespaceskip_over_comment)CheckboxRadioButtonAttributesFieldDictionaryAttributesOutlineFontFlagStreamAttributes)FilterTypes)TypArguments)TypFitArguments)STREAM_TRUNCATED_PREMATURELYLimitReachedErrorPdfReadErrorPdfStreamError   )
BooleanObjectByteStringObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObject	PdfObjectTextStringObjectis_null_or_none)Fit)extract_inline__ascii85_decode extract_inline__ascii_hex_decodeextract_inline__dct_decode!extract_inline__run_length_decodeextract_inline_default)read_hex_string_from_streamread_string_from_stream)      )Selfs   [+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]c                   @   s  e Zd Zdedd fddZ		d!dededeeee	e
f   dd fd	d
Zde
fddZdee fddZdedee fddZdedd fddZdedefddZdedefddZ	d"dedede	ef ddfddZe	d"dedee dede	ee	 ee
e	f f dd fdd ZdS )#ArrayObjectpdf_destreturnc                 C   sJ   t d| t |d}| D ]}t|dr||| q|| q|S )Nr9   F	replicate)r
   _reference_cloner9   hasattrappendr<   )selfr:   arrdata rC   d/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/pypdf/generic/_data_structures.pyr<   e   s   
zArrayObject.replicateFrC   force_duplicateignore_fieldsc                 C   s   z| j j|kr|s| W S W n	 ty   Y nw td| jt |dd}| D ]Q}t|tr?|||||||}|	|j  q%t|t
rat|  }tra||j|d|d||}|	|j  q%t|drq|	|||| q%|	| q%|S )Clone object into pdf_dest.r9   T)rE   )rE   rF   clone)indirect_referencepdf	Exceptionr
   r=   r9   
isinstanceStreamObjectrH   r?   r'   
get_objectr>   )r@   r:   rE   rF   rA   rB   dupresolvedrC   rC   rD   rH   t   s<   

zArrayObject.clonec                 C   s   t | jtdd | D fS )q
        Used to detect modified object.

        Returns:
            Hash considering type and value.

        c                 s   s    | ]}|  V  qd S Nhash_bin.0xrC   rC   rD   	<genexpr>   s    z'ArrayObject.hash_bin.<locals>.<genexpr>)hash	__class__tupler@   rC   rC   rD   rT      s   zArrayObject.hash_binc                 C   s   t | S )z:Emulate DictionaryObject.items for a list (index, object).)	enumerater\   rC   rC   rD   items   s   zArrayObject.itemslstc                 C   s   t |tttfrt|}|S t |tr|g}|S t |tr1|d dkr*t|g}|S t|g}|S t |tr=t	|g}|S |g}|S )Nr   /)
rL   listr[   setr+   strr(   r,   bytesr%   )r@   r_   resultrC   rC   rD   _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

        )r9   extendrf   )r@   r_   temprC   rC   rD   __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

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

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

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

        Nr   r#   c                 3   s    | ]@}| vo@|v o@t |to@t | to@d ddu p@tt| d ddu p@d dtt| d dkV  qdS )/TypeN)rL   raw_getr'   r   getr
   )rV   fieldrF   r   rC   rD   rX   Y  s"    

z*DictionaryObject._clone.<locals>.<genexpr>)/Next/Prev/N/V))r   r   )r   r   r   r   rI   rH   )ra   r   rL   r   r
   anyr   r'   r   r   r=   rZ   rI   idnum
generationaddr?   r(   rK   r   r^   rM   r>   rH   )r@   r   r:   rE   rF   r   rW   r_   r   objscur_objprev_objclonr   r   scr   vvrC   r   rD   r   9  s   	







3


zDictionaryObject._clonec                 C   s    t | jtdd |  D fS )rQ   c                 s   s     | ]\}}||  fV  qd S rR   rS   )rV   r   r   rC   rC   rD   rX     s    z,DictionaryObject.hash_bin.<locals>.<genexpr>)rY   rZ   r[   r^   r\   rC   rC   rD   rT     s   zDictionaryObject.hash_binkeyc                 C   s   t | |S rR   )r   __getitem__r@   r   rC   rC   rD   r     s   zDictionaryObject.raw_getdefaultc                 C   sT   || v r| | S zd| vr|W S t d t y)   td| d  || Y S w )a>  
        Returns the value of a key or from the parent if not found.
        If not found returns default.

        Args:
            key: string identifying the field to return

            default: default value to return

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

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

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

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

        r   )XmpInformationz	/MetadataNmypy)xmpr   r   r-   rN   )r@   r   metadatarC   rC   rD   xmp_metadata  s   zDictionaryObject.xmp_metadatarn   ro   c                 C   s   |d ur	t dd |d |  D ],\}}t|dkr)|d dkr)|d dkr)q||| |d || |d	 q|d
 d S )Nrp   rq      <<
r   r#   %ry   rs      
   >>)r   rt   r^   r   ru   )r@   rn   ro   r   r   rC   rC   rD   ru     s   
$

z DictionaryObject.write_to_streamposition_beforeposition_endgenerationsrJ   c           	         sH   }|D ]}|j | } fdd| D }|r!t|g|R  }q|S )Nc                    s(   g | ]} |  k rkrn n|qS rC   rC   rU   r   r   rC   rD   
<listcomp>  s   ( z>DictionaryObject._get_next_object_position.<locals>.<listcomp>)xrefvaluesmin)	clsr   r   r   rJ   outr   locationr   rC   r   rD   _get_next_object_position  s   
z*DictionaryObject._get_next_object_positionc                 C   s~   | j | dt|j|dd }| }|||  }|d}|dk r.td| d||| d  |d |d  S )	Nl        )r   r   r   rJ   r#   	   endstreamr   z6Unable to find 'endstream' marker for obj starting at .	   )r   tellra   r   r{   findr!   r}   )r   rn   rJ   object_positioncurrent_position
read_valueendstream_positionrC   rC   rD   _read_unsized_from_stream  s   

z*DictionaryObject._read_unsized_from_streamrv   c              
   C   s  |  d}|dkrtdt|   di }	 t| }|dkr!q|dkr0| dd	 t|  q|s6tt|d
kr@|  d	 n| dd	 zPzt	| |}t
|trUW W nt
|tsatd|W n" ty } z|d urr|jrr t| t W Y d }~W qd }~ww t| }| dd	 t	| ||}W nC ttfy } zt| d }~w ty } z$|d ur|jrt| t| t t }	|	| |	W  Y d }~S d }~ww ||s|||< ndt|   d| }
|d ur|jrt|
t|
t q|  }t| }|dkr(|  ddkr(|  d	}|dkr*|  d	}|dks |dvr3td|dkrF|  d	dkrF| dd	 tj|vrl|d urY|jrYtdtd|   t td|ttj< |tj }t
|tr|  }|d usJ d||}| |d |d u rd}|  }|dkrddlm} ||krtd| d|  ||d< n
t| t d|d< t| }|  d}|| dkr'|  }| d d	 |  d!}|dkr|d d d |d< n;|d ur|js| |d t!| ||d< |  }n| |d td"t|   d#|d$|d%n| |d d|v r8t"#|S t }	|	| |	S )&Nr      <<zDictionary read error at byte z: stream must begin with '<<'T    rx   ry   r#      >z)Expecting a NameObject for key but found z+Multiple definitions in dictionary at byte z	 for key    s   s   treamrs   )r      z)Stream data must be followed by a newliner   r   zStream length not definedzStream length not defined @pos=r   r   )MAX_DECLARED_STREAM_LENGTHzDeclared stream length of z  exceeds maximum allowed length.__streamdata__r      ir   z7Unable to find 'endstream' marker after stream at byte z (nd='z', end='z').)$r{   r!   hexr   r   r}   r   r"   r   r~   rL   r)   r(   strictr   __repr__r   RecursionErrorr    rK   r   updater   r   LENGTHr*   r'   rN   filtersr   r   recompiler   rM   initialize_from_dictionary)rn   rJ   rv   r   rB   r   r   excr   retvalmsgposr   eollengthtpstartr   endstreamendrC   rC   rD   r   *  s   









6


















z!DictionaryObject.read_from_streamr   rR   )&r   r   r   r   r<   r   r   r   r	   rc   r   rH   rb   r[   r   rT   r   r   r   r   r   r+   r   propertyr   r   r   rd   ru   classmethodra   r   r   r   r   r   r   r   rC   rC   rC   rD   r     s    


q
r   c                   @   s  e Zd Zd'dee ddfddZdefddZdefdd	Z	de
e fd
dZdededdfddZdeded f deddfddZdeded f deddfddZ	d'dedededeedef  def
ddZdededededdf
dd Zdeddfd!d"Zd(d#d$Zd(d%d&ZdS ))
TreeObjectNdctr;   c                 C   s    t |  |r| | d S d S rR   )r   __init__r   )r@   r   rC   rC   rD   r     s   
zTreeObject.__init__c                 C   s   d| v S )N/FirstrC   r\   rC   rC   rD   has_children     zTreeObject.has_childrenc                 C   s   |   S rR   )childrenr\   rC   rC   rD   __iter__  r   zTreeObject.__iter__c                 c   s    |   sd S | td }| td }| }t }	 t|}||v r-td| t d S || |V  ||kr;d S |td}t	|rHd S | }q)Nr   /LastTz(Detected cycle in outline structure for r   )
r   r(   rN   rb   idr   r   r   r   r-   )r@   	child_reflastchildr   child_idrC   rC   rD   r     s*   
zTreeObject.childrenr  rJ   c                 C   s   |  |d | d S rR   )insert_child)r@   r  rJ   rC   rC   rD   	add_child  s   zTreeObject.add_childparentnc                 C   sv   t |rd S |d usJ dtd| }d|v r9ttdtt|td | |td< | |dd | d S d S )Nr   r   /Countr   r   )	r-   r
   rN   r*   maxr   r(   inc_parent_counter_defaultr   )r@   r  r  rC   rC   rD   r
    s   z%TreeObject.inc_parent_counter_defaultc                 C   s   t |rd S |d usJ dtd| }|dddk}tt|dd}|dk r.t|}t|| |r6dnd |td< |sBd S | |d	d | d S )
Nr   r   z
/%is_open%Tr  r   r#   ry   r   )	r-   r
   rN   r   r   absr*   r(   inc_parent_counter_outline)r@   r  r  opnr   rC   rC   rD   r    s    z%TreeObject.inc_parent_counter_outlinebeforeinc_parent_counter.c                 C   s  |d u r| j }| }|jd usJ d|j}d| vrO|| td< td| td< || td< | j|td< || |dd d|v rF|d= d	|v rM|d	= |S td
| d }|j|krd|v rgtd|d }n/td||td< |j|td	< | j|td< d|v r|d= || td< || |dd |S |j|ks[zt|d	 tsJ ||d	 td< |d	 |td	< W n t	y   |d= Y nw ||td< ||td	< | j|td< || |dd |S )Nr   r   r   r  r   r   r#   r   r   r   r   )
r
  rN   rI   r(   r*   r   r
   rL   r   rK   )r@   r  r  rJ   r  	child_objchild_referenceprevrC   rC   rD   r    sR   


zTreeObject.insert_childr  prev_refcurr  c                 C   s   | tdd}|du rI|r-| }|td= || td< t| td d | td< dS td| td< | td= td| v rG| td= dS dS |r\| }||td< ||td< n||ksbJ |td= || td< t| td d | td< dS )	z
        Adjust the pointers of the linked list and tree node count.

        Args:
            prev:
            prev_ref:
            cur:
            last:

        r   Nr   r   r  r#   r   r   )r   r(   rN   r*   )r@   r  r  r  r  next_refnext_objrC   rC   rD   _remove_node_from_tree.  s,   


 z!TreeObject._remove_node_from_treec           
      C   s   |  }|j}td|vrtd|td | krtdd}d }d }| td }|  }| td }|  }	|d urg||krJ| ||||	 d}n|}|}td|v r_|td }|  }nd }d }|d us;|smtd	t| d S )
Nr   /Removed child does not appear to be a tree itemz*Removed child is not a member of this treeFr   r   Tr   z"Removal couldn't find item in tree)rN   rI   r(   rl   r  _reset_node_tree_relationship)
r@   r  r  foundr  r  cur_refr  last_refr  rC   rC   rD   remove_childY  s:   
zTreeObject.remove_childc                 C   s,   t d| vr
tdtd| d |  dS )z)Remove the object from the tree it is in.r   r  r   N)r(   rl   r
   r  r\   rC   rC   rD   remove_from_tree~  s   zTreeObject.remove_from_treec                 C   sd   | D ]
}|  }t| qtd| v r| td= td| v r#| td= td| v r0| td= d S d S )Nr  r   r   )rN   r  r(   )r@   r  r  rC   rC   rD   
empty_tree  s   


zTreeObject.empty_treerR   r;   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	   r'   r   r
  r  r   r  r  r  r  r  rC   rC   rC   rD   r     s\    


3
+
%r   r  r;   c                 C   s>   | t d= t d| v r| t d= t d| v r| t d= dS dS )z
    Call this after a node has been removed from a tree.

    This resets the nodes attributes in respect to that tree.

    Args:
        child_obj:

    r   r   r   N)r(   )r  rC   rC   rD   r    s   

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

        Args:
            src:
            pdf_dest:
            force_duplicate:
            ignore_fields:

        rM   Nr$  )r
   r"  r#  rH   rK   superr   )r@   r   r:   rE   rF   r   r#  rZ   rC   rD   r     s   zStreamObject._clonec                    s   t t  | jfS )rQ   )rY   r'  rT   r"  r\   r(  rC   rD   rT     s   	zStreamObject.hash_binc                 C   s   | j S rR   r"  r\   rC   rC   rD   get_data  s   zStreamObject.get_datarB   c                 C   s
   || _ d S rR   r)  r@   rB   rC   rC   rD   set_data     
zStreamObject.set_datac                    s   t   }||  7 }|S rR   )r'  hash_value_datar*  r+  r(  rC   rD   r.    s   
zStreamObject.hash_value_datarn   ro   c                 C   sb   |d ur	t dd tt| j| ttj< t| | | tj= |	d |	| j |	d d S )Nrp   rq   s   
stream
s
   
endstream)
r   r*   r   r"  r(   r   r   r   ru   rt   r@   rn   ro   rC   rC   rD   ru     s   
zStreamObject.write_to_stream)EncodedStreamObjectr$  c                 C   sH   t j| v r	t }nt }| d |_| d= t j| v r| t j= ||  |S )Nr   )r   FILTERr0  r$  r"  r   r   )rB   r   rC   rC   rD   r     s   



z'StreamObject.initialize_from_dictionaryry   levelr0  c              	   C   s  ddl m} tj| v r]| tj }t|trFtttjg|}ztt	 g| 
tjt }W n4 tyE   tt	 | 
tjt g}Y nw tttj|g}tt	 | 
tjt	 g}nttj}d }t }||  ||ttj< |d ur~||ttj< || j||_|S )Nr   FlateDecode)r   r4  r   r1  rL   r9   r(   FTFLATE_DECODEr)   r   DECODE_PARMS	TypeErrorr0  r   encoder"  )r@   r2  r4  fparamsr   rC   rC   rD   flate_encode  s6   




zStreamObject.flate_encodepillow_parametersc                 C   sx   ddl m} | dddkr,z| j d}W n ty&   |   d}Y nw t|t || |\}}}|du r:dS |S )	a  
        Try to decode the stream object as an image

        Args:
            pillow_parameters: parameters provided to Pillow Image.save() method,
                cf. <https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save>

        Returns:
            a PIL image if proper decoding has been found
        Raises:
            Exception: Errors during decoding will be reported.
                It is recommended to catch exceptions to prevent
                stops in your program.

        r#   )_xobj_to_imagez/Subtype z/Imagez does not seem to be an Imagez$ object does not seem to be an ImageN)_image_xobjectr>  r   rI   AttributeErrorr   r   r   )r@   r=  r>  r   	extension_imgrC   rC   rD   decode_as_image4  s   
zStreamObject.decode_as_imager   rR   )ry   )r   r   r   r   r   r<   r   r   r   r   r	   rc   r   rb   r[   r   rT   rd   r*  r,  r.  r   ru   r   r   r   r   r<  rE  __classcell__rC   rC   r(  rD   rM     sP    

 

, rM   c                   @   s   e Zd ZdS )r$  N)r   r   r   rC   rC   rC   rD   r$  R  s    r$  c                       s>   e Zd Zd
ddZdefddZdeddf fdd	Z  ZS )r0  r;   Nc                 C   s
   d | _ d S rR   )r#  r\   rC   rC   rD   r   W  r-  zEncodedStreamObject.__init__c                 C   sp   ddl m} | jd ur| j S t }|||  |  D ]\}}|tjtj	tj
fvr0|||< q|| _| S )Nr   )decode_stream_data)r   rG  r#  r*  r$  r,  r^   r   r   r1  r7  )r@   rG  decodedr   r   rC   rC   rD   r*  [  s   

zEncodedStreamObject.get_datarB   c                    s   ddl m} | tjdtjtjgfv r@t|tst	d| j
d u r&|   | j
d us/J d| j
| t || d S td)Nr   r3  r?  zData must be bytesr   zJStreams encoded with a filter different from FlateDecode are not supported)r   r4  r   r   r1  r5  r6  rL   rd   r8  r#  r*  r,  r'  r9  r!   )r@   rB   r4  r(  rC   rD   r,  l  s   

zEncodedStreamObject.set_datar   )r   r   r   r   rd   r*  r,  rF  rC   rC   r(  rD   r0  V  s    
r0  i'  c                       s  e Zd ZdZ	d(dedededeee ee	ef f ddf fddZ
d	edd fd
dZ		d)d	ededeeeee	f   dd fddZded	ededeeeee	f   deee	e	f  ddf fddZdeddfddZdedeeef fddZdefddZdeddf fddZedeeeef  fdd Zejd!eeeef  ddfd"d Zd*d#d$Z	d(ded%edeef ddf fd&d'Z  Z S )+ContentStreama  
    In order to be fast, this data structure can contain either:

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

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

    These fields are "rebuilt" lazily, when accessed:

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

    Conversely, these fields can be invalidated:

    * when .set_data() is called, ._operations is set to None.
    * when .operations is set, ._data is set to None.
    Nrn   rJ   rv   r;   c                    sT  || _ g | _|d u rt d n| }t|trddlm} t	| }t
kr3td| dt
 dt }d}|D ]Q}| }	t|	trFq:t|	tsXtdt|	j dt n|	 }
|t	|
7 }||krqtd	| d| d
||
7 }t	|dks|dd  dkr|d7 }|d7 }q:t t| n| }|d usJ t | || _d S )Nrw   r   )$MAX_ARRAY_BASED_STREAM_OUTPUT_LENGTHzArray-based stream has z > z
 elements.zExpected StreamObject, got z instead. Data might be wrong.z Array-based stream has at least z output bytes.ry   r   r#   )rJ   _operationsr'  r,  rN   rL   r9   pypdf.filtersrJ  r   CONTENT_STREAM_ARRAY_MAX_LENGTHr    	bytearrayr)   rM   r   typer   r*  rd   rv   )r@   rn   rJ   rv   rJ  stream_lengthrB   r   r   
s_resolvednew_datastream_datar(  rC   rD   r     sP   



zContentStream.__init__r:   c                 C   s   t d| | d d |d}| j|_z| j}|d u rd | _n	t d||| _W n	 ty1   Y nw |  D ]\}}t|drD||n||||< q6|S )NrI  Fr$  r<   )r
   r=   rZ   r"  r#  r<   rK   r^   r>   r,  rJ   ra   rK  rv   r%  rC   rC   rD   r<     s(   
zContentStream.replicateFrC   rE   rF   c                 C   sr   z| j j|kr|s| W S W n	 ty   Y nw t }td| | dd||}|du r.g }|| |||| |S )z
        Clone object into pdf_dest.

        Args:
            pdf_dest:
            force_duplicate:
            ignore_fields:

        Returns:
            The cloned ContentStream

        rI  N)rI   rJ   rK   rb   r
   r=   rZ   r   r   rC   rC   rD   rH     s$   zContentStream.cloner   r   c                    s6   t d|}t |j || _t|j| _|j| _dS )r&  rI  N)r
   r'  r,  r"  rJ   ra   rK  rv   )r@   r   r:   rE   rF   r   src_csr(  rC   rD   r     s
   
zContentStream._clonec                 C   s   | dd g }	 t|}|dv rd S | dd | s!|dv rJt|tj}|dkr?|g ks1J | |}| j|df n'| j||f g }n|d	kr\|d
vr[|	d}|d
vsRn
|t
|d | j q	)Nr   T)rw   r   ry   r#   )   '   "   BI   INLINE IMAGErx   )r   r   rw   )r}   r   isalphar   r(   delimiter_pattern_read_inline_imagerK  r?   r{   r~   rv   )r@   rn   operandspeekoperatoriirC   rC   rD   _parse_content_stream(  s,   

z#ContentStream._parse_content_streamc                 C   sx  t  }	 t|}|dd |dkrnt|| j}t|}|dd t|| j}|||< q|d}|d d dks=J |d|d	d
}| }t|t	rT|d }d|v s\d|v rat
|}	nd|v sid|v rnt|}	n~d|v svd|v r{t|}	nqd|v sd|v rt|}	nd|d
kr|dd}
t|
t	r|
d }
d|
v rd}n"d|
v rd}n|d|
dv rdnd}|dkr|d }nt|}	d}|dkr|ttt|d | tt|d  }	t|}|dd nt|}	|d}|dd |d d dks	|dd tvr7||d t|}	|d}|dd |d d dks0|dd tvr7td |||	d!S )"NTry   r#      Ir6   r   s   ID/Fz/Filterznot setr   AHxASCIIHexDecodeA85ASCII85DecodeRLRunLengthDecodeDCT	DCTDecodez/CSr?  RGBCMYK   z/BPC>   /G/I/Indexed/DeviceGrayr   g       @z/Wz/H   EIzHCould not extract inline image, even using fallback. Expected 'EI', got )settingsrB   )r   r   r}   r~   rJ   r{   r   r   rL   ra   r0   r/   r2   r1   r3   r   r
   r   r   r"   )r@   rn   rs  r   r   r   r   filtrsavposrB   cslcsbitseirC   rC   rD   r[  G  sz   







"
$
$
z ContentStream._read_inline_imagec                 C   s   | j s\t }| jD ]M\}}|dkr=|d t }|d | || dd  |d ||d  |d n|D ]}|| |d	 q?|| |d
 q	| | _ | j S )NrX  rW  rs  r   s   ID rB   rr  rs   r   )r"  r   rK  rt   ru   getvalue)r@   rR  r\  r^  	dict_textoprC   rC   rD   r*    s$   




zContentStream.get_datarB   c                    s   t  | g | _d S rR   )r'  r,  rK  r+  r(  rC   rD   r,    s   
zContentStream.set_datac                 C   s(   | j s| jr| t| j d| _| j S r!  )rK  r"  r`  r   r\   rC   rC   rD   
operations  s   zContentStream.operationsr~  c                 C   s   || _ d| _d S r!  )rK  r"  )r@   r~  rC   rC   rD   r~    s   
c                 C   sJ   | j r| j dg df | j g df d S | jr#d| j d | _d S d S )Nr      q   Qs   q
s   
Q
)rK  insertr?   r"  r\   rC   rC   rD   isolate_graphics_state  s   z$ContentStream.isolate_graphics_statero   c                    s&   | j s
| jr
|   t || d S rR   )r"  rK  r*  r'  ru   r/  r(  rC   rD   ru     s   zContentStream.write_to_streamrR   r   r   )!r   r   r   __doc__r   r	   rc   ra   r   r   r   r   r<   r   r   r   rH   r   rb   r[   r   r   r`  r[  rd   r*  r,  r   r~  setterr  ru   rF  rC   rC   r(  rD   rI    st    7
!
$L 
	rI  rn   rJ   rv   c                 C   s  |  d}| dd |dkrt| |S |dkr4|  d}| dd |dkr/t| ||S t| |S |dkr?t| ||S |d	v rHt| S |d
krQt| |S |dkr_|  ddkr_t	 S |dkrht	| S |dkrt
|  t| }| dd t| ||S |dv r|  d}| t| d t|d ur|d usJ dt| |S t| S |  }| dd |  d}| | t|  td|d| d|)Nr#   ry      /   <r   rz  r   rr   )   t   f   (   e   s   endobj   nrx   s   0123456789+-.   r   iP   z(Invalid Elementary Object starting with z @z: )r{   r}   r(   r   r   r4   r9   r$   r5   r)   r   r   r~   r   IndirectPatternmatchr'   r*   r   r   r!   )rn   rJ   rv   r   r]  r   stream_extractrC   rC   rD   r~     sP   









r~   c                   @   s   e Zd ZdZdeddfddZedee fddZ	edee fd	d
Z
eded fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZedee fddZdS )Fieldz
    A class representing a field dictionary.

    This class is accessed through
    :meth:`get_fields()<pypdf.PdfReader.get_fields>`
    rB   r;   Nc              	   C   s   t |  t t  }|j| _|D ]}z
|| | t|< W q ty(   Y qw t| 	dt
r[tt
| td  }t|trF| }n|d u rMd}ntdt|| td< d S d S )Nr   r?  zShould never happen)r   r   r   
attributesr   rI   r(   r   rL   r   r0  r
   r*  rd   decoderK   r,   )r@   rB   field_attributesattrdd_strrC   rC   rD   r     s*   


zField.__init__c                 C      |  tjS )z4Read-only property accessing the type of this field.)r   r   r5  r\   rC   rC   rD   
field_type     zField.field_typec                 C   r  )z6Read-only property accessing the parent of this field.)r   r   Parentr\   rC   rC   rD   r    r  zField.parentr9   c                 C   r  )z4Read-only property accessing the kids of this field.)r   r   Kidsr\   rC   rC   rD   kids$  r  z
Field.kidsc                 C   r  )z4Read-only property accessing the name of this field.)r   r   Tr\   rC   rC   rD   name)  r  z
Field.namec                 C   r  )z>Read-only property accessing the alternate name of this field.)r   r   TUr\   rC   rC   rD   alternate_name.  r  zField.alternate_namec                 C   r  )z
        Read-only property accessing the mapping name of this field.

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

        Format varies based on field type.
        )r   r   Vr\   rC   rC   rD   r   E  s   zField.valuec                 C   r  )z=Read-only property accessing the default value of this field.)r   r   DVr\   rC   rC   rD   default_valueN  r  zField.default_valuec                 C   r  )z
        Read-only property accessing the additional actions dictionary.

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

    See section 12.3.2 of the PDF 2.0 reference.

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

    Raises:
        PdfReadError: If destination type is invalid.

    Nnodetitlepagefitr;   c                 C   s  g | _ |j}|j}t|  t|| td< || td< || td< |dkrbt|dk r3|t	d t|dk r@|t	d t|dk rM|t	d |\| tt
j< | tt
j< | td	< d S t|d
krjd S |tjkr|\| tt
j< | tt
j< | tt
j< | tt
j< d S |tjtjfv rz|\| tt
j< W d S  ty   t | tt
j< Y d S w |tjtjfv rz|\| tt
j< W d S  ty   t | tt
j< Y d S w |tjtjfv rd S td|)N/Title/Pager   z/XYZr#   g        r   r6   /Zoomr   zUnknown Destination Type: )_filtered_childrenfit_typefit_argsr   r   r,   r(   r   r?   r*   TALEFTTOPTFFIT_RBOTTOMRIGHTFIT_HFIT_BHrK   r)   FIT_VFIT_BVFITFIT_Br!   )r@   r  r  r  typargsrC   rC   rD   r   s  sV   

zDestination.__init__r9   c                    s(   t  d d g fdddD  S )Nr  r   c                    s   g | ]
}| v r | qS rC   rC   rU   r\   rC   rD   r     s
    z*Destination.dest_array.<locals>.<listcomp>)/Left/Bottom/Right/Topr  )r9   r   r\   rC   r\   rD   
dest_array  s   
zDestination.dest_arrayrn   ro   c                 C   s   |d ur	t dd |d td}|| |d | j}|| td}|| |d td}|| |d |d	 d S )
Nrp   rq   r   z/Drs   z/Sz/GoTor   r   )r   rt   r(   ru   r  )r@   rn   ro   r   r   value_srC   rC   rD   ru     s"   







zDestination.write_to_streamc                 C   
   |  dS )z3Read-only property accessing the destination title.r  r   r\   rC   rC   rD   r       
zDestination.titlec                 C   r  )zHRead-only property accessing the IndirectObject of the destination page.r  r  r\   rC   rC   rD   r    r  zDestination.pagec                 C   r  )z2Read-only property accessing the destination type.r   r  r\   rC   rC   rD   r    r  zDestination.typc                 C      |  ddS )z-Read-only property accessing the zoom factor.r  Nr  r\   rC   rC   rD   zoom  r  zDestination.zoomc                 C   r  )z<Read-only property accessing the left horizontal coordinate.r  Nr  r\   rC   rC   rD   left  r  zDestination.leftc                 C   r  )z=Read-only property accessing the right horizontal coordinate.r  Nr  r\   rC   rC   rD   right  r  zDestination.rightc                 C   r  )z9Read-only property accessing the top vertical coordinate.r  Nr  r\   rC   rC   rD   top  r  zDestination.topc                 C   r  )z<Read-only property accessing the bottom vertical coordinate.r  Nr  r\   rC   rC   rD   bottom  r  zDestination.bottomc              
   C   s(   t d| 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.r9   z/Cr   )r
   r   r9   r&   r\   rC   rC   rD   color  s    zDestination.colorc                 C   s   t | ddS )z_
        Read-only property accessing the font type.

        1=italic, 2=bold, 3=both
        rb  r   )r   r   r\   rC   rC   rD   font_format  s   zDestination.font_formatc                 C   r  )z
        Read-only property accessing the outline count.

        positive = expanded
        negative = collapsed
        absolute value = number of visible descendants at all levels
        r  Nr  r\   rC   rC   rD   outline_count  s   	zDestination.outline_count)r;   r9   rR   )"r   r   r   r  r  r   r   __annotations__r	   rc   rd   r*   r'   r)   r.   r   r   r  r   ru   r  r  r  r   r  r&   r  r  r  r  r  r   r  r  rC   rC   rC   rD   r  ^  s^   
 

5
r  rR   )]
__author____author_email__loggingr   syscollections.abcr   r   ior   mathr   typingr   r   r   r	   r
   
_protocolsr   r   r   _utilsr   r   r   r   r   r   r   r   r   	constantsr   r   r   r   r   r5  r   r  r   r  errorsr   r    r!   r"   _baser$   r%   r&   r'   r(   r)   r*   r+   r,   r-   _fitr.   _image_inliner/   r0   r1   r2   r3   r4   r5   version_infor8   typing_extensions	getLoggerr   loggerr   r  ra   r9   r   r   r   r  rM   r$  r0  rM  rI  rc   r   r~   r  r  rC   rC   rC   rD   <module>   sl   ,0


 /   ' \ 1'  N
2c