o
    j                  
   @   s  d 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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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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* 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: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+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>Z;d?d@dAdBZ<dCdD Z=dEdF Z>dGdH Z?dIdJ Z@dKdL ZAdMdN ZBdOdP ZCdQdR ZDdSdT ZEG dUdV dVe6jFZGG dWdX dXeZHG dYdZ dZe!ZId[d\ ZJd]d^ ZKdod_d`ZLdodadbZMeNdcdpdddeZOdfdg ZPdhdi ZQdqdjdkZRe"ZSg dlZTe jUG dmdn dne ZVdS )rzG
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)	LoadFlags)	uni2type1)Path)
TexManager)Affine2D)MixedModeRenderer   )_backend_pdf_psFletter)      !@   Zlegal)r      ledger)r      Za0)Gz@@gHzgG@Za1)p=
c7@r   Za2)
ףp=0@r    a3)zGa'@r!   Za4)
ףp= @r#   Za5)RQ@r$   Za6)Q@r%   Za7)HzG@r&   Za8)ffffff @r'   Za9)\(\?r(   Za10)gRQ?r)   Zb0)fffffFD@g)\L@b1))\<@r*   b2)QE4@r,   ))\,@r.   )Q8$@r/   )p=
ף@r0   ))\(@r1   )p=
ף@r2   )Gz@r3   ))\(?r4   )g)\(?r5   )Zb3Zb4Zb5Zb6Zb7Zb8Zb9Zb10 )sepc                 G   s   |  dd |D S )Nc                 s   s$    | ]}|d  d dV  qdS )1.3f0.N)rstrip).0arg r>   _/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/matplotlib/backends/backend_ps.py	<genexpr>I   s   " z_nums_to_str.<locals>.<genexpr>)join)r7   argsr>   r>   r?   _nums_to_strH   s   rC   c                 C   sr   t |r.t|rt| 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.
    latin-1encodingrbN)copy_function)r   r   openshutilcopyfileobjmovecopyfile)srcdstfhr>   r>   r?   _move_path_to_path_or_streamL   s   "rQ   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
j}  \}}	|d |dtt|jdg|jd tt|d |	dddddg ddd d  q8|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r<   cfontr>   r?   
<listcomp>n   s    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
r6   c                 3   s     | ]}d   | V  qdS )/N)get_glyph_name)r<   glyph_idrU   r>   r?   r@      s    z$_font_to_ps_type3.<locals>.<genexpr>)Z	font_nameZinv_units_per_embboxrF   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   l       cr`   Tasciizce} _d
)r   formatpostscript_nameZunits_per_EMrA   mapstrr[   lenZ
load_glyphr   ZNO_SCALEget_pathappendrY   ZhoriAdvancer   convert_to_stringr   decode)
	font_pathcharsZ	glyph_idspreambleZ	postambleentriesrZ   gvrT   r>   rU   r?   _font_to_ps_type3\   s>   
rs   c              	   C   s  d dd |D }td| | zgi }| drd|d< tjj| fi |G}t| |&}t	|
 }td| t| jt| |t||| W d	   n1 sWw   Y  W d	   W d	S W d	   W d	S 1 sqw   Y  W d	S  ty   td
|   w )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 N)chrrS   r>   r>   r?   r@      s    z%_font_to_ps_type42.<locals>.<genexpr>zSUBSET %s characters: %sz.ttcr   Z
fontNumberzSUBSET %s %d -> %dNzIThe PostScript backend does not currently support the selected font (%s).)rA   _logdebugendswith	fontToolsZttLibZTTFontr   Zget_glyphs_subsetZfont_as_filegetvalueosstatst_sizerh   write_serialize_type42RuntimeErrorwarning)rm   rn   rP   Z
subset_strkwrV   subsetfontdatar>   r>   r?   _font_to_ps_type42   s0   

