o
    %eV                     @   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 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logz0.9.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   A/var/www/html/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   	reference1   s   zObject.referencec                 C   s   t  )z@Data contained in the object. Shall be defined in each subclass.)NotImplementedErrorr   r   r   r   r   6   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   C      
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>I   s    z#Dictionary.data.<locals>.<listcomp>   <<       >>)itemsjoin)r   resultr   r   r   r   G   s   zDictionary.datar-   r&   r'   r(   r)   r   r*   r   r   r   r   r   r,   A   
    
r,   c                       s~  e Zd ZdZdX fdd	ZdYddZdd	 ZdZd
dZdd ZdZddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdZddZdZd d!ZdZd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdZd2d3ZdZd4d5Zd6d7 Zd8d9 Zd: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,e-dVdW Z.  Z/S )[r$   zPDF Stream object.NFc                    s(   t    |pg | _|pi | _|| _d S r-   )superr   streamextracompress)r   rA   rB   rC   	__class__r   r   r   Q   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rA   append)r   tagproperty_listr   r   r   begin_marked_contentZ   s
   zStream.begin_marked_contentc                 C      | j d dS )zBegin a text object.s   BTNrG   r   r   r   r   
begin_textc      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*   WNrG   r   even_oddr   r   r   clipg   s   	zStream.clipc                 C   rL   )zClose current subpath.

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

           hNrG   r   r   r   r   closer   s   zStream.closec                 C   (   | 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.

        r1   r2   s   CSs   csNrA   rH   r   )r   spacestroker   r   r   color_space{      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.

        r2      cNrA   rH   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.

        r2      vNr^   )r   ra   rb   rc   rd   r   r   r   curve_start_to      
zStream.curve_start_toc              	   C   rf   )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.

        r2      yNr^   )r   r_   r`   rc   rd   r   r   r   curve_end_to   ri   zStream.curve_end_toc                 C      | j dt| d  dS )zDraw object given by reference.r1   s    DoNrW   )r   r"   r   r   r   draw_x_object      zStream.draw_x_objectc                 C   rL   )z%End path without filling or stroking.   nNrG   r   r   r   r   end   rN   z
Stream.endc                 C   rL   )zEnd marked-content sequence.s   EMCNrG   r   r   r   r   end_marked_content   rN   zStream.end_marked_contentc                 C   rL   )zEnd text object.s   ETNrG   r   r   r   r   end_text   rN   zStream.end_textc                 C   rO   )znFill path using nonzero winding rule.

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

        s   f*   fNrG   rQ   r   r   r   fill      zStream.fillc                 C   rO   )zyFill and stroke path usign nonzero winding rule.

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

        s   B*   BNrG   rQ   r   r   r   fill_and_stroke   ru   zStream.fill_and_strokec                 C   rO   )zFill, stroke and close path using nonzero winding rule.

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

        s   b*   bNrG   rQ   r   r   r   fill_stroke_and_close   ru   zStream.fill_stroke_and_closec                 C   $   | j dt|t|df dS )z0Add line from current point to point ``(x, y)``.r2      lNr^   r   xyr   r   r   line_to      $zStream.line_toc                 C   rz   )z8Begin new subpath by moving current point to ``(x, y)``.r2      mNr^   r|   r   r   r   move_to   r   zStream.move_toc                 C   rz   )zAMove text to next line at ``(x, y)`` distance from previous line.r2   s   TdNr^   r|   r   r   r   move_text_to   r   zStream.move_text_toc                 C   rl   )z@Paint shape and color shading using shading dictionary ``name``.r1   s    shNrW   )r   namer   r   r   shading   rn   zStream.shadingc                 C   rL   )zRestore graphic state.   QNrG   r   r   r   r   	pop_state   rN   zStream.pop_statec                 C   rL   )zSave graphic state.   qNrG   r   r   r   r   
push_state   rN   zStream.push_statec              	   C   rf   )zAdd rectangle to current path as complete subpath.

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

        r2   s   reNr^   )r   r}   r~   widthheightr   r   r   	rectangle   ri   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``.

        r2   s   RGs   rgNr^   )r   rgbrY   r   r   r   set_color_rgb   s   

zStream.set_color_rgbc                 C   rV   )z{Set color for nonstroking operations.

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

        r1   r2   s   SCNs   scnNrW   )r   r   rY   r   r   r   set_color_special   r[   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`

        r2      dN)rA   rH   r<   Arrayr   r   )r   
dash_array
dash_phaser   r   r   set_dash  s   
	zStream.set_dashc                 C   s(   | j dt| d t| d  dS )zSet font name and size.r1   r2   s    TfNrW   )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    TrNrW   )r   moder   r   r   set_text_rendering     zStream.set_text_renderingc                 C   r   )zSet text rise.s    TsNrW   )r   r   r   r   r   set_text_rise  r   zStream.set_text_risec                 C   r   )zSet line cap style.s    JNrW   )r   line_capr   r   r   set_line_cap  r   zStream.set_line_capc                 C   r   )zSet line join style.s    jNrW   )r   	line_joinr   r   r   set_line_join"  r   zStream.set_line_joinc                 C   r   )zSet line width.s    wNrW   )r   r   r   r   r   set_line_width&  r   zStream.set_line_widthc                 C   r   )zSet miter limit.s    MNrW   )r   miter_limitr   r   r   set_miter_limit*  r   zStream.set_miter_limitc                 C   rl   )zkSet specified parameters in graphic state.

        :param state_name: Name of the graphic state.

        r1   s    gsNrW   )r   
