
    h:                         S r SSKrSSKrSSKrSSKrSSKJr  SSKrSSKJ	r	  SSK
rSSKrSSKJrJr  \R                   " \5      rSS.S jr " S S	5      rg)
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)cbookdvireadoptionc                &    Ub  SU S3OSnSX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.
[] z]\makeatletter\@ifpackageloaded{%(package)s}{}{\usepackage%(option)s{%(package)s}}\makeatotherpackager    r   s     G/var/www/html/env/lib/python3.13/site-packages/matplotlib/texmanager.py_usepackage_if_not_loadedr   '   s/     %0q]bF	 .	/ /    c                      \ rS rSrSr\R                  R                  \R                  " 5       S5      r
0 rSr0 SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_rSSSSSSSSSSSSS .r\R                   S! 5       r\S" 5       r\S# 5       r\S2S% j5       r\S& 5       r\S' 5       r\S( 5       r\S) 5       r\S$S*.S+ j5       r\S, 5       r\S- 5       r\S3S. j5       r\S4S/ j5       r\S2S0 j5       rS1r g$)5
TexManager8   z
Convert strings to dvi files using TeX, caching the results to a directory.

The cache directory is called ``tex.cache`` and is located in the directory
returned by `.get_cachedir`.

Repeated calls to this constructor always return the same instance.
z	tex.cache)serif
sans-serifcursive	monospacenew century schoolbookz\renewcommand{\rmdefault}{pnc}bookmanz\renewcommand{\rmdefault}{pbk}timesz\usepackage{mathptmx}palatinoz\usepackage{mathpazo}zapf chanceryz\usepackage{chancery}r   charterz\usepackage{charter}r   r   r   	helveticaz\usepackage{helvet}avant gardez\usepackage{avant}courierz\usepackage{courier}r   z\usepackage{type1ec}computer modern romancomputer modern sans serifcomputer modern typewriter)r   r   r   r   r   r   r    r!   r"   r#   r$   r%   c                 r    [        U R                  5      R                  SSS9  [        R	                  U 5      $ )NTparentsexist_ok)r   	_texcachemkdirobject__new__clss    r   r-   TexManager.__new__j   s-    S]]!!$!>~~c""r   c                 r   [         R                  S   n[        U5      S:X  a  US   R                  5       OSn[        U5      S:X  a  X R                  ;   a  US4$ [        U5      S:X  a   X R
                  ;   a  U R                  U   S4$ [        R                  SSR                  U R                  5      5        g	)
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., )r   F)
mplrcParamslenlower_font_families_font_preambles_font_types_loginfojoin)r/   ffff_vals      r   _get_font_family_and_reduced'TexManager._get_font_family_and_reducedo   s     \\-("%b'Q,ADr7a<F&8&885= W\f(;(;;??6*D00II =ii 2 235 "r   c                 F   U R                  5       u  p0 nU R                   GH
  nU(       a:  XA:X  a5  U R                  [        R                  S   S   R                  5          X4'   ME  [        R                  SU 3   n[        [        [        R
                  U5      5       Ha  u  pgXpR                  ;   d  M  U R                  U   X4'   [        R                  SX@R                  U   XV   SR                  US U 5      5          M     [        R                  SU5        U R                  U   X4'   GM     S Vs1 s H  nX8   iM	     n	nUS:X  a  U	R                  US   5        U	R                  S	5        S
R                  [        U	5      5      n
US:X  a  SO	US:X  a  SOSnX4$ s  snf )Nr2   r   zfont.z.family: %s, package: %s, font: %s, skipped: %sr4   zPNo LaTeX-compatible font found for the %s fontfamily in rcParams. Using default.)r   r   r   r   z\usepackage{type1cm}
r   z	\sffamilyr   z	\ttfamilyz	\rmfamily)rA   r9   r:   r5   r6   r8   	enumeratemapstrr<   debugr>   r=   addsorted)r/   requested_familyis_reduced_font	preamblesfont_familyrcfontsifontfamilycmdpreamblefontcmds               r   _get_font_preamble_and_command)TexManager._get_font_preamble_and_command~   s   ,/,L,L,N)	--K;#B),)<)<LL/288:*<	& ,,{m'<=(SYY)@AGA222141D1DT1J	.