Vr   c           	      C   s   t | d|\}}| d }| d }t|}t|||}td|d  d|d  d| d	 jd
d|d d|d d|d d|d d|j d|j	rQdnd d|j
 d|j dtt|   d| d| dS )aw  
    Output a PostScript Type-42 format representation of font

    Parameters
    ----------
    font : fontTools.ttLib.ttFont.TTFont
        The original font object
    subset : fontTools.ttLib.ttFont.TTFont
        The subset font object
    fontdata : bytes
        The raw font data in TTF format

    Returns
    -------
    str
        The Type-42 formatted font
    locapostr\   z
        %%!PS-TrueTypeFont-r   r:   r   -headz.7fzi
        10 dict begin
        /FontType 42 def
        /FontMatrix [1 0 0 1 0 0] def
        /FontName /   z; def
        /FontInfo 7 dict dup begin
        /FullName (   z) def
        /FamilyName (z) def
        /Version (   z) def
        /ItalicAngle z def
        /isFixedPitch truefalsez  def
        /UnderlinePosition z! def
        /UnderlineThickness zX def
        end readonly def
        /Encoding StandardEncoding def
        /FontBBox [z=] def
        /PaintType 0 def
        /CIDMap 0 def
        z	
        z9
        FontName currentdict end definefont pop
        )_version_and_breakpointsget_generate_charstrings_generate_sfntstextwrapdedentZfontRevisionZgetDebugNameZitalicAngleZisFixedPitchZunderlinePositionZunderlineThicknessrC   _bounds)	rV   r   r   versionbreakpointsr   r\   rn   Zsfntsr>   r>   r?   r      sD   	


r   c              	      s   t d|dd \}}}||f}i  t|D ]!}t d|d|d  d|d d   \}}}	}|	 |d< q| durL fd	d
| jdd D }
nt }
th   |
t|}||fS )a  
    Read the version number of the font and determine sfnts breakpoints.

    When a TrueType font file is written as a Type 42 font, it has to be
    broken into substrings of at most 65535 bytes. These substrings must
    begin at font table boundaries or glyph boundaries in the glyf table.
    This function determines all possible breakpoints and it is the caller's
    responsibility to do the splitting.

    Helper function for _font_to_ps_type42.

    Parameters
    ----------
    loca : fontTools.ttLib._l_o_c_a.table__l_o_c_a or None
        The loca table of the font if available
    fontdata : bytes
        The raw data of the font

    Returns
    -------
    version : tuple[int, int]
        A 2-tuple of the major version number and minor version number.
    breakpoints : list[int]
        The breakpoints is a sorted list of offsets into fontdata; if loca is not
        available, just the table boundaries.
    z>3hNr   z>4sIII      r   rb   c                    s   h | ]} d  | qS )Zglyfr>   )r<   offsetZtablesr>   r?   	<setcomp>$  s    z+_version_and_breakpoints.<locals>.<setcomp>)	structunpackrangerl   	locationssetsortedvaluesrh   )r   r   Zv1Zv2Z	numTablesr   itag_r   Zglyf_breakpointsr   r>   r   r?   r     s   0r   c                 C   s>   |  d}tjj|}| D ]	}|| | q|jpdS )as  
    Compute the font bounding box, as if all glyphs were written
    at the same start position.

    Helper function for _font_to_ps_type42.

    Parameters
    ----------
    font : fontTools.ttLib.ttFont.TTFont
        The font

    Returns
    -------
    tuple
        (xMin, yMin, xMax, yMax) of the combined bounding box
        of all the glyphs in the font
    F)r   r   r   r   )ZgetGlyphSetrz   ZpensZ	boundsPenZ	BoundsPenkeysdrawZbounds)rV   gspenr\   r>   r>   r?   r   ,  s
   

r   c                 C   sL   |   }dt| d}t|D ]\}}|d| d| d7 }q|d7 }|S )a  
    Transform font glyphs into CharStrings

    Helper function for _font_to_ps_type42.

    Parameters
    ----------
    font : fontTools.ttLib.ttFont.TTFont
        The font

    Returns
    -------
    str
        A definition of the CharStrings dictionary in PostScript
    z/CharStrings z dict dup begin
rX   r6   z def
zend readonly def)ZgetGlyphOrderrh   	enumerate)rV   gosr   r\   r>   r>   r?   r   E  s   r   c                    s   d d}|t | k r6t||d }||d  }||kr |d } d| ||   d7  |}|t | k s
 d7  d	 fd
