a
    Sict;                     @   s   d 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m	Z	 ddl
ZddlZddlmZmZmZ eeZddddZG d	d
 d
ZdS )a  
Support for embedded TeX expressions in Matplotlib.

Requirements:

* LaTeX.
* \*Agg backends: dvipng>=1.6.
* PS backend: PSfrag, dvips, and Ghostscript>=9.0.
* PDF and SVG backends: if LuaTeX is present, it will be used to speed up some
  post-processing steps, but note that it is not used to parse the TeX string
  itself (only LaTeX is supported).

To enable TeX rendering of all text in your Matplotlib figure, set
:rc:`text.usetex` to True.

TeX and dvipng/dvips processing results are cached
in ~/.matplotlib/tex.cache for reuse between sessions.

`TexManager.get_rgba` can also be used to directly obtain raster output as RGBA
NumPy arrays.
    N)Path)TemporaryDirectory)_apicbookdvireadoptionc                C   s&   |durd| dnd}d| |d S )a$  
    Output LaTeX code that loads a package (possibly with an option) if it
    hasn't been loaded yet.

    LaTeX cannot load twice a package with different options, so this helper
    can be used to protect against users loading arbitrary packages/options in
    their custom preamble.
    N[] z]\makeatletter\@ifpackageloaded{%(package)s}{}{\usepackage%(option)s{%(package)s}}\makeatotherpackager    r   r   r   Q/var/www/html/django/DPS/env/lib/python3.9/site-packages/matplotlib/texmanager.py_usepackage_if_not_loaded'   s    	r   c                   @   sb  e Zd ZdZeje dZ	i Z
dZddddddd	d
d
ddddddddZdddddddddddddZedZedZedZedZe dd Zeddd Zedd Zedd Zed8d d!Zed"d# Zed$d% Zed&d' Zed(d) Z edd*d+d,Z!ed-d. Z"ed/d0 Z#ed9d1d2Z$ed:d4d5Z%ed;d6d7Z&dS )<
TexManagerz
    Convert strings to dvi files using TeX, caching the results to a directory.

    Repeated calls to this constructor always return the same instance.
    z	tex.cache)serif
sans-serifcursive	monospacez\renewcommand{\rmdefault}{pnc}z\renewcommand{\rmdefault}{pbk}z\usepackage{mathptmx}z\usepackage{mathpazo}z\usepackage{chancery}z\usepackage{charter}r   z\usepackage{helvet}z\usepackage{avant}z\usepackage{courier}z\usepackage{type1ec})new century schoolbookbookmantimespalatinozapf chanceryr   charterr   r   	helveticaavant gardecourierr   computer modern romancomputer modern sans serifcomputer modern typewriterr   r   r   r   )r   r   r   r   r   r   r   r   r   r   r    r!   z3.5c                 C   s   t | jjddd t| S )NT)parentsexist_ok)r   texcachemkdirobject__new__clsr   r   r   r'   l   s    zTexManager.__new__z3.6c                 C   s0   |   \}}|| |   }t|d S )Nutf-8)_get_font_preamble_and_commandget_custom_preamblehashlibmd5encode	hexdigest)selfpreambleZfont_cmd	preamblesr   r   r   get_font_configq   s    zTexManager.get_font_configc                 C   s   t jd }t|dkr"|d  nd}t|dkrD|| jv rD|dfS t|dkrh|| jv rh| j| dfS tdd	| j d	S dS )
z<Return the font family name and whether the font is reduced.font.family   r   NFTzXfont.family must be one of (%s) when text.usetex is True. serif will be used by default.z, )r   F)
mplrcParamslenlower_font_families_font_preambles_font_types_loginfojoin)r)   ffZff_valr   r   r   _get_font_family_and_reducedz   s    

z'TexManager._get_font_family_and_reducedc              
      s(  |   \}}i  | jD ]}|rD||krD| jtjd d    |< qtjd|  D ]P}| | jv r| j|   |< td||| j|    qqRtd| qRtd| | j|  |< q fddd	D }|d
kr|	 d
  |	d d
t|}|dkrdn|dkrdnd}||fS )Nr5   r   zfont.zfamily: %s, font: %s, info: %sz&%s font is not compatible with usetex.zPNo LaTeX-compatible font found for the %s fontfamily in rcParams. Using default.c                    s   h | ]} | qS r   r   ).0familyr3   r   r   	<setcomp>   s   z<TexManager._get_font_preamble_and_command.<locals>.<setcomp>)r   r   r   r   z\usepackage{type1cm}
r   z	\sffamilyr   z	\ttfamilyz	\rmfamily)rB   r;   r<   r7   r8   r:   r>   debugr?   addr@   sorted)r)   Zrequested_familyZis_reduced_fontfont_familyfontcmdr2   fontcmdr   rE   r   r+      sH    



z)TexManager._get_font_preamble_and_commandNc                 C   s4   |  ||t| }tj| jt|d	 S )zU
        Return a filename based on a hash of the string, fontsize, and dpi.
        r*   )
