a
    s=ic                     @   s  d Z ddlZddlZddlmZ ddlZ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mZ ddlZddlZddlZddlmZmZmZmZ ddlmZ ddlmZmZmZmZ dd	lm Z m!Z! dd
l"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 e5e6Z7dZ8dZ9G dd dZ:e: Z;ddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1Z<d2d3 Z=d4d5 Z>ej?d6d7d8d9d: Z@d;d< ZAd=d> ZBd?d@ ZCdAdB ZDG dCdD dDe4jEZFG dEdF dFeZGG dGdH dHeZHe?d6dIdJ ZIdKdL ZJdMdN ZKd[dPdQZLd\dRdSZMd]dTdUZNd^dVdWZOeZPg dXZQejRG dYdZ dZeZSdS )_zG
A PostScript backend, which can produce both PostScript .ps and .eps.
    N)Enum)StringIO)TemporaryDirectory)_apicbook_path_text_helpers)AFM)_BackendFigureCanvasBaseFigureManagerBaseRendererBase)is_writable_file_likefile_requires_unicode)get_font)LOAD_NO_SCALEFT2Font)convert_ttf_to_ps)	uni2type1)Path)
TexManager)Affine2D)MixedModeRenderer   )_backend_pdf_pszLevel IIFc                   @   s   e Zd Zdd ZdS )PsBackendHelperc                 C   s
   i | _ d S N)_cachedself r    o/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/matplotlib/backends/backend_ps.py__init__+   s    zPsBackendHelper.__init__N)__name__
__module____qualname__r"   r    r    r    r!   r   *   s   r   )      !@   )r&      )r'      )Gz@@gHzgG@)p=
c7@r*   )
ףp=0@r+   )zGa'@r,   )
ףp= @r-   )RQ@r.   )Q@r/   )HzG@r0   )ffffff @r1   )\(\?r2   )gRQ?r3   )fffffFD@g)\L@))\<@r4   )QE4@r5   ))\,@r6   )Q8$@r7   )p=
ף@r8   ))\(@r9   )p=
ף@r:   )Gz@r;   ))\(?r<   )g)\(?r=   )letterZlegalledgera0Za1Za2a3Za4Za5Za6Za7Za8Za9Za10Zb0b1b2Zb3Zb4Zb5Zb6Zb7Zb8Zb9Zb10c                 C   sF   t t ddD ]0\}\}}|dr(q| |k r||k r|  S qdS )NT)reverselr@   )sorted	papersizeitems
startswith)whkeypwphr    r    r!   _get_papertypeM   s    

rO   c                  G   s   d dd | D S )N c                 s   s"   | ]}|d  d dV  qdS )1.3f0.N)rstrip).0argr    r    r!   	<genexpr>W       z_nums_to_str.<locals>.<genexpr>)join)argsr    r    r!   _nums_to_strV   s    r[   z3.6z a vendored copy of this function)alternativec                 C   sX   |  dd} |  dd} |  dd} |  dd} |  d	d
} tddd | } | dS )zR
    Quote dangerous characters of S for use in a PostScript string constant.
       \s   \\   (s   \(   )s   \)   's   \251   `s   \301s   [^ -~\n]c                 S   s   dt |   S )Ns   \%03o)ordgroup)xr    r    r!   <lambda>d   rX   z!quote_ps_string.<locals>.<lambda>ascii)replaceresubdecode)sr    r    r!   quote_ps_stringZ   s    rl   c                 C   sp   t |rZt|rt| dddnt| d}| t|| W d   ql1 sN0    Y  ntj| |tjd dS )z
    Move the contents of file at *src* to path-or-filelike *dst*.

    If *dst* is a path, the metadata of *src* are *not* copied.
    rlatin-1encodingrbN)copy_function)r   r   openshutilcopyfileobjmovecopyfile)srcdstfhr    r    r!   _move_path_to_path_or_streamh   s    ,r{   c           
         s  t | dd  fdd|D }dj jd j dtt jd fdd|D t|d d	}d
}g }|D ]~} 	|t
}  \}}	|d |dtt|jdg|jd tt|d |	dddddg ddd d  qp|d| | S )a  
    Subset *chars* from the font at *font_path* into a Type 3 font.

    Parameters
    ----------
    font_path : path-like
        Path to the font to be subsetted.
    chars : str
        The characters to include in the subsetted font.

    Returns
    -------
    str
        The string representation of a Type 3 font, which can be included
        verbatim into a PostScript file.
    r   )Zhinting_factorc                    s   g | ]}  |qS r    get_char_indexrU   cfontr    r!   