dtdt  dD S )a  
    Transform font data into PostScript sfnts format.

    Helper function for _font_to_ps_type42.

    Parameters
    ----------
    fontdata : bytes
        The raw data of the font
    font : fontTools.ttLib.ttFont.TTFont
        The fontTools font object
    breakpoints : list
        Sorted offsets of possible breakpoints

    Returns
    -------
    str
        The sfnts array for the font definition, consisting
        of hex-encoded strings in PostScript format
    z/sfnts[r   i  r   r   <z00>z]defrc   c                 3   s     | ]} ||d   V  qdS )d   Nr>   )r<   r   r   r>   r?   r@   }  s    z"_generate_sfnts.<locals>.<genexpr>r   )rh   bisectbisect_lefthexrA   r   )r   rV   r   posr   Znewposr>   r   r?   r   ]  s   $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% rc   )debugPS	_pswriterr   __name__)selfrB   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                       s.  e Zd ZdZedZdZd8 fdd	Zdd Z	d9d
dZ
d9ddZedd Zd9ddZedd Zd9ddZd9ddZd9ddZdd Zdd Zd:d d!Zd"d# Zed;d$d%Zed;d&d'Ze	d;d(d)Zed*d+ Zedd,d-d.Zed:d/d0Zed1d2 Zed3d4 Zd	d	d5d6d7Z   Z!S )<
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tj| _d S )Ntext.usetexr   r   )super__init__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   cacherw   r   _logwarn_once)r   widthheightZpswriterr   	__class__r>   r?   r     s&   


zRendererPS.__init__c                 C   sF   |d u rdS t |dkr!|d dkrdS |d dkr| d dS dS )NTr      r   r   zlThe PostScript backend does not support transparency; partially transparent artists will be rendered opaque.F)rh   r   )r   Zrgb_or_rgbar>   r>   r?   _is_transparent  s   zRendererPS._is_transparentTc                 C   sl   |||f| j kr2| j||  kr|krn nt| dnt||| d |r4|||f| _ d S d S d S )Nz	 setgray
z setrgbcolor
)r   r   r   rC   )r   rrq   bstorer>   r>   r?   	set_color  s   zRendererPS.set_colorc                 C   s>   t |}|| jkr| jt| d |r|| _d S d S d S )N setlinewidth
)floatr   r   r   rC   )r   r   r   r>   r>   r?   set_linewidth  s   

zRendererPS.set_linewidthc                 C   "   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     zRendererPS._linejoin_cmdc                 C   2   || j kr| j| | |r|| _ d S d S d S ru   )r   r   r   r   )r   r   r   r>   r>   r?   set_linejoin     

zRendererPS.set_linejoinc                 C   r   )Nr   r   r   )Zbuttr   Z
projectingr   r   r   r   z setlinecap
r>   )r   r>   r>   r?   _linecap_cmd  r   zRendererPS._linecap_cmdc                 C   r   ru   )r   r   r   r   )r   r   r   r>   r>   r?   set_linecap  r   zRendererPS.set_linecapc                 C   sx   | j d ur| j \}}t||r||krd S | j|d ur.t|r.dt|  dt| dnd |r:||f| _ d S d S )N[z] z	 setdash
z[] 0 setdash
)r   npZarray_equalr   r   rh   rC   )r   r   seqr   ZoldoZoldseqr>   r>   r?   set_linedash  s   

