a
    •a‡B  ã                   @   sþ   d 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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 ddlmZ dZdd	d
ddddœZedddƒedddƒdddœZdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ de	ƒZ dS )aW  

Iconic Font
===========

A lightweight module handling iconic fonts.

It is designed to provide a simple way for creating QIcons from glyphs.

From a user's viewpoint, the main entry point is the ``IconicFont`` class which
contains methods for loading new iconic fonts with their character map and
methods returning instances of ``QIcon``.

é    )Úprint_functionN)Ú
QByteArrayÚQObjectÚQPointÚQRectÚQt)ÚQColorÚQFontÚQFontDatabaseÚQIconÚQIconEngineÚQPainterÚQPixmapÚ
QTransform)ÚQApplicationFZ b06871f281fee6b241d60582ae9369b9Z 6a745dc6a0871f350b0219f5a2678838Z acf50f59802f20d8b45220eaae532a1cZ ed2b8bf117160466ba6220a8f1da54a4Z 207966b04c032d5b873fd595a211582eZ b0fd91bb29dcb296a9a37f8bda0a2d85)zfontawesome4.7-webfont.ttfz fontawesome5-regular-webfont.ttfzfontawesome5-solid-webfont.ttfzfontawesome5-brands-webfont.ttfzelusiveicons-webfont.ttfzmaterialdesignicons-webfont.ttfé2   é–   ç      ð?)ÚcolorÚcolor_disabledÚopacityÚscale_factorc                  K   s>   g d¢}| D ],}||v r&| | t |< qd |¡}t|ƒ‚qdS )z?Set global defaults for the options passed to the icon painter.)ÚactiveÚselectedÚdisabledÚonÚoffÚ	on_activeÚon_selectedÚon_disabledÚ
off_activeÚoff_selectedÚoff_disabledr   Úcolor_onÚ	color_offÚcolor_activeÚcolor_selectedr   Úcolor_on_selectedÚcolor_on_activeÚcolor_on_disabledÚcolor_off_selectedÚcolor_off_activeÚcolor_off_disabledÚ	animationÚoffsetr   ÚrotatedÚhflipÚvflipzInvalid option '{0}'N)Ú_default_optionsÚformatÚKeyError)ÚkwargsZvalid_optionsÚkwÚerror© r8   ú4lib/python3.9/site-packages/qtawesome/iconic_font.pyÚset_global_defaults3   s    
r:   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCharIconPainterzChar icon painter.c              	   C   s"   |D ]}|   ||||||¡ qdS )zMain paint method.N)Ú_paint_icon)ÚselfÚiconicÚpainterÚrectÚmodeÚstateÚoptionsÚoptr8   r8   r9   ÚpaintM   s    zCharIconPainter.paintc                 C   sÈ  |  ¡  |d }|d }tjtj|d |d ftj|d |d ftj|d |d ftj|d	 |d
 fitjtj|d |d ftj|d |d ftj|d |d ftj|d |d fii}	|	| | \}}| t	|ƒ¡ t
d| ¡  |d  ƒ}
|d }| d¡}|dur| | ||¡ | | ||
¡¡ d|v rpt|ƒ}| t
|d d | ¡  ƒt
|d d | ¡  ƒ¡ d|v rÚ|d dkrÚ| ¡ d }| ¡ d }| ||¡ tƒ }| dd¡ | |d¡ | | | ¡ d|v rD|d dkrD| ¡ d }| ¡ d }| ||¡ tƒ }| dd¡ | |d¡ | | | ¡ d |v r| ¡ d }| ¡ d }| ||¡ | |d  ¡ | | | ¡ | | d!d"¡¡ | |ttjtjB ƒ|¡ | ¡  dS )#zPaint a single icon.r   Úcharr#   r   r)   r   r(   r   r'   r   r$   r   r,   r"   r+   r    r*   r!   g      ì?r   Úprefixr-   Nr.   r   é   r1   Tg      à?éÿÿÿÿr0   r/   r   r   )Zsaver   ZOnZNormalZDisabledZActiveZSelectedZOffZsetPenr   ÚroundZheightÚgetZsetupZsetFontÚfontr   Ú	translateÚwidthr   ZscaleZsetTransformÚrotateZ
setOpacityZdrawTextÚintr   ZAlignCenterZAlignVCenterZrestore)r=   r>   r?   r@   rA   rB   rC   r   rF   Zcolor_optionsZ	draw_sizerG   r-   Zx_centerZy_centerZ	transfromr8   r8   r9   r<   R   s„    
ÿ
ÿ
ÿú

ÿ
ÿ
ÿúõ


