
 m[c           @` s  d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z y d d l Z WnG e k
 r y d d l Z Wn e k
 r e d   n Xe Z n Xe Z e j d d d f k  r e d j e j    n  e j 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! d d l" m# Z# d f  d     YZ$ d e f d     YZ% d e f d     YZ& d e f d     YZ' e j( d e f d     Y Z) d S(   u   
A Cairo backend for matplotlib
==============================
:Author: Steve Chaplin and others

This backend depends on `cairo <http://cairographics.org>`_, and either on
cairocffi, or (Python 2 only) on pycairo.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNu=   cairo backend requires that cairocffi or pycairo is installedi   i   u/   cairo {} is installed; cairo>=1.4.0 is required(   t   _Backendt   FigureCanvasBaset   FigureManagerBaset   GraphicsContextBaset   RendererBase(   t   MathTextParser(   t   Path(   t   Affine2D(   t   ttfFontPropertyt   ArrayWrapperc           B` s    e  Z d  Z d   Z d   Z RS(   u   Thin wrapper around numpy ndarray to expose the interface
       expected by cairocffi. Basically replicates the
       array.array interface.
    c         C` s=   | |  _  | j j |  _ t | j    |  _ | j |  _ d  S(   N(   t   _ArrayWrapper__arrayt   ctypest   datat   _ArrayWrapper__datat   lent   flattent   _ArrayWrapper__sizet   itemsize(   t   selft   myarray(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   __init__7   s    	c         C` s   |  j  |  j f S(   N(   R   R   (   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   buffer_info=   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR   2   s   	t   RendererCairoc           B` s  e  Z i e j d  6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d	 6e j d
 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6Z i e j d 6e j d 6e j d 6Z	 d   Z
 d   Z d   Z d   Z e d# d   Z d# d  Z d# d  Z d   Z e d# d  Z d   Z d   Z d    Z d!   Z d"   Z RS($   id   i   i,  i  i  iX  i  i   i  u
   ultralightu   lightu   normalu   mediumu   regularu   semiboldu   boldu   heavyu	   ultraboldu   blacku   italicu   obliquec         C` s_   | |  _  t d |   |  _ t j t j t j d d   |  _ t d  |  _	 t
 j |   d  S(   Nt   rendereri   u   Cairo(   t   dpit   GraphicsContextCairot   gct   cairot   Contextt   ImageSurfacet   FORMAT_ARGB32t   text_ctxR	   t   mathtext_parserR   R   (   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR   ^   s    	c         C` s   t  j |  |  j _ d  S(   N(   R"   R#   R!   t   ctx(   R   t   surface(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_ctx_from_surfacef   s    c         C` s   | |  _  | |  _ d  S(   N(   t   widtht   height(   R   R+   R,   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_width_heightm   s    	c         C` s   | d  k	 r | j   t |  d k s. | rS | j | d | d | d |  n& | j | d | d | d | d  | j   | j   n  | j   d  S(   Ni   i    i   i   (   t   Nonet   saveR   t   set_source_rgbat   fill_preservet   restoret   stroke(   R   R(   t   fill_ct   alphat   alpha_overrides(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   _fill_and_strokeq   s    
%&
c      	   C` s   x | j  | d | D] \ } } | t j k rA |  j |   q | t j k r] |  j   q | t j k r| |  j |   q | t j k r |  j	 | d | d | d | d | d | d  q | t j
 k r |  j	 |   q q Wd  S(   Nt   clipi    i   i   i   (   t   iter_segmentsR
   t   MOVETOt   move_tot	   CLOSEPOLYt
   close_patht   LINETOt   line_tot   CURVE3t   curve_tot   CURVE4(   R(   t   patht	   transformR8   t   pointst   code(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   convert_path|   s    "c         C` s   | j  } | d  k r6 | j   d  k r6 | j   } n d  } | t   j d d  j d |  j  } | j   |  j	 | | | |  |  j
 | | | j   | j    d  S(   Ng      ?g      i    (   R(   R.   t	   get_hatcht   clip_extentsR   t   scalet	   translateR,   t   new_pathRG   R7   t	   get_alphat   get_forced_alpha(   R   R!   RC   RD   t   rgbFaceR(   R8   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt	   draw_path   s    	%
c         C` s  | j  } | j   |  j | | | t   j d d   | j   } | j   \ } }	 }
 } | d k r |	 d k r |
 d k r | d k r t } d  } n t	 } | t   j d d  j
 d |  j  } | j   x t | j | d t  D] \ } \ } } t |  r | d \ } } | j   | j
 | |  | j |  | j   | se| d d k r|  j | | | j   | j    qq q W| s|  j | | | j   | j    n  d  S(   Ng      ?g      i    t   simplifyii  (   R(   RL   RG   R   RJ   t   copy_path_flatt   fill_extentst   FalseR.   t   TrueRK   R,   t	   enumerateR9   R   R/   t   append_pathR2   R7   RM   RN   (   R   R!   t   marker_patht   marker_transRC   RD   RO   R(   t   x1t   y1t   x2t   y2t   filledt   it   verticest   codest   xt   y(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   draw_markers   s6    	
 0	%
(

&c         C` s;  t  j d k r1 | d  d   d  d   d f } n | d  d   d  d   d	 f } t rk t | j    } n | j   } t j j | t j | j	 d | j	 d | j	 d d  } | j
 } |  j | | j	 d } | j   | j | t |  t |   | j   d k r#| j | j    n
 | j   | j   d  S(
   Nu   littlei   i   i    i   i   g      ?(   i   i   i    i   (   i   i    i   i   (   t   syst	   byteordert   HAS_CAIRO_CFFIR   R   R"   R$   t   create_for_dataR%   t   shapeR(   R,   R/   t   set_source_surfacet   floatRM   t   paint_with_alphat   paintR2   (   R   R!   Rb   Rc   t   imt   imbufferR)   R(   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt
   draw_image   s"    "		(	

c	         C` s>  | r% |  j  | | | | | |  n| j }	 |	 j   |	 j | |  |	 j | j   |  j | j   |  j | j	    | j
   |  j d }
 |	 j   | r |	 j t j |   n  |	 j |
  t r t | t j  s#t j |  } q#n- t j r#t | t j  r#| j d  } n  |	 j |  |	 j   d  S(   Ng      R@u   utf-8(   t   _draw_mathtextR(   RL   R;   t   select_font_facet   get_namet
   fontanglest	   get_stylet   fontweightst
   get_weightt   get_size_in_pointsR   R/   t   rotatet   npt   deg2radt   set_font_sizeRg   t
   isinstancet   sixt	   text_typet   PY2t   encodet	   show_textR2   (   R   R!   Rb   Rc   t   st   propt   anglet   ismatht   mtextR(   t   size(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt	   draw_text   s(    	

c         C` s  | j  } |  j j | |  j |  \ } }	 }
 } } | j   | j | |  | rm | j t j |   n  x | D] \ } } } } } | j	   | j
 | |  t |  } | j   | j | j |  j | j |  j | j  | |  j d } | j |  t j r/t | t j  r/| j d  } n  | j |  | j   qt WxW | D]O \ } } } } | j	   | j | | | |  | j d d d  | j   qQW| j   d  S(   Ng      R@u   utf-8i    (   R(   R'   t   parseR   R/   RK   Ry   Rz   R{   RL   R;   R   Rr   t   nameRt   t   styleRv   t   weightR|   R~   t   PY3R}   R   R   R   R2   t	   rectanglet   set_source_rgbR1   (   R   R!   Rb   Rc   R   R   R   R(   R+   R,   t   descentt   glyphst   rectst   fontt   fontsizet   oxt   oyt   fontPropR   t   wt   h(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyRq     s6    		!



c         C` s   |  j  |  j f S(   N(   R+   R,   (   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   get_canvas_width_height1  s    c         C` s   | r= |  j  j | |  j |  \ } } } } } | | | f S|  j }	 |	 j   |	 j | j   |  j | j   |  j	 | j
    | j   |  j d }
 |	 j |
  |	 j |  d d !\ } } } |	 j   | | | | f S(   NiH   i   i   (   R'   R   R   R&   R/   Rr   Rs   Rt   Ru   Rv   Rw   Rx   R|   t   text_extentsR2   (   R   R   R   R   R+   R,   R   t   fontst   used_charactersR(   R   t	   y_bearingR   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   get_text_width_height_descent4  s    *	

c         C` s/   |  j  j j   d |  j  _ t |  j  _ |  j  S(   Ni   (   R!   R(   R/   t   _alphaRT   t   _forced_alpha(   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   new_gcN  s    c         C` s   | d |  j  S(   NiH   (   R   (   R   RE   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   points_to_pixelsT  s    N(   R   R   R"   t   FONT_WEIGHT_NORMALt   FONT_WEIGHT_BOLDRv   t   FONT_SLANT_ITALICt   FONT_SLANT_NORMALt   FONT_SLANT_OBLIQUERt   R   R*   R-   R7   t   staticmethodR.   RG   RP   Rd   Rp   RT   R   Rq   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR   A   sN   



















				/	 	#			R    c           B` s   e  Z i e j d  6e j d 6e j d 6Z i e j d 6e j d 6e j	 d 6Z
 d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d d  Z d   Z d   Z d   Z RS(   u   bevelu   miteru   roundu   buttu
   projectingc         C` s   t  j |   | |  _ d  S(   N(   R   R   R   (   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR   e  s    c         C` s   |  j  j   d  S(   N(   R(   R2   (   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR2   i  s    c         C` s   t  j |  |  |  j   } |  j } |  j   rY |  j j | d | d | d |  n) |  j j | d | d | d | d  d  S(   Ni    i   i   i   (   R   t	   set_alphaRM   t   _rgbRN   R(   R0   (   R   R5   R   t   rgb(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR   l  s    	(c         C` sC   | d k r/ | |  _  |  j j |  j |  n t d |   d  S(   Nu   buttu   roundu
   projectingu!   Unrecognized cap style.  Found %s(   u   buttu   roundu
   projecting(   t	   _capstyleR(   t   set_line_capt   _capdt
   ValueError(   R   t   cs(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_capstyley  s    	c         C` sm   | s
 d  St  j | j  \ } } } } |  j } | j   | j | |  j j | | | |  | j   d  S(   N(	   Rz   t   roundt   boundsR(   RL   R   R   R,   R8   (   R   R   Rb   Rc   R   R   R(   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_clip_rectangle  s    	
$c         C` s{   | s
 d  S| j    \ } } |  j } | j   | t   j d d  j d |  j j  } t j	 | | |  | j
   d  S(   Ni   ii    (   t   get_transformed_path_and_affineR(   RL   R   RJ   RK   R   R,   R   RG   R8   (   R   RC   t   tpatht   affineR(   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_clip_path  s    	
(c         C` sc   | | f |  _  | d  k r1 |  j j g  d  n. |  j j t |  j j t j |    |  d  S(   Ni    (	   t   _dashesR.   R(   t   set_dasht   listR   R   Rz   t   asarray(   R   t   offsett   dashes(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt
   set_dashes  s    	c         C` sU   t  j |  | |  t |  j  d k r> |  j j |  j   n |  j j |  j   d  S(   Ni   (   R   t   set_foregroundR   R   R(   R   R0   (   R   t   fgt   isRGBA(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR     s    c         C` s   |  j  j   j   d  S(   Ni   (   R(   t
   get_sourcet   get_rgba(   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   get_rgb  s    c         C` sC   | d k r/ | |  _  |  j j |  j |  n t d |   d  S(   Nu   miteru   roundu   bevelu"   Unrecognized join style.  Found %s(   u   miteru   roundu   bevel(   t
   _joinstyleR(   t   set_line_joint   _joindR   (   R   t   js(    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_joinstyle  s    	c         C` s/   t  |  |  _ |  j j |  j j |   d  S(   N(   Rk   t
   _linewidthR(   t   set_line_widthR   R   (   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   set_linewidth  s    N(   R   R   R"   t   LINE_JOIN_BEVELt   LINE_JOIN_MITERt   LINE_JOIN_ROUNDR   t   LINE_CAP_BUTTt   LINE_CAP_SQUAREt   LINE_CAP_ROUNDR   R   R2   R   R   R   R   R   R.   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR    X  s&   



											t   FigureCanvasCairoc           B` sD   e  Z e Z d    Z d   Z d   Z d   Z d   Z d   Z	 RS(   c         O` sz   |  j    \ } } t |  j j  } | j | |  t j t j | |  } | j |  |  j j	 |  | j
 |  d  S(   N(   t   get_width_heightR   t   figureR   R-   R"   R$   R%   R*   t   drawt   write_to_png(   R   t   fobjt   argst   kwargsR+   R,   R   R)   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt	   print_png  s    c         O` s   |  j  | d | |  S(   Nu   pdf(   t   _save(   R   R   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt	   print_pdf  s    c         O` s   |  j  | d | |  S(   Nu   ps(   R   (   R   R   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   print_ps  s    c         O` s   |  j  | d | |  S(   Nu   svg(   R   (   R   R   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt	   print_svg  s    c         O` s   |  j  | d | |  S(   Nu   svgz(   R   (   R   R   R   R   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt
   print_svgz  s    c         K` sC  | j  d d  } d } | |  j _ |  j j   \ } } | | | | } }	 | d k rj |	 | } }	 n  | d k r t t d  s t d   n  t j | | |	  }
 n | d k r t t d	  s t d
   n  t j | | |	  }
 n | d k r~t t d  st d   n  | d k rft	 | t
 j  rKt j | d  } qft j d  d d | } n  t j | | |	  }
 n t j d |  d  St |  j j  } | j | |	  | j |
  | j j } | d k r| j t j d  | j d |	  n  |  j j |  | j   |
 j   | d k r?| j   n  d  S(   Nu   orientationu   portraitiH   u	   landscapeu   psu	   PSSurfaceu3   cairo has not been compiled with PS support enabledu   pdfu
   PDFSurfaceu4   cairo has not been compiled with PDF support enabledu   svgu   svgzu
   SVGSurfaceu4   cairo has not been compiled with SVG support enabledu   wbt   fileobju   unknown format: %si   i    (   u   svgu   svgz(   t   getR   R   t   get_size_inchest   hasattrR"   t   RuntimeErrort	   PSSurfacet
   PDFSurfaceR}   R~   t   string_typest   gzipt   GzipFileR.   t
   SVGSurfacet   warningst   warnR   R-   R*   R!   R(   Ry   Rz   t   piRK   R   t	   show_paget   finisht   close(   R   t   fot   fmtR   t   orientationR   t   w_int   h_int   width_in_pointst   height_in_pointsR)   R   R(   (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR     sJ    

(
   R   R   RT   t   supports_blitR   R   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyR     s   					t   _BackendCairoc           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_cairo.pyR     s   (*   R   t
   __future__R    R   R   R   R~   R   Re   R   t   numpyRz   t	   cairocffiR"   t   ImportErrorRT   Rg   RU   t   version_infot   formatt   versiont   backend_versiont   matplotlib.backend_basesR   R   R   R   R   t   matplotlib.mathtextR	   t   matplotlib.pathR
   t   matplotlib.transformsR   t   matplotlib.font_managerR   R   R   R    R   t   exportR   (    (    (    s@   lib/python2.7/site-packages/matplotlib/backends/backend_cairo.pyt   <module>   s>   "
		( [R	