zRendererPS.set_linedashc                 C   sL   ||f| j | jfkr"| jd| d|dd |r$|| _ || _d S d S d S )NrX   r6   r8    selectfont
)r   r   r   r   )r   r   r   r   r>   r>   r?   set_font  s   
zRendererPS.set_fontc                 C   s   d}|| j v r| j | S dt| j  }| 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%dzH  << /PatternType 1
     /PaintType 2
     /TilingType 2
     /BBox[0 0 r   r6   z]
     /XStep z
     /YStep z(

     /PaintProc {
        pop
        rq   r   Fsimplifyz]
        gsave
        fill
        grestore
        stroke
     } bind
   >>
   matrix
   0 z translate
   makepattern
   /z
 exch def
)
r   rh   r   r   r   _convert_pathr   hatchr   scale)r   r   r   Zsidelenr\   Z
pageheightr>   r>   r?   create_hatch  s0   





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^   r_   r`   ra   s   clTrb   )r   r   r   rk   rl   )r   path	transformclipr   r>   r>   r?   r   $  s   

zRendererPS._convert_pathc              
   C   s   g }|  }|d ur|tg |j|jR   d | \}}|d ur\|t|f}| j|}|d u rTdt	| j }| j
d| d| j||dd d || j|< || d d	|S )
N
 rectclip
zc%drX   z {
Fr   z
clip
newpath
} bind def
rc   rt   )Zget_clip_rectanglerj   rC   p0sizeZget_clip_pathidr   r   rh   r   r   r   rA   )r   gcr  rectr  ZtrfkeyZ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 r2d}|| 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   rc   z1 0 0 1 0 0r6   g      ?gsave
rq   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tobytesr   r   rA   rf   rg   frozenZ	to_valuesr   r   r  )r   r  xyZimr  hwZimagecmddatahexdatamatrixZxscaleZyscaler>   r>   r?   
draw_imageC  sP   

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_pathd  s   
zRendererPS.draw_pathc                 C   s  |  |rd n%|d |d   kr|d kr"n n	t|d  dn
t|d d   d}g d}| }	| sAt| dkrE| n| d }
|	dkoR|
dk}|rp|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d|dd qd|}| j|||d	d	d d S )Nr   r   r   z setgrayr   z setrgbcolor)z/o {gsavenewpath	translatez%.1f setlinewidthFr   r  fillZgrestorestrokez
} bind defr   r  rq   r6   z orc   r"  r#  )r   rC   get_linewidthZget_forced_alpharh   get_rgbZ	get_alpharj   r   get_joinstyler   get_capstyler   extenditer_segmentsr   r   rA   r  )r   r  Zmarker_pathZmarker_transr  transr  Zps_colorZps_cmdZlwalphar#  verticescoder  r  r  r>   r>   r?   draw_markersl  sV   $





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
d|d
d| }| ||| q||  jd7  _d S )Nr   r   r   zp%d_%dFr   rX   z {
newpath
translate
z
} bind def
rq   r6   r   )rh   r.  Z_iter_collection_uses_per_pathr   draw_path_collectionr   Z_iter_collection_raw_pathsr   r   r   r   rj   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_codesr   r  r  r\   Z
path_bytesxoyoZpath_idZgc0r  r  r>   r>   r?   r1    sB   



zRendererPS.draw_path_collection)mtextc             
   C   s2  |  | r	d S t| ds!| d | ||||||d| d S | j||dd\}}	}
| }d| j }t| d d dd	i}d
dd	t
jd d d}|| }d||f }t|d }t||
t|  ||
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%dr   r7   ,z{\sffamily %s}z{\ttfamily %s})z
sans-serifZ	monospacezfont.familyr   z{\rmfamily %s}z\color[rgb]{%s} %sZ   z/\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   rC   r   r   r   r   radianscossinr   rj   r   r   )r   r  r  r  r   propangler7  r  r  blr   thetextr   ZfontcmdtexZrangler   r>   r>   r?   draw_tex  sD   

&
zRendererPS.draw_texc	                 C   s   |  | r	d S |dkr| ||||||S |r#| ||||||S g }	tjd r| |}
|
jdd	d}d|
  }d}d }|D ]D}tt|dt|d}z|
|}W n tyl   d	}|
d
}Y nw |
||}|}||| 7 }|	|||f ||| 7 }qEn0| |}
| j|
| t||
D ]}|jjdd	d}|j|j}|	||j|f q| j|   t|	dd D ]3\}}| ||
 d d dd |D }| j!"d| #| d|dd|dd|dd| d qd S )Nr8  r   rb   replacegMbP?r   uniZ04Xquestion?c                 S   s   | d S )Nr   r>   )entryr>   r>   r?   <lambda>&  s    z&RendererPS.draw_text.<locals>.<lambda>Frc   c                 s   s*    | ]\}}}|d d|ddV  qdS )rq   z 0 m /r   z
 glyphshowNr>   )r<   r   r  r\   r>   r>   r?   r@   (  s    z'RendererPS.draw_text.<locals>.<genexpr>r  rq   r6    translate
 rotate
r  )$r   r'  rG  draw_mathtextr   r   Z_get_font_afmre   encoderl   r>  r   r   ordZget_width_from_char_nameKeyErrorZget_width_charZget_kern_dist_from_namerj   Z_get_font_ttfr   trackr   ZlayoutZ	ft_objectrY   Z	glyph_idxr  r   	itertoolsgroupbyr   rA   r   r   r  )r   r  r  r  r   rB  rC  r9  r7  streamrV   Zps_namer   Zthisx	last_namerT   r\   r   kernitem
glyph_namegrouprE  r>   r>   r?   r=    sr   




zRendererPS.draw_textc                 C   s:  | j j|d|\}}}	}
}| j|   | jd|dd|dd|dd d}|
D ]M\}}}}}| j|| |j	|f|krT|j	|f}| jd|j	 d| d	 t
|tr`|t|n|||}| j|dd|dd
| d q,|D ]\}}}}| j| d| d| d| d q|| jd dS )z-Draw the math text using matplotlib.mathtext.r   r  rq   r6   rN  rO  NrX   r   z	 moveto
/z glyphshow
z
 rectfill
	grestore
)Z
_text2pathZmathtext_parserparser   r'  r   r   r   Ztrack_glyphre   
isinstancer	   Zget_name_charrv   rY   rR   )r   r  r  r  r   rB  rC  r   r   ZdescentZglyphsZrectsZlastfontrV   r   numoxZoyr[  r  r  r>   r>   r?   rP  3  s>   
&zRendererPS.draw_mathtextc                 C   s  t |t |ks
J t |dkrd S |jdksJ |jd dks"J |jd dks+J |jdks2J |jd dks;J |jd dksDJ |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)pointsz2>u4)colorsZ3u1)Zdtyperb  rd  g     o@re  rc   r  zgsave
<< /ShadingType 4
   /ColorSpace [/DeviceRGB]
   /BitsPerCoordinate 32
   /BitsPerComponent 8
   /BitsPerFlag 8
   /AntiAlias true
   /Decode [ rq   r6   z  0 1 0 1 0 1 ]
   /DataSource <
z
>
>>
shfill
grestore
)rh   ndimr  Zreshaper  r   minmaxceilemptyr  r   r   r   )r   r  rd  re  r,  r  Zflat_pointsZflat_colorsZ
points_minZ
points_maxfactorZxminZyminZxmaxZymaxr  r  r>   r>   r?   draw_gouraud_trianglesN  sJ   

