a
    Sic^S                     @   s  d Z ddlmZ ddlmZmZmZ ddl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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Z&dd Z'G dd deZ$G dd deZ(ej)G dd deZ*dS )a  
An `Anti-Grain Geometry`_ (AGG) backend.

Features that are implemented:

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

Still TODO:

* integrate screen dpi w/ ppi and text

.. _Anti-Grain Geometry: http://agg.sourceforge.net/antigrain.com
    )nullcontext)radianscossinN)_apicbook)_BackendFigureCanvasBaseFigureManagerBaseRendererBase)fontManagerget_font)LOAD_FORCE_AUTOHINTLOAD_NO_HINTINGLOAD_DEFAULTLOAD_NO_AUTOHINT)MathTextParser)Path)BboxBboxBase)RendererAggzv2.2c                  C   s(   t tttttt tttd
} | tjd  S )N)
defaultno_autohintforce_autohint
no_hintingTFeithernativeautononeztext.hinting)r   r   r   r   mplrcParams)mapping r"   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/matplotlib/backends/backend_agg.pyget_hinting_flag.   s    r$   c                       s   e Zd ZdZe Z fddZdd Zdd Z	dd	 Z
d/ddZdd Zd0ddZdd Zd
d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d'd( Zd1d)d*Zd+d, Zd-d. Z  ZS )2r   z
    The renderer handles all the drawing primitives using a graphics
    context instance that controls the colors/styles
    c                    sd   t    || _|| _|| _tt|t||| _g | _| 	  t
