
    
3j                     .   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                  N   S n [        [        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/U " 5       QSPO/ Q[        R
                  R                  SSS9P5      $ )z=Prepare a LaTeX preamble based on the rcParams configuration.c               3   (  #    S H  u  p[         R                  " U5      n[        R                  " U5      nSU < SUR                  < SUR
                  R                  5       < SUR                  S:X  a  SUR                  S 3OS	< S
3	v   M     g 7f)N))setmainfontserif)setsansfontzsans\-serif)setmonofont	monospacez  \{z}[Path=\detokenize{z/}z.ttcz,FontIndex=d ])	fmfindfontpathlibr   nameparentas_posixsuffix
face_index)commandfamily	font_pathpaths       Y/home/wildlama/miniconda3/lib/python3.13/site-packages/matplotlib/backends/backend_pgf.py_to_fontspec#_get_preamble.<locals>._to_fontspec)   sy      >OG F+I<<	*DDKK$8$8$::>++:O+i22156UWWY Y >s   BB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}z\fi
underscorestrings)option)r   mplrcParamsget_size_in_pointsjoin
texmanager_usepackage_if_not_loaded)r+   font_size_pts     r*   _get_preambler<   '   s    Y "s||K'@ATTVL99 	"?
 	)2\AE|+
,	-^$%. ||M2 A& ^ 	 9;/2 	00i0P3      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    r*   _tex_escaperB   Y   s    
 <<(*;<<r=   c                 H    U R                  U5        U R                  S5        g )Nz%
)write)fhlines     r*   _writelnrG   a   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   r1   z\fontfamily{\familydefault}c              3   @   >#    U  H  oR                   T:H  v   M     g 7fN)r!   ).0fontr'   s     r*   	<genexpr>*_escape_and_apply_props.<locals>.<genexpr>v   s     D-CTYY& -Cs   z9\ifdefined\pdftexversion\else\setmainfont{%s}\rmfamily\fizIgnoring unknown font: %sz\fontsize{%f}{%f}r/   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%{\%}r0   )
get_familyappendr5   r6   anyr   fontManagerttflist_logwarningr7   	get_style
get_weightr8   rB   )spropcommandsfamiliesr-   styles
boldstylesr'   s          @r*   _escape_and_apply_propsrh   h   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           	      .  ^ ^ [        T[        R                  5      (       a  [        T5      mO-T S:X  a  TR                  R	                  S5      mO[        T5      mSn[        U U4S jU 5       5      (       a  [        ST < ST< SU S35      eT  S	T S
3$ )z;Convert metadata key/value to a form that hyperref accepts.Trappedasciiz\{}[]()c              3   2   >#    U  H  oTT-   ;   v   M     g 7frK    )rL   ckeyvalues     r*   rN   #_metadata_to_str.<locals>.<genexpr>   s     
3]]s   zInvalid metadata value for z: z'. The value must not contain the chars .z={r0   )
isinstancedatetimer   r!   decodestrr[   
ValueError)ro   rp   invalid_charss   `` r*   _metadata_to_strry      s    %**++ '			

!!'*E
 M

3]
333)#5) <44A?!EF 	F 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$ )	Nr{   z-singlefilez-transpz-pngz-rz%dr   stderr)
subprocesscheck_outputosr)   splitextSTDOUT)pdffilepngfiledpis      r*   <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%dr}   )r   r   
executabler   )r   r   r   gs_infos      r*   r   r      sT    Z-D-D	9k<1$&7 "3g"=c\7$ $$.&r=   z&No suitable pdf to png renderer found.)r5   _get_executable_infoExecutableNotFoundErrorRuntimeError)r   s   @r*   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 rK   )super__init__latex_output)selfmessager   	__class__s      r*   r   LatexError.__init__   s    !(r=   c                 h    U R                   u  nU R                  (       a  USU R                  -   -  nU$ Nr.   )argsr   r   rb   s     r*   __str__LatexError.__str__   s0    YY))))Ar=   )r   )r   )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__r   s   @r*   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.   )_DOCUMENTCLASSr5   r6   r<   r8   )latex_headers    r*   _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    r*   _get_cached_or_newLatexManager._get_cached_or_new   s     **3+B+B+DEEr=      c                     U " 5       $ rK   rm   )r   headers     r*   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stdinrD   flushr   s     r*   _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 haltedr   )	listr   stdoutreadrZ   lenkillr   r8   )r   rb   charsrn   s       r*   _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    r*   _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   r~   s      r*   r   LatexManager.__init__  s    )+ll'' ' 0 0||7K7K L 	!!u!5//0KL::  A%:++-.0 	 
 
 ) 3 3D4I4I Jr=   Tr   c                X    [         R                  " [        R                  S   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-no-shell-escape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rK   )r   r   r   wait)r   s    r*   finalize_latex9LatexManager._setup_latex_process.<locals>.finalize_latex2  s4    JJL!!# 

s   # A A z*pgf_backend_query_start)r   Popenr5   r6   PIPEr   r   FileNotFoundErrorr   OSErrorr   r   _finalize_latexr   r   r   r   )r   r   errr   s       r*   r   !LatexManager._setup_latex_process  s   	N#))o.0@BTU 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)!C66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   rh   )r   rA   rc   s      r*   get_width_height_descent%LatexManager.get_width_height_descentA  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   rw   formatr   
splitlinessplit	Exceptionfloat)
r   texanswerr   widthheightoffsetwhos
             r*   r   LatexManager._get_box_metricsH  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   rm   r=   r*   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    r*   _get_image_inclusion_commandr  h  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$ )RendererPgfix  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   r   rE   figureimage_counter)r   r  rE   r   s      r*   r   RendererPgf.__init__z  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}r>   r   r   zQ\pgfsys@defobject{currentmarker}{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}{        strokefillr0   B^!Vl@F)simplifyclip"\pgfsys@transformshift{%fin}{%fin}z"\pgfsys@useobject{currentmarker}{}\end{pgfscope})rG   rE   r   _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_transr)   transrgbFacefbltrcoordsmaxcoordr  pointcodexys                    r*   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  r0   r>   r  z#\pgfsys@useobject{currentpattern}{}z \pgfsys@transformshift{1in}{0in}z"\pgfsys@transformshift{-%din}{0in}z \pgfsys@transformshift{0in}{1in})rG   rE   r  r  r"  r#  r$  	get_hatchr5   
