a
    [XhZ                     @   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   sf   t | tr| S t | trH|  r2tt| dS | dddS nt | trX| 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   S/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/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  dS )z@Data contained in the object. Shall be defined in each subclass.N)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                   @   s&   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   s    
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.data)Nr'   r(   r)   r*   r   r+   r   r   r   r   r   r,   B   s   
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dcddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZddddZded d!Zdfd"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dgd6d7Zdhd8d9Zdid: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 )jr%   zPDF Stream object.NFc                    s(   t    |pg | _|pi | _|| _d S r-   )superr   streamextracompress)r   r?   r@   rA   	__class__r   r   r   R   s    


zStream.__init__c                 C   sD   | j d|  |du r(| j d n| j | | j d dS )zBegin marked-content sequence./Ns   BMCs   BDCr?   append)r   tagZproperty_listr   r   r   begin_marked_content[   s
    zStream.begin_marked_contentc                 C   s   | j d dS )zBegin a text object.s   BTNrE   r   r   r   r   
begin_textd   s    zStream.begin_textc                 C   s   | j |rdn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*   WNrE   r   Zeven_oddr   r   r   cliph   s    	zStream.clipc                 C   s   | j d dS )zClose current subpath.

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

           hNrE   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   rO   |   s    zStream.color_spacec                 C   s<   | 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.

        r1      cNr?   rF   r;   r   )r   x1y1x2y2x3y3r   r   r   curve_to   s
    
zStream.curve_toc              	   C   s0   | 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.

        r1      vNrV   )r   rY   rZ   r[   r\   r   r   r   curve_start_to   s    
zStream.curve_start_toc              	   C   s0   | 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 `(x1, y1)`` and ``(x3,
        y3)`` as the Bézier control points.

        r1      yNrV   )r   rW   rX   r[   r\   r   r   r   curve_end_to   s    
zStream.curve_end_toc                 C   s   | j dt| d  dS )zDraw object given by reference.r0   s    DoNr?   rF   r   )r   r#   r   r   r   draw_x_object   s    zStream.draw_x_objectc                 C   s   | j d dS )z%End path without filling or stroking.   nNrE   r   r   r   r   end   s    z
Stream.endc                 C   s   | j d dS )zEnd marked-content sequence.s   EMCNrE   r   r   r   r   end_marked_content   s    zStream.end_marked_contentc                 C   s   | j d dS )zEnd text object.s   ETNrE   r   r   r   r   end_text   s    zStream.end_textc                 C   s   | j |rdnd dS )znFill path using nonzero winding rule.

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

        s   f*   fNrE   rK   r   r   r   fill   s    zStream.fillc                 C   s   | j |rdnd dS )zyFill and stroke path usign nonzero winding rule.

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

        s   B*   BNrE   rK   r   r   r   fill_and_stroke   s    zStream.fill_and_strokec                 C   s   | j |rdnd dS )zFill, stroke and close path using nonzero winding rule.

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

        s   b*   bNrE   rK   r   r   r   fill_stroke_and_close   s    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   ~>r1   s   BIs   /Ws   /Hs   /BPCs   /CSs   /Devices   /Fs
   [/A85 /Fl]s   /A85s   /Ls   IDs   EIN)	rA   zlibbase64	a85encoder?   rF   r;   r   len)r   widthheightrO   Zbpcraw_datar   Za85_datar   r   r   inline_image   s     

zStream.inline_imagec                 C   s$   | j dt|t|df dS )z0Add line from current point to point ``(x, y)``.r1      lNrV   r   xyr   r   r   line_to   s    zStream.line_toc                 C   s$   | j dt|t|df dS )z8Begin new subpath by moving current point to ``(x, y)``.r1      mNrV   rw   r   r   r   move_to   s    zStream.move_toc                 C   s$   | j dt|t|df dS )zAMove text to next line at ``(x, y)`` distance from previous line.r1   s   TdNrV   rw   r   r   r   move_text_to   s    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*   rP   paint_shadingr   namer   r   r   r~      s    zStream.shadingc                 C   s   | j dt| d  dS )z@Paint shape and color shading using shading dictionary ``name``.r0   s    shNrb   r   r   r   r   r      s    zStream.paint_shadingc                 C   s   | j d dS )zRestore graphic state.   QNrE   r   r   r   r   	pop_state   s    zStream.pop_statec                 C   s   | j d dS )zSave graphic state.   qNrE   r   r   r   r   
push_state  s    zStream.push_statec              	   C   s0   | j dt|t|t|t|df dS )zAdd rectangle to current path as complete subpath.

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

        r1   s   reNrV   )r   rx   ry   rr   rs   r   r   r   	rectangle  s    
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``.

        r1   s   RGs   rgNrV   )r   rgbrT   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.

        r0   r1   s   CSs   csNrb   rR   r   r   r   rQ     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``.

        r0   r1   c                 s   s   | ]}t |V  qd S r-   r2   )r3   operandr   r   r   	<genexpr>/  r8   z+Stream.set_color_special.<locals>.<genexpr>s   SCNs   scnN)r   r?   rF   r;   )r   r   rT   Z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`

        r1      dN)r?   rF   r;   Arrayr   r   )r   Z
