o
    hC                    @  sd  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ZddlZddl	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mZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&Z'ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. dd	l/m0Z0m1Z1m2Z2 dd
l3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z: ddlm;Z< e:e<j=dk rddlm>Z? nddlm?Z? ej@rddlAmBZB ddl3mCZC eD  eEdZFdddZGG dd deZHdeH_IG dd deZJdeJ_IG dd dZKG dd  d eZLG d!d" d"eZMG d#d$ d$ejNZOG d%d& d&eOejPd'ZQG d(d) d)eQZRG d*d+ d+eQZSG d,d- d-eSejPd'ZTG d.d/ d/eTZUG d0d1 d1eTZVG d2d3 d3eSZWG d4d5 d5eWZXd6ZYd7ZZG d8d9 d9Z[G d:d; d;e[Z\G d<d= d=e[Z]G d>d? d?e[Z^G d@dA dAe[Z_G dBdC dCe[Z`i dDe`dEe`dFe_dGe\dHe\dIe\dJe\dKe\dLe\dMe\dNe]dOe]dPe]dQe]dRe]dSe]dTe]e`e`dUZaddYdZZbG d[d\ d\ZcG d]d^ d^ecZdG d_d` d`edZeG dadb dbedZfG dcdd ddecZgG dedf dfegZhG dgdh dhedZiG didj djeiZjG dkdl dleiZkG dmdn dnedZlG dodp dpelZmG dqdr drelZnG dsdt dteZoeodudvddudeodudvdwdudeodudvddudeodududdvdeodududdvdweodududdvdeodududdudeodudvddxddyeo_pG dzd{ d{ecZqG d|d} d}ejZrG d~d dekZsG dd decZtG dd dejZuG dd dejZvddddZwdddZxG ddW dWZydddZzG dd dZ{dS )z.
Implementation details for :mod:`.mathtext`.
    )annotationsN)
NamedTuple)EmptyForwardLiteralNotAnyoneOf	OneOrMoreOptionalParseBaseExceptionParseExceptionParseExpressionParseFatalExceptionParserElementParseResultsQuotedStringRegex	StringEnd
ZeroOrMorepyparsing_commonGroup   )cbook)latex_to_bakomastix_glyph_fixesstix_virtual_fontstex2uni)FontPropertiesfindfontget_font)FT2FontFT2ImageKerning	LoadFlags)parse)__version__   )
nestedExpr)nested_expr)Iterable)Glyphzmatplotlib.mathtextsymbolstrreturnintc              
   C  sX   zt | W S  ty   Y nw zt| d W S  ty+ } zt| d|d}~ww )z
    Return the integer index (from the Unicode table) of *symbol*.

    Parameters
    ----------
    symbol : str
        A single (Unicode) character, a TeX command (e.g. r'\pi') or a Type1
        symbol name (e.g. 'phi').
    \z5 is not a valid Unicode character or TeX/Type1 symbolN)ord	TypeErrorr   stripKeyError
ValueError)r+   err r6   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/_mathtext.pyget_unicode_index5   s   

r8   c                   @  s:   e Zd ZU dZded< ded< ded< ded< ded	< d
S )VectorParsea  
    The namedtuple type returned by ``MathTextParser("path").parse(...)``.

    Attributes
    ----------
    width, height, depth : float
        The global metrics.
    glyphs : list
        The glyphs including their positions.
    rect : list
        The list of rectangles.
    floatwidthheightdepthz.list[tuple[FT2Font, float, int, float, float]]glyphsz'list[tuple[float, float, float, float]]rectsN__name__
__module____qualname____doc____annotations__r6   r6   r6   r7   r9   K   s   
 r9   c                   @  sB   e Zd ZU dZded< ded< ded< ded< ded< ded	< d
S )RasterParsea  
    The namedtuple type returned by ``MathTextParser("agg").parse(...)``.

    Attributes
    ----------
    ox, oy : float
        The offsets are always zero.
    width, height, depth : float
        The global metrics.
    image : FT2Image
        A raster image.
    r:   oxoyr;   r<   r=   r!   imageNr@   r6   r6   r6   r7   rF   a   s   
 rF   c                   @  s.   e Zd ZdZdddZddd	ZdddZdS )Outputz
    Result of `ship`\ping a box: lists of positioned glyphs and rectangles.

    This class is not exposed to end users, but converted to a `VectorParse` or
    a `RasterParse` by `.MathTextParser.parse`.
    boxBoxc                 C  s   || _ g | _g | _d S N)rK   r>   r?   )selfrK   r6   r6   r7   __init__   s   
zOutput.__init__r-   r9   c                   s`   t tj| jj| jj| jjg\} } fdd| jD } fdd| jD }t	| | |||S )Nc                   s0   g | ]\}}}|j |j|j| | |j fqS r6   )fontfontsizenumoffset.0rG   rH   infohr6   r7   
<listcomp>   s    "z$Output.to_vector.<locals>.<listcomp>c                   s,   g | ]\}}}}| | || || fqS r6   r6   rU   x1y1x2y2rW   r6   r7   rY      s    
)
mapnpceilrK   r;   r<   r=   r>   r?   r9   )rN   wdgsrsr6   rW   r7   	to_vector   s   


zOutput.to_vectorantialiasedboolrF   c             	   C  s  t g dd | jD dd | jD dd }t g dd | jD dd | jD dd }tg dd | jD d	d | jD dd }tg d
d | jD dd | jD dd }|| }|| | jj }|| | jj }ttt	
|tt	
|t|d }	t| j| | f}
|
jD ]\}}}|jj|	t|t||jj |j|d q|
jD ];\}}}}tt|| d d}|dkr|| d }t||d d  }nt|}|	t||tt	
|||  qtdd||| ||	S )Nc                 S     g | ]\}}}||j j qS r6   )metricsxminrT   r6   r6   r7   rY          z$Output.to_raster.<locals>.<listcomp>c                 S  s   g | ]\}}}}|qS r6   r6   rZ   r6   r6   r7   rY          r   r   c                 S     g | ]\}}}||j j qS r6   )rj   ymaxrT   r6   r6   r7   rY      rl   c                 S  s   g | ]\}}}}|qS r6   r6   rZ   r6   r6   r7   rY      rm   c                 S  ri   r6   )rj   xmaxrT   r6   r6   r7   rY      rl   c                 S  s   g | ]\}}}}|qS r6   r6   rZ   r6   r6   r7   rY      rm   c                 S  rn   r6   )rj   yminrT   r6   r6   r7   rY      rl   c                 S  s   g | ]\}}}}|qS r6   r6   rZ   r6   r6   r7   rY      rm   )rg      )minr>   r?   maxrK   r=   r<   r!   r.   r`   ra   shiprP   draw_glyph_to_bitmaprj   icebergglyphdraw_rect_filledrF   )rN   rg   rk   rq   rp   ro   rb   rX   rc   rI   shiftedrG   rH   rV   r[   r\   r]   r^   r<   centeryr6   r6   r7   	to_raster   s\   ($zOutput.to_rasterN)rK   rL   )r-   r9   )rg   rh   r-   rF   )rA   rB   rC   rD   rO   rf   r}   r6   r6   r6   r7   rJ   x   s
    

	rJ   c                   @  sZ   e Zd ZU dZded< ded< ded< ded< ded< ded< ded	< ded
< ded< dS )FontMetricsaa  
    Metrics of a font.

    Attributes
    ----------
    advance : float
        The advance distance (in points) of the glyph.
    height : float
        The height of the glyph in points.
    width : float
        The width of the glyph in points.
    xmin, xmax, ymin, ymax : float
        The ink rectangle of the glyph.
    iceberg : float
        The distance from the baseline to the top of the glyph. (This corresponds to
        TeX's definition of "height".)
    slanted : bool
        Whether the glyph should be considered as "slanted" (currently used for kerning
        sub/superscripts).
    r:   advancer<   r;   rk   rp   rq   ro   rw   rh   slantedNr@   r6   r6   r6   r7   r~      s   
 r~   c                   @  sF   e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< dS )FontInfor    rP   r:   rQ   r,   postscript_namer~   rj   r.   rR   r*   rx   rS   NrA   rB   rC   rE   r6   r6   r6   r7   r      s   
 r   c                   @  st   e Zd ZdZd9ddZd:ddZd;ddZd<ddZd=d!d"Zd>d(d)Z	d?d.d/Z
d@d0d1Zd@d2d3ZdAd6d7Zd8S )BFontsz
    An abstract base class for a system of fonts to use for mathtext.

    The class must be able to take symbol keys and font file names and
    return the character metrics.  It also delegates to a backend class
    to do the actual drawing.
    default_font_propr   load_glyph_flagsr#   c                 C  s   || _ || _dS )a  
        Parameters
        ----------
        default_font_prop : `~.font_manager.FontProperties`
            The default non-math font, or the base font for Unicode (generic)
            font rendering.
        load_glyph_flags : `.ft2font.LoadFlags`
            Flags passed to the glyph loader (e.g. ``FT_Load_Glyph`` and
            ``FT_Load_Char`` for FreeType-based fonts).
        N)r   r   )rN   r   r   r6   r6   r7   rO      s   
zFonts.__init__font1r,   
fontclass1sym1	fontsize1r:   font2
fontclass2sym2	fontsize2dpir-   c
           
      C     dS )z
        Get the kerning distance for font between *sym1* and *sym2*.

        See `~.Fonts.get_metrics` for a detailed description of the parameters.
                r6   )
rN   r   r   r   r   r   r   r   r   r   r6   r6   r7   get_kern   s   zFonts.get_kernrP   r    c                 C     t rM   NotImplementedError)rN   rP   r6   r6   r7   	_get_font      zFonts._get_font
font_classsymrQ   r   c                 C  r   rM   r   )rN   rP   r   r   rQ   r   r6   r6   r7   	_get_info     zFonts._get_infor~   c                 C  s   |  |||||}|jS )a#  
        Parameters
        ----------
        font : str
            One of the TeX font names: "tt", "it", "rm", "cal", "sf", "bf",
            "default", "regular", "bb", "frak", "scr".  "default" and "regular"
            are synonyms and use the non-math font.
        font_class : str
            One of the TeX font names (as for *font*), but **not** "bb",
            "frak", or "scr".  This is used to combine two font classes.  The
            only supported combination currently is ``get_metrics("frak", "bf",
            ...)``.
        sym : str
            A symbol in raw TeX form, e.g., "1", "x", or "\sigma".
        fontsize : float
            Font size in points.
        dpi : float
            Rendering dots-per-inch.

        Returns
        -------
        FontMetrics
        )r   rj   )rN   rP   r   r   rQ   r   rV   r6   r6   r7   get_metrics  s   zFonts.get_metricsoutputrJ   rG   rH   Nonec	           
      C  s(   |  |||||}	|j|||	f dS )z
        At position (*ox*, *oy*), draw the glyph specified by the remaining
        parameters (see `get_metrics` for their detailed description).
        N)r   r>   append)
