3
ƽh(                 @   s  d Z ddlmZ ddlZddlmZmZm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ZddlZddlmZmZ ejeZejddZed	d
ZeddZeddZdd Zdd Zdd Zdd Z dd Z!dd Z"e#eeeddeeddeeddee"e e!dZ$dOd!d"Z%G d#d$ d$Z&G d%d& d&Z'G d'd( d(e&Z(d)d* Z)d+d, Z*G d-d. d.Z+ed/d0Z,G d1d2 d2Z-ej.d3G d4d5 d5Z/d6d7 Z0e dPd8d9Z1e d:d; Z2ee2e+d<Z3ee2e(d=Z4ed>kr dd?l5m6Z6 ddl7Z7e6 Z8e8j9d@ e8j9dAdBe:ddC e8j; Z<e&e<j=e<j>Z?e-e1dDZ@xe?D ]ZAeBdE xxe7jCeAjDdFdG D ]b\ZEZFeBdHeEjGdIeEjHdQ  x@eFD ]8ZDeBeDjIeDjJeDjKeLeDjKjM reLeDjKndLeDjN qW q`W x(eAjOD ]\ZIZJZPZQeBeIeJdMePeQ qW q@W W dQ R X dS )RaC  
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:
              ...
    )
namedtupleN)	lru_cachepartialwraps)Path)cbookrcParamsZDviStatez!pre outer inpage post_post finalePageztext boxes height width descentTextzx y font glyph widthBoxzx y height widthc             C   s   |S )z?Return *delta* without reading anything more from the dvi file. )dvideltar   r   8/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/dviread.py_arg_rawH   s    r   c             C   s   |j | |S )z
    Read *nbytes* bytes, returning the bytes interpreted as a signed integer
    if *signed* is true, unsigned otherwise.
    )_arg)nbytessignedr   _r   r   r   r   M   s    r   c             C   s   |dkrdS | j |dS )z
    Signed, length *delta*

    Read *delta* bytes, returning None if *delta* is zero, and the bytes
    interpreted as a signed integer otherwise.
    r   NT)r   )r   r   r   r   r   	_arg_slenU   s    r   c             C   s   | j |d dS )zh
    Signed, length *delta*+1

    Read *delta*+1 bytes, returning the bytes interpreted as signed.
       T)r   )r   r   r   r   r   
_arg_slen1a   s    r   c             C   s   | j |d dS )zk
    Unsigned length *delta*+1

    Read *delta*+1 bytes, returning the bytes interpreted as unsigned.
    r   F)r   )r   r   r   r   r   
_arg_ulen1j   s    r   c             C   s   | j |d |dkS )z
    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.
    r      )r   )r   r   r   r   r   
_arg_olen1s   s    r   r   F   T)rawu1u4s4slenolen1slen1ulen1r   c                s    fdd}|S )ai  
    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                sn   dd D  t  fdd}d kr:|< n0x.td D ]}| d ks^t||< qJW |S )Nc             S   s   g | ]}t | qS r   )_arg_mapping).0xr   r   r   
<listcomp>   s    z/_dispatch.<locals>.decorate.<locals>.<listcomp>c                s:   d k	rj krtdf fddD  S )Nzstate precondition failedc                s   g | ]}|  qS r   r   )r%   f)byteminselfr   r   r'      s    z@_dispatch.<locals>.decorate.<locals>.wrapper.<locals>.<listcomp>)state
ValueError)r+   r)   )get_argsmethodr*   r,   )r)   r+   r   wrapper   s    z,_dispatch.<locals>.decorate.<locals>.wrapperr   )r   rangeAssertionError)r/   r0   i)argsmaxr*   r,   table)r.   r/   r   decorate   s    
z_dispatch.<locals>.decorater   )r6   r*   r5   r,   r4   r7   r   )r4   r5   r*   r,   r6   r   	_dispatch   s     r8   c            	   @   s  e Zd ZdZdgd ZeeeZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdddZeddejddd Zeddejddd d! Zed"ejdd$d%d& Zed'd(ejddd)d* Zd+d, Zed-ejdd$d.d/ Zd0d1 Zed2d3d4 Zed5ejdd$d7d8 Zed9ejd:d;d< Zed=ejd:d>d? Zed@ejd:dAdB ZedCdDejdddFdG ZedHdIejdddKdL Z edMdNejdddOdP Z!edQdRejdddSdT Z"edUdVejdddWdX Z#edYdZejddd[d\ Z$ed]d^ejdd_d` Z%edadbejdddcdd Z&ededfddhdidj Z'edkdlddhdodp Z(dqdr Z)edsej*dd$dtdu Z+edvejd:dwdx Z,edydzd{ Z-ed|d}d~dd Z.dS )DviaH  
    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(chr(t.glyph) for t in page.text))
    N   c             C   s<   t jd| t|d| _|| _i | _tj| _| j	|| _
