
 m[c           @` sm  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l m Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z m Z d d l m Z m Z m Z m Z d d l  j! Z! d d l" Z" e j# e$  Z% e j& j' d	  d
    Z( d e) f d     YZ* d S(   sA  
This module supports embedded TeX expressions in matplotlib via dvipng
and dvips for the raster and postscript backends.  The tex and
dvipng/dvips information is cached in ~/.matplotlib/tex.cache for reuse between
sessions

Requirements:

* latex
* \*Agg backends: dvipng>=1.6
* PS backend: psfrag, dvips, and Ghostscript>=8.60

Backends:

* \*Agg
* PS
* PDF

For raster output, you can get RGBA numpy arrays from TeX expressions
as follows::

  texmanager = TexManager()
  s = ('\TeX\ is Number '
       '$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!')
  Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1,0,0))

To enable tex rendering of all text in your matplotlib figure, set
text.usetex in your matplotlibrc file or include these two lines in
your script::

  from matplotlib import rc
  rc('text', usetex=True)

i    (   t   absolute_importt   divisiont   print_functionN(   t   md5(   t   rcParams(   t   read_png(   t   mkdirst   Locked(   t
   subprocesst   Popent   PIPEt   STDOUTs   2.1c          C` s  yO t  t d  d g d t d t d t d t j d k }  |  j   \ } } Wn t k
 rp t j	 d  t
 SX| j t j    j d	  } xe | D]] } | j d
  r | j   d } t j	 d |  t j j |  } | t j j d  k  Sq Wt j	 d  t
 S(   Nt   dvipngs   -versiont   stdint   stdoutt   stderrt	   close_fdst   win32s   No dvipng was founds   
s   dvipng is   Found dvipng version %ss   1.6s(   Unexpected response from dvipng -version(   R	   t   strR
   R   t   syst   platformt   communicatet   OSErrort   _logt   infot   Falset   decodet   getdefaultencodingt   splitt
   startswitht	   distutilst   versiont   LooseVersion(   t   pR   R   t   linest   lineR   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   dvipng_hack_alpha?   s     !t
   TexManagerc           B` s?  e  Z d  Z e j   Z e d@ k	 rF e j j	 e d  Z
 e e
  n d@ Z
 i  Z i  Z e e j j d  d     Z e e j j d  d     Z dA Z dB Z dC Z dD Z d Z dE Z i dF d 6dG d 6dH d 6dI d 6dJ d 6dK d 6dL d 6dM d 6dN d 6dO d! 6dP d$ 6dQ d' 6dR d 6dS d( 6dT d) 6dU d* 6Z d@ Z dV e g  dW e D] Z d- e ^ qK Z d.   Z d@ d/  Z d0   Z  d1   Z! d2   Z" d3   Z# e$ j% d4  Z& d5   Z' d6   Z( d7   Z) d8   Z* d9   Z+ e j j d  d:    Z, e j j d  d;    Z- d@ d@ d<  Z. d@ d@ dX d>  Z/ d@ d?  Z0 RS(Y   sU   
    Convert strings to dvi files using TeX, caching the results to a directory.
    s	   tex.caches   2.2c         C` s   i  S(   N(    (   t   self(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   <lambda>d   s    c         C` s   d S(   Ni    (    (   R&   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyR'   e   s    t   cmrt    t   cmsst   cmttt   pzcs   \usepackage{chancery}t   serifs
   sans-serift   cursivet	   monospacet   pncs   \renewcommand{\rmdefault}{pnc}s   new century schoolbookt   pbks   \renewcommand{\rmdefault}{pbk}t   bookmant   ptms   \usepackage{mathptmx}t   timest   ppls   \usepackage{mathpazo}t   palatinos   zapf chanceryt   pchs   \usepackage{charter}t   chartert   phvs   \usepackage{helvet}t	   helveticat   pags   \usepackage{avant}s   avant gardet   pcrs   \usepackage{courier}t   couriers   computer modern romans   computer modern sans serifs   computer modern typewriters   text.latex.preamblet   familys   font.c         C` sw  |  j  d  k r t d   n  t |  j   t d } t |  d k rv | d j   |  j k rv | d j   |  _ n^ t	 | t
 j  r | j   |  j k r | j   |  _ n% t j d d j |  j   d |  _ |  j g } x |  j D] } | j d d	  } x t d
 | D]o } | j   |  j k rpt |  | |  j | j    t j d | | |  j | j    Pqt j d |  qWt j d |  t |  | |  j |  | j t |  |  d  q W|  j   j d  } | j t |  j    d j |  |  _ |  j d |  j d |  j d g } |  j d k rS| j |  j d  n  d j d g | d g  |  _ d  S(   NsB   Cannot create TexManager, as there is no cache directory availables   font.familyi   i    sX   font.family must be one of (%s) when text.usetex is True. serif will be used by default.s   , R-   t   -t   _s   font.s   family: %s, font: %s, info: %ss&   %s font is not compatible with usetex.sQ   No LaTeX-compatible font found for the %s font family in rcParams. Using default.s   utf-8R)   R.   s   