z!RendererPS.draw_gouraud_trianglesr%  c          
      C   sf  | j j}| dko| |  }|sd}| |rd}| }|r?| |  | |  | 	|
  | j|   |sC|rN| j| dd   |d || | ||  |d |r|si|rm|d | j|dd ddi |d |s|r|d	 |r| || }	|d |t| dd   |d
|	 d |r|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  rc   r   zfill
r]  r6   z 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_linewidthrC   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)FNru   )"r   
__module____qualname____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  r0  r1  rG  r=  rP  rl  r  __classcell__r>   r>   r   r?   r     sL    








 
	 4
,):

/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   rn  ro  r   portraitrt  ru  r>   r>   r>   r?   rs    s    rs  c                       sz   e Zd ZdZdddZdd Z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_filetypeNrv  )metadata	papertypeorientationbbox_inches_restorec                K   s*  | j j}d| j _i }	t|ttjfr#t|j}
|
	dd
d|	d< |p&i ddtj d|	d< td}|rGtjt|tjjd	nt |	d
< ddd |	 D }	|d u rbtjd }| }tjdgt|d tjt| d}tjd r| j n| j!}|||f||	|||d| d S )Nr   rb   rH  ZTitleZCreatorzMatplotlib vz, https://matplotlib.org/SOURCE_DATE_EPOCHz%a %b %d %H:%M:%S %YZCreationDaterc   c                 s   s$    | ]\}}d | d| V  qdS )z%%z: Nr>   )r<   krr   r>   r>   r?   r@     s    
z+FigureCanvasPS._print_ps.<locals>.<genexpr>zps.papersizefigure)r{  )r|  r   )dpidsc_commentsr|  r{  r}  )"r  r  r_  rg   r|   PathLikepathlibr   r\   rQ  rl   r   r   __version__getenvdatetimefromtimestampinttimezoneutcstrftimetimectimerA   itemsr   lowerr   Zcheck_in_list	papersizeZcheck_getitemrs  _print_figure_tex_print_figure)r   fmtoutfilerz  r{  r|  r}  r   r  r  filenamesource_date_epochprinterr>   r>   r?   	_print_ps  sP   



zFigureCanvasPS._print_psr}  c                   s^  |dkt |ttjfst|stdj \	s!dkr'	}}	n	t	 \}}	d|	  
d|	  
}
}|
jj
j }|jj
j }dtju rj||
||f\}
}}}d|	  