<listcomp>   rX   z%_font_to_ps_type3.<locals>.<listcomp>aM  %!PS-Adobe-3.0 Resource-Font
%%Creator: Converted from TrueType to Type 3 by Matplotlib.
10 dict begin
/FontName /{font_name} def
/PaintType 0 def
/FontMatrix [{inv_units_per_em} 0 0 {inv_units_per_em} 0 0] def
/FontBBox [{bbox}] def
/FontType 3 def
/Encoding [{encoding}] def
/CharStrings {num_glyphs} dict dup begin
/.notdef 0 def
rP   c                 3   s   | ]}d   |V  qdS )z/{}N)formatget_glyph_name)rU   glyph_idr   r    r!   rW      s   z$_font_to_ps_type3.<locals>.<genexpr>)Z	font_nameZinv_units_per_embboxrp   Z
num_glyphsz
end readonly def

/BuildGlyph {
 exch begin
 CharStrings exch
 2 copy known not {pop /.notdef} if
 true 3 1 roll get exec
 end
} _d

/BuildChar {
 1 index /Encoding get exch get
 1 index /BuildGlyph get exec
} _d

FontName currentdict end definefont pop
z/%(name)s{%(bbox)s sc
r   )namer   @   NF)   m   lrX      crX   Trf   zce} _d
)r   r   postscript_nameZunits_per_EMrY   mapstrr   lenZ
load_glyphr   get_pathappendr   ZhoriAdvancer   convert_to_stringr   rj   )
	font_pathchars	glyph_idspreambleZ	postambleentriesr   gvr   r    r   r!   _font_to_ps_type3x   s@    
r   c           	   	      s  d dd |D }td| | zt| |}td| t| j| j	 t
|  fdd|D }t j}tj |d}t|d	}||  W d
   n1 s0    Y  tt||d| W d
   n1 s0    Y  W n  ty
   td  Y n0 d
S )a2  
    Subset *chars* from the font at *font_path* into a Type 42 font at *fh*.

    Parameters
    ----------
    font_path : path-like
        Path to the font to be subsetted.
    chars : str
        The characters to include in the subsetted font.
    fh : file-like
        Where to write the font.
     c                 s   s   | ]}t |V  qd S r   )chrr~   r    r    r!   rW      rX   z%_font_to_ps_type42.<locals>.<genexpr>zSUBSET %s characters: %szSUBSET %s %d -> %dc                    s   g | ]}  |qS r    r|   r~   r   r    r!   r      rX   z&_font_to_ps_type42.<locals>.<listcomp>ztmp.ttfwbN*   zDThe PostScript backend does not currently support the selected font.)rY   _logdebugr   Zget_glyphs_subsetosstatst_size	getbuffernbytesr   r   pathrs   writegetvaluer   fsencodeRuntimeErrorwarning)	r   r   rz   Z
subset_strZfontdatar   tmpdirtmpfiletmpr    r   r!   _font_to_ps_type42   s&    ,6r   c                    s   t   fdd}|S )z}
    Wrap `RendererPS` method *meth* to emit a PS comment with the method name,
    if the global flag `debugPS` is set.
    c                    s0   t r| jd j d  | g|R i |S )Nz% r   )debugPS	_pswriterr   r#   )r   rZ   kwargsmethr    r!   wrapper   s    z!_log_if_debug_on.<locals>.wrapper)	functoolswraps)r   r   r    r   r!   _log_if_debug_on   s    r   c                       s8  e Zd ZdZedZdZd: fdd	Zdd Z	d;d
dZ
d<ddZedd Zd=ddZedd Zd>ddZd?ddZd@ddZdd Zdd ZdAd d!Zd"d# ZedBd$d%ZedCd&d'ZedDd(d)Zed*d+ Zedd,d-d.ZedEd/d0Zed1d2 Zed3d4 Zed5d6 Z d	d	d7d8d9Z!  Z"S )F
RendererPSz
    The renderer handles all the drawing primitives using a graphics
    context instance that controls the colors/styles.
    z	fonts/afm	ps.useafmH   c                    s   t  || || _tjd r*d| _g | _|| _d | _d | _	d | _
d | _d | _d | _d | _i | _|d | _i | _d| _t | _td tj| _d S )Ntext.usetexr   r   )superr"   r   mplrcParamstextcntpsfragimagedpicolor	linewidthlinejoinlinecaplinedashfontnamefontsize_hatchesimage_magnification_clip_paths_path_collection_idr   ZCharacterTracker_character_trackerr   	lru_cacher   r   _logwarn_once)r   widthheightZpswriterr   	__class__r    r!   r"     s&    


