3
ƽh:                 @   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j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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   9/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/textpath.py__init__   s    
zTextToPath.__init__c             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_ps.   s    zTextToPath._get_char_id_psc             C   s   |dk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 )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| j ||\}}}n4|s>| j|}| j||\}}}n| j||\}}}g g  }}	xR|D ]J\}
}}}||
 \}}t|r`tj|| ||g }|j| |	j| q`W x$|D ]\}}|j| |	j| 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   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j||D ]D\}\}	}
| j|t|}|j| |j|
 ||kr:|j ||< 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 | 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 qW 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   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  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)r)   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_tex2   c             C   s   t jt jd}||  }|jd kr:td|j d|  dt|j}|jrXt j|j}nJxDdD ].}y|j	| W n tt
fk
r   Y q^X P q^W 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.CBDABODAz#No supported encoding in font (%s).)rf   rg   )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j|}|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 : 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.textrw   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~   rx   rw   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   | jS )zH
        Return the cached path after updating it if necessary.
        )_revalidate_pathrz   )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| 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   rz   r   r4   r   r{   r   	translatery   Z	transformr|   )r   trr   r   r   r     s    zTextPath._revalidate_path)NNrv   F)rn   ro   rp   rq   r   r   r   propertyr   rA   r   r   r   r   r   ru   P  s    
=ru   )collectionsr   rs   loggingurllib.parser   numpyr<   Z
matplotlibr   r   r   r   Zmatplotlib.font_managerr   r   Zmatplotlib.ft2fontr	   r
   Zmatplotlib.mathtextr   Zmatplotlib.pathr   Zmatplotlib.transformsr   	getLoggerrn   rl   r   r{   ru   r   r   r   r   <module>   s    
  =