3
$ßL]F  ã               @   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mZ d dlmZ d dlmZ d dlmZ ejeƒZG d	d
„ d
eƒZeƒ ZG dd„ deƒZ dS )é    )ÚOrderedDictN)ÚcbookÚdvireadÚfont_managerÚrcParams)ÚFontPropertiesÚget_font)ÚKERNING_DEFAULTÚLOAD_NO_HINTINGÚLOAD_TARGET_LIGHT)ÚMathTextParser)ÚPath)ÚAffine2Dc               @   sÈ   e Zd ZdZdZdZdd„ Zeej	dƒdd„ ƒƒZ
d	d
„ Zdd„ Zdd„ Zdd„ Zej	ddd�d)dd„ƒZdd„ Zejddƒd*dd„ƒZd+dd„Zd,d d!„Zd"d#„ Zd-d$d%„Zeej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   ú;/tmp/pip-install-edjban26/matplotlib/matplotlib/textpath.pyÚ__init__   s    
zTextToPath.__init__z3.0c             C   s   t jt jdƒƒS )Nz
pdftex.map)r   Ú
PsfontsMapÚfind_tex_file)r   r   r   r   Útex_font_map   s    zTextToPath.tex_font_mapc             C   s&   t j|ƒ}t|ƒ}|j| 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jdj|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$   |j ƒ d }tj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_ps4   s    zTextToPath._get_char_id_psz3.1z6font.get_path() and manual translation of the vertices)Úalternativeç        c             C   s4   |j ƒ \}}|dkr,|dd…df  |7  < ||fS )z?Convert the *font*'s current glyph to a (vertices, codes) pair.g        Nr   )Úget_path)r   r    ÚcurrxÚvertsÚcodesr   r   r   Úglyph_to_path<   s    zTextToPath.glyph_to_pathc             C   sî   t d r8| jƒ }|jƒ }|j||d d�\}}}|||fS |jƒ }|| j }	|r’|jƒ }|j| jƒ | jj|d|ƒ\}
}}}}|
|	 ||	 ||	 fS | j	|ƒ}|j
|dtd� |jƒ \}}|d }|d }|jƒ }|d }||	 ||	 ||	 fS )Nztext.usetex)Zrendererr   g        )Úflagsg      P@)r   Ú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   r6   F   s*    


z(TextToPath.get_text_width_height_descentÚusetexFc             C   sä   |rd}|dkr$| j ||ƒ\}}}n4|sF| j|ƒ}| j||ƒ\}}}n| j||ƒ\}}}g g  }	}
xR|D ]J\}}}}|| \}}t|ƒrhtj|ƒ| ||g }|	j|ƒ |
j|ƒ qhW x$|D ]\}}|	j|ƒ |
j|ƒ 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` instance
            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 renderering.

        usetex : bool, optional
            If set, forces *ismath* to True.  This parameter is deprecated.

        Returns
        -------

        verts, codes : tuple of lists
            *verts*  is a list of numpy arrays containing the x and y
            coordinates of the vertices. *codes* is 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.
        ÚTeX)Úget_glyphs_texr!   Úget_glyphs_with_fontÚget_glyphs_mathtextÚlenÚnpÚarrayÚextend)r   r   r8   r9   rB   Z
glyph_infoÚ	glyph_mapÚrectsr    r0   r1   Zglyph_idZ	xpositionZ	ypositionr>   Zverts1Zcodes1r   r   r   Úget_text_pathb   s&    ,



zTextToPath.get_text_pathNc             C   s  d}d}g }g }|dkrt ƒ }|r*t ƒ }	n|}	x²|D ]ª}
t|
ƒ}|j|ƒ}|dkr^tdƒ}d}|dk	rv|j||tƒ}nd}|j|td�}|jd }| j||ƒ}||kr²|j	ƒ |	|< ||d 7 }|j
|ƒ |j
|ƒ ||7 }|}q4W 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   ú?)r3   i   é@   g      ð?)r   ÚordZget_char_indexZget_kerningr	   Ú	load_charr
   ZlinearHoriAdvancer(   r.   ÚappendrG   ÚlistÚzip)r   r    r8   rK   Úreturn_new_glyphs_onlyZlastgindr/   Ú
xpositionsÚ	glyph_idsÚglyph_map_newÚcr'   ZgindÚkernÚglyphZhoriz_advancer*   Ú
ypositionsÚsizesrL   r   r   r   rE   §   sB    




zTextToPath.get_glyphs_with_fontc             C   sz  |j ƒ }|j| jƒ | jj|| j|ƒ\}}}}}	|s:tƒ }|rFtƒ }
n|}
g }g }g }g }xŽ|D ]†\}}}}}| j||ƒ}||kr´|jƒ  |j| j| jƒ |j	|t
d�}|jƒ |
|< |j|ƒ |j|ƒ |j|ƒ || j }|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}|j||fƒ qôW tt||||ƒƒ|
|fS )zW
        Parse mathtext string *s* and convert it to a (vertices, codes) pair.
        )r3   r   )r   r   )r7   r   r   r   r$   r   r   r(   ÚclearrQ   r
   r.   rR   r   ÚMOVETOÚLINETOÚ	CLOSEPOLYrS   rT   )r   r   r8   rK   rU   r?   r@   rA   ZglyphsrL   rX   rV   r\   rW   r]   r    r:   r'   ÚoxÚoyr*   r[   ÚsizeÚmyrectsr;   r<   Úvert1Úcode1r   r   r   rF   ã   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.texmanagerrh   )r   rh   r   r   r   r4     s    
zTextToPath.get_texmanagerc             C   s¾  | j ƒ j|| jƒ}tj|| jƒ�}|\}W dQ R X |dkr@tƒ }|rLtƒ }n|}g g g g f\}	}
}}xÄ|jD ]º\}}}}}| j|j	ƒ\}}| j
||ƒ}||krö|jƒ  |j| j| jƒ |dk	rÜ|j|| ƒ}|j|td� n|j|td� |jƒ ||< |	j|ƒ |
j|ƒ |j|ƒ |j|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}|j||fƒ �q6W tt|	|
||ƒƒ||fS )z?Convert the string *s* to vertices and codes using usetex mode.N)r3   r   )r   r   )r4   Zmake_dvir   r   ZDvir   r   ÚtextÚ_get_ps_font_and_encodingÚtexnamer+   r^   r   Zget_name_indexZ
load_glyphr   rQ   r.   rR   rd   Zboxesr   r_   r`   ra   rS   rT   )r   r   r8   rK   rU   ZdvifileZdviÚpagerX   rW   rV   r\   r]   Úx1Úy1Zdvifontr[   r?   r    Úencr*   Úindexre   rb   rc   r<   r;   rf   rg   r   r   r   rD     sD    


zTextToPath.get_glyphs_texé2   c             C   s²   t jt jdƒƒ}||  }|jd kr:td|j› d| › d�ƒ‚t|jƒ}|jrXt j|jƒ}nRxLdD ]2\}}y|j	|ƒ W n tt
fk
rŒ   Y q^X P q^W d	}tjd
|jƒ d }||fS )Nz
pdftex.mapzNo usable font file found for z (z&). The font may lack a Type-1 version.ÚADOBE_CUSTOMéCBDAÚADOBE_STANDARDéBODAÚ z#No supported encoding in font (%s).©rr   rs   ©rt   ru   )rw   rx   )r   r   r   ÚfilenameÚ
ValueErrorZpsnamer   ÚencodingZ
_parse_encZselect_charmapÚRuntimeErrorÚ_logÚwarning)rk   r   Z
font_bunchr    ro   Zcharmap_nameZcharmap_coder   r   r   rj   S  s*    

 z$TextToPath._get_ps_font_and_encoding)r-   )FF)NF)NF)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úpropertyr   Ú
