
    cag                     2   d 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	 ddl
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mZmZ ddlm Z  	 ddlm!Z! n# e"$ r dZ!Y nw xY wdZ#ej$        d	k    rdd
lm%Z% ddl&Z& ej'        dd          Z( ej'        dd          Z)dZ*dZ+dZ, e-e%d          s ej.        e%j/                  e%_0        e%j1        e(j2        _3        e%j4        e%j5        e%j1        e%j1        e%j5        e%j5        e%j1        fe(j2        _6        e%j7        fe)j8        _6        e%j7        e%j0        e%j1        e%j/        fe)j9        _6        d Z:d Z;e:e;dddZ<d Z= G d d          Z> G d de?          Z@ G d de          ZA G d de          ZBd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``.

    N)PYSIDE2PYSIDE6)QObjectQPointQRectQtQSizeFQRectFQPointFQThread)QColorQFontQFontDatabaseQIconQIconEngineQPainterQPixmap
QTransformQPaletteQRawFontQImage)QApplication)	QGlyphRunFnt)wintypesuser32T)use_last_errorgdi32z2Software\Microsoft\Windows NT\CurrentVersion\Fonts      LPDWORDc                      	 t          j                                                    } |                     t          j        t          j                  S # t          $ r t          ddd          cY S w xY w)N2   )	r   instancepalettecolorr   ActiveTextAttributeErrorr   r%   s    5lib/python3.11/site-packages/qtawesome/iconic_font.py
text_colorr,   Q   sl    "'))1133}}X_hm<<< " " "b"b!!!!!"   AA A/.A/c                      	 t          j                                                    } |                     t          j        t          j                  S # t          $ r t          ddd          cY S w xY w)N   )	r   r$   r%   r&   r   Disabledr(   r)   r   r*   s    r+   text_color_disabledr1   Y   sm    %'))1133}}X.>>> % % %c3$$$$$%r-         ?)r&   color_disabledopacityscale_factorc                      g d}| D ]:}||v r| |         t           |<   d                    |          }t          |          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_selectedr3   color_on_selectedcolor_on_activecolor_on_disabledcolor_off_selectedcolor_off_activecolor_off_disabled	animationoffsetr5   rotatedhflipvflipdrawzInvalid option '{0}'N)_default_optionsformatKeyError)kwargsvalid_optionskwerrors       r+   set_global_defaultsrY   i   sl    

 

 

M  " " 	"#)":R  *11"55E5//!" "    c                       e Zd ZdZd Zd ZdS )CharIconPainterzChar icon painter.c           	      D    |D ]}|                      ||||||           dS )zMain paint method.N)_paint_icon)selficonicpainterrectmodestateoptionsopts           r+   paintzCharIconPainter.paint   s@     	F 	FCVWdD%EEEE	F 	FrZ   c                                                      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}||         |         \  }d}	t          |t                    r|d         }	|d         }t          |          }
|	r|
                    |	                               |
           t          d                                z  |d         z            |d         |                    d                              |            d|v rt#                                        t          |d         d                                         z            t          |d         d                                         z                                                       dz  }                                dz  }t)                      }|                    |
 |
            d|v r |d         du r|                    dd           d|v r |d         du r|                    dd           d|v r|                    |d                    |                    | |                                |d                               |                    dd                      |                    d!          d"vrd#nd$fd%}d$k    s
 |            s                              }|                    t6          j                                       |                               t?          t@          j!        t@          j"        z                       #                                 dS )&zPaint a single icon.rB   r:   rH   r>   rG   r<   rF   r=   rC   r;   rK   rA   rJ   r?   rI   r@   Nr   r   g      ?r5   prefixrL   rM   g      ?rP   TrO   rN   r4   r2   rQ   )textpathglyphrunimagerl   rk   c                     dk    r$
"                     t          j                  } n                               } |                     d          sdS |                               d         }|                     |f          d         }|                                 }t          t          |	                                          || 
                                z             }                    t                                                                                   |                                 dz  |                                 dz             dk    r|                     |          }|                    d|           |                    t$          j                                       t*          j        d                               |                                                                           ndk    rt4          rt5                      }|                    |            |                    |f           |                    t=          d|          f                               t=          dd          |           n^tA          j!        d           dS d	k    r?| "                    |tF          j$                  %                    tL          j'                  }t+          |          }|(                    t*          j)                   |*                    |+                                                                                                           |,                                 | -                    |          }	|	                    d|                               t*          j.        d           /                    |	0                                |           ndS dS )
