
    wg7                         d dl Z d dlmZ d dlZd dlZ	 d dlmZmZmZ d dl
mZ d dlmZ ddlmZ dd	giZd
 Zd Zd ZdddddddZ eddd      	 	 	 dd       Zy# e	$ r Y Gw xY w)    N)join)STDOUTCalledProcessErrorcheck_output)doctest_depends_on)debug   )latex)previewpygletc                  L    t         j                  dk(  rd}nd}t        | d|i|S )Nnti   r   creationflags)osnamer   )argskwargscreation_flags      [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/printing/preview.py_check_output_no_windowr      s.     
ww$"E]EfEE    c                 &   t        j                  dt        j                  j	                  |       d   d      5 }t        | d      5 }t        j                  ||       ddd       ddd       ddl}|j                         dk(  r"ddl
}|j                  d	j                  f       y|j                         d
k(  r t        j                  j                         yddl
}|j                  dj                  f       y# 1 sw Y   xY w# 1 sw Y   xY w)a   Open fname with the default system viewer.

    In practice, it is impossible for python to know when the system viewer is
    done. For this reason, we ensure the passed file will not be deleted under
    it, and this function does not attempt to block.
    zsympy-preview-r	   F)prefixsuffixdeleterbNr   DarwinopenWindowszxdg-open)tempfileNamedTemporaryFiler   pathsplitextr   shutilcopyfileobjplatformsystem
subprocesscallr   	startfile)fnamefmttemp_ffr&   r(   s         r   system_default_viewerr/      s     
	$	$,<,.GG,<,<U,CA,F,1
3 *6<% 	*!q&)	** H$-.		i	'
V[[!V[[12	* 	** *s#   DC;D;D	 DDc                   	
 	 ddl m}m}m	 ddlm ddlm} 	 |j                  |       
d}	j                  d	      }|j                  
j                  d
|z  z   
j                  d
|z  z   dd|      j!                  d       	 fd_        fd}|_        	
fd}|_        j(                  s-j+                          j-                          j(                  s-j1                          y # t        $ r t        d      w xY w# |$ r t        dj                  |            w xY w# t.        $ r Y Ww xY w)Nr   )windowimagegl)key)ImageDecodeExceptionz:pyglet is required for preview.
 visit https://pyglet.org/z,pyglet preview does not work for '{}' files.   F)double_buffer   SymPy)widthheightcaption	resizableconfigc                      d _         y )NT)has_exit)wins   r   on_closezpyglet_viewer.<locals>.on_closeP   s
    CLr   c                 J    | j                   j                  fv r         y y )N)QESCAPE)symbol	modifiersr4   rB   s     r   on_key_pressz#pyglet_viewer.<locals>.on_key_pressU   s"    #%%,,
 -r   c                       j                  dddd        j                   j                         j                  j                  j                  z
  dz  j
                  j
                  z
  dz         y )Ng      ?r8   )glClearColorglClearGL_COLOR_BUFFER_BITblitr:   r;   )r3   imgrA   s   r   	on_exposez pyglet_viewer.<locals>.on_expose[   s]    OOCc3/JJr--.HHSYY&!+cjj(A-r   )r   r1   r2   r3   pyglet.windowr4   pyglet.image.codecsr5   ImportErrorload
ValueErrorformatConfigWindowr:   r;   	set_vsyncrB   rH   rO   r@   dispatch_eventsflipKeyboardInterruptclose)r+   r,   r1   r2   r5   offsetr>   rH   rO   r3   rN   r4   rB   rA   s            @@@@@r   pyglet_viewerr^   5   sG   Y,,%<Ujj FYYUY+F
--ii!F("zzAfH$  C MM%	   	 (	 ",,!HHJ ,, IIKe  YWXXY
   UGNNsSTTUT  s)   C< D AD7 <D D47	EE T)preamblepackagesextra_preambleeulerfontsizec                h   |h|dz   }|r|dz  }ddj                  |D cg c]  }d|z  	 c}      z   }	|r|	|z  }	|sd}n!t        |t              rdj                  |      }d|d|	d	}n|s|rt	        d
      t        | t
              r| }
ndt        | fddi|z   dz   }
|dz   |
z   dz   dz   S c c}w )zA
    Generate string of a LaTeX document rendering ``expr``.
    )amsmathamsfonts)rc   
z\usepackage{%s}12ptz{}ptz\documentclass[varwidth,z]{standalone}
z

\begin{document}
zcThe "packages" or "extra_preamble" keywordsmust not be set if a custom LaTeX preamble was specifiedz$\displaystyle modeplain$z