ÿ
zCharIconPainter._paint_iconN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   r<   r8   r8   r8   r9   r;   I   s   r;   c                   @   s   e Zd ZdZdS )Ú	FontErrorzException for font errors.N)rQ   rR   rS   rT   r8   r8   r8   r9   rU   ¤   s   rU   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCharIconEnginez<Specialization of QIconEngine used to draw font-based icons.c                    s    t ƒ  ¡  || _|| _|| _d S ©N)ÚsuperÚ__init__r>   r?   rC   )r=   r>   r?   rC   ©Ú	__class__r8   r9   rY   ¬   s    
zCharIconEngine.__init__c                 C   s   | j  | j||||| j¡ d S rW   )r?   rE   r>   rC   )r=   r?   r@   rA   rB   r8   r8   r9   rE   ²   s    ÿzCharIconEngine.paintc                 C   s8   t |ƒ}| tj¡ |  t|ƒttddƒ|ƒ||¡ |S )Nr   )r   Úfillr   ZtransparentrE   r   r   r   )r=   ÚsizerA   rB   Zpmr8   r8   r9   Úpixmap¶   s     zCharIconEngine.pixmap)rQ   rR   rS   rT   rY   rE   r^   Ú__classcell__r8   r8   rZ   r9   rV   ¨   s   rV   c                       sb   e Zd ZdZ‡ fdd„Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )Ú
IconicFontz%Main class for managing iconic fonts.c                    sH   t ƒ  ¡  tƒ | _i | _i | _i | _i | _i | _|D ]}| j	|Ž  q4dS )aÌ  IconicFont Constructor.

        Parameters
        ----------
        ``*args``: tuples
            Each positional argument is a tuple of 3 or 4 values:
            - The prefix string to be used when accessing a given font set,
            - The ttf font filename,
            - The json charmap filename,
            - Optionally, the directory containing these files. When not
              provided, the files will be looked for in ``./fonts/``.
        N)