L')<)<T)BGJ IIgbqk2
   B II C)+ .1-@-@-MI*' .0 BCA6  A 	 Cy(GGIi()'(99VC[)#3|#C<#3{#B< 	   Cs   4FNc                    U R                  X5      [        U5      -   n[        R                  " UR	                  S5      SS9R                  5       n[        U R                  5      nSu  px[        SXx-  U5       H  n	U[        XYU	S-    5      -  nM     UR                  SSS9  [        R                  R                  Xe5      $ )	zE
Return a filename based on a hash of the string, fontsize, and dpi.
utf-8F)usedforsecurity)   r[   r   r[   Tr'   )_get_tex_sourcerG   hashlibsha256encode	hexdigestr   r*   ranger+   ospathr>   )
r/   texfontsizedpisrcfilehashfilepathnum_letters
num_levelsrP   s
             r   get_basefileTexManager.get_basefile   s    
 !!#03s8;>>JJw!
 )+ 	 &"&q+0+>A$x!A#"77H ? 	td3ww||H//r   c                 *    U R                  5       u  pU$ )zE
Return a string containing font configuration for the tex preamble.
)rV   )r/   font_preamblecommands      r   get_font_preambleTexManager.get_font_preamble   s    
 "%!C!C!Er   c                 (    [         R                  S   $ )z>Return a string containing user additions to the tex preamble.ztext.latex.preamble)r5   r6   r.   s    r   get_custom_preambleTexManager.get_custom_preamble   s     ||122r   c           "          U R                  5       u  p4SU-  nSR                  SSSSSUSS	S