dS )z
        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.
        zDvi: %srbN)_logdebugopenfiledpifonts	_dvistateprer,   _get_baselinebaseline)r+   filenamer@   r   r   r   __init__   s    zDvi.__init__c             C   s@   t jtdr<t|jd}|j r<|j j \}}}t|S d S )Nztext.latex.previewz	.baseline)	dict__getitem__r   r   Zwith_suffixexistsZ
read_bytessplitfloat)r+   rF   rE   heightdepthwidthr   r   r   rD      s    zDvi._get_baselinec             C   s   | S )z+Context manager enter method, does nothing.r   )r+   r   r   r   	__enter__   s    zDvi.__enter__c             C   s   | j   dS )zX
        Context manager exit method, closes the underlying file if it is open.
        N)close)r+   etypeZevalueZetracer   r   r   __exit__   s    zDvi.__exit__c             c   s   x| j  r| j V  qW dS )a\  
        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)_read_output)r+   r   r   r   __iter__   s    
zDvi.__iter__c             C   s   | j js| j j  dS )z(Close the underlying file if it is open.N)r?   closedrQ   )r+   r   r   r   rQ      s    z	Dvi.closec                s  t jt jt j t j f\}}t j }x| j| j D ]z}t|trV|\}}}}d}	n|\}}}
}}|
j|\}}	t|t||| }t||| }t||	 t||}q6W | j	dk	r| j	}d| _	| j r| j rt
g g ddddS | jdkrt
| j| j| || | dS | jd  | jdkr>|   n| j fdd| jD } fd	d| jD }t
|||   ||   dS )zk
        Output the text and boxes belonging to the most recent page.
        page = dvi._output()
        r   N)textboxesrO   rM   descentgzGR@      c          	      s>   g | ]6\}}}}}t |   |    |||  qS r   )r
   )r%   r&   yr(   gw)drZ   maxyminxr   r   r'   *  s   zDvi._output.<locals>.<listcomp>c                s>   g | ]6\}}}}t |   |    |  |  qS r   )r   )r%   r&   r]   hr_   )r`   rZ   ra   rb   r   r   r'   ,  s   i   gzGRA)npinfrX   rY   
