
    j5j&                    |    d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ej                  e      Z G d d      Zy)zSystem font discovery for PDF rendering.

Provides lazy discovery of Noto fonts installed on the system across
Linux, macOS, and Windows platforms.
    )annotationsN)Path)FontManagerc                     e Zd ZU dZ ed       ed       ej
                         dz   ej
                         dz  g ed       ej
                         dz  g ed       ed       ej
                         dz  gd	Zd
ed<   i dddgdddgdddgdg ddddgdddgdddgd d!d"gd#d$d%gd&d'd(gd)d*d+gd,d-d.gd/d0d1gd2d3d4gd5d6d7gd8d9d:gd;d<d=gd>d?gd@dAgdBdCgdDdEgdFdGgdHZdIedJ<   dSdKZ	dTdLZ
dUdMZdVdNZdWdOZdXdPZdYdQZyR)ZSystemFontProvideraL  Discovers and provides system-installed Noto fonts with lazy scanning.

    This provider searches standard system font directories for Noto fonts.
    Scanning is performed lazily - only when a font is actually requested
    and not found in the builtin fonts. Results are cached for the lifetime
    of the provider instance.
    z/usr/share/fontsz/usr/local/share/fontsz.fontsz.local/share/fontsz/Library/Fontsz/System/Library/FontszLibrary/Fonts)linuxfreebsddarwinzdict[str, list[Path]]SYSTEM_FONT_DIRSzNotoSans-RegularzNotoSans-Regular.ttfzNotoSans-Regular.otfzNotoSansArabic-RegularzNotoSansArabic-Regular.ttfzNotoSansArabic-Regular.otfzNotoSansDevanagari-RegularzNotoSansDevanagari-Regular.ttfzNotoSansDevanagari-Regular.otfzNotoSansCJK-Regular)zNotoSansCJKsc-Regular.otfzNotoSansCJKtc-Regular.otfzNotoSansCJKjp-Regular.otfzNotoSansCJKkr-Regular.otfzNotoSansCJK-Regular.ttczNotoSansCJKsc-Regular.ttczNotoSansCJKsc-VF.otfzNotoSansThai-RegularzNotoSansThai-Regular.ttfzNotoSansThai-Regular.otfzNotoSansHebrew-RegularzNotoSansHebrew-Regular.ttfzNotoSansHebrew-Regular.otfzNotoSansBengali-RegularzNotoSansBengali-Regular.ttfzNotoSansBengali-Regular.otfzNotoSansTamil-RegularzNotoSansTamil-Regular.ttfzNotoSansTamil-Regular.otfzNotoSansGujarati-RegularzNotoSansGujarati-Regular.ttfzNotoSansGujarati-Regular.otfzNotoSansTelugu-RegularzNotoSansTelugu-Regular.ttfzNotoSansTelugu-Regular.otfzNotoSansKannada-RegularzNotoSansKannada-Regular.ttfzNotoSansKannada-Regular.otfzNotoSansMalayalam-RegularzNotoSansMalayalam-Regular.ttfzNotoSansMalayalam-Regular.otfzNotoSansMyanmar-RegularzNotoSansMyanmar-Regular.ttfzNotoSansMyanmar-Regular.otfzNotoSansKhmer-RegularzNotoSansKhmer-Regular.ttfzNotoSansKhmer-Regular.otfzNotoSansLao-RegularzNotoSansLao-Regular.ttfzNotoSansLao-Regular.otfzNotoSansGeorgian-RegularzNotoSansGeorgian-Regular.ttfzNotoSansGeorgian-Regular.otfzNotoSansArmenian-RegularzNotoSansArmenian-Regular.ttfzNotoSansArmenian-Regular.otfzNotoSansEthiopic-Regular.ttfzNotoSansEthiopic-Regular.otfzNotoSansSinhala-Regular.ttfzNotoSansSinhala-Regular.otfzNotoSansGurmukhi-Regular.ttfzNotoSansGurmukhi-Regular.otfzNotoSansOriya-Regular.ttfzNotoSansOriya-Regular.otfzNotoSansTibetan-Regular.ttfzNotoSansTibetan-Regular.otf)zNotoSansEthiopic-RegularzNotoSansSinhala-RegularzNotoSansGurmukhi-RegularzNotoSansOriya-RegularzNotoSansTibetan-Regularzdict[str, list[str]]NOTO_FONT_PATTERNSc                >    i | _         t               | _        d| _        y)z2Initialize system font provider with empty caches.N)_font_cacheset
