o
    h                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	Z	ddl
mZmZ ddlZddlmZmZ ddlmZ G d	d
 d
ejejZdd Zi g fddZeede_G dd deZdddZdd Zdd Zdd ZdS )a  
A role and directive to display mathtext in Sphinx
==================================================

The ``mathmpl`` Sphinx extension creates a mathtext image in Matplotlib and
shows it in html output. Thus, it is a true and faithful representation of what
you will see if you pass a given LaTeX string to Matplotlib (see
:ref:`mathtext`).

.. warning::
    In most cases, you will likely want to use one of `Sphinx's builtin Math
    extensions
    <https://www.sphinx-doc.org/en/master/usage/extensions/math.html>`__
    instead of this one. The builtin Sphinx math directive uses MathJax to
    render mathematical expressions, and addresses accessibility concerns that
    ``mathmpl`` doesn't address.

Mathtext may be included in two ways:

1. Inline, using the role::

     This text uses inline math: :mathmpl:`\alpha > \beta`.

   which produces:

     This text uses inline math: :mathmpl:`\alpha > \beta`.

2. Standalone, using the directive::

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

   which produces:

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

Options
-------

The ``mathmpl`` role and directive both support the following options:

fontset : str, default: 'cm'
    The font set to use when displaying math. See :rc:`mathtext.fontset`.

fontsize : float
    The font size, in points. Defaults to the value from the extension
    configuration option defined below.

Configuration options
---------------------

The mathtext extension has the following configuration options:

mathmpl_fontsize : float, default: 10.0
    Default font size, in points.

mathmpl_srcset : list of str, default: []
    Additional image sizes to generate when embedding in HTML, to support
    `responsive resolution images
    <https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images>`__.
    The list should contain additional x-descriptors (``'1.5x'``, ``'2x'``,
    etc.) to generate (1x is the default and always included.)

    N)Path)nodes)	Directive
directives)ConfigErrorExtensionError)_apimathtext)validate_float_or_Nonec                   @   s   e Zd ZdS )
latex_mathN)__name__
__module____qualname__ r   r   p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/sphinxext/mathmpl.pyr   W   s    r   c                 C   s   t | tjjS N)r   choicer	   MathTextParser_font_type_mapping)argr   r   r   fontset_choice[   s   r   c           
      C   sZ   | d}||d d }t|}	||	d< |dd|	d< |dtjjj|	d< |	gg fS )N`   latexfontsetcmfontsize)findr   getsetupappconfigmathmpl_fontsize)
rolerawtexttextlinenoinlineroptionscontentir   noder   r   r   	math_role_   s   

r-   r   r   c                   @   s2   e Zd ZdZdZdZdZdZee	dZ
dd ZdS )	MathDirectivezR
    The ``.. mathmpl::`` directive, as documented in the module's docstring.
    Tr   Fr.   c                 C   sN   d | j}t| j}||d< | jdd|d< | jdtjjj	|d< |gS )N r   r   r   r   )
joinr*   r   
block_textr)   r   r    r!   r"   r#   )selfr   r,   r   r   r   runx   s   
zMathDirective.runN)r   r   r   __doc__has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   r
   option_specr4   r   r   r   r   r/   m   s    r/   r   
   d   c              	   C   s   t ||d4 ztjd|  d||dd}W n ty+   td|   d}Y n	w W d    |S W d    |S 1 s?w   Y  |S )N)zmathtext.fontsetz	font.size$png)dpiformatz!Could not render math expression r   )mpl
rc_contextr	   math_to_image	Exceptionr   warn_external)r   filenamer   r   r?   depthr   r   r   	latex2png   s"   


rH   c                 C   s  t | jtj}| d }| d }| d }dtj| | |  dd dd  }t	t
jjjdd	}|jd
d
d || d }t||||d}	g }
t
jjjD ]1}| d|dd d}t||| ||dt|d d  d |
t
jjj d| d|  qQ|
rdt
jjj d| dd|
 d }
|rd}nd}|r|	dkrd|	d  }nd}dt
jjj d| d |
 | | d!	S )"Nr   r   r   zmath-{}F)usedforsecurityi_imagesmathmplT)parentsexist_okz.png)r   -._r<   r   )r   r?   z	/mathmpl/ zsrcset="z.png, z, z" r0   zclass="center" r   z)style="position: relative; bottom: -%dpx"r   z
<img src="z.png" z/>)
isinstanceparentr   TextElementr@   hashlibsha256encode	hexdigestr   r    r!   builderoutdirmkdirrH   r"   mathmpl_srcsetreplacefloatappendimgpathr1   )r,   sourceinliner   r   r   namedestdirdestrG   srcsetsizerF   clsstyler   r   r   
latex2html   sX   
rj   c              	   C   sh   t | jjD ]+\}}|d dkr*z
t|d d  W q ty)   td|dw td|dd S )Nr   xz,Invalid value for mathmpl_srcset parameter: ze. Must be a list of strings with the multiplicative factor followed by an "x".  e.g. ["2.0x", "1.5x"])	enumerater"   r\   r^   
ValueErrorr   )r!   r"   r+   rg   r   r   r   _config_inited   s   

rn   c                 C   s   | t _| ddd | dg d z| dt W n ty*   | ddd  Y nw d	d
 }dd }dd }dd }| jt||f||fd | dt	 | 
dt tjdk rd| dt	 | 
dt ddd}|S )Nr#   g      $@Tr\   zconfig-initedzenv-updatedc                 S   s
   t | d S r   )rn   )r!   envr   r   r   <lambda>   s   
 zsetup.<locals>.<lambda>c                 S   s"   | j jd }| jt|| d S )Nra   )document
attributesbodyr_   rj   )r3   r,   ra   r   r   r   visit_latex_math_html   s   z$setup.<locals>.visit_latex_math_htmlc                 S      d S r   r   r3   r,   r   r   r   depart_latex_math_html      z%setup.<locals>.depart_latex_math_htmlc                 S   sD   t |jtj}|r| jd|d   d S | jd|d dg d S )Nz$%s$r   z\begin{equation}z\end{equation})rR   rS   r   rT   rs   r_   extend)r3   r,   rb   r   r   r   visit_latex_math_latex   s   
z%setup.<locals>.visit_latex_math_latexc                 S   ru   r   r   rv   r   r   r   depart_latex_math_latex   rx   z&setup.<locals>.depart_latex_math_latex)htmlr   rK   )r      math)parallel_read_safeparallel_write_safe)r    r!   add_config_valueconnectrn   r   add_noder   add_roler-   add_directiver/   sphinxversion_info)r!   rt   rw   rz   r{   metadatar   r   r   r       s.   	

r    )r   r;   r<   )r5   rU   pathlibr   docutilsr   docutils.parsers.rstr   r   r   sphinx.errorsr   r   
matplotlibrA   r   r	   matplotlib.rcsetupr
   GeneralElementr   r   r-   r)   r/   rH   rj   rn   r    r   r   r   r   <module>   s,    H


)