Nrm   glyfFr      rl   Tz_QGlyphRun is unavailable for the current Qt binding! QtAwesome will use the default draw valuesrn   )1rawfontr   PreferNoHinting	fontTableglyphIndexesForStringadvancesForGlyphIndexesascentr	   absxdescent	translater
   centerwidthheightpathForGlyphsetFillRuler   WindingFillsetRenderHintr   AntialiasingfillPathpenr&   r   
setRawFontsetGlyphIndexessetPositionsr   drawGlyphRunwarningswarnalphaMapForGlyphr   PixelAntialiasingconvertToFormatr   Format_ARGB32_PremultipliedsetCompositionModeCompositionMode_SourceInfillRectrb   endboundingRectSmoothPixmapTransform	drawImagetopLeft)rr   glyphadvancerw   sizerl   rm   rn   painter2brectrL   charrQ   	draw_sizer`   ra   ri   rb   s             r+   try_draw_rawfontz5CharIconPainter._paint_icon.<locals>.try_draw_rawfont   sl   z! <i < !..E<QRR ..;; $$V,, u11$77:E55uh??BG^^%%F#giikk**FW__5F5F,FGGDfTll1133444tzz||ma/$++--!1CDDDv~  ++E22q&)))  000%%h&;TBBB  w{{}}':':'<'<====#  	!({{H''000,,eX666))71f+=+=*?@@@((AAAAAM #O P P P 5 008RSS /0R S S #E??++H,MNNN!!%**,,0C0C0E0EFFF,,U336***%%h&DdKKK!!%--//59999 u4rZ   )$saver   OnNormalr0   r'   SelectedOff
isinstancetupler   setAlphasetPenroundr~   getsetupr   r{   r}   r   scalerotatesetTransform
setOpacityfontsetHintingPreferencer   rs   setFontdrawTextintr   AlignCenterAlignVCenterrestore)r_   r`   ra   rb   rc   rd   re   color_optionsr&   alphaqcolorx_centery_center	transformr   r   rL   r   rQ   r   ri   s    ```            @@@@@r+   r^   zCharIconPainter._paint_icon   s    Hwz2GDMB)<!=!(!7!9w'89&{35)<!=!(!7!9 Iw{3WU^D)=!>!(!8!:w'9:&|46)=!>!(!8!:
, $E*40t eU## 	!HE!HE 	#OOE"""v %$++--/'.2IIJJ	" KK,,	 	1OOD'4000w 	H;;DNN5!21!5

!DEE !21!5!EFFH H H ::<<#%;;==3&LL	XIy111g 	"''"2d": 	"OOAb!!!g 	#''"2d": 	#OOB""" 	1WY/000XIy111Y---7;;y#66777{{6""<< 	? '>66D6	 6	 6	 6	 6	 6	 6	 6	 6	 6	 6	 6	p 6> 	P!1!1!3!3 	P;;vy11D  A))%*?@@@OOD!!!T3r~'G#H#H$OOOrZ   N)__name__
__module____qualname____doc__rg   r^    rZ   r+   r\   r\      s@        F F F
Q Q Q Q QrZ   r\   c                       e Zd ZdZdS )	FontErrorzException for font errors.N)r   r   r   r   r   rZ   r+   r   r     s        $$$$rZ   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )CharIconEnginez<Specialization of QIconEngine used to draw font-based icons.c                 r    t                                                       || _        || _        || _        d S N)super__init__r`   ra   re   )r_   r`   ra   re   	__class__s       r+   r   zCharIconEngine.__init__%  s2    rZ   c                 X    | j                             | j        ||||| j                   d S r   )ra   rg   r`   re   )r_   ra   rb   rc   rd   s        r+   rg   zCharIconEngine.paint+  s<    K$eT\	C 	C 	C 	C 	CrZ   c           	          t          |          }|                    t          j                   |                     t          |          t          t          dd          |          ||           |S )Nr   )r   fillr   transparentrg   r   r   r   )r_   r   rc   rd   pms        r+   pixmapzCharIconEngine.pixmap/  sV    T]]


