o
    Xh0hZ                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ d ZZdd	 ZG d
d dZG dd deeZG dd deZG dd deZG dd deeZG dd dZdS )z
A low-level PDF generator.

    N)BOM_UTF16_BE)md5)ceillog)warnz0.11.0c                 C   sd   t | tr| S t | tr#|  rtt| dS | dddS t | tr+| j	S t| dS )zConvert item to bytes.asciif0)

isinstancebytesfloat
is_integerstrintencoderstripObjectdata)item r   D/var/www/html/rh/venv/lib/python3.10/site-packages/pydyf/__init__.py	_to_bytes   s   


r   c                   @   sH   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	dS )r   zBase class for PDF objects.c                 C   s   d | _ d| _d| _d| _d S )Nr   n)numberoffset
generationfreeselfr   r   r   __init__!   s   
zObject.__init__c                 C   s&   | j  d| j d }|| j d S )z%Indirect representation of an object. z obj
s   
endobj)r   r   r   r   )r   headerr   r   r   indirect,   s   zObject.indirectc                 C   s   | j  d| j d S )zObject identifier.r    z R)r   r   r   r   r   r   r   	reference2   s   zObject.referencec                 C   s   t  )z@Data contained in the object. Shall be defined in each subclass.)NotImplementedErrorr   r   r   r   r   7   s   zObject.datac                 C   s   | j  o	t| t S )z7Whether the object can be included in an object stream.)r   r
   Streamr   r   r   r   compressible<   s   zObject.compressibleN)
__name__
__module____qualname____doc__r   propertyr"   r#   r   r&   r   r   r   r   r      s    


r   c                   @   &   e Zd ZdZdddZedd ZdS )
DictionaryzPDF Dictionary object.Nc                 C   s   t |  t| |pi  d S N)r   r   dict)r   valuesr   r   r   r   D      
zDictionary.__init__c                 C   s$   dd |   D }dd| d S )Nc                 S   s(   g | ]\}}d t | d t | qS )   /    r   ).0keyvaluer   r   r   
<listcomp>J   s    z#Dictionary.data.<locals>.<listcomp>   <<       >>)itemsjoin)r   resultr   r   r   r   H   s   zDictionary.datar.   r'   r(   r)   r*   r   r+   r   r   r   r   r   r-   B   
    
