
 m[c           @` s  d  Z  d d l m Z m Z m 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 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 m Z d d l m Z y d d	 l
 m Z Wn! e k
 rd d	 l m Z n Xe j r+d
   Z n  e j e   Z! e j" d d d d d d d d d d  Z# e	 d d  Z$ e	 d d  Z% e	 d d  Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, e- d  e' d! e e( d e.  d" e e( d e.  d# e e( d e/  d$ e) d% e, d& e* d' e+  Z0 e1 e1 d  f d(  Z2 d) e3 f d*     YZ4 d+ e3 f d,     YZ5 d- e4 f d.     YZ6 d/   Z7 d0   Z8 d1 e3 f d2     YZ9 e	 d3 d4  Z: d5 e3 f d6     YZ; d7 e3 f d8     YZ< e1 d9  Z= e   d:    Z> e e> e9 d;  Z? e e> e6 d<  Z@ e  d= k rd d l Z e jA d ZB y eC e jA d  ZD Wn eE k
 rue1 ZD n Xe4 eB eD  ZF e; e= d>   ZG x eF D] ZH eI d?  e1 ZJ x eH jK D] \ ZL ZM ZN ZO ZP eN eJ k reI d@ eN jQ dA eN jR eS dB dC   eN ZJ n  eI eL eM eO dD eO k o4dE k  n oBeT eO  pHdF eP  qWx3 eH jU D]( \ ZL ZM ZP ZV eI eL eM dG eP eV  q]WqWWd QXn  d S(H   s=  
A module for reading dvi files output by TeX. Several limitations make
this not (currently) useful as a general-purpose dvi preprocessor, but
it is currently used by the pdf backend for processing usetex text.

Interface::

  with Dvi(filename, 72) as dvi:
      # iterate over pages:
      for page in dvi:
          w, h, d = page.width, page.height, page.descent
          for x,y,font,glyph,width in page.text:
              fontname = font.texname
              pointsize = font.size
              ...
          for x,y,height,width in page.boxes:
              ...

i    (   t   absolute_importt   divisiont   print_functionN(   t   xrange(   t
   namedtuple(   t   partialt   wraps(   t   cbookt   rcParams(   t
   subprocess(   t	   lru_cachec         C` s   |  S(   N(    (   t   x(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   ord,   s    t   pret   outeri   t   inpagei   t	   post_posti   t   finalei   t   Pages   text boxes height width descentt   Texts   x y font glyph widtht   Boxs   x y height widthc         C` s   | S(   s>   Return *delta* without reading anything more from the dvi file(    (   t   dvit   delta(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _arg_rawP   s    c         C` s   | j  |  |  S(   st   Read *bytes* bytes, returning the bytes interpreted as a
    signed integer if *signed* is true, unsigned otherwise.(   t   _arg(   t   bytest   signedR   t   _(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR   U   s    c         C` s    | d k r d S|  j | t  S(   s   Signed, length *delta*

    Read *delta* bytes, returning None if *delta* is zero, and
    the bytes interpreted as a signed integer otherwise.i    N(   t   NoneR   t   True(   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _arg_slen[   s    c         C` s   |  j  | d t  S(   s^   Signed, length *delta*+1

    Read *delta*+1 bytes, returning the bytes interpreted as signed.i   (   R   R   (   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt
   _arg_slen1e   s    c         C` s   |  j  | d t  S(   sa   Unsigned length *delta*+1

    Read *delta*+1 bytes, returning the bytes interpreted as unsigned.i   (   R   t   False(   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt
   _arg_ulen1l   s    c         C` s   |  j  | d | d k  S(   s   Optionally signed, length *delta*+1

    Read *delta*+1 bytes, returning the bytes interpreted as
    unsigned integer for 0<=*delta*<3 and signed if *delta*==3.i   i   (   R   (   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt
   _arg_olen1s   s    t   rawt   u1t   u4t   s4t   slent   olen1t   slen1t   ulen1c         ` s         f d   } | S(   sd  Decorator for dispatch by opcode. Sets the values in *table*
    from *min* to *max* to this method, adds a check that the Dvi state
    matches *state* if not None, reads arguments from the file according
    to *args*.

    *table*
        the dispatch table to be filled in

    *min*
        minimum opcode for calling this function

    *max*
        maximum opcode for calling this function, None if only *min* is allowed

    *state*
        state of the Dvi object in which these opcodes are allowed

    *args*
        sequence of argument specifications:

        ``'raw'``: opcode minus minimum
        ``'u1'``: read one unsigned byte
        ``'u4'``: read four bytes, treat as an unsigned number
        ``'s4'``: read four bytes, treat as a signed number
        ``'slen'``: read (opcode - minimum) bytes, treat as signed
        ``'slen1'``: read (opcode - minimum + 1) bytes, treat as signed
        ``'ulen1'``: read (opcode - minimum + 1) bytes, treat as unsigned
        ``'olen1'``: read (opcode - minimum + 1) bytes, treat as unsigned
                     if under four bytes, signed if four bytes
    c         ` s   g   D] } t  | ^ q   t        f d    }  d  k rZ |   <n> x; t   d  D]& }  | d  k s t  |  | <qn W| S(   Nc         ` sW    d  k	 r* |  j  k r* t d   n   |  g    D] } | |  |   ^ q7  S(   Ns   state precondition failed(   R   t   statet
   ValueError(   t   selft   bytet   f(   t   get_argst   methodt   minR+   (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   wrapper   s    i   (   t   _arg_mappingR   R   R   t   AssertionError(   R1   R   R3   t   i(   t   argst   maxR2   R+   t   table(   R0   R1   s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   decorate   s    $(    (   R9   R2   R8   R+   R7   R:   (    (   R7   R8   R2   R+   R9   s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _dispatch   s    t   Dvic           B` s&  e  Z d  Z g  e d  D] Z dX ^ q Z e e e  Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z d	   Z e d
  Z e d d d d d e j  d    Z e d d d d d e j d dY  d    Z e d d e j d dZ d    Z e d d d d d e j d d[  d    Z d   Z e d d e j d d\ d    Z d   Z e d   d!    Z e d" d e j d d^ d$    Z e d% d e j d&    Z e d' d e j d(    Z  e d) d e j d*    Z! e d d+ d d, d e j d d_  d.    Z" e d d/ d d0 d e j d d`  d2    Z# e d d3 d d4 d e j d da  d5    Z$ e d d6 d d7 d e j d db  d8    Z% e d d9 d d: d e j d dc  d;    Z& e d d< d d= d e j d dd  d>    Z' e d d? d d@ d e j  dA    Z( e d dB d dC d e j d de  dD    Z) e d dE d dF d df  dH    Z* e d dI d dJ d dg  dM    Z+ dN   Z, e dO d e j- d dh dP    Z. e dQ d e j dR    Z/ e dS  dT    Z0 e d dU d dV  dW    Z1 RS(i   s?  
    A reader for a dvi ("device-independent") file, as produced by TeX.
    The current implementation can only iterate through pages in order,
    and does not even attempt to verify the postamble.

    This class can be used as a context manager to close the underlying
    file upon exit. Pages can be read via iteration. Here is an overly
    simple way to extract text without trying to detect whitespace::

    >>> with matplotlib.dviread.Dvi('input.dvi', 72) as dvi:
    >>>     for page in dvi:
    >>>         print(''.join(unichr(t.glyph) for t in page.text))
    i   c         C` sV   t  j d |  t | d  |  _ | |  _ i  |  _ t j |  _ |  j	 |  |  _
 d S(   s   
        Read the data from the file named *filename* and convert
        TeX's internal units to units of *dpi* per inch.
        *dpi* only sets the units and does not limit the resolution.
        Use None to return TeX's internal units.
        s   Dvi: %st   rbN(   t   _logt   debugt   opent   filet   dpit   fontst	   _dvistateR   R+   t   _get_baselinet   baseline(   R-   t   filenameRB   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __init__   s    		c   
      C` s   t  d r t j j |  \ } } | d } t j j |  r t | d   } | j   j   } Wd  QX| \ } } }	 t |  Sn  d  S(   Ns   text.latex.previews	   .baselineR=   (
   R   t   ost   patht   splitextt   existsR@   t   readt   splitt   floatR   (
   R-   RG   t   baset   extt   baseline_filenamet   fdt   lt   heightt   deptht   width(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRE      s    

c         C` s   |  S(   s=   
        Context manager enter method, does nothing.
        (    (   R-   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   __enter__   s    c         C` s   |  j    d S(   sX   
        Context manager exit method, closes the underlying file if it is open.
        N(   t   close(   R-   t   etypet   evaluet   etrace(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __exit__   s    c         c` s2   x+ t  r- |  j   } | r) |  j   Vq Pq Wd S(   s\  
        Iterate through the pages of the file.

        Yields
        ------
        Page
            Details of all the text and box objects on the page.
            The Page tuple contains lists of Text and Box tuples and
            the page dimensions, and the Text and Box tuples contain
            coordinates transformed into a standard Cartesian
            coordinate system at the dpi value given when initializing.
            The coordinates are floating point numbers, but otherwise
            precision is not lost and coordinate values are not clipped to
            integers.
        N(   R   t   _readt   _output(   R-   t	   have_page(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __iter__   s
    	c         C` s    |  j  j s |  j  j   n  d S(   s:   
        Close the underlying file if it is open.
        N(   RA   t   closedRY   (   R-   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRY     s    c         C` sY  t  j t  j t  j t  j f \ } } } } t  j } x |  j |  j D] } t | t  rw | \ } } }	 }
 d } n* | \ } } } } }
 | j |  \ }	 } t | |  } t | | |	  } t | | |
  } t | | |  } t | |  } qG W|  j	 d
 k rBt d |  j d |  j d | | d | | d | |  S|  j	 d d } |  j d
 k rs| | | } n	 |  j } g  |  j D]E \ } } } } }
 t | | | | | | | | | |
 |  ^ q} g  |  j D]C \ } } }	 }
 t | | | | | | | |	 | |
 |  ^ q} t d | d | d | | | d | | | d |  S(   sk   
        Output the text and boxes belonging to the most recent page.
        page = dvi._output()
        i    t   textt   boxesRW   RU   t   descentgzGR@i   i   Ni   (   t   npt   infRc   Rd   t
   isinstanceR   t   _height_depth_ofR2   R8   RB   R   R   RF   R   (   R-   t   minxt   minyt   maxxt   maxyt	   maxy_puret   eltR   t   yt   ht   wt   et   fontt   gt   dRe   R/   Rc   Rd   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR_     s6    ,
		RP c         C` sq   xj t  rl t |  j j d  d  } |  j | |  |  | d k rI t  S|  j t j k r |  j   t	 Sq Wd S(   st   
        Read one page from the file. Return True if successful,
        False if there were no more pages.
        i   i    i   N(
   R   R   RA   RM   t   _dtableR+   RD   R   RY   R    (   R-   R.   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR^   4  s    	
c         C` sw   |  j  j |  } t | d  } | rA | d k rA | d } n  x/ t d |  D] } d | t | |  } qQ W| S(   s~   
        Read and return an integer argument *nbytes* long.
        Signedness is determined by the *signed* keyword.
        i    i   i   i   (   RA   RM   R   t   range(   R-   t   nbytesR   t   strt   valueR6   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR   B  s    R2   i    R8   i   R+   c         C` s3   |  j  |  |  j |  j |  j j |  7_ d  S(   N(   t   _put_char_realRq   RC   R/   t	   _width_of(   R-   t   char(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _set_char_immediateO  s    i   i   R7   R(   c         C` s3   |  j  |  |  j |  j |  j j |  7_ d  S(   N(   R|   Rq   RC   R/   R}   (   R-   R~   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _set_charT  s    i   R&   c         C` s#   |  j  | |  |  j | 7_ d  S(   N(   t   _put_rule_realRq   (   R-   t   at   b(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _set_ruleY  s    i   i   c         C` s   |  j  |  d  S(   N(   R|   (   R-   R~   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _put_char^  s    c      
   C` s  |  j  |  j } | j d  k rS |  j j t |  j |  j | | | j	 |    n.| j
 } x | j | j D] \ } } } } } t d t | | j
  d | j d | j d | j  }	 |  j j t |  j t | |  |  j t | |  |	 | |	 j	 |    qm W|  j j g  | j | j D]Y \ } } }
 } t |  j t | |  |  j t | |  t |
 |  t | |   ^ q! d  S(   Nt   scalet   tfmt   texnamet   vf(   RC   R/   t   _vfR   Rc   t   appendR   Rq   t   vR}   t   _scalet   DviFontt   _mul2012t   _tfmR   Rd   t   extendR   (   R-   R~   Rt   R   R   Rp   R/   Ru   Rr   t   newfR   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR|   b  s    	&i   c         C` s   |  j  | |  d  S(   N(   R   (   R-   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _put_rulet  s    c         C` sD   | d k r@ | d k r@ |  j  j t |  j |  j | |   n  d  S(   Ni    (   Rd   R   R   Rq   R   (   R-   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR   x  s    i   c         C` s   d  S(   N(    (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _nop|  s    i   i   c         C` sU   t  j |  _ d \ |  _ |  _ |  _ |  _ |  _ |  _ g  |  _	 g  |  _
 g  |  _ d  S(   Ni    (   i    i    i    i    i    i    (   RD   R   R+   Rq   R   Rr   R   Rp   t   zt   stackRc   Rd   (   R-   t   c0t   c1t   c2t   c3t   c4t   c5t   c6t   c7t   c8t   c9t   p(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _bop  s
    *		i   c         C` s:   t  j |  _ |  ` |  ` |  ` |  ` |  ` |  ` |  `	 d  S(   N(
   RD   R   R+   Rq   R   Rr   R   Rp   R   R   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _eop  s    i   c         C` s8   |  j  j |  j |  j |  j |  j |  j |  j f  d  S(   N(   R   R   Rq   R   Rr   R   Rp   R   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _push  s    i   c         C` s7   |  j  j   \ |  _ |  _ |  _ |  _ |  _ |  _ d  S(   N(   R   t   popRq   R   Rr   R   Rp   R   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _pop  s    i   i   R)   c         C` s   |  j  | 7_  d  S(   N(   Rq   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _right  s    i   i   R'   c         C` s.   | d  k	 r | |  _ n  |  j |  j 7_ d  S(   N(   R   Rr   Rq   (   R-   t   new_w(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _right_w  s    i   i   c         C` s.   | d  k	 r | |  _ n  |  j |  j 7_ d  S(   N(   R   R   Rq   (   R-   t   new_x(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _right_x  s    i   i   c         C` s   |  j  | 7_  d  S(   N(   R   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _down  s    i   i   c         C` s.   | d  k	 r | |  _ n  |  j |  j 7_ d  S(   N(   R   Rp   R   (   R-   t   new_y(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _down_y  s    i   i   c         C` s.   | d  k	 r | |  _ n  |  j |  j 7_ d  S(   N(   R   R   R   (   R-   t   new_z(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _down_z  s    i   i   c         C` s   | |  _  d  S(   N(   R/   (   R-   t   k(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _fnt_num_immediate  s    i   i   c         C` s   | |  _  d  S(   N(   R/   (   R-   t   new_f(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _fnt_num  s    i   i   R*   c         C` s   |  j  j |  } t j r$ t } n	 d   } t j d d j g  | D]D } d t |  k of d k  n rw | |  n d t |  ^ qC   d  S(   Nc         S` s   |  S(   N(    (   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   chr_  s    s!   Dvi._xxx: encountered special: %st    i    i   s   <%02x>(	   RA   RM   t   sixt   PY3t   chrR>   R?   t   joinR   (   R-   t   datalent   specialR   t   ch(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _xxx  s    				i   i   R%   R$   c         C` s    |  j  | | | | | |  d  S(   N(   t   _fnt_def_real(   R-   R   t   ct   sRv   R   RT   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _fnt_def  s    c      	   C` s   |  j  j | |  } | | j d  } t |  }	 |	 d  k rm t j rT t }
 n t }
 |
 d |   n  | d k r |	 j	 d k r | |	 j	 k r t
 d |   n  t |  } t d | d |	 d | d |  |  j | <d  S(	   Nt   asciis   missing font metrics file: %si    s   tfm checksum mismatch: %sR   R   R   R   (   RA   RM   t   decodet   _tfmfileR   R   t   PY2t   OSErrort   FileNotFoundErrort   checksumR,   t   _vffileR   RC   (   R-   R   R   R   Rv   R   RT   t   nt   fontnameR   t   error_classR   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s    		*i   c         C` s   |  j  j |  } | d k r1 t d |   n  | d k sM | d d	 k r\ t d   n  | d k rw t d   n  t j |  _ d  S(
   Ni   s   Unknown dvi format %dii;  i   s   nonstandard units in dvi filei  s%   nonstandard magnification in dvi filei   (   RA   RM   R,   RD   R   R+   (   R-   R6   t   numt   dent   magR   t   comment(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _pre  s    i   c         C` s   t  j |  _ d  S(   N(   RD   R   R+   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   _post  s    i   c         C` s
   t   d  S(   N(   t   NotImplementedError(   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt
   _post_post  s    i   i   c         C` s   t  d d |   d  S(   Ns   unknown command: byte %di   (   R,   (   R-   t   offset(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt
   _malformed  s    N(   R(   (   R&   R&   (   R(   (   R&   R&   (   R&   (   R&   R&   R&   R&   R&   R&   R&   R&   R&   R&   R&   (   R)   (   R'   (   R'   (   R)   (   R'   (   R'   (   R(   (   R*   (   R(   R%   R%   R%   R$   R$   (   R$   R%   R%   R%   R$   (2   t   __name__t
   __module__t   __doc__R   R   R   Rw   R   R;   RH   RE   RX   R]   Ra   RY   R_   R^   R    R   RD   R   R   R   R   R   R|   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR<      sN   							)	'-$-	$	$------'-$$	$R   c           B` sA   e  Z d  Z d Z d   Z d   Z d	   Z d
   Z d   Z RS(   s  
    Encapsulation of a font that a DVI file can refer to.

    This class holds a font's texname and size, supports comparison,
    and knows the widths of glyphs in the same units as the AFM file.
    There are also internal attributes (for use by dviread.py) that
    are *not* used for comparison.

    The size is in Adobe points (converted from TeX points).

    Parameters
    ----------

    scale : float
        Factor by which the font is scaled from its natural size.
    tfm : Tfm
        TeX font metrics for this font
    texname : bytes
       Name of the font as used internally by TeX and friends, as an
       ASCII bytestring. This is usually very different from any external
       font names, and :class:`dviread.PsfontsMap` can be used to find
       the external name of the font.
    vf : Vf
       A TeX "virtual font" file, or None if this font is not virtual.

    Attributes
    ----------

    texname : bytes
    size : float
       Size of the font in Adobe points, converted from the slightly
       smaller TeX points.
    widths : list
       Widths of glyphs in glyph-space units, typically 1/1000ths of
       the point size.

    R   t   sizet   widthsR   R   R   c         C` s   t  | t  s( t d t |    n  | | | | f \ |  _ |  _ |  _ |  _ | d d d
 |  _ y t	 | j
  d } Wn t k
 r d } n Xg  t |  D]# } d | j
 j | d  d	 ?^ q |  _ d  S(   Ns$   texname must be a bytestring, got %sg      R@gzGR@i   i   i   i    i  i   i   (   Rh   R   R,   t   typeR   R   R   R   R   R8   RW   R   t   getR   (   R-   R   R   R   R   t   ncharsR~   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRH   (  s    *
c         C` s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S(   N(   t	   __class__R   R   (   R-   t   other(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __eq__6  s    c         C` s   |  j  |  S(   N(   R   (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __ne__:  s    c         C` sN   |  j  j j | d  } | d k	 r4 t | |  j  St j d | |  j  d S(   sM   
        Width of char in dvi units. For internal use by dviread.py.
        s    No width for char %d in font %s.i    N(	   R   RW   R   R   R   R   R>   R?   R   (   R-   R~   RW   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR}   =  s
    c         C` s   g  } x |  j  j d f |  j  j d f f D]l \ } } | j | d  } | d k r~ t j d | | |  j  | j d  q+ | j t	 | |  j
   q+ W| S(   sX   
        Height and depth of char in dvi units. For internal use by dviread.py.
        RU   RV   s   No %s for char %d in font %si    N(   R   RU   RV   R   R   R>   R?   R   R   R   R   (   R-   R~   t   resultt   metrict   nameR{   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRi   H  s    	(   R   R   R   R   R   R   (	   R   R   R   t	   __slots__RH   R   R   R}   Ri   (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR      s   %				t   Vfc           B` sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    A virtual font (\*.vf file) containing subroutines for dvi files.

    Usage::

      vf = Vf(filename)
      glyph = vf[code]
      glyph.text, glyph.boxes, glyph.width

    Parameters
    ----------

    filename : string or bytestring

    Notes
    -----

    The virtual font format is a derivative of dvi:
    http://mirrors.ctan.org/info/knuth/virtual-fonts
    This class reuses some of the machinery of `Dvi`
    but replaces the `_read` loop and dispatch mechanism.
    c         C` sE   t  j |  | d  z  d  |  _ i  |  _ |  j   Wd  |  j   Xd  S(   Ni    (   R<   RH   R   t   _first_fontt   _charsR^   RY   (   R-   RG   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRH   r  s    		c         C` s   |  j  | S(   N(   R   (   R-   t   code(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   __getitem__{  s    c         C` s  d \ } } d \ } } xt rt |  j j d  d  } |  j t j k r |  j j   d } | | k r |  j	 | |  d \ } } } q | | k r t
 d   q | d k s | d k r t
 d |   n  t j | |  |  q n  | d k  r?| } |  j d  |  j d	  } } |  j |  } t j |  _ q | d k rg  d D] } |  j |  ^ qR\ } } } |  j |  q d | k od k n r)|  j | d | d k  } g  d D] } |  j |  ^ q\ }	 }
 } } } |  j | |	 |
 | | |  |  j d k r| |  _ qq | d k r|  j d  |  j d  } } |  j j |  } |  j d
  |  j d
  } } |  j | | | |  q | d k rPq t
 d |   q Wd S(   st   
        Read one page from the file. Return True if successful,
        False if there were no more pages.
        i   i    s!   Packet length mismatch in vf filei   i   i   s"   Inappropriate opcode %d in vf filei   i   i   i   i   i   s   unknown vf opcode %dN(   NN(   NN(   NNN(   i   i   (   i   i   i   (   i   i   i   i   i   (   R   R   R   RA   RM   R+   RD   R   t   tellt   _finalize_packetR,   R<   Rw   R   t   _init_packetR   R   R   (   R-   t   packet_chart   packet_endst
   packet_lent   packet_widthR.   t   byte_atR   R   R   R   Rv   R   RT   R6   t   cst   ds(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR^   ~  sL    	+1c         C` s   |  j  t j k r! t d   n  d \ |  _ |  _ |  _ |  _ |  _ |  _	 g  g  g  |  _
 |  _ |  _ |  j |  _ |  j j   | S(   Ns   Misplaced packet in vf filei    (   i    i    i    i    i    i    (   R+   RD   R   R,   Rq   R   Rr   R   Rp   R   R   Rc   Rd   R   R/   RA   R   (   R-   t   pl(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s    *c         C` sD   t  d |  j d |  j d | d d  d d   |  j | <t j |  _ d  S(   NRc   Rd   RW   RU   Re   (   R   Rc   Rd   R   R   RD   R   R+   (   R-   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s    c         C` so   |  j  t j k r! t d   n  | d k r@ t d |   n  t |  r_ t j d |  n  t j |  _  d  S(   Ns    pre command in middle of vf filei   s   Unknown vf format %ds   vf file comment: %s(   R+   RD   R   R,   t   lenR>   R?   R   (   R-   R6   R   R   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s    (	   R   R   R   RH   R   R^   R   R   R   (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR   Z  s   				3		c         C` s<   d |  k o d k  n s" t   |  d @r4 |  d S|  Sd S(	   s4   
    Convert from two's complement to negative.
    i    i   i    i   NI       I       I       (   R5   (   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _fix2comp  s    "
c         C` s   |  | d ?S(   s;   
    Multiply two numbers in 20.12 fixed point format.
    i   (    (   t   num1t   num2(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s    t   Tfmc           B` s   e  Z d  Z d Z d   Z RS(   s  
    A TeX Font Metric file.

    This implementation covers only the bare minimum needed by the Dvi class.

    Parameters
    ----------
    filename : string or bytestring

    Attributes
    ----------
    checksum : int
       Used for verifying against the dvi file.
    design_size : int
       Design size of the font (unknown units)
    width, height, depth : dict
       Dimensions of each character, need to be scaled by the factor
       specified in the dvi file. These are dicts because indexing may
       not start from 0.
    R   t   design_sizeRW   RU   RV   c         C` s  t  j d |  t | d   } | j d  } t j d | d d ! \ } } } } } }	 t  j d | | | | | |	  | j d |  }
 t j d	 |
 d
   \ |  _ |  _ | j d | | d  } | j d |  } | j d |  } | j d |	  } Wd  QXi  i  i  |  _ |  _	 |  _
 g  | | | f D]& } t j d t |  d |  ^ q7\ } } } x t t | | d   D] \ } } t | d |  } t | d | d  } t | |  |  j | <t | | d ? |  j	 | <t | | d @ |  j
 | <qWd  S(   Ns   opening tfm file %sR=   i   s   !6Hi   i   s(   lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%di   s   !2Ii   i   s   !%dIi   (   R>   R?   R@   RM   t   structt   unpackR   R   RW   RU   RV   R   t	   enumerateR   R   R   (   R-   RG   RA   t   header1t   lht   bct   ect   nwt   nht   ndt   header2t	   char_infoR   t   heightst   depthsR   t   idxR~   t   byte0t   byte1(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRH     s*    +	"B&(   R   R   RW   RU   RV   (   R   R   R   R   RH   (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR     s   t   Fonts(   texname psname effects encoding filenamet
   PsfontsMapc           B` s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   s  
    A psfonts.map formatted file, mapping TeX fonts to PS fonts.

    Usage::

     >>> map = PsfontsMap(find_tex_file('pdftex.map'))
     >>> entry = map[b'ptmbo8r']
     >>> entry.texname
     b'ptmbo8r'
     >>> entry.psname
     b'Times-Bold'
     >>> entry.encoding
     '/usr/local/texlive/2008/texmf-dist/fonts/enc/dvips/base/8r.enc'
     >>> entry.effects
     {'slant': 0.16700000000000001}
     >>> entry.filename

    Parameters
    ----------

    filename : string or bytestring

    Notes
    -----

    For historical reasons, TeX knows many Type-1 fonts by different
    names than the outside world. (For one thing, the names have to
    fit in eight characters.) Also, TeX's native fonts are not Type-1
    but Metafont, which is nontrivial to convert to PostScript except
    as a bitmap. While high-quality conversions to Type-1 format exist
    and are shipped with modern TeX distributions, we need to know
    which Type-1 fonts are the counterparts of which native fonts. For
    these reasons a mapping is needed from internal font names to font
    file names.

    A texmf tree typically includes mapping files called e.g.
    :file:`psfonts.map`, :file:`pdftex.map`, or :file:`dvipdfm.map`.
    The file :file:`psfonts.map` is used by :program:`dvips`,
    :file:`pdftex.map` by :program:`pdfTeX`, and :file:`dvipdfm.map`
    by :program:`dvipdfm`. :file:`psfonts.map` might avoid embedding
    the 35 PostScript fonts (i.e., have no filename for them, as in
    the Times-Bold example above), while the pdf-related files perhaps
    only avoid the "Base 14" pdf fonts. But the user may have
    configured these files differently.
    t   _fontt	   _filenamec         C` s   i  |  _  | |  _ t j rW t | t  rW t j   p9 d } | j | d d |  _ n  t	 | d   } |  j
 |  Wd  QXd  S(   Ns   utf-8t   errorst   replaceR=   (   R  R  R   R   Rh   R   t   syst   getfilesystemencodingR   R@   t   _parse(   R-   RG   t   encodingRA   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRH   D  s    		c         C` s  t  | t  s t  y |  j | } Wn` t k
 r d } | j | j d  |  j  } t j	 | d t
 d t
 } t j |    n X| j | j } } | d  k	 r | j d  r t |  } n  | d  k	 r | j d  r t | j  } n  | j d | d |  S(   Ns$  A PostScript file for the font whose TeX name is "{0}" could not be found in the file "{1}". The dviread module can only handle fonts that have an associated PostScript font file. This problem can often be solved by installing a suitable PostScript font package in your (TeX) package manager.R   t   break_on_hyphenst   break_long_wordst   /RG   R  (   Rh   R   R5   R  t   KeyErrort   formatR   R  t   textwrapt   fillR    R>   t   infoRG   R  R   t
   startswitht   find_tex_filet   _replace(   R-   R   R   t   fmtt   msgt   fnt   enc(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR   M  s     	c         ` s(  t  j d    t  j d  } t  j d  }   f d   | D } x| D]} d \ } } } | j |  }	 t |	  }
 |
 j d  p |
 j d  } t |	  }
 |
 j d  p |
 j d  } x |	 D] }
 |
 j d  p |
 j d  } | r | } q n  |
 j d  p8|
 j d	  p8|
 j d
  p8|
 j d  } | ro| d k	 rct j d | |  n  | } q n  |
 j d  p|
 j d  } q Wi  } xX | j |  D]G } | j d  } | rt |  | d <qt | j d   | d <qWt	 d | d | d | d | d |  |  j
 | <qJ Wd S(   s>  
        Parse the font mapping file.

        The format is, AFAIK: texname fontname [effects and filenames]
        Effects are PostScript snippets like ".177 SlantFont",
        filenames begin with one or two less-than signs. A filename
        ending in enc is an encoding file, other filenames are font
        files. This can be overridden with a left bracket: <[foobar
        indicates an encoding file named foobar.

        There is some difference between <foo.pfb and <<bar.pfb in
        subsetting, but I have no example of << in my TeX installation.
        s   %|\s*$sZ  (?x) (?:
                 "<\[ (?P<enc1>  [^"]+    )" | # quoted encoding marked by [
                 "<   (?P<enc2>  [^"]+.enc)" | # quoted encoding, ends in .enc
                 "<<? (?P<file1> [^"]+    )" | # quoted font file name
                 "    (?P<eff1>  [^"]+    )" | # quoted effects or font name
                 <\[  (?P<enc3>  \S+      )  | # encoding marked by [
                 <    (?P<enc4>  \S+  .enc)  | # encoding, ends in .enc
                 <<?  (?P<file2> \S+      )  | # font file name
                      (?P<eff2>  \S+      )    # effects or font name
            )sy   (?x) (?P<slant> -?[0-9]*(?:\.[0-9]+)) \s* SlantFont
                    | (?P<extend>-?[0-9]*(?:\.[0-9]+)) \s* ExtendFontc         3` s*   |  ]  }   j  |  s | j   Vq d  S(   N(   t   matcht   strip(   t   .0t   line(   t   empty_re(    s1   lib/python2.7/site-packages/matplotlib/dviread.pys	   <genexpr>  s   R   t   eff2t   eff1t   enc4t   enc3t   enc2t   enc1s   Multiple encodings for %s = %st   file2t   file1t   slantR   R   t   psnamet   effectsR  RG   N(   R   NN(   t   ret   compileR   t   finditert   nextt   groupR>   R?   RO   t   PsFontR  (   R-   RA   t   word_ret
   effects_ret   linesR+  R7  R  RG   t   wordsRr   R   R6  t   effR'  t   effects_dictR(  R5  (    (   R,  s1   lib/python2.7/site-packages/matplotlib/dviread.pyR  e  sJ    
		
	"(   R  R  (   R   R   R   R   RH   R   R  (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR    s
   -			t   Encodingc           B` s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   s  
    Parses a \*.enc file referenced from a psfonts.map style file.
    The format this class understands is a very limited subset of
    PostScript.

    Usage (subject to change)::

      for name in Encoding(filename):
          whatever(name)

    Parameters
    ----------
    filename : string or bytestring

    Attributes
    ----------
    encoding : list
        List of character names
    R  c         C` sQ   t  | d  < } t j d |  |  j |  |  _ t j d |  j  Wd  QXd  S(   NR=   s   Parsing TeX encoding %ss
   Result: %s(   R@   R>   R?   R  R  (   R-   RG   RA   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRH     s    c         c` s   x |  j  D] } | Vq
 Wd  S(   N(   R  (   R-   R   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRa     s    c         C` s   g  } d   | D } d j  |  } | j d  } | d k  rX t d j |    n  | | } | j d  } | d k  r t d j |    n  | |  } t j d |  S(	   Nc         s` s+   |  ]! } | j  d  d  d j   Vq d S(   t   %i   i    N(   RN   R)  (   R*  R+  (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pys	   <genexpr>  s    R   t   [i    s)   Cannot locate beginning of encoding in {}t   ]s#   Cannot locate end of encoding in {}s   /([^][{}<>\s]+)(   R   t   findR,   R  R8  t   findall(   R-   RA   R   R@  t   datat	   beginningt   end(    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR    s    	
	
(   R  (   R   R   R   R   RH   Ra   R  (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyRD    s
   		c         C` s   t  j rZ t |  t  r0 |  j d d d }  n  t | t  rZ | j d d d } qZ n  d g } | d k	 r | d | g 7} n  | |  g 7} t j d |  |  t j	 | d t j
 d t j
 } | j   d	 j   } t j d
 |  | j d  S(   s  
    Find a file in the texmf tree.

    Calls :program:`kpsewhich` which is an interface to the kpathsea
    library [1]_. Most existing TeX distributions on Unix-like systems use
    kpathsea. It is also available as part of MikTeX, a popular
    distribution on Windows.

    Parameters
    ----------
    filename : string or bytestring
    format : string or bytestring
        Used as the value of the `--format` option to :program:`kpsewhich`.
        Could be e.g. 'tfm' or 'vf' to limit the search to that type of files.

    References
    ----------

    .. [1] `Kpathsea documentation <http://www.tug.org/kpathsea/>`_
        The library that :program:`kpsewhich` is part of.
    s   utf-8R  R  t	   kpsewhichs	   --format=s   find_tex_file(%s): %st   stdoutt   stderri    s   find_tex_file result: %sR   N(   R   R   Rh   R   R   R   R>   R?   R	   t   Popent   PIPEt   communicatet   rstrip(   RG   R  t   cmdt   pipeR   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyR"    s    		c         C` s$   t  | |  } | r  |  |  Sd  S(   N(   R"  R   (   t   clst   suffixR   RG   (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt	   _fontfile  s    s   .tfms   .vft   __main__s
   pdftex.maps   === new page ===Rt   t   scaledg       @i   i    i   t   .t   BOX(W   R   t
   __future__R    R   R   R   t	   six.movesR   t   collectionsR   t	   functoolsR   R   t   loggingt   numpyRf   RI   R8  R   R  R  t
   matplotlibR   R   t   matplotlib.compatR	   R
   t   ImportErrort   backports.functools_lru_cacheR   R   t	   getLoggerR   R>   t   BunchRD   R   R   R   R   R   R   R   R!   R"   t   dictR    R   R4   R   R;   t   objectR<   R   R   R   R   R   R=  R  RD  R"  RX  R   R   t   argvt   fnameRO   RB   t
   IndexErrorR   t   fontmapt   paget   printt   fPrevRc   R   Rp   R/   R   Rr   R   R   t   powR   Rd   Rq   (    (    (    s1   lib/python2.7/site-packages/matplotlib/dviread.pyt   <module>   s   	*
			
				1 KZp		433

&	B