a
    Sic#                     @   s  d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZmZ ddlmZ ddl
mZmZmZ eeZee_e	d	G d
d dZe	d	G dd deZe	d	G dd deZe	d	G dd deZG dd dZdddddZdS )a0  
A module for parsing a subset of the TeX math syntax and rendering it to a
Matplotlib backend.

For a tutorial of its usage, see :doc:`/tutorials/text/mathtext`.  This
document is primarily concerned with implementation details.

The module uses pyparsing_ to parse the TeX expression.

.. _pyparsing: https://pypi.org/project/pyparsing/

The Bakoma distribution of the TeX Computer Modern fonts, and STIX
fonts are supported.  There is experimental support for using
arbitrary fonts, but results may vary without proper tweaking and
metrics for those fonts.
    )
namedtupleN)_api	_mathtext)FT2ImageLOAD_NO_HINTING)FontProperties   )RasterParseVectorParseget_unicode_indexz3.6c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )MathtextBackenda  
    The base class for the mathtext backend-specific code.  `MathtextBackend`
    subclasses interface between mathtext and specific Matplotlib graphics
    backends.

    Subclasses need to override the following:

    - :meth:`render_glyph`
    - :meth:`render_rect_filled`
    - :meth:`get_results`

    And optionally, if you need to use a FreeType hinting style:

    - :meth:`get_hinting_type`
    c                 C   s   d| _ d| _d| _d S )Nr   widthheightdepthself r   O/var/www/html/django/DPS/env/lib/python3.9/site-packages/matplotlib/mathtext.py__init__6   s    zMathtextBackend.__init__c                 C   s   || _ || _|| _dS )z(Set the dimension of the drawing canvas.Nr   r   whdr   r   r   set_canvas_size;   s    zMathtextBackend.set_canvas_sizec                 C   s
   t  dS )z_
        Draw a glyph described by *info* to the reference point (*ox*,
        *oy*).
        NNotImplementedErrorr   oxoyinfor   r   r   render_glyphA   s    zMathtextBackend.render_glyphc                 C   s
   t  dS )zR
        Draw a filled black rectangle from (*x1*, *y1*) to (*x2*, *y2*).
        Nr   r   x1y1x2y2r   r   r   render_rect_filledH   s    z"MathtextBackend.render_rect_filledc                 C   s
   t  dS )zp
        Return a backend-specific tuple to return to the backend after
        all processing is done.
        Nr   r   boxr   r   r   get_resultsN   s    zMathtextBackend.get_resultsc                 C   s   t S )z\
        Get the FreeType hinting type to use with this particular
        backend.
        )r   r   r   r   r   get_hinting_typeU   s    z MathtextBackend.get_hinting_typeN)
__name__
__module____qualname____doc__r   r   r!   r'   r*   r+   r   r   r   r   r   %   s   r   c                       sT   e Zd ZdZ fddZdd Z fddZdd	 Zd
d Zdd Z	dd Z
  ZS )MathtextBackendAggz
    Render glyphs and rectangles to an FTImage buffer, which is later
    transferred to the Agg image by the Agg backend.
    c                    s0   d| _ d| _d | _d| _g d| _t   d S )Nr   bbox)r   r   r   r   )r   r   imagemoder1   superr   r   	__class__r   r   r   c   s    
zMathtextBackendAgg.__init__c                 C   sB   t | jd |t | jd |t| jd |t| jd |g| _d S )Nr   r         )minr1   maxr"   r   r   r   _update_bboxk   s
    zMathtextBackendAgg._update_bboxc                    s@   t  ||| | jdkr<tt|t|t|d | _d S )Nr1   r   )r4   r   r3   r   npceilr:   r2   r   r5   r   r   r   q   s    
z"MathtextBackendAgg.set_canvas_sizec                 C   sh   | j dkr<| ||jj ||jj ||jj ||jj  n(|jj| j	|||jj
 |jtjd d d S )Nr1   ztext.antialiased)antialiased)r3   r;   metricsxminymaxxmaxyminfontdraw_glyph_to_bitmapr2   ZicebergglyphmplrcParamsr   r   r   r   r!   v   s    



zMathtextBackendAgg.render_glyphc                 C   s   | j dkr| |||| nhtt|| d d}|dkr\|| d }t||d d  }nt|}| jt||t|||  d S )Nr1   r   r   g       @)r3   r;   r:   intr2   draw_rect_filledr<   r=   )r   r#   r$   r%   r&   r   centeryr   r   r   r'      s    
z%MathtextBackendAgg.render_rect_filledc                 C   s   d | _ d| _t| S )Nrender)r2   r3   r   ship	to_rasterr(   r   r   r   r*      s    zMathtextBackendAgg.get_resultsc                 C   s   ddl m} | S )Nr   backend_agg)matplotlib.backendsrQ   get_hinting_flag)r   rQ   r   r   r   r+      s    z#MathtextBackendAgg.get_hinting_type)r,   r-   r.   r/   r   r;   r   r!   r'   r*   r+   __classcell__r   r   r5   r   r0   ]   s   r0   c                       sB   e Zd ZdZeddZ fddZdd Zdd	 Zd
d Z	  Z
S )MathtextBackendPathzY
    Store information to write a mathtext rendering to the text path
    machinery.
    _Resultzwidth height depth glyphs rectsc                    s   t    g | _g | _d S N)r4   r   glyphsrectsr   r5   r   r   r      s    