dash_arrayZ
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.r0   r1   s    TfNrb   )r   fontsizer   r   r   set_font_size>  s    zStream.set_font_sizec                 C   s   | j t|d  dS )zSet text rendering mode.s    TrNrb   )r   moder   r   r   set_text_renderingC  s    zStream.set_text_renderingc                 C   s   | j t|d  dS )zSet text rise.s    TsNrb   )r   rs   r   r   r   set_text_riseG  s    zStream.set_text_risec                 C   s   | j t|d  dS )zSet line cap style.s    JNrb   )r   Zline_capr   r   r   set_line_capK  s    zStream.set_line_capc                 C   s   | j t|d  dS )zSet line join style.s    jNrb   )r   Z	line_joinr   r   r   set_line_joinO  s    zStream.set_line_joinc                 C   s   | j t|d  dS )zSet line width.s    wNrb   )r   rr   r   r   r   set_line_widthS  s    zStream.set_line_widthc                 C   s<   | j dt|t|t|t|t|t|df dS )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`

        r1   s   cmNrV   r   ar   cder   r   r   r   
set_matrixW  s    
zStream.set_matrixc                 C   s   | j t|d  dS )zSet miter limit.s    MNrb   )r   Zmiter_limitr   r   r   set_miter_limitl  s    zStream.set_miter_limitc                 C   s   | j dt| d  dS )zkSet specified parameters in graphic state.

        :param state_name: Name of the graphic state.

        r0   s    gsNrb   )r   Z
state_namer   r   r   	set_statep  s    zStream.set_statec                 C   s<   | j dt|t|t|t|t|t|df dS )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`

        r1   s   TmNrV   r   r   r   r   set_text_matrixx  s    
