o
    i                  
   @   s  d Z ddlZddlmZ ddlZddlmZ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 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mZmZmZmZmZ dd	lm Z 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-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 e7e8Z9dZ:dZ;G dd dZ<e< Z=i 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d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBZ>dCdD Z?dEdF Z@dGdH ZAdIdJ ZBdKdL ZCG dMdN dNe6jDZEdOdP ZFG dQdR dReZGG dSdT dTeZHG dUdV dVeZIdWdX ZJdYdZ ZKdgd\d]ZLdgd^d_ZMdhd`daZNdidbdcZOeZPg ddZQejRG dedf dfeZSdS )jzG
A PostScript backend, which can produce both PostScript .ps and .eps.
    N)Enum)StringIOTextIOWrapper)TemporaryDirectory)cbook_path)_text_layout)_Backend_check_savefig_extra_argsFigureCanvasBaseFigureManagerBaseGraphicsContextBaseRendererBase)is_writable_file_likefile_requires_unicode)is_opentype_cff_fontget_font)LOAD_NO_HINTING)convert_ttf_to_ps)MathTextParser)	uni2type1)Path)
TexManager)Affine2D)MixedModeRenderer   )_backend_pdf_pszLevel IIc                   @      e Zd Zdd ZdS )PsBackendHelperc                 C   s
   i | _ d S N)_cachedself r#   Y/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/backends/backend_ps.py__init__.   s   
zPsBackendHelper.__init__N)__name__
__module____qualname__r%   r#   r#   r#   r$   r   -       r   letter)      !@   legal)r+      ledger)r,      a0)Gz@@gHzgG@a1)p=
c7@r2   a2)
ףp=0@r4   a3)zGa'@r6   a4)
ףp= @r8   a5)RQ@r:   a6)Q@r<   a7)HzG@r>   a8)g(\ @r@   a9)Zd;O?gffffff @a10)gRQ?rC   b0)fffffFD@g)\L@b1))\<@rF   b2)QE4@rH   ))\,@rJ   )Q8$@rK   )p=
ף@rL   ))\(@rM   )p=
ף@rN   )Gz@rO   ))\(?rP   )g)\(?rQ   )b3b4b5b6b7b8b9b10c                 C   sF   t t ddD ]\}\}}|drq| |k r ||k r |  S qdS )NT)reverselr1   )sorted	papersizeitems
startswith)whkeypwphr#   r#   r$   _get_papertypeP   s   
re   c                 C   sF   t | tr| S t| }| |krt|S d|  }|d}|d}|S )Nz%1.3f0.)
isinstancestrintrstrip)valivalsr#   r#   r$   _num_to_strY   s   


ro   c                  G   s   d tt| S )N )joinmapro   )argsr#   r#   r$   _nums_to_strg   s   rt   c                 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>t   s    z!quote_ps_string.<locals>.<lambda>ascii)replaceresubdecode)rn   r#   r#   r$   quote_ps_stringk   s   
r   c                 C   st   t |r/t|rt| dddnt| d}| t|| W d   dS 1 s(w   Y  dS 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_streamx   s   "r   c                       s:  e Zd ZdZedZdZd= fdd	Ze	de
dd	 Ze	dd
d Ze	ddd Zd>ddZd>ddZ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#d$Zd%d& Zd?d'd(Z	d?d)d*Zd+d, Zedd-dAd/d0Zd@d1d2Zd3d4 Zd5d6 Z d7d8 Z!d9d: Z"dBd;d<Z#  Z$S )C
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| _d S )Ntext.usetexr   r   PS)superr%   	_pswritermplrcParamstextcntpsfragimagedpicolor	linewidthlinejoinlinecaplinedashfontnamefontsize_hatchesimage_magnification_clip_paths_path_collection_idr   CharacterTracker_character_trackerr   mathtext_parser)r"   widthheightpswriterr   	__class__r#   r$   r%      s&   


zRendererPS.__init__z3.3c                 C   s   | j jS r   )r   used_charactersr!   r#   r#   r$   r      s   zRendererPS.used_charactersc                 O   s   | j j|i | dS )z;Keep track of which characters are required from each font.N)r   trackr"   rs   kwargsr#   r#   r$   track_characters   s   zRendererPS.track_charactersc                 O   s   | j j|i | d S r   )r   merger   r#   r#   r$   merge_used_characters   s   z RendererPS.merge_used_charactersTc                 C   sd   |||f| j kr.||kr||kr| jd|  n| jd|||f  |r0|||f| _ d S d S d S )Nz%1.3f setgray
z%1.3f %1.3f %1.3f setrgbcolor
)r   r   write)r"   r   gbstorer#   r#   r$   	set_color   s   zRendererPS.set_colorc                 C   s8   t |}|| jkr| jd|  |r|| _d S d S d S )Nz%1.3f setlinewidth
)floatr   r   r   )r"   r   r   r#   r#   r$   set_linewidth   s   

zRendererPS.set_linewidthc                 C   0   || j kr| jd|  |r|| _ d S d S d S )Nz%d setlinejoin
)r   r   r   )r"   r   r   r#   r#   r$   set_linejoin      

zRendererPS.set_linejoinc                 C   r   )Nz%d setlinecap
)r   r   r   )r"   r   r   r#   r#   r$   set_linecap   r   zRendererPS.set_linecapc                 C   s|   | j d ur| j \}}t||r||krd S |d ur-t|r-dt| |f }| j| n| jd |r<||f| _ d S d S )Nz[%s] %d setdash
z[] 0 setdash
)r   nparray_equallenrt   r   r   )r"   offsetseqr   oldooldseqrn   r#   r#   r$   set_linedash   s   

zRendererPS.set_linedashc                 C   sV   t jd rd S ||f| j| jfkr'd||f }| j| |r)|| _|| _d S d S d S )Nr   z%/%s findfont
%1.3f scalefont
setfont
)r   r   r   r   r   r   )r"   r   r   r   outr#   r#   r$   set_font   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 drp   z]
     /XStep z
     /YStep z(

     /PaintProc {
        pop
        fz setlinewidth
Fsimplifyz_
        gsave
        fill
        grestore
        stroke
     } bind
   >>
   matrix
   0.0 z translate
   makepattern
   /z
 exch def
)r   r   r   r   r   r   r   _convert_pathr   hatchr   scale)r"   r   sidelennamer   
pageheightr#   r#   r$   create_hatch   s6   






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_magnificationNc                    s  |j d d \}}d}|d d dd d d df  d    d fddttt  D }	|d u rHd	}
|| j }|| j }nd
t	t
|  }
d}d}| }| \}}g }|d urx|dtg |j|jR    |d ur| ||}|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 )N   zfalse 3 colorimage      
c                    s$   g | ]} | |d    qS )r   r#   ).0ndatancharsr#   r$   
<listcomp>'  s    z)RendererPS.draw_image.<locals>.<listcomp>z1 0 0 1 0 0rp   g      ?
%s clipboxz%s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hexrq   rangemathceilr   r   rr   ri   frozen	to_valuesget_clip_rectangleget_clip_pathappendrt   sizep0_get_clip_pathr   r   )r"   gcr|   yim	transformra   r`   imagecmdhexlinesmatrixxscaleyscalebboxclippathclippath_transclipidr#   r   r$   
draw_image  sn   
 

