
    hM                        S r SSKJr  SSKJrJrJr  SSKrSSK	r
SSK	JrJr  SSKJrJrJrJr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJ r!  S r" " S S\5      r  " S S\5      r#\RH                   " S S\5      5       r%g)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)	LoadFlags)MathTextParser)Path)BboxBboxBase)RendererAggc                  `   [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  S.
n U [
        R                  S      $ )N)
defaultno_autohintforce_autohint
no_hintingTFeithernativeautononeztext.hinting)r   DEFAULTNO_AUTOHINTFORCE_AUTOHINT
NO_HINTINGmplrcParams)mappings    Q/var/www/html/env/lib/python3.13/site-packages/matplotlib/backends/backend_agg.pyget_hinting_flagr&   )   sy    $$ ,,#22**&&####''(($$G 3<</00    c                      ^  \ rS rSrSrU 4S jrS rS rS rSS jr	S	 r
SS
 jrU 4S jrSS.S jrS rS rS rS rS rS rS rS rSS jrS rS rSrU =r$ )r   9   zs
The renderer handles all the drawing primitives using a graphics
context instance that controls the colors/styles
c                 @  > [         TU ]  5         X0l        Xl        X l        [        [        U5      [        U5      U5      U l        / U l        U R                  5         [        S5      U l        [        R                  " SSU R                  U R                  5      U l        g )Naggr   )super__init__dpiwidthheight_RendererAggint	_renderer_filter_renderers_update_methodsr   mathtext_parserr   from_boundsbbox)selfr/   r0   r.   	__class__s       r%   r-   RendererAgg.__init__?   sv    
%c%j#f+sC!#-e4$$Q4::t{{C	r'   c                 J    U R                   U R                  U R                  S.$ )Nr/   r0   r.   r=   r9   s    r%   __getstate__RendererAgg.__getstate__M   s     t{{488LLr'   c                 <    U R                  US   US   US   5        g )Nr/   r0   r.   )r-   )r9   states     r%   __setstate__RendererAgg.__setstate__R   s    eGneHouU|Dr'   c                 H   U R                   R                  U l        U R                   R                  U l        U R                   R                  U l        U R                   R                  U l        U R                   R
                  U l        U R                   R                  U l        g N)r3   draw_gouraud_triangles
draw_imagedraw_markersdraw_path_collectiondraw_quad_meshcopy_from_bboxr>   s    r%   r5   RendererAgg._update_methodsU   sm    &*nn&K&K#..33 NN77$(NN$G$G!"nn;;"nn;;r'   Nc           	         [         R                  S   nUR                  R                  S   nXes=:  a  S:  Ga(  O  GO$UR                  (       Ga  UGc  UR                  5       c  [        R                  " Xe-  5      n[        [        R                  " Xg-  5      5      n[        R                  " SXh5      n	[        R                  " U	5      n
U	SS  S-
  U
S S& XjS'   [        X5       Hx  u  pUR                  X2S S 24   nUR                  nUb  XU n[        R                  US'   [        X5      nUR                  Ul         U R                   R#                  XX45        Mz     g  U R                   R#                  XX45        g ! [$         a6    SU S[         R                  S   S	 S
UR                  S	 S3n[%        U5      S ef = f! [$         a    SnUb  US-  nUR                  5       b  US-  nUR                  (       d  US-  n['        U5      (       a  SU S3nO<S[         R                  S    S
UR                   S3nUS:  a	  SU SU 3nO	SU S3U-   n[%        U5      S ef = f)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- cannot split filled path
z- cannot split hatched path
z - path.should_simplify is False
zFExceeded cell block limit in Agg, however for the following reasons:

zY
we cannot 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ceilr2   arange
zeros_likezipcodesr   MOVETOsimplify_thresholdr3   	draw_pathOverflowErrorlen)r9   gcpath	transformrgbFacenmaxnptsnchchsizei0i1ii0ii1vcpmsg
cant_chunkinc_thresholds                      r%   r_   RendererAgg.draw_path]   s   ||01}}""1%#$"6"6"6BLLN$:''$+&C,-F1d+Br"BfqjBsGrFKMM#'1*-JJ=c
A;;AaDJ'+'>'>$7NN,,RIG (.*3((9F % 7EEIF KB <<(AB3G HA2237H  (,$67 ! (3
&"@@J<<>-"AAJ++"EEJz??1%, 'BB B<<(ABC D!445 6)	) " cz**.r-B **./HJ ,,  $C(d2Q(3s   F #G  A G B I#c                 N   U R                   R                  X@R                  UUR                  5       S9u  pxppU[	        [        U5      5      -  nU[        [        U5      5      -  n[        X'-   U-   5      n[        X8-
  U-   5      nU R                  R                  XUS-   Xa5        g)z/Draw mathtext using :mod:`matplotlib.mathtext`.antialiasedrP   N)
r6   parser.   get_antialiasedr   r   r   roundr3   draw_text_image)r9   rb   xyspropangleoxoyr/   r0   descent
font_imagexdyds                  r%   draw_mathtextRendererAgg.draw_mathtext   s       &&q((D353E3E3G ' I 	3w s75>**s75>**!&2+!&2+&&za!eUGr'   c	                     U(       a  U R                  XX4XV5      $ U R                  U5      n	U	R                  US[        5       S9  U	R	                  UR                  5       S9  U	R                  5       S-  n
U	R                  5       u  pUS-  nUS-  nU
[        [        U5      5      -  nU
[        [        U5      5      -  n[        X+-   U-   5      n[        X<-   U-   5      nU R                  R                  XUS-   Xa5        g )Nr   flagsrv         P@rP   )r   _prepare_fontset_textr&   draw_glyphs_to_bitmapry   get_descentget_bitmap_offsetr   r   r   rz   r3   r{   )r9   rb   r|   r}   r~   r   r   ismathmtextfontdxoyor   r   s                  r%   	draw_textRendererAgg.draw_text   s    %%bQ4??!!$' 	a"2"45""**, 	# 	.%'')
d

d
WU^$$WU^$$!&2+!&2+&&tAuAr'   c                   > [         R                  " / SQUS9  US:X  a  [        TU ]  XU5      $ U(       a.  U R                  R                  XR                  U5      u  pEpgpXgU4$ U R                  U5      n
U
R                  US[        5       S9  U
R                  5       u  pU
R                  5       nUS-  nUS-  nUS-  nXU4$ )N)TeXTFr   r   g        r   r   )r   check_in_listr,   get_text_width_height_descentr6   rx   r.   r   r   r&   get_width_heightr   )r9   r~   r   r   r   r   r/   r0   r   r   r   whr   r:   s                 r%   r   )RendererAgg.get_text_width_height_descent   s     	/?U?78&II$$**1hh= 7BE7'))!!$'a$4$67$$&	T		T		T	Qwr'   )r   c                   UR                  5       nU R                  5       n	U	R                  XHU R                  5      n
[        R
                  " U
S-  [        R                  5      n
U R                  XESS9u  pnU[        [        U5      5      -  nU[        [        U5      5      -  n[        X.-   5      n[        X?-   5      nU R                  R                  XX6U5        g )N     o@r   r   )get_size_in_pointsget_texmanagerget_greyr.   rW   arrayuint8r   r   r   r   rz   r3   r{   )r9   rb   r|   r}   r~   r   r   r   size
texmanagerZr   r   r   r   r   s                   r%   draw_texRendererAgg.draw_tex   s     &&(((*
2HHQY)44QU4KaWU^$$WU^$$!&M!&M&&qQr:r'   c                 2    U R                   U R                  4$ rF   )r/   r0   r>   s    r%   get_canvas_width_height#RendererAgg.get_canvas_width_height   s    zz4;;&&r'   c                     [        [        R                  " U5      5      nUR                  5         UR	                  5       nUR                  X0R                  5        U$ )zI
Get the `.FT2Font` for *font_prop*, clear its buffer, and set its size.
)r   _fontManager_find_fonts_by_propsclearr   set_sizer.   )r9   	font_propr   r   s       r%   r   RendererAgg._prepare_font   sE     99)DE

++-dHH%r'   c                 $    XR                   -  S-  $ )NH   )r.   )r9   pointss     r%   points_to_pixelsRendererAgg.points_to_pixels  s     2%%r'   c                 ,    [        U R                  5      $ rF   )
memoryviewr3   r>   s    r%   buffer_rgbaRendererAgg.buffer_rgba  s    $..))r'   c                 ~    [         R                  " U R                  5      R                  / SQSS9R	                  5       $ )N)   r   rP      r   )axis)rW   asarrayr3   taketobytesr>   s    r%   tostring_argbRendererAgg.tostring_argb	  s.    zz$..)..|!.DLLNNr'   c                 8    U R                   R                  5         g rF   )r3   r   r>   s    r%   r   RendererAgg.clear  s    r'   c                     g)NT r>   s    r%   option_image_nocomposite$RendererAgg.option_image_nocomposite  s     r'   c                     g)NFr   r>   s    r%   option_scale_imageRendererAgg.option_scale_image  s    r'   c                    Uc  Ub  Uc  UR                  5       u  pEpgO*[        U[        5      (       a  UR                  u  pEpgOUu  pEpgUc  XEpOUu  pU R                  R                  U[        U5      [        U5      [        U5      [        U5      [        U5      [        U	5      5        gU R                  R                  U5        g)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 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   extentsr3   restore_regionr2   )
r9   regionr8   xyx1y1x2y2r   r   s
             r%   r   RendererAgg.restore_region  s     r~|!'!3!3!5BD(++!%B!%zB NN))&#b'3r7*-b'3r7CGSWN NN))&1r'   c                     U R                   R                  U R                  5        [        [	        U R
                  5      [	        U R                  5      U R                  5      U l        U R                  5         g)zI
Start filtering. It simply creates a new canvas (the old one is saved).
N)	r4   appendr3   r1   r2   r/   r0   r.   r5   r>   s    r%   start_filterRendererAgg.start_filter>  sP     	%%dnn5%c$**os4;;7G&*hh0r'   c           	         [         R                  " U R                  5       5      n[        R                  " US   5      u  p4X#U4   nU R
                  R                  5       U l        U R                  5         UR                  (       a  U" US-  U R                  5      u  pgnU R                  5       n	UR                  R                  S:X  a(  [         R                  " US-  [         R                  5      nU R                  R                  XR                   U-   [#        U R$                  5      UR&                  -
  U-   USSS2   5        gg)a?  
Save the current canvas as an image and apply post processing.

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   NrQ   )rW   r   r   r   _get_nonzero_slicesr4   popr3   r5   r   r.   new_gcdtypekindr   rH   startr2   r0   stop)
r9   post_processingorig_imgslice_yslice_xcropped_imgimgr   r   rb   s
             r%   stop_filterRendererAgg.stop_filterG  s    $ ::d..01 44Xf5EF/0//335)+*;TXXFKCRByy~~$jjtRXX6NN%%MMB&DKK(87<<(G"(LDbD	 r'   )r4   r3   r8   rL   r.   rG   rH   rI   rJ   rK   r0   r6   r/   rF   )FNNN)__name__
__module____qualname____firstlineno____doc__r-   r?   rC   r5   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes____classcell__r:   s   @r%   r   r   9   s    
DM
E<N3`
HB** ;? ;"'&*O!2F   r'   r   c                     ^  \ rS rSrSrS rSS jrU 4S jrS rS r	S r
SS	.S
 jr\rSS jrSSS.S jrS rSSS.S jr\rSSS.S jr\rSSS.S jr\" SR,                  / SQ5      u  \l        \l        \l        SrU =r$ )FigureCanvasAggij  Nc                 D    U R                  5       nUR                  U5      $ rF   )get_rendererrL   )r9   r8   renderers      r%   rL   FigureCanvasAgg.copy_from_bboxo  s!    $$&&&t,,r'   c                 F    U R                  5       nUR                  XU5      $ rF   )r  r   )r9   r   r8   r   r  s        r%   r   FigureCanvasAgg.restore_regions  s#    $$&&&vR88r'   c                 h  > U R                  5       U l        U R                  R                  5         U R                  (       a  U R                  R	                  5       O	[        5          U R                  R                  U R                  5        [        TU ]  5         S S S 5        g ! , (       d  f       g = frF   )	r  r  r   toolbar_wait_cursor_for_draw_cmr   figuredrawr,   )r9   r:   s    r%   r  FigureCanvasAgg.draww  st    ))+9=dll335=!KKT]]+ GLN! ! !s   &4B##
