B
    Mô`F  ã               @   s2   d dl Zd dlmZ d dlmZ G dd„ dƒZdS )é    N)ÚRendererAgg)Úprocess_figure_for_rasterizingc               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚMixedModeRenderera&  
    A helper class to implement a renderer that switches between
    vector and raster drawing.  An example may be a PDF writer, where
    most things are drawn with PDF vector commands, but some very
    complex objects, such as quad meshes, are rasterised and then
    output as images.
    Nc             C   sV   |dkrt }|| _|| _|| _|| _|| _d| _d| _|| _| 	¡ | _
|| _|| _dS )a*  
        Parameters
        ----------
        figure : `matplotlib.figure.Figure`
            The figure instance.

        width : scalar
            The width of the canvas in logical units

        height : scalar
            The height of the canvas in logical units

        dpi : float
            The dpi of the canvas

        vector_renderer : `matplotlib.backend_bases.RendererBase`
            An instance of a subclass of
            `~matplotlib.backend_bases.RendererBase` that will be used for the
            vector drawing.

        raster_renderer_class : `matplotlib.backend_bases.RendererBase`
            The renderer class to use for the raster drawing.  If not provided,
            this will use the Agg backend (which is currently the only viable
            option anyway.)

        Nr   )r   Ú_raster_renderer_classÚ_widthÚ_heightÚdpiÚ_vector_rendererÚ_raster_rendererÚ_rasterizingÚfigureZget_dpiÚ_figdpiÚ_bbox_inches_restoreÚ	_renderer)Úselfr   ÚwidthÚheightr   Zvector_rendererZraster_renderer_classZbbox_inches_restore© r   úE/tmp/pip-unpacked-wheel-0c1wfn4v/matplotlib/backends/backend_mixed.pyÚ__init__   s    
zMixedModeRenderer.__init__c             C   s   t | j|ƒS )N)Úgetattrr   )r   Úattrr   r   r   Ú__getattr__C   s    zMixedModeRenderer.__getattr__c             C   sn   | j  | j¡ | jr(t| j | jƒ}|| _| jdkr\|  | j| j | j| j | j¡| _	| j	| _
|  jd7  _dS )z›
        Enter "raster" mode.  All subsequent drawing commands (until
        `stop_rasterizing` is called) will be drawn with the raster backend.
        r   é   N)r   Úset_dpir   r   r   r   r   r   r   r
   r   )r   Úrr   r   r   Ústart_rasterizingL   s    
z#MixedModeRenderer.start_rasterizingc             C   sü   |  j d8  _ | j dkrÚ| j| _| j| j }| j ¡ \}}|\}}}}|dkrÀ|dkrÀtj|tj	d�}| 
||df¡}|ddd… }| j ¡ }	| j |	|| j | j || | | j | j |¡ d| _d| _ | j | j¡ | jrøt| j| j| jƒ}
|
| _dS )af  
        Exit "raster" mode.  All of the drawing that was done since
        the last `start_rasterizing` call will be copied to the
        vector backend by calling draw_image.

        If `start_rasterizing` has been called multiple times,
        `stop_rasterizing` must be called the same number of times before
        "raster" mode is exited.
        r   r   )Zdtypeé   NéÿÿÿÿF)r   r	   r   r   r   r
   Ztostring_rgba_minimizedÚnpZ
frombufferZuint8ZreshapeZnew_gcZ
draw_imager   r   r   r   r   )r   r   ÚbufferZboundsÚlÚbÚwÚhÚimageÚgcr   r   r   r   Ústop_rasterizing]   s0    


z"MixedModeRenderer.stop_rasterizing)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r'   r   r   r   r   r      s    
2	r   )Znumpyr   Zmatplotlib.backends.backend_aggr   Zmatplotlib.tight_bboxr   r   r   r   r   r   Ú<module>   s   