s   \usepackage{type1cm}s   \usepackage{textcomp}(   t   texcachet   Nonet   RuntimeErrorR   R   t   lent   lowert   font_familiest   font_familyt
   isinstancet   sixt   string_typesR   R   t   joint   replacet	   font_infot   setattrt   debugt   appendt   getattrt   get_custom_preamblet   encodeR   t	   hexdigestt   _fontconfigR-   t
   sans_serifR/   R.   t   _font_preamble(   R&   t   fft
   fontconfigRG   t   font_family_attrt   fontt   preamble_bytest   cmd(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   __init__   sH    
+						$c         C` se   d j  | |  j   d | |  j   t | p. d  g  } t j j  |  j t | j d   j	    S(   sU   
        Return a filename based on a hash of the string, fontsize, and dpi.
        R)   s   %fs   utf-8(
   RK   t   get_font_configRR   R   t   ost   pathRA   R   RS   RT   (   R&   t   text   fontsizet   dpit   s(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_basefile   s    !c         C` s   |  j  d k r' t j |  j  |  _  n  g  |  j D]# } t | |  j  | k r1 | ^ q1 } | r t j d |  xI | D]A } t j d | |  j  | t |  t j	 t |  |  j  | <qw Wt j d |  j
  |  j   n  t j d |  j
  |  j
 S(   s8   Reinitializes self if relevant rcParams on have changed.s   following keys changed: %ss   %-20s: %-10s -> %-10ss   RE-INIT
old fontconfig: %ss   fontconfig: %sN(   t	   _rc_cacheRB   t   dictt   fromkeyst   _rc_cache_keysR   R   RO   t   copyt   deepcopyRU   R^   (   R&   t   part   changedt   k(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyR_      s    #	c         C` s   |  j  S(   sU   
        Return a string containing font configuration for the tex preamble.
        (   RW   (   R&   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_font_preamble   s    c         C` s   d j  t d  S(   s>   Return a string containing user additions to the tex preamble.s   
s   text.latex.preamble(   RK   R   (   R&   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyRR      s    c      	   C` s  |  j  | |  } d | } |  j   } i d d 6d d 6j |  j d  } | | } t d rh d } n d	 } d
 |  j | | | | d | f } t | d  j }	 t d r |	 j | j d   n@ y |	 j | j d   Wn# t	 k
 r}
 t
 j d    n XWd QX| S(   sv   
        Generate a tex file to render the tex string at a specific font size.

        Return the file name.
        s   %s.texs   {\sffamily %s}s
   sans-serifs   {\ttfamily %s}R/   s   {\rmfamily %s}s   text.latex.unicodes.   
\usepackage{ucs}
\usepackage[utf8x]{inputenc}R)   s   
\documentclass{article}
%s
%s
%s
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
\pagestyle{empty}
\begin{document}
\fontsize{%f}{%f}%s
\end{document}
g      ?t   wbt   utf8t   asciisW   You are using unicode and latex, but have not enabled the 'text.latex.unicode' rcParam.N(   Rf   RR   t   getRG   R   RW   t   opent   writeRS   t   UnicodeEncodeErrorR   R   (   R&   Rb   Rc   t   basefilet   texfilet   custom_preamblet   fontcmdt   unicode_preambleRe   t   fht   err(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   make_tex   s*    

	

	
s3   MatplotlibBox:\(([\d.]+)pt\+([\d.]+)pt\)x([\d.]+)ptc      	   C` s  |  j  | |  } d | } |  j   } i d d 6d d 6j |  j d  } | | } t d rh d } n d	 } d
 |  j | | | | d | f } t | d  j }	 t d r |	 j | j d   n@ y |	 j | j d   Wn# t	 k
 r}
 t
 j d    n XWd QX| S(   s   
        Generate a tex file to render the tex string at a specific font size.

        It uses the preview.sty to determine the dimension (width, height,
        descent) of the output.

        Return the file name.
        s   %s.texs   {\sffamily %s}s
   sans-serifs   {\ttfamily %s}R/   s   {\rmfamily %s}s   text.latex.unicodes.   
\usepackage{ucs}
\usepackage[utf8x]{inputenc}R)   s  
\documentclass{article}
%s
%s
%s
\usepackage[active,showbox,tightpage]{preview}
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}

%% we override the default showbox as it is treated as an error and makes
%% the exit status not zero
\def\showbox#1%%
{\immediate\write16{MatplotlibBox:(\the\ht#1+\the\dp#1)x\the\wd#1}}

\begin{document}
\begin{preview}
{\fontsize{%f}{%f}%s}
\end{preview}
\end{document}
g      ?Rq   Rr   Rs   sW   You are using unicode and latex, but have not enabled the 'text.latex.unicode' rcParam.N(   Rf   RR   Rt   RG   R   RW   Ru   Rv   RS   Rw   R   R   (   R&   Rb   Rc   Rx   Ry   Rz   R{   R|   Re   R}   R~   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   make_tex_preview  s*    	

	

	
c      	   C` s   t  j |  y% t j | d |  j d t j } WnS t j k
 r } t d j d | d d | j	 d  d | j
 j d	     n Xt  j |  | S(
   Nt   cwdR   sr   {prog} was not able to process the following string:
{tex!r}

Here is the full report generated by {prog}:
{exc}

t   progi    Rb   t   unicode_escapet   excs   utf-8(   R   RO   R   t   check_outputRA   R   t   CalledProcessErrorRC   t   formatRS   t   outputR   (   R&   t   commandRb   t   reportR   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   _run_checked_subprocessJ  s    		
c      	   C` s   t  d r |  j | |  S|  j | |  } d | } t j j |  s |  j | |  } t |  j  ! |  j	 d d d | g |  Wd QXxU t
 j
 | d  D]= } | j d
  s y t j |  Wq t k
 r q Xq q Wn  | S(   sm   
        Generate a dvi file containing latex's layout of tex string.

        Return the file name.
        s   text.latex.previews   %s.dvit   latexs   -interaction=nonstopmodes   --halt-on-errorNt   *t   dviRb   (   R   Rb   (   R   t   make_dvi_previewRf   R`   Ra   t   existsR   R   RA   R   t   globt   endswitht   removeR   (   R&   Rb   Rc   Rx   t   dvifileRy   t   fname(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   make_dvi\  s"    

	c         C` s+  |  j  | |  } d | } d | } t j j |  sL t j j |  r'|  j | |  } |  j d d d | g |  } t j j | j	 d   } t
 | d d  # }	 |	 j d	 j | j     Wd
 QXxU t j | d  D]= }
 |
 j d  s y t j |
  Wq t k
 rq Xq q Wn  | S(   s   
        Generate a dvi file containing latex's layout of tex string.

        It calls make_tex_preview() method and store the size information
        (width, height, descent) in a separate file.

        Return the file name.
        s   %s.dvis   %s.baselineR   s   -interaction=nonstopmodes   --halt-on-errors   utf-8s	   .baselinet   wt    NR   R   Rb   t   baseline(   R   Rb   R   (   Rf   R`   Ra   R   R   R   R%   t   _re_vboxt   searchR   Ru   Rv   RK   t   groupsR   R   R   R   (   R&   Rb   Rc   Rx   R   t   baselinefileRy   R   t   mR}   R   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyR   w  s$    	

&	"c         C` s~   |  j  | | |  } d | } t j j |  sz |  j | |  } |  j d d d d t |  d d d | | g
 |  n  | S(	   sp   
        Generate a png file containing latex's rendering of tex string.

        Return the file name.
        s   %s.pngR   s   -bgt   Transparents   -Ds   -Tt   tights   -o(   Rf   R`   Ra   R   R   R   R   (   R&   Rb   Rc   Rd   Rx   t   pngfileR   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   make_png  s    
c         C` si   |  j  | |  } d | } t j j |  se |  j | |  } |  j d d d d | | g |  n  | S(   sw   
        Generate a postscript file containing latex's rendering of tex string.

        Return the file name.
        s   %s.epsft   dvipss   -qs   -Es   -o(   Rf   R`   Ra   R   R   R   (   R&   Rb   Rc   Rx   t   psfileR   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   make_ps  s    
c      	   C` s   |  j  | |  } t |  N } xD | D]< } | j d  r( g  | j   d D] } t |  ^ qN Sq( WWd QXt d |   d S(   sU   
        Return a list of PS bboxes for latex's rendering of the tex string.
        s   %%BoundingBox:i   Ns   Could not parse %s(   R   Ru   R   R   t   intRC   (   R&   Rb   Rc   R   t   psR#   t   val(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_ps_bbox  s    1c         C` s   | |  j    | | f } |  j j |  } | d k r |  j | | |  } t t j j |  j	 |   } | d d  d d  d f |  j | <} n  | S(   s   Return the alpha channel.Ni(
   R_   t   grey_arraydRt   RB   R   R   R`   Ra   RK   RA   (   R&   Rb   Rc   Rd   t   keyt   alphaR   t   X(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_grey  s    -i    c         C` s   | s t  d } n  | s& t  d } n  | \ } } } | |  j   | | t |  f } |  j j |  }	 |	 d k r |  j | | |  }
 t j | | | |
 g  }	 |	 |  j | <n  |	 S(   s<   Return latex's rendering of the tex string as an rgba array.s	   font.sizes   savefig.dpiN(	   R   R_   t   tuplet   rgba_arraydRt   RB   R   t   npt   dstack(   R&   Rb   Rc   Rd   t   rgbt   rt   gt   bR   t   ZR   (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_rgba  s    !c         C` sF  | j    d k r d	 S| r+ | j d  n d } t d r |  j | |  } d | } t j j |  s~ |  j | |  } n  t |   } | j	   j
   }	 Wd QXg  |	 D] }
 t |
  | ^ q \ } } } | | | | f S|  j | |  } t j | d |   } t t |   } Wd QX| j | j | j | j f Sd S(
   s-   Return width, height and descent of the text.R)   i    g      ?i   s   text.latex.previews   %s.baselineNiH   (   i    i    i    (   t   stript   points_to_pixelsR   Rf   R`   Ra   R   R   Ru   t   readR   t   floatR   t   dvireadt   Dvit   nextt   itert   widtht   heightt   descent(   R&   Rb   Rc   t   renderert   dpi_fractionRx   R   R   R}   t   lt   l1R   t   depthR   R   t   page(    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   get_text_width_height_descent  s     

,N(   R(   R)   (   R*   R)   (   R+   R)   (   R,   s   \usepackage{chancery}(   R-   s
   sans-serifR.   R/   (   R0   s   \renewcommand{\rmdefault}{pnc}(   R1   s   \renewcommand{\rmdefault}{pbk}(   R3   s   \usepackage{mathptmx}(   R5   s   \usepackage{mathpazo}(   R,   s   \usepackage{chancery}(   R,   s   \usepackage{chancery}(   R7   s   \usepackage{charter}(   R(   R)   (   R*   R)   (   R9   s   \usepackage{helvet}(   R;   s   \usepackage{avant}(   R<   s   \usepackage{courier}(   R+   R)   (   R(   R)   (   R*   R)   (   R+   R)   (   s   text.latex.preamble(   R>   (   i    i    i    (1   t   __name__t
   __module__t   __doc__t   mplt   get_cachedirt   cachedirRB   R`   Ra   RK   RA   R   R   R   t   propertyt   cbookt
   deprecatedt   postscriptdt   pscntR-   RV   R/   R.   RG   RF   RM   Rg   R   t   nRj   R^   Rf   R_   Rp   RR   R   t   ret   compileR   R   R   R   R   R   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyR%   S   sn   !!
		4				.		<			"	
(+   R   t
   __future__R    R   R   RI   Rk   R   R`   t   shutilR   t   warningst   loggingt   hashlibR   t   distutils.versionR   t   numpyR   t
   matplotlibR   R   t   matplotlib._pngR   t   matplotlib.cbookR   R   t   matplotlib.compat.subprocessR   R	   R
   R   t   matplotlib.dvireadR   R   t	   getLoggerR   R   R   R   R$   t   objectR%   (    (    (    s4   lib/python2.7/site-packages/matplotlib/texmanager.pyt   <module>"   s,   "