3
6Ad]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   ú~/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/matplotlib-3.1.1-py3.6-linux-x86_64.egg/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   Ú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ÚformatÚ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)Ú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)Úrendererr   g        )Úflagsg      P@)r   Ú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ÚdescentZtrashZused_charactersr!   r   r   r   r:   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   r?   r@   rJ   Z
glyph_infoÚ	glyph_mapÚrectsr!   r3   r4   Zglyph_idZ	xpositionZ	ypositionrF   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   ú?)r7   i   é@   g      ð?)r   ÚordÚget_char_indexÚget_kerningr	   Ú	load_charr
   ÚlinearHoriAdvancer*   r1   ÚappendrO   ÚlistÚzip)r   r!   r?   rS   Úreturn_new_glyphs_onlyZlastgindr2   Ú
xpositionsÚ	glyph_idsÚglyph_map_newÚcr)   ZgindZkernÚglyphZhoriz_advancer-   Ú
ypositionsÚsizesrT   r   r   r   rM   §   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.
        )r7   r   )r   r   )r;   r   r   r   r%   r   r   r*   Úclearr[   r
   r1   r]   r   ÚMOVETOÚLINETOÚ	CLOSEPOLYr^   r_   )r   r   r?   rS   r`   rG   rH   rI   ZglyphsrT   rc   ra   rf   rb   rg   r!   rB   r)   ÚoxÚoyr-   re   ÚsizeÚmyrectsrC   rD   Úvert1Úcode1r   r   r   rN   ã   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   Úmatplotlib.texmanagerrr   )r   rr   r   r   r   r8     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)r7   r   )r   r   )r8   Zmake_dvir   r   ZDvir   r   ÚtextÚ_get_ps_font_and_encodingÚtexnamer.   rh   r   Úget_name_indexÚ
load_glyphr   r[   r1   r]   rn   Zboxesr   ri   rj   rk   r^   r_   )r   r   r?   rS   r`   ZdvifileZdviZpagerc   rb   ra   rf   rg   Úx1Úy1Zdvifontre   rG   r!   Úencr-   Úindexro   rl   rm   rD   rC   rp   rq   r   r   r   rL     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).©r~   r   ©r€   r�   )rƒ   r„   )r   r   r   ÚfilenameÚ
ValueErrorZpsnamer   ÚencodingZ
_parse_encÚselect_charmapÚRuntimeErrorÚ_logÚwarning)rv   r   Z
font_bunchr!   r{   Zcharmap_nameZcharmap_coder   r   r   ru   S  s*    

 z$TextToPath._get_ps_font_and_encoding)r0   )FF)NF)NF)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úpropertyr   Ú
deprecatedr   r"   r#   r*   r.   r5   r:   Ú_delete_parameterrU   rM   rN   r8   rL   ÚstaticmethodÚ	functoolsÚ	lru_cacheru   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)rJ   )r@   Fzpath.simplify_threshold)Úmatplotlib.textr˜   r   Úwarn_deprecatedr   r9   Ú_xyr   Ú_cached_verticesÚ_preprocess_mathÚtext_to_pathrU   Ú	_verticesÚ_codesÚ_should_simplifyr   Ú_simplify_thresholdÚ_interpolation_steps)r   Úxyr?   rn   r   r¤   rJ   ÚargsÚkwargsr˜   r@   r   r   r   r   ‰  s"    0


zTextPath.__init__c             C   s   || _ d| _dS )zSet the text size.TN)Ú_sizeÚ_invalid)r   rn   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   r4   á  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   rF   r¨   rŸ   r   Ú	translaterœ   Ú	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.usetexrK   TFN)Úcountr   Úreplace)r   r?   Údollar_countÚ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)rJ   )r@   N)rŸ   rU   rµ   )r   r   r?   rJ   Ú
clean_liner@   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¬   r4   r«   r   r‘   rµ   r·   r   r   r   r   r–   „  s    
Fr–   )!Úcollectionsr   r”   ÚloggingÚurllib.parser$   ÚnumpyrP   Ú
matplotlibr   r   r   r   Úmatplotlib.font_managerr   r   Úmatplotlib.ft2fontr	   r
   r   Úmatplotlib.mathtextr   Úmatplotlib.pathr   Úmatplotlib.transformsr   Ú	getLoggerrŒ   rŠ   Úobjectr   rŸ   r–   r   r   r   r   Ú<module>   s    
  p