o
    h87                     @   s4  d dl mZmZ d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZm Z m!Z"m#Z#m$Z% d dl&m'Z'm(Z( e
)dduZ*d	d
 Z+dd Z,dd Z-dd Z.ej/j0ej1dkpe* dddd Z2ej/3dg ddd Z4dd Z5dd Z6dd Z7d d! Z8ej/j0ej1d"kpe* d#dd$d% Z9d&d' Z:ej/j0ej1dkd(dd)d* Z;d+d, Z<ej/j0e=ed- d.dd/d0 Z>d1d2 Z?d3d4 Z@d5d6 ZAd7d8 ZBd9d: ZCd;d< ZDej/j0ej1dkd=dd>d? ZEd@dA ZFdBdC ZGdS )D    )BytesIOStringION)Path)Image)findfontfindSystemFonts	FontEntryFontPropertiesfontManager	json_dump	json_loadget_fontis_opentype_cff_fontMSUserFontDirectories_get_fontconfig_fontsttfFontProperty)cbookft2fontpyplot
rc_contextfigure)subprocess_run_helpersubprocess_run_for_testingzfc-listc                  C   s   t dddgid ttdgd} W d    n1 sw   Y  t| jdks*J t| }| }t|dks:J |d	 d
ksBJ d S )Nfont.sans-serifcmmi10Bitstream Vera Sans)rc
sans-seriffamilyz
cmmi10.ttf   i"     )r   r   r	   r   namer   get_charmaplen)fontfilefontcmap r(   v/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/tests/test_font_manager.pytest_font_priority   s   
r*   c                   C   s   dt ddks
J dt ddksJ dt dd  k r't ddk s*J  J dt dd  k r=t ddk s@J  J t ddt ddksNJ d S )Nr   regularboldi  normal)r
   score_weightr(   r(   r(   r)   test_score_weight(   s   



r/   c                 C   s   t t| d  t| d }t 5 tdd ddiddddd	ifD ]}tdi |}tj|d
d|j|d
dks=J q$W d    d S 1 sIw   Y  d S )Nzfontlist.jsonignorez findfont: Font family.*not foundr   STIXGeneralr   i  )r   weightzno such font familyF)rebuild_if_missingr(   )r   r
   r   warningscatch_warningsfilterwarningsr	   r   )tmp_pathcopypropfpr(   r(   r)   test_json_serialization3   s   

"r;   c               	   C   s   d} t |  rt| sJ tjD ]-}d|jv r>t|jd}|ddk}W d    n1 s0w   Y  |t|jks>J qd S )Nz//usr/share/fonts/opentype/freefont/FreeMono.otfotfrb   s   OTTO)r   existsr   r
   ttflistfnameopenread)rA   ffdresr(   r(   r)   test_otfB   s   

rG   win32zno fontconfig installed)reasonc                   C   s   t t dks	J d S )N   )r$   r   r(   r(   r(   r)   test_get_fontconfig_fontsM   s   rK   factor)   r>         c                 C   s   t tdgd}t|dd}|  |dd |d | }t|| d}|  |dd |d tjj	| |dd	 d S )
Nr   r   rJ   )hinting_factor   d   abcg?)rtol)
r   r	   r   clearset_sizeset_textget_width_heightnptestingassert_allclose)rL   r&   font1expectedhinted_fontr(   r(   r)   test_hinting_factorS   s   


r_   c                  C   sJ   zt dd tjD } W n ty   td Y d S w | jdks#J d S )Nc                 s   s$    | ]}t |jjd kr|V  qdS )zseguisbi.ttfN)r   rA   r"   ).0entryr(   r(   r)   	<genexpr>i   s    z#test_utf16m_sfnt.<locals>.<genexpr>z0Couldn't find seguisbi.ttf font to test against.iX  )nextr
   r@   StopIterationpytestskipr2   )ra   r(   r(   r)   test_utf16m_sfntf   s   rg   c                  C   sd   t dgd} tt| jdkrtd t \}}|jddd| d dD ]
}|j	t
 |d	 q%d S )