transformsAffine2Dscaler   get_hatch_pathr   r!  mathceilrange)r   r&  r)   	transformr*  r;  r+  xminyminxmaxymaxrepxrepyiyixs                  r*   	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 )Nr>   r   r   zA\pgfpathrectangle{\pgfqpoint{%fin}{%fin}}{\pgfqpoint{%fin}{%fin}}r7  )r   get_clip_rectangler!  rG   rE   get_clip_pathr"  )r   r&  r+  bboxp1p2r  r  r.  clippathclippath_transs              r*   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)miterrV  bevel   r>   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}r   c              3   ,   #    U  H
  nS U-  v   M     g7f)z{%fpt}Nrm   )rL   dashs     r*   rN   5RendererPgf._print_pgf_path_styles.<locals>.<genexpr>,  s     GYT	D 0Ys   )rG   rE   get_capstyleget_joinstyleget_forced_alpha	get_alphaget_rgbr   tupler$  mpl_pt_to_inlatex_in_to_pt
get_dashesr8   )r   r&  r*  	capstyles
joinstyleshas_fillfillopacitystrokeopacitylwstroke_rgbadash_offset	dash_lists               r*   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 )Nr>   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{r0   z%\pgfdecoratecurrentpath{random steps})r   rL  r!  maxminr%  r   MOVETOre  rG   rE   	CLOSEPOLYLINETOCURVE3CURVE4get_sketch_paramsint)r   r&  r)   r@  r*  r+  rN  r/  rO  rP  r  pointsr1  r2  r3  cxcypxpyr.  c1xc1yc2xc2ysketch_paramsr;  length
randomnesss                               r*   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   )rZ   rG   rE   r8   )r   r  r  actionss       r*   r#  RendererPgf._pgf_path_drawq  sB    NN8$NN6",sxx/@@Ar=   c                     g)NTrm   r   s    r*   option_scale_imageRendererPgf.option_scale_imagey  s    r=   c                 2    [         R                  S   (       + $ )Nzimage.composite_image)r5   r6   r   s    r*   option_image_nocomposite$RendererPgf.option_image_nocomposite}  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 )Nrt  r   r!   r   zXstreamed pgf-code does not support raster graphics, consider using the pgf-to-pdf optionz%s-img%d.pngr   r  r>   r  z/\pgfsys@transformcm{%f}{%f}{%f}{%f}{%fin}{%fin}zD\pgftext[left,bottom]{%s[interpolate=%s,width=%fin,height=%fin]{%s}}r  )shaper   r)   existsgetattrrE   rw   r    r   r!   stemr  r   	fromarraysaver"   rG   r  r   frozen	to_valuesrv   lowerr  )r   r&  r2  r3  imr@  r  r  r)   	fname_imgr+  tr1tr2tr3tr4tr5tr6interps                     r*   
draw_imageRendererPgf.draw_image  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&  r2  r3  rb   rc   angler  s           r*   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  r>   r[  r\  rZ  z&\definecolor{textcolor}{rgb}{%f,%f,%f}z\pgfsetstrokecolor{textcolor}z\pgfsetfillcolor{textcolor}z\color{textcolor}r   anchorcenter_baselineleftrightr   )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]{r0   r  )rh   rG   rE   r  rc  re  rd  r  r   get_rotation_modeget_verticalalignmentget_unitless_positionget_transformr@  extendget_horizontalalignmentrZ   r8   )r   r&  r2  r3  rb   rc   r  r  r  alphargbr   	text_argsposhalignvaligns                   r*   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$ rK   )r   r   r   rf  r   )r   rb   rc   r  r  r  r   r+  s           r*   get_text_width_height_descent)RendererPgf.get_text_width_height_descent  sG      224,,Q5 	a
 488#uaeQU""r=   c                     g)NFrm   r   s    r*   flipyRendererPgf.flipy  s    r=   c                     U R                   R                  5       U R                  -  U R                   R                  5       U R                  -  4$ rK   )r  get_figwidthr   get_figheightr   s    r*   get_canvas_width_height#RendererPgf.get_canvas_width_height  s>    ((*TXX5))+dhh68 	8r=   c                 .    U[         -  U R                  -  $ rK   )rf  r   )r   r~  s     r*   points_to_pixelsRendererPgf.points_to_pixels  s    $txx//r=   )r   rE   r  r  rK   )TF)FN)r   r   r   r   r   r4  rI  r  r  r"  r#  r  r  r  r  r  r  r  r  r  r   r   r   s   @r*   r  r  x  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  rm   r   s    r*   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)r<   r   rZ   r8   r  r  r  r   rD   rG   r   r  draw)