zRendererPS.__init__c                 C   sJ   |d u rdS t |dkrB|d dkr(dS |d dkr>| d dS dS d S )NT      r   r   zlThe PostScript backend does not support transparency; partially transparent artists will be rendered opaque.F)r   r   )r   Zrgb_or_rgbar    r    r!   _is_transparent!  s    zRendererPS._is_transparentTc                 C   sl   |||f| j krh| j||  kr*|kr:n n|ddn|dd|dd|dd |rh|||f| _ d S )NrQ   z	 setgray
rP   z setrgbcolor
)r   r   r   )r   rm   r   bstorer    r    r!   	set_color/  s    zRendererPS.set_colorc                 C   s0   t |}|| jkr,| jd|  |r,|| _d S )Nz%1.3f setlinewidth
)floatr   r   r   )r   r   r   r    r    r!   set_linewidth7  s
    
zRendererPS.set_linewidthc                 C   s"   ddddddd|  } | ddS )Nr   r      )ZmiterroundZbevelr   r   r   dz setlinejoin
r    )r   r    r    r!   _linejoin_cmd>  s    zRendererPS._linejoin_cmdc                 C   s*   || j kr&| j| | |r&|| _ d S r   )r   r   r   r   )r   r   r   r    r    r!   set_linejoinE  s    
zRendererPS.set_linejoinc                 C   s"   ddddddd|  } | ddS )Nr   r   r   )Zbuttr   Z
projectingr   r   r   r   z setlinecap
r    )r   r    r    r!   _linecap_cmdK  s    zRendererPS._linecap_cmdc                 C   s*   || j kr&| j| | |r&|| _ d S r   )r   r   r   r   )r   r   r   r    r    r!   set_linecapR  s    
zRendererPS.set_linecapc                 C   st   | j d ur,| j \}}t||r,||kr,d S | j|d ur\t|r\dt|  dt| dnd |rp||f| _ d S )N[z] z	 setdash
z[] 0 setdash
)r   npZarray_equalr   r   r   r[   )r   offsetseqr   ZoldoZoldseqr    r    r!   set_linedashX  s    

zRendererPS.set_linedashc                 C   sD   ||f| j | jfkr@| jd| d|dd |r@|| _ || _d S )N/rP   rQ    selectfont
)r   r   r   r   )r   r   r   r   r    r    r!   set_fonte  s
    zRendererPS.set_fontc                 C   s   d}|| j v r| j | S dt| j  }tjd }| jd }| jd|dd|dd|dd|dd	|d
d| jt	|t
 |dd d|d
d| d || j |< |S )Nr   zH%dzhatch.linewidthzH  << /PatternType 1
     /PaintType 2
     /TilingType 2
     /BBox[0 0 r   rP   z]
     /XStep z
     /YStep z(

     /PaintProc {
        pop
        r   z setlinewidth
Fsimplifyz]
        gsave
        fill
        grestore
        stroke
     } bind
   >>
   matrix
   0 z translate
   makepattern
   /z
 exch def
)r   r   r   r   r   r   r   _convert_pathr   hatchr   scale)r   r   Zsidelenr   r   Z
pageheightr    r    r!   create_hatchl  s    



0

zRendererPS.create_hatchc                 C   s   | j S )z
        Get the factor by which to magnify images passed to draw_image.
        Allows a backend to have images at a different resolution to other
        artists.
        )r   r   r    r    r!   get_image_magnification  s    z"RendererPS.get_image_magnificationFNc              
   C   sD   |rdd| j d | jd f}nd }t||||d dg dddS )Ng        g      R@   )r   r   rX   r   s   clTrf   )r   r   r   r   rj   )r   r   	transformclipr   r    r    r!   r     s    

zRendererPS._convert_pathc              
   C   s   g }|  }|d ur4|dtg |j|jR    | \}}|d ur|t|f}| j|}|d u rdt	| j }| j
d| d| j||dd d || j|< || d d	|S )
Nz%s clipbox
zc%dr   z {
Fr   z
clip
newpath
} bind def
r   r   )Zget_clip_rectangler   r[   sizep0Zget_clip_pathidr   getr   r   r   r   rY   )r   gcr   rectr   ZtrfrL   Zcustom_clip_cmdr    r    r!   _get_clip_cmd  s$     

zRendererPS._get_clip_cmdc                 C   s  |j d d \}}d}|d d dd d d df }	|	 dd}
|d u rdd}|| j }|| j }n dtt|  }d	}d	}| j	
d
| | d|dd|dd| d|dd|dd|dd|dd|dd|dd|dd|dd| d|
 d d S )Nr   zfalse 3 colorimager   r   z1 0 0 1 0 0rP   g      ?gsave
r   z translate
[z	] concat
z scale
/DataString r   z string def
z 8 [ z 0 0 -z 0 z5 ]
{
currentfile DataString readhexstring pop
} bind 

