
    h                     .   S SK r S SKrS SKrS SKJr  S SKrS SK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JrJr  S SKJrJrJrJr  S SKJr  S SKJrJr  S S	KJ r   S S
K!J"r"  S SK#J$r$  S SK%J&r&  \RN                  " \(5      r)Sr*S r+Sr,S\,-  r-Sr.S\.-  r/S r0S r1S r2S r3S r4 " S S\55      r6 " S S5      r7\Rp                  " S5      S 5       r9 " S S\5      r: " S S \5      r;\r<\Rz                   " S! S"\5      5       r> " S# S$5      r?g)%    N)BytesIO)TemporaryDirectory)Image)cbookfont_manager)_BackendFigureCanvasBaseFigureManagerBaseRendererBase)MixedModeRenderer)_create_pdf_info_dict_datetime_to_pdf)Path)Figure)FontProperties)Gcfz\documentclass{article}c                  l   [        [        R                  S   S9R                  5       n SR	                  SSSSU -  SS	U S
U -  4-  SS[        R                  S   /	[        R                  S   (       a  SS/[        / SQS Vs/ s H-  n[        R                  " [        R                  " U5      5      PM/     sn5       VVs/ s H6  u  p#SU< SUR                  < SUR                  R                  5       < S3PM8     snn-   S/-   O/ Q[        R                  R                  SSS9P5      $ s  snf s  snnf )z=Prepare a LaTeX preamble based on the rcParams configuration.z	font.size)size
z\def\mathdefault#1{#1}z4\everymath=\expandafter{\the\everymath\displaystyle}z\IfFileExists{scrextend.sty}{z'  \usepackage[fontsize=%fpt]{scrextend}z}{z:  \renewcommand{\normalsize}{\fontsize{%f}{%f}\selectfont}333333?z  \normalsize}zpgf.preamblepgf.rcfontsz1\ifdefined\pdftexversion\else  % non-pdftex case.z  \usepackage{fontspec})setmainfontsetsansfontsetmonofont)serifzsans\-serif	monospacez  \{z}[Path=\detokenize{z/}]z\fi
underscorestrings)option)r   mplrcParamsget_size_in_pointsjoinzippathlibr   fmfindfontnameparentas_posix
texmanager_usepackage_if_not_loaded)font_size_ptfamilycommandpaths       Q/var/www/html/env/lib/python3.13/site-packages/matplotlib/backends/backend_pgf.py_get_preambler4   '   sS   !\\+&  99 	"?
 	)2\AE|+
,	-^$%!< ]3 A&
 "%=EGEV bkk&12EG"
" 		4;;#7#7#9;"

 H :<=!@ 	00i0PA! ! !6G
