
 m[c           @` s0  d  Z  d d l m Z m Z m Z m Z d d l Z y d d l Z Wn e k
 rc d d l	 Z n Xd d l
 Z d d l m Z d d l m Z m Z m Z d d l m Z m Z 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$ 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/ Z0 d d l1 m2 Z3 d d l m4 Z4 y d d l5 m6 Z6 e7 Z8 Wn e k
 re9 Z8 n Xd Z: d   Z; d e f d     YZ2 d e f d     YZ< e j= d e f d     Y Z> d S(   u  
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, optionally JPEG and TIFF
 * 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   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   OrderedDict(   t   radianst   cost   sin(   t   cbookt   rcParamst   __version__(   t   _Backendt   FigureCanvasBaset   FigureManagerBaset   RendererBaset   cursors(   t   maxdict(   t   Figure(   t   findfontt   get_font(   t   LOAD_FORCE_AUTOHINTt   LOAD_NO_HINTINGt   LOAD_DEFAULTt   LOAD_NO_AUTOHINT(   t   MathTextParser(   t   Path(   t   Bboxt   BboxBase(   t   colors(   t   RendererAgg(   t   _png(   t   Imageu   v2.2c          C` s<   i t  t 6t t 6t d 6t d 6t  d 6t d 6}  |  t d S(   Nu   eitheru   nativeu   autou   noneu   text.hinting(   R   t   TrueR   t   FalseR   R   R	   (   t   mapping(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   get_hinting_flag;   s    
R   c           B` s.  e  Z d  Z e e j d  d     Z e j   Z	 d   Z
 d   Z 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(   u   
    The renderer handles all the drawing primitives using a graphics
    context instance that controls the colors/styles
    u   2.2c         C` s   d S(   Ni   (    (   t   self(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   debugM   s    c         C` s   t  j |   | |  _ | |  _ | |  _ t t |  t |  |  |  _ g  |  _ |  j	   t
 d  |  _ t j d d |  j |  j  |  _ d  S(   Nu   Aggi    (   R   t   __init__t   dpit   widtht   heightt   _RendererAggt   intt	   _renderert   _filter_rendererst   _update_methodsR   t   mathtext_parserR   t   from_boundst   bbox(   R$   R(   R)   R'   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR&   _   s    			!	
c         C` s"   i |  j  d 6|  j d 6|  j d 6S(   Nu   widthu   heightu   dpi(   R(   R)   R'   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   __getstate__m   s    c         C` s#   |  j  | d | d | d  d  S(   Nu   widthu   heightu   dpi(   R&   (   R$   t   state(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   __setstate__r   s    c         C` s   t  d r t St Sd  S(   Nu   text.hinting(   R	   R   R   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   _get_hinting_flagu   s    
c         O` s   |  j  j | |   S(   N(   R,   t   draw_markers(   R$   t   klt   kw(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR6   }   s    c         O` s   |  j  j | |   S(   N(   R,   t   draw_path_collection(   R$   R7   R8   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR9      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   get_content_extents(   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR.      s    c         C` st   |  j    } | d |  j | d | d g | d | d |  j | d g g } |  j |  } t j |  | f S(   Ni    i   i   i   (   R?   R)   R>   t   npt   array(   R$   t   extentsR1   t   region(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   tostring_rgba_minimized   s
     &c         C` s  t  d } | j j d } | d k r| | k r| j r| d k r| j   d k rt j | |  } t t j | |   } t j	 d | |  }	 t j
 |	  }
 |	 d d |
 d *| |
 d <x t |	 |
  D] \ } } | j | |  d d  f } | j } | d k	 r2| | | !} t j | d <n  t | |  } y |  j j | | | |  Wq t k
 r}t d   q Xq Wn= y |  j j | | | |  Wn t k
 rt d   n Xd S(   u   
        Draw the path
        u   agg.path.chunksizei    id   i   iNu<   Exceeded cell block limit (set 'agg.path.chunksize' rcparam)(   R	   t   verticest   shapet   should_simplifyt   Nonet	   get_hatchR@   t   ceilR+   t   aranget
   zeros_liket   zipt   codesR   t   MOVETOR,   t	   draw_patht   OverflowError(   R$   t   gct   patht	   transformt   rgbFacet   nmaxt   nptst   ncht   chsizet   i0t   i1t   ii0t   ii1t   vt   ct   p(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRP      s2    
!
	c         C` s   |  j  j | |  j |  \ } } }	 }
 } } } | t t |   } | t t |   } t j | | |  } t j | | |  } |  j j	 | | | d | |  d S(   u>   
        Draw the math text using matplotlib.mathtext
        i   N(
   R/   t   parseR'   R   R   R   R@   t   roundR,   t   draw_text_image(   R$   RR   t   xt   yt   st   propt   anglet   oxt   oyR(   R)   t   descentt
   font_imaget   used_characterst   xdt   yd(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   draw_mathtext   s    0c	         C` s]  | r" |  j  | | | | | |  St   }	 |  j |  }
 |
 d k rJ d St |  d k r t |  d k r |
 j t |  d |	 n |
 j | d d |	 |
 j d t	 d  |
 j
   d } |
 j   \ } } | d } | d } | t t |   } | t t |   } |  j j |
 t j | | |  t j | | |  d | |  d S(	   u!   
        Render the text
        i   i   t   flagsi    t   antialiasedu   text.antialiasedg      P@N(   Rp   R#   t   _get_agg_fontRH   t   lent   ordt	   load_chart   set_textt   draw_glyphs_to_bitmapR	   t   get_descentt   get_bitmap_offsetR   R   R   R,   Rc   R@   Rb   (   R$   RR   Rd   Re   Rf   Rg   Rh   t   ismatht   mtextRq   t   fontt   dt   xot   yoRn   Ro   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   draw_text   s$    	$

	c         C` s  | d k 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(   u   
        Get the width, height, and descent (offset from the bottom
        to the baseline), in display coords, of the string *s* with
        :class:`~matplotlib.font_manager.FontProperties` *prop*
        u   TeXu   TeX!t   rendererg        Rq   g      P@(   u   TeXu   TeX!(   t   get_size_in_pointst   get_texmanagert   get_text_width_height_descentR/   Ra   R'   R#   Rs   Rw   t   get_width_heightRy   (   R$   Rf   Rg   R{   t   sizet
   texmanagert   fontsizet   wt   hR~   Ri   Rj   R(   R)   Rk   t   fontsRm   Rq   R}   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR      s&    0	


u   TeX!c	         C` s   | j    }	 |  j   }
 |
 j | |	 |  j  } t j | d t j  } |  j | | |  \ } } } | t t	 |   } | t
 t	 |   } t j | |  } t j | |  } |  j j | | | | |  d  S(   Ng     o@(   R   R   t   get_greyR'   R@   RA   t   uint8R   R   R   R   Rb   R,   Rc   (   R$   RR   Rd   Re   Rf   Rg   Rh   R{   R|   R   R   t   ZR   R   R~   Rn   Ro   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   draw_tex   s    c         C` s   |  j  |  j f S(   u4   return the canvas width and height in display coords(   R(   R)   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   get_canvas_width_height  s    c         C` sE   t  |  } t |  } | j   | j   } | j | |  j  | S(   uK   
        Get the font for text instance t, cacheing for efficiency
        (   R   R   t   clearR   t   set_sizeR'   (   R$   Rg   t   fnameR}   R   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyRs     s    
c         C` s   | |  j  d S(   uj   
        convert point measures to pixes using dpi and the pixels per
        inch of the display
        g      R@(   R'   (   R$   t   points(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   points_to_pixels   s    c         C` s   |  j  j   S(   N(   R,   t   tostring_rgb(   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   '  s    c         C` s   |  j  j   S(   N(   R,   t   tostring_argb(   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   *  s    c         C` s   |  j  j   S(   N(   R,   t   buffer_rgba(   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   -  s    c         C` s   |  j  j   d  S(   N(   R,   R   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   0  s    c         C` s   t  S(   N(   R    (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   option_image_nocomposite3  s    c         C` s   t  S(   uI   
        agg backend doesn't support arbitrary scaling of image.
        (   R!   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   option_scale_image9  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 | t |  t |  t |  t |  t |  t |	   n |  j j |  d S(   u:  
        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(   RH   t   get_extentst
   isinstanceR   RB   R,   t   restore_regionR+   (
   R$   RC   R1   t   xyt   x1t   y1t   x2t   y2Ri   Rj   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   ?  s    +c         C` sK   |  j  j |  j  t t |  j  t |  j  |  j  |  _ |  j   d S(   uX   
        Start filtering. It simply create a new canvas (the old one is saved).
        N(	   R-   t   appendR,   R*   R+   R(   R)   R'   R.   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   start_filterb  s    c         C` s:  t  |  j  t  |  j  } } |  j   \ } } | \ } } } }	 |  j j   |  _ |  j   | d k r6|	 d k r6t j	 | t j
  }
 | |
 j |	 | d f  d |  j  \ }
 } } |  j   } |
 j j d k r t j |
 d t j
  }
 n  |
 d d d  }
 |  j j | | | | | |	 | |
  n  d S(   u  
        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    i   g     o@u   fNi(   R+   R(   R)   RD   R-   t   popR,   R.   R@   t
   fromstringR   t   reshapeR'   t   new_gct   dtypet   kindt   asarrayR=   (   R$   t   post_processingR(   R)   t   buffert   boundst   lt   bR   R   t   imgRi   Rj   RR   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   stop_filterk  s    
	N(%   t   __name__t
   __module__t   __doc__t   propertyR   t
   deprecatedR%   t	   threadingt   RLockt   lockR&   R2   R4   R5   R6   R9   R.   RD   RH   RP   Rp   R!   R   R   R   R   Rs   R   R   R   R   R   R   R   R   R   R   (    (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   G   s:   								#											#		t   FigureCanvasAggc           B` s   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 e r d   Z e Z d   Z e Z n  RS(   u   
    The canvas the figure renders into.  Calls the draw and print fig
    methods, creates the renderers, etc...

    Attributes
    ----------
    figure : `matplotlib.figure.Figure`
        A high-level Figure instance

    c         C` s   |  j    } | j |  S(   N(   t   get_rendererR>   (   R$   R1   R   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR>     s    c         C` s   |  j    } | j | | |  S(   N(   R   R   (   R$   RC   R1   R   R   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s    c         C` sj   |  j  d t  |  _ t j j   |  j } z* |  j j |  j  t	 t
 |   j   Wd t j j   Xd S(   u4   
        Draw the figure using the renderer
        t   clearedN(   R   R    R   R   R   t   acquiret   toolbart   figuret   drawt   superR   t   release(   R$   R   (    (    s>   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   R1   R   R'   t   _lastKeyR   t   AttributeErrorR    R   R   (   R$   R   R   R   R   R   t   keyt   need_new_renderer(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s      
c         C` s   |  j  j   S(   u   Get the image as an RGB byte string

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.

        Returns
        -------
        bytes
        (   R   R   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s    
c         C` s   |  j  j   S(   u   Get the image as an ARGB byte string

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.

        Returns
        -------
        bytes

        (   R   R   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s    c         C` s   |  j  j   S(   u   Get the image as an RGBA byte string

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.

        Returns
        -------
        bytes
        (   R   R   (   R$   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s    
c         O` s   t  j |   |  j   } | j } |  j j | _ t | t j  r[ t | d  } t	 } n | } t
 } z | j | j j    Wd  | r | j   n  | | _ Xd  S(   Nu   wb(   R   R   R   R'   R   R   t   sixt   string_typest   openR    R!   t   writeR,   R   t   close(   R$   t   filename_or_objt   argst   kwargsR   t   original_dpit   fileobjR   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_raw  s    		c   
   
   O` s   t  j |   |  j   } | j } |  j j | _ d t d } t i | d 6 } | j d d   } | d  k	 r | j	 |  n  zA t
 j | d  ) }	 t j | j |	 |  j j d | Wd  QXWd  | | _ Xd  S(   Nu   matplotlib version u   , http://matplotlib.org/u   Softwareu   metadatau   wbt   metadata(   R   R   R   R'   R   R
   R   R   RH   t   updateR   t   open_file_cmR   t	   write_pngR,   (
   R$   R   R   R   R   R   t   version_strR   t   user_metadatat   fh(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_png  s    	c         C` ss   t  j |   |  j   } | j } |  j j | _ z1 | j j   t | j  t | j	  f f } Wd  | | _ X| S(   N(
   R   R   R   R'   R   R,   R   R+   R(   R)   (   R$   R   R   t   result(    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   print_to_buffer  s    	%
c         ` s!  |  j    \ } }   j d t  r( d St j d | | d d d d  } t j t d  } t g  | d  D] } t	 | d	  ^ qj  }	 t j
 d
 | |	  }
 |
 j | |    f d   d d d d g D } | j d t d  d | k r| d | d f | d <n  |
 j | d d | S(   u  
            Other Parameters
            ----------------
            quality : int
                The image quality, on a scale from 1 (worst) to
                95 (best). The default is 95, if not given in the
                matplotlibrc file in the savefig.jpeg_quality parameter.
                Values above 95 should be avoided; 100 completely
                disables the JPEG quantization stage.

            optimize : bool
                If present, indicates that the encoder should
                make an extra pass over the image in order to select
                optimal encoder settings.

            progressive : bool
                If present, indicates that this image
                should be stored as a progressive JPEG file.
            u   dryrunNu   RGBAu   rawi    i   u   savefig.facecolori   g     o@u   RGBc         ` s)   i  |  ] } |   k r   | |  q S(    (    (   t   .0t   k(   R   (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pys
   <dictcomp>D  s   	u   qualityu   optimizeu   progressiveu   dpiu   savefig.jpeg_qualityt   formatu   jpeg(   R   R   R!   R   t
   frombuffert   mcolorst   to_rgbaR	   t   tupleR+   t   newt   pastet
   setdefaultt   save(   R$   R   R   R   t   bufR   t   imaget   rgbaRd   t   colort
   backgroundt   options(    (   R   s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_jpg&  s    !-c         O` sz   |  j    \ } } | j d t  r( d  St j d | | d d d d  } |  j j |  j j f } | j | d d d | S(	   Nu   dryrunu   RGBAu   rawi    i   R   u   tiffR'   (   R   R   R!   R   R   R   R'   R   (   R$   R   R   R   R   R   R   R'   (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt	   print_tifP  s    !N(   R   R   R   R>   RH   R   R   R!   R   R   R   R   R   t
   print_rgbaR   R   t   _has_pilR   t
   print_jpegR   t
   print_tiff(    (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR     s"   
									'	t   _BackendAggc           B` s   e  Z e Z e Z RS(    (   R   R   R   t   FigureCanvasR   t   FigureManager(    (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyR   [  s   (?   R   t
   __future__R    R   R   R   R   R   t   ImportErrort   dummy_threadingt   numpyR@   t   collectionsR   t   mathR   R   R   t
   matplotlibR   R	   R
   t   matplotlib.backend_basesR   R   R   R   R   t   matplotlib.cbookR   t   matplotlib.figureR   t   matplotlib.font_managerR   R   t   matplotlib.ft2fontR   R   R   R   t   matplotlib.mathtextR   t   matplotlib.pathR   t   matplotlib.transformsR   R   R   R   t    matplotlib.backends._backend_aggR   R*   R   t   PILR   R    R   R!   t   backend_versionR#   R   t   exportR   (    (    (    s>   lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyt   <module>   sB   "("

	 O	