
    ]dd                        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 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  dZ"ejF                  d	k(  r4dd
lm$Z$ ddl%Z% ejL                  dd      Z' ejL                  dd      Z(dZ)dZ*dZ+ e,e$d      s! ejZ                  e$j\                        e$_/        e$j`                  e'jb                  _2        e$jf                  e$jh                  e$j`                  e$j`                  e$jh                  e$jh                  e$j`                  fe'jb                  _5        e$jl                  fe(jn                  _5        e$jl                  e$j^                  e$j`                  e$j\                  fe(jp                  _5        d Z9d Z:e9e:dddZ;d Z< G d d      Z= G d de>      Z? G d de      Z@ G d de
      ZAy# e!$ r dZ Y w xY w)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)PYSIDE_VERSION)QObjectQPointQRectQtQSizeFQRectFQPointFQThread)QColorQFontQFontDatabaseQIconQIconEngineQPainterQPixmap
QTransformQPaletteQRawFontQImage)QApplication)	QGlyphRunTnt)wintypesuser32)use_last_errorgdi32z2Software\Microsoft\Windows NT\CurrentVersion\Fonts      LPDWORDc                      	 t        j                         j                         } | 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    7/usr/lib/python3/dist-packages/qtawesome/iconic_font.py
text_colorr+   Q   sU    "'')113}}X__hmm<< "b"b!!"   AA A+*A+c                      	 t        j                         j                         } | 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_disabledr0   Y   sW    %'')113}}X..>> %c3$$%r,         ?)r%   color_disabledopacityscale_factorc                  r    g d}| D ].  }||v r| |   t         |<   dj                  |      }t        |       y)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_selectedr2   color_on_selectedcolor_on_activecolor_on_disabledcolor_off_selectedcolor_off_activecolor_off_disabled	animationoffsetr4   rotatedhflipvflipdrawzInvalid option '{0}'N)_default_optionsformatKeyError)kwargsvalid_optionskwerrors       r*   set_global_defaultsrX   i   sK    

M  "#)":R *11"5E5/!"    c                       e Zd ZdZd Zd Zy)CharIconPainterzChar icon painter.c           	      >    |D ]  }| j                  ||||||        y)zMain paint method.N)_paint_icon)selficonicpainterrectmodestateoptionsopts           r*   paintzCharIconPainter.paint   s+     	FCVWdD%E	FrY   c                 >   j                          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|
j                  |	       j                  |
       t        dj                         z  |d   z        |d   |j                  d      j!                  |        d|v r]t#              j%                  t        |d   d   j'                         z        t        |d   d   j                         z               j'                         dz  }j                         dz  }t)               }|j%                  ||       d|v r|d   du r|j+                  dd       d|v r|d   du r|j+                  dd       d|v r|j-                  |d          |j%                  | |        j/                  |d       j1                  |j                  dd              |j                  d!      d"vrd#nd$fd%}d$k(  s |       sj3                        }|j5                  t6        j8                         j;                  |       j=                  t?        t@        jB                  t@        jD                  z               jG                          y)&zPaint a single icon.rA   r9   rG   r=   rF   r;   rE   r<   rB   r:   rJ   r@   rI   r>   rH   r?   Nr   r   g      ?r4   prefixrK   rL   g      ?rO   TrN   rM   r3   r1   rP   )textpathglyphrunimagerk   rj   c                     dk(  r$