isinstancer   _height_depth_ofr*   r5   _baseline_vr	   r@   rE   )r+   ZminyZmaxxZ	maxy_pureeltr&   r]   rc   r_   efontr^   rX   rY   r   )r`   rZ   ra   rb   r   rU     s@     






zDvi._outputc             C   sz   d| _ xn| jjdd }| j| | | | j dkrNtt| dg dkrN| j| _ |dkrZdS | jtj	kr| j
  dS qW dS )	zt
        Read one page from the file. Return True if successful,
        False if there were no more pages.
        Nr   r   stackr      TF)rh   r?   read_dtablelengetattrvr,   rB   	post_postrQ   )r+   r)   r   r   r   rT   2  s    
z	Dvi._readFc             C   sN   | j j|}|d }|r(|dkr(|d }x |dd D ]}d| | }q6W |S )z~
        Read and return an integer argument *nbytes* long.
        Signedness is determined by the *signed* keyword.
        r      r:   r   N)r?   rn   )r+   r   r   bufvaluebr   r   r   r   O  s    zDvi._argr      )r*   r5   r,   c             C   s*   | j | |  j| j| j j|7  _d S )N)_put_char_realrc   rA   r(   	_width_of)r+   charr   r   r   _set_char_immediate\  s    
zDvi._set_char_immediatert      r!   )r*   r5   r,   r4   c             C   s*   | j | |  j| j| j j|7  _d S )N)ry   rc   rA   r(   rz   )r+   r{   r   r   r   	_set_chara  s    
zDvi._set_char   r   )r,   r4   c             C   s   | j || |  j|7  _d S )N)_put_rule_realrc   )r+   arw   r   r   r   	_set_rulef  s    zDvi._set_rule      c             C   s   | j | d S )N)ry   )r+   r{   r   r   r   	_put_chark  s    zDvi._put_charc       	   	      s   j j }|jd kr:jjtjj|||j| n|j	 xr|j| jD ]b\}}}}}t
t |j	|j|j|jd}jjtjt|  jt|  |||j| qNW jj fdd|j| jD  d S )N)scaletfmtexnamevfc          	      sF   g | ]>\}}}}t jt|  jt|  t| t| qS r   )r   rc   _mul2012rr   )r%   r&   r]   r   rw   )r   r+   r   r   r'   |  s   z&Dvi._put_char_real.<locals>.<listcomp>)rA   r(   _vfrX   appendr
   rc   rr   rz   _scaleDviFontr   _tfmr   rY   extend)	r+   r{   rk   r&   r]   r(   r^   r_   Znewfr   )r   r+   r   ry   o  s    
zDvi._put_char_real   c             C   s   | j || d S )N)r   )r+   r   rw   r   r   r   	_put_rule  s    zDvi._put_rulec             C   s.   |dkr*|dkr*| j jt| j| j|| d S )Nr   )rY   r   r   rc   rr   )r+   r   rw   r   r   r   r     s    zDvi._put_rule_real   c             C   s   d S )Nr   )r+   r   r   r   r   _nop  s    zDvi._nop      c             C   s:   t j| _d\| _| _| _| _| _| _g | _	g | _
g | _d S )Nr   )r   r   r   r   r   r   )rB   inpager,   rc   rr   r_   r&   r]   zrl   rX   rY   )r+   Zc0Zc1c2c3Zc4Zc5Zc6Zc7Zc8Zc9pr   r   r   _bop  s
    zDvi._boprm   )r,   c             C   s(   t j| _| `| `| `| `| `| `| `	d S )N)
rB   outerr,   rc   rr   r_   r&   r]   r   rl   )r+   r   r   r   r   _eop  s    zDvi._eop   c             C   s(   | j j| j| j| j| j| j| jf d S )N)rl   r   rc   rr   r_   r&   r]   r   )r+   r   r   r   r   _push  s    z	Dvi._push   c             C   s&   | j j \| _| _| _| _| _| _d S )N)rl   poprc   rr   r_   r&   r]   r   )r+   r   r   r   r   _pop  s    zDvi._pop      r"   c             C   s   |  j |7  _ d S )N)rc   )r+   rw   r   r   r   _right  s    z
Dvi._right      r    c             C   s"   |d k	r|| _ |  j| j 7  _d S )N)r_   rc   )r+   Znew_wr   r   r   _right_w  s    zDvi._right_w      c             C   s"   |d k	r|| _ |  j| j 7  _d S )N)r&   rc   )r+   Znew_xr   r   r   _right_x  s    zDvi._right_x      c             C   s   |  j |7  _ d S )N)rr   )r+   r   r   r   r   _down  s    z	Dvi._down      c             C   s"   |d k	r|| _ |  j| j 7  _d S )N)r]   rr   )r+   Znew_yr   r   r   _down_y  s    zDvi._down_y      c             C   s"   |d k	r|| _ |  j| j 7  _d S )N)r   rr   )r+   Znew_zr   r   r   _down_z  s    zDvi._down_z      c             C   s
   || _ d S )N)r(   )r+   kr   r   r   _fnt_num_immediate  s    zDvi._fnt_num_immediate      c             C   s
   || _ d S )N)r(   )r+   new_fr   r   r   _fnt_num  s    zDvi._fnt_num      r#   )r*   r5   r4   c             C   s,   | j j|}tjddjdd |D  d S )Nz!Dvi._xxx: encountered special: %s c             S   s4   g | ],}d |  kodk n  r(t |nd| qS )    rx   z<%02x>)chr)r%   chr   r   r   r'     s   zDvi._xxx.<locals>.<listcomp>)r?   rn   r<   r=   join)r+   ZdatalenZspecialr   r   r   _xxx  s
    