8B<<va||T!:!:D%HHH	rZ   )r   r   r   r   r   rg   r   __classcell__r   s   @r+   r   r   !  s_        FF    C C C      rZ   r   c                   t     e Zd ZdZ fdZddZd Zd Zd Zd Z	e
j        fd	Zd
 Zd Zd Zd Zd Z xZS )
IconicFontz%Main class for managing iconic fonts.c                     t                                                       t                      | _        i | _        i | _        i | _        i | _        i | _        i | _	        i | _
        |D ]} | j        |  d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 the QtAwesome ``fonts``
              directory.
        N)r   r   r\   ra   paintersfontnamefontdatafontidscharmap
icon_cacherawfont_cache	load_font)r_   argsfargsr   s      r+   r   zIconicFont.__init__:  s     	&(( 	# 	#EDNE"""	# 	#rZ   Nc                 (   d }||                                  }t          j                    dt          t          j                            ||          d          5 }|                                }t          j	        |          }ddd           n# 1 swxY w Y   |
                                 t          j        |          }	|	r%|| j        |<   |	d         | j        |<   || j        |<   n@t          d                    t          j                            ||                              t          t          j                            ||          d          5 }
t#          j        |
|          | j        |<   ddd           dS # 1 swxY w Y   dS dS )a  Loads a font file and the associated charmap.

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

        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 path for font and charmap files
        c           	      
   i }| D ]}}	 t          t          | |         d                    ||<   *# t          $ rG t          | |         d          dk    rn)t          d                    || |                             Y zw xY w|S )N   i  z Failed to load character {0}:{1})chrr   
ValueErrorr   rS   )objresultkeys      r+   hookz"IconicFont.load_font.<locals>.hookf  s    F 
I 
I	I"%c#c(B&7&7"8"8F3KK! I I I3s8R((61 I ' )228&c#h2G2GI I I 	I Ms   &/AB ?B Nrbr   zFont 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)object_hook)_get_fonts_directoryr   r$   openosrl   joinreadr   addApplicationFontFromDatacloseapplicationFontFamiliesr   r   r   r   rS   jsonloadr   )r_   ri   ttf_filenamecharmap_filename	directoryr   	font_datadataid_loadedFontFamiliescodess              r+   r   zIconicFont.load_fontT  s<   $	 	 	  	41133I  "" 	Jbgll9l;;TBB Ei ~~''#>tDDE E E E E E E E E E E E E E E OO!.!Fs!K!K! P'*V$(:1(=f%(,f%% !F
 GMf(*Y(M(MGO GOP P P bgll9.>??EE J'+yD'I'I'IV$J J J J J J J J J J J J J J J J J J+	J 	Js$   )BBBFF	F	c           	         d                     ||          }|r$d|d         v rt          j        dt                     || j        vr.|                    di gt          |          z            }|}t          |          t          |          k    r1d                     t          |                    }t          |          t          j	                    g }t          t          |                    D ]:}||         }	|                    |                     |	|||                              ;|}
|                     | j        |
          | j        |<   n"t          j        d           t                      S | j        |         S )	z>Return a QIcon object corresponding to the provided icon name.z{}{}zfa.r   zThe FontAwesome 4.7 ('fa' prefix) icon set will be removed in a future release in favor of FontAwesome 6. We recommend you to move to FontAwesome 5 ('fa5*' prefix) to prevent any issues in the futurere   z$"options" must be a list of size {0}Nz9You need to have a running QApplication to use QtAwesome!)rS   r   r   DeprecationWarningr   poplen	Exceptionr   r$   rangeappend_parse_options_icon_by_painterra   r   )r_   namesrU   	cache_keyoptions_listgeneral_optionsrX   parsed_optionsispecific_optionsapi_optionss              r+   iconzIconicFont.icon  s   MM%//	 	UeAh& 	M6 #   DO+ 	!::i"E

1BCCL$O<  CJJ. '>EEc%jjQQ&&&$&& !#s<0011 I IA'3A$"))$*=*=>N>M>CAh+H +H I I I I
 --1-B-B4<Q\-]-]	** ? @ @ @wwy))rZ   c                    d t                                           D             }t          |fi |}|                    |           g d}|                    d          }|                    d|          }|                    d|          }	|                    d|          }
|                    d|
          }|                    d|          }|                    d	|
          }|                    d
|          }|                    d|          }|                    d|
          }|                    d|          }|                    d|          }|||	|