z\end{document})r   
isinstanceintrU   rT   strr
   )exprr`   ra   rb   rc   rd   latex_settingsactual_packagesppackage_includeslatex_strings              r   _get_latex_mainrv   o   s
   
 "%<<z)O$))6E-G12 .@!-C -G #H H.H#&}}X.H
 !	# ~ C D D $*dCCNCD d?\)F25FFF7-Gs   B/
)r
   dvipng)r   )evincegimpzsuperior-dvi-viewer)exemodulesdisable_viewersc           
         ||dk(  r	 ddl }t        }|>g dg dg dd}|j                  |g       D ]  }t	        j
                  |      }||} n |t        }|dk(  r|Kt        d	      |d
k(  r|9t        d      t        |      s#t	        j
                  |      st        d|z        t        | f||||
|d|}t        d       t        |       t        j                         5 }t        t        |d      dd      5 }|j!                  |       ddd       |	 t	        j"                  t        |d      |	       t	        j
                  d      st%        d      	 t'        g d|t(               d|z  }|dk7  rdgg ddgdgd}	 ||   }|D ]  }t	        j
                  |      }|s|g} n= t1        |      d kD  rt%        d!d"j                  |      z        t%        d#|d   z        g d$d%gd&}d'|d(gd(|gd'|d(gd'|d(gd'|d(gd'|d(gd)}||j3                  |       n!|j3                  |j                  |g              |j3                  ||          	 t'        ||t(               |dk(  r!t	        j4                  t        ||      |       ny|d
k(  r?t        t        ||      d-      5 }|j!                  |j7                                ddd       n5t        |      r |t        ||      |.       n	 t'        ||g|t(               ddd       y# t        $ r Y w xY w# 1 sw Y   xY w# t*        $ r}t%        d|j,                  z        d}~ww xY w# t.        $ r t        d|z        dw xY w# t*        $ r/}t%        d*d+j                  |      d,|j,                        d}~ww xY w# 1 sw Y   xY w# t*        $ r#}t%        d*|d+|d,|j,                        d}~ww xY w# 1 sw Y   yxY w)/a  
    View expression or LaTeX markup in PNG, DVI, PostScript or PDF form.

    If the expr argument is an expression, it will be exported to LaTeX and
    then compiled using the available TeX distribution.  The first argument,
    'expr', may also be a LaTeX string.  The function will then run the
    appropriate viewer for the given output format or use the user defined
    one. By default png output is generated.

    By default pretty Euler fonts are used for typesetting (they were used to
    typeset the well known "Concrete Mathematics" book). For that to work, you
    need the 'eulervm.sty' LaTeX style (in Debian/Ubuntu, install the
    texlive-fonts-extra package). If you prefer default AMS fonts or your
    system lacks 'eulervm' LaTeX package then unset the 'euler' keyword
    argument.

    To use viewer auto-detection, lets say for 'png' output, issue

    >>> from sympy import symbols, preview, Symbol
    >>> x, y = symbols("x,y")

    >>> preview(x + y, output='png')

    This will choose 'pyglet' by default. To select a different one, do

    >>> preview(x + y, output='png', viewer='gimp')

    The 'png' format is considered special. For all other formats the rules
    are slightly different. As an example we will take 'dvi' output format. If
    you would run

    >>> preview(x + y, output='dvi')

    then 'view' will look for available 'dvi' viewers on your system
    (predefined in the function, so it will try evince, first, then kdvi and
    xdvi). If nothing is found, it will fall back to using a system file
    association (via ``open`` and ``xdg-open``). To always use your system file
    association without searching for the above readers, use

    >>> from sympy.printing.preview import system_default_viewer
    >>> preview(x + y, output='dvi', viewer=system_default_viewer)

    If this still does not find the viewer you want, it can be set explicitly.

    >>> preview(x + y, output='dvi', viewer='superior-dvi-viewer')

    This will skip auto-detection and will run user specified
    'superior-dvi-viewer'. If ``view`` fails to find it on your system it will
    gracefully raise an exception.

    You may also enter ``'file'`` for the viewer argument. Doing so will cause
    this function to return a file object in read-only mode, if ``filename``
    is unset. However, if it was set, then 'preview' writes the generated
    file to this filename instead.

    There is also support for writing to a ``io.BytesIO`` like object, which
    needs to be passed to the ``outputbuffer`` argument.

    >>> from io import BytesIO
    >>> obj = BytesIO()
    >>> preview(x + y, output='png', viewer='BytesIO',
    ...         outputbuffer=obj)

    The LaTeX preamble can be customized by setting the 'preamble' keyword
    argument. This can be used, e.g., to set a different font size, use a
    custom documentclass or import certain set of LaTeX packages.

    >>> preamble = "\\documentclass[10pt]{article}\n" \
    ...            "\\usepackage{amsmath,amsfonts}\\begin{document}"
    >>> preview(x + y, output='png', preamble=preamble)

    It is also possible to use the standard preamble and provide additional
    information to the preamble using the ``extra_preamble`` keyword argument.

    >>> from sympy import sin
    >>> extra_preamble = "\\renewcommand{\\sin}{\\cos}"
    >>> preview(sin(x), output='png', extra_preamble=extra_preamble)

    If the value of 'output' is different from 'dvi' then command line
    options can be set ('dvioptions' argument) for the execution of the
    'dvi'+output conversion tool. These options have to be in the form of a
    list of strings (see ``subprocess.Popen``).

    Additional keyword args will be passed to the :func:`~sympy.printing.latex.latex` call,
    e.g., the ``symbol_names`` flag.

    >>> phidd = Symbol('phidd')
    >>> preview(phidd, symbol_names={phidd: r'\ddot{\varphi}'})

    For post-processing the generated TeX File can be written to a file by
    passing the desired filename to the 'outputTexFile' keyword
    argument. To write the TeX code to a file named
    ``"sample.tex"`` and run the default png viewer to display the resulting
    bitmap, do

    >>> preview(x + y, outputTexFile="sample.tex")


    Npngr   )rx   okularkdvixdvi)rx   r   gsviewgv)rx   r   kpdfacroreadxpdfr   )dvipspdffilez-filename has to be specified if viewer="file"BytesIOzFoutputbuffer has to be a BytesIO compatible object if viewer="BytesIO"zUnrecognized viewer: %s)r`   ra   rc   rb   rd   zLatex code:
