
    G8c4                         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j6                  e      Z G d
 d      Z e       Z G d de      Z y)    )OrderedDictN)_api_text_helpersdviread)FontPropertiesget_fontfontManager)LOAD_NO_HINTINGLOAD_TARGET_LIGHT)MathTextParser)Path)
TexManager)Affine2Dc                       e Zd ZdZdZdZd Zd Zd Zd Z	d Z
dd	Z	 	 ddZ	 	 ddZ ej                  dd      d        Z	 	 ddZed        Zy
)
TextToPathz'A class that converts strings to paths.g      Y@H   c                 2    t        d      | _        d | _        y )Npath)r   mathtext_parser_texmanagerselfs    5/usr/lib/python3/dist-packages/matplotlib/textpath.py__init__zTextToPath.__init__   s    -f5    c                     t        j                  |      }t        |      }|j                  | j                  | j
                         |S )zX
        Find the `FT2Font` matching font properties *prop*, with its size set.
        )_fontManager_find_fonts_by_propsr   set_size
FONT_SCALEDPI)r   prop	filenamesfonts       r   	_get_fontzTextToPath._get_font   s9     !55d;		"dootxx0r   c                     t         S )N)r
   r   s    r   _get_hinting_flagzTextToPath._get_hinting_flag'   s    r   c                 `    t         j                  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$   ccodes      r   _get_char_idzTextToPath._get_char_id*   s-     ||!!T%9%9$:!E!9"EFFr   c                    |j                         }|dk(  rt               j                  ||      S || j                  z  }|rZ|j	                         }|j                  | j                         | j                  j                  |d|      ^}}}}	||z  ||z  ||z  fS | j                  |      }
|
j                  |dt               |
j                         \  }}|dz  }|dz  }|
j                         }|dz  }||z  ||z  ||z  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ds                 r   r6   z(TextToPath.get_text_width_height_descent0   s
   **,U?<==aJJ4??*99;DMM$//*$$**1b$7 'E67Q5=&5.'E/AA~~d#aO4$$&1	T		T		T	5y!e)QY..r   c                    |dk(  r| j                  ||      \  }}}n@|s(| j                  |      }| j                  ||      \  }}}n| j                  ||      \  }}}g g }	}|D ]9  \  }
}}}||
   \  }}|j	                  ||z  ||gz          |	j	                  |       ; |D ]'  \  }}|j	                  |       |	j	                  |       ) |st        j                  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.
        r2   )r      )get_glyphs_texr%   get_glyphs_with_fontget_glyphs_mathtextextendnpempty)r   r"   r;   r<   
glyph_info	glyph_maprectsr$   vertscodesglyph_id	xposition	ypositionr>   verts1codes1s                   r   get_text_pathzTextToPath.get_text_pathH   s   P U?+/+>+>tQ+G(J	5>>$'D+/+D+DT1+M(J	5+/+C+CD!+L(J	52u5? 	!1HiE&x0NFFLL%9i*@@ALL 	! $ 	!NFFLL LL 	! HHV$Ee|r   Nc                    |
t               }|rt               }n|}g }g }t        j                  ||      D ]  }| j                  |j                  t        |j                              }	|j                  |	       |j                  |j                         |	|vsc|j                  j                         ||	<    dgt        |      z  }
dgt        |      z  }g }t        t        |||
|            ||fS )zW
        Convert string *s* to vertices and codes using the provided ttf font.
        r   g      ?)r   r   layoutr0   	ft_objectordcharappendr*   get_pathlenlistzip)r   r$   r;   rO   return_new_glyphs_onlyglyph_map_new
xpositions	glyph_idsitemchar_id
ypositionssizesrP   s                r   rI   zTextToPath.get_glyphs_with_font   s     #I!'MM%M
	!((D1 	CD''DIIGGW%dff%i')-)@)@)Bg&	C S3z?*
s:&SJ
EBCu& 	&r   c                    |j                         }|j                  | j                         | j                  j	                  || j
                  |      \  }}}}}	|s