r-   c                       s  e Zd ZdZd` fdd	ZdaddZdd	 Zdbd
dZdd ZdbddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdbddZdbd d!Zdbd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zdbd6d7Zdbd8d9Zdbd:d;Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0e1d^d_ Z2  Z3S )cr%   zPDF Stream object.NFc                    s(   t    |pg | _|pi | _|| _d S r.   )superr   streamextracompress)r   rB   rC   rD   	__class__r   r   r   R   s   



zStream.__init__c                 C   sF   | j d|  |du r| j d dS | j | | j d dS )zBegin marked-content sequence./Ns   BMCs   BDCrB   append)r   tagproperty_listr   r   r   begin_marked_content[   s
   zStream.begin_marked_contentc                 C      | j d dS )zBegin a text object.s   BTNrH   r   r   r   r   
begin_textd      zStream.begin_textc                 C      | j |r
d dS d dS )a  Modify current clipping path by intersecting it with current path.

        Use the nonzero winding number rule to determine which regions lie
        inside the clipping path by default.

        Use the even-odd rule if ``even_odd`` set to ``True``.

        s   W*   WNrH   r   even_oddr   r   r   cliph   s   	zStream.clipc                 C   rM   )zClose current subpath.

        Append a straight line segment from the current point to the starting
        point of the subpath.

           hNrH   r   r   r   r   closes   s   zStream.closec                 C   s   t tjjt | || dS )z7color_space is deprecated, use set_color_space instead.N)r   r%   color_spacer*   DeprecationWarningset_color_spacer   spacestroker   r   r   rW   |   s   zStream.color_spacec                 C   <   | j dt|t|t|t|t|t|df dS )u   Add cubic Bézier curve to current path.

        The curve shall extend from ``(x3, y3)`` using ``(x1, y1)`` and ``(x2,
        y2)`` as the Bézier control points.

        r3      cNrB   rI   r=   r   )r   x1y1x2y2x3y3r   r   r   curve_to   s
   
zStream.curve_toc              	   C   0   | j dt|t|t|t|df dS )u   Add cubic Bézier curve to current path

        The curve shall extend to ``(x3, y3)`` using the current point and
        ``(x2, y2)`` as the Bézier control points.

        r3      vNr_   )r   rb   rc   rd   re   r   r   r   curve_start_to      
zStream.curve_start_toc              	   C   rg   )u   Add cubic Bézier curve to current path

        The curve shall extend to ``(x3, y3)`` using `(x1, y1)`` and ``(x3,
        y3)`` as the Bézier control points.

        r3      yNr_   )r   r`   ra   rd   re   r   r   r   curve_end_to   rj   zStream.curve_end_toc                 C      | j dt| d  dS )zDraw object given by reference.r2   s    DoNrB   rI   r   )r   r#   r   r   r   draw_x_object      zStream.draw_x_objectc                 C   rM   )z%End path without filling or stroking.   nNrH   r   r   r   r   end   rO   z
Stream.endc                 C   rM   )zEnd marked-content sequence.s   EMCNrH   r   r   r   r   end_marked_content   rO   zStream.end_marked_contentc                 C   rM   )zEnd text object.s   ETNrH   r   r   r   r   end_text   rO   zStream.end_textc                 C   rP   )znFill path using nonzero winding rule.

        Use even-odd rule if ``even_odd`` is set to ``True``.

        s   f*   fNrH   rR   r   r   r   fill      zStream.fillc                 C   rP   )zyFill and stroke path usign nonzero winding rule.

        Use even-odd rule if ``even_odd`` is set to ``True``.

        s   B*   BNrH   rR   r   r   r   fill_and_stroke   rw   zStream.fill_and_strokec                 C   rP   )zFill, stroke and close path using nonzero winding rule.

        Use even-odd rule if ``even_odd`` is set to ``True``.

        s   b*   bNrH   rR   r   r   r   fill_stroke_and_close   rw   zStream.fill_stroke_and_closec                 C   s~   | j rt |n|}t|d }| jdddt|dt|dt|ddt| d	| j r/d
nddtt|d|df dS )a  Add an inline image.

        :param width: The width of the image.
        :type width: :obj:`int`
        :param height: The height of the image.
        :type height: :obj:`int`
        :param colorspace: The color space of the image, f.e. RGB, Gray.
        :type colorspace: :obj:`str`
        :param bpc: The bits per component. 1 for BW, 8 for grayscale.
        :type bpc: :obj:`int`
        :param raw_data: The raw pixel data.

        s   ~>r3   s   BIs   /Ws   /Hs   /BPCs   /CSs   /Devices   /Fs
   [/A85 /Fl]s   /A85s   /Ls   IDs   EIN)	rD   zlibbase64	a85encoderB   rI   r=   r   len)r   widthheightrW   bpcraw_datar   a85_datar   r   r   inline_image   s    

zStream.inline_imagec                 C   $   | j dt|t|df dS )z0Add line from current point to point ``(x, y)``.r3      lNr_   r   xyr   r   r   line_to      $zStream.line_toc                 C   r   )z8Begin new subpath by moving current point to ``(x, y)``.r3      mNr_   r   r   r   r   move_to   r   zStream.move_toc                 C   r   )zAMove text to next line at ``(x, y)`` distance from previous line.r3   s   TdNr_   r   r   r   r   move_text_to   r   zStream.move_text_toc                 C   s   t tjjt | | dS )z1shading is deprecated, use paint_shading instead.N)r   r%   shadingr*   rX   paint_shadingr   namer   r   r   r      s   zStream.shadingc                 C   rm   )z@Paint shape and color shading using shading dictionary ``name``.r2   s    shNrn   r   r   r   r   r      rp   zStream.paint_shadingc                 C   rM   )zRestore graphic state.   QNrH   r   r   r   r   	pop_state   rO   zStream.pop_statec                 C   rM   )zSave graphic state.   qNrH   r   r   r   r   
push_state  rO   zStream.push_statec              	   C   rg   )zAdd rectangle to current path as complete subpath.

        ``(x, y)`` is the lower-left corner and width and height the
        dimensions.

        r3   s   reNr_   )r   r   r   r   r   r   r   r   	rectangle  rj   zStream.rectanglec                 C   s2   | j dt|t|t||rdndf dS )zSet RGB color for nonstroking operations.

        Set RGB color for stroking operations instead if ``stroke`` is set to
        ``True``.

        r3   s   RGs   rgNr_   )r   rgbr\   r   r   r   set_color_rgb  s   

zStream.set_color_rgbc                 C   s(   | j dt| d |rdnd  dS )zwSet the nonstroking color space.

        If stroke is set to ``True``, set the stroking color space instead.

        r2   r3   s   CSs   csNrn   rZ   r   r   r   rY     s   zStream.set_color_spacec                 G   sJ   |rg |dt | R }| jddd |D d |rdnd  dS )zSet special color for nonstroking operations.

        Set special color for stroking operation if ``stroke`` is set to ``True``.

        r2   r3   c                 s       | ]}t |V  qd S r.   r4   )r5   operandr   r   r   	<genexpr>/      z+Stream.set_color_special.<locals>.<genexpr>s   SCNs   scnN)r   rB   rI   r=   )r   r   r\   operandsr   r   r   set_color_special&  s   
zStream.set_color_specialc                 C   s&   | j dt|jt|df dS )zSet dash line pattern.

        :param dash_array: Dash pattern.
        :type dash_array: :term:`iterable`
        :param dash_phase: Start of dash phase.
        :type dash_phase: :obj:`int`

        r3      dN)rB   rI   r=   Arrayr   r   )r   
dash_array
dash_phaser   r   r   set_dash2  s   
	zStream.set_dashc                 C   s(   | j dt| d t| d  dS )zSet font name and size.r2   r3   s    TfNrn   )r   fontsizer   r   r   set_font_size>  s   zStream.set_font_sizec                 C      | j t|d  dS )zSet text rendering mode.s    TrNrn   )r   moder   r   r   set_text_renderingC     zStream.set_text_renderingc                 C   r   )zSet text rise.s    TsNrn   )r   r   r   r   r   set_text_riseG  r   zStream.set_text_risec                 C   r   )zSet line cap style.s    JNrn   )r   line_capr   r   r   set_line_capK  r   zStream.set_line_capc                 C   r   )zSet line join style.s    jNrn   )r   	line_joinr   r   r   set_line_joinO  r   zStream.set_line_joinc                 C   r   )zSet line width.s    wNrn   )r   r   r   r   r   set_line_widthS  r   zStream.set_line_widthc                 C   r]   )am  Set current transformation matrix.

        :param a: Top left number in the matrix.
        :type a: :obj:`int` or :obj:`float`
        :param b: Top middle number in the matrix.
        :type b: :obj:`int` or :obj:`float`
        :param c: Middle left number in the matrix.
        :type c: :obj:`int` or :obj:`float`
        :param d: Middle middle number in the matrix.
        :type d: :obj:`int` or :obj:`float`
        :param e: Bottom left number in the matrix.
        :type e: :obj:`int` or :obj:`float`
        :param f: Bottom middle number in the matrix.
        :type f: :obj:`int` or :obj:`float`

        r3   s   cmNr_   r   ar   cder   r   r   r   
set_matrixW     
zStream.set_matrixc                 C   r   )zSet miter limit.s    MNrn   )r   miter_limitr   r   r   set_miter_limitl  r   zStream.set_miter_limitc                 C   rm   )zkSet specified parameters in graphic state.

        :param state_name: Name of the graphic state.

        r2   s    gsNrn   )r   
state_namer   r   r   	set_statep  s   zStream.set_statec                 C   r]   )a  Set current text and text line transformation matrix.

        :param a: Top left number in the matrix.
        :type a: :obj:`int` or :obj:`float`
        :param b: Top middle number in the matrix.
        :type b: :obj:`int` or :obj:`float`
        :param c: Middle left number in the matrix.
        :type c: :obj:`int` or :obj:`float`
        :param d: Middle middle number in the matrix.
        :type d: :obj:`int` or :obj:`float`
        :param e: Bottom left number in the matrix.
        :type e: :obj:`int` or :obj:`float`
        :param f: Bottom middle number in the matrix.
        :type f: :obj:`int` or :obj:`float`

        r3   s   TmNr_   r   r   r   r   set_text_matrixx  r   zStream.set_text_matrixc                 C   &   t tjjt | |||||| dS )z7text_matrix is deprecated, use set_text_matrix instead.N)r   r%   text_matrixr*   rX   r   r   r   r   r   r        zStream.text_matrixc                 C   r   )z0transform is deprecated, use set_matrix instead.N)r   r%   	transformr*   rX   r   r   r   r   r   r     r   zStream.transformc                 C   rm   )z4Show text strings with individual glyph positioning.   [s   ] TJNrn   r   textr   r   r   	show_text  rp   zStream.show_textc                 C   s   | j t|jd  dS )zShow single text string.s    TjN)rB   rI   Stringr   r   r   r   r   show_text_string  s   zStream.show_text_stringc                 C   rM   )zStroke path.   SNrH   r   r   r   r   r\     rO   zStream.strokec                 C   rM   )zStroke and close path.   sNrH   r   r   r   r   stroke_and_close  rO   zStream.stroke_and_closec                 C   st   d dd | jD }t| j }| jr*d|d< tjdd}||}|| 7 }t	||d< d |j
d	|d
fS )N   
c                 s   r   r.   r4   )r5   r   r   r   r   r     r   zStream.data.<locals>.<genexpr>z/FlateDecodeFilter	   )levelLengths   streams	   endstream)r=   rB   r-   rC   copyrD   r|   compressobjflushr   r   )r   rB   rC   r   r   r   r   r     s   
zStream.data)NNFr.   )F)4r'   r(   r)   r*   r   rL   rN   rT   rV   rW   rf   ri   rl   ro   rr   rs   rt   rv   ry   r{   r   r   r   r   r   r   r   r   r   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r\   r   r+   r   __classcell__r   r   rE   r   r%   P   sb    
		

	





	r%   c                       s.   e Zd ZdZd fdd	Zedd Z  ZS )r   zPDF String object. c                    s   t    || _d S r.   )rA   r   string)r   r   rE   r   r   r     s   

zString.__init__c                 C   s^   zt ddt| j}d| d W S  ty.   tt| jd }d|   d  Y S w )Ns
   ([\\\(\)])s   \\\1   (   )z	utf-16-be   <   >)	resubr   r   UnicodeEncodeErrorr   r   r   hex)r   escapedencodedr   r   r   r     s   zString.data)r   )r'   r(   r)   r*   r   r+   r   r   r   r   rE   r   r     s
    r   c                   @   r,   )r   zPDF Array object.Nc                 C   s   t |  t| |pg  d S r.   )r   r   list)r   arrayr   r   r   r     r1   zArray.__init__c                 C   s   dd dd | D  d S )Nr   r3   c                 s   r   r.   r4   )r5   childr   r   r   r     r   zArray.data.<locals>.<genexpr>   ])r=   r   r   r   r   r     rp   z
Array.datar.   r?   r   r   r   r   r     r@   r   c                   @   sF   e Zd ZdZdd Zdd Zdd Zedd	 Zd
d Z	dddZ
dS )PDFzPDF document.c                 C   s   g | _ t }d|_d|_| | tdtg dd| _| | j ti | _| | j td| jj	d| _
| | j
 d| _d| _dS )	zCreate a PDF document.i  r   z/Pagesr   )TypeKidsCountz/Catalog)r   PagesN)objectsr   r   r   
add_objectr-   r   pagesinfor#   catalogcurrent_positionxref_position)r   zero_objectr   r   r   r     s(   




zPDF.__init__c                 C   s8   | j d  d7  < | | | j d |jddg dS )zfAdd page to the PDF.

        :param page: New page.
        :type page: :class:`Dictionary`

        r      r   r   RN)r   r   extendr   )r   pager   r   r   add_page  s   
zPDF.add_pagec                 C   s   t | j|_| j| dS )zAdd object to the PDF.N)r   r   r   rI   )r   object_r   r   r   r     s   zPDF.add_objectc                 C   s"   t dd | jd d d d D S )Nc                 s   s     | ]}| d  dV  qdS )z 0 Rr   N)r   )r5   object_numberr   r   r   r     s
    
z&PDF.page_references.<locals>.<genexpr>r      )tupler   r   r   r   r   page_references	  s   zPDF.page_referencesc                 C   s(   |  j t|d 7  _ ||d  dS )zWrite line to output.

        :param content: Content to write.
        :type content: :obj:`bytes`
        :param output: Output stream.
        :type output: binary :term:`file object`

        r   r   N)r   r   write)r   contentoutputr   r   r   
write_line  s   	zPDF.write_line   1.7Fc              	      sp  t |pd}|dvrt |}| d| | | d| |dkrl|rlg }| jD ]}|jdkr1q)|jr:|| q)| j|_| |j| q)g g}d}t	|D ]#\}	}|j
}
||
 |d |j |d | |t|
d 7 }qOd	d
d |d D |d< dt|t|d d d}t|||}| j|_| | | |j| g }d}| jD ] }|jr|d|j|f |d7 }q|t|j|j|jf q|d| jdf tt| jd d}tdd | jD }ttt|t|d d}d||f d fdd|D }dtdt| jd ft t| jd | jj| jjd}|rQddd | jD }
t|
  }|du rC|}tt|j
t|j
f|d< t|g||}| j | _|_| | | |j| n| jD ]}|jdkryqo| j|_| |j| qo| j| _| d| | dt| j  | | jD ]}| |jdd	|jdd	|j d	 | q| d| | d| | dt| j  | | d| jj | | d | jj | |rdd!d | jD }
t|
  }|du r|}| d"t|j
 d# t|j
 d$ | | d%| | d&| | | j  | | d'| d(S ))a  Write PDF to output.

        :param output: Output stream.
        :type output: binary :term:`file object`
        :param bytes version: PDF version.
        :param identifier: PDF file identifier. Default is :obj:`False`
          to include no identifier, can be set to :obj:`True` to generate an
          automatic identifier.
        :type identifier: :obj:`bytes` or :obj:`bool`
        :param bool compress: whether the PDF uses a compressed object stream.

        r  )FTNs   %PDF-s   %🖤s   1.5r   r   r   r    c                 s   r   r.   )r   )r5   ir   r   r   r   F  r   zPDF.write.<locals>.<genexpr>z/ObjStm)r   NFirst      c                 s   s    | ]}|j V  qd S r.   )r   )r5   r   r   r   r   r   _  s    
r:   c                 3   s0    | ]}t  |D ]\}}||d V  q	qdS )bigN)zipto_bytes)r5   linelengthr7   xref_lengthsr   r   r   d  s    

z/XRef)r   IndexWSizeRootInfoc                 s        | ]}|j d kr|jV  qdS r   Nr   r   r5   objr   r   r   r   p      TIDs   xrefz0 01005s   trailerr9   z/Size s   /Root s   /Info c                 s   r  r  r  r  r   r   r   r     r  s   /ID [r3   r   r;   s	   startxrefs   %%EOFN)r   r  r   r   r&   rI   r   r   r"   	enumerater   r   r   r=   r%   r   boolr   r   r   maxr   r   r#   r   r   	hexdigestr   r   r   )r   r   version
identifierrD   compressed_objectsr   rB   positionr  r   rC   object_streamxref
dict_indexfield2_sizemax_generationfield3_sizexref_stream	data_hashdict_streamr   r  r   r     s   














z	PDF.writeN)r  FF)r'   r(   r)   r*   r   r   r   r+   r   r  r   r   r   r   r   r     s    #
r   )r*   r}   r   r|   codecsr   hashlibr   mathr   r   warningsr   VERSION__version__r   r   r/   r-   r%   r   r   r   r   r   r   r   r   <module>   s$    #  f