a
    Sic4                     @   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	m
Z
 d dlmZ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 d d	lmZ eeZG d
d dZe ZG dd deZ dS )    )OrderedDictN)_api_text_helpersdviread)FontPropertiesget_fontfontManager)LOAD_NO_HINTINGLOAD_TARGET_LIGHT)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ejdd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_parser_texmanagerself r   O/var/www/html/django/DPS/env/lib/python3.9/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.
        )_fontManagerZ_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 S )N)r	   r   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 }|rn| }|| j | j|d|^}}}}	|| || || fS | |}
|
j	|dt
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	   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|  ||\}}}n4|s>| |}| ||\}}}n| ||\}}}g g  }}	|D ]8\}
}}}||
 \}}||| ||g  |	| q^|D ]\}}|| |	| q|st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 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*   )r      )get_glyphs_texr    get_glyphs_with_fontget_glyphs_mathtextextendnpempty)r   r   r3   r4   Z
glyph_info	glyph_maprectsr   vertscodesZglyph_idZ	xpositionZ	ypositionr6   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 ]D}| |jt|j}	||	 ||j |	|vr2|j	 ||	< q2d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   r3   rE   return_new_glyphs_onlyglyph_map_new
xpositions	glyph_idsitemchar_id
ypositionssizesrF   r   r   r   r@      s&    
zTextToPath.get_glyphs_with_fontc                 C   sr  |  }|| j | j|| j|\}}}}}	|s:t }|rFt }
n|}
g }g }g }g }|D ]\}}}}}| ||}||vr|  || j| j |j	|t
d | |
|< || || || || j }|| q^g }|	D ]j\}}}}||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 )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	   rN   rM   r   MOVETOLINETO	CLOSEPOLYrP   rQ   )r   r   r3   rE   rR   r7   r8   r9   ZglyphsrF   rS   rT   rX   rU   rY   r   r5   r(   oxoyrW   sizemyrectsr;   r<   vert1code1r   r   r   rA      sN    



zTextToPath.get_glyphs_mathtextz3.6zTexManager())alternativec                 C   s   | j du rt | _ | j S )z5Return the cached `~.texmanager.TexManager` instance.N)r   r   r   r   r   r   get_texmanager   s    
zTextToPath.get_texmanagerc                 C   s  t  || j}t|| j}|\}W d   n1 s:0    Y  |du rRt }|r^t }n|}g g g g f\}	}
}}|jD ]}t|j	}| 
||j}||vr$|  || j| j |j}t|tr||}|j|td n4t|tr
| | |j|td ntd|| ||< |	| |
|j ||j ||j| j  q|g }|jD ]l\}}}}||f|| |f|| || f||| f||fdg}tjtj tj tj tj tj!g}|||f qdt"t#|	|
||||fS )z?Convert the string *s* to vertices and codes using usetex mode.Nr+   zGlyph spec of unexpected type: rZ   )$r   Zmake_dvir   r   ZDvir   r   textr   Z	font_pathr)   Zglyphr[   r   glyph_name_or_index
isinstancestrget_name_index
load_glyphr
   int_select_native_charmapr\   	TypeErrorrN   rM   r#   y	font_sizeboxesr   r]   r^   r_   rP   rQ   )r   r   r3   rE   rR   ZdvifileZdvipagerS   rU   rT   rX   rY   rh   r   rW   ri   indexrc   r`   ra   r<   r;   rd   re   r   r   r   r?      sZ    $






zTextToPath.get_glyphs_texc              
   C   sF   dD ].}z|  | W n ttfy,   Y q0  qBq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   ro     s    z!TextToPath._select_native_charmap)F)NF)NF)NF)__name__
__module____qualname____doc__r   r   r   r    r!   r)   r.   rI   r@   rA   r   
deprecatedrg   r?   staticmethodro   r   r   r   r   r      s,   	
@  
  
3
  
:r   c                       sR   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.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)r4   T)_interpolation_stepsreadonlyF)matplotlib.textr   r   Z	_from_anyr-   _xyr   _cached_verticesZ_preprocess_mathsuperr   text_to_pathrI   _should_simplify)	r   xyr3   rb   r   r   r   r   r4   	__class__r   r   r   5  s    -

zTextPath.__init__c                 C   s   || _ d| _dS )zSet the text size.TN)_size_invalid)r   rb   r   r   r   r   s  s    zTextPath.set_sizec                 C   s   | j S )zGet the text size.)r   r   r   r   r   get_sizex  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   s   | j S )z"
        Return the codes
        )_codesr   r   r   r   rH     s    zTextPath.codesc                 C   sN   | j s| jdu rJt | jtj j| j }|	| j
| _d| jj_d| _ 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   r6   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   rH   r   __classcell__r   r   r   r   r   0  s     >

r   )!collectionsr   loggingurllib.parser$   numpyrC   
matplotlibr   r   r   matplotlib.font_managerr   r   r   r   Zmatplotlib.ft2fontr	   r
   Zmatplotlib.mathtextr   matplotlib.pathr   matplotlib.texmanagerr   matplotlib.transformsr   	getLoggerr|   ry   r   r   r   r   r   r   r   <module>   s    
  