o
    Zh0h4D                     @   sL  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZmZ dd	lmZmZ d
dlmZ d
dlmZmZ ddl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(m)Z)m*Z*m+Z+m,Z, dd Z-e-e$.e'/ e'j0 G dd dZ1				d ddZ2dd Z3dd Z4d!ddZ5dd Z6dS )"zIInterface with external libraries managing fonts installed on the system.    )md5)BytesIO)Path)rmtree)mkdtemp)warn)Element
SubElementtostring)TTFontwoff2   )LOGGER)FILESYSTEM_ENCODINGfetch   )
	CAPS_KEYSEAST_ASIAN_KEYSFONTCONFIG_STRETCHFONTCONFIG_STYLEFONTCONFIG_WEIGHTLIGATURE_KEYSNUMERIC_KEYSPANGO_STRETCHPANGO_STYLEPANGO_VARIANT)	
FROM_UNITSTO_UNITSffi
fontconfiggobjectharfbuzzpangopangoft2unicode_to_char_pc                 C   sR   t | t j}|jdkrdS t | }t |}|tjkr#td dS td dS )u  Check whether the given font_config has fonts.

    The default fontconfig configuration file may be missing (particularly
    on Windows or macOS, where installation of fontconfig isn't as
    standardized as on Linux), resulting in "Fontconfig error: Cannot load
    default config file".

    Fontconfig tries to retrieve the system fonts as fallback, which may or
    may not work, especially on macOS, where fonts can be installed at
    various loactions. On Windows (at least since fontconfig 2.13) the
    fallback seems to work.

    If there’s no default configuration and the system fonts fallback
    fails, or if the configuration file exists but doesn’t provide fonts,
    output will be ugly.

    If you happen to have no fonts and an HTML document without a valid
    @font-face, all letters turn into rectangles.

    If you happen to have an HTML document with at least one valid
    @font-face, all text is styled with that font.

    On Windows and macOS we can cause Pango to use native font rendering
    instead of rendering fonts with FreeType. But then we must do without
    @font-face. Expect other missing features and ugly output.

    r   Nz?FontConfig cannot load default config file. Expect ugly output.z6No fonts configured in FontConfig. Expect ugly output.)	r   FcConfigGetFontsFcSetSystemnfontFcConfigGetConfigFilesFcStrListNextr   NULLr   )font_configfontsconfig_filesconfig_file r/   K/var/www/html/rh/venv/lib/python3.10/site-packages/weasyprint/text/fonts.py_check_font_configuration   s   



r1   c                   @   s,   e Zd ZdZdZdd Zdd Zdd ZdS )	FontConfigurationa  A Fontconfig font configuration.

    Keep a list of fonts, including fonts installed on the system, fonts
    installed for the current user, and fonts referenced by cascading
    stylesheets.

    When created, an instance of this class gathers available fonts. It can
    then be given to :class:`weasyprint.HTML` methods or to
    :class:`weasyprint.CSS` to find fonts in ``@font-face`` rules.

    Nc                 C   sV   t t tj| _t t tj	| _
tt d| j
| j t| j d| _dS )zCreate a Fontconfig font configuration.

        See Behdad's blog:
        https://mces.blogspot.fr/2015/05/how-to-use-custom-application-fonts.html

        PangoFcFontMap *N)r   gcr   FcInitLoadConfigAndFontsFcConfigDestroy_configr#   pango_ft2_font_map_newr    g_object_unreffont_mappango_fc_font_map_set_configcast_folderselfr/   r/   r0   __init__X   s   


zFontConfiguration.__init__c           *      C   sP  t |}t| dd }| jdu rttdd| _| j| }| r&dS t	d}|d D ]m\}}|du s<|dkr=q/|d	kr| }	t
t tj}
t| j|
tj t|
 t|