rX   rY   r;   r?   ÚpaintersÚfontnameÚfontidsÚcharmapÚ
icon_cacheÚ	load_font)r=   ÚargsZfargsrZ   r8   r9   rY   Á   s    
zIconicFont.__init__Nc                 C   s®  dd„ }|du r.t j t j t j t¡¡d¡}t ¡ durªtt j ||¡dƒ"}t	 
t| ¡ ƒ¡}W d  ƒ n1 sx0    Y  | ¡  t	 |¡}|r²|| j|< |d | j|< ntd t j ||¡¡ƒ‚tt j ||¡dƒ$}	tj|	|d	| j|< W d  ƒ n1 s
0    Y  tsªt |d¡}
|
durªt ¡ }tt j ||¡dƒ"}| ¡ }| |¡ W d  ƒ n1 sv0    Y  | ¡ }||
krªtd
 t j ||¡¡ƒ‚dS )aÔ  Loads a font file and the associated charmap.

        If ``directory`` is None, the files will be looked for in ``./fonts/``.

        Parameters
        ----------
        prefix: str
            Prefix string to be used when accessing a given font set
        ttf_filename: str
            Ttf font filename
        charmap_filename: str
            Charmap filename
        directory: str or None, optional
            Directory for font and charmap files
        c              
   S   sh   i }| D ]Z}zt t| | dƒƒ||< W q ty`   t| | dƒdkrHntd || | ¡ƒ‚Y q0 q|S )Né   iÿÿ  z Failed to load character {0}:{1})ÚchrrP   Ú
ValueErrorrU   r3   )ÚobjÚresultÚkeyr8   r8   r9   Úhooké   s    ÿz"IconicFont.load_font.<locals>.hookNZfontsÚrbr   zÈFont at '{0}' appears to be empty. If you are on Windows 10, please read https://support.microsoft.com/en-us/kb/3053676 to know how to prevent Windows from blocking the fonts that come with QtAwesome.Úr)Zobject_hookzFont is corrupt at: '{0}')ÚosÚpathÚjoinÚdirnameÚrealpathÚ__file__r   ÚinstanceÚopenr
   ZaddApplicationFontFromDatar   ÚreadÚcloseZapplicationFontFamiliesrc   rb   rU   r3   ÚjsonÚloadrd   ÚSYSTEM_FONTSÚ
MD5_HASHESrK   ÚhashlibZmd5ÚupdateZ	hexdigest)r=   rG   Zttf_filenameZcharmap_filenameZ	directoryrn   Z	font_dataZid_ZloadedFontFamiliesZcodesZttf_hashZhasherÚfZcontentZttf_calculated_hash_coder8   r8   r9   rf   Ø   sB    ÿ0

ú4
ÿ*
ÿzIconicFont.load_fontc              	   O   sÌ   d  ||¡}|| jvrÂ| di gt|ƒ ¡}|}t|ƒt|ƒkrVd  t|ƒ¡}t|ƒ‚t ¡ dur²g }tt|ƒƒD ]$}|| }	| |  	|	||| ¡¡ qr|}
|  
| j|
¡| j|< nt d¡ tƒ S | j| S )z>Return a QIcon object corresponding to the provided icon name.z{}{}rC   z$"options" must be a list of size {0}Nz9You need to have a running QApplication to use QtAwesome!)r3   re   ÚpopÚlenÚ	Exceptionr   rw   ÚrangeÚappendÚ_parse_optionsÚ_icon_by_painterr?   ÚwarningsÚwarnr   )r=   Únamesr5   Z	cache_keyZoptions_listÚgeneral_optionsr7   Zparsed_optionsÚiÚspecific_optionsZapi_optionsr8   r8   r9   Úicon!  s(    

þ
zIconicFont.iconc                    s¼  t tfi |¤Ž}| |¡ g d¢}| dˆ¡}| d|¡}| d|¡}| d|¡}	| d|	¡}
| d|¡}| d|	¡}| d	|
¡}| d
|¡}| d|	¡}| d|
¡}| d|¡}||||	|
|||||||dœ‰ ‡ ‡fdd„|D ƒ}|  |¡\}}| t t||fŽ ƒ¡ | d|i¡ | d¡}| d|¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|¡ | d|d ¡ | d|d ¡ | d|d ¡ |S )N)rF   r   r   r   r   r   r   r   r   r    r!   r"   rF   r   r   r   r   r   r   r   r   r    r!   r"   c                    s   g | ]}ˆ   |ˆ¡‘qS r8   )rK   )Ú.0r6   ©Z	icon_dictÚnamer8   r9   Ú
<listcomp>a  ó    z-IconicFont._parse_options.<locals>.<listcomp>rG   r   r#   r%   r&   r(   r'   r)   r   r$   r+   r*   r,   )Údictr2   r€   rK   Ú_get_prefix_charsÚzipÚ
setdefault)r=   rŽ   rŒ   r’   rC   Zicon_kwrF   r   r   r   r   r   r   r   r   r    r!   r"   r‹   rG   Úcharsr   r8   r‘   r9   r‡   >  sX    
õ
zIconicFont._parse_optionsc                 C   sŒ   g }|D ]z}d|v rz|  d¡\}}|| jv rf|| j| v rP| | j| | ¡ qxd ||¡}t|ƒ‚q‚d |¡}t|ƒ‚qtdƒ‚q||fS )NÚ.z%Invalid icon name "{0}" in font "{1}"zInvalid font prefix "{0}"zInvalid icon name)Úsplitrd   r†   r3   r„   )r=   r‹   r™   r’   rG   Únr7   r8   r8   r9   r–   v  s    
ÿ



zIconicFont._get_prefix_charsc                 C   s6   t | j| ƒ}| t|ƒ¡ |d dkr2| d¡ |S )z:Return a QFont corresponding to the given prefix and size.rI   ÚsZSolid)r	   rb   ZsetPixelSizerJ   ZsetStyleName)r=   rG   r]   rL   r8   r8   r9   rL   Š  s
    
zIconicFont.fontc                 C   s   || j |< dS )a»  Associate a user-provided CharIconPainter to an icon name.

        The custom icon can later be addressed by calling
        icon('custom.NAME') where NAME is the provided name for that icon.

        Parameters
        ----------
        name: str
            name of the custom icon
        painter: CharIconPainter
            The icon painter, implementing
            ``paint(self, iconic, painter, rect, mode, state, options)``
        N)ra   )r=   r’   r?   r8   r8   r9   Úset_custom_icon’  s    zIconicFont.set_custom_iconc                 K   s:   t tfi |¤Ž}|| jv r0| j| }|  ||¡S tƒ S dS )z7Return the custom icon corresponding to the given name.N)r•   r2   ra   rˆ   r   )r=   r’   r5   rC   r?   r8   r8   r9   Ú_custom_icon¢  s
    

zIconicFont._custom_iconc                 C   s   t | ||ƒ}t|ƒS )z3Return the icon corresponding to the given painter.)rV   r   )r=   r?   rC   Zenginer8   r8   r9   rˆ   «  s    zIconicFont._icon_by_painter)N)rQ   rR   rS   rT   rY   rf   r   r‡   r–   rL   rž   rŸ   rˆ   r_   r8   r8   rZ   r9   r`   ½   s   
I8	r`   )!rT   Z
__future__r   r   r{   rq   r‰   Zqtpy.QtCorer   r   r   r   r   Z
qtpy.QtGuir   r	   r
   r   r   r   r   r   Zqtpy.QtWidgetsr   r}   r~   r2   r:   r;   r„   rU   rV   r`   r8   r8   r8   r9   Ú<module>   s4   (ú


ü[