zMathtextBackendPath.__init__c                 C   s0   | j | |j }| j|j|j|j||f d S rW   )r   offsetrX   appendrD   fontsizenumr   r   r   r   r!      s    z MathtextBackendPath.render_glyphc                 C   s&   | j || j| || || f d S rW   )rY   r[   r   r"   r   r   r   r'      s    z&MathtextBackendPath.render_rect_filledc                 C   s   t | S rW   )r   rN   	to_vectorr(   r   r   r   r*      s    zMathtextBackendPath.get_results)r,   r-   r.   r/   r   rV   r   r!   r'   r*   rT   r   r   r5   r   rU      s   
rU   c                   @   s   e Zd ZdS )MathTextWarningN)r,   r-   r.   r   r   r   r   r_      s   r_   c                   @   sR   e Zd ZdZejejejejej	ej
dZdd ZdddZedd	d
 ZdS )MathTextParserN)cmdejavuserif
dejavusansstixstixsanscustomc                 C   s    t jdddd| d| _dS )a	  
        Create a MathTextParser for the given backend *output*.

        Parameters
        ----------
        output : {"path", "agg"}
            Whether to return a `VectorParse` ("path") or a
            `RasterParse` ("agg", or its synonym "macosx").
        vectorraster)pathaggmacosx)outputN)r   check_getitemlower_output_type)r   rl   r   r   r   r      s    

zMathTextParser.__init__H   c                 C   s"   |dur|  nd}| |||S )a  
        Parse the given math expression *s* at the given *dpi*.  If *prop* is
        provided, it is a `.FontProperties` object specifying the "default"
        font to use in the math expression, used for all non-math text.

        The results are cached, so multiple calls to `parse`
        with the same expression should be fast.

        Depending on the *output* type, this returns either a `VectorParse` or
        a `RasterParse`.
        N)copy_parse_cached)r   sdpipropr   r   r   parse   s    zMathTextParser.parse2   c                 C   s   ddl m} |d u rt }tj| j| d}t| d| j	 }|||}|
 }| jd u rjt | j_| j||||}	t|	}
| j	dkr|
 S | j	dkr|
 S d S )Nr   rP   )fontset)rg   rh   rg   rh   )rR   rQ   r   r   rm   _font_type_mappingget_math_fontfamilyr   rS   ro   get_size_in_points_parserr   Parserr6   rv   rN   r^   rO   )r   rs   rt   ru   rQ   Zfontset_classZload_glyph_flagsrx   r\   r)   rl   r   r   r   rr      s*    





zMathTextParser._parse_cached)rp   N)r,   r-   r.   r|   r   ZBakomaFontsZDejaVuSerifFontsZDejaVuSansFontsZ	StixFontsZStixSansFontsZUnicodeFontsry   r   rv   	functools	lru_cacherr   r   r   r   r   r`      s   	
r`   )colorc                C   sr   ddl m} td}|j| d|d\}}	}
}}|j|d |	d fd}|jd|
|	 | ||d |j|||d	 |
S )
a  
    Given a math expression, renders it in a closely-clipped bounding
    box to an image file.

    Parameters
    ----------
    s : str
        A math expression.  The math portion must be enclosed in dollar signs.
    filename_or_obj : str or path-like or file-like
        Where to write the image data.
    prop : `.FontProperties`, optional
        The size and style of the text.
    dpi : float, optional
        The output dpi.  If not set, the dpi is determined as for
        `.Figure.savefig`.
    format : str, optional
        The output format, e.g., 'svg', 'pdf', 'ps' or 'png'.  If not set, the
        format is determined as for `.Figure.savefig`.
    color : str, optional
        Foreground color, defaults to :rc:`text.color`.
    r   )figureri   rp   )rt   ru   g      R@)figsize)fontpropertiesr   )rt   format)
matplotlibr   r`   rv   Figuretextsavefig)rs   Zfilename_or_objru   rt   r   r   r   parserr   r   r   _figr   r   r   math_to_image   s    r   )NNN)r/   collectionsr   r~   loggingnumpyr<   r   rG   r   r   matplotlib.ft2fontr   r   matplotlib.font_managerr   r	   r
   r   	getLoggerr,   _logr-   
deprecatedr   r0   rU   Warningr_   r`   r   r   r   r   r   <module>   s.   
79F