B1c                    U R                   R                  R                  u  pXU R                   R                  4nU R                  U:H  nU(       d+  [        XU R                   R                  5      U l        X0l        U R                  $ rF   )r
  r8   r   r.   _lastKeyr   r  )r9   r   r   keyreuse_renderers        r%   r  FigureCanvasAgg.get_renderer  sa    {{$$DKKOO#--3.'dkkoo>DMM}}r'   c                 6    U R                   R                  5       $ )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   r>   s    r%   r   FigureCanvasAgg.tostring_argb  s     }}**,,r'   c                 6    U R                   R                  5       $ )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   r>   s    r%   r   FigureCanvasAgg.buffer_rgba  s     }}((**r'   )metadatac                
   Ub  [        S5      e[        R                  U 5        U R                  5       n[        R
                  " US5       nUR                  UR                  5       5        S S S 5        g ! , (       d  f       g = f)Nz#metadata not supported for raw/rgbawb)
ValueErrorr   r  r  r   open_file_cmwriter   )r9   filename_or_objr  r  fhs        r%   	print_rawFigureCanvasAgg.print_raw  sc    BCCT"$$&6"HHX))+, 766s    A44
Bc           
          [         R                  U 5        [        R                  R	                  XR                  5       USU R                  R                  XCS9  g)zk
Draw the canvas, then save it using `.image.imsave` (to which
*pil_kwargs* and *metadata* are forwarded).
upper)formatoriginr.   r  
pil_kwargsN)r   r  r"   imageimsaver   r
  r.   )r9   r  fmtr$  r  s        r%   