rN   r   rG   rH   rP   r   r   rQ   r   rV   r6   r6   r7   render_glyph!  s   zFonts.render_glyphr[   r\   r]   r^   c                 C  s   |j ||||f dS )zL
        Draw a filled rectangle from (*x1*, *y1*) to (*x2*, *y2*).
        N)r?   r   rN   r   r[   r\   r]   r^   r6   r6   r7   render_rect_filled*  s   zFonts.render_rect_filledc                 C     t  )zF
        Get the xheight for the given *font* and *fontsize*.
        r   rN   rP   rQ   r   r6   r6   r7   get_xheight1  s   zFonts.get_xheightc                 C  r   )z
        Get the line thickness that matches the given font.  Used as a
        base unit for drawing lines such as in a fraction or radical.
        r   r   r6   r6   r7   get_underline_thickness7  s   zFonts.get_underline_thicknessfontnamelist[tuple[str, str]]c                 C  s
   ||fgS )a  
        Override if your font provides multiple sizes of the same
        symbol.  Should return a list of symbols matching *sym* in
        various sizes.  The expression renderer will select the most
        appropriate size for a given situation from this list.
        r6   rN   r   r   r6   r6   r7   !get_sized_alternatives_for_symbol>  s   
z'Fonts.get_sized_alternatives_for_symbolNr   r   r   r#   r   r,   r   r,   r   r,   r   r:   r   r,   r   r,   r   r,   r   r:   r   r:   r-   r:   )rP   r,   r-   r    )rP   r,   r   r,   r   r,   rQ   r:   r   r:   r-   r   )rP   r,   r   r,   r   r,   rQ   r:   r   r:   r-   r~   )r   rJ   rG   r:   rH   r:   rP   r,   r   r,   r   r,   rQ   r:   r   r:   r-   r   r   rJ   r[   r:   r\   r:   r]   r:   r^   r:   r-   r   rP   r,   rQ   r:   r   r:   r-   r:   r   r,   r   r,   r-   r   )rA   rB   rC   rD   rO   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r      s    







	

r   c                      sl   e Zd ZdZd- fddZd.ddZd/ddZd0ddZd1ddZd2dd Z	d3d!d"Z
d4 fd+d,Z  ZS )5TruetypeFontsza
    A generic base class for all font setups that use Truetype fonts
    (through FT2Font).
    r   r   r   r#   c                   sR   t  || t| j| _i | _i | _t| j}t	|}|| jd< || jd< d S )Ndefaultregular)
superrO   	functoolscacher   _fontsfontmapr   r   r   )rN   r   r   filenamedefault_font	__class__r6   r7   rO   O  s   

zTruetypeFonts.__init__rP   	str | intr-   r    c                 C  s|   || j v r| j | }ntt|}| j|}|d u r8tj|r8t	|}|| j|< || j|j
< || j|j
 < tt|S rM   )r   Tcastr,   r   getospathexistsr   r   lowerr    )rN   rP   basenamecached_fontr6   r6   r7   r   [  s   

zTruetypeFonts._get_fontrx   r*   rQ   r:   r   c                 C  s,   |j dkr|jd d |d | d  S dS )NCmex10@   rr   r&   H   r   )r   r<   )rN   rP   rx   rQ   r   r6   r6   r7   _get_offsetj  s   
zTruetypeFonts._get_offsetr   r,   r   r   tuple[FT2Font, int, bool]c                 C  r   rM   r   )rN   r   r   r   r6   r6   r7   
_get_glyphp  r   zTruetypeFonts._get_glyphr   c                 C  s   |  |||\}}}||| |j|| jd}	dd |	jD \}
}}}| ||	||}t|	jd |	jd |	j	d |
||| || |	j
d | |d	}t|||j|||	|dS )N)flagsc                 s  s    | ]}|d  V  qdS )r   Nr6   )rU   valr6   r6   r7   	<genexpr>{      z*TruetypeFonts._get_info.<locals>.<genexpr>   r   )	r   r<   r;   rk   rp   rq   ro   rw   r   )rP   rQ   r   rj   rR   rx   rS   )r   set_size	load_charr   bboxr   r~   linearHoriAdvancer<   r;   horiBearingYr   r   )rN   r   r   r   rQ   r   rP   rR   r   rx   rk   rq   rp   ro   rS   rj   r6   r6   r7   r   u  s2   zTruetypeFonts._get_infoc                 C  sf   |  |}||| |d}|d u r#| |tjd d||}|jS |d d |d  |d  }|S )Npcltmathtext.defaultxxHeightg      P@g      (@g      Y@)r   r   get_sfnt_tabler   mplrcParamsrw   )rN   r   rQ   r   rP   r   rj   r   r6   r6   r7   r     s   

zTruetypeFonts.get_xheightc                 C  s   d| | d S )Ng      ?g      R@r6   r   r6   r6   r7   r     s   z%TruetypeFonts.get_underline_thicknessr   r   r   r   r   r   r   r   c
                   sn   ||kr)||kr)|  |||||	}
|  |||||	}|
j}||
j|jtjd S t |||||||||		S )Nr   )r   rP   get_kerningrR   r"   DEFAULTr   r   )rN   r   r   r   r   r   r   r   r   r   info1info2rP   r   r6   r7   r     s   
zTruetypeFonts.get_kernr   )rP   r   r-   r    )
rP   r    rx   r*   rQ   r:   r   r:   r-   r:   r   r,   r   r,   r   r,   r-   r   )r   r,   r   r,   r   r,   rQ   r:   r   r:   r-   r   )r   r,   rQ   r:   r   r:   r-   r:   r   r   )rA   rB   rC   rD   rO   r   r   r   r   r   r   r   __classcell__r6   r6   r   r7   r   I  s    





r   )	metaclassc                      s0  e Zd ZdZdddddddd	ZdD fddZed ZdEddZ	i dg ddg ddg ddg d d!g d"d#g d$d%g d&d'g d(d)g d*d+g d,d-g d.d/g d0d1g d2d3g d4d5g d6d7g d8d9g d:d;d<gd=d>gd?Z
d@D ]
\ZZe
e e
e< qdFdBdCZ  ZS )GBakomaFontsz
    Use the Bakoma TrueType fonts for rendering.

    Symbols are strewn about a number of font files, each of which has
    its own proprietary 8-bit encoding.
    cmsy10cmr10cmtt10cmmi10cmb10cmss10cmex10)calrmttitbfsfexr   r   r   r#   c                   sN   t ||| _t || | j D ]\}}t|}|| j|< || j|< qd S rM   )	StixFonts_stix_fallbackr   rO   _fontmapitemsr   r   )rN   r   r   keyr   fullpathr   r6   r7   rO     s   
zBakomaFonts.__init__
\int \ointr   r,   r   r   r-   r   c                 C  s   d }|| j v r |tv r t| \}}|dkp|| jv }| |}nt|dkr7|dk}| |}|d ur7t|}|d urG||dkrG|||fS | j|||S )Nr   r   r   r   )	r   r   _slanted_symbolsr   lenr0   get_char_indexr   r   )rN   r   r   r   rP   r   rR   r   r6   r6   r7   r     s   

zBakomaFonts._get_glyph())r   r   )r      ¡)r      ³)r      µ)r      Ã)))r   r  )r      ¢)r      ´)r      ¶)r   !{))r   r  )r      ©)r   n)r      ½)r   r   }))r   r  )r      ª)r   o)r      ¾)r   r  [))r   r  )r      £)r   rX   )r   "]))r   r  )r      ¤)r   i)r   #z\lfloor))r      ¥)r   j)r      ¹)r   $z\rfloor))r      ¦)r   k)r      º)r   %z\lceil))r      §)r   l)r      »)r   &z\rceil))r      ¨)r   m)r      ¼)r   'z\langle))r      ­r   D)r      ¿)r   *z\rangle))r      ®r   E)r      À)r   +	\__sqrt__))r   p)r   q)r   r)r   sz
\backslash))r      ²)r   /)r      Â)r   -r6  ))r   r6  )r      ±)r   .)r      Ë)r   ,z\widehat))r   ^)r   b)r   c)r   rc   z
\widetilde))r   ~)r   e)r   f)r   g)r   rX   r'  )r   r  r,  )<>)
)z
\leftparenr   )z\rightparentr  )z
\leftbracer  )z\rightbracer  )z\leftbracketr  )z\rightbracketr  )\{r  )\}r  )\[r  )\]r  r   c                 C  s   | j |||fgS rM   )_size_alternativesr   r   r6   r6   r7   r     s   z-BakomaFonts.get_sized_alternatives_for_symbolr   r   r   )rA   rB   rC   rD   r   rO   setsplitr   r   rJ  aliastargetr   r   r6   r6   r   r7   r     sl    
	
!#%)
r   c                      sV   e Zd ZdZdddZd fd	d
Zed ZdddZ	dddZ
dddZ  ZS ) UnicodeFontsa`  
    An abstract base class for handling Unicode fonts.

    While some reasonably complete Unicode fonts (such as DejaVu) may
    work in some situations, the only Unicode font I'm aware of with a
    complete set of math symbols is STIX.

    This class will "fallback" on the Bakoma fonts when a required
    symbol cannot be found in the font.
          )   i"  r   r   r   r#   c                   s   t jd }tttd|}|r|||nd | _t || d	 D ]}t jd|  }t
|}|| j|< q#td}t
|}|| jd< t| jtriddd	d
ddd}| D ]\}	}
t
|
}|| j|	< || j|
< qVd S d S )Nzmathtext.fallback)stixstixsanscmzcal rm tt it bf sf bfitz	mathtext.r   r   STIXGeneralSTIXSizeOneSymSTIXSizeTwoSymSTIXSizeThreeSymSTIXSizeFourSymSTIXSizeFiveSym)r   r   rr   r&         )r   r   r   StixSansFontsr   r   _fallback_fontr   rO   rL  r   r   r   
isinstancer   )rN   r   r   fallback_rcfont_clstexfontproprP   stixsizedaltfontssizenamer   r   r6   r7   rO   0  sB   


	zUnicodeFonts.__init__r   r   r,   r   uniindexr.   r-   tuple[str, int]c                 C  s   ||fS rM   r6   )rN   r   r   rh  r6   r6   r7   _map_virtual_fontU  s   zUnicodeFonts._map_virtual_fontr   r   c                 C  s  zt |}d}W n ty   td}d}td| Y nw | |||\}}|}|r|dkrJ|dk rJt|}t|d dksHt	|
d	rJd
}|dkpR|| jv }d}| |}	|	d ur|| jv rt|	jdkrtttd}	| j| }|	|}
|
dkrd}|s| jr|dv rt| jtrd
}| j|||}|d j}|ttj v rd}td|| |S |dv rt| tr| d
||S td||| | d
}	d}d}|	||fS )NT?Fz!No TeX to Unicode mapping for %a.r   r   r   LzGREEK CAPITALr   r   zfonts/ttf/cmsy10.ttf)r   r   zComputer ModernzSubstituting symbol %s from %szNFont %r does not have a glyph for %a [U+%x], substituting with a dummy symbol.   )r8   r4   r0   _logwarningrj  chrunicodedatacategoryrg  
startswithr   r   _cmr10_substitutionsfamily_namer   r   _get_data_pathr   r_  r`  r   r   listr   r   valuesrV   )rN   r   r   r   rh  found_symbolnew_fontnamecharr   rP   
glyphindexrC  familyr6   r6   r7   r   Y  sn   