_not_found
_font_dirsselfs    X/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/font/system_font_provider.py__init__zSystemFontProvider.__init__   s     46$'E-1    c                z    t         j                  dk(  ryt         j                  dk(  rydt         j                  v ryy)zGet the current platform identifier.

        Returns:
            Platform string: 'linux', 'darwin', 'windows', or 'freebsd'
        win32windowsr
   r	   r   )sysplatformr   s    r   _get_platformz SystemFontProvider._get_platform   s4     <<7"\\X%#,,&r   c                   | j                   | j                   S | j                         }|dk(  rt        j                  j	                  dd      }t        |      dz  g| _         t        j                  j	                  d      }|r-| j                   j                  t        |      dz  dz  dz         | j                   S t        | j                  j	                  |g             | _         | j                   S )zGet font directories for the current platform.

        Returns:
            List of paths to search for fonts (may include non-existent paths)
        r   WINDIRz
C:\WindowsFontsLOCALAPPDATA	MicrosoftWindows)	r   r   osenvirongetr   appendlistr   )r   r   windirlocalappdatas       r   _get_font_dirsz!SystemFontProvider._get_font_dirs   s     ??&??"%%'y ZZ^^Hm<F#F|g56DO::>>.9L&&&4y@7J  #4#8#8#<#<Xr#JKDOr   c                8   || j                   vry| j                   |   }| j                         D ]Z  }|j                         s|D ]B  }	 t        |j	                  |            }|r#t
        j                  d||d          |d   c c S D \ y# t        $ r Y Sw xY w)zSearch system directories for a font file.

        Args:
            font_name: Logical font name (e.g., 'NotoSansCJK-Regular')

        Returns:
            Path to font file if found, None otherwise
        NzFound system font %s at %sr   )r   r*   existsr'   rgloblogdebugPermissionError)r   	font_namepatternsfont_dirpatternmatchess         r   _find_font_filez"SystemFontProvider._find_font_file   s     D333**95++- 	H??$# 	"8>>'#:;G		8)WQZ  'qz)	 			" 	 ' s   	:B	BBc                N   || j                   v r| j                   |   S || j                  v ry| j                  |      }|	 t        |      }|| j                   |<   |S | j                  j                  |       y# t        $ r"}t
        j                  d|||       Y d}~Bd}~ww xY w)a  Get a FontManager for the named font (lazy loading).

        This method implements lazy scanning: fonts are only searched for
        when first requested. Results (both positive and negative) are
        cached for subsequent calls.

        Args:
            font_name: Logical font name (e.g., 'NotoSansCJK-Regular')

        Returns:
            FontManager if font is found and loadable, None otherwise
        Nz*Found font %s at %s but failed to load: %s)r   r   r6   r   	Exceptionr.   warningadd)r   r1   	font_pathfmes        r   get_fontzSystemFontProvider.get_font   s     (((##I.. ' ((3	 
 +.0  +	 	I&  @	 s   A9 9	B$BB$c                H    t        | j                  j                               S )a9  Get list of font names this provider can potentially find.

        Note: This returns all font names we know patterns for, not
        necessarily fonts that are actually installed. Use get_font()
        to check if a specific font is available.

        Returns:
            List of logical font names
        )r'   r   keysr   s    r   get_available_fontsz&SystemFontProvider.get_available_fonts  s     D++00233r   c                    t        d      )zGet the glyphless fallback font.

        Raises:
            NotImplementedError: System provider doesn't provide fallback.
                Use BuiltinFontProvider for the fallback font.
        zfSystemFontProvider does not provide a fallback font. Use BuiltinFontProvider for Occulta.ttf fallback.)NotImplementedErrorr   s    r   get_fallback_fontz$SystemFontProvider.get_fallback_font  s     "@
 	
r   N)returnNone)rE   str)rE   z
list[Path])r1   rG   rE   zPath | None)r1   rG   rE   zFontManager | None)rE   z	list[str])rE   r   )__name__
__module____qualname____doc__r   homer   __annotations__r   r   r   r*   r6   r>   rA   rD    r   r   r   r      s    #$)*DIIK("DIIK..	
 )*DIIK("

 !"()DIIK/)
/+ *a0""
a0
 	!((#
a0 	%,,'
a0 	  
a02 	&&!
3a0: 	!((#
;a0B 	"))$
Ca0J 	 ''"
Ka0R 	#**%
Sa0Z 	!((#
[a0b 	"))$
ca0j 	$++&
ka0r 	"))$
sa0z 	 ''"
{a0B 	%% 
Ca0J 	#**%
Ka0R 	#**%
Sa0\ +*%

 *)$

 +*%

 ('"

 *)$
{a0, aF24B&P
4

r   r   )rK   
__future__r   loggingr#   r   pathlibr   ocrmypdf.font.font_managerr   	getLoggerrH   r.   r   rN   r   r   <module>rT      s<    #  	 
  2g!S
 S
r   