zRendererPS.draw_imageFc              
   C   sD   |rdd| j d | jd f}nd }t||||d dg dddS )Ng        g      R@   )   m   l       cs   clTr~   )r   r   r   convert_to_stringr   )r"   pathr   r  r   r#   r#   r$   r   P  s   

zRendererPS._convert_pathc                 C   sf   |t |f}| j|}|d u r1dt| j }| j||dd}| jd| d| d || j|< |S )Nzc%xFr   /z {
z
clip
newpath
} bind def
)r  r   getr   r   r   r   )r"   r   clippath_transformrb   pidclippath_bytesr#   r#   r$   r   Y  s   

zRendererPS._get_clip_pathc                 C   sB   |d u o	|  d u }|jo|}| j||||d}| ||| d S )Nr  r   )get_hatch_pathshould_simplifyr   _draw_ps)r"   r   r  r   rgbFacer  r   psr#   r#   r$   	draw_pathj  s   
zRendererPS.draw_pathc                 C   s  t r| jd t|rd n|d |d   kr|d kr&n nd|d  nd|d d  }g d}| }	| sBt| dkrF| n| d }
|	dkoS|
dk}|rs|	d	|	  |
 }|	d
|  | }|	d|  |	| j||dd |r|r|	d |r||dg |r|	d |r|	d |ddg |j|dd| jd | jd fddD ]\}}t|r|dd  \}}|	d||f  qd|}| j|||ddd d S )Nz% draw_markers 
r   r   r   z%1.3f setgrayz%1.3f %1.3f %1.3f setrgbcolorr   )z/o {gsavenewpath	translatez%.1f setlinewidthz%d setlinejoinz%d setlinecapFr   r  fillgrestorestrokez
} bind defr   r  z%g %g or   )r  r  )debugPSr   r   _is_transparentget_linewidthget_forced_alphar   get_rgb	get_alphar   get_joinstyleget_capstyler   extenditer_segmentsr   r   rq   r  )r"   r   marker_pathmarker_transr  transr  ps_colorps_cmdlwalphar  jintcintverticescoder|   r   r  r#   r#   r$   draw_markersq  s^   $