zUnicodeFonts._get_glyphr   c                 C  s   | j r
| j ||S ||fgS rM   )r_  r   r   r6   r6   r7   r     s
   
z.UnicodeFonts.get_sized_alternatives_for_symbolr   r   r,   r   r,   rh  r.   r-   ri  r   r   )rA   rB   rC   rD   rt  rO   rK  rL  r   rj  r   r   r   r6   r6   r   r7   rO    s    #

?rO  c                      s6   e Zd ZU i Zded< dddZd fddZ  ZS )DejaVuFontsdict[str | int, str]r   r   r   r   r#   c                 C  s   t | trt||| _nt||| _t||| _t| || | j	
dddddd | j	 D ]\}}t|}|| j|< || j|< q0d S )NrW  rX  rY  rZ  r[  )r   rr   r&   r\  r]  )r`  DejaVuSerifFontsr   r_  r^  r   bakomar   rO   r   updater   r   r   rN   r   r   r   rg  r   r6   r6   r7   rO     s"   

zDejaVuFonts.__init__r   r,   r   r   r-   r   c                   sd   |dkr| j |||S t|}| d}|d ur*||}|dkr*t d||S t |||S )N\primer   r   )r  r   r8   r   r   r   )rN   r   r   r   rh  rP   r|  r   r6   r7   r     s   

zDejaVuFonts._get_glyphr   r   )rA   rB   rC   r   rE   rO   r   r   r6   r6   r   r7   r    s   
 
r  c                	   @  s&   e Zd ZdZddddddddd	Zd
S )r  zv
    A font handling class for the DejaVu Serif fonts

    If a glyph is not found it will fallback to Stix Serif
    DejaVu SerifzDejaVu Serif:italiczDejaVu Serif:weight=boldzDejaVu Serif:italic:boldDejaVu SansDejaVu Sans MonozDejaVu Serif Displayr   r   r   bfitr   r   r   r   NrA   rB   rC   rD   r   r6   r6   r6   r7   r        
r  c                	   @  s&   e Zd ZdZdddddddddZd	S )
DejaVuSansFontszt
    A font handling class for the DejaVu Sans fonts

    If a glyph is not found it will fallback to Stix Sans
    r  zDejaVu Sans:italiczDejaVu Sans:weight=boldzDejaVu Sans:italic:boldr  zDejaVu Sans Displayr  Nr  r6   r6   r6   r7   r    r  r  c                   @  sf   e Zd ZU dZddddddddd	d
ddddZded< dZdZd&ddZd'd d!Z	e
jd(d$d%ZdS ))r   aa  
    A font handling class for the STIX fonts.

    In addition to what UnicodeFonts provides, this class:

    - supports "virtual fonts" which are complete alpha numeric
      character sets with different font styles at special Unicode
      code points, such as "Blackboard".

    - handles sized alternative characters for the STIXSizeX fonts.
    rV  zSTIXGeneral:italiczSTIXGeneral:weight=boldzSTIXGeneral:italic:boldSTIXNonUnicodezSTIXNonUnicode:italiczSTIXNonUnicode:weight=boldrW  rX  rY  rZ  r[  )r   r   r   r  nonunirmnonuniitnonunibfr   r   rr   r&   r\  r]  r  r   NFr   r   r   r#   c                 C  sB   t | || | j D ]\}}t|}|| j|< || j|< qd S rM   )r   rO   r   r   r   r   r  r6   r6   r7   rO     s   
zStixFonts.__init__r   r,   r   rh  r.   r-   ri  c                 C  sr  t |}| jr|d u r|dvrt d }d}nd}t|tr2z|| }W n ty1   |d }Y nw t|tr:|}nd }|d urd}t|}||k rl|| d }	||	 }
||
d k r]|	}n||
d krdn|	d }||k sJ|
d |  krz|
d krn n||
d  |
d	  }|
d }n	|sd}tj	d
 }|dv rt
||}|dv rd|  krdkrn ||fS d| }||fS )N)r   r   r   TFr   r   rr   r   r&   r   )r   r   )r   r   r   r  i   i  nonuni)r   r   _sansr`  dictr3   rw  r   r   r   r   )rN   r   r   rh  font_mappingdoing_sans_conversionmappinglohimidranger6   r6   r7   rj    sN   


 


zStixFonts._map_virtual_fontr   -list[tuple[str, str]] | list[tuple[int, str]]c                   sx   ddddddd}| ||}zt|W n ty#   ||fg Y S w  fdd	td
D }|dkr:|d d }|S )Nr  r  r  r  u   ⟨u   ⟩)rF  rG  rH  rI  rD  rE  c                   s,   g | ]}  |d kr|tfqS )r   )r   r   rp  rU   r  rN   rh  r6   r7   rY   W  s    
z?StixFonts.get_sized_alternatives_for_symbol.<locals>.<listcomp>   r0  )r   r8   r4   r  )rN   r   r   fixesalternativesr6   r  r7   r   H  s   z+StixFonts.get_sized_alternatives_for_symbolr   r~  )r   r,   r   r,   r-   r  )rA   rB   rC   rD   r   rE   r_  r  rO   rj  r   r   r   r6   r6   r6   r7   r     s,   
 

6r   c                   @  s   e Zd ZdZdZdS )r^  zd
    A font handling class for the STIX fonts (that uses sans-serif
    characters by default).
    TN)rA   rB   rC   rD   r  r6   r6   r6   r7   r^  `  s    r^  ffffff?r  c                   @  sr   e Zd ZU dZdZded< dZded< dZded< d	Zded
< dZ	ded< dZ
ded< dZded< dZded< dS )FontConstantsBasez
    A set of constants that controls how certain things, such as sub-
    and superscripts are laid out.  These are all metrics that can't
    be reliably retrieved from the font metrics in the font itself.
    皙?zT.ClassVar[float]script_spaceg?subdropr  sup1333333?sub1      ?sub2g?delta皙?delta_slanted皙?delta_integralN)rA   rB   rC   rD   r  rE   r  r  r  r  r  r  r  r6   r6   r6   r7   r    s   
 r  c                   @  s,   e Zd ZdZdZdZdZdZdZdZ	dZ
dS )ComputerModernFontConstantsg333333?r  g?r  N)rA   rB   rC   r  r  r  r  r  r  r  r  r6   r6   r6   r7   r    s    r  c                   @  s$   e Zd ZdZdZdZdZdZdZdS )STIXFontConstantsr  皙?333333?r  r  N)	rA   rB   rC   r  r  r  r  r  r  r6   r6   r6   r7   r    s    r  c                   @     e Zd ZdZdZdZdZdS )STIXSansFontConstantsr  r  r  r  N)rA   rB   rC   r  r  r  r  r6   r6   r6   r7   r    
    r  c                   @     e Zd ZdS )DejaVuSerifFontConstantsNrA   rB   rC   r6   r6   r6   r7   r        r  c                   @  r  )DejaVuSansFontConstantsNr  r6   r6   r6   r7   r    r  r  r  r  r  r   r   r   r   r   r   r   rV  r  r[  rZ  rY  rX  rW  )zBitstream Vera SanszBitstream VerastateParserStatetype[FontConstantsBase]c                 C  s4   t | j| jjt}|tu rt| jt	rt
S |S rM   )_font_constant_mappingr   fontsetr   rP   ru  r  r  r`  r^  r  )r  	constantsr6   r6   r7   _get_font_constant_set  s   
r  c                   @  sB   e Zd ZdZdddZdddZdddZdddZdddZdS )NodezA node in the TeX box model.r-   r   c                 C  s
   d| _ d S Nr   rf  rN   r6   r6   r7   rO        
zNode.__init__r,   c                 C  s
   t | jS rM   )typerA   r  r6   r6   r7   __repr__  r  zNode.__repr__nextNode | Noner:   c                 C  r   Nr   r6   )rN   r  r6   r6   r7   r     r   zNode.get_kerningc                 C  s   |  j d7  _ dS )z
        Shrinks one level smaller.  There are only three levels of
        sizes, after which things will no longer get smaller.
        r   Nr  r  r6   r6   r7   shrink  s   zNode.shrinkr   rJ   r   r|   c                 C  r   )zRender this node.Nr6   rN   r   r   r|   r6   r6   r7   render  s    zNode.renderNr-   r   r-   r,   r  r  r-   r:   r   rJ   r   r:   r|   r:   r-   r   )	rA   rB   rC   rD   rO   r  r   r  r  r6   r6   r6   r7   r    s    



r  c                      s:   e Zd ZdZd fdd	Zd fd
dZdddZ  ZS )rL   z A node with a physical location.r;   r:   r<   r=   r-   r   c                   s    t    || _|| _|| _d S rM   )r   rO   r;   r<   r=   )rN   r;   r<   r=   r   r6   r7   rO     s   

zBox.__init__c                   sF   t    | jtk r!|  jt9  _|  jt9  _|  jt9  _d S d S rM   )r   r  rf  NUM_SIZE_LEVELSr;   SHRINK_FACTORr<   r=   r  r   r6   r7   r    s   