"j                  t        j                        } nj                        } | j                  d      sy| j	                        d   }| j                  |f      d   }| j                         }t        t        |j                               || j                         z         }j                  t              j                                j                  |j                          dz  |j                          dz         dk(  r| j!                  |      }|j                  d|       |j#                  t$        j&                         j)                  t*        j,                  d       j/                  |j1                         j3                                ydk(  rt4        rft5               }|j7                  |        |j9                  |f       |j;                  t=        d|      f       j?                  t=        dd      |       ytA        jB                  d       yd	k(  r| jE                  |tF        jH                        jK                  tL        jN                        }t+        |      }|jQ                  t*        jR                         |jU                  |jW                         j1                         j3                                |jY                          | j[                  |      }	|	j                  d|       j)                  t*        j\                  d       j_                  |	ja                         |       yy)
Nrl   glyfFr      rk   Tz_QGlyphRun is unavailable for the current Qt binding! QtAwesome will use the default draw valuesrm   )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fillRectra   endboundingRectSmoothPixmapTransform	drawImagetopLeft)rq   glyphadvancerv   sizerk   rl   rm   painter2brectrK   charrP   	draw_sizer_   r`   rh   ra   s             r*   try_draw_rawfontz5CharIconPainter._paint_icon.<locals>.try_draw_rawfont   s   z!i&; !..E<Q<QR ..; $$V,11$7:E55uh?BG^^%F#giik*FW__5F,FGDfTl1134tzz|ma/$++-!1CDv~++E2q&)  0%%h&;&;TB  w{{}':':'<=: 7 #({H''0,,eX6))71f+=*?@((AA* ' MM #O P 008R8RS /0R0R S #E?++H,M,MN!!%**,0C0C0EF,,U36*%%h&D&DdK!!%--/59  rY   )$saver   OnNormalr/   r&   SelectedOff
isinstancetupler   setAlphasetPenroundr}   getsetupr   rz   r|   r   scalerotatesetTransform
setOpacityfontsetHintingPreferencer   rr   setFontdrawTextintr   AlignCenterAlignVCenterrestore)r^   r_   r`   ra   rb   rc   rd   color_optionsr%   alphaqcolorx_centery_center	transformr   r   rK   r   rP   r   rh   s    ```            @@@@@r*   r]   zCharIconPainter._paint_icon   s    HHwz2GDMB)<!=!(!7!9w'89&{35)<!=!(!7!9 IIw{3WU^D)=!>!(!8!:w'9:&|46)=!>!(!8!:
, $E*40t eU#!HE!HEOOE"v %$++-/'.2IIJ	" KK,	 OOD'40w;DNN5!21!5

!DE !21!5!EFH ::<#%;;=3&L	XIy1g''"2d":OOAb!g''"2d":OOB"WY/0XIy1Y-7;;y#67{{6"<< '26D6	 6	p 6>!1!3;;vy1D $))%*?*?@OOD!T3r~~'G#H$OrY   N)__name__
__module____qualname____doc__rf   r]    rY   r*   r[   r[      s    F
QrY   r[   c                       e Zd ZdZy)	FontErrorzException for font errors.N)r   r   r   r   r   rY   r*   r   r     s    $rY   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                 L    t         |           || _        || _        || _        y N)super__init__r_   r`   rd   )r^   r_   r`   rd   	__class__s       r*   r   zCharIconEngine.__init__%  s#    rY   c                 l    | j                   j                  | j                  ||||| j                         y r   )r`   rf   r_   rd   )r^   r`   ra   rb   rc   s        r*   rf   zCharIconEngine.paint+  s*    KK$eT\\	CrY   c           	          t        |      }|j                  t        j                         | j	                  t        |      t        t        dd      |      ||       |S )Nr   )r   fillr   transparentrf   r   r   r   )r^   r   rb   rc   pms        r*   pixmapzCharIconEngine.pixmap/  sC    T]


8B<va|T!:D%H	rY   )r   r   r   r   r   rf   r   __classcell__r   s   @r*   r   r   !  s    FCrY   r   c                   ~     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                  |   y)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[   r`   paintersfontnamefontdatafontidscharmap
icon_cacherawfont_cache	load_font)r^   argsfargsr   s      r*   r   zIconicFont.__init__:  si     	&( 	#EDNNE"	#rY   c                    d }|| j                         }t        j                         @t        t        j
                  j                  ||      d      5 }|j                         }t        j                  |      }ddd       j                          t        j                        }	|	r1|| j                  |<   |	d   | j                  |<   | j                  |<   n8t        dj!                  t        j
                  j                  ||                  t        t        j
                  j                  ||      d      5 }
t#        j$                  |
|      | j&                  |<   ddd       yy# 1 sw Y   xY w# 1 sw Y   yxY w)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            ||<     |S # t        $ r4 t        | |   d      dkD  rnt        dj	                  || |               Y _w xY w)N   i  z Failed to load character {0}:{1})chrr   
ValueErrorr   rR   )objresultkeys      r*   hookz"IconicFont.load_font.<locals>.hookf  s    F 
I	I"%c#c(B&7"8F3K
I M " I3s8R(61 ' )228&c#h2GI I 	Is   (:A%$A%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osrk   joinreadr   addApplicationFontFromDatacloseapplicationFontFamiliesr   r   r   r   rR   jsonloadr   )r^   rh   ttf_filenamecharmap_filename	directoryr   	font_datadataid_loadedFontFamiliescodess              r*   r   zIconicFont.load_fontT  s_   $	 113I   ".bggll9l;TB Ei ~~'#>>tDE OO!.!F!Fs!K!'*V$(:1(=f%(,f% !F
 GMf(*Y(MGOP P bggll9.>?E J'+yyD'IV$J J+ /E E(J Js   &E,=%E8,E58Fc           	         dj                  ||      }|r!d|d   v rt        j                  dt               || j                  vr|j                  di gt        |      z        }|}t        |      t        |      k7  r%dj                  t        |            }t        |      t        j                         qg }t        t        |            D ],  }||   }	|j                  | j                  |	|||                . |}