SSSU R                  5       SS[        SSS9SS[        S5      SSSSSSSU SU S3SS U S!U S3S"/5      $ )#z;Return the complete TeX source for processing a TeX string.g      ?rD   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.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\end{document})rV   r>   rt   r   )r/   rd   re   ro   rU   baselineskips         r   r\   TexManager._get_tex_source   s     "%!C!C!Ehyy&LM /*=BK@##%NN%l9EJ1%j1 MLJ&8*Dc:0'!C5$;
  	r   c                 ~    U R                  X5      S-   n[        U5      R                  U R                  X5      SS9  U$ )z^
Generate a tex file to render the tex string at a specific font size.

Return the file name.
z.texrY   )encoding)rl   r   
write_textr\   )r/   rd   re   texfiles       r   make_texTexManager.make_tex   sE     ""31F:W  !4!4S!C*1 	! 	3r   cwdc                   [         R                  [        R                  " U5      5         [        R
                  " Xb  UOU R                  [        R                  S9n[         R                  U5        U$ ! [         a  n[        SUS    S35      UeS nAf[        R                   aa  n[        SR                  US   [        R                  " U5      UR                  S5      UR                  R                  SS5      S	95      S eS nAff = f)
N)r   stderrz*Failed to process string with tex because r   z could not be foundz{prog} was not able to process the following string:
{tex!r}

Here is the full command invocation and its output:

{format_command}

{exc}

unicode_escaperY   backslashreplace)progformat_commandrd   exc)r<   rH   r   _pformat_subprocess
subprocesscheck_outputr*   STDOUTFileNotFoundErrorRuntimeErrorCalledProcessErrorformatr_   outputdecode)r/   rp   rd   r   reportr   s         r   _run_checked_subprocess"TexManager._run_checked_subprocess   s    

5,,W56	,,OS!((*F& 	

6# ! 	/<WQZL I% %&+./ ,, 	 #F #(#<#<W#E

#34

))'3EF	 # H
 
	s$   2A4 4
D	>BD	(ADD	c                    U R                  X5      nSU-  n[        R                  R                  U5      (       d  [	        U R                  X5      5      n[	        U5      R                  n[        US9 n[	        U5      nU R                  SSSSUR                   3UR                   /XS9  U[	        U5      R                  -  R                  U5        SSS5        U$ U$ ! , (       d  f       U$ = f)	zU
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--output-directory=r   N)rl   rb   rc   existsr   r   parentr   r   namereplace)	r/   rd   re   basefiledvifiler   r   tmpdirtmppaths	            r   make_dviTexManager.make_dvi  s     ##C2X%ww~~g&&3<<67G w-&&C#,v,++8:K*7<<.9~(), , 7 4=---66w? - w -, s   3A C
C.c           
         U R                  XU5      nSU-  n[        R                  R                  U5      (       d  U R	                  X5      nSSSS[        U5      SSSXV/
n[        [        S	S
5      (       a6  [        R                  " S5      R                  S:w  a  UR                  SS5        U R                  Xq5        U$ )zX
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.16r3   z--freetype0)rl   rb   rc   r   r   rG   getattrr5   _get_executable_inforaw_versioninsertr   )r/   rd   re   rf   r   pngfiler   rS   s           r   make_pngTexManager.make_png.  s     ##C37X%ww~~g&&ll31GUM4S$:C 2E::,,X6BBfL

1m,''1r   c                    U(       d  [         R                  S   nU(       d  [         R                  S   nU R                  X5      U4nU R                  R	                  U5      nUct  U R                  XU5      n[         R                  R                  [        R                  R                  U R                  U5      5      nUSS2SS2S4   =U R                  U'   nU$ )zReturn the alpha channel.z	font.sizezsavefig.dpiN)r5   r6   r\   _grey_arraydgetr   imageimreadrb   rc   r>   r*   )r/   rd   re   rf   keyalphar   rgbas           r   get_greyTexManager.get_greyF  s     ||K0H,,}-C!!#0#5  $$S)=ll3#6G99##BGGLL$HID,0ArN:CS!Er   c                     U R                  XU5      n[        R                  " / UR                  QSP75      n[        R
                  R                  U5      USSS24'   XVS'   U$ )z
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shaper5   colorsto_rgb)r/   rd   re   rf   rgbr   r   s          r   get_rgbaTexManager.get_rgbaU  s]     SC0xx)%++)q)*

))#.S"1"WWr   c                 R   UR                  5       S:X  a  gU R                  X5      nU(       a  UR                  S5      OSn[        R                  " USU-  5       nUu  nSSS5        WR
                  UR                  UR                  -   UR                  4$ ! , (       d  f       N>= f)z-Return width, height and descent of the text.r   r   r   r   g      ?r3   H   N)stripr   points_to_pixelsr   Dviwidthheightdescent)r/   rd   re   rendererr   dpi_fractiondvipages           r   get_text_width_height_descent(TexManager.get_text_width_height_descentf  s     99;",,s-8@x004a[["|"34ED 5 zz4;;5t||CC 54s   B
B&r   )N)NN)NNr   )!__name__
__module____qualname____firstlineno____doc__rb   rc   r>   r5   get_cachedirr*   r   r9   r:   r;   	functools	lru_cacher-   classmethodrA   rV   rl   rq   rt   r\   r   r   r   r   r   r   r   __static_attributes__r   r   r   r   r   8   sS    S--/=ILDN "C4 	) 	,	
 	1 	+ 	* 	 	b 	+ 	, 	*  	,!" 	 !8#$ 	%&=%& 	%&='O, #*"!#!(&2&1K # # " " $! $!L 0 0$   3 3 " "H 	 	 :>  2  :  .      	D 	Dr   r   )r   r   r]   loggingrb   pathlibr   r   tempfiler   numpyr   
matplotlibr5   r   r   	getLoggerr   r<   r   r   r   r   r   <module>r      sR   ,    	   '   %" 26 /"xD xDr   