NzWenQuanYi Zen Heir   zwqy-zenhei.ttcz"Font wqy-zenhei.ttc may be missing      ?u   ⿓fontpropertiesrawsvgpdfpsformatr	   r   r   r"   re   rf   pltsubplotstextsavefigr   )r:   figaxfmtr(   r(   r)   test_find_ttcs   s   
rz   c                  C   sr   t ddgd} tt| j}|dvrtd| d t \}}|jddd| d	 d
D ]
}|j	t
 |d q,d S )NzNoto Sans CJK SCzNoto Sans CJK JPr   )zNotoSansCJKsc-Regular.otfzNotoSansCJK-Regular.ttcz,Noto Sans CJK SC font may be missing (found )rh   u   Hello, 你好ri   rk   rp   rr   )r:   r"   rw   rx   ry   r(   r(   r)   test_find_noto}   s   r|   c                 C   s   t t t| d  W d    n1 sw   Y  t t tt| d  W d    n1 s3w   Y  t t tt| d  W d    n1 sPw   Y  ddlm} t jtdd |t	  W d    d S 1 ssw   Y  d S )Nznon-existent-font-name.ttfr   )FT2Fontzfont file or a binary-mode file)match)
re   raisesFileNotFoundErrorr   strbytesmatplotlib.ft2fontr}   	TypeErrorr   )r7   r}   r(   r(   r)   test_find_invalid   s   "r   linuxz$only Linux with fontconfig installedc                    s   d t  }t fdd|D rt  d | d}|jdd ttt	j
  |  | $}|dt|  t  t  }t fd	d|D sRJ W d    n1 s\w   Y  t  d S )
Nmpltest.ttfc                 3       | ]} |v V  qd S Nr(   r`   r&   font_test_filer(   r)   rb          z(test_user_fonts_linux.<locals>.<genexpr> already exists in system fontsfontsT)dirXDG_DATA_HOMEc                 3   r   r   r(   r   r   r(   r)   rb      r   )r   anyre   rf   joinensureshutilcopyfiler   __file__parentcontextsetenvr   r   cache_clear)tmpdirmonkeypatchr   user_fonts_dirmr(   r   r)   test_user_fonts_linux   s    

r   c               
      s   d t tj  } z.t|   fddtjD \}tj| W  fddtjD }|D ]}tj| q-dS  fddtjD }|D ]}tj| qDw )z/Smoke test that addfont() accepts pathlib.Path.r   c                 3   s     | ]}|j  r|V  qd S r   rA   endswithr   r   r(   r)   rb      s    
z'test_addfont_as_path.<locals>.<genexpr>c                    s   g | ]
}|j  r|qS r(   r   r   r   r(   r)   
<listcomp>   s    

z(test_addfont_as_path.<locals>.<listcomp>N)r   r   r   r
   addfontr@   remove)pathadded	to_remover&   r(   r   r)   test_addfont_as_path   s   
r   zWindows onlyc                     s   t jdst jdstd td d t } t fdd| D r.t  d td	 }t 	| t
ttj  | t } t fd
d| D sRJ d S )NAPPVEYORTF_BUILDznThis test should only run on CI (appveyor or azure) as the developer's font directory should remain unchanged.z4We need to update the registry for this test to workr   c                 3   r   r   r(   r   r   r(   r)   rb      r   z(test_user_fonts_win32.<locals>.<genexpr>r   r   c                 3   r   r   r(   r   r   r(   r)   rb      r   )osenvirongetre   xfailr   r   rf   r   makedirsr   r8   r   r   r   )r   r   r(   r   r)   test_user_fonts_win32   s   


r   c                 C   s(   t  \}}|jt dd t   d S )Nrn   rp   )rs   rt   rv   r   close)_rw   rx   r(   r(   r)   _model_handler   s   r   register_at_forkz Cannot register at_fork handlersc                  C   sT   t d td} | jdd}|t td W d    d S 1 s#w   Y  d S )Nr   forkrM   )	processes)r   multiprocessingget_contextPoolmaprange)ctxpoolr(   r(   r)   	test_fork   s
   
"r   c                 C   s^   dgt jd< | d td W d    n1 sw   Y  dd | jD ddgks-J d S )	Nzthis-font-does-not-existr   WARNINGsansc                 S   s   g | ]}|  qS r(   )
getMessage)r`   recr(   r(   r)   r      s    z'test_missing_family.<locals>.<listcomp>zFfindfont: Font family ['sans'] not found. Falling back to DejaVu Sans.zufindfont: Generic family 'sans' not found because none of the following families were found: this-font-does-not-exist)rs   rcParamsat_levelr   records)caplogr(   r(   r)   test_missing_family   s   
r   c                     s   dd l ddlm  dd lm dd } | _d}| fddfdd	t|D }|D ]}|  q4|D ]}|j	d
d |
 rMtdq=d S )Nr   )	LoadFlagsc                 S   s   t d)Nzerror in thread!)RuntimeError)argsr(   r(   r)   loud_excepthook  s   z(_test_threading.<locals>.loud_excepthook
   c                    sD   j dd tdD ]}d}|jt| d jd q