t               }|rt               }
n|}
g }g }g }g }|D ]  \  }}}}}| j                  ||      }||vr`|j                          |j                  | j                  | j
                         |j                  |t               |j                         |
|<   |j                  |       |j                  |       |j                  |       || j                  z  }|j                  |        g }|	D ]  \  }}}}||f|||z   f||z   ||z   f||z   |f||fdg}t        j                  t        j                  t        j                  t        j                  t        j                  t        j                   g}|j                  ||f        t#        t%        ||||            |
|fS )zW
        Parse mathtext string *s* and convert it to a (vertices, codes) pair.
        r3   r   r   )r7   r   r    r   r,   r!   r   r0   clear	load_charr
   r_   r^   r   MOVETOLINETO	CLOSEPOLYra   rb   )r   r"   r;   rO   rc   r?   r@   rA   glyphsrP   rd   re   ri   rf   rj   r$   r=   r/   oxoyrh   sizemyrectsrC   rD   vert1code1s                              r   rJ   zTextToPath.get_glyphs_mathtext   s    yy{doo&040D0D0J0Jtxx1-vw #I!'MM%M

	-3 	)D(E2r''e4Gi'

dootxx8uO<)-g&b!b!W%doo-DLL	 ! 	+LBAq"XBF|b1fb1f-=1fb\B8V5E[[[[$++t{{DKK^^%E NNE5>*	+ SJ
EBCw( 	(r   z3.6zTexManager())alternativec                 P    | j                   t               | _         | j                   S )z5Return the cached `~.texmanager.TexManager` instance.)r   r   r   s    r   get_texmanagerzTextToPath.get_texmanager   s&     #)|Dr   c                    t               j                  || j                        }t        j                  || j
                        5 }|\  }ddd       |
t               }|rt               }n|}g g g g f\  }	}
}}j                  D ]|  }t        |j                        }| j                  ||j                        }||vr|j                          |j                  | j                  | j
                         |j                  }t        |t               r)|j#                  |      }|j%                  |t&               nGt        |t(              r)| j+                  |       |j-                  |t&               nt/        d|      |j1                         ||<   |	j3                  |       |
j3                  |j4                         |j3                  |j6                         |j3                  |j8                  | j                  z          g }|j:                  D ]  \  }}}}||f||z   |f||z   ||z   f|||z   f||fdg}t<        j>                  t<        j@                  t<        j@                  t<        j@                  t<        j@                  t<        jB                  g}|j3                  ||f        tE        tG        |	|
||            ||fS # 1 sw Y   {xY w)z?Convert the string *s* to vertices and codes using usetex mode.Nr3   zGlyph spec of unexpected type: rl   )$r   make_dvir    r   Dvir!   r   textr   	font_pathr0   glyphrm   r   glyph_name_or_index
isinstancestrget_name_index
load_glyphr   int_select_native_charmaprn   	TypeErrorr_   r^   r*   y	font_sizeboxesr   ro   rp   rq   ra   rb   )r   r"   r;   rO   rc   dvifiledvipagerd   rf   re   ri   rj   r   r$   rh   r   indexrv   rs   rt   rD   rC   rw   rx   s                            r   rH   zTextToPath.get_glyphs_tex   s   
 ,''4??;[[$((+ 	sED	 #I!'MM%M35r2r>0	:z5 II 	;DDNN+D''djj9Gi'

dootxx8&*&>&>#137 //0CDEOOE1BOC 3S9//5NN+3D # F $&E':&=%? @ @)-g&W%dff%dff%LL$//9:-	;0  JJ 	+LBAq"XQ|b1fb1f-="q&\B8V5E[[[[$++t{{DKK^^%E NNE5>*	+ SJ
EBCw( 	(c	 	s   KKc                     dD ]  }	 | j                  |        y  t        j	                  d| j
                         y # t        t        f$ r Y Jw xY w)N)iCBDAiBODAz#No supported encoding in font (%s).)select_charmap
ValueErrorRuntimeError_logwarningfname)r$   charmap_codes     r   r   z!TextToPath._select_native_charmap  s[    
 	LL##L1 	L LL>

K - s   <AA)F)NF)__name__
__module____qualname____doc__r    r!   r   r%   r'   r0   r6   rX   rI   rJ   r   
deprecatedr{   rH   staticmethodr    r   r   r   r      s    1J
C G/0>@ 7;49&> 6:381(f T__U7  8  15.38(t L Lr   r   c                   Z     e Zd ZdZ	 	 d fd	Zd Zd Zed        Zed        Z	d Z
 xZS )	TextPathz&
    Create a path from the text.
    c                 .   ddl m} t        j                  |      }||j	                         }|| _        | j                  |       d| _         ||      j                  |      \  }}t        	| (  t        j                  |||      |dd d| _        y)	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)r<   T)_interpolation_stepsreadonlyF)matplotlib.textr   r   	_from_anyr5   _xyr   _cached_vertices_preprocess_mathsuperr   text_to_pathrX   _should_simplify)
r   xyr;   ru   r"   r   r   r   r<   	__class__s
            r   r   zTextPath.__init__5  s    Z 	)''-<**,Dd $'88;	6''a'?!5	 !&r   c                      || _         d| _        y)zSet the text size.TN)_size_invalid)r   ru   s     r   r   zTextPath.set_sizes  s    
r   c                     | j                   S )zGet the text size.)r   r   s    r   get_sizezTextPath.get_sizex  s    zzr   c                 :    | j                          | j                  S )zH
        Return the cached path after updating it if necessary.
        )_revalidate_pathr   r   s    r   verticeszTextPath.vertices|  s    
 	$$$r   c                     | j                   S )z"
        Return the codes
        )_codesr   s    r   rR   zTextPath.codes  s    
 {{r   c                 P   | j                   s| j                   t               j                  | j                  t
        j                  z        j                  | j                   }|j                  | j                        | _        d| j                  j                  _        d| _         yy)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   	transform	_verticesr4   	writeable)r   trs     r   r   zTextPath._revalidate_path  s     ==D119(*5l&=&==>9dhh(B %'LL$@D!49D!!''1!DM :r   )NN   F)r   r   r   r   r   r   r   propertyr   rR   r   __classcell__)r   s   @r   r   r   0  sM     /305<&|
 % %  "r   r   )!collectionsr   loggingurllib.parser+   numpyrL   
matplotlibr   r   r   matplotlib.font_managerr   r   r	   r   matplotlib.ft2fontr
   r   matplotlib.mathtextr   matplotlib.pathr   matplotlib.texmanagerr   matplotlib.transformsr   	getLoggerr   r   r   r   r   r   r   r   <module>r      sk    #    3 3  B .   , *w"VL VLr |h"t h"r   