a
    ø£Beñ  ã                   @   sì   d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	 ddl
Z
ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ G dd„ deƒZddd„Zddd„Zd dd„Zdd„ Zdd„ ZdZ d!dd„Z!dS )"zTools for handling LaTeX.é    )ÚBytesIOÚopenN)Úencodebytes)ÚPath)Úfind_cmdÚFindCmdError©Ú
get_config)ÚSingletonConfigurable)ÚListÚBoolÚUnicode)Úcast_unicodec                   @   sr   e Zd ZdZdd„ Zeeƒ ddgdd�jdd	�Ze	dd
d�jdd	�Z
eg d¢dd�jdd	�Zedd�jdd	�ZdS )Ú	LaTeXToolz3An object to store configuration of the LaTeX tool.c                 C   s   t ƒ S )Nr   )Úself© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/lib/latextools.pyÚ_config_default   s    zLaTeXTool._config_defaultÚ
matplotlibÚdvipnga  Preferred backend to draw LaTeX math equations. Backends in the list are checked one by one and the first usable one is used.  Note that `matplotlib` backend is usable only for inline style equations.  To draw  display style equations, `dvipng` backend must be specified. )ÚhelpT)ÚconfigzmUse breqn.sty to automatically break long equations. This configuration takes effect only for dvipng backend.)ZamsmathZamsthmZamssymbZbmziA list of packages to use for dvipng backend. 'breqn' will be automatically appended when use_breqn=True.zKAdditional preamble to use when generating LaTeX source for dvipng backend.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚtagÚbackendsr   Ú	use_breqnÚpackagesÚpreambler   r   r   r   r      s2   
þôþüþüÿýr   FÚBlackç      ð?c           
   
   C   s  t | ƒ} t ¡ j}|du r"|d }||vr.dS |dkr<t}n¦|dkrÔt}| d¡rât|ƒdkrÄz.d d 	d	d
„ t
 |dd… d¡D ƒ¡¡}W qÒ tyÀ } ztd |¡ƒ|‚W Y d}~qÒd}~0 0 qâtd |¡ƒ‚ntd |¡ƒ‚|| |||ƒ}	|�r|	�rt|	ƒ}	|	S )al  Render a LaTeX string to PNG.

    Parameters
    ----------
    s : str
        The raw string containing valid inline LaTeX.
    encode : bool, optional
        Should the PNG data base64 encoded to make it JSON'able.
    backend : {matplotlib, dvipng}
        Backend for producing PNG data.
    wrap : bool
        If true, Automatically wrap `s` as a LaTeX equation.
    color : string
        Foreground color name among dvipsnames, e.g. 'Maroon' or on hex RGB
        format, e.g. '#AA20FA'.
    scale : float
        Scale factor for the resulting PNG.
    None is returned when the backend cannot be used.

    Nr   r   r   ú#é   zRGB {}ú c                 S   s   g | ]}t t|d ƒƒ‘qS )é   )ÚstrÚint)Ú.0Úxr   r   r   Ú
<listcomp>a   ó    z latex_to_png.<locals>.<listcomp>é   é   zInvalid color specification {}.zNo such backend {0})r   r   Úinstancer   Úlatex_to_png_mplÚlatex_to_png_dvipngÚ
startswithÚlenÚformatÚjoinÚtextwrapÚwrapÚ
ValueErrorr   )
ÚsÚencodeÚbackendr7   ÚcolorÚscaleZallowed_backendsÚfÚeZbin_datar   r   r   Úlatex_to_png=   s0    