zDvi._xxx      r   r   c             C   s   | j |||||| d S )N)_fnt_def_real)r+   r   csr`   r   lr   r   r   _fnt_def  s    zDvi._fnt_defc             C   s   | j j|| }|| d  jd}t|}	|	d kr@td| |dkrh|	jdkrh||	jkrhtd| t|}
t||	||
d| j	|< d S )Nasciizmissing font metrics file: %sr   ztfm checksum mismatch: %s)r   r   r   r   )
r?   rn   decode_tfmfileFileNotFoundErrorchecksumr-   _vffiler   rA   )r+   r   r   r   r`   r   r   nZfontnamer   r   r   r   r   r     s    zDvi._fnt_def_real   c             C   sT   | j j| |dkr td| |dks0|d
kr8td|dkrHtdtj| _d S )Nr[   zUnknown dvi format %dii;  r\   zNonstandard units in dvi filei  z%Nonstandard magnification in dvi filei   i  ;)r?   rn   r-   rB   r   r,   )r+   r3   numZdenZmagr   r   r   r   _pre  s    zDvi._pre   c             C   s   t j| _d S )N)rB   rs   r,   )r+   r   r   r   r   _post  s    z	Dvi._post   c             C   s   t d S )N)NotImplementedError)r+   r   r   r   r   
_post_post  s    zDvi._post_post      )r*   r5   c             C   s   t dd|  d S )Nzunknown command: byte r   )r-   )r+   offsetr   r   r   
_malformed  s    zDvi._malformed)F)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   )/__name__
__module____qualname____doc__ro   r   r8   rG   rD   rP   rS   rV   rQ   rU   rT   r   rB   r   r|   r~   r   r   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   r   r   r   r   r9      sN   

/
r9   c               @   sD   e 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S )r   a  
    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   sizewidthsr   r   r   c                sx   t jt|d || _ | _|| _|| _|d | _yt j	d }W n t
k
rZ   d}Y nX  fdd	t|D | _d S )N)r   g      R@gzGR@r[   r\   r   r   c                s"   g | ]}d  j j|d d? qS )i  r      )rO   get)r%   r{   )r   r   r   r'   5  s   z$DviFont.__init__.<locals>.<listcomp>i   gzGRAg]e>)r   Z_check_isinstancebytesr   r   r   r   r   r5   rO   r-   r1   r   )r+   r   r   r   r   Zncharsr   )r   r   rG   *  s    


zDviFont.__init__c             C   s(   t | t |ko&| j|jko&| j|jkS )N)typer   r   )r+   otherr   r   r   __eq__8  s    zDviFont.__eq__c             C   s   | j | S )N)r   )r+   r   r   r   r   __ne__<  s    zDviFont.__ne__c             C   s   dj t| j| jS )Nz<{}: {}>)formatr   r   r   )r+   r   r   r   __repr__?  s    zDviFont.__repr__c             C   s8   | j jj|d}|dk	r$t|| jS tjd|| j dS )zWidth of char in dvi units.Nz No width for char %d in font %s.r   )r   rO   r   r   r   r<   r=   r   )r+   r{   rO   r   r   r   rz   B  s
    zDviFont._width_ofc             C   s   g }xh| j jdf| j jdffD ]L\}}|j|d}|dkrXtjd||| j |jd q|jt|| j	 qW | jdkr|dkrd|d< |S )	z&Height and depth of char in dvi units.rM   rN   NzNo %s for char %d in font %sr   s   cmsy10r   )