grestore
)shapetobyteshexr   rY   r   r   frozenZ	to_valuesr   r   r   )r   r   rd   yZimr   rK   rJ   ZimagecmddatahexdatamatrixZxscaleZyscaler    r    r!   
draw_image  s(    
D
zRendererPS.draw_imagec                 C   sB   |d u o|  d u }|jo|}| j||||d}| ||| d S )Nr   r   )Zget_hatch_pathZshould_simplifyr   _draw_ps)r   r   r   r   rgbFacer   r   psr    r    r!   	draw_path  s    
zRendererPS.draw_pathc                 C   s  |  |rd n>|d |d   kr.|d kr>n nd|d  nd|d d  }g d}| }	| svt| dkr~| n
| d }
|	dko|
dk}|r|d|	  || |  || 	|
  || j||d	d
 |r"|r|d |r||dg |r"|d |r2|d |ddg |j|dd| jd | jd fd	dD ]6\}}t|rd|dd  \}}|d||f  qdd|}| j|||d	d	d d S )Nr   r   r   z%1.3f setgrayz%1.3f %1.3f %1.3f setrgbcolorr   )z/o {gsavenewpath	translatez%.1f setlinewidthFr   r  fillZgrestorestrokez
} bind defr   r  z%g %g or   r  r  )r   get_linewidthZget_forced_alphar   get_rgbZ	get_alphar   r   get_joinstyler   get_capstyler   extenditer_segmentsr   r   rY   r  )r   r   Zmarker_pathZmarker_transr   transr  Zps_colorZps_cmdZlwalphar  verticescoderd   r  r  r    r    r!   draw_markers  sX    







zRendererPS.draw_markersc                 C   s6  t |dkrt |d jnd}| |||||}|d|  d |d | k }|stt| |||||||||	|
|||S g }t| |||D ]N\}\}}d| j|f }| j||dd}| j	
d| d| d	 || q| |||||||	|
|||D ]*\}}}}}d
|||f }| ||| q|  jd7  _d S )Nr   r   r   zp%d_%dFr   r   z {
newpath
translate
z
} bind def
z%g %g %sr   )r   r  Z_iter_collection_uses_per_pathr   draw_path_collection	enumerateZ_iter_collection_raw_pathsr   r   r   r   r   Z_iter_collectionr  )r   r   Zmaster_transformpathsZall_transformsoffsetsZoffset_transZ
facecolorsZ
edgecolorsZ
linewidthsZ
linestylesZantialiasedsurlsZoffset_positionZlen_pathZuses_per_pathZshould_do_optimizationZ
path_codesir   r   r   Z
path_bytesxoyoZpath_idZgc0r  r  r    r    r!   r"    sB    



zRendererPS.draw_path_collection)mtextc             
   C   s  |  | rd S t| dsB| d | ||||||d| d S | j||dd\}}	}
| }d| j }d| d d  }d	d
dt	j
d d d}|| }d||f }t|||
 }| jd||||d |f  | jd| d| d |  jd7  _d S )Nr   zThe PS backend determines usetex status solely based on rcParams['text.usetex'] and does not support having usetex=True only for some elements; this element will thus be rendered as if usetex=False.FTeX)ismathz
psmarker%dz%1.3f,%1.3f,%1.3fr   z{\sffamily %s}z{\ttfamily %s})z
sans-serifZ	monospacezfont.familyr   z{\rmfamily %s}z\color[rgb]{%s} %sz/\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}g      ?r   z	 moveto
(z)
show
grestore
r   )r   r  hasattrr   	draw_textZget_text_width_height_descentget_size_in_pointsr   r   r   r   r[   r   r   r   r   )r   r   rd   r  rk   propangler*  rJ   rK   blr   thetextr   Zfontcmdtexposr    r    r!   draw_tex@  s>    


zRendererPS.draw_texc	                 C   sH  |  | rd S |dkr.| ||||||S |rF| ||||||S tjd r(| |}	d|  }
g }d}d }g }|D ]}t	t
|dt
|d}z|	|}W n  ty   d}|	d}Y n0 |	||}|}|||
 7 }|||f |||
 7 }q||	jd	d
d	}|||f n| |}	| j|	| g }d  }}t||	D ]d}|jjd	d
d	}|j|ur|r|| |j}|g g}|d |j|j|jf qX|| | j|   |D ]h\}}| || d ddd |D }| j d| !| d|dd|dd|dd| d qd S )Nr+  r   gMbP?r   uniZ04Xquestion?rf   rg   r   Fr   c                 s   s&   | ]\}}|d d|ddV  qdS )r   z 0 m /rk   z
 glyphshowNr    )rU   rd   r   r    r    r!   rW     s   z'RendererPS.draw_text.<locals>.<genexpr>r   r   rP    translate
 rotate