| j                  | 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 futurerd   z$"options" must be a list of size {0}z9You need to have a running QApplication to use QtAwesome!)rR   r   r   DeprecationWarningr   poplen	Exceptionr   r#   rangeappend_parse_options_icon_by_painterr`   r   )r^   namesrT   	cache_keyoptions_listgeneral_optionsrW   parsed_optionsispecific_optionsapi_optionss              r*   iconzIconicFont.icon  sF   MM%/	UeAh&MM6 # DOO+!::i"E
1BCL$O< CJ.>EEc%jQ&&$$&2!#s<01 IA'3A$"))$*=*=>N>M>CAh+H II --1-B-B4<<Q\-]	* ? @wy))rY   c                 (   t         j                         D ci c]  \  }}|t        |      r |       n| }}}t        |fi |}|j	                  |       g d}|j                  d|      }	|j                  d|	      }
|j                  d|	      }|j                  d|
      }|j                  d|      }|j                  d|	      }|j                  d|      }|j                  d	|      }|j                  d
|      }|j                  d|      }|j                  d|      }|j                  d|      }|	|
||||||||||d}|D cg c]  }|j                  ||       }}| j                  |      \  }}|j	                  t        t        ||f              |j	                  d|i       |j                  d      }|j                  d|       |j                  d|d          |j                  d|d          |j                  d|d          |j                  d|d          |j                  d|d          |j                  d|       |j                  d|d          |j                  d|d          |j                  d|d          |S c c}}w c c}w )N)r   r9   r:   r6   r7   r8   r;   r<   r=   r>   r?   r@   r   r9   r:   r6   r7   r8   r;   r<   r=   r>   r?   r@   rh   r%   rA   rC   rD   rF   rE   rG   r2   rB   rI   rH   rJ   )	rQ   itemscallabledictupdater   _get_prefix_charszip
setdefault)r^   r  r
  namekv	live_dictrd   icon_kwr   r9   r:   r6   r7   r8   r;   r<   r=   r>   r?   r@   	icon_dictrV   r  rh   charsr%   s                              r*   r  zIconicFont._parse_options  s   >N>T>T>VWdaQx{1W	Wy4O4'(3 {{64([[t$kk%&Xr*;;z62;;z40KKV4	kk-:kk-:[[v6
{{>8<{{>8<!%!)!)"+$/$/#-%1%1	 4;;Rr4(;;..u5tC'5!1234&)* G$:u->7:+>?+W^-DE,gn.EF.8H0IJ.8H0IJ;.-w~/FG/9I1JK/9I1JKo XH <s   J	Jc                 T   g }|D ]  }d|v r|j                  d      \  }}|| j                  v rP|| j                  |   v r"|j                  | j                  |   |          \dj                  ||      }t	        |      dj                  |      }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  rR   r  )r^   r  r  r  rh   nrW   s          r*   r  zIconicFont._get_prefix_chars  s     	5Dd{ JJsO	T\\)DLL00T\\&%9!%<= G N Nv!''..7>>vFE#E** 344	5  u}rY   c                     t               }|j                  | j                  |          |j                  t	        |             |d   dk(  r|j                  d       |S )z:Return a QFont corresponding to the given prefix and size.ri   sSolid)r   	setFamilyr   setPixelSizer   setStyleName)r^   rh   r   r   s       r*   r   zIconicFont.font  sO    wt}}V,-%+&":g&rY   c                    | j                   t        rt        t        j                               nt        t        j                               vr<i <   fd}t               j	                         j                  j                  |       |||f}|   vr t        | j                  |   ||         |<      |   S )z=Return a QRawFont corresponding to the given prefix and size.c                  (     j                         y r   )r   )cachetids   r*   clear_cachez'IconicFont.rawfont.<locals>.clear_cache  s    uyy~rY   )r   r   strr   currentThreadr   currentThreadIdfinishedconnectr   r   )r^   rh   r   hintingPreferencer-  r   r+  r,  s         @@r*   rq   zIconicFont.rawfont  s    "" g++-.Cg--/0CeE#J-I##%..66{Cd--eCj &t}}V'<dDUVE#JsOSz#rY   c                 "    || j                   |<   y)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  r`   s      r*   set_custom_iconzIconicFont.set_custom_icon&  s     &drY   c                     t        t        fi |}|| j                  v r!| j                  |   }| j                  ||      S t	               S )z7Return the custom icon corresponding to the given name.)r  rQ   r   r  r   )r^   r  rT   rd   r`   s        r*   _custom_iconzIconicFont._custom_icon6  sF    '2624== mmD)G(('::7NrY   c                 2    t        | ||      }t        |      S )z3Return the icon corresponding to the given painter.)r   r   )r^   r`   rd   engines       r*   r  zIconicFont._icon_by_painter?  s    gw7V}rY   c                 
   t         j                  j                  t         j                  j                  t         j                  j	                  t
                    d      }t         j                  dk(  r| j                  |      }|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   rk   r   dirnamerealpath__file__r  _install_fonts)r^   fonts_directorys     r*   r   zIconicFont._get_fonts_directoryD  sY     '',,GGOOBGG,,X67B77d?"11/BOrY   c           
         t         j                  j                  dd      }|s|S t         j                  j	                  |ddd      }t        j
                  |d       t        j                  |      D ]  \  }}}|D ]  }t         j                  j	                  ||      }t         j                  j	                  |t         j                  j                  |            }	t         j                  j                  |	      rt        j                  ||	      rt        j                  ||       t         j                  j                  |      d   d	k(  st        j                  |	      s$t        j                   |	       t#        d
| d      t         j                  j                  |	      }
t         j                  j                  |
      d   }t%        j&                         }t        j)                  |
