ó
±éRc           @  s¦   d  Z  d d l m Z d d l j 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 e j d d k rŒ d „  Z n  d e f d „  ƒ  YZ d S(	   sª  
This module contains a class representing a Type 1 font.

This version reads pfa and pfb files and splits them for embedding in
pdf files. It also supports SlantFont and ExtendFont transformations,
similarly to pdfTeX and friends. There is no support yet for
subsetting.

Usage::

   >>> font = Type1Font(filename)
   >>> clear_part, encrypted_part, finale = font.parts
   >>> slanted_font = font.transform({'slant': 0.167})
   >>> extended_font = font.transform({'extend': 1.2})

Sources:

* Adobe Technical Note #5040, Supporting Downloadable PostScript
  Language Fonts.

* Adobe Type 1 Font Format, Adobe Systems Incorporated, third printing,
  v1.1, 1993. ISBN 0-201-57044-0.
iÿÿÿÿ(   t   print_functionNi    i   c         C  s   |  S(   N(    (   t   x(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   ord#   s    t	   Type1Fontc           B  s›   e  Z d  Z d Z d „  Z d „  Z d „  Z e j d ƒ Z	 e j d ƒ Z
 e j d ƒ Z e j d	 ƒ Z e d
 „  ƒ Z d „  Z e d „  ƒ Z d „  Z RS(   sû   
    A class representing a Type-1 font, for use by backends.

    .. attribute:: parts

       A 3-tuple of the cleartext part, the encrypted part, and the
       finale of zeros.

    .. attribute:: prop

       A dictionary of font properties.
    t   partst   propc         C  st   t  | t ƒ r- t | ƒ d k r- | |  _ n9 t | d ƒ � } |  j | ƒ } Wd QX|  j | ƒ |  _ |  j ƒ  d S(   s˜   
        Initialize a Type-1 font. *input* can be either the file name of
        a pfb file or a 3-tuple of already-decoded Type-1 font parts.
        i   t   rbN(   t
   isinstancet   tuplet   lenR   t   opent   _readt   _splitt   _parse(   t   selft   inputt   filet   data(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   __init__6   s    !c         C  sI  | j  ƒ  } | j d ƒ s | Sd } xt | ƒ d k rD| j d ƒ sf t d t | d ƒ ƒ ‚ n  t | d ƒ } | d k rÀ t j d | d d !ƒ \ } | d d | !} | d | } n  | d k rÙ | | 7} q( | d k r!| d j g  | D] } d	 t | ƒ j d
 ƒ ^ qõ ƒ 7} q( | d k r1Pq( t d | ƒ ‚ q( W| S(   sH   
        Read the font from a file, decoding into usable parts.
        s   €t    i    s+   Broken pfb file (expected byte 128, got %d)i   i   s   <ii   s   %02xt   asciii   s#   Unknown segment type %d in pfb file(   i   i   (	   t   readt
   startswithR	   t   RuntimeErrorR   t   structt   unpackt   joint   encode(   R   R   t   rawdataR   t   typet   lengtht   segmentt   char(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyR   D   s.    0c      	   C  sK  | j  d ƒ } | t d ƒ 7} x | | d k r? | d 7} q" W| } | j d ƒ d } d } xn | rÏ t | | ƒ t d ƒ t d ƒ t d ƒ f k rÏ t | | ƒ t d ƒ k rÂ | d 8} n  | d 8} qb W| rå t d
 ƒ ‚ n  d j g  t | | d ƒ D]/ } t t | | | d !d ƒ ƒ j	 d ƒ ^ qþ ƒ } | |  | | | f S(   sS  
        Split the Type 1 font into its three main parts.

        The three parts are: (1) the cleartext part, which ends in a
        eexec operator; (2) the encrypted part; (3) the fixed part,
        which contains 512 ASCII zeros possibly divided on various
        lines, a cleartomark operator, and possibly something else.
        t   eexecs    	
i   t   cleartomarki   t   0i    s   
s   s(   Insufficiently many zeros in Type 1 fontR   i   i   s   latin-1R#   t   
t   R#   (
   t   indexR	   t   rindexR   R   R   t   ranget   unichrt   intR   (   R   R   t   idxt   len1t   zerost   it   binary(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyR   d   s"    $	Hs   [\0\t\r\014\n ]+s   /{0,2}[^]\0\t\r\v\n ()<>{}/%[]+s   %[^\r\n\v]*s   [()\\]c         c  sM  d } x@| t  | ƒ k  rH|  j j | | ƒ pD |  j j | | ƒ } | rq d | j ƒ  f V| | j ƒ  7} q	 | | d k r7| } | d 7} d } xˆ | r!|  j j | | ƒ } | d k rÆ d S| | j ƒ  7} | j ƒ  d k rõ | d 7} qš | j ƒ  d k r| d 8} qš | d 7} qš Wd | | | !f Vq	 | | | d !d k rqd | | | d !f V| d 7} q	 | | d k r³| } | | | j	 d ƒ 7} d | | | !f Vq	 |  j
 j | | ƒ } | r,y% t | j ƒ  ƒ d | j ƒ  f VWn" t k
 rd | j ƒ  f Vn X| | j ƒ  7} q	 d | | f V| d 7} q	 Wd S(   s‚   
        A PostScript tokenizer. Yield (token, value) pairs such as
        ('whitespace', '   ') or ('name', '/Foobar').
        i    t
   whitespacet   (i   Nt   )t   stringi   s   <<s   >>t	   delimitert   <t   >t   numbert   name(   s   <<s   >>(   R	   t   _commentt   matcht   _whitespacet   groupt   endt	   _instringt   searcht   NoneR&   t   _tokent   floatt
   ValueError(   t   clst   textt   posR:   t   startt   depth(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   _tokens�   sN    
	c         C  sù  i d d 6d d 6t  d 6d d 6d d	 6} |  j |  j d
 ƒ } t j d „  | ƒ } x| D]\ } } | d k r[ | j d ƒ r[ | d } t | ƒ \ } } | d k r× | d k rÅ | d k } q:| j d ƒ } nc | d k rþ | j d ƒ j d ƒ } n< | d k r4d | k r%t	 | ƒ } q:t
 | ƒ } n d } | d k rb| d k	 rb| | | <qbq[ q[ Wd | k r�| j d ƒ p“| j d ƒ p“d | d <n  d | k rº| d | d <n  d | k rìd } t j | d | d ƒ | d <n  | |  _ d S(   s¼   
        Find the values of various font properties. This limited kind
        of parsing is described in Chapter 10 "Adobe Type Manager
        Compatibility" of the Type-1 spec.
        t   Regulart   weightg        t   ItalicAnglet   isFixedPitchiœÿÿÿt   UnderlinePositioni2   t   UnderlineThicknessi    c         S  s   |  d d k S(   Ni    R0   (    (   R   (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   <lambda>Ë   s    R8   t   /i   t   truet   falseR3   R1   R2   R7   t   .t   FontInfot   FontNamet   FullNamet
   FamilyNamet   UnknownsT   (?i)([ -](regular|plain|italic|oblique|(semi)?bold|(ultra)?light|extra|condensed))+$R   N(   s   trues   false(   t   FalseRI   R   t	   itertoolst   ifilterR   t   nextt   lstript   rstripRB   R*   R@   t   gett   ret   subR   (   R   R   t	   tokenizert   filteredt   tokent   valuet   keyt   extras(    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyR   Á   s>    
 c         #  sñ   ‡  ‡ f d †  } ‡ f d †  } ‡  ‡ f d †  } d „  } d „  } i | | ƒ d 6| | ƒ d 6| | ƒ d 6| d	 6}	 xq t  rì t | ƒ \ }
 } |
 d
 k rä | |	 k rä x: |	 | t j |
 | f g | ƒ ƒ D] } | VqÒ Wq| | Vq| Wd  S(   Nc           s^   |  } ˆ r- | d t  t d ˆ ƒ ƒ 7} n  ˆ  d k rZ | d t  t d ˆ  ƒ ƒ 7} n  | S(   Nt   _Slant_iè  g      ð?t   _Extend_(   t   strR*   (   R8   t   result(   t   extendt   slant(    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   fontnameð   s    !!c           s(   t  t |  ƒ t j ˆ  ƒ t j d ƒ S(   Ni´   (   Rk   RB   t   npt   arctant   pi(   t   angle(   Rn   (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   italicangleø   s    c           sE  |  j  d ƒ j d ƒ j ƒ  j ƒ  }  g  |  D] } t | ƒ ^ q+ }  t j d d ƒ } |  d  d  d … | d d … d f <|  d d  d … | d d … d f <t j ˆ  d d g ˆ d d g d d d g g ƒ } t j | | ƒ } | d d … d f |  d  d  d … <| d d … d f |  d d  d … <d d j	 d „  |  Dƒ ƒ d S(	   Nt   [t   ]i   i   i    i   t    c         s  s   |  ] } t  | ƒ Vq d  S(   N(   Rk   (   t   .0R   (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pys	   <genexpr>  s    (
   R^   R_   t   stript   splitRB   Rp   t   eyet   arrayt   dotR   (   R|   R   t	   oldmatrixt   modifiert	   newmatrix(   Rm   Rn   (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt
   fontmatrixû   s    $####c           s   ‡  f d †  } | S(   Nc         3  sÄ   t  |  ƒ \ } } | Vt  |  ƒ \ } } x' | d k rR | Vt  |  ƒ \ } } q, W| d k rm ˆ  | ƒ VnS g  } x, | d k r¡ | | 7} t  |  ƒ \ } } qv W| | 7} ˆ  d j | ƒ ƒ Vd  S(   NR0   Ru   Rv   R   (   R]   R   (   t   tokensRe   Rf   R|   (   t   fun(    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   replacer
  s    

(    (   Rƒ   R„   (    (   Rƒ   sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   replace	  s    c         s  s)   x t  j d „  |  ƒ D] } q Wd Vd  S(   Nc         S  s   |  d d k S(   Ni   t   def(    (   R   (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyRP     s    R   (   R[   t	   takewhile(   R‚   R   (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   suppress  s    s	   /FontNames   /ItalicAngles   /FontMatrixs	   /UniqueIDR8   (   t   TrueR]   R[   t   chain(   RD   R‚   Rn   Rm   Ro   Rt   R�   R…   Rˆ   t   tableRe   Rf   (    (   Rm   Rn   sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   _transformerî   s     		
	c         C  sæ   t  j ƒ  } z« |  j |  j d ƒ } x‚ |  j | d | j d d ƒ d | j d d ƒ ƒD]M } t j d d k r™ t | t	 ƒ r™ t
 | ƒ j d ƒ } n  | j | ƒ qY W| j ƒ  } Wd | j ƒ  Xt | |  j d	 |  j d
 f ƒ S(   s¨  
        Transform the font by slanting or extending. *effects* should
        be a dict where ``effects['slant']`` is the tangent of the
        angle that the font is to be slanted to the right (so negative
        values slant to the left) and ``effects['extend']`` is the
        multiplier by which the font is to be extended (so values less
        than 1.0 condense). Returns a new :class:`Type1Font` object.
        i    Rn   g        Rm   g      ð?i   s   latin-1Ni   i   (   t   iot   BytesIORI   R   RŒ   R`   t   syst   version_infoR   R*   t   chrR   t   writet   getvaluet   closeR   (   R   t   effectst   bufferRc   Rf   Rl   (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt	   transform/  s    	"(   s   partss   prop(   t   __name__t
   __module__t   __doc__t	   __slots__R   R   R   Ra   t   compileR;   RA   R9   R>   t   classmethodRI   R   RŒ   R—   (    (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyR   '   s   		 	'1	-A(   Rš   t
   __future__R    t   matplotlib.cbookt   cbookR�   R[   t   numpyRp   Ra   R   R�   R�   R   t   objectR   (    (    (    sN   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/type1font.pyt   <module>   s   