r  )"r   r  r6  draw_mathtextr   r   Z_get_font_afmr/  r   r   rb   Zget_width_from_char_nameKeyErrorZget_width_charZget_kern_dist_from_namer   r   encoderj   Z_get_font_ttfr   trackr   ZlayoutZ	ft_objectrd   r   Z	glyph_idxr   r   rY   r   r   r   )r   r   rd   r  rk   r0  r1  r,  r*  r   r   streamZthisx	last_nameZxs_namesr   r   r   kernZps_nameZ	prev_fontZcurr_streamitemr3  r    r    r!   r.  h  sz    




zRendererPS.draw_textc                 C   s:  | j j|d|\}}}	}
}| j|   | jd|dd|dd|dd d}|
D ]\}}}}}| j|| |j	|f|kr|j	|f}| jd|j	 d| d	 t
|tr|t|n|||}| j|dd|dd
| d qX|D ]0\}}}}| j| d| d| d| d q| jd dS )z-Draw the math text using matplotlib.mathtext.r   r   r   rP   r:  r;  Nr   r   z	 moveto
/z glyphshow
z
 rectfill
	grestore
)Z
_text2pathZmathtext_parserparser   r  r   r   r   Ztrack_glyphr   
isinstancer	   Zget_name_charr   r   r}   )r   r   rd   r  rk   r0  r1  r   r   ZdescentZglyphsZrectsZlastfontr   r   numoxZoyZ
glyph_namerJ   rK   r    r    r!   r<    s>    
&zRendererPS.draw_mathtextc                 C   s    |  ||d|d| d S )N)r   r   r   )r   r   r   )draw_gouraud_trianglesreshape)r   r   pointscolorsr  r    r    r!   draw_gouraud_triangle  s    
z RendererPS.draw_gouraud_trianglec                 C   s  t |t |ksJ |jdks"J |jd dks4J |jd dksFJ |jdksTJ |jd dksfJ |jd dksxJ |j}||d |d  df}||}||d |d  df}tj|ddd }tj|ddd }	td|	|  }
|\}}|	\}}tj	|d |d  g d	d
}d|d< || |
 |d< |d d d df d |d< |
 dd}| jd|dd|dd|dd|dd| d d S )Nr   r   r   r   r   )Zaxisi   l    ))flagsu1)rK  z2>u4)rL  Z3u1)ZdtyperN  rK  g     o@rL  r   r   zgsave
<< /ShadingType 4
   /ColorSpace [/DeviceRGB]
   /BitsPerCoordinate 32
   /BitsPerComponent 8
   /BitsPerFlag 8
   /AntiAlias true
   /Decode [ r   rP   z  0 1 0 1 0 1 ]
   /DataSource <
z
>
>>
shfill
grestore
)r   ndimr  rJ  r   r   minmaxceilemptyr  r  r   r   )r   r   rK  rL  r  r  Zflat_pointsZflat_colorsZ
points_minZ
points_maxfactorZxminZyminZxmaxZymaxr  r  r    r    r!   rI    s6    
(
z!RendererPS.draw_gouraud_trianglesr  c          
      C   sf  | j j}| dko"| |  }|s,d}| |r:d}| }|r~| |  | |  | 	|
  | j|   |s|r| j| dd   |d || | ||  |d |r|s|r|d | j|dd ddi |d |s|r|d	 |rL| |}	|d |d
| dd   |d|	  |rZ|d |d	 dS )at  
        Emit the PostScript snippet *ps* with all the attributes from *gc*
        applied.  *ps* must consist of PostScript commands to construct a path.

        The *fill* and/or *stroke* kwargs can be set to False if the *ps*
        string already includes filling and/or stroking, in which case
        `_draw_ps` is just supplying properties and clipping.
        r   FNr   r   r   r   zfill
rD  z	%f %f %f z%s setpattern fill grestore
zstroke
)r   r   r  r   r  Z	get_hatchr   r   r  r   r  r   Z
get_dashesr   r   stripr   Zget_hatch_color)
r   r  r   r  r  r  r   Zmightstroker   Z
hatch_namer    r    r!   r    sF    	

zRendererPS._draw_ps)r   )T)T)T)T)T)T)FN)N)N)N)FN)#r#   r$   r%   __doc__r   Z_get_data_pathZ_afm_font_dirZ_use_afm_rc_namer"   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r
  r  r!  r"  r6  r.  r<  rM  rI  r  __classcell__r    r    r   r!   r      sN   