ÿ(r@   c              
   C   s  z0ddl m}m}m} ddlm} ddlm} W n tyD   Y d S 0 |  	dd¡} |r`d 
| ¡} z”|jdd	�}	d
| }
tƒ }| d¡}|j| d|	d�\}}}}}|j|d |d fd�}|jd|| | |	|d� | |¡ |j||
ddd� | ¡ W S  tt|f�y   Y d S 0 d S )Nr   )ÚfigureÚfont_managerÚmathtext)Úbackend_agg)ÚParseFatalExceptionz$$ú$z${0}$é   )Úsizeéx   ÚpathéH   )ÚdpiÚprop)Zfigsize)Zfontpropertiesr<   ZpngT)rL   r4   Útransparent)r   rA   rB   rC   Zmatplotlib.backendsrD   Ú	pyparsingrE   ÚImportErrorÚreplacer4   ZFontPropertiesr   ZMathTextParserÚparseZFigureÚtextZFigureCanvasAggZsavefigÚgetvaluer8   ÚRuntimeError)r9   r7   r<   r=   rA   rB   rC   rD   rE   rM   rL   ÚbufferÚparserÚwidthÚheightÚdepthÚ_Zfigr   r   r   r0   o   s,    



r0   c                 C   s¨  zt dƒ t dƒ W n ty(   Y d S 0 d }tjdkrPt ¡ }| jtjO  _�zD�ztt	 
¡ ƒ}d}d}d}| |¡jddd	�� }	|	 t| |ƒ¡ W d   ƒ n1 sª0    Y  tjdd
dd|g|tjtj|d� td| ƒ}
tjddddt|
ƒddddd||d|g|tjtj|d� | |¡ d¡�&}	|	 ¡ W  d   ƒ W W t |¡ S 1 �sX0    Y  W n$ tj�yˆ   Y W t |¡ d S 0 W t |¡ nt |¡ 0 d S )NÚlatexr   Úntztmp.texztmp.dviztmp.pngÚwÚutf8)Úencodingz-halt-on-errorz-interactionZ	batchmode)ÚcwdÚstdoutÚstderrÚstartupinfoé–   z-TZtightz-Dz-zÚ9z-bgZTransparentz-oz-fgÚrb)r   r   ÚosÚnameÚ
subprocessZSTARTUPINFOZdwFlagsZSTARTF_USESHOWWINDOWr   ÚtempfileÚmkdtempÚjoinpathr   Ú
writelinesÚ	genelatexÚ
check_callÚDEVNULLÚroundr'   ÚreadÚshutilÚrmtreeÚCalledProcessError)r9   r7   r<   r=   rd   ZworkdirZtmpfileZdvifileÚoutfiler>   Ú
resolutionr   r   r   r1   �   sl    
.ûòì
ü
þr1   c                 C   sV   z>t dƒ tjd| gtjtjd�}| ¡ \}}| ¡  dd¡W S  tyP   Y n0 dS )z5Invoke kpsewhich command with an argument `filename`.Ú	kpsewhich)rb   rc   r_   rQ   N)r   rj   ÚPopenÚPIPEÚcommunicateÚstripÚdecoder   )ÚfilenameÚprocrb   rc   r   r   r   ry   Ë   s    þry   c                 c   sž   t  ¡ }|o|jotdƒ}dV  |j}|r4|dg }|D ]}d |¡V  q8dV  |jr^|jV  dV  |r|dV  | V  dV  n|rŽd	 | ¡V  n| V  d
V  dS )z+Generate LaTeX document for dvipng backend.z	breqn.styz\documentclass{article}Úbreqnz\usepackage{{{0}}}z\pagestyle{empty}z\begin{document}z\begin{dmath*}z\end{dmath*}z$${0}$$z\end{document}N)r   r/   r   ry   r   r4   r    )Úbodyr7   Últr�   r   Úpackr   r   r   ro   Ø   s(    
ro   z-<img src="data:image/png;base64,%s" alt=%s />Úimagec                 C   s&   t | dd� d¡}|r"t||f S dS )zÚRender LaTeX to HTML with embedded PNG data using data URIs.

    Parameters
    ----------
    s : str
        The raw string containing valid inline LateX.
    alt : str
        The alt text to use for the HTML.
    T)r:   ÚasciiN)r@   r~   Ú_data_uri_template_png)r9   ZaltZbase64_datar   r   r   Úlatex_to_htmló   s    
rˆ   )FNFr!   r"   )r!   r"   )r!   r"   )r…   )"r   Úior   r   rh   rk   rt   rj   Úbase64r   r6   Úpathlibr   ZIPython.utils.processr   r   Ztraitlets.configr	   Ztraitlets.config.configurabler
   Z	traitletsr   r   r   ZIPython.utils.py3compatr   r   r@   r0   r1   ry   ro   r‡   rˆ   r   r   r   r   Ú<module>   s.   %  ÿ
2

>