z
Box.shrinkr   rJ   r[   r\   r]   r^   c                 C  s   d S rM   r6   r   r6   r6   r7   r    r   z
Box.render)r;   r:   r<   r:   r=   r:   r-   r   r  r   )rA   rB   rC   rD   rO   r  r  r   r6   r6   r   r7   rL   	  s
    rL   c                      s"   e Zd ZdZd fddZ  ZS )Vboxz$A box with only height (zero width).r<   r:   r=   c                   s   t  d|| d S r  r   rO   )rN   r<   r=   r   r6   r7   rO   !     zVbox.__init__)r<   r:   r=   r:   rA   rB   rC   rD   rO   r   r6   r6   r   r7   r        r  c                      "   e Zd ZdZd fddZ  ZS )Hboxz.A box with only width (zero height and depth).r;   r:   c                   s   t  |dd d S r  r  rN   r;   r   r6   r7   rO   (  r  zHbox.__init__r;   r:   r  r6   r6   r   r7   r  %  r  r  c                      sb   e Zd ZdZd fddZdd	d
Zd ddZd!ddZd"ddZd#ddZ	d  fddZ
  ZS )$Chara  
    A single character.

    Unlike TeX, the font information and metrics are stored with each `Char`
    to make it easier to lookup the font metrics when needed.  Note that TeX
    boxes have a width, height, and depth, unlike Type1 and TrueType which use
    a full bounding box and an advance in the x-direction.  The metrics must
    be converted to the TeX model, and the advance (if different from width)
    must be converted into a `Kern` node when the `Char` is added to its parent
    `Hlist`.
    r?  r,   r  r  c                   sD   t    || _|j| _|j| _|j| _|j| _|j| _|   d S rM   )	r   rO   r?  r  rP   r   rQ   r   _update_metrics)rN   r?  r  r   r6   r7   rO   9  s   
zChar.__init__r-   c                 C  
   d| j  S )Nz`%s`r?  r  r6   r6   r7   r  E  r  zChar.__repr__r   c                 C  s\   | j | j| j| j| j| j }| _| jdkr|j| _	n|j	| _	|j
| _|j
|j  | _d S )N )r  r   rP   r   r?  rQ   r   _metricsr   r;   rw   r<   r=   rN   rj   r6   r6   r7   r  H  s   


zChar._update_metricsrh   c                 C  s   | j jS rM   )r  r   r  r6   r6   r7   
is_slantedR  s   zChar.is_slantedr  r  r:   c                 C  sR   | j j| j }d}t|tr%| j| j| j| j	| j
|j|j|j	|j
| j	}|| S )z
        Return the amount of kerning between this and the given character.

        This method is called when characters are strung together into `Hlist`
        to create `Kern` nodes.
        r   )r  r   r;   r`  r  r  r   rP   r   r?  rQ   r   )rN   r  r   kernr6   r6   r7   r   U  s   
zChar.get_kerningr   rJ   r   r|   c              
   C  s(   | j |||| j| j| j| j| j d S rM   )r  r   rP   r   r?  rQ   r   r  r6   r6   r7   r  e  s   zChar.renderc                   sT   t    | jtk r(|  jt9  _|  jt9  _|  jt9  _|  jt9  _d S d S rM   )	r   r  rf  r  rQ   r  r;   r<   r=   r  r   r6   r7   r  j  s   

zChar.shrink)r?  r,   r  r  r  r  )r-   rh   r  r  )rA   rB   rC   rD   rO   r  r  r  r   r  r  r   r6   r6   r   r7   r  ,  s    





r  c                      s6   e Zd ZdZdddZd fddZdddZ  ZS )Accentz
    The font metrics need to be dealt with differently for accents,
    since they are already offset correctly from the baseline in
    TrueType fonts.
    r-   r   c                 C  sJ   | j | j| j| j| j| j }| _|j|j	 | _
|j|j | _d| _d S r  )r  r   rP   r   r?  rQ   r   r  rp   rk   r;   ro   rq   r<   r=   r  r6   r6   r7   r  y  s   

zAccent._update_metricsc                   s   t    |   d S rM   )r   r  r  r  r   r6   r7   r    s   
zAccent.shrinkr   rJ   r   r:   r|   c              
   C  s8   | j ||| jj || jj | j| j| j| j| j	 d S rM   )
r  r   r  rk   rq   rP   r   r?  rQ   r   r  r6   r6   r7   r    s   zAccent.renderr  r  )rA   rB   rC   rD   r  r  r  r   r6   r6   r   r7   r  s  s
    
r  c                      sH   e Zd ZdZd fddZd fdd	ZdddZd fddZ  ZS )Listz0A list of nodes (either horizontal or vertical).elementsT.Sequence[Node]c                   s6   t  ddd d| _g || _d| _d| _d| _d S )Nr   r   )r   rO   shift_amountchildrenglue_set	glue_sign
glue_orderrN   r  r   r6   r7   rO     s   

zList.__init__r-   r,   c                   s4   d t  | j| j| j| jddd | jD S )Nz/{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]z, c                 S     g | ]}t |qS r6   )reprrU   r   r6   r6   r7   rY         z!List.__repr__.<locals>.<listcomp>)	formatr   r  r;   r<   r=   r  joinr  r  r   r6   r7   r    s   zList.__repr__r   r:   signr.   totalslist[float]
error_typer   c                   s   t  fddtt d d d D d | _}|| _ | dkr)| |  | _nd| _d| _|dkrEt| jrGt	d|t
| j|  d S d S d S )Nc                 3  s     | ]} | d kr|V  qdS )r   Nr6   r  r  r6   r7   r     s    z!List._set_glue.<locals>.<genexpr>r  r   r   z	%s %s: %r)r  r  r   r  r  r  
glue_ratior  rn  ro  r  rA   )rN   r   r  r  r  r  r6   r  r7   	_set_glue  s   $

zList._set_gluec                   sL   | j D ]}|  qt   | jtk r$|  jt9  _|  jt9  _d S d S rM   )r  r  r   rf  r  r  r  r  )rN   childr   r6   r7   r    s   



zList.shrink)r  r  r  )
r   r:   r  r.   r  r  r  r,   r-   r   r  )	rA   rB   rC   rD   rO   r  r   r  r   r6   r6   r   r7   r    s    	
r  c                      sD   e Zd ZdZ			dd fddZdddZ		ddddZ  ZS )HlistzA horizontal list of boxes.r   
additionalTr  r  rb   r:   r#  "T.Literal['additional', 'exactly']do_kernrh   c                   s*   t  | |r|   | j||d d S )N)rb   r#  )r   rO   r  hpack)rN   r  rb   r#  r  r   r6   r7   rO     s   zHlist.__init__r-   r   c                 C  s   g }t | j}|rAt|D ].}| j| }||d k r"| j|d  }nd}|| ||}|dkr;t|}|| q|| _dS dS )z
        Insert `Kern` nodes between `Char` nodes to set kerning.

        The `Char` nodes themselves determine the amount of kerning they need
        (in `~Char.get_kerning`), and this function just creates the correct
        linked list.
        r   Nr   )r   r  r  r   r   Kern)rN   new_childrennum_childrenr  elemr  kerning_distancer  r6   r6   r7   r    s    





z
Hlist.kernc                 C  s  d}d}d}dgd }dgd }| j D ]r}t|tr,||j7 }t||j}t||j}qt|trY||j7 }t	|jsXt	|jsXt
|dd}	t||j|	 }t||j|	 }qt|tr{|j}
||
j7 }||
j  |
j7  < ||
j  |
j7  < qt|tr||j7 }q|| _|| _|dkr||7 }|| _|| }|dkrd| _d| _d| _dS |dkr| |d|d dS | |d	|d
 dS )a  
        Compute the dimensions of the resulting boxes, and adjust the glue if
        one of those dimensions is pre-specified.  The computed sizes normally
        enclose all of the material inside the new box; but some items may
        stick out if negative glue is used, if the box is overfull, or if a
        ``\vbox`` includes other boxes that have been shifted left.

        Parameters
        ----------
        w : float, default: 0
            A width.
        m : {'exactly', 'additional'}, default: 'additional'
            Whether to produce a box whose width is 'exactly' *w*; or a box
            with the natural width of the contents, plus *w* ('additional').

        Notes
        -----
        The defaults produce a box with the natural width of the contents.
        r   r\  r  r  r   Nr   Overfulr  Underful)r  r`  r  r;   rt   r<   r=   rL   r`   isinfgetattrGlue	glue_specstretch_orderstretchshrink_orderr  r  r  r  r  r   )rN   rb   r#  rX   rc   r   total_stretchtotal_shrinkr1  r4  r  r6   r6   r7   r    sN   










zHlist.hpack)r   r  T)r  r  rb   r:   r#  r  r  rh   r  r   r  )rb   r:   r#  r  r-   r   )rA   rB   rC   rD   rO   r  r  r   r6   r6   r   r7   r    s    
r  c                      s<   e Zd ZdZ		dd fd
dZddejfdddZ  ZS )VlistzA vertical list of boxes.r   r  r  r  rX   r:   r#  r  c                   s   t  | | j||d d S )N)rX   r#  )r   rO   vpack)rN   r  rX   r#  r   r6   r7   rO     s   zVlist.__init__r  r-   r   c                 C  s  d}d}d}dgd }dgd }| j D ]f}	t|	tr9|||	j 7 }|	j}t|	js8t|	dd}
t	||	j|
 }qt|	t
ra||7 }d}|	j}||j7 }||j  |j7  < ||j  |j7  < qt|	trp|||	j 7 }d}qt|	trytdq|| _||kr||| 7 }|| _n|| _|dkr||7 }|| _|| }|dkrd| _d| _d| _dS |dkr| |d|d	 dS | |d
|d dS )ak  
        Compute the dimensions of the resulting boxes, and to adjust the glue
        if one of those dimensions is pre-specified.

        Parameters
        ----------
        h : float, default: 0
            A height.
        m : {'exactly', 'additional'}, default: 'additional'
            Whether to produce a box whose height is 'exactly' *h*; or a box
            with the natural height of the contents, plus *h* ('additional').
        l : float, default: np.inf
            The maximum height.

        Notes
        -----
        The defaults produce a box with the natural height of the contents.
        r   r\  r  z1Internal mathtext error: Char node found in Vlistr  r   Nr   r  r  r  )r  r`  rL   r<   r=   r`   r  r;   r  rt   r  r  r  r  r  r  r  r  RuntimeErrorr  r  r  r   )rN   rX   r#  r  rb   rc   r   r  r  r1  r4  r  r6   r6   r7   r  $  sZ   







zVlist.vpackr  )r  r  rX   r:   r#  r  )rX   r:   r#  r  r  r:   r-   r   )	rA   rB   rC   rD   rO   r`   infr  r   r6   r6   r   r7   r    s    r  c                      s,   e Zd ZdZd fdd	ZdddZ  ZS )Rulea  
    A solid black rectangle.

    It has *width*, *depth*, and *height* fields just as in an `Hlist`.
    However, if any of these dimensions is inf, the actual value will be
    determined by running the rule up to the boundary of the innermost
    enclosing box.  This is called a "running dimension".  The width is never
    running in an `Hlist`; the height and depth are never running in a `Vlist`.
    r;   r:   r<   r=   r  r  c                   s   t  ||| |j| _d S rM   )r   rO   r  )rN   r;   r<   r=   r  r   r6   r7   rO   y  s   zRule.__init__r   rJ   r   r|   rb   rX   r-   r   c                 C  s    | j ||||| ||  d S rM   )r  r   )rN   r   r   r|   rb   rX   r6   r6   r7   r  }  s    zRule.render)r;   r:   r<   r:   r=   r:   r  r  )r   rJ   r   r:   r|   r:   rb   r:   rX   r:   r-   r   )rA   rB   rC   rD   rO   r  r   r6   r6   r   r7   r  n  s    