"
	  4
,'E


-r   c                   @   s    e Zd Zed\ZZdd ZdS )_Orientationr   c                 C   s   | j dkr|d d d S |S )N	landscaper   )r   )r   r  r    r    r!   swap_if_landscape3  s    z_Orientation.swap_if_landscapeN)r#   r$   r%   rangeportraitr[  r\  r    r    r    r!   rZ  0  s   rZ  c                       s   e Zd ZdZdddZdd Zeddd	d	d
dddZd	dddZ	d	dddZ
eedZeedZ fddZ  ZS )FigureCanvasPSr   Z
PostscriptzEncapsulated Postscript)r  epsc                 C   s   dS )Nr  r    r   r    r    r!   get_default_filetype<  s    z#FigureCanvasPS.get_default_filetypez3.5rZ   Nr^  )metadata	papertypeorientationc                O   s"  | j j}d| j _i }	t|ttjfrFt|j}
|
	dd
d|	d< |pLi ddtj d|	d< td}|rtjt|d	nt |	d
< ddd |	 D }	|d u rtjd }| }tjdgt|d tjt| d}tjd r| jn| j}|||f||	||d| d S )Nr   rf   rg   ZTitleZCreatorzMatplotlib vz, https://matplotlib.org/SOURCE_DATE_EPOCHz%a %b %d %H:%M:%S %YZCreationDater   c                 s   s"   | ]\}}d | d| V  qdS )z%%z: Nr    )rU   kr   r    r    r!   rW   W  s   z+FigureCanvasPS._print_ps.<locals>.<genexpr>zps.papersizeauto)rc  )rd  r   )dpidsc_commentsrd  rc  ) figurerh  rF  r   r   PathLikepathlibr   r   r>  rj   r   r   __version__getenvdatetimeutcfromtimestampintstrftimetimectimerY   rH   r   lowerr   Zcheck_in_listrG   Zcheck_getitemrZ  _print_figure_tex_print_figure)r   fmtoutfilerb  rc  rd  rZ   r   rh  ri  filenamesource_date_epochprinterr    r    r!   	_print_ps?  sN    


zFigureCanvasPS._print_psbbox_inches_restorec                   s  |dkt |ttjfs(t|s(tdj \	dkrPt		f 	t
 \}}	tjd r	|ks||	krt		f 	t
 \}}	d|	  
d|	  
}
}|
jjj }|jjj }dtju r||
||f\}
}}}d|	  
 
d|
|||f t _t	j|d	tj	||d
}j|  	
fdd}tjd rLt }tj|d}t|ddd}|| W d   n1 s0    Y  tjd dkrtt| d n"tjd dkr tt| d t|| W d   n1 s@0    Y  nRtj |ddd0}t!|svt"#d|}|| W d   n1 s0    Y  dS )a+  
        Render the figure to a filesystem path or a file-like object.

        Parameters are as for `.print_figure`, except that *dsc_comments* is a
        all string containing Document Structuring Convention comments,
        generated from the *metadata* parameter to `.print_figure`.
        r`  z,outfile must be a path or a file-like objectrg  ps.usedistillerg      B@r   r   Z   r   r~  c                    s  rt d| d nt d dd| d t  dj dt d	  d
d| d tt}t d| d tjd s|tjj7 }t d| | d t d| d t d	t| d tjd s jj
 D ]X\}}|sqtjd }t|dkrd}|   |dkr| t|| qt|||  qt d| d t d| d sJt d| d t d| d t dt
 | d rt d | d t dt	d d d	d	 | d t j | d t d| d t d| d st d| d |   d S )Nz%!PS-Adobe-3.0 EPSF-3.0)filez%%!PS-Adobe-3.0
%%DocumentPaperSizes: z
%%Pages: 1
r   )endr  z
%%Orientation: r   r   z
%%EndComments
z%%BeginPrologr   z/mpldict %d dict defzmpldict beginzps.fonttype   r   r   r  z%%EndPrologz%%Page: 1 1z%s translatez	%d rotatez
%s clipboxr   Zshowpagez%%EOF)printr   get_bbox_headerr   psDefsr   r   r   usedrY   rH   flushr   r   r   r[   r   r   )rz   ZNdictr   r   Zfonttyper   ri  r   is_epsrd  rc  ps_rendererZrotationr   r   r(  r)  r    r!   print_figure_impl  sd    





z7FigureCanvasPS._print_figure.<locals>.print_figure_impltmp.psrJ   rn   ro   Nghostscript)ptyper   xpdf)$rF  r   r   rk  r   
ValueErrorrj  get_size_inchesrO   r\  rG   r   r   r   r   r   rZ  r[  r   r   r   r   drawr   r   rY   rs   _try_distill
gs_distillxpdf_distillr{   r   Zopen_file_cmr   codecs	getwriter)r   rx  ry  rh  ri  rd  rc  r  paper_widthpaper_heightllxllyurxuryrendererr  r   r   rz   r  r    r  r!   rw  h  sv    
";(,
zFigureCanvasPS._print_figurec                C   s  |dk}| j  \}	}
d}d}|}|}|| j jj }|| j jj }||||f}t | _t|	|
| j|d}t| j |	|
|||d}| j 	| t
 T}t|d}|jd| dt|d  dtt d	d
t dt|| dt|	d |
d  d| j  ddd |tju r(|
|	 }	}
||||f}|rD|| j  \}}n |dkrXt|	|
}t| \}}t||j|||j}tjd dkstjd rtt|||||d n$tjd dkrtt |||||d t!|| W d   n1 s0    Y  dS )a(  
        If :rc:`text.usetex` is True, a temporary pair of tex/eps files
        are created to allow tex to manage the text layout via the PSFrags
        package. These files are processed to yield the final ps or eps file.

        The rest of the behavior is as for `._print_figure`.
        r`  r   r  r~  r  z%!PS-Adobe-3.0 EPSF-3.0
