o
    môjÆ3  ã                   @   s¶   d dl m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mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ e e¡ZG d
d„ dƒZeƒ ZG dd„ deƒZdS )é    )ÚOrderedDictN)Ú_text_helpersÚdviread)ÚFontPropertiesÚget_fontÚfontManager)Ú	LoadFlags)ÚMathTextParser)ÚPath)Ú
TexManager)ÚAffine2Dc                   @   s€   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
ddd„Z		ddd„Z		ddd„Z		ddd„Zedd„ ƒZdS )Ú
TextToPathz'A class that converts strings to paths.g      Y@éH   c                 C   s   t dƒ| _d | _d S )NÚpath)r	   Úmathtext_parserZ_texmanager©Úself© r   úf/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/matplotlib/textpath.pyÚ__init__   s   

zTextToPath.__init__c                 C   s&   t  |¡}t|ƒ}| | j| j¡ |S )zX
        Find the `FT2Font` matching font properties *prop*, with its size set.
        )Ú_fontManagerÚ_find_fonts_by_propsr   Úset_sizeÚ
FONT_SCALEÚDPI)r   ÚpropÚ	filenamesÚfontr   r   r   Ú	_get_font   s   
zTextToPath._get_fontc                 C   s   t jS )N)r   Ú
NO_HINTINGr   r   r   r   Ú_get_hinting_flag'   s   zTextToPath._get_hinting_flagc                 C   s   t j |j› d|d›�¡S )zO
        Return a unique id for the given font and character-code set.
        ú-Úx)ÚurllibÚparseÚquoteÚpostscript_name)r   r   Úccoder   r   r   Ú_get_char_id*   s   zTextToPath._get_char_idc                 C   sÌ   |  ¡ }|dkrtƒ  ||¡S || j }|r7| ¡ }| | j¡ | j |d|¡^}}}}	|| || || fS |  |¡}
|
j	|dt
jd� |
 ¡ \}}|d }|d }|
 ¡ }|d }|| || || fS )NÚTeXr   g        ©Úflagsg      P@)Úget_size_in_pointsr   Úget_text_width_height_descentr   Úcopyr   r   r$   r   Úset_textr   r   Úget_width_heightÚget_descent)r   Úsr   ÚismathÚfontsizeÚscaleÚwidthÚheightÚdescentÚ_r   ÚwÚhÚdr   r   r   r-   0   s$   

ÿ
z(TextToPath.get_text_width_height_descentFc                 C   sÐ   |dkr|   ||¡\}}}n|s|  |¡}|  ||¡\}}}n	|  ||¡\}}}g g }}	|D ]\}
}}}||
 \}}| || ||g ¡ |	 |¡ q/|D ]\}}| |¡ |	 |¡ qN|sdt d¡}||	fS )a‘  
        Convert text *s* to path (a tuple of vertices and codes for
        matplotlib.path.Path).

        Parameters
        ----------
        prop : `~matplotlib.font_manager.FontProperties`
            The font properties for the text.
        s : str
            The text to be converted.
        ismath : {False, True, "TeX"}
            If True, use mathtext parser.  If "TeX", use tex for rendering.

        Returns
        -------
        verts : list
            A list of arrays containing the (x, y) coordinates of the vertices.
        codes : list
            A list of path codes.

        Examples
        --------
        Create a list of vertices and codes from a text, and create a `.Path`
        from those::

            from matplotlib.path import Path
            from matplotlib.text import TextToPath
            from matplotlib.font_manager import FontProperties

            fp = FontProperties(family="Comic Neue", style="italic")
            verts, codes = TextToPath().get_text_path(fp, "ABC")
            path = Path(verts, codes, closed=False)

        Also see `TextPath` for a more direct way to create a path from a text.
        r)   )r   é   )Úget_glyphs_texr   Úget_glyphs_with_fontÚget_glyphs_mathtextÚextendÚnpÚempty)r   r   r2   r3   Z
glyph_infoÚ	glyph_mapÚrectsr   ÚvertsÚcodesZglyph_idZ	xpositionZ	ypositionr5   Zverts1Zcodes1r   r   r   Úget_text_pathH   s"   $