r  c                      s$   e Zd ZdZd	d
 fddZ  ZS )Hrulez.Convenience class to create a horizontal rule.Nr  r  	thicknessfloat | Nonec                   s4   |d u r|  }|d  }}t tj||| d S )Nr  get_current_underline_thicknessr   rO   r`   r  )rN   r  r  r<   r=   r   r6   r7   rO     s   zHrule.__init__rM   )r  r  r  r  r  r6   r6   r   r7   r    s    r  c                      r  )Vrulez,Convenience class to create a vertical rule.r  r  c                   s"   |  }t |tjtj| d S rM   r   )rN   r  r  r   r6   r7   rO     s   zVrule.__init__)r  r  r  r6   r6   r   r7   r"    r  r"  c                   @  s6   e Zd ZU ded< ded< ded< ded< ded< dS )		_GlueSpecr:   r;   r  r.   r  r  r  Nr   r6   r6   r6   r7   r#    s   
 r#  r         ?rr   g      )filfillfilllneg_filneg_fill	neg_filllemptyssc                      s0   e Zd ZdZd
 fddZd fdd	Z  ZS )r  a  
    Most of the information in this object is stored in the underlying
    ``_GlueSpec`` class, which is shared between multiple glue objects.
    (This is a memory optimization which probably doesn't matter anymore, but
    it's easier to stick to what TeX does.)
    	glue_type`_GlueSpec | T.Literal['fil', 'fill', 'filll', 'neg_fil', 'neg_fill', 'neg_filll', 'empty', 'ss']c                   sB   t    t|trtj| }nt|tr|}ntd|| _d S )Nz.glue_type must be a glue spec name or instance)r   rO   r`  r,   r#  _namedr4   r  )rN   r-  r  r   r6   r7   rO     s   



zGlue.__init__r-   r   c                   s6   t    | jtk r| j}|j|jt d| _d S d S )Nr;   )r   r  rf  r  r  _replacer;   r  )rN   rC  r   r6   r7   r    s
   

zGlue.shrink)r-  r.  r  )rA   rB   rC   rD   rO   r  r   r6   r6   r   r7   r    s    r  c                      r  )	HCenteredzl
    A convenience class to create an `Hlist` whose contents are
    centered within its enclosing box.
    r  
list[Node]c                   s&   t  jtdg|tddd d S )Nr,  Fr  r   rO   r  r  r   r6   r7   rO     s   &zHCentered.__init__r  r3  r  r6   r6   r   r7   r2        r2  c                      r  )	VCenteredzk
    A convenience class to create a `Vlist` whose contents are
    centered within its enclosing box.
    r  r3  c                   s"   t  tdg|td d S )Nr,  r5  r  r   r6   r7   rO     s   "zVCentered.__init__r6  r  r6   r6   r   r7   r8    r7  r8  c                      sB   e Zd ZdZdZdZd fddZdd	d
Zd fddZ  Z	S )r  a  
    A `Kern` node has a width field to specify a (normally
    negative) amount of spacing. This spacing correction appears in
    horizontal lists between letters like A and V when the font
    designer said that it looks better to move them closer together or
    further apart. A kern node can also appear in a vertical list,
    when its *width* denotes additional spacing in the vertical
    direction.
    r   r;   r:   c                   s   t    || _d S rM   )r   rO   r;   r  r   r6   r7   rO     s   

zKern.__init__r-   r,   c                 C  r  )Nzk%.02fr0  r  r6   r6   r7   r    r  zKern.__repr__r   c                   s*   t    | jtk r|  jt9  _d S d S rM   )r   r  rf  r  r;   r  r  r   r6   r7   r    s   

zKern.shrinkr  r  r  )
rA   rB   rC   rD   r<   r=   rO   r  r  r   r6   r6   r   r7   r    s    

r  c                      s&   e Zd ZdZ	dd fddZ  ZS )AutoHeightChara  
    A character as close to the given height and depth as possible.

    When using a font with multiple height versions of some characters (such as
    the BaKoMa fonts), the correct glyph will be selected, otherwise this will
    always just return a scaled version of the glyph.
    FNr?  r,   r<   r:   r=   r  r  alwaysrh   factorr  c                   s   |j |j|}|j |j|j|j}| }|| }	|D ]\}
}|
|_t||}|j|j	 |	d|  kr7 nqd}|jdksEt
|dkrb|d u rQ|	|j|j	  }| j|9  _t||}||j	 }t |g || _d S )Nr  r   r   r   )r  r   rP   r   rQ   r   copyr  r<   r=   r   r   rO   r  )rN   r?  r<   r=   r  r:  r;  r  r   target_totalr   r   r{  shiftr   r6   r7   rO     s.   



zAutoHeightChar.__init__)FN)r?  r,   r<   r:   r=   r:   r  r  r:  rh   r;  r  r  r6   r6   r   r7   r9    s    	r9  c                      s(   e Zd ZdZdefd fddZ  ZS )AutoWidthChara  
    A character as close to the given width as possible.

    When using a font with multiple width versions of some characters (such as
    the BaKoMa fonts), the correct glyph will be selected, otherwise this will
    always just return a scaled version of the glyph.
    Fr?  r,   r;   r:   r  r  r:  rh   
char_class
type[Char]c                   s   |j |j|}| }|D ]\}}||_|||}	|	j|kr! nq||	j }
| j|
9  _|||}	t |	g |	j| _d S rM   )r  r   rP   r<  r;   rQ   r   rO   )rN   r?  r;   r  r:  r@  r  r   r   r{  r;  r   r6   r7   rO   $  s   



zAutoWidthChar.__init__)
r?  r,   r;   r:   r  r  r:  rh   r@  rA  )rA   rB   rC   rD   r  rO   r   r6   r6   r   r7   r?    s
    r?  r   r   rK   xytuple[float, float]c              	     s   |\}}dd||| j  t| ddd d fd
dd fddt| ts;J |  S )a  
    Ship out *box* at offset *xy*, converting it to an `Output`.

    Since boxes can be inside of boxes inside of boxes, the main work of `ship`
    is done by two mutually recursive routines, `hlist_out` and `vlist_out`,
    which traverse the `Hlist` nodes and `Vlist` nodes inside of horizontal
    and vertical boxes.  The global variables used in TeX to store state as it
    processes have become local variables here.
    r   valuer:   r-   c                 S  s   | dk rdS | dkrdS | S )Ng    eg    eAr6   )rE  r6   r6   r7   clampI     zship.<locals>.clamprK   r  r   c                   s  d}d}| j }| j}}}| jD ]}t|tr)|   |j7 qt|tr4|j7 qt|trmt	|jdkrF|j7 q}||j
 t|trW| nt|tra| nJ d||j |qt|tr|j}	|j}
|j}t|	r| j}	t|
r| j}
|	dkr|dkr||
 |  ||	 ||7 qt|tr|j}|j| }|dkr|dkr|j|kr||j7 }t | j| }n|j|kr||j7 }t | j| }||7 }|7 qd S )Nr   r   Funreachable coder   )r  r  r  r`  r  r  r;   r  r  r   r  r  r  rL   r<   r=   r`   r  r  r  r  r  roundr  r  r  )rK   cur_gcur_gluer  r  	base_line	left_edger1  edgerule_height
rule_depth
rule_widthr  rF  cur_hcur_v	hlist_outoff_hoff_vr   	vlist_outr6   r7   rU  L  sp   



















zship.<locals>.hlist_outr  c                   s  d}d}| j }| j}}| j8 }| jD ]}t|tr#|j7 qt|trht|jdkr8|j|j	 7 q|j7 ||j
 }| j|_t|trR| nt|tr\| nJ d||j	 |qt|tr|j}	|j	}
|j}t|r~| j}|	|
7 }	|	dkr|
dkr|	7 |  ||	 qt|tr|j}|j| }	|dkr|dkr|j|kr||j7 }t | j| }n|j|kr||j7 }t | j| }|	|7 }	|	7 qt|trtdqd S )Nr   r   FrH  r   z1Internal mathtext error: Char node found in vlist)r  r  r<   r  r`  r  r;   r  r   r=   r  r  r  rL   r`   r  r  r  r  r  r  rI  r  r  r  r  r  )rK   rJ  rK  r  r  rM  top_edger1  save_vrO  rP  rQ  r  rR  r6   r7   rX    st   




















zship.<locals>.vlist_outN)rE  r:   r-   r:   )rK   r  r-   r   )rK   r  r-   r   )r<   rJ   r`  r  )rK   rC  rG   rH   r6   rR  r7   ru   8  s   


