B
    ]t\R                 @   sZ  d Z yddlZW n ek
r,   ddlZY nX d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 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$Z% ddl&m'Z( ddlm)Z) ddlm*Z* e*rddl+m,Z, dZ-dd Z.G dd deZ'G dd deZ/ej0G dd deZ1dS )a  
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

    N)OrderedDict)radianscossin)cbookrcParams__version__)_BackendFigureCanvasBaseFigureManagerBaseRendererBase)findfontget_font)LOAD_FORCE_AUTOHINTLOAD_NO_HINTINGLOAD_DEFAULTLOAD_NO_AUTOHINT)MathTextParser)Path)BboxBboxBase)colors)RendererAgg)_png)_has_pil)Imagezv2.2c              C   s   t tttt td} | td  S )N)TFZeitherZnativeautoZnoneztext.hinting)r   r   r   r   r   )mapping r   >lib/python3.7/site-packages/matplotlib/backends/backend_agg.pyget_hinting_flag2   s    r    c               @   s   e Zd ZdZe Zdd Zdd Zdd Z	dd	 Z
d
d Zd1ddZdd Zd2ddZdd Zd3dd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d)d* Zd4d+d,Zd-d. Zd/d0 ZdS )5r   z
    The renderer handles all the drawing primitives using a graphics
    context instance that controls the colors/styles
    c             C   sd   t |  || _|| _|| _tt|t||| _g | _| 	  t
d| _tdd| j| j| _d S )NZAggr   )r   __init__dpiwidthheight_RendererAggint	_renderer_filter_renderers_update_methodsr   mathtext_parserr   Zfrom_boundsbbox)selfr#   r$   r"   r   r   r   r!   Q   s    

zRendererAgg.__init__c             C   s   | j | j| jdS )N)r#   r$   r"   )r#   r$   r"   )r,   r   r   r   __getstate___   s    zRendererAgg.__getstate__c             C   s   |  |d |d |d  d S )Nr#   r$   r"   )r!   )r,   stater   r   r   __setstate__d   s    zRendererAgg.__setstate__c             C   sT   | j j| _| j j| _| j j| _| j j| _| j j| _| j j| _| j j| _| j j| _d S )N)	r'   Zdraw_gouraud_triangleZdraw_gouraud_triangles
draw_imageZdraw_markersZdraw_path_collectionZdraw_quad_meshcopy_from_bboxget_content_extents)r,   r   r   r   r)   g   s    






zRendererAgg._update_methodsc             C   s\   |   }|d | j|d |d   g|d |d  | j|d  gg}| |}t||fS )Nr            )r2   r$   r1   nparray)r,   extentsr+   regionr   r   r   tostring_rgba_minimizedq   s
     
z#RendererAgg.tostring_rgba_minimizedNc          	   C   sn  t d }|jjd }|dkr4||kr4|jr4|dkr4| dkr4t|| }tt|| }td||}	t	|	}
|	dd d |
dd< ||
d< xt
|	|
D ]\}}|j||ddf }|j}|dk	r||| }tj|d< t||}y| j|||| W q tk
r,   tdY qX qW n6y| j|||| W n tk
rh   tdY nX dS )z
        Draw the path
        zagg.path.chunksizer   d   Nr3   z<Exceeded cell block limit (set 'agg.path.chunksize' rcparam))r   ZverticesshapeZshould_simplifyZ	get_hatchr6   Zceilr&   ZarangeZ
zeros_likezipZcodesr   ZMOVETOr'   	draw_pathOverflowError)r,   gcpathZ	transformZrgbFaceZnmaxZnptsZnchZchsizeZi0Zi1Zii0Zii1vcpr   r   r   r?   x   s2    


zRendererAgg.draw_pathc             C   s   | j || j|\}}}	}
}}}|tt| }|tt| }t|| | }t|| | }| j	|||d || dS )z>
        Draw the math text using matplotlib.mathtext
        r3   N)