r   rM   rN   r   r<   r=   r   r   r   r   )r+   r{   resultZmetricnamerv   r   r   r   rg   J  s    zDviFont._height_depth_ofN)r   r   r   r   r   r   )r   r   r   r   	__slots__rG   r   r   r   rz   rg   r   r   r   r   r     s   #r   c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )Vfa  
    A virtual font (\*.vf file) containing subroutines for dvi files.

    Parameters
    ----------
    filename : str or path-like

    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.

    Examples
    --------
    ::

        vf = Vf(filename)
        glyph = vf[code]
        glyph.text, glyph.boxes, glyph.width
    c          
   C   s6   t j| |d zd | _i | _| j  W d | j  X d S )Nr   )r9   rG   _first_font_charsrT   rQ   )r+   rF   r   r   r   rG   w  s    zVf.__init__c             C   s
   | j | S )N)r   )r+   coder   r   r   rI     s    zVf.__getitem__c                s  d\}}d\}}xԈ j jdd } jtjkr j j d }||kr^ j|| d\}}}n@||krptdn.|dks|dkrtd| tj	|  | q|d	k r|} j
d j
d
 }} j|}tj _q|d	kr fdddD \}}} j| qd|  kodkn  rv j
|d	 |dk} fdddD \}}	}
}} j|||	|
||  jdkr| _q|dkrʈ j
d j
d }} j j|} j
d j
d }} j|||| q|dkrP qtd| qW dS )zt
        Read one page from the file. Return True if successful,
        False if there were no more pages.
        Nr   r   z!Packet length mismatch in vf filer   rm   r   z"Inappropriate opcode %d in vf filer   r   c                s   g | ]} j |qS r   )r   )r%   r&   )r+   r   r   r'     s    zVf._read.<locals>.<listcomp>r   r   c                s   g | ]} j |qS r   )r   )r%   r&   )r+   r   r   r'     s    r   r   zUnknown vf opcode %d)NN)NN)NNN)r   rm   )r   r   r   )r   r   r   r   r   )r?   rn   r,   rB   r   tell_finalize_packetr-   r9   ro   r   _init_packetr   r   r   )r+   packet_charZpacket_endsZ
