a
    [¨Xh4D  ã                   @   sD  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ƒZ1d 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   sP   t  | t j¡}|jdkrdS t  | ¡}t  |¡}|tjkrDtdƒ n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   ZFcConfigGetFontsZFcSetSystemZnfontZFcConfigGetConfigFilesZFcStrListNextr   ÚNULLr   )Zfont_configZfontsÚconfig_filesÚconfig_file© r(   úZ/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/weasyprint/text/fonts.pyÚ_check_font_configuration   s    




r*   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 )zŸCreate 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#   Zpango_ft2_font_map_newr    Zg_object_unrefÚfont_mapZpango_fc_font_map_set_configÚcastÚ_folder©Úselfr(   r(   r)   Ú__init__X   s    
ÿ
ÿÿzFontConfiguration.__init__c           *      C   sv  t |ƒ}t| ¡ dd ¡ }| jdu r6ttddƒ| _| j| }| ¡ rLdS t 	d¡}|d D ] \}}|du s^|dkrzq^|d	krŽ| ¡ }	t 
t ¡ tj¡}
t | j|
tj¡ t |
¡ t |
d
|	¡ t |
d|	¡ t 	d¡}t | j|
|¡}|tjkr
t d|¡ q^dD ]l}t ||d|¡ t |d ¡}|	 ¡ | ¡ krt |dd|¡ t |d ¡ t¡}t|ƒ ¡ } qŽqt d|	 ¡ ¡ q^zRt||ƒ2}d|v }|r²|d n
|d  ¡ }W d  ƒ n1 sÔ0    Y  W n@ ty  } z&t d||¡ W Y d}~q^W Y d}~n
d}~0 0 zx|dd… dkr˜t ƒ }|dd… }|dkrvt!t |ƒƒ}d |_"|_#| $|¡ n|dkrt% &t |ƒ|¡ | '¡ }W n@ tyÚ } z&t d||¡ W Y d}~q^W Y d}~n
d}~0 0 | (|¡ d}t)dƒ}t*|ddd}t*|d d!d"d#}t |ƒt*|dƒ_+t*|d$d%|d&}|d' t*|dƒ_+d(|v rrt*|d$d)|d&}t,|d(  }|t*|d*ƒ_+d+|v r¨t*|d$d,|d&}t-|d+  }t |ƒt*|d-ƒ_+d.|v rÚt*|d$d/|d&}t.|d.  }|t*|d*ƒ_+t*|dd0d}t*|d d!d"d#}t |ƒt*|dƒ_+d1d2„ | /d3g ¡D ƒ}| /d4d5¡}t0f d4|i|¤Ž}|r|t*|d$d6|d&}| 1¡ D ] \} }!| › d7|!› t*|dƒ_+qZ| /d8¡ }"rît*|d$d9|d&}t*|d:ƒ}#|"D ]B}$t*|#d9ƒ}%t*|%d;ƒ}&|$j2|$j3fD ]}!d<|!d=›t*|&d-ƒ_+qÎqªd>}'d? 4g |'¢t5|d@dA‘R ¡}(t 6| j|(dB¡ t 7| jt |ƒ t¡¡})|)rTt8 9t :dC| j;¡¡  S t dD|¡ q^t <dE|d' ¡ dS )Fz0Add a font face to the Fontconfig configuration.F)ÚusedforsecurityNzweasyprint-)Úprefixz
FcChar8 **ÚsrcZinternalÚlocaló   fullnameó   postscriptnamez
FcResult *z(Failed to get matching local font for %r)r;   r<   r   s   filezFailed to load local font %rÚstringZfile_objzFailed to load font at %r (%s)é   s   wOFé   ó   Fó   2z%Failed to handle woff font at %r (%s)Zassign_replacer   ÚmatchÚscan)ÚtargetÚtestÚfileÚeq)ÚnameÚcompareÚeditÚfamily)rH   ÚmodeÚfont_familyÚ
font_styleZslantÚconstÚfont_weightZweightÚintÚfont_stretchÚwidthÚfontc                 S   s*   i | ]"}|d  d    dd¡|d  d “qS )r   ú-Ú_r   )Úreplace)Ú.0Úrulesr(   r(   r)   Ú
<dictcomp>Ó   s   z3FontConfiguration.add_font_face.<locals>.<dictcomp>Úfont_variantÚfont_feature_settingsÚnormalZfontfeaturesú Úunicode_rangeÚcharsetÚplusÚrangeÚ0xÚx)s   <?xml version="1.0"?>s7   <!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">ó   
zutf-8)ÚencodingTr,   zFailed to load font at %rzFont-face %r cannot be loaded)=Ústrr   ÚencodeÚ	hexdigestr3   r   r   Úexistsr   Únewr-   r   ZFcPatternCreateZFcPatternDestroyZFcConfigSubstituter0   ZFcMatchFontZFcDefaultSubstituteZFcPatternAddStringZFcFontMatchr%   r   ÚdebugZFcPatternGetStringr=   ÚlowerÚdecoder   Úas_urir   ÚreadÚ	Exceptionr   r   ZflavorZ
flavorDataÚsaver   Ú
decompressÚgetvalueÚwrite_bytesr   r	   Útextr   r   r   ÚgetÚfont_featuresÚitemsÚstartÚendÚjoinr
   ZFcConfigParseAndLoadFromMemoryZFcConfigAppFontAddFiler#   Z pango_fc_font_map_config_changedr2   r1   Úwarning)*r5   Zrule_descriptorsZurl_fetcherZ
