Check the Existent of Font by Font Name [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Check the Existent of Font by Font Name [Windows]

Post by RASHAD »

Your typo mistakes Not Windows or PB responsibility
Check to be sure that your font will be loaded
If Not Load your other option

Code: Select all

Global font.s,result

Procedure EnumFontFamProc(*f.ENUMLOGFONT,*nt.NEWTEXTMETRIC, FontType, lParam) 
  If PeekS(@*f\elfLogFont\lfFaceName[0]) = font.s
    result = 1
  EndIf
  ProcedureReturn 1
EndProcedure 

Procedure.s SysInfo_Fonts() 
  hWnd = GetDesktopWindow_() 
  hDC = GetDC_(hWnd) 
  EnumFontFamilies_(hDC, 0, @EnumFontFamProc(), 0) 
  ReleaseDC_ (hWnd, hDC) 
EndProcedure 

font.s = "Consolas"
SysInfo_Fonts()
If result 
  Debug "Found"
Else
  Debug "Not Found"
EndIf
Egypt my love
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: Check the Existent of Font by Font Name [Windows]

Post by AZJIO »

Removed global variables and the search stops when found.

Code: Select all

EnableExplicit

Structure Res
  Name.s
  FontExists.i
EndStructure

Procedure EnumFontFamProc(*f.ENUMLOGFONT,*nt.NEWTEXTMETRIC, FontType, *Res.Res) 
	If PeekS(@*f\elfLogFont\lfFaceName[0]) = *Res\Name
		*Res\FontExists = 1
		ProcedureReturn 0
	EndIf
; 	Debug PeekS(@*f\elfLogFont\lfFaceName[0]) ;  поиск прекращается когда найденно
	ProcedureReturn 1
EndProcedure 

Procedure SysInfo_Fonts(font$)
	Protected hWnd, hDC, Res.Res
	Res\Name = font$
	hWnd = GetDesktopWindow_() 
	hDC = GetDC_(hWnd) 
	EnumFontFamilies_(hDC, 0, @EnumFontFamProc(), @Res) 
	ReleaseDC_ (hWnd, hDC) 
	ProcedureReturn Res\FontExists
EndProcedure 


If SysInfo_Fonts("Consolas") 
	Debug "Found"
Else
	Debug "Not Found"
EndIf
Post Reply