state_namer   r   r   	set_state.  s   zStream.set_statec                 C   rl   )z4Show text strings with individual glyph positioning.   [s   ] TJNrW   r   textr   r   r   	show_text6  rn   zStream.show_textc                 C   s   | j t|jd  dS )zShow single text string.s    TjN)rA   rH   Stringr   r   r   r   r   show_text_string:  s   zStream.show_text_stringc                 C   rL   )zStroke path.   SNrG   r   r   r   r   rY   >  rN   zStream.strokec                 C   rL   )zStroke and close path.   sNrG   r   r   r   r   stroke_and_closeB  rN   zStream.stroke_and_closec                 C   r\   )ap  Set text matrix and text line 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`

        r2   s   TmNr^   r   ar   cder   r   r   r   text_matrixF     
zStream.text_matrixc                 C   r\   )ap  Modify 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`

        r2   s   cmNr^   r   r   r   r   	transform[  r   zStream.transformc                 C   s   | j r	t |}n|}t|}| jdddt|dt|dt|dd|  d| j r.d	nd
dtt	|d d|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.

        r2   s   BIs   /Ws   /Hs   /BPCs   /CSs   /Devices   /Fs
   [/A85 /Fl]s   /A85s   /L   s   IDs   ~>s   EIN)
rC   zlibbase64	a85encoderA   rH   r<   r   r   len)r   r   r   rZ   bpcraw_datar   enc_datar   r   r   inline_imagep  s*   

zStream.inline_imagec                 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       | ]}t |V  qd S r-   r3   )r4   r   r   r   r   	<genexpr>      zStream.data.<locals>.<genexpr>z/FlateDecodeFilter	   )levelLengths   streams	   endstream)r<   rA   r,   rB   copyrC   r   compressobjflushr   r   )r   rA   rB   r   r   r   r   r     s   
zStream.dataNNFr-   )F)0r&   r'   r(   r)   r   rK   rM   rS   rU   rZ   re   rh   rk   rm   rp   rq   rr   rt   rw   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   r   r   r   r   r*   r   __classcell__r   r   rD   r   r$   O   sZ    
		

		




	&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-   )r@   r   string)r   r   rD   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   rD   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     r0   zArray.__init__c                 C   s   dd dd | D  d S )Nr   r2   c                 s   r   r-   r3   )r4   childr   r   r   r     r   zArray.data.<locals>.<genexpr>   ])r<   r   r   r   r   r     rn   z
Array.datar-   r>   r   r   r   r   r     r?   r   c                   @   sH   e Zd ZdZd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.   1.7Nc                 C   s   t || _|| _g | _t }d|_d|_| | tdt	g dd| _
| | j
 ti | _| | j td| j
jd| _| | j d| _d| _dS )	zCreate a PDF document.

        :param bytes version: PDF version.
        :param bytes identifier: PDF file identifier.

        i  r   z/Pagesr   )TypeKidsCountz/Catalog)r   PagesN)r   version
identifierobjectsr   r   r   
add_objectr,   r   pagesinfor"   catalogcurrent_positionxref_position)r   r   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   rH   )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   )r4   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_lineFc              	      sh  |du r| j nt|}|du r| jn|}| d| | | d| |dkrm|rmg }| jD ]}|jdkr7q/|jr@|| q/| j|_	| |j
| q/g g}d}t|D ]#\}	}|j}
||
 |d |j |d | |t|
d 7 }qUd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}|durRddd
 | jD }
t|
  }tt|jt|jf|d< t|g||}| j | _|_	| | | |j
| n| jD ]}|jdkrzqp| j|_	| |j
| qp| 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 | |durddd
 | jD }
t|
  }| 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 bytes identifier: PDF file identifier.
        :param bool compress: whether the PDF uses a compressed object stream.

        Ns   %PDF-s   %🖤s   1.5r   r   r   r   c                 s   r   r-   )r   )r4   ir   r   r   r   9  r   zPDF.write.<locals>.<genexpr>z/ObjStm)r   NFirstr      c                 s   s    | ]}|j V  qd S r-   )r   )r4   r   r   r   r   r   R  s    
r9   c                 3   s0    | ]}t  |D ]\}}||d V  q	qdS )bigN)zipto_bytes)r4   linelengthr6   xref_lengthsr   r   r   W  s    

z/XRef)r   IndexWSizeRootInfoc                 s        | ]}|j d kr|jV  qdS r   Nr   r   r4   objr   r   r   r   c      IDs   xrefz0 01005s   trailerr8   z/Size s   /Root s   /Info c                 s   r  r  r  r  r   r   r   r     r  s   /ID [r2   r   r:   s	   startxrefs   %%EOF) r   r   r   r   r   r   r%   rH   r   r   r!   	enumerater   r   r   r<   r$   r   boolr   r   r   maxr   r   r"   r   r   	hexdigestr   r   r   )r   r   r   r   rC   compressed_objectsr   rA   positionr   r   rB   object_streamxref
dict_indexfield2_sizemax_generationfield3_sizexref_stream	data_hashdict_streamr   r  r   r     s   















z	PDF.write)r   Nr   )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   VERSION__version__r   r   r.   r,   r$   r   r   r   r   r   r   r   r   <module>   s"    #  V