rL iMdZddlmZddlmZmZmZddlZddl Z ddl m Z m Z ddl mZmZmZmZddlmZmZddlmZdd lmZdd lmZdd lmZmZdd lm Z!d Z"GddeZ GddeZ#ejHGddeZ%y)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)radianscossinN)_apicbook)_BackendFigureCanvasBaseFigureManagerBase RendererBase) fontManagerget_font) LoadFlags)MathTextParser)Path)BboxBboxBase) RendererAggc `tjtjtjtjtjtjtjtjtjtjd }|t j dS)N) default no_autohintforce_autohint no_hintingTFeithernativeautononez text.hinting)rDEFAULT NO_AUTOHINTFORCE_AUTOHINT NO_HINTINGmplrcParams)mappings e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/backends/backend_agg.pyget_hinting_flagr&)sy$$ ,,#22**&&####''(($$ G 3<</ 00ceZdZdZfdZdZdZdZddZdZ dd Z fd Z dd d Z d Z dZdZdZdZdZdZdZddZdZdZxZS)rz The renderer handles all the drawing primitives using a graphics context instance that controls the colors/styles cDt|||_||_||_t t |t |||_g|_|jtd|_ tjdd|j|j|_y)Naggr)super__init__dpiwidthheight _RendererAggint _renderer_filter_renderers_update_methodsrmathtext_parserr from_boundsbbox)selfr.r/r- __class__s r%r,zRendererAgg.__init__?s|   %c%j#f+sC!# -e4$$Q4::t{{C r'cJ|j|j|jdS)Nr.r/r-r;r8s r% __getstate__zRendererAgg.__getstate__Mst{{488LLr'c<|j|d|d|dy)Nr.r/r-)r,)r8states r% __setstate__zRendererAgg.__setstate__Rs eGneHouU|Dr'cH|jj|_|jj|_|jj|_|jj|_|jj |_|jj |_yN)r2draw_gouraud_triangles draw_image draw_markersdraw_path_collectiondraw_quad_meshcopy_from_bboxr<s r%r4zRendererAgg._update_methodsUsm&*nn&K&K#..33 NN77$(NN$G$G!"nn;;"nn;;r'Nc tjd}|jjd}||cxkDrdkDr&nn"|jr||j t j||z }tt j||z }t jd||} t j| } | dddz | dd|| d<t| | D]|\} } |j| | ddf} |j}||| | }tj|d<t| |}|j|_ |j j#||||~y |j j#||||y#t$$r6d|dtjdd d |jd d }t%|dwxYw#t$$rd }||d z }|j |dz }|js|dz }t'|rd|d}n;dtjdd |jd }|dkDr d|d|}n d|d|z}t%|dwxYw)Nzagg.path.chunksizerdzhExceeded 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'] = zpath.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#verticesshapeshould_simplify get_hatchnpceilr1arange zeros_likezipcodesrMOVETOsimplify_thresholdr2 draw_path OverflowErrorlen)r8gcpath transformrgbFacenmaxnptsnchchsizei0i1ii0ii1vcpmsg cant_chunk inc_thresholds r%rZzRendererAgg.draw_path]s||01}}""1% 4 # $"6"6BLLN$:''$+&C,-F1dF+Br"BfqjBsGBrFBK 7SMM#c'1*-JJ=#c A;;AaDAJ'+'>'>$ 7NN,,RIwG 7.* 3((T9gF% 7EEIFKB<<(AB3GHA2237H(,$6 7!( 3 &"@@J<<>-"AAJ++"EEJz?1%,'BBB<<(ABCD!4456) )"cz**.r-B**./HJ,,$C(d2Q( 3s>E? G??F>BIcd|jj||j||j\}}} } } } | t t |z} | t t |z}t||z| z}t||z |z}|jj| ||dz||y)z/Draw mathtext using :mod:`matplotlib.mathtext`. antialiasedrKN) r5parser-get_antialiasedrrrroundr2draw_text_image)r8r]xyspropangleoxoyr.r/descent font_imagexdyds r% draw_mathtextzRendererAgg.draw_mathtexts  & &q$((D353E3E3G ' I 3Bvw s75>* * s75>* * !b&2+  !b&2+  &&z1a!eUBGr'c |r|j||||||S|j|} | j|dt| j |j | j dz } | j\} } | dz} | dz} | tt|z} | tt|z}t|| z| z}t|| z|z}|jj| ||dz||y)NrflagsrpP@rK)r _prepare_fontset_textr&draw_glyphs_to_bitmaprs get_descentget_bitmap_offsetrrrrtr2ru)r8r]rvrwrxryrzismathmtextfontdxoyorrs r% draw_textzRendererAgg.draw_texts %%b!Q4? ?!!$' a"2"4 5 ""**, # .     %'')B d  d  WU^$ $ WU^$ $ !b&2+  !b&2+  &&tQAubAr'ctjgd||dk(rt| |||S|r3|jj ||j |\}}}}}} |||fS|j|} | j|dt| j\} } | j} | dz} | dz} | dz} | | | fS)N)TeXTFrrgrr) r check_in_listr+get_text_width_height_descentr5rrr-rrr&get_width_heightr)r8rxryrr{r|r.r/r}r~rwhrr9s r%rz)RendererAgg.get_text_width_height_descents /? U?78D&I I $$**1dhh= 7BE67J&') )!!$' a$4$6 7$$&1     T  T  T !Qwr')rc|j}|j} | j|||j} t j | dztj } |j||d\} } } | tt|z}| tt|z}t||z}t||z}|jj| ||||y)No@rr)get_size_in_pointsget_texmanagerget_greyr-rRarrayuint8rrrrrtr2ru)r8r]rvrwrxryrzrsize texmanagerZrrrrrs r%draw_texzRendererAgg.draw_texs&&(((*   4 2 HHQY )44QU4K1a WU^$ $ WU^$ $ !b&M !b&M &&q!Qr:r'c2|j|jfSrB)r.r/r<s r%get_canvas_width_heightz#RendererAgg.get_canvas_width_heightszz4;;&&r'cttj|}|j|j }|j ||j |S)zY Get the `.FT2Font` for *font_prop*, clear its buffer, and set its size. )r _fontManager_find_fonts_by_propsclearrset_sizer-)r8 font_proprrs r%rzRendererAgg._prepare_fontsG 99)DE ++- dDHH% r'c&||jzdz S)NH)r-)r8pointss r%points_to_pixelszRendererAgg.points_to_pixelss 2%%r'c,t|jSrB) memoryviewr2r<s r% buffer_rgbazRendererAgg.buffer_rgbas$..))r'ctj|jjgddj S)N)rrKr)axis)rRasarrayr2taketobytesr<s r% tostring_argbzRendererAgg.tostring_argb s.zz$..)..|!.DLLNNr'c8|jjyrB)r2rr<s r%rzRendererAgg.clear s r'cy)NTr<s r%option_image_nocompositez$RendererAgg.option_image_nocomposites r'cy)NFrr<s r%option_scale_imagezRendererAgg.option_scale_imagesr'c ||||j\}}}}n)t|tr|j\}}}}n|\}}}}|||} }n|\}} |jj |t |t |t |t |t |t | y|jj |y)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 isinstancerextentsr2restore_regionr1) r8regionr7xyx1y1x2y2r{r|s r%rzRendererAgg.restore_regions  r~|!'!3!3!5BBD(+!%BB!%BBzRBB NN ) )&#b'3r7*-b'3r7CGSW N NN ) )& 1r'c|jj|jtt |j t |j |j|_|jy)zY Start filtering. It simply creates a new canvas (the old one is saved). N) r3appendr2r0r1r.r/r-r4r<s r% start_filterzRendererAgg.start_filter>sP %%dnn5%c$**os4;;7G&*hh0 r'c tj|j}tj|d\}}|||f}|j j |_|j|jr||dz |j\}}}|j} |jjdk(r'tj|dztj}|jj| |j |zt#|j$|j&z |z|dddyy)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. ).rfrNrL)rRrrr_get_nonzero_slicesr3popr2r4rr-new_gcdtypekindrrDstartr1r/stop) r8post_processingorig_imgslice_yslice_x cropped_imgimgr{r|r]s r% stop_filterzRendererAgg.stop_filterGs$::d..01 44Xf5EFw/0 //335    )+*;TXXFKCRByy~~$jjtRXX6 NN % %GMMB&DKK(87<<(G"(LDbD   r'rB)FNNN)__name__ __module__ __qualname____doc__r,r=r@r4rZrrrrrrrrrrrrrrr __classcell__r9s@r%rr9sz DM E<N3` HB**;?;"'&*O!2F r'rceZdZdZdZddZfdZdZdZdZ ddd Z e Z dd Z ddd d Z d Zddd dZeZddd dZeZddd dZedj*gd\e_e_e_xZS)FigureCanvasAggNcD|j}|j|SrB) get_rendererrH)r8r7renderers r%rHzFigureCanvasAgg.copy_from_bboxos!$$&&&t,,r'cH|j}|j|||SrB)rr)r8rr7rrs r%rzFigureCanvasAgg.restore_regionss%$$&&&vtR88r'cT|j|_|jj|jr|jj n t 5|j j|jt|dddy#1swYyxYwrB) rrrtoolbar_wait_cursor_for_draw_cmrfiguredrawr+)r8r9s r%rzFigureCanvasAgg.drawwst))+  9=dll335=  KK  T]] + GLN    s !4BB'c|jjj\}}|||jjf}|j|k(}|s-t |||jj|_||_|j SrB)rr7rr-_lastKeyrr)r8rrkeyreuse_renderers r%rzFigureCanvasAgg.get_renderersi{{$$1DKKOO#--3.'1dkkoo>DMDM}}r'c6|jjS)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. )rrr<s r%rzFigureCanvasAgg.tostring_argbs}}**,,r'c6|jjS)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. )rrr<s r%rzFigureCanvasAgg.buffer_rgbas}}((**r')metadatac| tdtj||j}t j |d5}|j |jdddy#1swYyxYw)Nz#metadata not supported for raw/rgbawb) ValueErrorrrrr open_file_cmwriter)r8filename_or_objrrfhs r% print_rawzFigureCanvasAgg.print_rawsl  BC CT"$$&    6 -" HHX))+ , - - -s A22A;c tj|tjj ||j |d|j j||y)z Draw the canvas, then save it using `.image.imsave` (to which *pil_kwargs* and *metadata* are forwarded). upper)formatoriginr-r pil_kwargsN)rrr"imageimsaverrr-)r8rfmtrrs r% _print_pilzFigureCanvasAgg._print_pilsK T"  T--/G (z  Kr')rrc,|j|d||y)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. pngNr r8rrrs r% print_pngzFigureCanvasAgg.print_pngs^  HEr'ctj||j}t|j t |j t |jffSrB)rrrbytesrr1r.r/)r8rs r%print_to_bufferzFigureCanvasAgg.print_to_buffersST"$$&h**,-X^^$c(//&:;= =r'ctjddi5|j|d||dddy#1swYyxYw)Nzsavefig.facecolorwhitejpeg)r" rc_contextr rs r% print_jpgzFigureCanvasAgg.print_jpgsA^^0': ; K OOOVZ J K K Ks6?c,|j|d||y)Ntiffr rs r% print_tifzFigureCanvasAgg.print_tif XFr'c,|j|d||y)Nwebpr rs r% print_webpzFigureCanvasAgg.print_webprr'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. )JPEGTIFFWebPrrB)rrrrrHrrrrrr print_rgbar rrr print_jpegr print_tiffrmaprrrrs@r%rrjsH-9 -+6:-JK6:d/Fb=6:dKJ59dGJ6:tG@C  F, @.r7s.#"""AAI(. 0H 1 n,nb _.&_.D &(&&r'