zTextToPath.get_text_pathNc                 C   s°   |du rt ƒ }|rt ƒ }n|}g }g }t ||¡D ]"}|  |jt|jƒ¡}	| |	¡ | |j¡ |	|vr;|j 	¡ ||	< qdgt
|ƒ }
dgt
|ƒ }g }tt|||
|ƒƒ||fS )zW
        Convert string *s* to vertices and codes using the provided ttf font.
        Nr   g      ð?)r   r   Úlayoutr(   Z	ft_objectÚordÚcharÚappendr"   Úget_pathÚlenÚlistÚzip)r   r   r2   rD   Úreturn_new_glyphs_onlyÚglyph_map_newÚ
xpositionsÚ	glyph_idsÚitemÚchar_idÚ
ypositionsÚsizesrE   r   r   r   r?   „   s(   
€ÿzTextToPath.get_glyphs_with_fontc                 C   st  |  ¡ }| | j¡ | j || j|¡\}}}}}	|stƒ }|r#tƒ }
n|}
g }g }g }g }|D ]D\}}}}}|  ||¡}||vrZ| ¡  | | j| j¡ |j	|t
jd� | ¡ |
|< | |¡ | |¡ | |¡ || j }| |¡ q/g }|	D ]5\}}}}||f||| f|| || f|| |f||fdg}tjtjtjtjtjtjg}| ||f¡ qxtt||||ƒƒ|
|fS )zW
        Parse mathtext string *s* and convert it to a (vertices, codes) pair.
        r*   ©r   r   )r.   r   r   r   r$   r   r   r(   ÚclearÚ	load_charr   r   rM   rL   r
   ÚMOVETOÚLINETOÚ	CLOSEPOLYrO   rP   )r   r   r2   rD   rQ   r6   r7   r8   ZglyphsrE   rR   rS   rW   rT   rX   r   r4   r'   ÚoxÚoyrV   ÚsizeÚmyrectsr:   r;   Úvert1Úcode1r   r   r   r@   £   sN   ÿ



ÿþÿzTextToPath.get_glyphs_mathtextc                 C   sè  t ƒ  || j¡}t || j¡�}|\}W d  ƒ n1 sw   Y  |du r)tƒ }|r/tƒ }n|}g g g g f\}	}
}}|jD ]n}t|j	ƒ}|  
||j¡}||vr’| ¡  | | j| j¡ |j}t|tƒrr| |¡}|j|tjd� nt|tƒr…|  |¡ |j|tjd� ntd|›�ƒ‚| ¡ ||< |	 |¡ |
 |j¡ | |j¡ | |j| j ¡ q>g }|jD ]5\}}}}||f|| |f|| || f||| f||fdg}tj tj!tj!tj!tj!tj"g}| ||f¡ q²t#t$|	|
