o
    e/>                     @  s  d dl mZ d dlZd dlZd dl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 edZedZdadad	d
 ZdddZG dd dZdd ZG dd de
j
ZdddZe	ejee e	eje e	ejddg e	ejd dS )    )annotationsN   )Image	ImageFile)i32le)	deprecatez^%%([^:]*):[ \t]*(.*)[ \t]*$z^%[%!\w]([^:]*)[ \t]*$c                  C  s   t d u rJtjdr*td u r$dd l} dD ]}| |d ur!|a nqdata t duS ztjddgtj	d da W t duS  t
yI   da Y t duS w t duS )Nwinr   )gswin32cgswin64cgsFr   z	--version)stdout)	gs_binarysysplatform
startswithgs_windows_binaryshutilwhich
subprocess
check_callDEVNULLOSError)r   binary r   E/var/www/html/venv/lib/python3.10/site-packages/PIL/EpsImagePlugin.pyhas_ghostscript-   s*   r   Fc                 C  s  t  s	d}t|| d \}} }}|\}	}
t|pd}|d | }|d | }d| |
d |
d   }d| |
d |
d   }t \}}t| d}t|dr\tj	|j
r\|j
}nRt \}}t| |}t|d	8}|dtj | }|d |}|dkr|t|d
}|sn|t|8 }|| |dksW d   n1 sw   Y  |rdnd}tdd|dd|dd|dd|ddddd| d| d|
d   d|
d   dd|ddg}z?d}tjdrt }| jtjO  _tj||d t|}|  W zt| |rt| W n& ty&   Y nw zt| |r7t| W w W w  tyB   Y w w |j ! }|  |S )z!Render an image using Ghostscriptz%Unable to locate Ghostscript on pathsr   r   g      R@      Nnamewbi  pngalphappmrawz-qz-gdxz-rfz-dBATCHz	-dNOPAUSEz-dSAFERz	-sDEVICE=z-sOutputFile=z-c z
 translatez-fshowpager   )startupinfo)"r   r   inttempfilemkstemposclosehasattrpathexistsr   openseekioSEEK_ENDtellreadminlenwriter   r   r   r   r   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWr   r   loadunlinkimcopy)tilesizefpscaletransparencymsgdecoderoffsetdatalengthbboxwidthheightres_xres_yout_fdoutfileinfile_tempinfilein_fdr$   fsize
lengthfilesdevicecommandr'   out_imr>   r   r   r   GhostscriptD   s   









rZ   c                   @  s.   e Zd ZdZdd ZejfddZdd ZdS )	PSFilez
    Wrapper for bytesio object that treats either CR or LF as end of line.
    This class is no longer used internally, but kept for backwards compatibility.
    c                 C  s   t dddd || _d | _d S )Nr[      zSIf you need the functionality of this class you will need to implement it yourself.)action)r   rB   char)selfrB   r   r   r   __init__   s   
zPSFile.__init__c                 C  s   d | _ | j|| d S N)r^   rB   r1   )r_   rG   whencer   r   r   r1      s   zPSFile.seekc                 C  s   | j pdg}d | _ | jd}|dvr*t|r*|| | jd}|dvr*t|s| jd| _ | j dv r9d | _ d|dS )N    r      
latin-1)r^   rB   r5   r7   appendjoindecode)r_   rV   cr   r   r   readline   s   