s   4D+?=D0gRhV?      ?gqq?c                 &    U R                  SS5      $ )z\
Do some necessary and/or useful substitutions for texts to be included in
LaTeX documents.
u   −z\ensuremath{-})replace)texts    r3   _tex_escaper9   X   s    
 <<(*;<<    c                 H    U R                  U5        U R                  S5        g )Nz%
)write)fhlines     r3   _writelnr?   `   s     HHTNHHUOr:   c                   ^ / nSSSSS.nUR                  5       S   mTU;   a  UR                  UT   5        O[        R                  S   (       d  UR                  S5        O][	        U4S j[
        R                  R                   5       5      (       a  UR                  S	T-  5        O[        R                  S
T5        UR                  5       nUR                  SXDS-  4-  5        SSSS.nUR                  XQR                  5          5        / SQnUR                  5       U;   a  UR                  S5        UR                  S5        SSR                  U5      -   S-   S-   [        U 5      -   S-   $ )zw
Generate a TeX string that renders string *s* with font properties *prop*,
also applying any required escapes to *s*.
z	\rmfamilyz	\sffamilyz	\ttfamily)r   sansz
sans-serifr   r   r   z\fontfamily{\familydefault}c              3   @   >#    U  H  oR                   T:H  v   M     g 7fN)r*   ).0fontr0   s     r3   	<genexpr>*_escape_and_apply_props.<locals>.<genexpr>u   s     D-CTYY& -Cs   z9\ifdefined\pdftexversion\else\setmainfont{%s}\rmfamily\fizIgnoring unknown font: %sz\fontsize{%f}{%f}r    z\itshapez\slshape)normalitalicoblique)semibolddemibolddemiboldheavyz
extra boldblackz	\bfseriesz\selectfontr   z1\catcode`\^=\active\def^{\ifmmode\sp\else\^{}\fi}z\catcode`\%=\active\def%{\%}r   )
get_familyappendr"   r#   anyr(   fontManagerttflist_logwarningr$   	get_style
get_weightr%   r9   )spropcommandsfamiliesr   styles
boldstylesr0   s          @r3   _escape_and_apply_propsra   g   sZ   
 H%|*GH__q!F()\\-(67	DR^^-C-CD	D	DH6Q	S 	0&9""$DOO(D*+==>{{KFOOF>>+,-)JJ&%OON#
''(
	
>	? *	* a.	 	
r:   c                     [        U[        R                  5      (       a  [        U5      nO-U S:X  a  UR                  R	                  S5      nO[        U5      nU  SU S3$ )z;Convert metadata key/value to a form that hyperref accepts.Trappedasciiz={r   )
isinstancedatetimer   r*   decodestr)keyvalues     r3   _metadata_to_strrk      sW    %**++ '			

!!'*E
U#eWBr:   c                     ^   [         R                  " S5        S $ ! [         R                   a     Of = f [         R                  " S5      m U 4S j$ ! [         R                   a     Of = f[        S5      e)z9Return a function that converts a pdf file to a png file.
pdftocairoc                     [         R                  " SSSSSSU-  U [        R                  R	                  U5      S   /[         R
                  S9$ )	Nrm   z-singlefilez-transpz-pngz-rz%dr   stderr)
subprocesscheck_outputosr2   splitextSTDOUT)pdffilepngfiledpis      r3   <lambda>+make_pdf_to_png_converter.<locals>.<lambda>   sG    Z-D-D=)VT4#:bgg&&w/24$$.&r:   gsc                    > [         R                  " TR                  SSSSSSSSS	S
SU-  SU-  U /[         R                  S9$ )Nz-dQUIETz-dSAFERz-dBATCHz	-dNOPAUSEz
-dNOPROMPTz-dUseCIEColorz-dTextAlphaBits=4z-dGraphicsAlphaBits=4z-dDOINTERPOLATEz-sDEVICE=pngalphaz-sOutputFile=%sz-r%dro   )rq   rr   
executableru   )rv   rw   rx   gs_infos      r3   ry   rz      sT    Z-D-D	9k<1$&7 "3g"=c\7$ $$.&r:   z&No suitable pdf to png renderer found.)r"   _get_executable_infoExecutableNotFoundErrorRuntimeError)r~   s   @r3   make_pdf_to_png_converterr      s    &  .& 	& && &**40& 	& &&  ?
@@s    33A A*)A*c                   2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )
LatexError   c                 0   > [         TU ]  U5        X l        g rC   )super__init__latex_output)selfmessager   	__class__s      r3   r   LatexError.__init__   s    !(r:   c                 h    U R                   u  nU R                  (       a  USU R                  -   -  nU$ Nr   )argsr   r   r[   s     r3   __str__LatexError.__str__   s0    YY))))Ar:   )r   )rH   )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__r   s   @r3   r   r      s    ) r:   r   c                       \ rS rSrSr\S 5       r\S 5       r\\	R                  " S5      S 5       5       rS rS rS	 rS
 rSS.S jrS rS rSrg)LatexManager   z
The LatexManager opens an instance of the LaTeX application for
determining the metrics of text elements. The LaTeX environment can be
modified by setting fonts and/or a custom preamble in `.rcParams`.
c                  t    [         S[        R                  S    3S[        5       SS/n SR	                  U 5      $ )Nz% !TeX program = pgf.texsystemz\usepackage{graphicx}\begin{document}z!\typeout{pgf_backend_query_start}r   )_DOCUMENTCLASSr"   r#   r4   r%   )latex_headers    r3   _build_latex_header LatexManager._build_latex_header   sC      !o!> ?@$O0

 yy&&r:   c                 @    U R                  U R                  5       5      $ )zl
Return the previous LatexManager if the header and tex system did not
change, or a new instance otherwise.
)_get_cached_or_new_implr   )clss    r3   _get_cached_or_newLatexManager._get_cached_or_new   s     **3+B+B+DEEr:      c                     U " 5       $ rC    )r   headers     r3   r   $LatexManager._get_cached_or_new_impl   s     ur:   c                    U R                   c  U R                  5         U R                   R                  R                  U5        U R                   R                  R                  S5        U R                   R                  R	                  5         g r   )latex_setup_latex_processstdinr<   flushr   s     r3   _stdin_writelnLatexManager._stdin_writeln   s^    ::%%'

q!

t$

 r:   c                 ^   [        U5      n/ n U R                  R                  R                  S5      nUR	                  U5        U[        U5      * S  U:X  a  OEU(       d<  U R                  R                  5         S U l        [        SSR                  U5      5      eM  SR                  U5      $ )Nr   zLaTeX process haltedrH   )	listr   stdoutreadrS   lenkillr   r%   )r   r[   charscs       r3   _expectLatexManager._expect   s    G

!!&&q)ALLOc!fWX!#

!!
 !7HH  wwu~r:   c                 $    U R                  S5      $ )Nz
*)r   r   s    r3   _expect_promptLatexManager._expect_prompt   s    ||E""r:   c                    [        5       U l        U R                  R                  U l        [        R
                  " X R                  R                  5      U l        U R                  SS9  U R                  R                  S5      u  pU R                  R                  S:w  a  [        SU R                  5        3U5      eS U l	        [        R                  " U R                   5      U l        g )NFexpect_replyz
\makeatletter\@@end
r   zaLaTeX errored (probably missing font or error in preamble) while processing the following input:
)r   _tmpdirr*   tmpdirweakreffinalizecleanup_finalize_tmpdirr   r   communicate
returncoder   r   	functools	lru_cache_get_box_metrics)r   r   rp   s      r3   r   LatexManager.__init__  s    )+ll'' ' 0 0||7K7K L 	!!u!5//0KL::  A%:++-.0 	 
 
 ) 3 3D4I4I Jr:   Tr   c                V    [         R                  " [        R                  S   S/[         R                  [         R                  SU R
                  S9U l        S n[        R                  " XU R                  5      U l        U R                  U R                  5       5        U(       a"  U R                  S5        U R!                  5         g g ! [         a&  n[        [        R                  S   < S35      UeS nAf[         a&  n[        S[        R                  S   < 35      UeS nAff = f)	Nr   -halt-on-errorutf-8)r   r   encodingcwdz] not found; install it or change rcParams['pgf.texsystem'] to an available TeX implementationzError starting c                     U R                  5          U R                  5         g ! [         a    U R                  5          g f = frC   )r   r   r   wait)r   s    r3   finalize_latex9LatexManager._setup_latex_process.<locals>.finalize_latex(  s4    JJL!!# 

s   # A A z*pgf_backend_query_start)rq   Popenr"   r#   PIPEr   r   FileNotFoundErrorr   OSErrorr   r   _finalize_latexr   r   r   r   )r   r   errr   s       r3   r   !LatexManager._setup_latex_process  s   	N#))o.0@A oojoo dkk3DJ	  '//$** . 	D4467LL34! ) ! 	<<03 4O P   	N!#,,"?!BCEJMN	Ns$   AC
 

D(!C55D(!D##D(c                 6    U R                  [        X5      5      $ )zo
Get the width, total height, and descent (in TeX points) for a text
typeset by the current LaTeX environment.
)r   ra   )r   r8   r\   s      r3   get_width_height_descent%LatexManager.get_width_height_descent7  s    
 $$%<T%HIIr:   c                    U R                  SU-  5         U R                  5       n UR                  5       S   R                  S5      u  pEn[        USS 5      [        USS 5      [        USS 5      pnXxU	-   U	4$ ! [         a*  n[        SR	                  XR
                  5      5      UeSnAff = f! [         a   n[        SR	                  X5      5      UeSnAff = f)zv
Get the width, total height and descent (in TeX points) for a TeX
command's output in the current LaTeX environment.
zW{\catcode`\^=\active\catcode`\%%=\active\sbox0{%s}\typeout{\the\wd0,\the\ht0,\the\dp0}}z#Error measuring {}
LaTeX Output:
{}N,)
r   r   r   
ValueErrorformatr   
splitlinessplit	Exceptionfloat)
r   texanswerr   widthheightoffsetwhos
             r3   r   LatexManager._get_box_metrics>  s    	5		F((*F	< %+$5$5$7$;$A$A#$F!E6 cr
#U6#2;%7vcr{9Ka a%{  	F D$fS*:*:;=BEF	F  	<D$fS138;<	<s.   A< %B3 <
B0%B++B03
C=CC)r   r   r   r   r   r   N)r   r   r   r   __doc__staticmethodr   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r:   r3   r   r      s     ' ' F F   !#K& 48 !"FJr:   r   r   c                     [         R                  5       n U R                  S[        R                  " S5      R                  5       -  5         U R                  5         g! [         a!    [         R                  R                  5          gf = f)Nz&\includegraphics[interpolate=true]{%s}zimages/matplotlib.pngz\includegraphicsz	\pgfimage)
r   r   r   r   _get_data_pathr,   r   r   r   cache_clear)mans    r3   _get_image_inclusion_commandr   ^  sx    

)
)
+C1


6
7
@
@
B	CD" ,,88:s   A (BBc                      ^  \ rS rSrU 4S jr SS jrSS jrS rS rSS jr	SS	 jr
S
 rS rSS jrSS.S jrSS jrS rS rS rS rSrU =r$ )RendererPgfin  c                 j   > [         TU ]  5         UR                  U l        X l        Xl        SU l        g)aN  
Create a new PGF renderer that translates any drawing instruction
into text commands to be interpreted in a latex pgfpicture environment.

Attributes
----------
figure : `~matplotlib.figure.Figure`
    Matplotlib figure to initialize height, width and dpi from.
fh : file-like
    File handle for the output of the drawing commands.
r   N)r   r   rx   r=   figureimage_counter)r   r  r=   r   s      r3   r   RendererPgf.__init__p  s-     	::r:   Nc                 >   [        U R                  S5        SU R                  -  nU R                  U5        U R	                  X5        UR                  U5      R                  5       u  pUS   U-  US   U-  U	S   U-  U	S   U-  4n
[        U R                  SU
-  5        U R                  S X#5        U R                  UR                  5       S:g  US LS9  [        U R                  S5        S	U R                  -  nU* U* X4nUR                  US
US9 Hr  u  pUS   U-  US   U-  nn[        U R                  S5        [        U R                  SUU4-  5        [        U R                  S5        [        U R                  S5        Mt     [        U R                  S5        g )N\begin{pgfscope}r5   r   r   zQ\pgfsys@defobject{currentmarker}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{        strokefillr   B^!Vl@F)simplifyclip"\pgfsys@transformshift{%fin}{%fin}z"\pgfsys@useobject{currentmarker}{}\end{pgfscope})r?   r=   rx   _print_pgf_clip_print_pgf_path_stylesget_extents
get_points_print_pgf_path_pgf_path_drawget_linewidthiter_segments)r   gcmarker_pathmarker_transr2   transrgbFacefbltrcoordsmaxcoordr  pointcodexys                    r3   draw_markersRendererPgf.draw_markers  s    	-. M 	R ##B0 ((6AACABqEAIr!uqy"Q%!);FHNO	P 	T;=2#3#3#5#<!(!4 	 	6$ 488+	H9h9  --ee37 . 9KE8a<qAqATWW12TWWCq!fLMTWWCDTWW/09 	+,r:   c                    [        U R                  S5        U R                  U5        U R                  X5        U R	                  XX45        U R                  UR                  5       S:g  US LS9  [        U R                  S5        UR                  5       (       Gam  [        U R                  S5        U R                  X5        U R                  U5        U R	                  XX45        [        U R                  S5        [        U R                  S5        [        U R                  S5        [        U R                  S5        [        U R                  S5        [        R                  R                  5       R                  U R                  5      nU R	                  S UR                  5       U5        U R                  SS	9  [        U R                  S5        [        U R                  S
5        SU R                  -  nUR                  U5      R                  5       u  u  pxu  pXg-  Xi-  pXh-  Xj-  p[         R"                  " X-
  5      [         R"                  " X-
  5      p[        U R                  SXx4-  5        [%        U5       Hp  n[%        U5       H/  n[        U R                  S5        [        U R                  S5        M1     [        U R                  SU-  5        [        U R                  S5        Mr     [        U R                  S5        g g )Nr  r	  r
  r  \pgfusepath{clip}zN\pgfsys@defobject{currentpattern}{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}{z=\pgfpathrectangle{\pgfqpoint{0in}{0in}}{\pgfqpoint{1in}{1in}}T)r  r   r5   r  z#\pgfsys@useobject{currentpattern}{}z \pgfsys@transformshift{1in}{0in}z"\pgfsys@transformshift{-%din}{0in}z \pgfsys@transformshift{0in}{1in})r?   r=   r  r  r  r  r  	get_hatchr"   
transformsAffine2Dscalerx   get_hatch_pathr  r  mathceilrange)r   r  r2   	transformr  r/  r  xminyminxmaxymaxrepxrepyiyixs                  r3   	draw_pathRendererPgf.draw_path  s{   -.R ##B0Ry:2#3#3#5#<!(!4 	 	6+, <<>>TWW12''4   $  9>TWW23 TWWFG TWW12TWWEF TWW23NN++-33DHH=E  r'8'8':EBt,TWW/0TWWd#TXXA  +668 'LT,418$18$4;/4;1G$TWW:d\IKDk+BTWW&LMTWW&IJ & "G$"NO"EF " TWW/0O r:   c                    SU R                   -  nUR                  5       nU(       a]  UR                  5       u  pEXT-
  u  pgUS   U-  US   U-  Xb-  Xr-  4n[        U R                  SU-  5        [        U R                  S5        UR                  5       u  pU	b)  U R                  XU
5        [        U R                  S5        g g )Nr5   r   r   zA\pgfpathrectangle{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}r+  )rx   get_clip_rectangler  r?   r=   get_clip_pathr  )r   r  r  bboxp1p2r   r   r"  clippathclippath_transs              r3   r  RendererPgf._print_pgf_clip  s    M$$&__&FB7DAUQY1	15!%7FTWWI TWW23 $&#3#3#5   ~>TWW23  r:   c           	      $   SSSS.n[        U R                  X1R                  5          5        SSSS.n[        U R                  XAR                  5          5        US LnUR	                  5       (       a  UR                  5       =pgO0UR                  5       S	   nU(       a  [        U5      S	:  a  US	   OS
nU(       a;  [        U R                  S[        US S	 5      -  5        [        U R                  S5        U(       a  US
:w  a  [        U R                  SU-  5        UR                  5       [        -  [        -  nUR                  5       n	[        U R                  SU-  5        [        U R                  SU	S S	 -  5        [        U R                  S5        US
:w  a  [        U R                  SU-  5        UR                  5       u  pUc  [        U R                  S5        g [        U R                  SSR                  S U 5       5      U
4-  5        g )Nz\pgfsetbuttcapz\pgfsetroundcapz\pgfsetrectcap)buttround
projectingz\pgfsetmiterjoinz\pgfsetroundjoinz\pgfsetbeveljoin)miterrJ  bevel   r5   z(\definecolor{currentfill}{rgb}{%f,%f,%f}z\pgfsetfillcolor{currentfill}\pgfsetfillopacity{%f}z\pgfsetlinewidth{%fpt}z*\definecolor{currentstroke}{rgb}{%f,%f,%f}z!\pgfsetstrokecolor{currentstroke}\pgfsetstrokeopacity{%f}z\pgfsetdash{}{0pt}z\pgfsetdash{%s}{%fpt}rH   c              3   ,   #    U  H
  nS U-  v   M     g7f)z{%fpt}Nr   )rD   dashs     r3   rF   5RendererPgf._print_pgf_path_styles.<locals>.<genexpr>"  s     GYT	D 0Ys   )r?   r=   get_capstyleget_joinstyleget_forced_alpha	get_alphaget_rgbr   tupler  mpl_pt_to_inlatex_in_to_pt
get_dashesr%   )r   r  r  	capstyles
joinstyleshas_fillfillopacitystrokeopacitylwstroke_rgbadash_offset	dash_lists               r3   r  "RendererPgf._print_pgf_path_styles  s   .0#46	 	)OO$567  3224
 	*%5%5%789 $&  *,,,.8K-JJLOM(0S\A5E'!*3KTWW@WRa[)*+ TWW>?s*TWW7+EF ,.?jjl3b89>r?#	$ 	>?CTWW9MIJ "$TWW34TWW-GYGG#%%&r:   c                    SU R                   -  nU(       a  UR                  5       OS nSU R                   -  nU(       aR  UcO  UR                  5       u  p[        US   U* 5      [        US   U* 5      [	        U	S   U5      [	        U	S   U5      4n
OU* U* Xw4n
UR                  X:S9 GHc  u  pU[        R                  :X  a.  [        U5      u  p[        U R                  SX]-  X^-  4-  5        MH  U[        R                  :X  a  [        U R                  S5        Mt  U[        R                  :X  a.  [        U5      u  p[        U R                  SX]-  X^-  4-  5        M  U[        R                  :X  a=  [        U5      u  nnnnX-  UU-  UU-  UU-  4n[        U R                  S	U-  5        GM  U[        R                  :X  d  GM  [        U5      u  nnnnnnUU-  UU-  UU-  UU-  UU-  UU-  4n[        U R                  S
U-  5        GMf     U(       a  UR                  5       OS nUb  Uu  nnnUb  US-  nUS-  n[        U R                  S5        [        U R                  S5        [        U R                  SUU-  S SUU-  S S35        [        U R                  S[!        U5       S35        [        U R                  S5        g g g )Nr5   r  r   r   )r  z&\pgfpathmoveto{\pgfqpoint{%fin}{%fin}}z\pgfpathclosez&\pgfpathlineto{\pgfqpoint{%fin}{%fin}}zH\pgfpathquadraticcurveto{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}zW\pgfpathcurveto{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}g      ?   z\usepgfmodule{decorations}z(\usepgflibrary{decorations.pathmorphing}z/\pgfkeys{/pgf/decoration/.cd, segment length = r  zin, amplitude = zin}z\pgfmathsetseed{r   z%\pgfdecoratecurrentpath{random steps})rx   r@  r  maxminr  r   MOVETOrY  r?   r=   	CLOSEPOLYLINETOCURVE3CURVE4get_sketch_paramsint)r   r  r2   r4  r  r  rB  r#  rC  rD  r  pointsr%  r&  r'  cxcypxpyr"  c1xc1yc2xc2ysketch_paramsr/  length
randomnesss                               r3   r  RendererPgf._print_pgf_path%  s   M*,r$$&$ 488+W___&FB1y)3r!uxi+@1x(#beX*>@D Iy(=D ..y.DLFt{{"V}B%() '"23$V}B%() $!&vBBaaa7M!"# $-26]*S#sBq#'37C!GR!VR!VK5 "	"#/ E> 35,,.$$ )6%E6: #
"?@"MN #-.4qj!,< =().Ad#< = $6s:6Gr"JK"JK ! %r:   c                     / nU(       a  UR                  S5        U(       a  UR                  S5        [        U R                  SSR                  U5      -  5        g )Nr  r  z\pgfusepath{%s}r   )rS   r?   r=   r%   )r   r  r  actionss       r3   r  RendererPgf._pgf_path_drawg  sB    NN8$NN6",sxx/@@Ar:   c                     g)NTr   r   s    r3   option_scale_imageRendererPgf.option_scale_imageo  s    r:   c                 2    [         R                  S   (       + $ )Nzimage.composite_image)r"   r#   r   s    r3   option_image_nocomposite$RendererPgf.option_image_nocomposites  s    << 7888r:   c                    UR                   S S u  pgUS:X  d  US:X  a  g [        R                  R                  [	        U R
                  SS5      5      (       d  [        S5      e[        R                  " U R
                  R                  5      nSUR                  U R                  4-  n	[        R                  " US S S2   5      R                  UR                  U	-  5        U =R                  S-  sl        [!        U R
                  S	5        U R#                  U5        S
U R$                  -  n
Uc'  [!        U R
                  SX*-  X:-  4-  5        Xz-  Xj-  pgOXUR'                  5       R)                  5       u  ppnn[!        U R
                  SX-  X-  X-  X-  X-   U
-  UU-   U
-  4-  5        S=pv[+        US L 5      R-                  5       n[!        U R
                  S[/        5       UXvU	4-  5        [!        U R
                  S5        g )Nrh  r   r*   rH   zXstreamed pgf-code does not support raster graphics, consider using the pgf-to-pdf optionz%s-img%d.pngr   r  r5   r  z/\pgfsys@transformcm{%f}{%f}{%f}{%f}{%fin}{%fin}zD\pgftext[left,bottom]{%s[interpolate=%s,width=%fin,height=%fin]{%s}}r  )shapers   r2   existsgetattrr=   r   r'   r   r*   stemr  r   	fromarraysaver+   r?   r  rx   frozen	to_valuesrh   lowerr   )r   r  r&  r'  imr4  r   r   r2   	fname_imgr  tr1tr2tr3tr4tr5tr6interps                     r3   
draw_imageRendererPgf.draw_imagew  s    xx|6Q!Vww~~gdggvr:;;./ /
 ||DGGLL)"dii1C1C%DD	4R4!&&t{{Y'>?a 	-.R MTWW:aeQU^KM5!%q+4+;+;+=+G+G+I(CcSTWWGgsww!mcAg]445 IAY$&'--/D.0!	++	,
 	+,r:   )mtextc                *    U R                  XX4XVSUS9  g )NTeX)ismathr  )	draw_text)r   r  r&  r'  r[   r\   angler  s           r3   draw_texRendererPgf.draw_tex  s    raDUKr:   c	                 Z   [        XE5      n[        U R                  S5        U R                  U5        UR	                  5       n	U	S:w  a2  [        U R                  SU	-  5        [        U R                  SU	-  5        [        UR                  5       5      S S n
[        U R                  SU
-  5        [        U R                  S5        [        U R                  S5        S	U-   nU R                  R                  n/ nU(       a  US
:X  d  UR                  5       S:X  a  UR                  5       S:w  a  UR                  5       nUR                  5       R                  U5      u  p#SSSS.nSSSSS.nUR                  SX+-  S S3SX;-  S S3XR                  5          XR                  5          /5        OUR!                  SX+-  S SX;-  S S35        US
:w  a  UR!                  SU-  5        [        U R                  SSR#                  U5      < SU< S35        [        U R                  S 5        g )!Nr  r5   rO  rP  rN  z&\definecolor{textcolor}{rgb}{%f,%f,%f}z\pgfsetstrokecolor{textcolor}z\pgfsetfillcolor{textcolor}z\color{textcolor}r   anchorcenter_baselineleftrightrH   )r  r  centertopbottombase)r  r  baseliner  zx=r  inzy=zin, y=zin, left, basez	rotate=%fz	\pgftext[r   z]{r   r  )ra   r?   r=   r  rW  rY  rX  r  rx   get_rotation_modeget_verticalalignmentget_unitless_positionget_transformr4  extendget_horizontalalignmentrS   r%   )r   r  r&  r'  r[   r\   r  r  r  alphargbrx   	text_argsposhalignvaligns                   r3   r  RendererPgf.draw_text  s    $A,-.R C<TWW7%?@TWW9EABBJJL!"1%CcIJ:;89 1$kkoo	!((*h6++-1BB --/C&&(2237DA$w"EF"h"(B8FQU1IR QU1IR 44672245	  r!%&q	HIA:[501)1DaHI+,r:   c                     [         R                  5       R                  X5      u  pEn[        U R                  -  nXG-  XW-  Xg-  4$ rC   )r   r   r   rZ  rx   )r   r[   r\   r  r   r   dr  s           r3   get_text_width_height_descent)RendererPgf.get_text_width_height_descent  sG      224,,Q5 	a
 488#uaeQU""r:   c                     g)NFr   r   s    r3   flipyRendererPgf.flipy  s    r:   c                     U R                   R                  5       U R                  -  U R                   R                  5       U R                  -  4$ rC   )r  get_figwidthrx   get_figheightr   s    r3   get_canvas_width_height#RendererPgf.get_canvas_width_height  s>    ((*TXX5))+dhh68 	8r:   c                 .    U[         -  U R                  -  $ rC   )rZ  rx   )r   rr  s     r3   points_to_pixelsRendererPgf.points_to_pixels  s    $txx//r:   )rx   r=   r  r  rC   )TF)FN)r   r   r   r   r   r(  r=  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   s   @r3   r  r  n  sl    ( "$-L31j4(1&f@LDB9'-R ;? L.-`	#8
