a
    7Xh'                     @   s   d Z d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mZ g dfd	d
ZddddZddddZd+ddZd,ddZd-ddZdd Zdd Zd.dd Zd!d" Zd#d$ Zd/d%d&Zd'd( ZG d)d* d*ZdS )0z3.3.0     N)BytesIO)	rl_config)ImageReader	isUnicode)asciiBase85EncodeasciiBase85Decode<   c                 C   s0   t dt| |D ]}|| |||   q|S Nr   )rangelenappend)srcdst	chunkSizei r   _/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/reportlab/pdfbase/pdfutils.py_chunker   s    r   RGBCMYKG)r   r   L         Fc                 C   s   dd l }t| }|d ur2|| |r2| r2d S | \}}| }g }|j}	|	d |	d||t|j f  |	d t||| t	|j  ksJ d|
|}
t|
}t|| |	d |S )Nr   BIz(/W %s /H %s /BPC 8 /CS /%s /F [/A85 /Fl]IDWrong amount of data for imageEI)zlibr   r   jpeg_fhgetSize
getRGBData_mode2csmoder   	_mode2bppcompressr   r   )filenameIMG
detectJpegr    imgimgwidth	imgheightrawcoder   
compressedencodedr   r   r   makeA85Image   s&    
"

r2   c                 C   s   dd l }t| }|d ur2|| |r2| r2d S | \}}| }g }|j}	|	d |	d||t|j f  |	d t||| t	|j  ksJ d|
|}
t|
| |	d |S )Nr   r   z#/W %s /H %s /BPC 8 /CS /%s /F [/Fl]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   r0   r   r   r   makeRawImage9   s$    
"

r3   c                 C   s   t j| d tjrdpd }| |krft| rXddlm} |rdtd||	 
dS qtd|  n\tjrxt| |}n
t| |}|r|S t|d}|d|d  |  tjrtd	|  dS )
zHProcesses image as if for encoding, saves to a file with .a85 extension.r   .a85z.bin)open_for_readN
zNo such cached image %swbzcached image as %s)ospathsplitextr   useA85cachedImageExistsreportlab.lib.utilsr5   filterreadsplitIOErrorr2   r3   openwritejoincloseverboseprint)r(   ZreturnInMemoryr)   
cachednamer5   r/   fr   r   r   cacheImageFileT   s    

rJ   c                 C   sT   ddl }t| tr| | }n| }|D ](}t|rFtjrNtd|  q&t| q&dS )zPreprocesses one or more image files.

    Accepts either a filespec ('C:\mydir\*.jpg') or a list
    of image filenames, crunches them all to save time.  Run this
    to save huge amounts of time when repeatedly building image
    documents.r   Nz#cached version of %s already exists)glob
isinstancestrr<   r   rF   rG   rJ   )specrK   filelistr(   r   r   r   preProcessImagesm   s    
rP   c                 C   s`   t j| d tjrdpd }t j|rXt | d }t |d }||krRdS dS ndS dS )zDetermines if a cached image already exists for a given file.

    Determines if a cached image exists which has the same name
    and equal or newer date to the given file.r   r4   bin   r   N)r8   r9   r:   r   r;   isfilestat)r(   rH   Zoriginal_dateZcached_dater   r   r   r<      s    r<   r6    c                 C   s$   |  d| d| d| ||S )zzNormalizes different line end character(s).

    Ensures all instances of CR, LF and CRLF end up as
    the specified one.r6   
)replace)textZdesiredZunlikelyr   r   r   _normalizeLineEnds   s    rZ   c                 C   s:   t | r| d} t }|t|  |d | S )zEncodes input using ASCII-Hex coding.

    This is a verbose encoding used for binary data within
    a PDF file.  One byte binary becomes two bytes of ASCII.
    Helper function used by images.utf-8   >)r   encoder   rC   binasciib2a_hexgetvalue)inputoutputr   r   r   _AsciiHexEncode   s    

rc   c                    s|   t | s| d} d|    d dks4J d dd  t d dksXJ d	d fd
dtdt dD S )zeDecodes input using ASCII-Hex coding.

    Not used except to provide a test of the inverse function.r[   r   >z'Invalid terminator for Ascii Hex StreamN   r   z(Ascii Hex stream has odd number of bytesc                    s&   g | ]}t t ||d   dqS )rf      )chrint).0r   strippedr   r   
<listcomp>       z#_AsciiHexDecode.<locals>.<listcomp>)r   decoderD   r@   r   r   )ra   r   rk   r   _AsciiHexDecode   s    
rp   c                 C   s   g }t | }d}|| }||k rH|| |||   |d }|| }qt |d dkr|d dd |d d |d  g|dd< d|S )z>Wraps input at a given column size by inserting 
 characters.r   r   rd   Nr6   )r   r   rD   )ra   columnsrb   lengthr   posr   r   r   _wrap   s    
,ru   c                 C   s  ddl }ddlm} g d}g d}g d}d}d}|s|d| d	}|d d
kr4|d| d	}|d |v r| dd	 |d| d	}|d dkr|d|d| d}	|	d d> |	d	  }
|d| d}	|	d d> |	d	  }|d| d	}	|	d }||
||fS |d dkr|d| d}|d d> |d	  d }| |}|d|dd }	|d|dd }|d d> |d	  |	d d> |	d	  f}q4|d |v r|d|d  q4|d |vr4|d| d}| |d d> |d	  d d	 q4dS )z@Read width, height and number of components from open JPEG file.r   N)PDFError)         )
                           r   )                                 )H   r   Br      rf   rR   z#JPEG must have 8 bits per componentBB   
      z#JPEG Unsupported JPEG marker: %0.2x)structZreportlab.pdfbase.pdfdocrv   unpackr?   seek)imager   rv   ZvalidMarkersZnoParamMarkersZunsupportedMarkersZdpidonexyheightwidthcolornr   r   r   readJPEGInfo   sJ    
*r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )_fuscc                 C   s.   |sJ d|| _ t|| _t|p&d| _d S )Nz'Argument k should be a non empty string   )_kr   _klenri   _n)selfkr   r   r   r   __init__  s    
z_fusc.__init__c                 C   s.   |  tdtt| ttt|| jS Nr   )	_fusc__rotater   rD   maprh   _fusc__fusclistordr   r   sr   r   r   encrypt  s    z_fusc.encryptc                 C   s0   d tt| tttt| || j S r   )	rD   r   rh   r   r   r   r   r   r   r   r   r   r   decrypt  s    z_fusc.decryptc                 C   sF   t |}|dk r|| }||; }|s(|S || d  |d ||   S r
   )r   )r   r   r   lr   r   r   Z__rotate!  s
    z_fusc.__rotatec              
   C   sB   t |}ttdd |tttt|| j d | j d | S )Nc                 S   s   | |A S )Nr   )r   r   r   r   r   <lambda>*  rn   z_fusc.__fusc.<locals>.<lambda>r   )r   r   r   r   ri   r   r   )r   r   slenr   r   r   Z__fusc(  s    z_fusc.__fuscN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r     s
   r   )NF)NF)r   N)r6   rU   )r	   )__version____doc__r8   r^   ior   Z	reportlabr   r=   r   r   Zreportlab.lib.rl_accelr   r   r   r$   r&   r2   r3   rJ   rP   r<   rZ   rc   rp   ru   r   r   r   r   r   r   <module>   s*   




5