r   z&
%%EndComments
%%BeginProlog
/mpldict z dict def
mpldict begin
r   z
end
%%EndProlog
mpldict begin
r:  r   z 0 0 clipbox
z
end
showpage
rn   ro   rg  r  r  r   )r  r   rotatedr  N)"rj  r  r   r   r   r   r   r   r   r  r   rl  r   
write_textr  r   r  rY   r[   r   rZ  r[  r\  rO   rG   _convert_psfragsr   r   r   r   r  r  r  r{   )r   rx  ry  rh  ri  rd  rc  r  r  r   r   r(  r)  r  r  r  r  r   r  r  r   tmppathr  r  psfrag_rotatedr    r    r!   rv    s~    






z FigureCanvasPS._print_figure_texr  r`  c                    s   | j   t  S r   )rj  Zdraw_without_renderingr   r  r   r   r    r!   r  L  s    
zFigureCanvasPS.draw)r#   r$   r%   Z	fixed_dpi	filetypesra  r   Zdelete_parameterr}  rw  rv  r   partialmethodZprint_psZ	print_epsr  rY  r    r    r   r!   r_  7  s    
+ Xr_  c                 C   s   t t| ||||S r   )r  rl  r   )r   psfragsZfont_preambleZcustom_preambler  r  rd  r    r    r!   convert_psfragsQ  s    r  c           
   	   C   s0  t dt jd t jd t jd t jd d||d  iF t jdd||d	krdd
nd|  	 d dd}W d   n1 s0    Y  t
 B}tj|d}tdddd||gt t||  W d   n1 s0    Y  t| }d|dv }	W d   n1 s"0    Y  |	S )aj  
    When we want to use the LaTeX backend with postscript, we write PSFrag tags
    to a temporary postscript file, each one marking a position for LaTeX to
    render some text. convert_psfrags generates a LaTeX document containing the
    commands to convert those tags to text. LaTeX/dvips produces the postscript
    file that includes the actual text.
    ztext.latex.preambler   Zgraphicxr   z:\geometry{papersize={%(width)sin,%(height)sin},margin=0in})r   r   zt
\begin{figure}
  \centering\leavevmode
  %(psfrags)s
  \includegraphics*[angle=%(angle)s]{%(epsfile)s}
\end{figure}r   r[  r  r   )r  r1  epsfile
   )r   Nr  Zdvipsz-qz-R0z-oZ	Landscapei  )r   Z
rc_contextr   Z
texmanagerZ_usepackage_if_not_loadedr   Zmake_dvirY   resolveas_posixr   r   r   r   _check_and_log_subprocessr   rt   rv   rs   read)
r  r  r  r  rd  Zdvifiler   psfilerz   r  r    r    r!   r  X  s@    


	
$*	
.r  c              
   O   sV   z| t |g|R i | W n2 tjyP } ztd| W Y d }~n
d }~0 0 d S )Nz%s.  Distillation step skipped.)r   r   ZExecutableNotFoundErrorr   r   )funcr  rZ   r   excr    r    r!   r    s    r  r>   c              
   C   s|   |r
d}nd| }| d }t jd }tt djddd| d	|d
| | gt t|  t	
||  |rxt| ||d dS )z
    Use ghostscript's pswrite or epswrite device to distill a file.
    This yields smaller files without illegal encapsulated postscript
    operators. The output is low-level, converting text to outlines.
    	-dEPSCropz-sPAPERSIZE=%sz.pszps.distiller.resgsz-dBATCHz	-dNOPAUSEz-r%dz-sDEVICE=ps2writez-sOutputFile=%sr  N)r   r   r   r  _get_executable_info
executabler   r   removert   rv   pstoeps)r   r`  r  r   r  Zpaper_optionr  rh  r    r    r!   r    s     



r  c                 C   s   t d t d t v}t|d}t|d}tddddd	d
|rLdnd| | |g	t tdddd||gt t	||  W d   n1 s0    Y  |rt
|  dS )a
  
    Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file.
    This yields smaller files without illegal encapsulated postscript
    operators. This distiller is preferred, generating high-level postscript
    output that treats text as text.
    r  Zpdftopsztmp.pdfr  Zps2pdfz-dAutoFilterColorImages#falsez-dAutoFilterGrayImages#falsez-sAutoRotatePages#Nonez-sGrayImageFilter#FlateEncodez-sColorImageFilter#FlateEncoder  z-sPAPERSIZE#%sz-papermatchz-level2N)r   r  r   rl  r   r   r  r   rt   rv   r  )r   r`  r  r   r  r   ZtmppdfZtmppsr    r    r!   r    s.    

	*r  c           	      C   sd   | \}}}}|r"d|| df }nd}d||t |t |f }d||||f }d||g|fS )zx
    Return a postscript header string for the given bbox lbrt=(l, b, r, t).
    Optionally, return rotate command.
    z%.2f %.2f translate