0 0r:   r  c                   h   ^  \ rS rSrSSSS.rS rSS.S	 jrS
 rSS.S jrS r	S r
U 4S jrSrU =r$ )FigureCanvasPgfi  zLaTeX PGF picturezLaTeX compiled PGF picturezPortable Network Graphics)pgfpdfpngc                     g)Nr  r   r   s    r3   get_default_filetype$FigureCanvasPgf.get_default_filetype  s    r:   Nbbox_inches_restorec          
         SnS/n[        5       R                  5        H  nUR                  SU-   5        M     UR                  S5        SR                  U5      nU R                  R                  5       U R                  R                  5       pvU R                  R                  nUR                  U5        UR                  U5        UR                  S5        [        US5        [        US5        [        US5        [        US	Xg4-  5        [        US
5        [        U R                  XgU[        U R                  U5      US9n	U R                  R                  U	5        [        US5        [        US5        [        US5        g )Na  %% Creator: Matplotlib, PGF backend
%%
%% To include the figure in your LaTeX document, write
%%   \input{<filename>.pgf}
%%
%% Make sure the required packages are loaded in your preamble
%%   \usepackage{pgf}
%%
%% Also ensure that all the required font packages are loaded; for instance,
%% the lmodern package is sometimes necessary when using math font.
%%   \usepackage{lmodern}
%%
%% Figures using additional raster images can only be included by \input if
%% they are in the same directory as the main LaTeX file. For loading figures
%% from other directories you can use the `import` package
%%   \usepackage{import}
%%
%% and then include the figures with
%%   \import{<path to file>}{<filename>.pgf}
%%
z)%% Matplotlib used the following preamblez%%   z%%r   z\begingroupz\makeatletterz\begin{pgfpicture}z:\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}z&\pgfusepath{use as bounding box, clip}r  z\end{pgfpicture}z\makeatotherz	\endgroup)r4   r   rS   r%   r  r  r  rx   r<   r?   r   r  draw)
r   r=   r  header_textheader_info_preambler>   r   r   rx   renderers
             r3   _print_pgf_to_fh FigureCanvasPgf._print_pgf_to_fh  sO   . !LL!O..0D ''$7 1##D)#yy)=> {{'')4;;+D+D+F1kkoo 	
%&
^$%&*+N6	 	>?$T[[!%0b%A9LN 	" 	()_%\"r:   c                     [         R                  " USSS9 n[         R                  " U5      (       d  [        R                  " S5      " U5      nU R
                  " U40 UD6  SSS5        g! , (       d  f       g= f)za
Output pgf macros for drawing the figure so it can be included and
rendered in latex documents.
r   r   r   N)r   open_file_cmfile_requires_unicodecodecs	getwriterr  )r   fname_or_fhkwargsfiles       r3   	print_pgfFigureCanvasPgf.print_pgf-  s^    
 S7Ct..t44''06!!$1&1 DCCs   AA++
A9metadatac                   U R                   R                  5       u  pE[        SU=(       d    0 5      nSR                  S UR	                  5        5       5      n[        5        n[        R                  " U5      n	U R                  " U	S-  40 UD6  U	S-  R                  SR                  [        SU-  SXE4-  S	[        5       S
SSS/	5      SS9  [        R                  S   n
[        R                  " U
SSS/[         US9  U	S-  R#                  S5       n[        R$                  " US5       n[&        R(                  " X5        SSS5        SSS5        SSS5        g! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g= f)z3Use LaTeX to compile a pgf generated figure to pdf.r  r   c              3   <   #    U  H  u  p[        X5      v   M     g 7frC   rk   rD   kvs      r3   rF   ,FigureCanvasPgf.print_pdf.<locals>.<genexpr><  s       B/@tqQ""/@   z
figure.pgfz
figure.texr   #\usepackage[pdfinfo={%s}]{hyperref}8\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}\usepackage{pgf}r   z
\centeringz\input{figure.pgf}z\end{document}r   r  r   -interaction=nonstopmoder   r   
figure.pdfrbwbN)r  get_size_inchesr   r%   itemsr   r'   r   r  
write_textr   r4   r"   r#   r   _check_and_log_subprocessrW   openr  shutilcopyfileobj)r   r  r  r  r   r   	info_dictpdfinfor   tmppath
texcommandorigdests                r3   	print_pdfFigureCanvasPgf.print_pdf7  sk   {{**,)%R@	(( B/8/@B B  !Vll6*GNN7\1<V<|#//		":WDOf'!O'!)%  % 0 & o6J++79I $&2 \)//5$$[$74""4. 8 6) "!* 87 65) "!s=   "B0E<E+*EE+	E<
E($E++
E9	5E<<
F
c                    [        5       n[        5        n[        R                  " U5      nUS-  nUS-  nU R                  " U40 UD6  U" XgU R
                  R                  S9  UR                  S5       n[        R                  " US5       n	[        R                  " X5        SSS5        SSS5        SSS5        g! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g= f)z?Use LaTeX to compile a pgf figure to pdf and convert it to png.r  z
figure.png)rx   r  r  N)r   r   r'   r   r  r  rx   r  r   r  r   r  )
r   r  r  	converterr   r  pdf_pathpng_pathr  r  s
             r3   	print_pngFigureCanvasPgf.print_pngX  s    -/	!Vll6*G-H-HNN8.v.hdkkoo>--%$$[$74""4. 8 & "! 87 &% "!s<   A C5CB=$C,C=
CC
C	C
C-c                 .    [        U R                  S 5      $ rC   )r  r  r   s    r3   get_rendererFigureCanvasPgf.get_renderere  s    4;;--r:   c                 T   > U R                   R                  5         [        TU ]  5       $ rC   )r  draw_without_renderingr   r  )r   r   s    r3   r  FigureCanvasPgf.drawh  s    **,w|~r:   r   )r   r   r   r   	filetypesr  r  r  r  r  r  r  r   r   r   s   @r3   r  r    sG    +437I ;? 6#p2 26 /B/. r:   r  c                       \ rS rSr\rSrg)_BackendPgfip  r   N)r   r   r   r   r  FigureCanvasr   r   r:   r3   r  r  p  s    "Lr:   r  c                   T    \ rS rSrSrSS.S jrS rS rS rS	 r	S
 r
SS jrS rSrg)PdfPagesiu  am  
A multi-page PDF file using the pgf backend

Examples
--------
>>> import matplotlib.pyplot as plt
>>> # Initialize:
>>> with PdfPages('foo.pdf') as pdf:
...     # As many times as you like, create a figure fig and save it:
...     fig = plt.figure()
...     pdf.savefig(fig)
...     # When no figure is specified the current figure is saved
...     pdf.savefig()
Nr  c                    Xl         SU l        U=(       d    0 R                  5       U l        [	        SU R                  5      U l        [        5       U l        g)a!  
Create a new PdfPages object.

Parameters
----------
filename : str or path-like
    Plots using `PdfPages.savefig` will be written to a file at this
    location. Any older file with the same name is overwritten.

metadata : dict, optional
    Information dictionary object (see PDF reference section 10.2.1
    'Document Information Dictionary'), e.g.:
    ``{'Creator': 'My software', 'Author': 'Me', 'Title': 'Awesome'}``.

    The standard keys are 'Title', 'Author', 'Subject', 'Keywords',
    'Creator', 'Producer', 'CreationDate', 'ModDate', and
    'Trapped'. Values have been predefined for 'Creator', 'Producer'
    and 'CreationDate'. They can be removed by setting them to `None`.

    Note that some versions of LaTeX engines may ignore the 'Producer'
    key and set it to themselves.
r   r  N)_output_name
_n_figurescopy	_metadatar   
_info_dictr   _file)r   filenamer  s      r3   r   PdfPages.__init__  s@    . %".b..0/t~~FY
r:   c           	         SR                  S U R                  R                  5        5       5      nSR                  [        SU-  SX4-  S[	        5       SS/5      nU R
                  R                  UR                  S	5      5        g )
Nr   c              3   <   #    U  H  u  p[        X5      v   M     g 7frC   r  r  s      r3   rF   )PdfPages._write_header.<locals>.<genexpr>  s       H/FtqQ""/Fr  r   r  r  r  z\setlength{\parindent}{0pt}z\begin{document}%r   )r%   r!  r  r   r4   r"  r<   encode)r   width_inchesheight_inchesr  r   s        r3   _write_headerPdfPages._write_header  s    (( H/3/D/D/FH Hyy2W<G+,O* 	"
 	 	

,,W56r:   c                     U $ rC   r   r   s    r3   	__enter__PdfPages.__enter__  s    r:   c                 $    U R                  5         g rC   )close)r   exc_typeexc_valexc_tbs       r3   __exit__PdfPages.__exit__  s    

r:   c                     U R                   R                  S5        U R                  S:  a  U R                  5         U R                   R	                  5         g)zk
Finalize this object, running LaTeX in a temporary directory
and moving the final pdf file to *filename*.
s   \end{document}\nr   N)r"  r<   r  
_run_latexr1  r   s    r3   r1  PdfPages.close  s=    
 	

-.??QOO

r:   c                    [         R                  S   n[        5        n[        R                  " US5      nUR                  U R                  R                  5       5        [        R                  " USSU/[        US9  [        R                  " UR                  S5      U R                  5        S S S 5        g ! , (       d  f       g = f)Nr   zpdf_pages.texr  r   r  z.pdf)r"   r#   r   r'   r   write_bytesr"  getvaluer   r  rW   r   movewith_suffixr  )r   r  r   
tex_sources       r3   r8  PdfPages._run_latex  s    \\/2
!V fo>J""4::#6#6#89++79I&" KK
..v68I8IJ "!!s   BB66
Cc                    [        U[        5      (       dV  Uc  [        R                  " 5       nO[        R                  " U5      nUc  [        SU 35      eUR                  R                  nUR                  5       u  pEU R                  S:X  a  U R                  XE5        OU R                  R                  SXE4-  5        UR                  " U R                  4SSS.UD6  U =R                  S-  sl	        g)z
Save a `.Figure` to this file as a new page.

Any other keyword arguments are passed to `~.Figure.savefig`.

Parameters
----------
figure : `.Figure` or int, default: the active figure
    The figure, or index of the figure, that is saved to the file.
Nz
No figure r   s   \newpage\ifdefined\pdfpagewidth\pdfpagewidth\else\pagewidth\fi=%fin\ifdefined\pdfpageheight\pdfpageheight\else\pageheight\fi=%fin%%
r  )r   backendr   )re   r   r   
get_activeget_fig_managerr   canvasr  r  r  r+  r"  r<   savefig)r   r  r  managerr   r   s         r3   rF  PdfPages.savefig  s     &&))~..*--f5 :fX!677^^**F..0??au-
 JJ !/* 	tzzI%I&I1r:   c                     U R                   $ )z=Return the current number of pages in the multipage pdf file.)r  r   s    r3   get_pagecountPdfPages.get_pagecount  s    r:   )r"  r!  r   r  r  rC   )r   r   r   r   r   r   r+  r.  r5  r1  r8  rF  rJ  r   r   r:   r3   r  r  u  s6     .2 :7	K"Hr:   r  )@r  rf   r   ior   loggingr1  rs   r'   r   rq   tempfiler   r   PILr   
matplotlibr"   r   r   r(   matplotlib.backend_basesr   r	   r
   r   !matplotlib.backends.backend_mixedr   matplotlib.backends.backend_pdfr   r   matplotlib.pathr   matplotlib.figurer   matplotlib.font_managerr   matplotlib._pylab_helpersr   	getLoggerr   rW   r   r4   latex_pt_to_inr[  rZ  mpl_in_to_ptr9   r?   ra   rk   r   r   r   r   r   r   r  r  FigureManagerPgfexportr  r  r   r:   r3   <module>r]     s5         	    '    0  @-   $ 2 )" ,&V n$L =*Z A6	 	U Up Q |0, |0~}& }@ %  
#( # #} }r:   