||||||||df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 )Nc                 L    i | ]!\  }}|t          |          r
 |            n|"S r   )callable).0kvs      r+   
<dictcomp>z-IconicFont._parse_options.<locals>.<dictcomp>  s3    WWWdaQx{{1WWWrZ   )r   r:   r;   r7   r8   r9   r<   r=   r>   r?   r@   rA   r   r:   r;   r7   r8   r9   r<   r=   r>   r?   r@   rA   c                 <    g | ]}                     |          S r   )r   )r  rW   	icon_dictnames     r+   
<listcomp>z-IconicFont._parse_options.<locals>.<listcomp>  s'    ;;;Rr4((;;;rZ   ri   r&   rB   rD   rE   rG   rF   rH   r3   rC   rJ   rI   rK   )rR   itemsdictupdater   _get_prefix_charszip
setdefault)r_   r  r  r  	live_dictre   icon_kwr   r:   r;   r7   r8   r9   r<   r=   r>   r?   r@   rA   r  ri   charsr&   r  s      `                   @r+   r  zIconicFont._parse_options  s
   WW>N>T>T>V>VWWW	y44O44'(((3 3 3 {{64(([[t$$kk%&&Xr**;;z622;;z400KKV44	kk-::kk-::[[v66
{{>8<<{{>8<<!%!)!)"+$/$/#-%1%1 	 <;;;;7;;;..u55tC'5!1233444&)*** G$$:u--->7:+>???+W^-DEEE,gn.EFFF.8H0IJJJ.8H0IJJJ;...-w~/FGGG/9I1JKKK/9I1JKKKrZ   c                 |   g }|D ]}d|v r|                     d          \  }}|| j        v r[|| j        |         v r'|                    | j        |         |                    ]d                    ||          }t	          |          d                    |          }t	          |          t	          d          ||fS )N.z%Invalid icon name "{0}" in font "{1}"zInvalid font prefix "{0}"zInvalid icon name)splitr   r  rS   r  )r_   r  r$  r  ri   nrX   s          r+   r  zIconicFont._get_prefix_chars  s     	5 	5Dd{ 5 JJsOO	T\) 	+DL00 /T\&%9!%<==== G N Nv!' !''...7>>vFFE#E*** 3444u}rZ   c                     t                      }|                    | j        |                    |                    t	          |                     |d         dk    r|                    d           |S )z:Return a QFont corresponding to the given prefix and size.rj   sSolid)r   	setFamilyr   setPixelSizer   setStyleName)r_   ri   r   r   s       r+   r   zIconicFont.font  sh    wwt}V,---%++&&&": 	'g&&&rZ   c                    | j         t          st          r!t          t	          j                              n t          t	          j                              vrCi <   fd}t	                                                      j        	                    |           |||f}|         vr%t          | j        |         ||                   |<            |         S )z=Return a QRawFont corresponding to the given prefix and size.c                  2                                     d S r   )r  )cachetids   r+   clear_cachez'IconicFont.rawfont.<locals>.clear_cache  s    uyy~~~~~rZ   )r   r   r   strr   currentThreadr   currentThreadIdfinishedconnectr   r   )r_   ri   r   hintingPreferencer3  r   r1  r2  s         @@r+   rr   zIconicFont.rawfont  s    "  	1g 	1g+--..CCg-//00Ce 	DE#J------II##%%.66{CCCd--eCj  	W&t}V'<dDUVVE#JsOSz#rZ   c                     || 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)r   )r_   r  ra   s      r+   set_custom_iconzIconicFont.set_custom_icon&  s     &drZ   c                     t          t          fi |}|| j        v r#| j        |         }|                     ||          S t	                      S )z7Return the custom icon corresponding to the given name.)r  rR   r   r  r   )r_   r  rU   re   ra   s        r+   _custom_iconzIconicFont._custom_icon6  sR    '226224=  	mD)G((':::77NrZ   c                 B    t          | ||          }t          |          S )z3Return the icon corresponding to the given painter.)r   r   )r_   ra   re   engines       r+   r  zIconicFont._icon_by_painter?  s    gw77V}}rZ   c                    t           j                            t           j                            t           j                            t
                              d          }t           j        dk    r|                     |          }|S )z
        Get bundled fonts directory.

        On Windows an attempt to install the fonts per user is done
        to prevent errors with fonts loading.

        See spyder-ide/qtawesome#167 and spyder-ide/spyder#18642 for
        context.
        fontsr   )r   rl   r   dirnamerealpath__file__r  _install_fonts)r_   fonts_directorys     r+   r   zIconicFont._get_fonts_directoryD  sf     ',,GOOBG,,X6677B B7d? 	C"11/BBOrZ   c           
         t           j                            dd          }|s|S t           j                            |ddd          }t          j        |d           t          j        |          D ]\  }}}|D ]}t           j                            ||          }t           j                            |t           j                            |                    }	t           j                            |	          rt          j
        ||	          rt          j        ||           t           j                            |          d         d	k    rt                              |	          s't          j        |	           t#          d
| d          t           j                            |	          }
t           j                            |
          d         }t%          j                    }t                              |
t+          j        |          dt.                    rVt+          j        |j        z              }t                              |
t+          j        |          |t.                    r|j        }t%          j                    }t+          j        |          |_        t                              |
t+          j        |          t+          j        |          t8                     |r|dz  }	 t;          j        t:          j        t@          dt:          j!                  5 }t;          j"        ||dt:          j#        |
           ddd           n# 1 swxY w Y   # tH          $ r	 |cY c c S w xY w|S )z
        Copy the fonts to the user Fonts folder.
        
        Based on https://stackoverflow.com/a/41841088/15954282
        LOCALAPPDATAN	MicrosoftWindowsFontsT)exist_okrj   z.ttfz AddFontResource failed to load ""r   z (TrueType))%r   environr   rl   r   makedirswalkbasenameisfilefilecmpcmpshutilcopysplitextr   AddFontResourceWremoveWindowsErrorr   DWORDGetFontResourceInfoWctypesbyrefGFRI_DESCRIPTIONc_wcharvalueBOOLsizeofGFRI_ISTRUETYPEwinregOpenKeyHKEY_CURRENT_USERFONTS_REG_PATHKEY_SET_VALUE
SetValueExREG_SZOSError)r_   rF  local_appdata_diruser_fonts_dirroot__filesr  src_pathdst_pathfilenamer   cbbufis_truetyper   s                   r+   rE  zIconicFont._install_fontsT  s    JNN>4@@  	#"" {Iw@ @
NT2222  "w77 0	/ 0	/OD"e // //7<<d337<<"G$$X..0 0
 7>>(++ Hh0O0O Hn555 7##D))"-7 #/ 11(;; L	(+++*JxJJJL L L  "w//99H!w//99!<H "))B11$fl2&6&6>NP P 1%~8;; 55 (&,r*:*:CAQS S 1'*yH"*-//K%}[99BH.. &,r"2"2FL4M4M') ) ) # 2 M1	/#^F,DnVW,2,@B B YEH"-c8QxXXXY Y Y Y Y Y Y Y Y Y Y Y Y Y Y # / / /
  /......./=#///b s6   *0M#M	=M	MMMMM)(M)r   )r   r   r   r   r   r   r  r  r  r   r   PreferDefaultHintingrr   r;  r=  r  r   rE  r   r   s   @r+   r   r   6  s       //# # # # #4;J ;J ;J ;Jz%* %* %*N8 8 8t  (   7<6P    .& & &     
   C C C C C C CrZ   r   )Cr   r]  rS  r   r   rU  r   qtpyr   r   qtpy.QtCorer   r   r   r   r	   r
   r   r   
qtpy.QtGuir   r   r   r   r   r   r   r   r   r   r   qtpy.QtWidgetsr   r   ImportErrorSYSTEM_FONTSr  r   re  WinDLLr   r   rh  r_  rd  hasattrPOINTERr[  r!   LPVOIDSendMessageTimeoutWrestypeHWNDUINTargtypesLPCWSTRrX  r\  r,   r1   rR   rY   r\   r  r   r   r   r   rZ   r+   <module>r     s        				   " ! ! ! ! ! ! !; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;                                                    ( ' ' ' ' ' %$$$$$$   III
  7d?  MMMV]8D999FFM'$777EJNO78Y'' :)6>(.99)1F&+F' 	'E#
 		+E'" " "% % % )	  " " ".Z Z Z Z Z Z Z Zz% % % % %	 % % %    [   *a a a a a a a a a as   A A)(A)