zPSFile.readlineN)	__name__
__module____qualname____doc__r`   r2   SEEK_SETr1   rj   r   r   r   r   r[      s
    
r[   c                 C  s(   | d d dkpt | dkot| dkS )N      %!PS   P )r7   i32)prefixr   r   r   _accept   s   (ru   c                   @  sH   e Zd ZdZdZdZdddddZd	d
 Zdd ZdddZ	dd Z
dS )EpsImageFilez.EPS File Parser for the Python Imaging LibraryEPSzEncapsulated PostscriptLLABRGBCMYK)r   r   r   rp   c                   s   j\ j d_d _td}t|}d}d}ddfdd} fdd	}	 jd
}|dkrE|dkrDnn0|dv rO|dkrNq4n&|dkrj|d tdkrad}t	||rh|  d}d}|d ||< |d
7 }q4|r|d tdks|d d dkr|  d}q4t
|d | d}	||	st|	}
|
r|
d
}|d d dkr|dd  jd< ndj|< n|	d dkrnzd}t|ns|d d dkr|d| d d}dd |d d D \}}}}|d
krd_n|dkrzj| _W n ty   Y n5w n3||f_d S r4r4|d d dkr&nt
|d | d}	||	 n|d d d kr?dd}q5|  jsOd!}t|d S )"Nrz      r   TFc                    s0   d j vrd} t| d j vrd} t| d S )NPS-Adobez'EPS header missing "%!PS-Adobe" commentBoundingBoxz*EPS header missing "%%BoundingBox" comment)infoSyntaxError)rE   )r_   r   r   check_required_header_comments   s   

z:EpsImageFile._open.<locals>.check_required_header_commentsc              
     s   zt | }W n tjy } zd}t||d }~ww |ru|dd\}}|j|< |dkrs|dkr7ddS jr>rsrsz*dd |  D }|d |d	  |d
 |d  f_ddj  |ffg_	W dS  t
yr   Y dS w dS d S )Nnot an EPS filer   r   r~   z(atend)Tc                 S  s   g | ]}t t|qS r   )r(   float).0ir   r   r   
<listcomp>  s    z=EpsImageFile._open.<locals>._read_comment.<locals>.<listcomp>r   r   epsr   r   )splitmatchreerrorr   groupr   _sizerA   r@   	Exception)rV   merE   kvboxrI   rG   reading_trailer_commentsr_   trailer_reachedr   r   _read_comment   s>   

"z)EpsImageFile._open.<locals>._read_commentr   rc   rd   %r      s   %%EndCommentsre      r}   	    zbad EPS headerr\   s   %ImageData:   c                 s  s    | ]}t |V  qd S ra   )r(   )r   valuer   r   r   	<genexpr>W  s    
z%EpsImageFile._open.<locals>.<genexpr>rp   1   s   %%EOFs	   %%Trailerz!cannot determine EPS bounding box)_find_offsetrB   r1   _moder   	bytearray
memoryviewr5   ordr   strfieldr   r   r   r   r   mode_map
ValueError)r_   byte_arrbytes_mv
bytes_readreading_header_commentsr   r   byterE   rV   r   r   image_data_valuescolumnsrows	bit_depthmode_idr   r   r   _open   s    





hzEpsImageFile._openc                 C  sp   | d}|dkr|dtj | }d}||fS t|dkr2| d}t|}t|d}||fS d}t|)Nrp   rq   r   rr   r   r   )r5   r1   r2   r3   r4   rs   r   )r_   rB   rV   rI   rG   rE   r   r   r   r   {  s   


zEpsImageFile._find_offsetr   Fc                 C  sD   | j rt| j | j| j||| _| jj| _| jj| _g | _ tj	| S ra   )
r@   rZ   rA   rB   r>   moder   r   r   r<   )r_   rC   rD   r   r   r   r<     s   

zEpsImageFile.loadc                 O  s   d S ra   r   )r_   argskwargsr   r   r   	load_seek  s   zEpsImageFile.load_seekNr   F)rk   rl   rm   rn   formatformat_descriptionr   r   r   r<   r   r   r   r   r   rv      s     %
	rv   c                 C  s  |    | jdkrd}n| jdkrd}n| jdkrd}nd}t||rT|d |d	 |d
| j  |d |d |d |d| j  |d|  |d |d |d| jd |d    |d| j  |d| j  |d| jd | jd | jd f  |d ||d d  t|dr|  t| |dd| j ddfg |d |d  t|dr|  dS dS )!z*EPS Writer for the Python Imaging Library.rx   )r   r   s   imagerz   )r   r   s   false 3 colorimager{   )r   rp   s   false 4 colorimagezimage mode is not supporteds   %!PS-Adobe-3.0 EPSF-3.0
s   %%Creator: PIL 0.1 EpsEncode
s   %%%%BoundingBox: 0 0 %d %d
s   %%Pages: 1
s   %%EndComments
s   %%Page: 1 1
s   %%ImageData: %d %d s   %d %d 0 1 1 "%s"
s   gsave
s   10 dict begin
s   /buf %d string def
r   r   s   %d %d scale
s   %d %d 8
s   [%d 0 0 -%d 0 %d]
s+   { currentfile buf readhexstring pop } bind
r      
flushr   r   Ns   
%%%%EndBinary
s   grestore end
)	r<   r   r   r8   rA   r-   r   r   _save)r>   rB   filenamer   operatorrE   r   r   r   r     sD   









&




r   z.psz.epszapplication/postscriptr   )r   ) 
__future__r   r2   r+   r   r   r   r)   r   r   r   _binaryr   rs   
_deprecater   compiler   r   r   r   r   rZ   r[   ru   rv   r   register_openr   register_saveregister_extensionsregister_mimer   r   r   r   <module>   s2   


]%	 
V4