Installing fonts: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category)
(→‎FontInstall.nsh: Allow skipping extraction)
 
Line 11: Line 11:
!macro FontInstallHelper FontFileSrc FontFileDst FontInternalName Resource RegSuffix RegRoot
!macro FontInstallHelper FontFileSrc FontFileDst FontInternalName Resource RegSuffix RegRoot
ClearErrors
ClearErrors
!if "${FontFileSrc}" != ""
${IfNot} ${FileExists} "${FontFileDst}"
${IfNot} ${FileExists} "${FontFileDst}"
File "/oname=${FontFileDst}" "${FontFileSrc}"
File "/oname=${FontFileDst}" "${FontFileSrc}"
${EndIf}
${EndIf}
!endif
${IfNot} ${Errors}
${IfNot} ${Errors}
Push $0
Push $0

Latest revision as of 21:40, 12 June 2023

Author: Anders (talk, contrib)


Basic font installation/uninstallation.

FontInstall.nsh

!include LogicLib.nsh
!include WinMessages.nsh
 
!macro FontInstallHelper FontFileSrc FontFileDst FontInternalName Resource RegSuffix RegRoot
ClearErrors
!if "${FontFileSrc}" != ""
${IfNot} ${FileExists} "${FontFileDst}"
	File "/oname=${FontFileDst}" "${FontFileSrc}"
${EndIf}
!endif
${IfNot} ${Errors}
	Push $0
	Push "${Resource}"
	Exch $1
	Push "${FontInternalName}${RegSuffix}"
	Exch $2
	Push $9
	StrCpy $9 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
	!if "${NSIS_CHAR_SIZE}" < 2
	ReadRegStr $0 ${RegRoot} "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
	${IfThen} $0 == "" ${|} StrCpy $9 "Software\Microsoft\Windows\CurrentVersion\Fonts" ${|}
	!endif
	System::Call 'GDI32::AddFontResource(tr1)i.r0'
	${If} $0 <> 0
		WriteRegStr ${RegRoot} "$9" "$2" "$1"
	${Else}
		SetErrors
	${EndIf}
	Pop $9
	Pop $2
	Pop $1
	Pop $0
${Else}
	SetErrors
${EndIf}
!macroend
!macro FontInstallTTF FontFileSrc FontFileName FontInternalName
!insertmacro FontInstallHelper "${FontFileSrc}" "$Fonts\${FontFileName}" "${FontInternalName}" "${FontFileName}" " (TrueType)" HKLM
!macroend
 
!macro FontUninstallHelper FontFileDst FontInternalName Resource RegSuffix RegRoot
System::Call 'GDI32::RemoveFontResource(t"${Resource}")'
DeleteRegValue ${RegRoot} "Software\Microsoft\Windows NT\CurrentVersion\Fonts" "${FontInternalName}${RegSuffix}"
!if "${NSIS_CHAR_SIZE}" < 2
DeleteRegValue ${RegRoot} "Software\Microsoft\Windows\CurrentVersion\Fonts" "${FontInternalName}${RegSuffix}"
!endif
ClearErrors
Delete "${FontFileDst}"
!macroend
!macro FontUninstallTTF FontFileName FontInternalName
!insertmacro FontUninstallHelper "$Fonts\${FontFileName}" "${FontInternalName}" "${FontFileName}" " (TrueType)" HKLM
!macroend

Example

RequestExecutionLevel Admin
!include "FontInstall.nsh"
 
Section
 
StrCpy $1 0
; Hardcoded name
!insertmacro FontInstallTTF "C:\myfiles\msgithub\CascadiaCodePL.ttf" "CascadiaCodePL.ttf" "Cascadia Code PL Regular"
${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
 
; Gettting the name dynamically using a plug-in
InitPluginsDir
File "/oname=$PluginsDir\AnotherFont.ttf" "C:\myfiles\AnotherFont.ttf"
FontInfo::GetFontName "$PluginsDir\AnotherFont.ttf"
${If} $0 != ""
	!insertmacro FontInstallTTF "C:\myfiles\AnotherFont.ttf" "AnotherFont.ttf" $0
	${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
${EndIf}
 
${If} $1 <> 0
	DetailPrint "Successfully installed $1 font(s)..."
	SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
${EndIf}
 
SectionEnd
 
 
Section Uninstall
 
StrCpy $1 0
!insertmacro FontUninstallTTF "CascadiaCodePL.ttf" "Cascadia Code PL Regular"
${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
 
FontInfo::GetFontName "$Fonts\AnotherFont.ttf"
${If} $0 != ""
	!insertmacro FontUninstallTTF "AnotherFont.ttf" $0
	${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
${EndIf}
 
${If} $1 <> 0
	DetailPrint "Successfully uninstalled $1 font(s)..."
	SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
${EndIf}
 
SectionEnd