zStream.set_text_matrixc                 C   s&   t tjjt | |||||| dS )z7text_matrix is deprecated, use set_text_matrix instead.N)r   r%   text_matrixr*   rP   r   r   r   r   r   r     s    zStream.text_matrixc                 C   s&   t tjjt | |||||| dS )z0transform is deprecated, use set_matrix instead.N)r   r%   	transformr*   rP   r   r   r   r   r   r     s    zStream.transformc                 C   s   | j dt| d  dS )z4Show text strings with individual glyph positioning.   [s   ] TJNrb   r   textr   r   r   	show_text  s    zStream.show_textc                 C   s   | j t|jd  dS )zShow single text string.s    TjN)r?   rF   Stringr   r   r   r   r   show_text_string  s    zStream.show_text_stringc                 C   s   | j d dS )zStroke path.   SNrE   r   r   r   r   rT     s    zStream.strokec                 C   s   | j d dS )zStroke and close path.   sNrE   r   r   r   r   stroke_and_close  s    zStream.stroke_and_closec                 C   st   d dd | jD }t| j }| jrTd|d< tjdd}||}|| 7 }t	||d< d |j
d	|d
fS )N   
c                 s   s   | ]}t |V  qd S r-   r2   )r3   r   r   r   r   r     r8   zStream.data.<locals>.<genexpr>z/FlateDecodeFilter	   )levelZLengths   streams	   endstream)r;   r?   r,   r@   copyrA   rn   compressobjflushrq   r   )r   r?   r@   r   r   r   r   r     s    
zStream.data)NNF)N)F)F)F)F)F)F)F)F)4r'   r(   r)   r*   r   rH   rI   rL   rN   rO   r]   r_   ra   rc   re   rf   rg   ri   rk   rm   ru   rz   r|   r}   r~   r   r   r   r   r   rQ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   r   r+   r   __classcell__r   r   rB   r   r%   P   s`   	
	
	





	
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   rB   r   r   r     s    
zString.__init__c                 C   sb   z"t ddt| j}d| d W S  ty\   tt| jd }d|   d  Y S 0 d S )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   rB   r   r     s   r   c                   @   s&   e Zd ZdZdddZedd ZdS )r   zPDF Array object.Nc                 C   s   t |  t| |pg  d S r-   )r   r   list)r   arrayr   r   r   r     s    
zArray.__init__c                 C   s   dd dd | D  d S )Nr   r1   c                 s   s   | ]}t |V  qd S r-   r2   )r3   childr   r   r   r     r8   zArray.data.<locals>.<genexpr>   ])r;   r   r   r   r   r     s    z
Array.data)Nr=   r   r   r   r   r     s   
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   ZPagesN)objectsr   r   r   
add_objectr,   r   pagesinfor#   catalogcurrent_positionxref_position)r   Z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)rq   r   r   rF   )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   )r3   Z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   rq   write)r   contentoutputr   r   r   
write_line  s    	zPDF.write_line   1.7Fc              	      sv  t |pd}|dvrt |}| d| | | d| |dkr|rg }| jD ]8}|jdkrbqR|jrt|| qR| j|_| |j| qRg g}d}t	|D ]F\}	}|j
}
||
 |d |j |d | |t|
d 7 }qd	d
d |d D |d< dt|t|d d d}t|||}| j|_| | | |j| g }d}| jD ]D}|jr|d|j|f |d7 }n|t|j|j|jf qX|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}|rddd | jD }
t|
  }|du r|}tt|j
t|j
f|d< t|g||}| j | _|_| | | |j| nh| jD ],}|jdkrq| j|_| |j| q| j| _| d| | dt| j  | | jD ]2}| |jdd	|jdd	|j d	 | qH| 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   s   | ]}t |V  qd S r-   )r   )r3   ir   r   r   r   F  r8   zPDF.write.<locals>.<genexpr>z/ObjStm)r   NZFirst      c                 s   s   | ]}|j V  qd S r-   )r   )r3   r   r   r   r   r   _  s   r8   c                 3   s.   | ]&}t  |D ]\}}||d V  qqdS )bigN)zipto_bytes)r3   linelengthr5   Zxref_lengthsr   r   r   d  s   z/XRef)r   IndexWSizeZRootZInfoc                 s   s   | ]}|j d kr|jV  qdS r   Nr   r   r3   objr   r   r   r   p  s   TZIDs   xrefz0 Z010Z05s   trailerr7   z/Size s   /Root s   /Info c                 s   s   | ]}|j d kr|jV  qdS r   r   r   r   r   r   r     s   s   /ID [r1   r   r9   s	   startxrefs   %%EOFN)r   r   r   r   r&   rF   r   r   r"   	enumerater   r   rq   r;   r%   r   boolr   r   r   maxr   r   r#   r   r   	hexdigestr   r   r   )r   r   version
identifierrA   Zcompressed_objectsr   r?   positionr   r   r@   Zobject_streamZxrefZ
dict_indexZfield2_sizeZmax_generationZfield3_sizeZxref_streamZ	data_hashZ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*   ro   r   rn   codecsr   hashlibr   mathr   r   warningsr   VERSION__version__r   r   r.   r,   r%   r   r   r   r   r   r   r   r   <module>   s"   #  f