_get_tex_sourcestrospathr@   r$   r-   r.   r/   r0   )r)   texfontsizedpisrcr   r   r   get_basefile   s    zTexManager.get_basefilec                 C   s   |   \}}|S )zU
        Return a string containing font configuration for the tex preamble.
        )r+   )r)   font_preamblecommandr   r   r   get_font_preamble   s    zTexManager.get_font_preamblec                 C   s
   t jd S )z>Return a string containing user additions to the tex preamble.ztext.latex.preamble)r7   r8   r(   r   r   r   r,      s    zTexManager.get_custom_preamblec              "   C   s   |   \}}d| }dddddd|dd	d
ddd|  ddtdddddtdddddddd| d| ddd | d!| dd"d#gS )$z;Return the complete TeX source for processing a TeX string.g      ?rG   z\documentclass{article}z=% Pass-through \mathdefault, which is used in non-usetex modez>% to use the default text font but was historically suppressedz% in usetex mode.z \newcommand{\mathdefault}[1]{#1}z\usepackage[utf8]{inputenc}z.\DeclareUnicodeCharacter{2212}{\ensuremath{-}}z3% geometry is loaded before the custom preamble as z<% convert_psfrags relies on a custom preamble to change the z% geometry.z1\usepackage[papersize=72in, margin=1in]{geometry}z?% Use `underscore` package to take care of underscores in text.z?% The [strings] option allows to use underscores in file names.
underscorestringsr   z;% Custom packages (e.g. newtxtext) may already have loaded z"% textcomp with different options.Ztextcompz\pagestyle{empty}z\begin{document}z>% The empty hbox ensures that a page is printed even for emptyz=% inputs, except when using psfrag which gets confused by it.z;% matplotlibbaselinemarker is used by dviread to detect thez% last line's baseline.z
\fontsize{z}{z}%z!\ifdefined\psfrag\else\hbox{}\fi%z{\obeylines z#\special{matplotlibbaselinemarker}%z\end{document})r+   r@   r,   r   )r)   rS   rT   rX   rN   Zbaselineskipr   r   r   rO      sD    
zTexManager._get_tex_sourcec                 C   s.   |  ||d }t|j| ||dd |S )zv
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        z.texr*   )encoding)rW   r   
write_textrO   )r)   rS   rT   texfiler   r   r   make_tex   s
    zTexManager.make_texcwdc                C   s   t t| z$tj||d ur$|n| jtjd}W n tyl } z t	d
|d |W Y d }~n`d }~0  tjy } z>t	dj
|d t||d|jdddd W Y d }~n
d }~0 0 t | |S )	N)rc   stderrz?Failed to process string with tex because {} could not be foundr   z{prog} was not able to process the following string:
{tex!r}

Here is the full command invocation and its output:

{format_command}

{exc}

unicode_escaper*   backslashreplace)progformat_commandrS   exc)r>   rH   r   _pformat_subprocess
subprocesscheck_outputr$   STDOUTFileNotFoundErrorRuntimeErrorformatCalledProcessErrorr/   outputdecode)r)   rY   rS   rc   reportri   r   r   r   _run_checked_subprocess   s6    


z"TexManager._run_checked_subprocessc                 C   s   |  ||}d| }tj|st| ||}tt|jdH}| jdddd|j	 g||d t|t|j	 
| W d   n1 s0    Y  |S )	zm
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        z%s.dvi)dirlatexz-interaction=nonstopmodez--halt-on-errorz../rb   N)rW   rQ   rR   existsr   ra   r   parentru   namereplace)r)   rS   rT   basefiledvifiler`   tmpdirr   r   r   make_dvi  s    	
6zTexManager.make_dvic              
   C   s   |  |||}d| }tj|s~| ||}ddddt|ddd||g
}ttd	d
rrtdj	dkrr|
dd | || |S )zp
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        z%s.pngdvipngz-bgZTransparentz-Dz-Ttightz-oZ_called_from_pytestFz1.16r6   z--freetype0)rW   rQ   rR   rx   r   rP   getattrr7   _get_executable_inforaw_versioninsertru   )r)   rS   rT   rU   r|   pngfiler}   rM   r   r   r   make_png.  s    
zTexManager.make_pngc                 C   s   |st jd }|st jd }| |||f}| j|}|du r| |||}t jtj	
| j|}|dddddf  | j|< }|S )zReturn the alpha channel.z	font.sizezsavefig.dpiN)r7   r8   rO   _grey_arraydgetr   imageimreadrQ   rR   r@   r$   )r)   rS   rT   rU   keyalphar   rgbar   r   r   get_greyF  s    

 zTexManager.get_greyr   r   r   c                 C   sH   |  |||}tg |jdR }tj||dddf< ||d< |S )a*  
        Return latex's rendering of the tex string as an rgba array.

        Examples
        --------
        >>> texmanager = TexManager()
        >>> s = r"\TeX\ is $\displaystyle\sum_n\frac{-e^{i\pi}}{2^n}$!"
        >>> Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))
           .N   ).r   )r   npemptyshaper7   colorsto_rgb)r)   rS   rT   rU   rgbr   r   r   r   r   get_rgbaU  s
    zTexManager.get_rgbac                 C   sz   |  dkrdS | ||}|r*|dnd}t|d| }|\}W d   n1 sZ0    Y  |j|j|j |jfS )z-Return width, height and descent of the text.r   r   g      ?r6   H   N)stripr   points_to_pixelsr   Dviwidthheightdescent)r)   rS   rT   rendererr}   Zdpi_fractiondvipager   r   r   get_text_width_height_descentf  s    $z(TexManager.get_text_width_height_descent)N)NN)NNr   )N)'__name__
__module____qualname____doc__rQ   rR   r@   r7   get_cachedirr$   r   r;   r<   r=   r   deprecate_privatize_attributeZgrey_arraydrK   Zfont_familiesZ	font_info	functools	lru_cacher'   
deprecatedr4   classmethodrB   r+   rW   rZ   r,   rO   ra   ru   r   r   r   r   r   r   r   r   r   r   8   s   







(


%


r   )r   r   r-   loggingrQ   pathlibr   rk   tempfiler   numpyr   
matplotlibr7   r   r   r   	getLoggerr   r>   r   r   r   r   r   r   <module>   s   