d S )N   timeoutrR   zDejaVu Sansg        )flags)waitr   r   r   rW   r   
NO_HINTING)njr&   )r   bfmr(   r)   bad_idea	  s
   z!_test_threading.<locals>.bad_ideac                    s$   g | ]}j  d | |fdqS )bad_thread_)targetr"   r   )Thread)r`   r   )r   	threadingr(   r)   r     s    z#_test_threading.<locals>.<listcomp>	   r   zthread failed to join)r   r   r   matplotlib.font_managerfont_manager
excepthookBarrierr   startr   is_aliver   )r   Nthreadstr(   )r   r   r   r   r   r)   _test_threading   s&   

r   c                   C   s   t d ttdd d S )Nr   r   r   )re   importorskipr   r   r(   r(   r(   r)   test_fontcache_thread_safe  s   
r   c                 C   s,   t tjddgi tjdt| idd}d S )Nz-czlimport matplotlib;import os;p = matplotlib.get_cachedir();os.chmod(p, 0o555);import matplotlib.font_manager;MPLCONFIGDIRT)envcheck)r   sys
executabler   r   r   )r7   procr(   r(   r)   test_lockfilefailure#  s   	
r   c                  C   sV   t dd} |  }tt|}|jdksJ |jdksJ |  }|ds)J d S )N	font-name)r"   r   z<img src="data:image/png;base64)	r   
_repr_png_r   rB   r   widthheight_repr_html_
startswith)fontentpngimghtmlr(   r(   r)   test_fontentry_dataclass9  s   
r   c                  C   sD   t t tddd} |   W d    d S 1 sw   Y  d S )Nz/randomr   )rA   r"   )re   r   r   r   r   )r   r(   r(   r)   %test_fontentry_dataclass_invalid_pathE  s   
"r   zLinux or OS onlyc            	   	   C   s   dd dD } t | dd}t dd}g }|| D ]}zt|}t|}||j W q ty4   Y qw ttt	|}tt
 }t	|t	|ksMJ t|t|ksWJ ||ks]J d S )Nc                 S   s   g | ]}t d |qS )r   )r   _get_data_path)r`   subdirr(   r(   r)   r   M  s    z'test_get_font_names.<locals>.<listcomp>)ttfr   )fontext)r   r   r}   r   appendr"   	Exceptionsortedlistsetr
   get_font_namesr$   )		paths_mpl	fonts_mplfonts_system	ttf_fontsr   r&   r9   available_fontsmpl_font_namesr(   r(   r)   test_get_font_namesK  s"   

r  c                     sF   G dd d  fdd} |   t  D ]}t| r td qd S )Nc                   @   s   e Zd ZdS )z/test_donot_cache_tracebacks.<locals>.SomeObjectN)__name__
__module____qualname__r(   r(   r(   r)   
SomeObjecta  s    r  c               	      s     } t  }| }|jddddd t 1}t  td |j|dd W d    n1 s3w   Y  W d    d S W d    d S 1 sKw   Y  d S )Nrh   aardvarkdoesnotexistr   r0   rl   rp   )	mfigureFigurert   ru   r   r4   r5   r6   rv   )xrw   rx   outr  r(   r)   innerd  s   

"z*test_donot_cache_tracebacks.<locals>.innerz#object from inner stack still alive)gcget_objects
isinstancere   fail)r  objr(   r  r)   test_donot_cache_tracebacks_  s   


r  c                   C   s   t tj tdd W d   n1 sw   Y  t tj tddd W d   n1 s2w   Y  t tj tdg W d   n1 sMw   Y  tddd tdd td td tdd dS )	aS  
    Test the deprecated API of FontProperties.__init__.

    The deprecation does not change behavior, it only adds a deprecation warning
    via a decorator. Therefore, the purpose of this test is limited to check
    which calls do and do not issue deprecation warnings. Behavior is still
    tested via the existing regular tests.
    TimesitalicNr   )size)r   styler   z"serif-24:style=oblique:weight=bold)re   warnsmplMatplotlibDeprecationWarningr	   r(   r(   r(   r)   $test_fontproperties_init_deprecationu  s   	
r'  )Hior   r   r  r   r   pathlibr   PILr   r   r   r4   numpyrY   re   
matplotlibr%  r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   rs   r   r   r  matplotlib.testingr   r   which
has_fclistr*   r/   r;   rG   markskipifplatformrK   parametrizer_   rg   rz   r|   r   r   r   r   r   hasattrr   r   r   r   r   r   r   r  r  r'  r(   r(   r(   r)   <module>   sh    8





!
