o
    8Va.                     @   s   d dl Z d dlmZ d dlZd dlZzd dlmZmZmZ W n	 e	y'   Y nw d dl
mZ ddlmZ ddgiZd	d
 Zdd Zedddd			dddZdS )    N)join)STDOUTCalledProcessErrorcheck_output)doctest_depends_on   )latex)previewpygletc                  O   s&   t jdkrd}nd}t| d|i|S )Nnti   r   Zcreationflags)osnamer   )argskwargsZcreation_flag r   8/usr/lib/python3/dist-packages/sympy/printing/preview.py_check_output_no_window   s   
r   c           	         s  ddl m}m}m  ddlm |dkr#ddlm} |j| | dnt	dd} j
d	d
}|jjd|  jd|  dd	|dd	 z.fdd_fdd}|_ fdd}|_jsu    jrjW n	 ty   Y nw   d S )Nr   )windowimagegl)keypng)PNGImageDecoder)decoderz*pyglet preview works only for 'png' files.   F)Zdouble_buffer   Zsympy)widthheightZcaptionZ	resizableconfigc                      s
   d _ d S )NT)has_exitr   )winr   r   on_close3   s   
z_run_pyglet.<locals>.on_closec                    s   |  j  jfv r  d S d S )N)QZESCAPE)symbol	modifiers)r   r!   r   r   on_key_press8   s   
z!_run_pyglet.<locals>.on_key_pressc                      sD     dddd   j jj d jj d  d S )Ng      ?r   )ZglClearColorZglClearZGL_COLOR_BUFFER_BITZblitr   r   r   )r   imgr    r   r   	on_expose>   s   z_run_pyglet.<locals>.on_expose)r
   r   r   r   Zpyglet.windowr   Zpyglet.image.codecs.pngr   load
ValueErrorZConfigZWindowr   r   Z	set_vsyncr!   r%   r'   r   Zdispatch_eventsZflipKeyboardInterruptclose)	fnamefmtr   r   r   offsetr   r%   r'   r   )r   r&   r   r!   r    r   _run_pyglet   s@   
	r/   )r   dvipng)r
   )evinceZgimpzsuperior-dvi-viewer)exemodulesZdisable_viewersr   Tr   c
                  K   s  dg}|du rH|dkrd}ncg dg dg dd}z|| }W n t y.   td| dw |D ]}t|}|dur@|} nq1td	| n)|d
krU|du rTtdn|dkrb|du ratdn||vrqt|sqtd| |du r|d }|r|d7 }dddd |D  }d| }n|rtdt| tr| }ndt| fddi|
 d }|d | d d }t	
 }tt|dddd}|| W d   n1 sw   Y  |	durtt|d|	 td std!ztg d"|td# W n ty } ztd$|j d}~ww d%| }|d&krd'gg d(d)gd*gd+}z|| }W n t y;   td| dw |D ]}t|}|rM|g} nq>t|d,kr_td-d.| td/|d0  g d1d2gd3}d4|d5gd5|gd4|d5gd4|d5gd4|d5gd4|d5gd6}|dur|| n	|||g  |||  z	t||td# W n ty } ztd7d8||jf d}~ww |d
krtt||| nq|dkrtt||d9}||  W d   n	1 sw   Y  nS|dkr$zd0dl}W n ty   td:w tt|||d;W  d   S zt||g|td# W n' tyF } z
td<|||jf d}~ww W d   dS W d   dS W d   dS 1 scw   Y  dS )=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 you will need to set the viewer 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 genereted
    file to this filename instead.

    There is also support for writing to a 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)

    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 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")


    r
   Nr   )r1   okularZkdviZxdvi)r1   r4   Zgsviewgv)r1   r4   ZkpdfZacroreadZxpdfr5   )dvipspdfzInvalid output format: %sz(No viewers found for '%s' output format.filez-filename has to be specified if viewer="file"BytesIOzFoutputbuffer has to be a BytesIO compatible object if viewer="BytesIO"zUnrecognized viewer: %s)ZamsmathZamsfonts)euler
c                 S   s   g | ]}d | qS )z\usepackage{%s}r   ).0pr   r   r   
<listcomp>   s    zpreview.<locals>.<listcomp>z?\documentclass[varwidth,12pt]{standalone}
%s

\begin{document}
zOThe "packages" keyword must not be set if a custom LaTeX preamble was specifiedz$\displaystyle modeZplain$z

z\end{document}
texput.texwzutf-8)encodingr   zlatex program is not installed)r   z-halt-on-errorz-interaction=nonstopmoderB   )cwdstderrz7'latex' exited abnormally with the following output:
%sz	texput.%sr6   dvips)dvipdfmxdvipdfmdvipdfr0   dvisvgm)r7   r8   r   Zsvgr   zNone of %s are installedz, z%s is not installedr   )z-TZtightz-z9z--truecolorz
--no-fonts)r0   rK   z-oz
texput.dvi)rG   rJ   rI   rH   r0   rK   z4'%s' exited abnormally with the following output:
%s rbz=pyglet is required for preview.
 visit http://www.pyglet.org/)r-   z7'%s %s' exited abnormally with the following output:
%s)KeyErrorr)   shutilwhichOSErrorr   
isinstancestrr   tempfileZTemporaryDirectoryopenwritecopyfileRuntimeErrorr   r   r   outputlenextendgetmovereadr
   ImportErrorr/   ) exprrZ   Zviewerr;   ZpackagesfilenameZoutputbufferZpreambleZ
dvioptionsZoutputTexFileZlatex_settingsZspecialZ
candidatesZcandidate_viewers	candidatepathZactual_packagesZpackage_includesZlatex_stringZ
latex_mainZworkdirZfhesrcZcommandnamesZcmd_variantsZcmd_variantZcmd_pathcmdZdefaultoptionsZ
commandendr
   r   r   r   r	   R   s2  Z







	


Y


 Z$r	   )	r   NTr   NNNNN)r   os.pathr   rP   rU   
subprocessr   r   r   r`   Zsympy.utilities.decoratorr   r   Z__doctest_requires__r   r/   r	   r   r   r   r   <module>   s*    

7