zRendererPS.draw_markersc                 C   s:  t |dkrt |d jnd}| |||||}|d|  d |d | k }|s:t| |||||||||	|
|||S g }t| |||D ]'\}\}}d| j|f }| j||dd}| j	
d| d| d	 || qE| |||||||||	|
|||D ]\}}}}}d
|||f }| ||| q~|  jd7  _d S )Nr   r   r   zp%x_%xFr   r  z {
newpath
translate
z
} bind def
z%g %g %sr   )r   r2  _iter_collection_uses_per_pathr   draw_path_collection	enumerate_iter_collection_raw_pathsr   r   r   r   r   _iter_collectionr  )r"   r   master_transformpathsall_transformsoffsetsoffsetTrans
facecolors
edgecolors
linewidths
linestylesantialiasedsurlsoffset_positionlen_pathuses_per_pathshould_do_optimization
path_codesir  r   r   
path_bytesxoyopath_idgc0r  r  r#   r#   r$   r6    sB   	




zRendererPS.draw_path_collectionismathTeX!c	              
   C   s8  t | dstd | ||||||d| d S | j||dd\}	}
}| }d| j }d| d d  }d	d
dt	j
d d d}|| }d||f }d}tt	j
drnt|| |}| jd||||d |f  n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)rP  z
psmarker%dz%1.3f,%1.3f,%1.3fr   z{\sffamily %s}z{\ttfamily %s})z
sans-serif	monospacezfont.familyr   z{\rmfamily %s}z\color[rgb]{%s} %sztext.latex.previewz/\psfrag{%s}[Bl][Bl][1][%f]{\fontsize{%f}{%f}%s}g      ?z/\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}r   z	 moveto
(z)
show
grestore
r   )hasattr_logwarning	draw_textget_text_width_height_descentget_size_in_pointsr   r#  r  r   r   dict__getitem__rt   r   r   r   r   )r"   r   r|   r   rn   propanglerP  mtextr`   ra   blr   thetextr   fontcmdtexcorrposr#   r#   r$   draw_tex  sN   


zRendererPS.draw_texc	                    s<  t r| jd t| rd S |dkr| ||||||S |r*| ||||||S tjd r| j	|   | 
|   }	| }
d|
 }d} |d | }d }g }|D ]M}tt|dt|d}z |}W n ty   d	} d
}Y nw |d ur ||}nd}|}||| 7 }|d|||f  ||| 7 }qXd|}| jd|	 d|
 d|dd|dd|dd| d d S | |  j|dtd | j | | j	|    jddd}| ||  d fddt !| D }| jd|dd|dd|dd| d	 d S )Nz% text
rR  r   gMbP?r      uni04Xquestion?z%f %f m /%s glyphshowr   zgsave
/z
 findfont
z scalefont
setfont
r   rp    translate
 rotate
r   )flagsr~   r   c                 3   s&    | ]\}}d |  |f V  qdS )z%f 0 m /%s glyphshowN)get_glyph_name)r   	glyph_idxr|   fontr#   r$   	<genexpr>F  s
    