d|
|||f t _t	j|dtj	||d	}j|  	
fd
d}tjd rt Q}tj|d}t|ddd}|| W d   n1 sw   Y  tjd dkrtt| d ntjd dkrtt| d t|| W d   dS 1 sw   Y  dS tj|ddd}t |st!"d|}|| W d   dS 1 s(w   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
        string containing Document Structuring Convention comments,
        generated from the *metadata* parameter to `.print_figure`.
        rx  z,outfile must be a path or a file-like objectr  g      B@r   r   r;  r   r  c                    s  r	t d| d nt d| d dkrt d | d t d| d t d dj d	t  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 ]+\}}|svqotjd }t|dkrd}|   |dkr| t|| qot|||  qot 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 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.0r  z%%DocumentPaperSizes: z
%%Pages: 1z%%LanguageLevel: 3
z
%%Orientation: rc   z
%%EndComments
rt   )endr  z%%BeginPrologr   z/mpldict %d dict defzmpldict beginzps.fonttype   *   r   r  z%%EndPrologz%%Page: 1 1z%s translatez	%d rotatez0 0 r   z	 rectclipZshowpagez%%EOF)printr\   _get_bbox_headerrh   _psDefsr   r   r   usedrA   r  flushr   rs   r   rC   r   r{   )rP   ZNdictrm   rn   Zfonttyper[   r  r   is_epsr|  r{  ps_rendererZrotationr   r   r5  r6  r>   r?   print_figure_impl  sb   




"z7FigureCanvasPS._print_figure.<locals>.print_figure_implps.usedistillertmp.psr  rD   rE   Nghostscript)ptyper[   xpdf)#r_  rg   r|   r  r   
ValueErrorr  get_size_inchesru  r  r[   r   r   rs  rt  r   r   r   r   r   r   r   r   r  rA   rI   _try_distill
gs_distillxpdf_distillrQ   r   Zopen_file_cmr   codecs	getwriter)r   r  r  r  r  r|  r{  r}  paper_widthpaper_heightllxllyurxuryrendererr  tmpdirtmpfilerP   r  r>   r  r?   r    sh   
";
"

$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|d}|jd| dt| dtt d	d
t dt|| dt|	d |
d  d| j  ddd |tju r|
|	}	}
||||f}|s|dkr|| j  \}}n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   dS 1 sw   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`.
        rx  r   r  r  r  z+%!PS-Adobe-3.0 EPSF-3.0
%%LanguageLevel: 3
rc   z&
%%EndComments
%%BeginProlog
/mpldict z dict def
mpldict begin
rt   z
end
%%EndProlog
mpldict begin
z translate
0 0 r   r  z
end
showpage
rD   rE   r  r  r  r   )r  r[   rotatedr  N)!r  r  r[   r   r   r   r   r   r   r   r   r  r   
write_textr  rh   r  rA   rC   r{   rs  rt  ru  r  _convert_psfragsr   r\   r   r   r  r  r  rQ   )r   r  r  r  r  r|  r{  r}  r  r   r   r5  r6  r  r  r  r  r[   r  r  r  tmppathr  r  psfrag_rotatedr>   r>   r?   r  l  s|   	



"z FigureCanvasPS._print_figure_texr  rx  c                    s   | j   t  S ru   )r  Zdraw_without_renderingr   r   r   r   r>   r?   r     s   

zFigureCanvasPS.draw)r   rn  ro  Z	fixed_dpi	filetypesry  r  r  r  r   partialmethodZprint_psZ	print_epsr   rr  r>   r>   r   r?   rw    s"    - Wrw  c           
   	   C   s0  t dt jd t jd t jd t jd d||d  i# t jdd||d	kr2d
nd|  	 d dd}W d   n1 sIw   Y  t
 !}tj|d}tdddd||gt t||  W d   n1 suw   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.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}rc   rt  r;  r   )psfragsrC  epsfile
   )r   Nr  Zdvipsz-qz-R1z-oZ	Landscapei  )r   Z
rc_contextr   Z
texmanagerZ_usepackage_if_not_loadedr   Zmake_dvirA   resolveas_posixr   r|   r  r   _check_and_log_subprocessrw   rJ   rL   rI   read)
r  r  r  r  r|  Zdvifiler  psfilerP   r  r>   r>   r?   r    sJ   


	


r  c              
   O   sV   z| t |g|R i | W d S  tjy* } ztd| W Y d }~d S d }~ww )Nz%s.  Distillation step skipped.)rg   r   ZExecutableNotFoundErrorrw   r   )funcr  rB   r   excr>   r>   r?   r    s    r  c                 C   s   |rdg}n|dkrd|d  d|d  g}nd| g}| d }t jd	 }tt d
jdddd| dg|d| | t t|  t	
||  |rYt| ||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.
    	-dEPSCropr  z-dDEVICEWIDTHPOINTS=r   z-dDEVICEHEIGHTPOINTS=r   z-sPAPERSIZE=z.pszps.distiller.resr   z-dBATCHz	-dNOPAUSE-dSAFERz-r%dz-sDEVICE=ps2writez-sOutputFile=)r  N)r   r   r   r  _get_executable_info
executablerw   r|   removerJ   rL   pstoeps)r  rx  r  r[   r  paper_optionr  r  r>   r>   r?   r    s4   


r  c           	   
   C   s   t d t d |rdg}n|dkr#d|d  d|d  g}nd	| g}t 9}t|d
}t|d}tdddddddg|| |t tdddd||gt t	||  W d   n1 shw   Y  |rut
|  dS 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pdftopsr  r  z-dDEVICEWIDTHPOINTS#r   z-dDEVICEHEIGHTPOINTS#r   z-sPAPERSIZE#ztmp.pdfr  Zps2pdfr  z-dAutoFilterColorImages#falsez-dAutoFilterGrayImages#falsez-sAutoRotatePages#Nonez-sGrayImageFilter#FlateEncodez-sColorImageFilter#FlateEncodez-papermatchz-level3N)r   r  r   r  r   r   r  rw   rJ   rL   r  )	r  rx  r  r[   r  r  r  ZtmppdfZtmppsr>   r>   r?   r  +  sJ   


r  z3.9c                 C   s   t | |r
t| fS dfS )zx
    Return a postscript header string for the given bbox lbrt=(l, b, r, t).
    Optionally, return rotate command.
    rt   )r  _get_rotate_command)lbrtr  r>   r>   r?   get_bbox_headerU  s   r  c                 C   s\   | \}}}}dt | dt | dt| dt| d|dd|dd|dd|dS )zGReturn a PostScript header string for bounding box *lbrt*=(l, b, r, t).z%%BoundingBox: r6   z
%%HiResBoundingBox: z.6f)r  mathri  r  lr   r   tr>   r>   r?   r  ^  s   .r  c                 C   s$   | \}}}}|| dddddS )uO   Return a PostScript 90° rotation command for bounding box *lbrt*=(l, b, r, t).z.2fr6   r   z translate
90 rotater>   r  r>   r>   r?   r  e  s   r  c              	   C   s6  | d }t |d}}t | dh}|j}|D ]A}|dr.|d |r-|t|dd  q|drJ|| |d	 |rH|t|dd   n|rR|d
rRq|| q|D ]}|dre|d qY|drkqY|| qYW d   n1 szw   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.
    z.epswbrG   s   %!PSs   %!PS-Adobe-3.0 EPSF-3.0
rb      
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)
rI   r   
startswithr  rQ  r  r|   r  rJ   rL   )r  r[   r  r  ZepshZtmphr   liner>   r>   r?   r  k  s<   

	




 
+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 } _dz/sc { setcachedevice } _dc                   @   s   e Zd ZdZeZdS )
_BackendPSzLevel IIN)r   rn  ro  Zbackend_versionrw  ZFigureCanvasr>   r>   r>   r?   r    s    r  )Fr   NF)F)NF)Wrp  r   r  r  enumr   r   ior   rU  loggingr  r|   r  rJ   r   tempfiler   r   r  rz   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   Zmatplotlib._mathtext_datar   Zmatplotlib.pathr   Zmatplotlib.texmanagerr   Zmatplotlib.transformsr   Z!matplotlib.backends.backend_mixedr   rt   r   	getLoggerr   rw   r   r  rC   rQ   rs   r   r   r   r   r   r   r   ZRendererPDFPSBaser   rs  rw  r  r  r  r  
deprecatedr  r  r  r  ZFigureManagerPSr  Zexportr  r>   r>   r>   r?   <module>   s    
	
Q$0+#    (  2

'*
7