d| _tdd| j| j| _d S )NAggr   )super__init__dpiwidthheight_RendererAggint	_renderer_filter_renderers_update_methodsr   mathtext_parserr   from_boundsbbox)selfr)   r*   r(   	__class__r"   r#   r'   Q   s    

zRendererAgg.__init__c                 C   s   | j | j| jdS )Nr)   r*   r(   r6   r3   r"   r"   r#   __getstate___   s    zRendererAgg.__getstate__c                 C   s   |  |d |d |d  d S )Nr)   r*   r(   )r'   )r3   stater"   r"   r#   __setstate__d   s    zRendererAgg.__setstate__c                 C   sJ   | j j| _| j j| _| j j| _| j j| _| j j| _| j j| _| j j| _d S N)r-   draw_gouraud_triangledraw_gouraud_triangles
draw_imagedraw_markersdraw_path_collectiondraw_quad_meshcopy_from_bboxr7   r"   r"   r#   r/   g   s    





zRendererAgg._update_methodsNc                 C   s:  t jd }|jjd }||  kr,dkrfn n4|jrf|d u rf| d u rft|| }tt|| }t	d||}	t
|	}
|	dd  d |
d d< ||
d< t|	|
D ]\}}|j||d d f }|j}|d ur||| }tj|d< t||}|j|_z| j|||| W q ty`   d| dt jd d	d
|jd	d}t|d Y q0 qnz| j|||| W n ty4   d}|d ur|d7 }| d ur|d7 }|js|d7 }t|rd| d}nFdt jd  d
|j d}|dkrd| d| }nd| d| }t|d Y n0 d S )Nzagg.path.chunksizer   d      zhExceeded cell block limit in Agg.

Please reduce the value of rcParams['agg.path.chunksize'] (currently zV) or increase the path simplification threshold(rcParams['path.simplify_threshold'] = zpath.simplify_thresholdz.2fz* by default and path.simplify_threshold = z on the input). z- can not split filled path
z- can not split hatched path
z - path.should_simplify is False
zFExceeded cell block limit in Agg, however for the following reasons:

zZ
we can not automatically split up this path to draw.

Please manually simplify your path.zTor increase the path simplification threshold(rcParams['path.simplify_threshold'] = zhExceeded cell block limit in Agg.  Please reduce the value of rcParams['agg.path.chunksize'] (currently z) zfExceeded cell block limit in Agg.  Please set the value of rcParams['agg.path.chunksize'], (currently z) to be greater than 100 )r   r    verticesshapeshould_simplify	get_hatchnpceilr,   arange
zeros_likezipcodesr   MOVETOsimplify_thresholdr-   	draw_pathOverflowErrorlen)r3   gcpath	transformrgbFacenmaxnptsZnchZchsizei0i1Zii0Zii1vcpmsgZ
cant_chunkZinc_thresholdr"   r"   r#   rS   p   s    
$









zRendererAgg.draw_pathc                 C   sz   | j || j|\}}}	}
}}|tt| }|tt| }t|| | }t|| | }| j|||d || dS )z/Draw mathtext using :mod:`matplotlib.mathtext`.rD   N)	r0   parser(   r   r   r   roundr-   draw_text_image)r3   rV   xyspropangleoxoyr)   r*   descent
font_imagexdydr"   r"   r#   draw_mathtext   s    zRendererAgg.draw_mathtextFc	                 C   s   |r|  ||||||S | |}	|	j|dt d |	jtjd d |	 d }
|	 \}}|d }|d }|
t	t
| }|
tt
| }t|| | }t|| | }| j|	||d || d S )Nr   flagsztext.antialiased)antialiased      P@rD   )rp   _prepare_fontset_textr$   draw_glyphs_to_bitmapr   r    get_descentget_bitmap_offsetr   r   r   rc   r-   rd   )r3   rV   re   rf   rg   rh   ri   ismathmtextfontdxoyorn   ro   r"   r"   r#   	draw_text   s     
zRendererAgg.draw_textc                 C   s   t jg d|d |dkrJ|  }| }|j||| d\}}}|||fS |rv| j|| j|\}	}
}}}}|||fS | |}|j	|dt
 d | \}}| }|d }|d }|d }|||fS )N)TeXTFrz   r   )rendererg        rq   rt   )r   check_in_listget_texmanagerget_size_in_pointsget_text_width_height_descentr0   rb   r(   ru   rv   r$   get_width_heightrx   )r3   rg   rh   rz   
texmanagerfontsizewhr}   rj   rk   r)   r*   rl   rm   r|   r"   r"   r#   r      s(    


z)RendererAgg.get_text_width_height_descent)r{   c                C   s   |  }|  }	|	||| j}
t|
d tj}
| j||dd\}}}|tt	| }|t
t	| }t|| }t|| }| j|
|||| d S )N     o@r   r   )r   r   get_greyr(   rK   arrayuint8r   r   r   r   rc   r-   rd   )r3   rV   re   rf   rg   rh   ri   r{   sizer   Zr   r   r}   rn   ro   r"   r"   r#   draw_tex   s    zRendererAgg.draw_texc                 C   s   | j | jfS r;   )r)   r*   r7   r"   r"   r#   get_canvas_width_height  s    z#RendererAgg.get_canvas_width_heightc                 C   s0   t t|}|  | }||| j |S )zY
        Get the `.FT2Font` for *font_prop*, clear its buffer, and set its size.
        )r   _fontManager_find_fonts_by_propsclearr   set_sizer(   )r3   Z	font_propr|   r   r"   r"   r#   ru     s
    zRendererAgg._prepare_fontc                 C   s   || j  d S )NH   )r(   )r3   pointsr"   r"   r#   points_to_pixels  s    zRendererAgg.points_to_pixelsc                 C   s
   t | jS r;   )
memoryviewr-   r7   r"   r"   r#   buffer_rgba  s    zRendererAgg.buffer_rgbac                 C   s   t | jjg ddd S )N)   r   rD      r   axisrK   asarrayr-   taketobytesr7   r"   r"   r#   tostring_argb   s    zRendererAgg.tostring_argbc                 C   s   t | jjg ddd S )N)r   rD   r   r   r   r   r7   r"   r"   r#   tostring_rgb#  s    zRendererAgg.tostring_rgbc                 C   s   | j   d S r;   )r-   r   r7   r"   r"   r#   r   &  s    zRendererAgg.clearc                 C   s   dS )NTr"   r7   r"   r"   r#   option_image_nocomposite)  s    z$RendererAgg.option_image_nocompositec                 C   s   dS )NFr"   r7   r"   r"   r#   option_scale_image1  s    zRendererAgg.option_scale_imagec           
   
   C   s   |dus|dur|du r*|  \}}}}n&t|trD|j\}}}}n|\}}}}|du rd|| }}	n|\}}	| j|t|t|t|t|t|t|	 n| j| dS )a4  
        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 pair of floats) 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)get_extents
isinstancer   extentsr-   restore_regionr,   )
r3   regionr2   xyx1y1x2y2rj   rk   r"   r"   r#   r   5  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/   r7   r"   r"   r#   start_filterX  s
    zRendererAgg.start_filterc           
   	   C   s   t |  }t|d \}}|||f }| j | _|   |j	r||d | j
\}}}|  }	|jjdkrt |d t j}| j|	|j| t| j|j | |ddd  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      fr   NrE   )rK   r   r   r   _get_nonzero_slicesr.   popr-   r/   r   r(   new_gcdtypekindr   r>   startr,   r*   stop)
r3   post_processingorig_imgZslice_yZslice_xZcropped_imgimgrj   rk   rV   r"   r"   r#   stop_filtera  s    zRendererAgg.stop_filter)N)FN)NN)__name__
__module____qualname____doc__	threadingRLocklockr'   r8   r:   r/   rS   rp   r   r   r   r   ru   r   r   r   r   r   r   r   r   r   r   __classcell__r"   r"   r4   r#   r   >   s.   	
P


#	r   c                       s  e Zd ZdZdd Zd)ddZ fddZejdd	d
dd*ddZ	dd Z
dd Zdd Zedddd ZeZd+ddZedddddddZdd Zedddd d!d"ZeZdd d#d$ZeZdd d%d&Zed'jg d(\e_e_e_  ZS ),FigureCanvasAggNc                 C   s   |   }||S r;   )get_rendererrB   )r3   r2   r   r"   r"   r#   rB     s    zFigureCanvasAgg.copy_from_bboxc                 C   s   |   }||||S r;   )r   r   )r3   r   r2   r   r   r"   r"   r#   r     s    zFigureCanvasAgg.restore_regionc              	      s   |   | _| j  tj^ | jr,| j nt ( | j	| j t
 	  W d    n1 s`0    Y  W d    n1 s~0    Y  d S r;   )r   r   r   r   r   toolbar_wait_cursor_for_draw_cmr   figuredrawr&   r7   r4   r"   r#   r     s    