t+        j,                  |      dt.              r\t+        j0                  |j2                  z         }t        j)                  |
t+        j,                  |      |t.              r|j2                  }t%        j4                         }t+        j6                  |      |_        t        j)                  |
t+        j,                  |      t+        j,                  |      t8               |r|dz  }	 t;        j<                  t:        j>                  t@        dt:        jB                        5 }t;        jD                  ||dt:        jF                  |
       ddd         |S # 1 sw Y   xY w# tH        $ r	 |cY c c S w xY w)z
        Copy the fonts to the user Fonts folder.
        
        Based on https://stackoverflow.com/a/41841088/15954282
        LOCALAPPDATAN	MicrosoftWindowsFontsT)exist_okri   z.ttfz AddFontResource failed to load ""r   z (TrueType))%r   environr   rk   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^   r@  local_appdata_diruser_fonts_dirroot__filesr  src_pathdst_pathfilenamer   cbbufis_truetyper   s                   r*   r?  zIconicFont._install_fontsT  s    JJNN>4@ "" {Iw@
NT2  "ww7 0	/OD"e //77<<d377<<"GG$$X.0
 77>>(+Hh0OHn5 77##D)"-7 11(;		(+*>xjJL L  "ww//9H!ww//9!<H ")B11$fll2&6>NP%~~8; 55 (&,,r*:CAQS'*yyH"*--/K%}}[9BH.. &,,r"2FLL4M') # M1	/#^^F,D,DnVW,2,@,@B YEH"--c8QxXYO//0	/d Y Y # /
  /./s*   !8M(MMMMM/.M/r   )r   r   r   r   r   r   r  r  r  r   r   PreferDefaultHintingrq   r5  r7  r  r   r?  r   r   s   @r*   r   r   6  sR    /#4;Jz%*N8t( 7<6P6P .& 
 CrY   r   )Br   rW  rM  r   r   rO  r   qtpyr   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   r_  WinDLLr   r   rb  rY  r^  hasattrPOINTERrU  r    LPVOIDSendMessageTimeoutWrestypeHWNDUINTargtypesLPCWSTRrR  rV  r+   r0   rQ   rX   r[   r  r   r   r   r   rY   r*   <module>r     s       	    ; ; ;        ( %  77d?V]]8D9FFMM'$7EJNO8Y')6>>(..9)1F&+F' 	'E#
 		+E'"% )	 ".Z Zz%	 %[ *a ac  Is   G G)(G)