z'RendererPS.draw_text.<locals>.<genexpr>r   )"r  r   r   r   r#  re  draw_mathtextr   r   r   _get_font_afmget_fontnamerY  get_str_bbox_and_descentr   r  rz   get_width_from_char_nameKeyErrorget_width_charget_kern_dist_from_namer   rq   _get_font_ttfset_textr   r   r   postscript_nameencoder   r   r   layout)r"   r   r|   r   rn   r\  r]  rP  r^  r   r   r   thisxthisy	last_namelinescr   r   kernr`  ps_namer#   rp  r$   rW    s   




zRendererPS.draw_textc                 C   s   t  S r   )GraphicsContextPSr!   r#   r#   r$   new_gcQ  s   zRendererPS.new_gcc                 C   s|   t r| jd | j|d|\}}}	}
}| j| | j|   |
	 }| jd|dd|dd|dd| d	 d	S )
z-Draw the math text using matplotlib.mathtext.z% mathtext
r   r   r   rp   rk  rl  r   N)
r  r   r   r   parser   r   r   r#  getvalue)r"   r   r|   r   rn   r\  r]  r   r   descentr   r   r`  r#   r#   r$   rs  U  s    zRendererPS.draw_mathtextc                 C   s    |  ||d|d| d S )N)r   r   r   )r   r   rf  )draw_gouraud_trianglesreshape)r"   r   pointscolorsr+  r#   r#   r$   draw_gouraud_triangleg  s   
z RendererPS.draw_gouraud_trianglec                 C   s  t |t |ks
J |jdksJ |jd dksJ |jd dks#J |jdks*J |jd dks3J |jd dks<J |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< t
| }| jd|dd|dd|dd|dd| d d S )Nr   r   r   rf  r   )axisi   l    ))rm  u1)r  z2>u4)r  3u1)dtyperm  r  g     o@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   r  r   r   minmaxr   emptyr   r   r   r   )r"   r   r  r  r+  r   flat_pointsflat_colors
points_min
points_maxfactorxminyminxmaxymax	streamarrstreamr#   r#   r$   r  k  sF   
	z!RendererPS.draw_gouraud_trianglesc                 C   s  | j j}tr|r|d| d  | dkot|  }|s!d}t|r'd}| }	|rX| |  | }
| 	|
 |
 }| | | j|   | j| dd   |d | }|ro|dg |j|jR   | \}}|r| ||}|d	|  ||  |d |r|s|	r|d | j|dd d
di |d |s|	r|d |	r| |	}|d |d| dd   |d|  |r|d |d dS )av  
        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.
        z% r   r   FNr   r   z %1.4g %1.4g %1.4g %1.4g clipbox
z%s
r   zfill
z	grestore
z	%f %f %f z%s setpattern fill grestore
zstroke
)r   r   r  r!  r   r#  	get_hatchr   r%  r   r&  r   r   
get_dashesr   r   r   r   r   r   stripr   get_hatch_color)r"   r  r   r  r  r  commandr   mightstroker   r0  r1  cliprectr   r  r  
hatch_namer#   r#   r$   r    s\   


zRendererPS._draw_ps)r   )Tr   )FN)rQ  N)TTN)%r&   r'   r(   __doc__r   _get_data_path_afm_font_dir_use_afm_rc_namer%   
deprecatedpropertyr   r   r   r   r   r   r   r   r   r   r   r  r   r   r  r4  r6  _delete_parameterre  rW  r  rs  r  r  r  __classcell__r#   r#   r   r$   r      sF    









"

5	

9
,
,N+r   c                 C   sF   | d u rdS t | dkr!| d dkrdS | d dkrtd dS dS )NTrf  r   r   r   zlThe PostScript backend does not support transparency; partially transparent artists will be rendered opaque.F)r   rU  rV  )rgb_or_rgbar#   r#   r$   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )r  c                 C      ddddt |  S )Nr   r   r   )buttround
projecting)r   r&  r!   r#   r#   r$   r&       
zGraphicsContextPS.get_capstylec                 C   r  )Nr   r   r   )miterr  bevel)r   r%  r!   r#   r#   r$   r%    r  zGraphicsContextPS.get_joinstyleN)r&   r'   r(   r&  r%  r#   r#   r#   r$   r    s    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_landscape  s   z_Orientation.swap_if_landscapeN)r&   r'   r(   r   portraitr  r  r#   r#   r#   r$   r    s    r  c                   @   s   e Zd ZdZdddZdd Zdd Zd	d
 ZdddddddZe	e