||ƒƒ||fS )z?Convert the string *s* to vertices and codes using usetex mode.Nr*   zGlyph spec of unexpected type: rY   )%r   Zmake_dvir   r   ZDvir   r   Útextr   Ú	font_pathr(   ZglyphrZ   r   Úglyph_name_or_indexÚ
isinstanceÚstrÚget_name_indexÚ
load_glyphr   ÚTARGET_LIGHTÚintÚ_select_native_charmapr[   Ú	TypeErrorrM   rL   r"   ÚyÚ	font_sizeÚboxesr
   r\   r]   r^   rO   rP   )r   r   r2   rD   rQ   ZdvifileÚdviÚpagerR   rT   rS   rW   rX   re   r   rV   rg   Úindexrb   r_   r`   r;   r:   rc   rd   r   r   r   r>   Ö   s\   ÿ





ÿÿ
ÿþÿzTextToPath.get_glyphs_texc              
   C   sD   dD ]}z	|   |¡ W  d S  ttfy   Y qw t d| j¡ d S )N)iCBDAiBODAz#No supported encoding in font (%s).)Úselect_charmapÚ
ValueErrorÚRuntimeErrorÚ_logÚwarningÚfname)r   Zcharmap_coder   r   r   rn     s   ýÿz!TextToPath._select_native_charmap)F)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    r(   r-   rH   r?   r@   r>   Ústaticmethodrn   r   r   r   r   r      s*    	
<
ÿ
ÿ3
ÿ:r   c                       sV   e Zd ZdZ		d‡ fdd„	Zdd„ Zd	d
„ Zedd„ ƒZedd„ ƒZ	dd„ Z
‡  ZS )ÚTextPathz&
    Create a path from the text.
    Né   Fc           	         sz   ddl m} t |¡}|du r| ¡ }|| _|  |¡ d| _||d� |¡\}}t	ƒ j
tj|||d�|ddœŽ d| _dS )	a  
        Create a path from the text. Note that it simply is a path,
        not an artist. You need to use the `.PathPatch` (or other artists)
        to draw this path onto the canvas.

        Parameters
        ----------
        xy : tuple or array of two float values
            Position of the text. For no offset, use ``xy=(0, 0)``.

        s : str
            The text to convert to a path.

        size : float, optional
            Font size in points. Defaults to the size specified via the font
            properties *prop*.

        prop : `~matplotlib.font_manager.FontProperties`, optional
            Font property. If not provided, will use a default
            `.FontProperties` with parameters from the
            :ref:`rcParams<customizing-with-dynamic-rc-settings>`.

        _interpolation_steps : int, optional
            (Currently ignored)

        usetex : bool, default: False
            Whether to use tex rendering.

        Examples
        --------
        The following creates a path from the string "ABC" with Helvetica
        font face; and another path from the latex fraction 1/2::

            from matplotlib.text import TextPath
            from matplotlib.font_manager import FontProperties

            fp = FontProperties(family="Helvetica", style="italic")
            path1 = TextPath((12, 12), "ABC", size=12, prop=fp)
            path2 = TextPath((0, 0), r"$\frac{1}{2}$", size=12, usetex=True)

        Also see :doc:`/gallery/text_labels_and_annotations/demo_text_path`.
        r   )ÚTextN)Úusetex)r3   T)Ú_interpolation_stepsÚreadonlyF)Úmatplotlib.textrƒ   r   Ú	_from_anyr,   Ú_xyr   Ú_cached_verticesÚ_preprocess_mathÚsuperr   Útext_to_pathrH   Ú_should_simplify)	r   Úxyr2   ra   r   r…   r„   rƒ   r3   ©Ú	__class__r   r   r   *  s   -

ý
zTextPath.__init__c                 C   s   || _ d| _dS )zSet the text size.TN)Ú_sizeÚ_invalid)r   ra   r   r   r   r   h  s   
zTextPath.set_sizec                 C   ó   | j S )zGet the text size.)r’   r   r   r   r   Úget_sizem  s   zTextPath.get_sizec                 C   s   |   ¡  | jS )zH
        Return the cached path after updating it if necessary.
        )Ú_revalidate_pathrŠ   r   r   r   r   Úverticesq  s   zTextPath.verticesc                 C   r”   )z"
        Return the codes
        )Ú_codesr   r   r   r   rG   y  s   zTextPath.codesc                 C   sR   | j s| jdu r'tƒ  | jtj ¡j| jŽ }| 	| j
¡| _d| jj_d| _ dS dS )zÇ
        Update the path if necessary.

        The path for the text is initially create with the font size of
        `.FONT_SCALE`, and this path is rescaled to other size when necessary.
        NF)r“   rŠ   r   r5   r’   r�   r   Ú	translater‰   Ú	transformÚ	_verticesr+   Ú	writeable)r   Útrr   r   r   r–   €  s   þ

úzTextPath._revalidate_path)NNr‚   F)r|   r}   r~   r   r   r   r•   Úpropertyr—   rG   r–   Ú__classcell__r   r   r�   r   r�   %  s    ÿ>

r�   )Úcollectionsr   ÚloggingÚurllib.parser#   ÚnumpyrB   Ú
matplotlibr   r   Úmatplotlib.font_managerr   r   r   r   Zmatplotlib.ft2fontr   Zmatplotlib.mathtextr	   Úmatplotlib.pathr
   Zmatplotlib.texmanagerr   Úmatplotlib.transformsr   Ú	getLoggerr|   ry   r   r�   r�   r   r   r   r   Ú<module>   s"    
  