packet_lenpacket_widthr)   Zbyte_atr   r   r   r`   r   r   r3   r&   csdsr   )r+   r   rT     sL    






zVf._readc             C   s\   | j tjkrtdd\| _| _| _| _| _| _	g g g   | _
| _| _| j| _| jj | S )NzMisplaced packet in vf filer   )r   r   r   r   r   r   )r,   rB   r   r-   rc   rr   r_   r&   r]   r   rl   rX   rY   r   r(   r?   r   )r+   plr   r   r   r     s    zVf._init_packetc             C   s(   t | j| j|d d d| j|< tj| _d S )N)rX   rY   rO   rM   rZ   )r	   rX   rY   r   rB   r   r,   )r+   r   r   r   r   r   r     s    
zVf._finalize_packetc             C   sH   | j tjk	rtd|dkr(td| t|r<tjd| tj| _ d S )Nz pre command in middle of vf file   zUnknown vf format %dzvf file comment: %s)r,   rB   rC   r-   rp   r<   r=   r   )r+   r3   r&   r   r   r   r   r   r     s    zVf._preN)
r   r   r   r   rG   rI   rT   r   r   r   r   r   r   r   r   _  s   	3r   c             C   s4   d|   kodk n  st | d@ r,| d S | S dS )	z*Convert from two's complement to negative.r   r[   r      Nl        l        l        )r2   )r   r   r   r   	_fix2comp  s    r  c             C   s   | | d? S )z1Multiply two numbers in 20.12 fixed point format.r   r   )Znum1Znum2r   r   r   r     s    r   c               @   s   e Zd ZdZd
Zdd Zd	S )Tfma  
    A TeX Font Metric file.

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

    Parameters
    ----------
    filename : str or path-like

    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   design_sizerO   rM   rN   c             C   s|  t jd| t|d}|jd}tjd|dd \}}}}}}	t jd||||||	 |jd| }
tjd	|
d d
 \| _| _|jd|| d  }|jd| }|jd| }|jd|	 }W d Q R X i i i   | _| _	| _
dd |||fD \}}}xztt||d D ]d\}}|d|  }|d| d  }t|| | j|< t||d?  | j	|< t||d@  | j
|< qW d S )Nzopening tfm file %sr;      z!6Hr[      z(lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%dr   z!2I   r   c             S   s$   g | ]}t jd t|d  |qS )z!%dIr   )structunpackrp   )r%   r&   r   r   r   r'     s   z Tfm.__init__.<locals>.<listcomp>   )r<   r=   r>   rn   r  r  r   r  rO   rM   rN   	enumerater1   r  )r+   rF   r?   Zheader1ZlhZbcZecnwZnhZndZheader2Z	char_infor   ZheightsZdepthsidxr{   Zbyte0Zbyte1r   r   r   rG     s*    
 zTfm.__init__N)r   r  rO   rM   rN   )r   r   r   r   r   rG   r   r   r   r   r    s   r  PsFontz(texname psname effects encoding filenamec               @   s2   e Zd ZdZdZe dd Zdd Zdd	 Zd
S )
PsfontsMapa}  
    A psfonts.map formatted file, mapping TeX fonts to PS fonts.

    Parameters
    ----------
    filename : str or path-like

    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.

    Examples
    --------
    >>> 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
    _font	_filenamec          	   C   s@   t j| }i |_tj||_t|d}|j| W d Q R X |S )Nr;   )object__new__r  osfsdecoder  r>   _parse)clsrF   r+   r?   r   r   r   r  F  s    
zPsfontsMap.__new__c             C   s   t |tsty| j| }W nH tk
rd   d}|j|jd| j}tj	|ddd}t
j|  Y nX |j|j }}|d k	r|jd rt|}|d k	r|jd rt|j}|j||dS )Na$  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   F)break_on_hyphensbreak_long_words   /)rF   encoding)rf   r   r2   r  KeyErrorr   r   r  textwrapfillr<   inforF   r  
startswithfind_tex_file_replace)r+   r   r   fmtmsgfnencr   r   r   rI   O  s     

zPsfontsMap.__getitem__c                s  t jd t jd}t jd} fdd|D }xH|D ]>}d\}}}|j|}	t|	}
|
jdpl|
jd	}t|	}
|
jdp|
jd	}x|	D ]}
|
jd	p|
jd}|r|}q|
jd
p|
jdp|
jdp|
jd}| r|dk	rtjd|| |}q|
jdp|
jd}qW i }xD|j|D ]6}|jd}|rHt||d< nt|jd|d< q&W t|||||d| j	|< q8W dS )a>  
        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)matchstrip)r%   line)empty_rer   r   	<genexpr>  s   z$PsfontsMap._parse.<locals>.<genexpr>    NZeff2Zeff1Zenc4Zenc3Zenc2Zenc1zMultiple encodings for %s = %sfile2file1slantr   )r   psnameeffectsr  rF   )r+  NN)
recompilefinditernextgroupr<   r=   rL   r  r  )r+   r?   Zword_reZ
effects_relinesr(  r0  r  rF   wordsr_   r   r/  Zeffr%  Zeffects_dictr&  r.  r   )r)  r   r  g  sJ    






zPsfontsMap._parseN)r  r  )	r   r   r   r   r   r   r  rI   r  r   r   r   r   r    s
   +	r  z3.3c               @   s0   e Zd ZdZd
Zdd Zdd Zedd Zd	S )Encodinga  
    Parse 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 : str or path-like

    Attributes
    ----------
    encoding : list
        List of character names
    r  c          
   C   s@   t |d,}tjd| | j|| _tjd| j W d Q R X d S )Nr;   zParsing TeX encoding %sz
Result: %s)r>   r<   r=   r  r  )r+   rF   r?   r   r   r   rG     s    zEncoding.__init__c             c   s   | j E d H  d S )N)r  )r+   r   r   r   rV     s    zEncoding.__iter__c             C   s|   dd | D }dj |}|jd}|dk r8tdj| ||d  }|jd}|dk rdtdj| |d | }tjd	|S )
Nc             s   s"   | ]}|j d dd j V  qdS )   %r   r   N)rK   r'  )r%   r(  r   r   r   r*    s    z"Encoding._parse.<locals>.<genexpr>r+     [r   z)Cannot locate beginning of encoding in {}   ]z#Cannot locate end of encoding in {}s   /([^][{}<>\s]+))r   findr-   r   r1  findall)r?   r6  dataZ	beginningendr   r   r   r    s    


zEncoding._parseN)r  )	r   r   r   r   r   rG   rV   staticmethodr  r   r   r   r   r8    s
   r8  c             C   sp   t jddt| jdd}t jd|jd}dd |j D }td	d
 |D r^dd |D S tdj	| dS )aG  
    Parses a \*.enc file referenced from a psfonts.map style file.
    The format this class understands is a very limited subset of PostScript.

    Parameters
    ----------
    path : os.PathLike

    Returns
    -------
    list
        The nth entry of the list is the PostScript glyph name of the nth
        glyph.
    z%.*r   r   )r  z(?s)\[(.*)\]r   c             S   s   g | ]}|r|qS r   r   )r%   r(  r   r   r   r'     s    z_parse_enc.<locals>.<listcomp>c             s   s   | ]}|j d V  qdS )/N)r  )r%   r(  r   r   r   r*    s    z_parse_enc.<locals>.<genexpr>c             S   s   g | ]}|d d qS )r   Nr   )r%   r(  r   r   r   r'     s    z)Failed to parse {} as Postscript encodingN)
r1  subr   Z	read_textsearchr5  rK   allr-   r   )pathZno_commentsarrayr6  r   r   r   
_parse_enc  s    rG  c             C   s   t | tr| jddd} t |tr0|jddd}tjdkrPtjddidd}ntj dd}d	g}|d
k	rz|d| g7 }|| g7 }ytj	|t
f|}W n ttfk
r   dS X |jdS )a  
    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.

    *If the file is not found, an empty string is returned*.

    Parameters
    ----------
    filename : str or path-like
    format : str or bytes
        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.
    zutf-8replace)errorsntZcommand_line_encoding)envr  Zsurrogatescape)r  rI  Z	kpsewhichNz	--format=r   
)rf   r   r   r  r   environsysgetfilesystemencodingr   Z_check_and_log_subprocessr<   r   RuntimeErrorrstrip)rF   r   kwargscmdr   r   r   r   r     s$    




r   c             C   s   t || }|r| |S d S )N)r   )r  suffixr   rF   r   r   r   	_fontfile@  s    rU  z.tfmz.vf__main__)ArgumentParserrF   r@   ?)nargsr   defaultz
pdftex.mapz=== new page ===c             C   s   | j S )N)rk   )rX   r   r   r   <lambda>W  s    r[  rk   Zscaledr[   r   .ZBOXr   )NNr]  )Ni   )Rr   collectionsr   enum	functoolsr   r   r   loggingr  Zpathlibr   r1  r  rN  r  numpyrd   Z
matplotlibr   r   	getLoggerr   r<   EnumrB   r	   r
   r   r   r   r   r   r   r   rH   r$   r8   r9   r   r   r  r   r  r  r  Z
deprecatedr8  rG  r   rU  r   r   argparserW  	itertoolsparseradd_argumentrL   
parse_argsr4   rF   r@   r   ZfontmappageprintgroupbyrX   rk   r5  r   r   r&   r]   Zglyphr   isprintablerO   rY   r_   rc   r   r   r   r   <module>   s   




		




2  M[p	4
 '74