dddddddZe	e
dddddddZdS )FigureCanvasPSr   
PostscriptzEncapsulated Postscript)r  epsc                 C   s   dS Nr  r#   r!   r#   r#   r$   get_default_filetype     z#FigureCanvasPS.get_default_filetypec                 O      | j |dg|R i |S r  	_print_psr"   outfilers   r   r#   r#   r$   print_ps     zFigureCanvasPS.print_psc                 O   r  )Nr  r  r  r#   r#   r$   	print_eps  r  zFigureCanvasPS.print_epsNr  )dpimetadata	papertypeorientationc                O   s  | j d i }	t|ttjfrt|ddd|	d< |p!i 	ddt
j d|	d< td}
|
r?tjt|
d	nt |	d
< ddd |	 D }	|d u rZt
jd }| }tjdgt|d tjt| d}t
jd ry| jn| j}|||f||	||d| d S )Nr   r~   r   TitleCreatorzmatplotlib version z, http://matplotlib.org/SOURCE_DATE_EPOCHz%a %b %d %H:%M:%S %YCreationDater   c                 s   s$    | ]\}}d | d| V  qdS )z%%z: Nr#   )r   kvr#   r#   r$   rr    s    
z+FigureCanvasPS._print_ps.<locals>.<genexpr>zps.papersizeauto)r  )r  r   )r  dsc_commentsr  r  )figureset_dpirh   ri   osPathLikefspathr~  r   r  r   __version__getenvdatetimeutcfromtimestamprj   strftimetimectimerq   r^   r   lowerr   _check_in_listr]   _check_getitemr  _print_figure_tex_print_figure)r"   r  formatr  r  r  r  rs   r   r  source_date_epochprinterr#   r#   r$   r    sH   



zFigureCanvasPS._print_psz3.2dryrunF)r  bbox_inches_restorec                   s   |dkt |ttjfrt|}d}	nt|rd}	ntdj \	dkr3t	
	f 
t \}
}tjd rZ	|
ksI|krZt	
	 
t \}
}d|
	  
d|  
}}|jjj }|jjj }dtju r||||f\}}}}d	|  

d
||||f |rG dd d}| _nt _t	j|dtj	||d}j| |rdS  	
fdd}tjd rCt T}tj|d}t|ddd}|| W d   n	1 sw   Y  tjd dkrtt| d ntjd dkr+tt| d t|| W d   dS 1 s<w   Y  dS |	rat |}|sYt!|dd}dd |_"n|}|| dS t|ddd}|| W d   dS 1 syw   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  FTz,outfile must be a path or a file-like objectr  ps.usedistillerg      B@r   r   Z   c                   @   r   )z0FigureCanvasPS._print_figure.<locals>.NullWriterc                 _      d S r   r#   r   r#   r#   r$   r   b  r  z6FigureCanvasPS._print_figure.<locals>.NullWriter.writeNr&   r'   r(   r   r#   r#   r#   r$   
NullWritera  r)   r  r   r  Nc                    s4  r	t d| d nt d dd| d t  dj dd	  d
d  d
d  d
d  dd| d tt}t d| d tjd sM|tjj7 }t d| | d t d| d t dt| d tjd sjj	 D ]F\}}|svqot
|  fdd|D }tjd }t|dkrd}t|rtd|   ztt|| || W qo ty   td  w t d| d t d| d st 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 st d#| d |   d S )$Nz%!PS-Adobe-3.0 EPSF-3.0)filez%%!PS-Adobe-3.0
%%DocumentPaperSizes: z
%%Pages: 1
 )endr  z