?;ru   msgr   c                   s   d fd	d
}t  |S )z$Helper class to raise parser errors.r4  r,   locr.   toksr   r-   T.Anyc                   s   t | | rM   r   )r4  r\  r]  r[  r6   r7   raise_error  s   zError.<locals>.raise_errorNr4  r,   r\  r.   r]  r   r-   r^  )r   setParseAction)r[  ra  r6   r`  r7   Error  s   rd  c                   @  sL   e Zd ZdZdd
dZdddZedddZejdddZdddZ	dS )r  a   
    Parser state.

    States are pushed and popped from a stack as necessary, and the "current"
    state is always at the top of the stack.

    Upon entering and leaving a group { } or math/non-math, the stack is pushed
    and popped accordingly.
    r  r   rP   r,   r   rQ   r:   r   c                 C  s"   || _ || _|| _|| _|| _d S rM   )r  _fontr   rQ   r   )rN   r  rP   r   rQ   r   r6   r6   r7   rO     s
   
zParserState.__init__r-   c                 C  s
   t  | S rM   )r<  r  r6   r6   r7   r<    r  zParserState.copyc                 C     | j S rM   )re  r  r6   r6   r7   rP     s   zParserState.fontrg  r   c                 C  s   |dv r|| _ || _d S )N)r   r   r   r  )r   re  )rN   rg  r6   r6   r7   rP     s   
c                 C  s   | j | j| j| jS )z.Return the underline thickness for this state.)r  r   rP   rQ   r   r  r6   r6   r7   r!    s   z+ParserState.get_current_underline_thicknessN)
r  r   rP   r,   r   r,   rQ   r:   r   r:   r-   r  r  )rg  r,   r-   r   )r-   r:   )
rA   rB   rC   rD   rO   r<  propertyrP   setterr!  r6   r6   r6   r7   r    s    


exprargsc                   s\   d fdd |  ddd	 }| |kr!|d
dd  |D  n| }||td| B  S )a  
    Helper to define TeX commands.

    ``cmd("\cmd", args)`` is equivalent to
    ``"\cmd" - (args | Error("Expected \cmd{arg}{...}"))`` where the names in
    the error message are taken from element names in *args*.  If *expr*
    already includes arguments (e.g. "\cmd{arg}{...}"), then they are stripped
    when constructing the parse element, but kept (and *expr* is used as is) in
    the error message.
    eltr   r-   T.Generator[str, None, None]c                 3  s@    t | tr| jD ]	} |E d H  q	d S | jr| jV  d S d S rM   )r`  r   exprsresultsName)rl  rj  namesr6   r7   rq  	  s   

zcmd.<locals>.namesr  r   r    c                 s  s    | ]}d | V  qdS )z{%s}Nr6   )rU   rg  r6   r6   r7   r     r   zcmd.<locals>.<genexpr>z	Expected N)rl  r   r-   rm  )rL  r  rd  )rj  rk  csnamer5   r6   rp  r7   cmd  s   rt  c                      sP  e Zd ZdZG dd dejZed Z	ed Z
ed Ze	e
B eB Zed Zed Zed	 Zed
 Zed Zed Zed Zed Zed ZeeB eB Zedd eededd D ZeejZdddZdd!d"Zdd$d%Zdd&d'Z dd(d)Z!dd-d.Z"dd/d0Z#dd2d3Z$dd4d5Z%e&e'j(Z)dd6d7Z*dd:d;Z+d<d<d<d=d=d>d?d?d@ddAdBdCZ,ddDdEZ-ddFdGZ.ddKdLZ/ddMdNZ0i dOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcddded^dfdVdgdXdhdZdid\djdPdkdldmdnZ1edo Z2ddpdqZ3ddrdsZ4ddtduZ5ddvdwZ6ddxdyZ7ddzd{Z8e8Z9dd|d}Z:dd~dZ;dddZ<dddZ=dddZ>dddZ?d fddZ@dddZAdddZBdddZCdddZDdddZEdddZFdddZGeG ZHZIdddZJdddZKdddZLdddZMdddZNdddZO  ZPS )Parserz
    A pyparsing-based parser for strings containing math expressions.

    Raw text may also appear outside of pairs of ``$``.

    The grammar is based directly on that in TeX, though it cuts a few corners.
    c                   @  r  )zParser._MathStyler   r   rr   r&   N)rA   rB   rC   DISPLAYSTYLE	TEXTSTYLESCRIPTSTYLESCRIPTSCRIPTSTYLEr6   r6   r6   r7   
_MathStyle  r  rz  u  + * - −
      \pm             \sqcap                   \rhd
      \mp             \sqcup                   \unlhd
      \times          \vee                     \unrhd
      \div            \wedge                   \oplus
      \ast            \setminus                \ominus
      \star           \wr                      \otimes
      \circ           \diamond                 \oslash
      \bullet         \bigtriangleup           \odot
      \cdot           \bigtriangledown         \bigcirc
      \cap            \triangleleft            \dagger
      \cup            \triangleright           \ddagger
      \uplus          \lhd                     \amalg
      \dotplus        \dotminus                \Cap
      \Cup            \barwedge                \boxdot
      \boxminus       \boxplus                 \boxtimes
      \curlyvee       \curlywedge              \divideontimes
      \doublebarwedge \leftthreetimes          \rightthreetimes
      \slash          \veebar                  \barvee
      \cupdot         \intercal                \amalg
      \circledcirc    \circleddash             \circledast
      \boxbar         \obar                    \merge
      \minuscolon     \dotsminusdots
      aF  
      = < > :
      \leq          \geq          \equiv       \models
      \prec         \succ         \sim         \perp
      \preceq       \succeq       \simeq       \mid
      \ll           \gg           \asymp       \parallel
      \subset       \supset       \approx      \bowtie
      \subseteq     \supseteq     \cong        \Join
      \sqsubset     \sqsupset     \neq         \smile
      \sqsubseteq   \sqsupseteq   \doteq       \frown
      \in           \ni           \propto      \vdash
      \dashv        \dots         \doteqdot    \leqq
      \geqq         \lneqq        \gneqq       \lessgtr
      \leqslant     \geqslant     \eqgtr       \eqless
      \eqslantless  \eqslantgtr   \lesseqgtr   \backsim
      \backsimeq    \lesssim      \gtrsim      \precsim
      \precnsim     \gnsim        \lnsim       \succsim
      \succnsim     \nsim         \lesseqqgtr  \gtreqqless
      \gtreqless    \subseteqq    \supseteqq   \subsetneqq
      \supsetneqq   \lessapprox   \approxeq    \gtrapprox
      \precapprox   \succapprox   \precnapprox \succnapprox
      \npreccurlyeq \nsucccurlyeq \nsqsubseteq \nsqsupseteq
      \sqsubsetneq  \sqsupsetneq  \nlesssim    \ngtrsim
      \nlessgtr     \ngtrless     \lnapprox    \gnapprox
      \napprox      \approxeq     \approxident \lll
      \ggg          \nparallel    \Vdash       \Vvdash
      \nVdash       \nvdash       \vDash       \nvDash
      \nVDash       \oequal       \simneqq     \triangle
      \triangleq         \triangleeq         \triangleleft
      \triangleright     \ntriangleleft      \ntriangleright
      \trianglelefteq    \ntrianglelefteq    \trianglerighteq
      \ntrianglerighteq  \blacktriangleleft  \blacktriangleright
      \equalparallel     \measuredrightangle \varlrtriangle
      \Doteq        \Bumpeq       \Subset      \Supset
      \backepsilon  \because      \therefore   \bot
      \top          \bumpeq       \circeq      \coloneq
      \curlyeqprec  \curlyeqsucc  \eqcirc      \eqcolon
      \eqsim        \fallingdotseq \gtrdot     \gtrless
      \ltimes       \rtimes       \lessdot     \ne
      \ncong        \nequiv       \ngeq        \ngtr
      \nleq         \nless        \nmid        \notin
      \nprec        \nsubset      \nsubseteq   \nsucc
      \nsupset      \nsupseteq    \pitchfork   \preccurlyeq
      \risingdotseq \subsetneq    \succcurlyeq \supsetneq
      \varpropto    \vartriangleleft \scurel
      \vartriangleright \rightangle \equal     \backcong
      \eqdef        \wedgeq       \questeq     \between
      \veeeq        \disin        \varisins    \isins
      \isindot      \varisinobar  \isinobar    \isinvb
      \isinE        \nisd         \varnis      \nis
      \varniobar    \niobar       \bagmember   \ratio
      \Equiv        \stareq       \measeq      \arceq
      \rightassert  \rightModels  \smallin     \smallowns
      \notsmallowns \nsimeqa  
     \leftarrow \longleftarrow \uparrow \Leftarrow \Longleftarrow
     \Uparrow \rightarrow \longrightarrow \downarrow \Rightarrow
     \Longrightarrow \Downarrow \leftrightarrow \updownarrow
     \longleftrightarrow \updownarrow \Leftrightarrow
     \Longleftrightarrow \Updownarrow \mapsto \longmapsto \nearrow
     \hookleftarrow \hookrightarrow \searrow \leftharpoonup
     \rightharpoonup \swarrow \leftharpoondown \rightharpoondown
     \nwarrow \rightleftharpoons \leadsto \dashrightarrow
     \dashleftarrow \leftleftarrows \leftrightarrows \Lleftarrow
     \Rrightarrow \twoheadleftarrow \leftarrowtail \looparrowleft
     \leftrightharpoons \curvearrowleft \circlearrowleft \Lsh
     \upuparrows \upharpoonleft \downharpoonleft \multimap
     \leftrightsquigarrow \rightrightarrows \rightleftarrows
     \rightrightarrows \rightleftarrows \twoheadrightarrow
     \rightarrowtail \looparrowright \rightleftharpoons
     \curvearrowright \circlearrowright \Rsh \downdownarrows
     \upharpoonright \downharpoonright \rightsquigarrow \nleftarrow
     \nrightarrow \nLeftarrow \nRightarrow \nleftrightarrow
     \nLeftrightarrow \to \Swarrow \Searrow \Nwarrow \Nearrow
     \leftsquigarrow \overleftarrow \overleftrightarrow \cwopencirclearrow
     \downzigzagarrow \cupleftarrow \rightzigzagarrow \twoheaddownarrow
     \updownarrowbar \twoheaduparrow \rightarrowbar \updownarrows
     \barleftarrow \mapsfrom \mapsdown \mapsup \Ldsh \Rdsh
     z, ; . ! \ldotp \cdotpz}
       \sum \prod \coprod \bigcap \bigcup \bigsqcup \bigvee
       \bigwedge \bigodot \bigotimes \bigoplus \biguplus
       zlim liminf limsup sup max minz.\int \oint \iint \oiint \iiint \oiiint \iiiintz3rm cal it tt sf bf bfit default bb frak scr regularz
      arccos csc ker min arcsin deg lg Pr arctan det lim sec arg dim
      liminf sin cos exp limsup sinh cosh gcd ln sup cot hom log tan
      coth inf max tanhzn
      | \| / \backslash \uparrow \downarrow \updownarrow \Uparrow
      \Downarrow \Updownarrow . \vert \Vertz[
      ( [ \{ < \lfloor \langle \lceil \lbrace \leftbrace \lbrack \leftparen \lgroup
      z]
      ) ] \} > \rfloor \rangle \rceil \rbrace \rightbrace \rbrack \rightparen \rgroup
      c                 C  s&   g | ]}t t| d   qS )r  )rq  rg  rp  rL  r   r  r6   r6   r7   rY     s   & zParser.<listcomp>u   αu   ωr   r-   r   c              	     sh  t   dM fdd}dNd