d
|	 t|
d|	 t	d}t| j|
|}|tjkrtd| q/dD ]3}t||d| t|d }|	 | krt|dd| t|d t}t| } n
qtd|	  q/z't||}d|v }|r|d n|d  }W d   n1 sw   Y  W n ty } ztd|| W Y d}~q/d}~ww z<|dd dkr?t  }|dd }|dkr.t!t |}d |_"|_#|$| n|dkr;t%&t || |' }W n tyZ } ztd|| W Y d}~q/d}~ww |(| d}t)d}t*|ddd}t*|d d!d"d#}t |t*|d_+t*|d$d%|d&}|d' t*|d_+d(|v rt*|d$d)|d&}t,|d(  }|t*|d*_+d+|v rt*|d$d,|d&}t-|d+  }t |t*|d-_+d.|v rt*|d$d/|d&}t.|d.  }|t*|d*_+t*|dd0d}t*|d d!d"d#}t |t*|d_+d1d2 |/d3g D }|/d4d5}t0dFd4|i|}|r+t*|d$d6|d&}|1 D ]\} }!|  d7|! t*|d_+q|/d8 }"rdt*|d$d9|d&}t*|d:}#|"D ]!}$t*|#d9}%t*|%d;}&|$j2|$j3fD ]}!d<|!d=t*|&d-_+qTqBd>}'d?4g |'t5|d@dAR }(t6| j|(dB t7| jt |t})|)rt89t:dC| j;  S tdD| q/t<dE|d'  dS )Gz0Add a font face to the Fontconfig configuration.F)usedforsecurityNzweasyprint-)prefixz
FcChar8 **srcinternallocal   fullname   postscriptnamez
FcResult *z(Failed to get matching local font for %r)rF   rG   r   s   filezFailed to load local font %rstringfile_objzFailed to load font at %r (%s)   s   wOF      F   2z%Failed to handle woff font at %r (%s)assign_replacer   matchscan)targettestfileeq)namecompareeditfamily)rU   modefont_family
font_styleslantconstfont_weightweightintfont_stretchwidthfontc                 S   s*   i | ]}|d  d   dd|d  d qS )r   -_r   )replace).0rulesr/   r/   r0   
<dictcomp>   s    z3FontConfiguration.add_font_face.<locals>.<dictcomp>font_variantfont_feature_settingsnormalfontfeatures unicode_rangecharsetplusrange0xx)s   <?xml version="1.0"?>s7   <!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">   
zutf-8)encodingTr3   zFailed to load font at %rzFont-face %r cannot be loadedr/   )=strr   encode	hexdigestr=   r   r   existsr   newr4   r   FcPatternCreateFcPatternDestroyFcConfigSubstituter7   FcMatchFontFcDefaultSubstituteFcPatternAddStringFcFontMatchr*   r   debugFcPatternGetStringrH   lowerdecoder   as_urir   read	Exceptionr   r   flavor
flavorDatasaver   
decompressgetvaluewrite_bytesr   r	   textr   r   r   getfont_featuresitemsstartendjoinr
   FcConfigParseAndLoadFromMemoryFcConfigAppFontAddFiler#    pango_fc_font_map_config_changedr<   r:   warning)*r?   rule_descriptorsurl_fetcher
config_keyconfig_digest	font_pathrH   	font_typeurl	font_namepatternresultmatching_patterntagrU   pathrc   	exceptionoutwoff_version_bytettfontexcrY   rootrO   rR   rW   r   integerdescriptorssettingsfeatureskeyvalueunicode_rangesrq   ro   rp   range_headerxml
font_addedr/   r/   r0   add_font_facel   s   

















zFontConfiguration.add_font_facec                 C   s   | j rt| j dd dS dS )z*Clean a font configuration for a document.T)ignore_errorsN)r=   r   r>   r/   r/   r0   __del__   s   zFontConfiguration.__del__)__name__
__module____qualname____doc__r=   r@   r   r   r/   r/   r/   r0   r2   J   s     
r2   rl   c                 C   s:  i }| dkrt | dk|d< |dkr#t D ]}	|	D ]}
d||
< qqn#|dkrF|D ]}d}|dr:d}|dd	 }t| D ]}
|||
< q>q)|d
krOd|d< n|dkrWd|d< |dkrft| D ]}
d||
< q_|dkru|D ]}
d|t|
 < ql|dkr|dkrd|d< |dkr|D ]}