zFigureCanvasAgg.drawz3.6clearedzrenderer.clear())alternativeFc                 C   sX   | j jj\}}||| j jf}| j|k}|sDt||| j j| _|| _n|rR| j  | jS r;   )r   r2   r   r(   _lastKeyr   r   r   )r3   r   r   r   keyZreuse_rendererr"   r"   r#   r     s    

zFigureCanvasAgg.get_rendererc                 C   s
   | j  S )z
        Get the image as RGB `bytes`.

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.
        )r   r   r7   r"   r"   r#   r     s    zFigureCanvasAgg.tostring_rgbc                 C   s
   | j  S )z
        Get the image as ARGB `bytes`.

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.
        )r   r   r7   r"   r"   r#   r     s    zFigureCanvasAgg.tostring_argbc                 C   s
   | j  S )z
        Get the image as a `memoryview` to the renderer's buffer.

        `draw` must be called at least once before this function will work and
        to update the renderer for any subsequent changes to the Figure.
        )r   r   r7   r"   r"   r#   r     s    zFigureCanvasAgg.buffer_rgbaz3.5argsc                 G   sP   t |  |  }t|d}||  W d    n1 sB0    Y  d S )Nwb)r   r   r   r   open_file_cmwriter   )r3   filename_or_objr   r   fhr"   r"   r#   	print_raw  s    
zFigureCanvasAgg.print_rawc              	   C   s0   t |  tjj||  |d| jj||d dS )z
        Draw the canvas, then save it using `.image.imsave` (to which
        *pil_kwargs* and *metadata* are forwarded).
        upper)formatoriginr(   metadata
pil_kwargsN)r   r   r   imageimsaver   r   r(   )r3   r   fmtr   r   r"   r"   r#   
_print_pil  s
    

zFigureCanvasAgg._print_pil)r   r   c                G   s   |  |d|| dS )a  
        Write the figure to a PNG file.

        Parameters
        ----------
        filename_or_obj : str or path-like or file-like
            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.  This can be removed by setting it to *None*.

            For more details see the `PNG specification`_.

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

        pil_kwargs : dict, optional
            Keyword arguments passed to `PIL.Image.Image.save`.

            If the 'pnginfo' key is present, it completely overrides
            *metadata*, including the default 'Software' key.
        pngNr   )r3   r   r   r   r   r"   r"   r#   	print_png  s    1zFigureCanvasAgg.print_pngc                 C   s2   t |  |  }t| t|jt|jffS r;   )r   r   r   bytesr   r,   r)   r*   )r3   r   r"   r"   r#   print_to_buffer
  s
    

zFigureCanvasAgg.print_to_buffer)r   c                G   s@   t ddi | |d| W d    n1 s20    Y  d S )Nzsavefig.facecolorwhitejpeg)r   
rc_contextr   )r3   r   r   r   r"   r"   r#   	print_jpg  s    zFigureCanvasAgg.print_jpgc                C   s   |  |d| d S )Ntiffr   r3   r   r   r"   r"   r#   	print_tif  s    zFigureCanvasAgg.print_tifc                C   s   |  |d| d S )Nwebpr   r   r"   r"   r#   
print_webp#  s    zFigureCanvasAgg.print_webpaL  
        Write the figure to a {} file.

        Parameters
        ----------
        filename_or_obj : str or path-like or file-like
            The file to write to.
        pil_kwargs : dict, optional
            Additional keyword arguments that are passed to
            `PIL.Image.Image.save` when saving the figure.
        )JPEGZTIFFZWebP)NN)F)N)r   r   r   r   rB   r   r   r   delete_parameterr   r   r   r   r   Z
print_rgbar   r   r   r   Z
print_jpegr   Z
print_tiffr   mapr   r   r   r"   r"   r4   r#   r     s6   
			




2


r   c                   @   s   e Zd ZeZeZdS )_BackendAggN)r   r   r   r   FigureCanvasr
   FigureManagerr"   r"   r"   r#   r   4  s   r   )+r   
contextlibr   mathr   r   r   r   numpyrK   
matplotlibr   r   r   matplotlib.backend_basesr   r	   r
   r   matplotlib.font_managerr   r   r   Zmatplotlib.ft2fontr   r   r   r   matplotlib.mathtextr   Zmatplotlib.pathr   matplotlib.transformsr   r   Z matplotlib.backends._backend_aggr   r+   backend_versionr$   r   exportr   r"   r"   r"   r#   <module>   s,     G 2