r*   parser"   r   r   r   r6   roundr'   draw_text_image)r,   rA   xyspropangleoxoyr#   r$   descentZ
font_imageused_charactersxdydr   r   r   draw_mathtext   s     zRendererAgg.draw_mathtextFc	             C   s  |r|  ||||||S t }	| |}
|
dkr4dS t|dkr`t|dkr`|
jt||	d n|
j|d|	d |
jtd d |
	 d }|

 \}}|d }|d }| tt| }|tt| }| j|
t|| | t|| | d || dS )	z!
        Render the text
        Nr3      )flagsr   ztext.antialiased)Zantialiasedg      P@)rT   r    _get_agg_fontlenordZ	load_charset_textZdraw_glyphs_to_bitmapr   get_descentZget_bitmap_offsetr   r   r   r'   rH   r6   rG   )r,   rA   rI   rJ   rK   rL   rM   ismathmtextrV   fontdZxoZyorR   rS   r   r   r   	draw_text   s$    
zRendererAgg.draw_textc             C   s   |dkr@|  }|  }|  }|j||| d\}}}	|||	fS |rn| j|| j|\}
}}}}}}|||fS t }| |}|j|d|d |	 \}}|
 }	|d }|d }|	d }	|||	fS )z
        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*
        )ZTeXzTeX!)rendererg        )rV   g      P@)get_size_in_pointsget_texmanagerget_text_width_height_descentr*   rF   r"   r    rW   rZ   Zget_width_heightr[   )r,   rK   rL   r\   size
texmanagerZfontsizewhr_   rN   rO   r#   r$   rP   ZfontsrQ   rV   r^   r   r   r   rd      s&    
 

z)RendererAgg.get_text_width_height_descentTeX!c	             C   s   |  }	|  }
|
||	| j}t|d tj}| |||\}}}|tt	| }|t
t	| }t|| }t|| }| j||||| d S )Ng     o@)rb   rc   Zget_greyr"   r6   r7   uint8rd   r   r   r   rG   r'   rH   )r,   rA   rI   rJ   rK   rL   rM   r\   r]   re   rf   Zrg   rh   r_   rR   rS   r   r   r   draw_tex   s    zRendererAgg.draw_texc             C   s   | j | jfS )z4return the canvas width and height in display coords)r#   r$   )r,   r   r   r   get_canvas_width_height   s    z#RendererAgg.get_canvas_width_heightc             C   s2   t |}t|}|  | }||| j |S )zJ
        Get the font for text instance t, caching for efficiency
        )r   r   clearrb   Zset_sizer"   )r,   rL   fnamer^   re   r   r   r   rW      s    zRendererAgg._get_agg_fontc             C   s   || j  d S )zj
        convert point measures to pixes using dpi and the pixels per
        inch of the display
        H   )r"   )r,   Zpointsr   r   r   points_to_pixels  s    zRendererAgg.points_to_pixelsc             C   s
   | j  S )N)r'   tostring_rgb)r,   r   r   r   rr     s    zRendererAgg.tostring_rgbc             C   s
   | j  S )N)r'   tostring_argb)r,   r   r   r   rs     s    zRendererAgg.tostring_argbc             C   s
   | j  S )N)r'   buffer_rgba)r,   r   r   r   rt     s    zRendererAgg.buffer_rgbac             C   s   | j   d S )N)r'   rn   )r,   r   r   r   rn     s    zRendererAgg.clearc             C   s   dS )NTr   )r,   r   r   r   option_image_nocomposite  s    z$RendererAgg.option_image_nocompositec             C   s   dS )zI
        agg backend doesn't support arbitrary scaling of image.
        Fr   )r,   r   r   r   option_scale_image  s    zRendererAgg.option_scale_imagec       
   
   C   s   |dk	s|dk	r|dkr*|  \}}}}n&t|trD|j\}}}}n|\}}}}|dkrd|| }}	n|\}}	| j|t|t|t|t|t|t|	 n| j| dS )a:  
        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)Zget_extents