d|t|
 < q|dkr|t| |S )zGet the font features from the different properties in style.

    See https://www.w3.org/TR/css-fonts-3/#feature-precedence

    autorl   kernnoner   r   zno-rJ   Nsubsubssupersupszhistorical-formshist)	r`   r   values
startswithr   r   r   updatedict)font_kerningfont_variant_ligaturesfont_variant_positionfont_variant_capsfont_variant_numericfont_variant_alternatesfont_variant_east_asianrk   r   keysr   ligature_typer   r/   r/   r0   r      sJ   	




r   c           
      C   s   t t tj}td| d \}}t|| t| d  }t	|| t
| d  }t|| | d }t|| t| d t }t|| t| d  }t|| | d d	krpdd
d | d D  }	t||	 |S )z/Get font description string out of given style.,rZ   r[   ra   r^   	font_sizer   font_variation_settingsrl   c                 s   s"    | ]\}}| d | V  qdS )=Nr/   )rg   r   r   r/   r/   r0   	<genexpr>R  s    
z'get_font_description.<locals>.<genexpr>)r   r4   r"   pango_font_description_newpango_font_description_freer$   r   !pango_font_description_set_familyr    pango_font_description_set_styler   "pango_font_description_set_stretch!pango_font_description_set_weightr`   r   (pango_font_description_set_absolute_sizer   "pango_font_description_set_variantrx   %pango_font_description_set_variations)
stylefont_descriptionfamily_prX   r[   ra   r^   r   rj   rH   r/   r/   r0   get_font_descriptionA  s.   

r   c                 C   s*   t d| }t dt| }t||S )z*Get Harfbuzz face out of given Pango font.zPangoFcFont *r3   )r   r<   r"   pango_font_get_font_mapr#   pango_fc_font_map_get_hb_face)
pango_fontfc_fontfontmapr/   r/   r0   get_pango_font_hb_faceY  s   r   Nc                 C   s   |dkrt | |}n|dkrt | |}nt | }td&}t ||}|tjkr.dn	t|t	|d }t 
| |W  d   S 1 sIw   Y  dS )u   Get binary data out of given Harfbuzz font or face.

    If ``ot_color`` is 'svg', return the SVG color glyph reference. If it’s 'png',
    return the PNG color glyph reference. Otherwise, return the whole face blob.

    pngsvgzunsigned int *Nr   )r!   hb_ot_color_glyph_reference_pnghb_ot_color_glyph_reference_svghb_face_reference_blobr   r{   hb_blob_get_datar*   unpackr`   hb_blob_destroy)	hb_objectot_colorglyphhb_bloblengthhb_datadatar/   r/   r0   get_hb_object_data`  s   
"
$r   c                 C   sJ   t t| tj}t|t }tjtj }t	|| t
|||fS )z*Get key corresponding to given Pango font.)r   r4   r"   pango_font_describer   pango_font_description_get_sizer   PANGO_FONT_MASK_SIZEPANGO_FONT_MASK_GRAVITY#pango_font_description_unset_fieldspango_font_description_hash)r   descriptionr   maskr/   r/   r0   get_pango_font_keyt  s   r  )rl   rl   rl   rl   rl   rl   rl   rl   )NN)7r   hashlibr   ior   pathlibr   shutilr   tempfiler   warningsr   xml.etree.ElementTreer   r	   r
   fontTools.ttLibr   r   loggerr   urlsr   r   	constantsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r1   r4   r5   r6   r2   r   r   r   r   r  r/   r/   r/   r0   <module>   s8    0,.
 2
F