config_keyZconfig_digestZ	font_pathr=   Z	font_typeÚurlZ	font_nameÚpatternÚresultZmatching_patternÚtagrH   ÚpathrT   Ú	exceptionÚoutZwoff_version_byteZttfontÚexcrL   ÚrootrB   rE   rJ   rv   ÚintegerZdescriptorsÚsettingsÚfeaturesÚkeyÚvalueZunicode_rangesra   r_   r`   Zrange_ÚheaderÚxmlZ
font_addedr(   r(   r)   Úadd_font_facel   sà    




ÿ
ÿ

ÿ
>"

"




þ


ÿÿzFontConfiguration.add_font_facec                 C   s   | j rt| j dd dS )z*Clean a font configuration for a document.T)Úignore_errorsN)r3   r   r4   r(   r(   r)   Ú__del__õ   s    zFontConfiguration.__del__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r6   rŽ   r   r(   r(   r(   r)   r+   J   s    
r+   r]   c                 C   sD  i }| dkrt | dkƒ|d< |dkrFt ¡ D ]}	|	D ]}
d||
< q4q,nF|dkrŒ|D ]8}d}| d¡rtd}|dd	… }t| D ]}
|||
< q|qR|d
kržd|d< n|dkr®d|d< |dkrÌt| D ]}
d||
< q¾|dkrê|D ]}
d|t|
 < qØ|dkr|dkrd|d< |dkr(|D ]}
d|t|
 < q|dkr@| t|ƒ¡ |S )z‚Get the font features from the different properties in style.

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

    Úautor]   ÚkernÚnoner   r   zno-r>   NÚsubÚsubsÚsuperZsupszhistorical-formsÚhist)	rQ   r   ÚvaluesÚ
startswithr   r   r   ÚupdateÚdict)Zfont_kerningZfont_variant_ligaturesZfont_variant_positionÚfont_variant_capsZfont_variant_numericZfont_variant_alternatesZfont_variant_east_asianr\   r‰   ÚkeysrŠ   Zligature_typer‹   r(   r(   r)   rx   û   sD    	






rx   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ràd d
d„ | d D ƒ¡ ¡ }	t ||	¡ |S )z/Get font description string out of given style.ú,rM   rN   rR   rP   Ú	font_sizer    Zfont_variation_settingsr]   c                 s   s    | ]\}}|› d |› V  qdS )ú=Nr(   )rX   rŠ   r‹   r(   r(   r)   Ú	<genexpr>R  s   z'get_font_description.<locals>.<genexpr>)r   r-   r"   Zpango_font_description_newÚpango_font_description_freer$   r|   Z!pango_font_description_set_familyr   Z pango_font_description_set_styler   Z"pango_font_description_set_stretchZ!pango_font_description_set_weightrQ   r   Z(pango_font_description_set_absolute_sizer   Z"pango_font_description_set_variantrh   Z%pango_font_description_set_variations)
ÚstyleZfont_descriptionZfamily_prK   rN   rR   rP   r£   r[   r=   r(   r(   r)   Ú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 *r,   )r   r2   r"   Zpango_font_get_font_mapr#   Zpango_fc_font_map_get_hb_face)Ú
pango_fontZfc_fontZfontmapr(   r(   r)   Úget_pango_font_hb_faceY  s    rª   Nc                 C   s    |dkrt  | |¡}n |dkr,t  | |¡}n
t  | ¡}t d¡L}t  ||¡}|tjkr\dnt |t	|d ƒ¡}t  
|¡ |W  d  ƒ S 1 s’0    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.

    ZpngÚsvgzunsigned int *Nr   )r!   Zhb_ot_color_glyph_reference_pngZhb_ot_color_glyph_reference_svgZhb_face_reference_blobr   rk   Zhb_blob_get_datar%   ÚunpackrQ   Zhb_blob_destroy)Z	hb_objectZot_colorZglyphZhb_blobÚlengthZhb_dataÚdatar(   r(   r)   Ú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   r-   r"   Zpango_font_describer¦   Zpango_font_description_get_sizer   ZPANGO_FONT_MASK_SIZEZPANGO_FONT_MASK_GRAVITYZ#pango_font_description_unset_fieldsZpango_font_description_hash)r©   Údescriptionr£   Úmaskr(   r(   r)   Úget_pango_font_keyt  s    ÿr²   )r]   r]   r]   r]   r]   r]   r]   r]   )NN)7r”   Úhashlibr   Úior   Úpathlibr   Úshutilr   Útempfiler   Úwarningsr   Úxml.etree.ElementTreer   r	   r
   Z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$   r*   r-   r.   r/   r+   rx   r¨   rª   r¯   r²   r(   r(   r(   r)   Ú<module>   s6   0,.
ÿ 2    ý
F
