B
    ò¶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   ú7/tmp/pip-unpacked-wheel-tvo012g9/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   Z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ÚformatZ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)Zget_ps_font_infor   r    r!   )r   r   r#   Zps_nameÚchar_idr   r   r   Ú_get_char_id_ps.   s    zTextToPath._get_char_id_psc             C   sî   |dkr8|   ¡ }| ¡ }|j||d d�\}}}|||fS | ¡ }|| j }	|r’| ¡ }| | j¡ | j |d|¡\}
}}}}|
|	 ||	 ||	 fS |  |¡}|j	|dt
d� | ¡ \}}|d }|d }| ¡ }|d }||	 ||	 ||	 fS )NÚTeX)Zrendererr   g        )Úflagsg      P@)Úget_texmanagerÚget_size_in_pointsÚget_text_width_height_descentr   Úcopyr   r   r    r   Zset_textr	   Zget_width_heightZget_descent)r   Úsr   ÚismathZ
texmanagerÚfontsizeÚwÚhÚdÚscaleÚwidthÚheightÚdescentZtrashZused_charactersr   r   r   r   r,   6   s*    


z(TextToPath.get_text_width_height_descentFc             C   sÜ   |dkr|   ||¡\}}}n4|s>|  |¡}|  ||¡\}}}n|  ||¡\}}}g g  }}	xR|D ]J\}
}}}||
 \}}t|ƒr`t |¡| ||g }| |¡ |	 |¡ q`W x$|D ]\}}| |¡ |	 |¡ q´W ||	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   r.   r/   Z
glyph_infoÚ	glyph_mapÚrectsr   ZvertsÚcodesZglyph_idZ	xpositionZ	ypositionr4   Zverts1Zcodes1r   r   r   Úget_text_pathR   s"    (



zTextToPath.get_text_pathNc             C   sº   |dkrt ƒ }|rt ƒ }n|}g }g }xZt|t ||¡ƒD ]D\}\}	}
|  |t|ƒ¡}| |¡ | |
¡ ||kr:| ¡ ||< q:W 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   Zlayoutr$   ÚordÚappendÚget_pathr;   Úlist)r   r   r.   r?   Úreturn_new_glyphs_onlyÚglyph_map_newÚ
xpositionsÚ	glyph_idsÚcharÚ_Úxr&   Ú
ypositionsÚsizesr@   r   r   r   r9   ‘   s$     

zTextToPath.get_glyphs_with_fontc             C   sz  |  ¡ }| | j¡ | j || j|¡\}}}}}	|s:tƒ }|rFtƒ }
n|}
g }g }g }g }xŽ|D ]†\}}}}}|  ||¡}||kr´| ¡  | | j| j¡ |j	|t
d� | ¡ |
|< | |¡ | |¡ | |¡ || j }| |¡ q`W g }xr|	D ]j\}}}}||f||| f|| || f|| |f||fdg}tjtjtjtjtjtjg}| ||f¡ qôW t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	   rF   rE   r   ÚMOVETOÚLINETOÚ	CLOSEPOLYrG   rC   )r   r   r.   r?   rH   r5   r6   r7   Zglyphsr@   rI   rJ   rO   rK   rP   r   r0   r#   ÚoxÚoyr&   ÚsizeÚmyrectsr1   r2   Úvert1Úcode1r   r   r   r:   °   sF    



zTextToPath.get_glyphs_mathtextc             C   s$   | j dkrddlm} |ƒ | _ | j S )z5Return the cached `~.texmanager.TexManager` instance.Nr   )Ú
TexManager)r   Zmatplotlib.texmanagerr\   )r   r\   r   r   r   r*   ã   s    
zTextToPath.get_texmanagerc          	   C   s¾  |   ¡  || j¡}t || j¡�}|\}W dQ R X |dkr@tƒ }|rLtƒ }n|}g g g g f\}	}
}}xÄ|jD ]º\}}}}}|  |j	¡\}}|  
||¡}||krö| ¡  | | j| j¡ |dk	rÜ| || ¡}|j|td� n|j|td� | ¡ ||< |	 |¡ |
 |¡ | |¡ | |j| j ¡ qlW g }xv|jD ]l\}}}}||f|| |f|| || f||| f||fdg}tjtjtjtjtjtjg}| ||f¡ �q6W tt|	|
||ƒƒ||fS )z?Convert the string *s* to vertices and codes using usetex mode.N)r)   )r   r   )r*   Zmake_dvir   r   ZDvir   r   ÚtextÚ_get_ps_font_and_encodingÚtexnamer'   rQ   r   Zget_name_indexZ
load_glyphr
   rR   rF   rE   rX   Zboxesr   rS   rT   rU   rG   rC   )r   r   r.   r?   rH   ZdvifileZdviÚpagerI   rK   rJ   rO   rP   Úx1Úy1ZdvifontZglyphr5   r   Úencr&   ÚindexrY   rV   rW   r2   r1   rZ   r[   r   r   r   r8   ê   sD    


zTextToPath.get_glyphs_texé2   c          
   C   sª   t  t  d¡¡}||  }|jd kr:td|j› d| › d�ƒ‚t|jƒ}|jrXt  |j¡}nJxDdD ].}y| 	|¡ W n tt
fk
rˆ   Y q^X P q^W 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   Z
PsfontsMapZfind_tex_fileÚfilenameÚ
ValueErrorZpsnamer   ÚencodingZ
_parse_encZselect_charmapÚRuntimeErrorÚ_logÚwarning)r_   Ztex_font_mapZpsfontr   rc   Zcharmap_coder   r   r   r^      s$    


z$TextToPath._get_ps_font_and_encoding)F)NF)NF)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r$   r'   r,   rB   r9   r:   r*   r8   ÚstaticmethodÚ	functoolsÚ	lru_cacher^   r   r   r   r   r      s&   	
? 
 
2 
5r   c               @   sJ   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k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)r/   Fzpath.simplify_threshold)Zmatplotlib.textru   r   Z	_from_anyr+   Ú_xyr   Ú_cached_verticesZ_preprocess_mathÚtext_to_pathrB   Ú	_verticesÚ_codesZ_should_simplifyr   Z_simplify_thresholdÚ_interpolation_steps)	r   Zxyr.   rX   r   r|   rv   ru   r/   r   r   r   r   U  s    -


zTextPath.__init__c             C   s   || _ d| _dS )zSet the text size.TN)Ú_sizeÚ_invalid)r   rX   r   r   r   r   “  s    zTextPath.set_sizec             C   s   | 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_pathrx   )r   r   r   r   Úverticesœ  s    zTextPath.verticesc             C   s   | j S )z"
        Return the codes
        )r{   )r   r   r   r   rA   ¤  s    zTextPath.codesc             C   sD   | j s| jdkr@tƒ  | jtj ¡j| 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~   rx   r   r4   r}   ry   r   Ú	translaterw   Z	transformrz   )r   Útrr   r   r   r€   «  s    zTextPath._revalidate_path)NNrt   F)rl   rm   rn   ro   r   r   r   Úpropertyr�   rA   r€   r   r   r   r   rs   P  s    
=rs   )Úcollectionsr   rq   ÚloggingÚurllib.parser   Znumpyr<   Z
matplotlibr   r   r   r   Zmatplotlib.font_managerr   r   Zmatplotlib.ft2fontr	   r
   Zmatplotlib.mathtextr   Zmatplotlib.pathr   Zmatplotlib.transformsr   Ú	getLoggerrl   rj   r   ry   rs   r   r   r   r   Ú<module>   s    
  =