isinstancer   r8   r'   restore_regionr&   )
r,   r9   r+   xyZx1Zy1Zx2Zy2rN   rO   r   r   r   rx   $  s    
zRendererAgg.restore_regionc             C   s6   | j | j tt| jt| j| j| _|   dS )zX
        Start filtering. It simply create a new canvas (the old one is saved).
        N)	r(   appendr'   r%   r&   r#   r$   r"   r)   )r,   r   r   r   start_filterG  s    
zRendererAgg.start_filterc             C   s   t | jt | j }}|  \}\}}}}| j | _|   |dkr|dkrt	|tj
}	||	||dfd | j\}	}
}|  }|	jjdkrt|	d tj
}	|	ddd }	| j|||
 || | | |	 dS )a  
        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.
        r      g     o@fNr<   )r&   r#   r$   r:   r(   popr'   r)   r6   Z
fromstringrj   Zreshaper"   Znew_gcZdtypeZkindZasarrayr0   )r,   Zpost_processingr#   r$   bufferlbrg   rh   ZimgrN   rO   rA   r   r   r   stop_filterP  s    zRendererAgg.stop_filter)N)FN)ri   N)NN)__name__
__module____qualname____doc__	threadingRLocklockr!   r-   r/   r)   r:   r?   rT   r`   rd   rl   rm   rW   rq   rr   rs   rt   rn   ru   rv   rx   r{   r   r   r   r   r   r   >   s0   

"


#	r   c                   s   e Zd ZdZdd ZdddZ fddZdd
dZdd Zdd Z	dd Z
dd ZeZdd Zdd Zerd	dddZeZd	dddZeZ  ZS )FigureCanvasAggz
    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   |   }||S )N)get_rendererr1   )r,   r+   ra   r   r   r   r1     s    zFigureCanvasAgg.copy_from_bboxNc             C   s   |   }||||S )N)r   rx   )r,   r9   r+   ry   ra   r   r   r   rx     s    zFigureCanvasAgg.restore_regionc                sL   | j dd| _tj  | j}z| j| j t   W dtj	  X dS )z5
        Draw the figure using the renderer.
        T)clearedN)
r   ra   r   r   acquiretoolbarfiguredrawsuperrelease)r,   r   )	__class__r   r   r     s    
zFigureCanvasAgg.drawFc             C   s   | j jj\}}}}||| j jf}y| j| jf W n tk
rH   d}Y nX | j|k}|rrt||| j j| _|| _n|r| j  | jS )NT)	r   r+   Zboundsr"   Z_lastKeyra   AttributeErrorr   rn   )r,   r   r   r   rg   rh   keyZneed_new_rendererr   r   r   r     s      


zFigureCanvasAgg.get_rendererc             C   s
   | j  S )zGet 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
        )ra   rr   )r,   r   r   r   rr     s    
zFigureCanvasAgg.tostring_rgbc             C   s
   | j  S )zGet 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

        )ra   rs   )r,   r   r   r   rs     s    zFigureCanvasAgg.tostring_argbc             C   s
   | j  S )zGet 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
        )ra   rt   )r,   r   r   r   rt     s    
zFigureCanvasAgg.buffer_rgbac          
   O   s\   t |  |  }tj|| jjd. t|d}||j	
  W d Q R X W d Q R X d S )N)r"   wb)r   r   r   r   _setattr_cmr   r"   open_file_cmwriter'   rt   )r,   filename_or_objargskwargsra   fhr   r   r   	print_raw  s
    
zFigureCanvasAgg.print_rawc       	   
   O   s   t |  |  }dt d }td|i}|dd}|dk	rH|| tj|| j	j
d6 t|d}tj|j|| j	j
|d W dQ R X W dQ R X dS )	a  
        Write the figure to a PNG file.

        Parameters
        ----------
        filename_or_obj : str or PathLike or file-like object
            The file to write to.

        metadata : dict, optional
            Metadata in the PNG file as key-value pairs of bytes or latin-1
            encodable strings.
            According to the PNG specification, keys must be shorter than 79
            chars.

            The `PNG specification`_ defines some common keywords that may be
            used as appropriate:

            - Title: Short (one line) title or caption for image.
            - Author: Name of image's creator.
            - Description: Description of image (possibly long).
            - Copyright: Copyright notice.
            - Creation Time: Time of original image creation
              (usually RFC 1123 format).
            - Software: Software used to create the image.
            - Disclaimer: Legal disclaimer.
            - Warning: Warning of nature of content.
            - Source: Device used to create the image.
            - Comment: Miscellaneous comment;
              conversion from other image format.

            Other keywords may be invented for other purposes.

            If 'Software' is not given, an autogenerated value for matplotlib
            will be used.

            For more details see the `PNG specification`_.

            .. _PNG specification:                 https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords

        zmatplotlib version z, http://matplotlib.org/ZSoftwaremetadataN)r"   r   )r   )r   r   r   r   r   r~   updater   r   r   r"   r   r   Z	write_pngr'   )	r,   r   r   r   ra   Zversion_strr   Zuser_metadatar   r   r   r   	print_png  s    *


zFigureCanvasAgg.print_pngc          	   C   sN   t |  |  }tj|| jjd  |j t	|j
t	|jffS Q R X d S )N)r"   )r   r   r   r   r   r   r"   r'   rt   r&   r#   r$   )r,   ra   r   r   r   print_to_buffer  s
    
zFigureCanvasAgg.print_to_buffer)dryrunc         	      s   |   \}}|rdS td||dddd}ttd }tdd |dd	 D }	td
||	}
|
||  fdddD }|	dtd  d|kr|d |d f|d< |
j
|fddi|S )a  
            Write the figure to a JPEG file.

            Parameters
            ----------
            filename_or_obj : str or PathLike or file-like object
                The file to write to.

            Other Parameters
            ----------------
            quality : int
                The image quality, on a scale from 1 (worst) to 100 (best).
                The default is :rc:`savefig.jpeg_quality`.  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.
            NRGBArawr   r3   zsavefig.facecolorc             S   s   g | ]}t |d  qS )   )r&   ).0rI   r   r   r   
<listcomp>7  s    z-FigureCanvasAgg.print_jpg.<locals>.<listcomp>r4   ZRGBc                s   i | ]}| kr | |qS r   r   )r   k)r   r   r   
<dictcomp>:  s   z-FigureCanvasAgg.print_jpg.<locals>.<dictcomp>)qualityoptimizeZprogressiver"   r   zsavefig.jpeg_qualityr"   formatZjpeg)r   r   
frombuffermcolorsZto_rgbar   tuplenewZpaste
setdefaultsave)r,   r   r   r   r   bufre   imageZrgbaZcolorZ
backgroundZoptionsr   )r   r   	print_jpg  s    
zFigureCanvasAgg.print_jpgc      	   	   O   sJ   |   \}}|rd S td||dddd}| jj| jjf}|j|d|dS )Nr   r   r   r3   Ztiff)r   r"   )r   r   r   r   r"   r   )	r,   r   r   r   r   r   re   r   r"   r   r   r   	print_tifF  s    zFigureCanvasAgg.print_tif)NN)F)r   r   r   r   r1   rx   r   r   rr   rs   rt   r   Z
print_rgbar   r   r   r   Z
print_jpegr   Z
print_tiff__classcell__r   r   )r   r   r   t  s"   


9-r   c               @   s   e Zd ZeZeZdS )_BackendAggN)r   r   r   r   ZFigureCanvasr   ZFigureManagerr   r   r   r   r   P  s   r   )2r   r   ImportErrorZdummy_threadingZnumpyr6   collectionsr   Zmathr   r   r   Z
matplotlibr   r   r   Zmatplotlib.backend_basesr	   r
   r   r   Zmatplotlib.font_managerr   r   Zmatplotlib.ft2fontr   r   r   r   Zmatplotlib.mathtextr   Zmatplotlib.pathr   Zmatplotlib.transformsr   r   r   r   Z matplotlib.backends._backend_aggr   r%   r   r   ZPILr   Zbackend_versionr    r   Zexportr   r   r   r   r   <module>   s:     8 ]