texput.texwzutf-8)encodingr
   zlatex program is not installed)r
   z-halt-on-errorz-interaction=nonstopmoder   )cwdstderrz7'latex' exited abnormally with the following output:
%sz	texput.%sr   dvips)dvipdfmxdvipdfmdvipdfrw   dvisvgm)r   r   r~   svgzInvalid output format: %sr	   zNone of %s are installedz, z%s is not installed)z-Ttightz-z9z--truecolorz
--no-fonts)rw   r   z-oz
texput.dvi)r   r   r   r   rw   r   ' z/' exited abnormally with the following output:
r   )r,   )r   r^   rR   getr$   whichr/   rT   callableOSErrorrv   r   r    TemporaryDirectoryr   r   writecopyfileRuntimeErrorr   r   r   outputKeyErrorlenextendmoveread)rp   r   viewerrc   ra   filenameoutputbufferr`   
dvioptionsoutputTexFilerb   rd   rq   r   
candidates	candidater"   
latex_mainworkdirfhesrccommandnamescmd_variantscmd_variantcmd_pathcmddefaultoptions
commandends                                r   r   r      s{   T ~&E/	# #F ~ :8K

 $3 	I<<	*D		 ~&NOO	9	 G H Hffll6&:/&899  F8',^*2F6DFJ 
-	*		$	$	& Z-'$w-sWE 	!HHZ 	! $OOD,7G||G$?@@		#	 V$U? i8 z!{	LQ+F3
  , 	P!<<4#*C		P |$q(&'ADIIlD['[\\&'<|A'NOO D(>N \2'- #|4!35l3 #|4J %

:&

>--k2>?JJz+.//'H VKKWc*H5y d7C($/ .2""2779-. .f4%62-'SMwv?kZ- Z-S  		T	! 	! " 	J 	"  Q !<v!EFDPQJ & /"XXc]AHH./ //. . & -"S!((,- --oZ- Z-s   L )O,L$A
O,L13O,
MO,,B-O,M6,AO,- N1)O,7N=	L! L!$L.	)O,1	M:MMO,M33O,6	N.?*N))N..O,1N:	6O,=	O)O$$O))O,,O5)r~   NTr_   NNNNNNN)r   os.pathr   r$   r    r(   r   r   r   rR   sympy.utilities.decoratorr   sympy.utilities.miscr   r
   __doctest_requires__r   r/   r^   rv   r   r_   r   r   <module>r      s    	   	CC 9 & $xj1 F327t '+R$GN +[EGBDHL>Bn-Gn-a  		s   
A A&%A&