a
    ضa7                     @   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mZ ddlZddlmZ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)parse)_apicbookdvireadrcParams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   e/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/matplotlib/texmanager.py_usepackage_if_not_loaded(   s    	r   c                   @   s  e Zd ZdZeje dZ	i Z
d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dd Zd6ddZd d! Zd"d# Zd$d% Zd&d' Zdd(d)d*Zd+d, Zd-d. Z d7d/d0Z!d8d2d3Z"d9d4d5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)r   
sans-serifcursive	monospace)Zpncz\renewcommand{\rmdefault}{pnc})Zpbkz\renewcommand{\rmdefault}{pbk})Zptmz\usepackage{mathptmx})Zpplz\usepackage{mathpazo})Zpzcz\usepackage{chancery})Zpchz\usepackage{charter})cmrr   )cmssr   )Zphvz\usepackage{helvet})Zpagz\usepackage{avant})Zpcrz\usepackage{courier})Zcmtt\usepackage{type1ec})r   r   )r   r   )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&   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,   f   s    zTexManager.__new__c           	   
      s  t d }t|dkr |d  nd }d}t|dkrF|| jv rF|| _nBt|dkrn|| jv rnd}| j| | _ntdd	| j d| _| jg}i  | jD ]}|r| j|kr| j|  |< n|t d	|  D ]T}| | jv r| j|   |< t
d
||| j|    q8qt
d| qtd| | j|  |< | | d  q|  d}|t|   fdddD }| jdkr| d d  |d d	t|| _d	|S )Nzfont.family   r   FTzXfont.family must be one of (%s) when text.usetex is True. serif will be used by default.z, 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.utf-8c                    s   h | ]} | d  qS )r.   r   ).0familyZfontsr   r   	<setcomp>   s   z-TexManager.get_font_config.<locals>.<setcomp>)r   r   r   r   z\usepackage{type1cm}
r   )r   lenlower_font_families_font_family
_font_info_font_types_loginfojoindebugappendget_custom_preambleencodehashlibmd5	hexdigestaddsorted_font_preamble)	selfffZff_valZreduced_notationZ
fontconfigfont_familyfontZpreamble_bytescmdr   r2   r   get_font_configk   sV    



zTexManager.get_font_configNc                 C   sH   d ||  d| |  t|p dg}tj | jt|	d
 S )zU
        Return a filename based on a hash of the string, fontsize, and dpi.
        r   z%fr/   )r=   rM   r@   strospathr)   rB   rC   rA   rD   )rH   texfontsizedpisr   r   r   get_basefile   s    zTexManager.get_basefilec                 C   s   | j S )zU
        Return a string containing font configuration for the tex preamble.
        )rG   rH   r   r   r   get_font_preamble   s    zTexManager.get_font_preamblec                 C   s   t d S )z>Return a string containing user additions to the tex preamble.ztext.latex.preamble)r   rV   r   r   r   r@      s    zTexManager.get_custom_preamblec                 C   s.   d dd| jddd|  tddd	td
g	S )Nr4   z\documentclass{article}z \newcommand{\mathdefault}[1]{#1}z\usepackage[utf8]{inputenc}z.\DeclareUnicodeCharacter{2212}{\ensuremath{-}}z1\usepackage[papersize=72in, margin=1in]{geometry}Z
underscorestringsr	   Ztextcomp)r=   rG   r@   r   rV   r   r   r   _get_preamble   s    
zTexManager._get_preamblec                 C   sV   |  ||}d| }ddd| jd}t|jd|  ||d || f dd	 |S )
zv
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        z%s.texz{\sffamily %s}z{\ttfamily %s})r   r   z{\rmfamily %s}z
%s
\pagestyle{empty}
\begin{document}
%% The empty hbox ensures that a page is printed even for empty inputs, except
%% when using psfrag which gets confused by it.
\fontsize{%f}{%f}%%
\ifdefined\psfrag\else\hbox{}\fi%%
%s
\end{document}
g      ?r/   )encoding)rU   getr8   r   
write_textrY   )rH   rQ   rR   basefiletexfileZfontcmdr   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 }~nVd }~0  tjy } z4t	dj
|d |d|jdd|W Y d }~n
d }~0 0 t | |S )N)ra   stderrz?Failed to process string with tex because {} could not be foundr   zr{prog} was not able to process the following string:
{tex!r}

Here is the full report generated by {prog}:
{exc}

unicode_escaper/   )progrQ   exc)r;   r>   r   Z_pformat_subprocess
subprocesscheck_outputr)   STDOUTFileNotFoundErrorRuntimeErrorformatCalledProcessErrorrA   outputdecode)rH   commandrQ   ra   reportre   r   r   r   _run_checked_subprocess   s4    


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)dirZlatexz-interaction=nonstopmodez--halt-on-errorz../r`   N)rU   rO   rP   existsr   r_   r   parentrq   namereplace)rH   rQ   rR   r]   dvifiler^   Z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d	}ttd
drzt	dj
|krz|dd | || |S )zp
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        z%s.pngZdvipngz-bgZTransparentz-Dz-TZtightz-oz1.16Z_called_from_pytestFr.   z--freetype0)rU   rO   rP   rs   rx   rN   parse_versiongetattrmplZ_get_executable_infoversioninsertrq   )	rH   rQ   rR   rS   r]   pngfilerw   rL   Zbad_verr   r   r   make_png  s    
zTexManager.make_pngc                 C   s   |st d }|st 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)r   rM   _grey_arraydr[   r   r{   imageZimreadrO   rP   r=   r)   )rH   rQ   rR   rS   keyalphar~   rgbar   r   r   get_grey-  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shaper{   colorsZto_rgb)rH   rQ   rR   rS   Zrgbr   r   r   r   r   get_rgba;  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      ?r.   H   N)striprx   Zpoints_to_pixelsr   ZDviwidthheightZdescent)rH   rQ   rR   Zrendererrw   Zdpi_fractionZdvipager   r   r   get_text_width_height_descentK  s    $z(TexManager.get_text_width_height_descent)N)NN)NNr   )N)$__name__
__module____qualname____doc__rO   rP   r=   r{   Zget_cachedirr)   r   r8   r7   r9   r:   r   Zdeprecate_privatize_attributeZgrey_arraydrJ   Zfont_familiesZ	font_info	functools	lru_cacher,   rM   rU   rW   r@   rY   r_   rq   rx   r   r   r   r   r   r   r   r   r   9   s^   




6
	

r   )r   r   rB   loggingrO   pathlibr   rf   tempfiler   Znumpyr   Zpackaging.versionr   ry   Z
matplotlibr{   r   r   r   r   	getLoggerr   r;   r   r   r   r   r   r   <module>   s   