deprecatedr   r!   r"   r(   r+   r2   r6   Z_delete_parameterrM   rE   rF   r4   rD   ÚstaticmethodÚ	functoolsÚ	lru_cacherj   r   r   r   r   r      s4   	
D 
; 
2 
5r   c               @   sr   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
ejdƒdd„ ƒZejdd d�dd„ ƒZdS )ÚTextPathz&
    Create a path from the text.
    Né   Fc             O   sœ   ddl m}	 |s|r"tjddd� |dkr0tƒ }|dkr@|jƒ }|| _| j|ƒ d| _|	|d�j	|ƒ\}}
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 : integer, optional
            (Currently ignored)

        usetex : bool, optional
            Whether to use tex rendering. Defaults to ``False``.

        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   )ÚTextz3.1z^Additional arguments to TextPath used to be ignored, but will trigger a TypeError %(removal)s.)ÚmessageN)rB   )r9   Fzpath.simplify_threshold)Zmatplotlib.textrŠ   r   Zwarn_deprecatedr   r5   Ú_xyr   Ú_cached_verticesZ_preprocess_mathÚtext_to_pathrM   Ú	_verticesÚ_codesZ_should_simplifyr   Z_simplify_thresholdÚ_interpolation_steps)r   Zxyr8   rd   r   r‘   rB   ÚargsÚkwargsrŠ   r9   r   r   r   r   ‰  s"    0


zTextPath.__init__c             C   s   || _ d| _dS )zSet the text size.TN)Ú_sizeÚ_invalid)r   rd   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 ƒ  | 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
        )r�   )r   r   r   r   r1   á  s    zTextPath.codesc             C   sN   | j s| jdkrJtƒ j| jtj | jtj ƒ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•   r�   r   r>   r”   rŽ   r   Ú	translaterŒ   Z	transformr�   )r   Útrr   r   r   r—   è  s    
zTextPath._revalidate_pathz3.1c             C   sX   |j dƒ|j dƒ }|dko&|d dk}td r8|dfS |rD|dfS |jddƒdfS d	S )
zM
        Returns True if the given string *s* contains any mathtext.
        ú$z\$r   r)   ztext.usetexrC   TFN)Úcountr   Úreplace)r   r8   Zdollar_countZeven_dollarsr   r   r   Úis_math_textö  s    	zTextPath.is_math_text)r,   c             C   s6   |rt j||dd�S | j|ƒ\}}t j|||d�S dS )zd
        Convert string *s* to a (vertices, codes) pair using font property
        *prop*.
        T)rB   )r9   N)rŽ   rM   rž   )r   r   r8   rB   Z
clean_liner9   r   r   r   Útext_get_vertices_codes
  s    z TextPath.text_get_vertices_codes)NNr‰   F)r   r€   r�   r‚   r   r   r–   rƒ   r˜   r1   r—   r   r„   rž   rŸ   r   r   r   r   rˆ   „  s    
Frˆ   )!Úcollectionsr   r†   ÚloggingÚurllib.parser#   ZnumpyrH   Z
matplotlibr   r   r   r   Zmatplotlib.font_managerr   r   Zmatplotlib.ft2fontr	   r
   r   Zmatplotlib.mathtextr   Zmatplotlib.pathr   Zmatplotlib.transformsr   Ú	getLoggerr   r}   Úobjectr   rŽ   rˆ   r   r   r   r   Ú<module>   s    
  p