%%Orientation: 
%%BoundingBox: r   rp   r   r   r   z
%%EndComments
z%%BeginPrologr   z/mpldict %d dict defzmpldict beginr   c                    s   g | ]}  |qS r#   )get_char_index)r   r  rp  r#   r$   r     s    zKFigureCanvasPS._print_figure.<locals>.print_figure_impl.<locals>.<listcomp>zps.fonttype   *   zxOpenType CFF fonts can not be saved using the internal Postscript backend at this time; consider using the Cairo backendzDThe PostScript backend does not currently support the selected font.r  z%%EndPrologz%%Page: 1 1z%s translatez	%d rotater   r   showpagez%%EOF)printr   r   psDefsr   r   r   usedrq   r^   r   r   RuntimeErrorflushr   r  fsencoderU  rV  rt   r   r  )r   Ndict	font_pathchars	glyph_idsfonttyper   r  r   is_epsr  r  ps_rendererrotationr"   r   rL  rM  rp  r$   print_figure_implt  s   






z7FigureCanvasPS._print_figure.<locals>.print_figure_impltmp.psr`   r   r   ghostscript)ptyper   xpdfc                   S   r  r   r#   r#   r#   r#   r$   r}     s    z.FigureCanvasPS._print_figure.<locals>.<lambda>)#rh   ri   r  r  r  r   
ValueErrorr  get_size_inchesre   r  r]   r   r   r   r   r   r  r  r   r   r   r   drawr   r  rq   r   _try_distill
gs_distillxpdf_distillr   r   r   close)r"   r  r  r  r  r  r  r  r  passed_in_file_objectpaper_widthpaper_heightllxllyurxuryr  rendererr  tmpdirtmpfiler   requires_unicoder#   r
  r$   r  +  s   




"H
$
$zFigureCanvasPS._print_figurec                C   sV  |dk}	| j  \}
}d}d}|}|}|| j jj }|| j jj }||||f}|r5G dd d}| | _nt | _t|
|| j|d}t| j |
||||d}| j 	| |rWdS t
 }tj|d}t|jd	| d
|d  d|d  d|d  d|d  dtt ddt dt|| dt|
d |d  d| j  ddd |tju r||
}
}||||f}|	r|| j  \}}n|dkrt|
|}t| \}}| }| }| }t||j|||||j}t j!d dkst j!d rt"t#||	|||d nt j!d dkrt"t$||	|||d t%|| W d   dS 1 s$w   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   c                   @   r   )z4FigureCanvasPS._print_figure_tex.<locals>.NullWriterc                 _   r  r   r#   r   r#   r#   r$   r     r  z:FigureCanvasPS._print_figure_tex.<locals>.NullWriter.writeNr  r#   r#   r#   r$   r    r)   r  r  r  Nr  z%!PS-Adobe-3.0 EPSF-3.0
r  rp   r   r   r   z&
%%EndComments
%%BeginProlog
/mpldict z dict def
mpldict begin
r  z
end
%%EndProlog
mpldict begin
rk  r   z 0 0 clipbox
z
end
showpage
r   r   r  r  r  r   )r  r   rotatedr  )&r  r  r   r   r   r   r   r   r   r  r   r  r  rq   pathlibr   
write_textr   r   rt   r  r  r  r  re   r]   get_texmanagerget_font_preambleget_custom_preambleconvert_psfragsr   r   r   r   r  r  r  r   )r"   r  r  r  r  r  r  r  r  r  r   r   rL  rM  r  r  r  r   r   r  r  r!  r"  r#  r  r  
texmanagerfont_preamblecustom_preamblepsfrag_rotatedr#   r#   r$   r    s   





$z FigureCanvasPS._print_figure_tex)r&   r'   r(   	fixed_dpi	filetypesr  r  r  r  r
   r   r  r  r  r#   r#   r#   r$   r    s&    &
 1
r  c              	   C   s  t dt jd d||d  i& t jdd||dkr dndt|  	 d	 d
d}W d   n1 s:w   Y  t
 !}tj|d}	tdddd|	|gt t|	|  W d   n1 sfw   Y  t| }
d|
dv }W d   |S 1 sw   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.preamblez\usepackage{psfrag,color}
\usepackage[dvips]{graphicx}
\geometry{papersize={%(width)sin,%(height)sin},body={%(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   )psfragsr]  epsfile
   )r   Nr  dvipsz-qz-R0z-o	Landscapei  )r   
rc_contextr   r   make_dvirq   r&  r   resolveas_posixr   r  r  r   _check_and_log_subprocessrU  r   r   r   read)r#  r2  r-  r.  r  r  r  dvifiler"  psfiler   r/  r#   r#   r$   r+  H  s>   		

r+  c              
   O   sJ   z
| |i | W d S  t jy$ } ztd| W Y d }~d S d }~ww )Nz%s.  Distillation step skipped.)r   ExecutableNotFoundErrorrU  rV  )funcrs   r   excr#   r#   r$   r  z  s   r  Fc              
   C   s   |rd}nd| }| d }t jd }tt djddd| d	|d
| | gt t|  t	
||  |r>t| ||d dS 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=%s.pszps.distiller.resgsz-dBATCHz	-dNOPAUSEz-r%dz-sDEVICE=ps2writez-sOutputFile=%sr%  N)r   r   r   r;  _get_executable_info
executablerU  r  remover   r   pstoeps)r#  r  r  r   r%  paper_optionr>  r  r#   r#   r$   r    s"   



r  c                 C   s   t d t d | d }| d }tddddd	d
|rdnd| | |g	t tdddd||gt t|  t||  |rDt	|  t

| d D ]}t| qKd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.
    rD  pdftopsz.pdfrC  ps2pdfz-dAutoFilterColorImages#falsez-dAutoFilterGrayImages#falsez-sAutoRotatePages#Nonez-sGrayImageFilter#FlateEncodez-sColorImageFilter#FlateEncoderB  z-sPAPERSIZE#%sz-papermatchz-level2z.*N)r   rF  r   r;  rU  r  rH  r   r   rI  glob)r#  r  r  r   r%  pdffiler>  fnamer#   r#   r$   r    s4   

	
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   r   rq   )	lbrtr%  r[   r   r   trotate	bbox_infohires_bbox_infor#   r#   r$   get_bbox_header  s   rV  c           
   	   C   sL  |rt ||d\}}nd\}}| d }t|dy}t| dd}|j}|D ]=}	|	dr;|d |r:||dd	  q$|	d
rU||	 |d |rS||dd	   n|r]|	dr]q$||	 q$|D ]}	|	drp|d qd|	drvqd||	 qdW d   n1 sw   Y  W d   n1 sw   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.
    rE  )NNz.epswbr   s   %!PSs   %!PS-Adobe-3.0 EPSF-3.0
r~      
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)	rV  r   r   r_   r~  r  rH  r   r   )
r#  r   r%  rT  rS  r3  epshtmphr   liner#   r#   r$   rI    sB   

	




 
+rI  )z/m { moveto } bind defz/l { lineto } bind defz/r { rlineto } bind defz/c { curveto } bind defz/cl { closepath } bind defzU/box {
      m
      1 index 0 r
      0 exch r
      neg 0 r
      cl
    } bind defz</clipbox {
      box
      clip
      newpath
    } bind defc                   @   s   e Zd ZeZdS )
_BackendPSN)r&   r'   r(   r  FigureCanvasr#   r#   r#   r$   r\  B  s    r\  )Fr*   NF)F)NF)Tr  r  enumr   rN  ior   r   loggingr   r  r&  r   r   tempfiler   r  numpyr   
matplotlibr   r   r   r   matplotlib.backend_basesr	   r
   r   r   r   r   matplotlib.cbookr   r   matplotlib.font_managerr   r   matplotlib.ft2fontr   matplotlib._ttconvr   matplotlib.mathtextr   matplotlib._mathtext_datar   matplotlib.pathr   matplotlib.texmanagerr   matplotlib.transformsr   !matplotlib.backends.backend_mixedr   r  r   	getLoggerr&   rU  backend_versionr  r   ps_backend_helperr]   re   ro   rt   r   r   RendererPDFPSBaser   r   r  r  r  r+  r  r  r  rV  rI  FigureManagerPSr   exportr\  r#   r#   r#   r$   <module>   s     
	
	    S
  S2

"
%
=