_print_pilFigureCanvasAgg._print_pil  sG    
 	T"		--/G( 	 	Kr'   )r  r$  c                *    U R                  USX25        g)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(  r9   r  r  r$  s       r%   	print_pngFigureCanvasAgg.print_png  s    ^ 	
Er'   c                     [         R                  U 5        U R                  5       n[        UR	                  5       5      [        UR                  5      [        UR                  5      44$ rF   )r   r  r  bytesr   r2   r/   r0   )r9   r  s     r%   print_to_bufferFigureCanvasAgg.print_to_buffer  sS    T"$$&h**,-X^^$c(//&:;= 	=r'   c                    [         R                  " SS05         U R                  USX25        S S S 5        g ! , (       d  f       g = f)Nzsavefig.facecolorwhitejpeg)r"   
rc_contextr(  r-  s       r%   	print_jpgFigureCanvasAgg.print_jpg  s2     ^^0':;OOOVZJ <;;s	   6
Ac                *    U R                  USX25        g )Ntiffr,  r-  s       r%   	print_tifFigureCanvasAgg.print_tif      Fr'   c                *    U R                  USX25        g )Nwebpr,  r-  s       r%   
print_webpFigureCanvasAgg.print_webp  r>  r'   aL  
        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.
        )JPEGTIFFWebP)r  r  r   rF   )r   r   r   r   r  rL   r   r  r  r   r   r  
print_rgbar(  r.  r2  r8  
print_jpegr<  
print_tiffrA  mapr"  r   r   r   r   s   @r%   r   r   j  s     H-9
-+ 6: - JK 6:d /Fb= 6:d K J59d G J6:t G @C
	 F,@.<Iy(**<*<r'   r   c                        \ rS rSrSr\r\rSr	g)_BackendAggi  zv2.2r   N)
r   r   r   r   backend_versionr   FigureCanvasr   FigureManagerr   r   r'   r%   rK  rK    s    O"L%Mr'   rK  )&r   
contextlibr   mathr   r   r   numpyrW   
matplotlibr"   r   r   matplotlib.backend_basesr	   r
   r   r   matplotlib.font_managerr   r   r   matplotlib.ft2fontr   matplotlib.mathtextr   matplotlib.pathr   matplotlib.transformsr   r    matplotlib.backends._backend_aggr   r1   r&   r   exportrK  r   r'   r%   <module>r[     s   . # " "   "A A I ( .   0 H1 n, nb	_.& _.D 
&( & &r'   