r   rE   r  header_textheader_info_preamblerF   r  r  r   renderers
             r*   _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       r*   	print_pgfFigureCanvasPgf.print_pgf7  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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rK   ry   rL   kvs      r*   rN   ,FigureCanvasPgf.print_pdf.<locals>.<genexpr>F  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   r   
figure.pdfrbwbN)r  get_size_inchesr   r8   itemsr   r    r   r  
write_textr   r<   r5   r6   r   _check_and_log_subprocessr^   openr  shutilcopyfileobj)r   r  r  r  r  r  	info_dictpdfinfor   tmppath
texcommandorigdests                r*   	print_pdfFigureCanvasPgf.print_pdfA  sn   {{**,)%R@	(( B/8/@B B  !Vll6*GNN7\1<V<|#//		":WDOf'!O'!)%  % 0 & o6J++79I#\348fF \)//5$$[$74""4. 8 6) "!* 87 65) "!s=   "B1E=E,+EE,
E=
E)%E,,
E:	6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)r   r  r  N)r   r   r    r   r  r  r   r
  r   r  r  r  )
r   r  r  	converterr   r  pdf_pathpng_pathr  r  s
             r*   	print_pngFigureCanvasPgf.print_pngb  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      $ rK   )r  r  r   s    r*   get_rendererFigureCanvasPgf.get_renderero  s    4;;--r=   c                 T   > U R                   R                  5         [        TU ]  5       $ rK   )r  draw_without_renderingr   r  )r   r   s    r*   r  FigureCanvasPgf.drawr  s    **,w|~r=   rm   )r   r   r   r   	filetypesr  r  r  r  r  r  r  r   r   r   s   @r*   r  r    sG    +437I ;? 6#p2 26 /B/. r=   r  c                       \ rS rSr\rSrg)_BackendPgfiz  rm   N)r   r   r   r   r  FigureCanvasr   rm   r=   r*   r#  r#  z  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)PdfPagesi  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      r*   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rK   r  r  s      r*   rN   )PdfPages._write_header.<locals>.<genexpr>  s       H/FtqQ""/Fr  r.   r  r  r   z\setlength{\parindent}{0pt}z\begin{document}%r   )r8   r,  r  r   r<   r-  rD   encode)r   width_inchesheight_inchesr  r   s        r*   _write_headerPdfPages._write_header  s    (( H/3/D/D/FH Hyy2W<G+,O* 	"
 	 	

,,W56r=   c                     U $ rK   rm   r   s    r*   	__enter__PdfPages.__enter__  s    r=   c                 $    U R                  5         g rK   )close)r   exc_typeexc_valexc_tbs       r*   __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-  rD   r)  
_run_latexr<  r   s    r*   r<  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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   r  z.pdf)r5   r6   r   r    r   write_bytesr-  getvaluer   r	  r^   r  movewith_suffixr(  )r   r  r   
tex_sources       r*   rC  PdfPages._run_latex  s    \\/2
!V fo>J""4::#6#6#89++79I#Z1&" KK
..v68I8IJ "!!s   BB77
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   )rs   r   r   
get_activeget_fig_managerrw   canvasr  r  r)  r6  r-  rD   savefig)r   r  r  managerr   r   s         r*   rQ  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    r*   get_pagecountPdfPages.get_pagecount  s    r=   )r-  r,  r+  r)  r(  rK   )r   r   r   r   r  r   r6  r9  r@  r<  rC  rQ  rU  r   rm   r=   r*   r&  r&    s6     .2 :7	K"Hr=   r&  )@r  rt   r   ior   loggingr=  r   r    r  r   tempfiler   r   PILr   
matplotlibr5   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   r^   r   r<   latex_pt_to_inrg  rf  mpl_in_to_ptrB   rG   rh   ry   r   r   r   r   r   r  r  r  FigureManagerPgfexportr#  r&  rm   r=   r*   <module>rh     s5         	    '    0  @-   $ 2 )" ,'X n$L =*Z (A6	 	U Up Q |0, |0~}& }@ %  
#( # #} }r=   