ó
±éRc           @   se  d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l	 m
 Z
 m Z m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z m Z m Z m Z m Z d d
 l m Z d d l m Z d d l m Z m  Z  d d l! m" Z# d d l m$ Z$ d Z% d „  Z& d e
 f d „  ƒ  YZ" d „  Z' d „  Z( d e f d „  ƒ  YZ) d S(   s‰  
An agg http://antigrain.com/ backend

Features that are implemented

 * capstyles and join styles
 * dashes
 * linewidth
 * lines, rectangles, ellipses
 * clipping to a rectangle
 * output to RGBA and PNG
 * alpha blending
 * DPI scaling properly - everything scales properly (dashes, linewidths, etc)
 * draw polygon
 * freetype2 w/ ft2font

TODO:

  * integrate screen dpi w/ ppi and text

iÿÿÿÿ(   t   divisionN(   t   verboset   rcParams(   t   RendererBaset   FigureManagerBaset   FigureCanvasBase(   t   is_string_liket   maxdict(   t   Figure(   t   findfont(   t   FT2Fontt   LOAD_FORCE_AUTOHINTt   LOAD_NO_HINTINGt   LOAD_DEFAULTt   LOAD_NO_AUTOHINT(   t   MathTextParser(   t   Path(   t   Bboxt   BboxBase(   t   RendererAgg(   t   _pngs   v2.2c          C   s<   i t  t 6t t 6t d 6t d 6t  d 6t d 6}  |  t d S(   Nt   eithert   nativet   autot   nones   text.hinting(   R   t   TrueR   t   FalseR   R   R   (   t   mapping(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   get_hinting_flag+   s    
R   c           B   s  e  Z d  Z d Z e j ƒ  Z e d ƒ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d d „ Z d	 „  Z e d d
 „ Z d „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z d „  Z  RS(   s   
    The renderer handles all the drawing primitives using a graphics
    context instance that controls the colors/styles
    i   i2   c         C   sï   t  j d d ƒ t j |  ƒ t d ƒ |  _ | |  _ | |  _ | |  _ t  j d | | f d ƒ t	 t
 | ƒ t
 | ƒ | d t ƒ|  _ g  |  _ t  j d d ƒ |  j ƒ  t d ƒ |  _ t j d d |  j |  j ƒ |  _ t  j d	 d ƒ d  S(
   Ns   RendererAgg.__init__s   debug-annoyingi2   s(   RendererAgg.__init__ width=%s, height=%st   debugs&   RendererAgg.__init__ _RendererAgg donet   Aggi    s   RendererAgg.__init__ done(   R   t   reportR   t   __init__R   t   texdt   dpit   widtht   heightt   _RendererAggt   intR   t	   _renderert   _filter_rendererst   _update_methodsR   t   mathtext_parserR   t   from_boundst   bbox(   t   selfR#   R$   R"   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR    K   s     			'		
!	c         C   s   t  d r t St Sd  S(   Ns   text.hinting(   R   R   R   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   _get_hinting_flaga   s    
c         O   s   |  j  j | | Ž  S(   N(   R'   t   draw_markers(   R-   t   klt   kw(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR/   i   s    c         O   s   |  j  j | | Ž  S(   N(   R'   t   draw_path_collection(   R-   R0   R1   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR2   l   s    c         C   s^   |  j  j |  _ |  j  j |  _ |  j  j |  _ |  j  j |  _ |  j  j |  _ |  j  j |  _ d  S(   N(   R'   t   draw_quad_mesht   draw_gouraud_trianglet   draw_gouraud_trianglest
   draw_imaget   copy_from_bboxt   tostring_rgba_minimized(   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR)   o   s    c         C   s„  t  d } | j j d } | d k rg| | k rg| j rg| d k rg| j ƒ  d k rgt j | t | ƒ ƒ } t	 t j | | ƒ ƒ } t j
 d | | ƒ }	 t j |	 ƒ }
 |	 d d |
 d *| |
 d <x² t |	 |
 ƒ D]… \ } } | j | | … d d … f } | j } | d k	 r8| | | !} t j | d <n  t | | ƒ } |  j j | | | | ƒ qÛ Wn |  j j | | | | ƒ d S(   s   
        Draw the path
        s   agg.path.chunksizei    id   i   iÿÿÿÿN(   R   t   verticest   shapet   should_simplifyt   Nonet	   get_hatcht   npt   ceilt   floatR&   t   aranget
   zeros_liket   zipt   codesR   t   MOVETOR'   t	   draw_path(   R-   t   gct   patht	   transformt   rgbFacet   nmaxt   nptst   ncht   chsizet   i0t   i1t   ii0t   ii1t   vt   ct   p(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRF   z   s&    
!
	 c         C   sÊ   t  j d d ƒ |  j j | |  j | ƒ \ } } }	 }
 } } } | t j t j | ƒ ƒ } | t j t j | ƒ ƒ } t j	 | | | ƒ } t j	 | | | ƒ } |  j
 j | | | d | | ƒ d S(   s>   
        Draw the math text using matplotlib.mathtext
        s   RendererAgg.draw_mathtexts   debug-annoyingi   N(   R   R   R*   t   parseR"   R>   t   sint   deg2radt   cost   roundR'   t   draw_text_image(   R-   RG   t   xt   yt   st   propt   anglet   oxt   oyR#   R$   t   descentt
   font_imaget   used_characterst   xdt   yd(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   draw_mathtext“   s    	0c	         C   sQ  t  j d d ƒ | r2 |  j | | | | | | ƒ St ƒ  }	 |  j | ƒ }
 |
 d
 k rZ d
 St | ƒ d k rš t | ƒ d k rš |
 j t | ƒ d |	 ƒn |
 j	 | d d |	 ƒ|
 j
 d t d ƒ |
 j ƒ  d	 } | t j t j | ƒ ƒ } | t j t j | ƒ ƒ } |  j j |
 j ƒ  t j | | ƒ t j | | ƒ d | | ƒ d
 S(   s!   
        Render the text
        s   RendererAgg.draw_texts   debug-annoyingi   i   t   flagsi    t   antialiaseds   text.antialiasedg      P@N(   R   R   Rh   R   t   _get_agg_fontR<   t   lent   ordt	   load_chart   set_textt   draw_glyphs_to_bitmapR   t   get_descentR>   RW   RX   RY   R'   R[   t	   get_imageRZ   (   R-   RG   R\   R]   R^   R_   R`   t   ismatht   mtextRi   t   fontt   dRf   Rg   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   draw_text¢   s     	 $	c         C   s  t  d r\ | j ƒ  } |  j ƒ  } | j ƒ  } | j | | d |  ƒ\ } } }	 | | |	 f S| rŸ |  j j | |  j | ƒ \ }
 } } } } } } | | | f St ƒ  } |  j | ƒ } | j	 | d d | ƒ| j
 ƒ  \ } } | j ƒ  }	 | d } | d } |	 d }	 | | |	 f S(   s  
        get the width and height in display coords of the string s
        with FontPropertry prop

        # passing rgb is a little hack to make cacheing in the
        # texmanager more efficient.  It is not meant to be used
        # outside the backend
        s   text.usetext   rendererg        Ri   g      P@(   R   t   get_size_in_pointst   get_texmanagert   get_text_width_height_descentR*   RV   R"   R   Rk   Ro   t   get_width_heightRq   (   R-   R^   R_   Rs   t   sizet
   texmanagert   fontsizet   wt   hRv   Ra   Rb   R#   R$   Rc   t   fontsRe   Ri   Ru   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR{   ¾   s&    	
0	


s   TeX!c	         C   s$  | j  ƒ  }	 |  j ƒ  }
 | |	 |  j | |
 j ƒ  f } |  j j | ƒ } | d  k rˆ |
 j | |	 |  j ƒ } t j	 | d t j
 ƒ } n  |  j | | | ƒ \ } } } | t j t j | ƒ ƒ } | t j t j | ƒ ƒ } t j | | ƒ } t j | | ƒ } |  j j | | | | | ƒ d  S(   Ng     ào@(   Ry   Rz   R"   t   get_font_configR!   t   getR<   t   get_greyR>   t   arrayt   uint8R{   RW   RX   RY   RZ   R'   R[   (   R-   RG   R\   R]   R^   R_   R`   Rs   Rt   R}   R~   t   keyt   imt   ZR€   R�   Rv   Rf   Rg   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   draw_texà   s    c         C   s   |  j  |  j f S(   s4   return the canvas width and height in display coords(   R#   R$   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   get_canvas_width_heightó   s    c         C   sÍ   t  j d d ƒ t | ƒ } t j j | ƒ } | d k r  t | ƒ } t j j | ƒ } | d k r� t t	 | ƒ d t
 d ƒ} | t j | <n  | t j | <n  | j ƒ  | j ƒ  } | j | |  j ƒ | S(   sK   
        Get the font for text instance t, cacheing for efficiency
        s   RendererAgg._get_agg_fonts   debug-annoyingt   hinting_factors   text.hinting_factorN(   R   R   t   hashR   t   _fontdR„   R<   R	   R
   t   strR   t   clearRy   t   set_sizeR"   (   R-   R_   Rˆ   Ru   t   fnameR}   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRk   ÷   s"    	
c         C   s   t  j d d ƒ | |  j d S(   sj   
        convert point measures to pixes using dpi and the pixels per
        inch of the display
        s   RendererAgg.points_to_pixelss   debug-annoyingg      R@(   R   R   R"   (   R-   t   points(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   points_to_pixels  s    	c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   RendererAgg.tostring_rgbs   debug-annoying(   R   R   R'   t   tostring_rgb(   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR–     s    	c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   RendererAgg.tostring_argbs   debug-annoying(   R   R   R'   t   tostring_argb(   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR—     s    	c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   RendererAgg.buffer_rgbas   debug-annoying(   R   R   R'   t   buffer_rgba(   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR˜   $  s    	c         C   s   |  j  j ƒ  d  S(   N(   R'   R‘   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR‘   )  s    c         C   s   t  S(   N(   R   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   option_image_nocomposite,  s    c         C   s   t  S(   sA   
        agg backend support arbitrary scaling of image.
        (   R   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   option_scale_image2  s    c   
      C   sÙ   | d k	 s | d k	 rÅ | d k r? | j ƒ  \ } } } } n9 t | t ƒ rf | j \ } } } } n | \ } } } } | d k r” | | } }	 n | \ } }	 |  j j | | | | | | |	 ƒ n |  j j | ƒ d S(   s:  
        Restore the saved region. If bbox (instance of BboxBase, or
        its extents) is given, only the region specified by the bbox
        will be restored. *xy* (a tuple of two floasts) optionally
        specifies the new position (the LLC of the original region,
        not the LLC of the bbox) where the region will be restored.

        >>> region = renderer.copy_from_bbox()
        >>> x1, y1, x2, y2 = region.get_extents()
        >>> renderer.restore_region(region, bbox=(x1+dx, y1, x2, y2),
        ...                         xy=(x1-dx, y1))

        N(   R<   t   get_extentst
   isinstanceR   t   extentsR'   t   restore_region2t   restore_region(
   R-   t   regionR,   t   xyt   x1t   y1t   x2t   y2Ra   Rb   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRŸ   8  s    %c         C   sK   |  j  j |  j ƒ t t |  j ƒ t |  j ƒ |  j ƒ |  _ |  j ƒ  d S(   sX   
        Start filtering. It simply create a new canvas (the old one is saved).
        N(	   R(   t   appendR'   R%   R&   R#   R$   R"   R)   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   start_filterX  s    c         C   s%  d d l  m } t |  j ƒ t |  j ƒ } } |  j j ƒ  \ } } | \ } } }	 }
 |  j j ƒ  |  _ |  j	 ƒ  |	 d k r!|
 d k r!t
 j | t
 j ƒ } | | j |
 |	 d f ƒ d |  j ƒ \ } } } | | d ƒ } | j ƒ  |  j ƒ  } |  j j | | | | | |
 | | ƒ n  d S(   sŸ  
        Save the plot in the current canvas as a image and apply
        the *post_processing* function.

           def post_processing(image, dpi):
             # ny, nx, depth = image.shape
             # image (numpy array) has RGBA channels and has a depth of 4.
             ...
             # create a new_image (numpy array of 4 channels, size can be
             # different). The resulting image may have offsets from
             # lower-left corner of the original image
             return new_image, offset_x, offset_y

        The saved renderer is restored and the returned image from
        post_processing is plotted (using draw_image) on it.
        iÿÿÿÿ(   t	   fromarrayi    i   g     ào@i   N(   t   matplotlib._imageR¨   R&   R#   R$   R'   R8   R(   t   popR)   R>   t
   fromstringR‡   t   reshapeR"   t
   flipud_outt   new_gcR6   (   R-   t   post_processingR¨   R#   R$   t   buffert   boundst   lt   bR€   R�   t   imgRa   Rb   t   imageRG   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   stop_filtera  s     

N(!   t   __name__t
   __module__t   __doc__R   t	   threadingt   RLockt   lockR   R�   R    R.   R/   R2   R)   R<   RF   Rh   R   Rw   R{   R‹   RŒ   Rk   R•   R–   R—   R˜   R‘   R™   Rš   RŸ   R§   R¶   (    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   7   s4   							"										 		c         O   s>   t  j d d ƒ | j d t ƒ } | | | Ž  } t |  | ƒ S(   s.   
    Create a new figure manager instance
    s   backend_agg.new_figure_managers   debug-annoyingt   FigureClass(   R   R   Rª   R   t   new_figure_manager_given_figure(   t   numt   argst   kwargsR½   t   thisFig(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   new_figure_manager�  s
    	c         C   s   t  | ƒ } t | |  ƒ } | S(   sD   
    Create a new figure manager instance for the given figure.
    (   t   FigureCanvasAggR   (   R¿   t   figuret   canvast   manager(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR¾   �  s    RÄ   c           B   sw   e  Z d  Z d „  Z d d d „ Z d „  Z e d „ Z d „  Z	 d „  Z
 d „  Z d „  Z e Z d	 „  Z d
 „  Z RS(   s®   
    The canvas the figure renders into.  Calls the draw and print fig
    methods, creates the renderers, etc...

    Public attribute

      figure - A Figure instance
    c         C   s   |  j  ƒ  } | j | ƒ S(   N(   t   get_rendererR7   (   R-   R,   Rx   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR7   °  s    c         C   s   |  j  ƒ  } | j | | | ƒ S(   N(   RÈ   RŸ   (   R-   R    R,   R¡   Rx   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRŸ   ´  s    c         C   s^   t  j d d ƒ |  j d t ƒ |  _ t j j ƒ  z |  j j	 |  j ƒ Wd t j j
 ƒ  Xd S(   s4   
        Draw the figure using the renderer
        s   FigureCanvasAgg.draws   debug-annoyingt   clearedN(   R   R   RÈ   R   Rx   R   R¼   t   acquireRÅ   t   drawt   release(   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRË   ¸  s    c         C   s·   |  j  j j \ } } } } | | |  j  j f } y |  j |  j f Wn t k
 r] t } n X|  j | k } | rš t | | |  j  j ƒ |  _ | |  _ n | r° |  j j	 ƒ  n  |  j S(   N(
   RÅ   R,   R±   R"   t   _lastKeyRx   t   AttributeErrorR   R   R‘   (   R-   RÉ   R²   R³   R€   R�   Rˆ   t   need_new_renderer(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRÈ   É  s      
c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   FigureCanvasAgg.tostring_rgbs   debug-annoying(   R   R   Rx   R–   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR–   ×  s    	c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   FigureCanvasAgg.tostring_argbs   debug-annoying(   R   R   Rx   R—   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR—   Ü  s    	c         C   s   t  j d d ƒ |  j j ƒ  S(   Ns   FigureCanvasAgg.buffer_rgbas   debug-annoying(   R   R   Rx   R˜   (   R-   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR˜   á  s    	c         O   s“   t  j |  ƒ |  j ƒ  } | j } |  j j | _ t | ƒ rU t | d ƒ } t } n t } z | j	 j
 | ƒ Wd  | r… | j ƒ  n  X| | _ d  S(   Nt   wb(   RÄ   RË   RÈ   R"   RÅ   R   t   openR   R   R'   t
   write_rgbat   close(   R-   t   filename_or_objRÀ   RÁ   Rx   t   original_dpiRÓ   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_rawæ  s    		c         O   s±   t  j |  ƒ |  j ƒ  } | j } |  j j | _ t | ƒ rU t | d ƒ } t } n t } z2 t	 j
 | j j ƒ  | j | j | |  j j ƒ Wd  | r£ | j ƒ  n  X| | _ d  S(   NRÐ   (   RÄ   RË   RÈ   R"   RÅ   R   RÑ   R   R   R   t	   write_pngR'   R˜   R#   R$   RÓ   (   R-   RÔ   RÀ   RÁ   Rx   RÕ   RÓ   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_pngø  s    		c         C   sk   t  j |  ƒ |  j ƒ  } | j } |  j j | _ | j j ƒ  t | j ƒ t | j	 ƒ f f } | | _ | S(   N(
   RÄ   RË   RÈ   R"   RÅ   R'   R˜   R&   R#   R$   (   R-   Rx   RÕ   t   result(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   print_to_buffer  s    	!	N(   R·   R¸   R¹   R7   R<   RŸ   RË   R   RÈ   R–   R—   R˜   RÖ   t
   print_rgbaRØ   RÚ   (    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRÄ   ¦  s   							(*   R¹   t
   __future__R    Rº   t   numpyR>   t
   matplotlibR   R   t   matplotlib.backend_basesR   R   R   t   matplotlib.cbookR   R   t   matplotlib.figureR   t   matplotlib.font_managerR	   t   matplotlib.ft2fontR
   R   R   R   R   t   matplotlib.mathtextR   t   matplotlib.pathR   t   matplotlib.transformsR   R   t    matplotlib.backends._backend_aggR   R%   R   t   backend_versionR   RÃ   R¾   RÄ   (    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   <module>   s*   (	ÿ Z			