d}td _tjd _tdd jD d _tdd	d
ttjt d  _tdd _|dj _tdtjd d  _td _tj _t  _t  _t  _t  _t  _t  _   j jK  _|    jdt! j d d K  _  jdt" j d d K  _t#dd d d  _$|dg j%j& d  _'|dj( _) jt! j d  j  _* jt! j d t+   _,t#d d d  _-t#d  d d  _.t#d! d d  _/t#d"dt jd# d d t jd$ d d  d% d d t jd& d  d  d  _0t#d'td(t"t1d) j  d* d)  d+  _2t#d, d- _3t#d. d/ d-  _4t#d0 d/ d-  _5t#d1t6dd2dd3 _7t#d4t8ddt9t" j t!td5:  d6d7 _;t jd8t"td9d:g j d; td<d= td>d=B  d8td<d= B  _< j j$B  jB  j<B  _=  j  j= jB  j,B  jB K  _ t#d?dt! j=d d  _>t#d@dt! j=d+ d  _?  j j' jB  j)B  j>B  j*B  j-B  j.B  j/B  j0B  j4B  j5B  j2B  j3B  j7B  j?B  j;B K  _dA dBt@dCB  }  jdD dEt@dCB  t! j= jB |B dF dG  dHt@dCB  K  _t" j  _At6dId2dJdK _BtdL  _C jCt! jB jC  t+   _D|   jD_E jA_FdJ_Gd S )ONr-   r   c                    sP   t   D ]\} }| ds%| dvr||  t| r%|t|  qd S )N_)token	placeable
auto_delim)varsr   rs  setNamehasattrrc  r  )r   r   r1  rN   r6   r7   set_names_and_parse_actions  s   


z4Parser.__init__.<locals>.set_names_and_parse_actionsgroupr,   rq  Iterable[str]r   c              
   S  sn   g }g }|D ]}|d   r|| q|| qtdj| dttj||r*dnddttj|dS )Nr  z<\\(?P<{group}>(?:{alpha})(?![A-Za-z]){additional}{nonalpha})|rr  )r  alphar  nonalpha)isalphar   r   r  r  r_   reescape)r  rq  ends_with_alphaends_with_nonalpharg  r6   r6   r7   csnames  s   
z Parser.__init__.<locals>.csnamesz[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)spacec                 S  s   g | ]}t |jqS r6   )r,   rE  )rU   rA  r6   r6   r7   rY         z#Parser.__init__.<locals>.<listcomp>style_literalzI[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]|\\[%${}\[\]_|]z|\\(?:{})(?![A-Za-z])r  r   z\\[A-Za-z]+rg  rP   z\mathr  r  z\hspaceaccentz\fracrR   denz\dfracz\binomz\genfracldelimrdelimrulesizestylez\sqrt{value}r  r  rootrE  z	\overlinebodyz\overset
annotationz	\undersetz\textr/   )endQuoteCharz	\substackz\\)openerclosercontentpartsnucleusr{  r=  subsuperz'*apostrophesz'+z\operatornamez\boldsymbol\middlemdelimzExpected a delimiterz\leftleftr  z\rightrightr  F)unquoteResultsz(?:(?:\\[$])|[^$])*r  )r  r,   rq  r  r-   r   )HtypesSimpleNamespacer   float_literalr   _space_widthsr  rz  r  r  r  r_   r  r  r   leaveWhitespacer+   unknown_symbol
_fontnamesrP   r
   start_groupr   	end_group_delimsdelimr   r~  r}  named_placeablerequired_groupoptional_groupr|  r   r	   rt  customspace_accent_map_wide_accentsr  _function_namesfunctionr  r   unclosed_groupfracdfracbinomgenfracr   sqrtoverlineoversetundersetr   textr(   r   suppresssubstackr  simpleoperatorname
boldsymbolrd  mathmath_stringnon_mathmain_expression_math_expression_in_subscript_or_superscript)rN   r  r  r  r6   r  r7   rO     s\  


  	 

	

zParser.__init__r4  r,   fonts_objectr   rQ   r:   r   r  c              
   C  s   t |dd||g| _i | _z| j|}W n ty, } ztdt|d dd}~ww g | _d| _	i | _t
  tt|d S )z
        Parse expression *s* using the given *fonts_object* for
        output, at the given *fontsize* and *dpi*.

        Returns the parse tree of `Node` instances.
        r   r   
r   NF)r  _state_stack_em_width_cacher  parseStringr   r4   r   explainr  r   
resetCacher   r   r  )rN   r4  r  rQ   r   resultr5   r6   r6   r7   r$   o  s   zParser.parser  c                 C  s
   | j d S )z&Get the current `State` of the parser.r  )r  r  r6   r6   r7   	get_state  s   
zParser.get_statec                 C  s   | j   dS )zPop a `State` off of the stack.N)r  popr  r6   r6   r7   	pop_state  s   zParser.pop_statec                 C  s   | j |    dS )z=Push a new `State` onto the stack, copying the current state.N)r  r   r  r<  r  r6   r6   r7   
push_state  s   zParser.push_stater]  r   list[Hlist]c                 C  s   t | gS rM   )r  asListrN   r]  r6   r6   r7   r    s   zParser.mainc                 C  s   | j j|d dd ddS )Nr   r   r  T)parseAll)r  r  r  r6   r6   r7   r    rG  zParser.math_stringr^  c                 C  s   t | }|   |gS rM   )r  r  r  rN   r]  hlistr6   r6   r7   r    s   zParser.mathc                   sH   |d  dd} fdd|D }t|}   tjd   _|gS )Nr   z\$r  c                   s   g | ]	}t |  qS r6   )r  r  rU   r?  r  r6   r7   rY     s    z#Parser.non_math.<locals>.<listcomp>r   )replacer  r  r   r   r  rP   )rN   r]  r4  symbolsr  r6   r  r7   r    s   zParser.non_mathc                   s>   |    |   d _t fdd|d D }|   |gS )Nr   c                   s   g | ]}t | qS r6   )r  r  r  r6   r7   rY     r  zParser.text.<locals>.<listcomp>r   )r  r  rP   r  r  r  r6   r  r7   r    s   zParser.text
percentager  c                 C  sf   |   }|j|j|jf}| j|}|d u r-|jdtj	d d|j|j}|j
}|| j|< t|| S )Nr   r   r#  )r  rP   rQ   r   r  r   r  r   r   r   r   r  )rN   r  r  r   r;   rj   r6   r6   r7   _make_space  s   
zParser._make_spacegKqU?g0Bxq?g%?g1ZGU?r  rr   gKqUſ)\,z
\thinspacez\/z\>z\:z\;z\ r@  z\enspacez\quadz\qquadz\!c                 C  s   | j |d  }| |}|gS Nr  )r  r  )rN   r]  rR   rK   r6   r6   r7   r    s   
zParser.spacec                 C  s   |  |d gS r  )r  r  r6   r6   r7   r       zParser.customspacer\  r.   ParseResults | dict[str, str]c           	   
   C  s  |d }|dkr
d}z	t ||  }W n ty( } z	t||d| |d }~ww || jv rstdd |d | d d d D d}| js`|| jv rct|d | 	 d	ks`|d
h| j
| jv rc|gS t| d|| dgddgS || jv rtdd |d | d d d D d}tdd ||d d  D d}|dkr|d
kr|dkr|gS |dkr| r| r|gS t|| dgddgS |gS )Nr   r8  u   −zUnknown symbol: %sc                 s      | ]	}|d kr|V  qdS r  Nr6   r  r6   r6   r7   r         z Parser.symbol.<locals>.<genexpr>r  rr  r   r  r  Tr4  c                 s  r  r  r6   r  r6   r6   r7   r     r  c                 s  r  r  r6   r  r6   r6   r7   r     r  r   r<  r  r:  )r  r  r4   r   _spaced_symbolsr  r  _binary_operatorsr   rL  _left_delims_relation_symbolsr  r  _punctuation_symbolsisdigit)	rN   r4  r\  r]  r?  r{  r5   	prev_char	next_charr6   r6   r7   r+     sR   
&


& zParser.symbolc                 C  s   t ||d|d  )NzUnknown symbol: rg  r_  rN   r4  r\  r]  r6   r6   r7   r  	  s   zParser.unknown_symbolhatz\circumflexaccentbrevez\combiningbrevebarz\combiningoverlinegravez\combininggraveaccentacutez\combiningacuteaccenttildez\combiningtildedotz\combiningdotaboveddotz\combiningdiaeresisdddotz\combiningthreedotsaboveddddotz\combiningfourdotsabovevecz\combiningrightarrowabover  `r%  r@  r:  r=  z\rightarrowz
\leftarrowz\circ)overrightarrowoverleftarrowmathringzwidehat widetilde widebarc                 C  s   |   }| }|d }|d }|| jv r!td| |j|td}nt| j| |}|dkr5|  |  tt	|jd |g}|
|jd t|td|d	 t|ggS )
Nr  r   r/   )r@  r  g      @exactlyr          @)r  r!  r  r?  r;   r  r  r  r2  r  r  r  r  r  )rN   r]  r  r  r  r   
accent_boxcenteredr6   r6   r7   r  #	  s&   
zParser.accentc                 C  s   |  |||}|d |_|S )Nrg  )r  function_name)rN   r4  r\  r]  r  r6   r6   r7   r  9	  s   
zParser.functionc                 C  s  |    |  }d|_g }|d }|D ]'}t|tr'd|_|  || qt|tr5|t|| q|| q|t| d }t|t	rN|td7 }t
dd ||d  D d}	| jdd	hB }
|	|
vrw|| jvrw|| | jd
 g7 }|   |	dv rd| _t|S d| _t|S )Nr   rg  r   zoperatorname{}c                 s  r  r  r6   r  r6   r6   r7   r   Q	  r  z&Parser.operatorname.<locals>.<genexpr>rr  r=  r{  r  >   r{  r=  TF)r  r  rP   r`  r  r  r   r,   r   r   r  r  _overunder_functionsr  r  r  r  r  )rN   r4  r\  r]  r  
hlist_listrg  r?  next_char_locr  
delimitersr6   r6   r7   r  >	  s6   



