o
    i:                     @   s   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
mZm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 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_layoutdvireadfont_managerrcParams)FontPropertiesget_font)LOAD_NO_HINTINGLOAD_TARGET_LIGHT)MathTextParser)Path)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 ZdddZ		d ddZ		d ddZdd Z		d ddZeed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_parser_texmanagerself r   N/var/www/edux/Edux_v2/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.
        )r   findfontr   set_size
FONT_SCALEDPI)r   propfnamefontr   r   r   	_get_font   s   
zTextToPath._get_fontc                 C   s   t S )N)r	   r   r   r   r   _get_hinting_flag%   s   zTextToPath._get_hinting_flagc                 C   s   t jd|j|S )zO
        Return a unique id for the given font and character-code set.
        z{}-{})urllibparsequoteformatpostscript_name)r   r   ccoder   r   r   _get_char_id(   s   zTextToPath._get_char_idc                 C   s$   |  d }tjd||f }|S )zY
        Return a unique id for the given font and character-code set (for tex).
           z%s-%d)get_ps_font_infor!   r"   r#   )r   r   r&   ps_namechar_idr   r   r   _get_char_id_ps.   s   zTextToPath._get_char_id_psc                 C   s   |dkr|   }| }|j||d d\}}}|||fS | }|| j }	|rI| }|| j | j|d|\}
}}}}|
|	 ||	 ||	 fS | |}|j	|dt
d | \}}|d }|d }| }|d }||	 ||	 ||	 fS )NTeX)rendererr   g        flagsg      P@)get_texmanagerget_size_in_pointsget_text_width_height_descentr   copyr   r   r"   r   set_textr	   get_width_heightget_descent)r   sr   ismath
texmanagerfontsizewhdscalewidthheightdescenttrashused_charactersr   r   r   r   r3   6   s.   


z(TextToPath.get_text_width_height_descentFc                 C   s   |dkr|  ||\}}}n|s| |}| ||\}}}n	| ||\}}}g g }}	|D ]%\}
}}}||
 \}}t|rTt|| ||g }|| |	| q/|D ]\}}|| |	| qW||	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 numpy arrays containing the x and 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.textpath import TextToPath
            from matplotlib.font_manager import FontProperties

            fp = FontProperties(family="Humor Sans", 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-   )get_glyphs_texr   get_glyphs_with_fontget_glyphs_mathtextlennparrayextend)r   r   r8   r9   
glyph_info	glyph_maprectsr   vertscodesglyph_id	xposition	ypositionr?   verts1codes1r   r   r   get_text_pathR   s$   (




zTextToPath.get_text_pathNc                 C   s   |du rt  }|rt  }n|}g }g }t|t||D ]"\}\}	}
| |t|}|| ||
 ||vr>| ||< 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   zipr   layoutr'   ordappendget_pathrH   list)r   r   r8   rM   return_new_glyphs_onlyglyph_map_new
xpositions	glyph_idschar_xr+   
ypositionssizesrN   r   r   r   rF      s(   

zTextToPath.get_glyphs_with_fontc                 C   sr  |  }|| j | j|| j|\}}}}}	|st }|r#t }
n|}
g }g }g }g }|D ]C\}}}}}| ||}||vrY|  || j| j |j	|t
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 qwtt|||||
|fS )zW
        Parse mathtext string *s* and convert it to a (vertices, codes) pair.
        r/   r   r   )r4   r   r   r   r"   r   r   r'   clear	load_charr	   r[   rZ   r   MOVETOLINETO	CLOSEPOLYr\   rW   )r   r   r8   rM   r]   r@   rA   rB   glyphsrN   r^   r_   rd   r`   re   r   r;   r&   oxoyr+   sizemyrectsr<   r=   vert1code1r   r   r   rG      sN   



zTextToPath.get_glyphs_mathtextc                 C   s$   | j du rddlm} | | _ | j S )z5Return the cached `~.texmanager.TexManager` instance.Nr   )
TexManager)r   matplotlib.texmanagerrs   )r   rs   r   r   r   r1      s   
zTextToPath.get_texmanagerc                 C   s  |   || j}t|| j}|\}W d   n1 sw   Y  |du r*t }|r0t }n|}g g g g f\}	}
}}|jD ]]\}}}}}| |j	\}}| 
||}||vr|  || j| j |durw||| }|j|td n|j|td | ||< |	| |
| || ||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 qtt|	|
||||fS )z?Convert the string *s* to vertices and codes using usetex mode.Nr/   rf   )r1   make_dvir   r   Dvir   r   text_get_ps_font_and_encodingtexnamer,   rg   r   get_name_index
load_glyphr
   rh   r[   rZ   ro   boxesr   ri   rj   rk   r\   rW   )r   r   r8   rM   r]   dvifiledvipager^   r`   r_   rd   re   x1y1dvifontglyphr@   r   encr+   indexrp   rm   rn   r=   r<   rq   rr   r   r   r   rE      sL   


zTextToPath.get_glyphs_tex2   c              
   C   s   t t d}||  }|jd u rtd|j d|  dt|j}|jr/t |j}||fS dD ]}z|	| W n tt
fyE   Y q1w  ntd|j d }||fS )Nz
pdftex.mapzNo usable font file found for z (z&). The font may lack a Type-1 version.)iCBDAiBODAz#No supported encoding in font (%s).)r   
PsfontsMapfind_tex_filefilename
ValueErrorpsnamer   encoding
_parse_encselect_charmapRuntimeError_logwarning)ry   tex_font_mappsfontr   r   charmap_coder   r   r   rx      s,   

z$TextToPath._get_ps_font_and_encoding)F)NF)__name__
__module____qualname____doc__r   r   r   r   r    r'   r,   r3   rV   rF   rG   r1   rE   staticmethod	functools	lru_cacherx   r   r   r   r   r      s0    	
?

3
6r   c                   @   sN   e Zd ZdZ		dddZdd Zd	d
 Zedd Zedd Z	dd Z
dS )TextPathz&
    Create a path from the text.
    N   Fc           	      C   s   ddl m} t|}|du r| }|| _| | d| _||d|\}}t	j
|||d\| _| _d| _t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 <matplotlib-rcparams>`.

        _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.textpath 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)r9   Fzpath.simplify_threshold)matplotlib.textr   r   	_from_anyr2   _xyr   _cached_vertices_preprocess_mathtext_to_pathrV   	_vertices_codes_should_simplifyr   _simplify_threshold_interpolation_steps)	r   xyr8   ro   r   r   r   r   r9   r   r   r   r   U  s   -



zTextPath.__init__c                 C   s   || _ d| _dS )zSet the text size.TN)_size_invalid)r   ro   r   r   r   r     s   
zTextPath.set_sizec                 C      | j S )zGet the text size.)r   r   r   r   r   get_size  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   vertices  s   zTextPath.verticesc                 C   r   )z"
        Return the codes
        )r   r   r   r   r   rP     s   zTextPath.codesc                 C   sH   | j s| jdu r"t | jtj j| 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   r?   r   r   r   	translater   	transformr   )r   trr   r   r   r     s   
zTextPath._revalidate_path)NNr   F)r   r   r   r   r   r   r   propertyr   rP   r   r   r   r   r   r   P  s    
>

r   )collectionsr   r   loggingurllib.parser!   numpyrI   
matplotlibr   r   r   r   matplotlib.font_managerr   r   matplotlib.ft2fontr	   r
   matplotlib.mathtextr   matplotlib.pathr   matplotlib.transformsr   	getLoggerr   r   r   r   r   r   r   r   r   <module>   s"    
  =