Skip to content

Instantly share code, notes, and snippets.

@sredna
Last active October 13, 2022 22:57
Show Gist options
  • Save sredna/3827a7f335b878063d5aba81699aa56b to your computer and use it in GitHub Desktop.
Save sredna/3827a7f335b878063d5aba81699aa56b to your computer and use it in GitHub Desktop.
NSIS FontInstall
; FontInstall.nsh >8 >8 >8 >8 >8 >8 >8 >8
!include LogicLib.nsh
!include WinMessages.nsh
!macro FontInstallHelper FontFileSrc FontFileDst FontInternalName Resource RegSuffix RegRoot
ClearErrors
${IfNot} ${FileExists} "${FontFileDst}"
File "/oname=${FontFileDst}" "${FontFileSrc}"
${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
; EOF ----------- >8 >8 >8 >8 >8 >8 >8 >8
; .nsi
RequestExecutionLevel Admin
Section
StrCpy $0 0
!insertmacro FontInstallTTF "C:\myfiles\msgithub\CascadiaCodePL.ttf" "CascadiaCodePL.ttf" "Cascadia Code PL Regular"
${IfNotThen} ${Errors} ${|} IntOp $0 $0 + 1 ${|}
;!insertmacro FontInstallTTF ...
;${IfNotThen} ${Errors} ${|} IntOp $0 $0 + 1 ${|}
${If} $0 <> 0
DetailPrint "Successfully installed $0 font(s)..."
SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
${EndIf}
SectionEnd
Section Uninstall
StrCpy $0 0
!insertmacro FontUninstallTTF "CascadiaCodePL.ttf" "Cascadia Code PL Regular"
${IfNotThen} ${Errors} ${|} IntOp $0 $0 + 1 ${|}
;!insertmacro FontUninstallTTF ...
;${IfNotThen} ${Errors} ${|} IntOp $0 $0 + 1 ${|}
${If} $0 <> 0
DetailPrint "Successfully uninstalled $0 font(s)..."
SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
${EndIf}
SectionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment