o
    i=                     @   s   d 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mZmZmZ eeZG dd dZdS )a  
Support for embedded TeX expressions in Matplotlib via dvipng and dvips for the
raster and PostScript backends.  The tex and dvipng/dvips information is cached
in ~/.matplotlib/tex.cache for reuse between sessions.

Requirements:

* latex
* \*Agg backends: dvipng>=1.6
* PS backend: psfrag, dvips, and Ghostscript>=8.60

Backends:

* \*Agg
* PS
* PDF

For raster output, you can get RGBA numpy arrays from TeX expressions
as follows::

  texmanager = TexManager()
  s = ('\TeX\ is Number '
       '$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!')
  Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))

To enable tex rendering of all text in your matplotlib figure, set
:rc:`text.usetex` to True.
    N)Path)cbookdvireadrcParamsc                   @   s  e Zd ZdZeje dZ	i Z
dZd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dd d!dZejd"d#d$ed%d& Zed"ed'd( Ze d)d* Zi Zed"ed+d, Zed"ed-d. Zed"ed/d0 Zed"ed1d2 Zd3d4 ZdRd6d7Zd8d9 Zd:d; Zd<d= Zd>d? Z e!"d@Z#ed"dAdB Z$dCdD Z%dEdF Z&ed"dGdH Z'dIdJ Z(dSdKdLZ)dTdNdOZ*dRdPdQZ+d5S )U
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	monospaceznew century schoolbook)pncz\renewcommand{\rmdefault}{pnc}bookman)pbkz\renewcommand{\rmdefault}{pbk}times)ptmz\usepackage{mathptmx}palatino)pplz\usepackage{mathpazo}zzapf chancerypzcz\usepackage{chancery}r	   charter)pchz\usepackage{charter}cmr r   cmssr   	helvetica)phvz\usepackage{helvet}zavant garde)pagz\usepackage{avant}courier)pcrz\usepackage{courier}r
   )cmtt\usepackage{type1ec}zcomputer modern roman)r   r!   zcomputer modern sans serif)r   r!   zcomputer modern typewriterz3.3zmatplotlib.get_cachedir())alternativec                 C   s   t  S N)mplget_cachedirself r(   P/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/texmanager.pycachedirR   s   zTexManager.cachedirc                 C   s   i S r#   r(   r&   r(   r(   r)   rgba_arraydW   s   zTexManager.rgba_arraydc                 C   s   t | jjddd t| S )NT)parentsexist_ok)r   texcachemkdirobject__new__)clsr(   r(   r)   r1   \   s   
zTexManager.__new__c                 C      | j ddS )Nr   r   _fontsgetr&   r(   r(   r)   r   c      zTexManager.serifc                 C   r3   )Nr   r   r4   r&   r(   r(   r)   
sans_serifh   r7   zTexManager.sans_serifc                 C   r3   )Nr	   r   r4   r&   r(   r(   r)   r	   m   r7   zTexManager.cursivec                 C   r3   )Nr
   )r    r   r4   r&   r(   r(   r)   r
   r   r7   zTexManager.monospacec              
   C   sv  t d }t|dkr|d  | jv r|d  | _ntdd| j d| _| jg}| jD ]I}t d|  D ](}| | jv rY| j|  | j	|< t
d||| j|    nt
d	| q7td
| | j| | j	|< || j	| d  q/|  d}|t|  | j	d d | j	d d | j	d d g}| jdkr|| j	d d  ddg|| _d|S )Nzfont.family   r   zXfont.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.zQNo LaTeX-compatible font found for the %s font family in rcParams. Using default.utf-8r   r
   r	   
z\usepackage{type1cm}r   )r   lenlowerfont_familiesfont_family_loginfojoin	font_infor5   debugappendget_custom_preambleencodehashlibmd5	hexdigest_font_preamble)r'   ff
fontconfigr?   fontpreamble_bytescmdr(   r(   r)   get_font_configw   s@   



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:   )rB   rQ   rF   strospathr.   rH   rI   rG   rJ   )r'   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.
        )rK   r&   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   r&   r(   r(   r)   rF      s   zTexManager.get_custom_preamblec              
   C   s    d dd| jddd|  dgS )Nr;   z\documentclass{article}z \newcommand{\mathdefault}[1]{#1}z\usepackage[utf8]{inputenc}z.\DeclareUnicodeCharacter{2212}{\ensuremath{-}}z:\usepackage[papersize=72in,body=70in,margin=1in]{geometry}zM\makeatletter\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}\makeatother)rB   rK   rF   r&   r(   r(   r)   _get_preamble   s   zTexManager._get_preamblec                 C   V   |  ||}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.
        %s.tex{\sffamily %s}{\ttfamily %s}r   r
   {\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}
      ?r:   encodingrY   r6   r?   r   
write_textr[   r'   rU   rV   basefiletexfilefontcmdr(   r(   r)   make_tex   s    
zTexManager.make_texz3MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)ptc                 C   r\   )
z
        Generate a tex file to render the tex string at a specific font size.

        It uses the preview.sty to determine the dimension (width, height,
        descent) of the output.

        Return the file name.
        r]   r^   r_   r`   ra   aD  
%s
\usepackage[active,showbox,tightpage]{preview}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\def\showbox#1%%
{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}}

\begin{document}
\begin{preview}
{\fontsize{%f}{%f}%s}
\end{preview}
\end{document}
rb   r:   rc   re   rg   r(   r(   r)   make_tex_preview   s    
zTexManager.make_tex_previewc              
   C   s   t t| ztj|| jtjd}W n6 ty* } z
t	d
|d |d }~w tjyJ } zt	dj
|d |d|jdd|d }~ww t | |S )N)cwd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:   )progrU   exc)r@   rD   r   _pformat_subprocess
subprocesscheck_outputr.   STDOUTFileNotFoundErrorRuntimeErrorformatCalledProcessErrorrG   outputdecode)r'   commandrU   reportrq   r(   r(   r)   _run_checked_subprocess  s:   



	z"TexManager._run_checked_subprocessc              	   C   s   t tdr| ||S | ||}d| }tj|sa| ||}t	
| | ddd|g| W d   n1 s<w   Y  t|d D ]}|ds`zt| W qH ty_   Y qHw qH|S )	zm
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        text.latex.preview%s.dvilatex-interaction=nonstopmode--halt-on-errorN*)dvirU   )dict__getitem__r   make_dvi_previewrY   rS   rT   existsrk   r   
_lock_pathr~   globendswithremoveOSError)r'   rU   rV   rh   dvifileri   fnamer(   r(   r)   make_dvi&  s.   
zTexManager.make_dvic              	   C   s   |  ||}d| }d| }tj|rtj|st| ||}| ddd|g|}tj|	d}t
|d d}	|	d	|  W d
   n1 sOw   Y  t|d D ]}
|
dsszt|
 W q[ tyr   Y q[w q[|S )z
        Generate a dvi file containing latex's layout of tex string.

        It calls make_tex_preview() method and store the size information
        (width, height, descent) in a separate file.

        Return the file name.
        r   %s.baseliner   r   r   r:   z	.baselinew Nr   )r   rU   baseline)rY   rS   rT   r   rl   r~   r   _re_vboxsearchr{   openwriterB   groupsr   r   r   r   )r'   rU   rV   rh   r   baselinefileri   r}   mfhr   r(   r(   r)   r   A  s0   

zTexManager.make_dvi_previewc              
   C   s   |  |||}d| }tj|s?| ||}ddddt|ddd||g
}ttd	d
r9tdj	dkr9|
dd | || |S )zp
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        z%s.pngdvipngz-bgTransparentz-Dz-Ttightz-o_called_from_pytestFz1.16r9   z--freetype0)rY   rS   rT   r   r   rR   getattrr$   _get_executable_infoversioninsertr~   )r'   rU   rV   rW   rh   pngfiler   rP   r(   r(   r)   make_pngd  s   
zTexManager.make_pngc                 C   s   |st d }|st d }||  ||f}| j|}|du rA| |||}tjtj	
| j|}|dddddf  | j|< }|S )zReturn the alpha channel.z	font.sizezsavefig.dpiN)r   rQ   grey_arraydr6   r   r$   imageimreadrS   rT   rB   r.   )r'   rU   rV   rW   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 )z<Return latex's rendering of the tex string as an rgba array.   .N   ).r   )r   npemptyshaper$   colorsto_rgb)r'   rU   rV   rW   rgbr   r   r(   r(   r)   get_rgba  s
   zTexManager.get_rgbac                    s  |  dkrdS |r|dnd ttdrZ| ||}d| }tj|s-| 	||}t
|}|  }W d   n1 sBw   Y   fdd	|D \}	}
}||	|
 |
fS | ||}t|d
  }|\}W d   n1 svw   Y  |j|j|j |jfS )z-Return width, height and descent of the text.r   r   g      ?r9   r   r   Nc                    s   g | ]}t |  qS r(   )float).0l1dpi_fractionr(   r)   
<listcomp>  s    z<TexManager.get_text_width_height_descent.<locals>.<listcomp>H   )strippoints_to_pixelsr   r   r   rY   rS   rT   r   r   r   readsplitr   r   Dviwidthheightdescent)r'   rU   rV   rendererrh   r   r   r   lr   depthr   r   pager(   r   r)   get_text_width_height_descent  s$   
z(TexManager.get_text_width_height_descentr#   )NN)NNr   ),__name__
__module____qualname____doc__rS   rT   rB   r$   r%   r.   r   r?   r>   rC   r   
deprecatedpropertyr*   r+   	functools	lru_cacher1   r5   r   r8   r	   r
   rQ   rY   rZ   rF   r[   rk   recompiler   rl   r~   r   r   r   r   r   r   r(   r(   r(   r)   r   /   s    	


*	
&
"

r   )r   r   r   rH   loggingrS   pathlibr   r   rs   numpyr   
matplotlibr$   r   r   r   	getLoggerr   r@   r   r(   r(   r(   r)   <module>   s    