zParser.operatornamec                 C  s&   |    |dr|d|  _g S NrP   )r  r   r  rP   r  r6   r6   r7   r  a	  s   
zParser.start_groupc                 C  s   t |dg }|gS Nr  r  r   )rN   r]  grpr6   r6   r7   r  h	  s   zParser.groupc                 C  s   t |dg S r  r  r  r6   r6   r7   r  l	  r  zParser.required_groupc                 C  s   |    g S rM   )r  r  r6   r6   r7   r  q	  s   zParser.end_groupc                 C  s   t |t|d)NzExpected '}')r   r   r  r6   r6   r7   r  u	  r  zParser.unclosed_groupc                 C  s   |d |   _g S r  )r  rP   r  r6   r6   r7   rP   x	  s   zParser.fontr  r  rh   c                 C  s:   t |tr|j| jv S t |trt|dr|j| jv S dS )Nr  F)r`  r  r?  _overunder_symbolsr  r  r  r  rN   r  r6   r6   r7   is_overunder|	  s
   
zParser.is_overunderc                 C  s   t |tr|j| jv S dS NF)r`  r  r?  _dropsub_symbolsr  r6   r6   r7   
is_dropsub	  s   
zParser.is_dropsubc                 C  s   t |tr	| S dS r  )r`  r  r  r  r6   r6   r7   r  	  s   
zParser.is_slantedc           #   	     s  | dtd}| dg }t| dg }|s|s|S d  }}|rB|^}	}
}|	dkr6|d ur3td|
}n
|d ur>td|
}|s"|  }|j|j|j|j	}|j
|j|j|j	}|r|d u rftg }t|D ]}|j| ||dd	i qj|  |  | |rg }d
}|j}|d ur|  t||j}|d ur|  t||j}|d }|d urt|g}||d ||td|g t|g}||d || |d urt|g}||d |td||g |j| |j }t|}||_t|g}|gS |}t|trD|j}t|r=t|d tr.t|d dr.|d d }|d }t|dr=|j j!|_t|dd}nt|t"rO|j j!|_t|g}t#|}|j}d}| $|rf|j}|j%| }|j%| }| &|r||j%| 7 }||j'||d d   7 }| $|rd|j% |j( | }d|j% |j( | }nd}|d u rtt|t)*t+|g}|  | $|r||j,|  }n|j-| }||_nstt||g}|  | $|r||j,|  }n|j.| }|d u r| |_nJtt||g} |   | $|r||j,|  }n|j/| }d| ||j | j|   }!|!d
kr2||!7 }t|t||j | j|  | g}||_| $|sV| j|j0| 7  _||g}"| j1rl|"| 2| j3d g7 }"d| _1t|"}|gS )Nr  r   r  r  r{  zDouble subscriptzDouble superscriptr   r  r         @r  r  r  Fr4  r	  r&   r  )4r   r  r   r   r  r  r   rP   rQ   r   r   r  r  r  extendr+   r  r  r  r;   r  rt   r2  r  r   r<   r=   r  r  r`  r  r  r  r   r  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  )#rN   r4  r\  r]  r  r  napostrophessubr   opargr  rule_thicknessr   r  vlistr>  r;   vgapr  vltr  	last_charr  r  	lc_heightlc_baseline	superkernsubkernr   
shift_downshift_upr|   clrspaced_nucleusr   r6   r7   r  	  s  





	












zParser.subsuperr  r  ruler  r  rR   r  c                 C  s6  |   }| }t|jD ]
}	|  |  qt|g}
t|g}t|j|j}|
|d ||d t	|
t
d|d t||t
d|d |g}|j|jtjd d|j|j}|j|j|j d |d   }||_t|t|d gg}|s||r|dkrd	}|dkrd	}| |ttttB tB  ||S |S )
Nr  r   r	  r   =rr   r  rr  r:  ) r  r!  r  rE  r  r2  rt   r;   r  r  r  r  r  r   rP   r   r   rQ   r   r<   ro   rq   r  r  r  _auto_sized_delimiterr   r   rw  rL   r  r,   )rN   r  r  r/  r  rR   r  r  r  r{  cnumcdenr;   r#  rj   r>  r  r6   r6   r7   _genfrac3
  sN   


	
zParser._genfracc                 C  s   |  t|d S )Nr  )rz  r.   r  r6   r6   r7   r  ]
  s   zParser.style_literalc              	   C  s<   |  |dd|dd|d |d| jj|d |d S )Nr  rr  r  r  r  rR   r  )r4  r   rz  rw  r  r6   r6   r7   r  `
  s
   zParser.genfracc              	   C  (   |  dd|   | jj|d |d S Nrr  rR   r  )r4  r  r!  rz  rw  r  r6   r6   r7   r  f
     zParser.fracc              	   C  r5  r6  )r4  r  r!  rz  rv  r  r6   r6   r7   r  k
  r7  zParser.dfracc              	   C  s    |  ddd| jj|d |d S )Nr   r  r   rR   r  )r4  rz  rw  r  r6   r6   r7   r  p
  s   zParser.binomc                 C  s   |d }|d }|    }|  t|g}t|g}t|j|j}	||	d ||	d |d }
||d  dkrPt|td|
|g}|j	|j
 |
 |_|S t|td|
|g}|S )Nr  r  r  r&   r   ur   )r  r!  r  r2  rt   r;   r  r  r  r=   r<   r  )rN   r4  r\  r]  r  r  r  centered_annotationcentered_bodyr;   r$  r#  r6   r6   r7   _gensetu
  s0   


zParser._gensetc                 C  s,  | d}|d }|  }| }|j|j |d  }|j|j }td|||dd}|j|j }|j|j }ttd| |td| g}	t	t
|td|	g}
|
||j|j d	  d
| |skt|jd dd}nt|}|  |  t	t|gg}| d |_t|t|j d ||
g}|gS )Nr  rE  g      @r0  T)r:  rr   r&       @r  r  r   r  )r   r  r!  r<   r  r=   r9  r  r  r  r  r  r  rQ   r   rL   r;   r  r  )rN   r]  r  r  r  r  r<   r=   checkpadded_body	rightside
root_vlistr  r6   r6   r7   r  
  s6   
zParser.sqrtc           	      C  s   |d }|   }| }|j|j |d  }|j|j }tt|tdt|gg}|	||j
|j d  d| t|g}|gS )Nr  r  r&  r<  r  )r  r!  r<   r  r=   r  r  r  r  r  rQ   r   )	rN   r]  r  r  r  r<   r=   r?  r  r6   r6   r7   r  
  s   
zParser.overlinefrontmiddlelist[Box | Char | str]backc              	   C  s  |   }t|rXtdd |D }tdd |D }d }t|D ],\}}	|	dkrLtt||d  }
|
dkrDt|
||||d||d < n||
 ||= q tt	t
tB  |}nd}d}d	}g }g }|dkrr|t|||||d || |dkr|t|||||d t|}|S )
Nc                 S     g | ]
}t |ts|jqS r6   )r`  r,   r<   r  r6   r6   r7   rY   
      z0Parser._auto_sized_delimiter.<locals>.<listcomp>c                 S  rE  r6   )r`  r,   r=   r  r6   r6   r7   rY   
  rF  r  r   r:  )r;  r   r$  )r  r   rt   	enumerater   r   r,   r9  removerw  rL   r  r   r  r  )rN   rA  rB  rD  r  r<   r=   r;  idxelr?  middle_partr  r  r6   r6   r7   r1  
  s@   


zParser._auto_sized_delimiterc                 C  s*   |  |d d|v r|d  ng |d S )Nr  r  r  )r1  r  r  r6   r6   r7   r~  
  s
   zParser.auto_delimc                 C  s   |    |  }g }|d }|D ]L}t|tr.|jd }t|tr(d|_|  || qt|trWd|_|j	| j
v sF|j	dd  | jv rMd|_|  |  || q|| q|   t|S )NrE  r   r   r  )r  r  r`  r  r  r  rP   r  r   r?  _latin_alphabets_small_greekr  )rN   r]  r  r  rg  r?  r  r6   r6   r7   r  
  s,   



zParser.boldsymbolc                 C  s   |d }|   }| }dd |d D }ttdd |}g }|D ]}t|g}	|	|d ||	 q"dd t|td|d	 gt	| D }
|
d
= t
|
}t|gg}|S )Nr  c                 S  r  r6   )r  )rU   r  r6   r6   r7   rY     r  z#Parser.substack.<locals>.<listcomp>r   c                 S  rf  rM   r0  r  r6   r6   r7   <lambda>  s    z!Parser.substack.<locals>.<lambda>r  c                 S  s   g | ]	}|D ]}|qqS r6   r6   )rU   pairr   r6   r6   r7   rY   '  s    
rr   r  )r  r!  rt   r_   r2  r  r   zipr  r   r  r  )rN   r]  r  r  r  r  	max_widthr#  r  cpstackr%  r  r6   r6   r7   r    s"   
zParser.substackr  )
r4  r,   r  r   rQ   r:   r   r:   r-   r  rg  )r]  r   r-   r  )r]  r   r-   r   )r]  r   r-   r^  )r  r:   r-   r  )r4  r,   r\  r.   r]  r  r-   r^  rb  )r-   r^  )r  r  r-   rh   )r  r,   r  r,   r/  r  r  rz  rR   r  r  r  r-   r^  )rA  r,   rB  rC  rD  r,   r-   r^  )QrA   rB   rC   rD   enumEnumrz  rK  rL  r  r  _arrow_symbolsr  r  r  r  r  r  r  _ambi_delimsr  _right_delimsr  r  r0   rM  stringascii_lettersrL  rO   r$   r  r  r  r  r  r  r  staticmethodr   convertToFloatr  r  r  r  r  r  r+   r  r  r  r  r  r  r  r  r  r  r  r  rP   r  r  r  r  r4  r  r  r  r  r  r;  r  r  r  r  r1  r~  r  r  r   r6   r6   r   r7   ru    s   57


 
:







	





0	




#







 
'
*




 

(

&
ru  )r+   r,   r-   r.   )r  r  r-   r  )rB  )rK   rL   rC  rD  r-   rJ   )r[  r,   r-   r   )rj  r,   rk  r   r-   r   )|rD   
__future__r   abcr<  rT  r   loggingr   r  r  rq  rY  typingr   r   numpyr`   	pyparsingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   
matplotlibr   rr  r   _mathtext_datar   r   r   r   font_managerr   r   r   ft2fontr    r!   r"   r#   packaging.versionr$   parse_versionr%   pyparsing_versionmajorr'   r(   TYPE_CHECKINGcollections.abcr)   r*   enablePackrat	getLoggerrn  r8   r9   rB   rF   rJ   r~   r   ABCr   ABCMetar   r   rO  r  r  r  r   r^  r  r  r  r  r  r  r  r  r  r  r  rL   r  r  r  r  r  r  r  r  r  r"  r#  r/  r  r2  r8  r  r9  r?  ru   rd  r  rt  ru  r6   r6   r6   r7   <module>   s   T

; 
lij )s!$		

G,fR
	

( 

&