90 rotater   r   z%%%%BoundingBox: %d %d %d %dz)%%%%HiResBoundingBox: %.6f %.6f %.6f %.6fr   )r   rS  rY   )	Zlbrtr  rE   r   rm   trotate	bbox_infoZhires_bbox_infor    r    r!   r    s    r  c           
   	   C   sR  |rt ||d\}}nd\}}| d }t|d}t| d}|j}|D ]|}	|	drv|d |r||dd	  qH|	d
r||	 |d |r||dd	   qqH|r|	drqH||	 qH|D ],}	|	dr|d q|	drq||	 qW d   n1 s0    Y  W d   n1 s.0    Y  t|  t||  dS )z
    Convert the postscript to encapsulated postscript.  The bbox of
    the eps file will be replaced with the given *bbox* argument. If
    None, original bbox will be used.
    r  )NNz.epsr   rq   s   %!PSs   %!PS-Adobe-3.0 EPSF-3.0
rf      
s   %%EndCommentssp   %%BeginProlog
save
countdictstack
mark
newpath
/showpage {} def
/setpagedevice {pop} def
%%EndProlog
%%Page 1 1
)s   %%Bounds   %%HiResBounds   %%DocumentMedias   %%Pagess   %%EOFsJ   cleartomark
countdictstack
exch sub { end } repeat
restore
showpage
%%EOF
s   %%PageBoundingBoxN)	r  rs   r   rI   r>  r   r  rt   rv   )
r   r   r  r  r  r  ZepshZtmphr   liner    r    r!   r    s8    

	



J
r  )
z/_d { bind def } bind defz/m { moveto } _dz/l { lineto } _dz/r { rlineto } _dz/c { curveto } _dz/cl { closepath } _dz/ce { closepath eofill } _dzO/box {
      m
      1 index 0 r
      0 exch r
      neg 0 r
      cl
    } _dz6/clipbox {
      box
      clip
      newpath
    } _dz/sc { setcachedevice } _dc                   @   s   e Zd ZeZdS )
_BackendPSN)r#   r$   r%   r_  ZFigureCanvasr    r    r    r!   r  T  s   r  )Fr>   NF)Fr>   NF)F)NF)TrW  r  ro  enumr   r   ior   loggingr   rl  rh   rt   tempfiler   rs  numpyr   Z
matplotlibr   r   r   r   r   Zmatplotlib._afmr	   Zmatplotlib.backend_basesr
   r   r   r   Zmatplotlib.cbookr   r   Zmatplotlib.font_managerr   Zmatplotlib.ft2fontr   r   Zmatplotlib._ttconvr   Zmatplotlib._mathtext_datar   Zmatplotlib.pathr   Zmatplotlib.texmanagerr   Zmatplotlib.transformsr   Z!matplotlib.backends.backend_mixedr   r   r   	getLoggerr#   r   Zbackend_versionr   r   Zps_backend_helperrG   rO   r[   
deprecatedrl   r{   r   r   r   ZRendererPDFPSBaser   rZ  r_  r  r  r  r  r  r  r  ZFigureManagerPSr  exportr  r    r    r    r!   <module>   s   
	
Q&    7  
1
"


=%