Advanced Font Installation

From NSIS Wiki

Author: Vytautas (talk, contrib)


Note

This script is now included in the FontName plugin by default. The newest version of the script also includes code from other scripts as noted in the actual script.

A few notes about using InstallFON macro: You should copy the 'FontName' out of the registry of the Source machine as the fonts might not work if the wrong name or type is specified. The location of the fonts folder can be attained in a number of different ways, reading a registry key from the target machine or the way shown below, obtained from an example by Joel. Note that this way uses the System plug-in and if the size of your installer is a *very* big issue reading the registry might save a few bytes in the final installer size.

Some recent bugs: Do not put a trailing backslash onto the $FONT_DIR. This will make the font install, but be 0 Kilobytes. Some all uppercase font filenames generate errors. By changing the font to all lowercase, the font was automatically found. UNINSTALL - Font removal function do not seem to work at the momment -> I am trying to find a fix for this and will post this as soon as i find one.

Description

This page uses the functionality of the FontName plug-in. This will allow you to register TTF fonts without the need to supply the font's registry name. The second macro is designed to register FON fonts and still requires the input of the font's name. If you do not wish to use the FontName plugin you should use the FON font macro to install TTF fonts.

These macros have been designed to work with NSIS 2.0b4 (CVS), although it could possibly be modified to work with earlier versions of NSIS.

These macros allow the user to uninstall fonts in the uninstaller as does the standard version the main difference being that if you define FontBackup to a 'registry store' of the fonts the installer created it will only remove fonts that were not present before the program was installed.

Please note that uninstalling fonts is generally not a good idea since other programs might depend on them. Exceptions would be custom created fonts which would only be used by your program or when installing an *huge* amount of fonts.

Save this script as a NSIS Include file in the Include directory of NSIS so that then you can easily get font registration functionality in any script by simply including this file. (Suggested file name: FontRegAdv.nsh)

The Script

var FONT_DIR
 
!include "WinMessages.nsh"
!ifndef CSIDL_FONTS
  !define CSIDL_FONTS '0x14' ;Fonts directory path constant
!endif
!ifndef CSIDL_FLAG_CREATE
  !define CSIDL_FLAG_CREATE 0x8000
!endif
 
### Modified Code from FileFunc.nsh    ###
### Original by Instructor and kichik  ###
 
!include "Util.nsh"
 
!macro AdvFontUtil_GetFileNameImp
	Exch $0
	Push $1
	Push $2
 
	StrCpy $2 $0 1 -1
	StrCmp $2 '\' 0 +3
	StrCpy $0 $0 -1
	goto -3
 
	StrCpy $1 0
	IntOp $1 $1 - 1
	StrCpy $2 $0 1 $1
	StrCmp $2 '' end
	StrCmp $2 '\' 0 -3
	IntOp $1 $1 + 1
	StrCpy $0 $0 '' $1
 
	end:
	Pop $2
	Pop $1
	Exch $0
!macroend
!macro AdvFontUtil_GetFileName _PATHSTRING _RESULT
	Push `${_PATHSTRING}`
	!insertmacro CallArtificialFunction AdvFontUtil_GetFileNameImp
	Pop ${_RESULT}
!macroend
!ifndef GetFileName
	!define GetFileName `!insertmacro AdvFontUtil_GetFileName`
!endif
 
### End Code From ###
 
### We now need to fix the broken FontName.nsh file ###
!ifmacroNdef FontNameVer
	!include FontName.nsh
!endif
!macro AdvFontUtil_CheckFontNameError
	exch $1
	strcmp $1 "*:*" 0 Index
		pop $1
		exch $1
		SetErrors
Index:
	exch $1
!macroend
!macro AdvFontUtil_TranslateFontName
  StrCmp $LANGUAGE 1063 0 End-1063 ; Lithuanian (by Vytautas Krivickas)
    Push "Neteisinga šrifto versija"
    Push "Planines bylos adreso klaida: %u"
    Push "Planines bylos sukurimo klaida: %u"
    Push "Neteisingas bylos dydis: %u"
    Push "Neteisinga bylos rankena: %u"
    Push "FontName %s ijungiamoji byla i NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1063:
  StrCmp $LANGUAGE 1031 0 End-1031 ; German (by Jan T. Sott)
    Push "Falsche Fontversion"
    Push "MappedFile Addressfehler: %u"
    Push "MappedFile Fehler: %u"
    Push "Ungültige Dateigrösse: %u"
    Push "Ungültiges Dateihandle %u"
    Push "FontName %s Plugin für NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1031:
  StrCmp $LANGUAGE 1037 0 End-1037 ; Hebrew (by kichik)
    Push "âøñú ëåôï ùâåéä"
    Push "ùâéàú ëúåáú ÷åáõ îîåôä: %u"
    Push "ùâéàú ÷åáõ îîåôä: %u"
    Push "âåãì ÷åáõ ìà çå÷é: %u"
    Push "éãéú ÷åáõ ìà çå÷éú %u"
    Push "FontName %s plugin for NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1037:
  StrCmp $LANGUAGE 1046 0 End-1046 ; Portuguese (Brazil) (by deguix)
    Push "Versão de Fonte Errada"
    Push "Erro de Endereço do ArquivoMapeado: %u"
    Push "Erro do ArquivoMapeado: %u"
    Push "Tamanho de arquivo inválido: %u"
    Push "Manuseio de arquivo inválido %u"
    Push "FontName %s plugin para NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1046:
  StrCmp $LANGUAGE 1025 0 End-1025 ; Arabic (by asdfuae)
    Push "ÅÕÏÇÑ ÇáÎØ ÎÇØÆ"
    Push "ÎØÇÁ ÚäæÇä ÎÑíØÉÇáãáÝ: %u"
    Push "ÎØÇÁ ÎÑíØÉ ÇáãáÝ: %u"
    Push "ÍÌã ÇáãáÝ ÛíÑÕÍíÍ: %u"
    Push "ãÚÇáÌ ÇáãáÝ ÛíÑ ÕÍíÍ %u"
    Push "ãÞÈÓ ÇÓã ÇáÎØ %s áäÓíÓ"
    goto AdvFontUtil_TranslateFontName__end
  End-1025:
  StrCmp $LANGUAGE 1028 0 End-1028 ; Chinese (Traditional) by Kii Ali <kiiali@cpatch.org>
    Push "¿ù»~ªº¦r«¬ª©¥»"
    Push "¹ïÀ³Àɮצì§}¿ù»~: %u"
    Push "¹ïÀ³Àɮ׿ù»~: %u"
    Push "µL®ÄªºÀɮפj¤p: %u"
    Push "µL®ÄªºÀɮ׬`µ{: %u"
    Push "¥Î©ó NSIS ªº¦r«¬¦WºÙ %s ´¡¥ó"
    goto AdvFontUtil_TranslateFontName__end
  End-1028:
  StrCmp $LANGUAGE 2052 0 End-2052 ; Chinese (Simplified) by Kii Ali <kiiali@cpatch.org>
    Push "´íÎóµÄ×ÖÌå°æ±¾"
    Push "Ó³ÉäÎļþµØÖ·´íÎó: %u"
    Push "Ó³ÉäÎļþ´íÎó: %u"
    Push "ÎÞЧµÄÎļþ´óС: %u"
    Push "ÎÞЧµÄÎļþ±ú³Ì: %u"
    Push "ÓÃÓÚ NSIS µÄ×ÖÌåÃû³Æ %s ²å¼þ"
    goto AdvFontUtil_TranslateFontName__end
  End-2052:
  StrCmp $LANGUAGE 1036 0 End-1036 ; French by evilO/Olive
    Push "Version de police incorrecte"
    Push "Erreur d'adresse du fichier mappé : %u"
    Push "Erreur de fichier mappé : %u"
    Push "Taille de fichier invalide : %u"
    Push "Descripteur de fichier invalide %u"
    Push "FontName %s plugin pour NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1036:
  StrCmp $LANGUAGE 1034 0 End-1034 ; Spanish (traditional) by Cecilio
    Push "Versión del font incorrecta"
    Push "Error de dirección de archivo mapeado: %u"
    Push "Error de archivo mapeado: %u"
    Push "Tamaño de archivo erroneo: %u"
    Push "Manipulador de archivo erroneo: %u"
    Push "Plugin de NSIS para FontName %s "
    goto AdvFontUtil_TranslateFontName__end
  End-1034:
  StrCmp $LANGUAGE 1071 0 End-1071 ; Macedonian by Sasko Zdravkin <wingman2083@yahoo.com>
    Push "Ïîãðåøíà âåðçè¼à íà Ôîíòîò"
    Push "ÌàïèðàíàòàÄàòîòåêà Ãðåøêà íà àäðåñàòà: %u"
    Push "ÌàïèðàíàòàÄàòîòåêà Ãðåøêà: %u"
    Push "Ïîãðåøíà ãîëåìèíà íà äàòîòåêàòà: %u"
    Push "Ïîãðåøíî ðàêóâàœå ñî äàòîòåêàòà: %u"
    Push "FontName %s ïëóãèí çà NSIS"
    goto AdvFontUtil_TranslateFontName__end
  End-1071:
; Add your languages here
 
  ; Default English (1033) by Vytautas Krivickas - MUST REMAIN LAST!
  Push "Wrong Font Version"
  Push "MappedFile Address Error: %u"
  Push "MappedFile Error: %u"
  Push "Invalid file size: %u"
  Push "Invalid file handle %u"
  Push "FontName %s plugin for NSIS"
  goto AdvFontUtil_TranslateFontName__end
AdvFontUtil_TranslateFontName__end:
!macroend
!macro UnFontName FONTFILE
	push ${FONTFILE}
	!insertmacro CallArtificialFunction AdvFontUtil_TranslateFontName
	FontName::Name
	!insertmacro CallArtificialFunction AdvFontUtil_CheckFontNameError
!macroend
### End of FontName.nsh fix ###
 
!macro InstallTTF FontFile
  Push $0  
  Push $R0
  Push $R1
  Push $R2
  Push $R3
 
  !define Index 'Line${__LINE__}'
 
; Get the Font's File name
  ${GetFileName} "${FontFile}" $0
  !define FontFileName $0
 
  SetOutPath $FONT_DIR
  IfFileExists "$FONT_DIR\${FontFileName}" ${Index} "${Index}-New"
 
  !ifdef FontBackup
    "${Index}-New:"
    ;Implementation of Font Backup Store
      WriteRegStr HKLM "${FontBackup}" "${FontFileName}" "OK"
      File '${FontFile}'
      goto ${Index}
  !else
    "${Index}-New:"
      File '${FontFile}'
      goto ${Index}
  !endif
 
${Index}:
  ClearErrors
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
  IfErrors "${Index}-9x" "${Index}-NT"
 
"${Index}-NT:"
  StrCpy $R1 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-9x:"
  StrCpy $R1 "Software\Microsoft\Windows\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-GO:"
  ClearErrors
  !insertmacro FontName "$FONT_DIR\${FontFileName}"
  pop $R2
  IfErrors 0 "${Index}-Add"
    MessageBox MB_OK "$R2"
    goto "${Index}-End"
 
"${Index}-Add:"
  StrCpy $R2 "$R2 (TrueType)"
  ClearErrors
  ReadRegStr $R0 HKLM "$R1" "$R2"
  IfErrors 0 "${Index}-End"
    System::Call "GDI32::AddFontResourceA(t) i ('${FontFileName}') .s"
    WriteRegStr HKLM "$R1" "$R2" "${FontFileName}"
    goto "${Index}-End"
 
"${Index}-End:"
 
  !undef Index
  !undef FontFileName
 
  pop $R3
  pop $R2
  pop $R1
  Pop $R0  
  Pop $0
!macroend
 
!macro InstallFON FontFile FontName
  Push $0  
  Push $R0
  Push $R1
 
  !define Index 'Line${__LINE__}'
 
; Get the Font's File name
  ${GetFileName} "${FontFile}" $0
  !define FontFileName $0
 
  SetOutPath $FONT_DIR
  IfFileExists "$FONT_DIR\${FontFileName}" ${Index} "${Index}-New"
 
  !ifdef FontBackup
    "${Index}-New:"
    ;Implementation of Font Backup Store
      WriteRegStr HKLM "${FontBackup}" "${FontFileName}" "OK"
      File '${FontFile}'
      goto ${Index}
  !else
    "${Index}-New:"
      File '${FontFile}'
      goto ${Index}
  !endif
 
${Index}:
  ClearErrors
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
  IfErrors "${Index}-9x" "${Index}-NT"
 
"${Index}-NT:"
  StrCpy $R1 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-9x:"
  StrCpy $R1 "Software\Microsoft\Windows\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-GO:"
  ClearErrors
  ReadRegStr $R0 HKLM "$R1" "${FontName}"
  IfErrors 0 "${Index}-End"
    System::Call "GDI32::AddFontResourceA(t) i ('${FontFileName}') .s"
    WriteRegStr HKLM "$R1" "${FontName}" "${FontFileName}"
    goto "${Index}-End"
 
"${Index}-End:"
 
  !undef Index
  !undef FontFileName
 
  pop $R1
  Pop $R0  
  Pop $0
!macroend
 
; Uninstaller entries
 
!macro RemoveTTF FontFile
  Push $0  
  Push $R0
  Push $R1
  Push $R2
  Push $R3
  Push $R4
 
  !define Index 'Line${__LINE__}'
 
; Get the Font's File name
  ${GetFileName} "${FontFile}" $0
  !define FontFileName $0
 
  IfFileExists "$FONT_DIR\${FontFileName}" ${Index} "${Index}-End"
 
${Index}:
  ClearErrors
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
  IfErrors "${Index}-9x" "${Index}-NT"
 
"${Index}-NT:"
  StrCpy $R1 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-9x:"
  StrCpy $R1 "Software\Microsoft\Windows\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
  !ifdef FontBackup
  "${Index}-GO:"
  ;Implementation of Font Backup Store
    StrCpy $R2 ''
    ReadRegStr $R2 HKLM "${FontBackup}" "${FontFileName}"
    StrCmp $R2 'OK' 0 "${Index}-Skip"
 
    ClearErrors
    !insertmacro UnFontName "$FONT_DIR\${FontFileName}"
    pop $R2
    IfErrors 0 "${Index}-Remove"
    MessageBox MB_OK "$R2"
    goto "${Index}-End"    
 
  "${Index}-Remove:"
    StrCpy $R2 "$R2 (TrueType)"
    System::Call "GDI32::RemoveFontResourceA(t) i ('${FontFileName}') .s"
    DeleteRegValue HKLM "$R1" "$R2"
    DeleteRegValue HKLM "${FontBackup}" "${FontFileName}"
    EnumRegValue $R4 HKLM "${FontBackup}" 0
    IfErrors 0 "${Index}-NoError"
      MessageBox MB_OK "FONT (${FontFileName}) Removal.$\r$\n(Registry Key Error: $R4)$\r$\nRestart computer and try again. If problem persists contact your supplier."
      Abort "EnumRegValue Error: ${FontFileName} triggered error in EnumRegValue for Key $R4."
  "${Index}-NoError:"
    StrCmp $R4 "" 0 "${Index}-NotEmpty"
      DeleteRegKey HKLM "${FontBackup}" ; This will delete the key if there are no more fonts...
  "${Index}-NotEmpty:"
    Delete /REBOOTOK "$FONT_DIR\${FontFileName}"
    goto "${Index}-End"
  "${Index}-Skip:"
    goto "${Index}-End"
  !else
  "${Index}-GO:"
 
    ClearErrors
    !insertmacro UnFontName "$FONT_DIR\${FontFileName}"
    pop $R2
    IfErrors 0 "${Index}-Remove"
    MessageBox MB_OK "$R2"
    goto "${Index}-End"
 
  "${Index}-Remove:"
    StrCpy $R2 "$R2 (TrueType)"
    System::Call "GDI32::RemoveFontResourceA(t) i ('${FontFileName}') .s"
    DeleteRegValue HKLM "$R1" "$R2"
    delete /REBOOTOK "$FONT_DIR\${FontFileName}"
    goto "${Index}-End"
  !endif
 
"${Index}-End:"
 
  !undef Index
  !undef FontFileName
 
  pop $R4
  pop $R3
  pop $R2
  pop $R1
  Pop $R0  
  Pop $0
!macroend
 
!macro RemoveFON FontFile FontName
  Push $0  
  Push $R0
  Push $R1
  Push $R2
  Push $R3
  Push $R4
 
  !define Index 'Line${__LINE__}'
 
; Get the Font's File name
  ${GetFileName} "${FontFile}" $0
  !define FontFileName $0
 
  IfFileExists "$FONT_DIR\${FontFileName}" ${Index} "${Index}-End"
 
${Index}:
  ClearErrors
  ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
  IfErrors "${Index}-9x" "${Index}-NT"
 
"${Index}-NT:"
  StrCpy $R1 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
"${Index}-9x:"
  StrCpy $R1 "Software\Microsoft\Windows\CurrentVersion\Fonts"
  goto "${Index}-GO"
 
  !ifdef FontBackup
  "${Index}-GO:"
  ;Implementation of Font Backup Store
    StrCpy $R2 ''
    ReadRegStr $R2 HKLM "${FontBackup}" "${FontFileName}"
    StrCmp $R2 'OK' "${Index}-Remove" "${Index}-Skip"
 
  "${Index}-Remove:"
    System::Call "GDI32::RemoveFontResourceA(t) i ('${FontFileName}') .s"
    DeleteRegValue HKLM "$R1" "${FontName}"
    DeleteRegValue HKLM "${FontBackup}" "${FontFileName}"
    EnumRegValue $R4 HKLM "${FontBackup}" 0
    IfErrors 0 "${Index}-NoError"
      MessageBox MB_OK "FONT (${FontFileName}) Removal.$\r$\n(Registry Key Error: $R4)$\r$\nRestart computer and try again. If problem persists contact your supplier."
      Abort "EnumRegValue Error: ${FontFileName} triggered error in EnumRegValue for Key $R4."
  "${Index}-NoError:"
    StrCmp $R4 "" 0 "${Index}-NotEmpty"
      DeleteRegKey HKLM "${FontBackup}" ; This will delete the key if there are no more fonts...
  "${Index}-NotEmpty:"
    Delete /REBOOTOK "$FONT_DIR\${FontFileName}"
    goto "${Index}-End"
  "${Index}-Skip:"
    goto "${Index}-End"
  !else
  "${Index}-GO:"
    System::Call "GDI32::RemoveFontResourceA(t) i ('${FontFileName}') .s"
    DeleteRegValue HKLM "$R1" "${FontName}"
    delete /REBOOTOK "$FONT_DIR\${FontFileName}"
    goto "${Index}-End"
  !endif
 
"${Index}-End:"
 
  !undef Index
  !undef FontFileName
 
  pop $R4
  pop $R3
  pop $R2
  pop $R1
  Pop $R0  
  Pop $0
!macroend

After including that file in your script you should make sure that the '$FONT_DIR' variable is set to the right fonts directory of the target machine and make sure that you define the FontBackup variable to point to the location were you would like to store the names of the fonts that were not present on the users machine when the installer was executed and thus should be safe to remove when uninstalling provided that no newer program are using this font. Also of note is the last command in the fonts Section, it enables Windows and other programs to refresh their fonts lists without the need to restart the computer. Fonts will most likely not be completely removed until the PC is restarted as fonts are only removed if no programs are using the fonts list.

Calling the RemoveTTF and RemoveFON macros is identical to their installation variants. You should note that you must also include the FontName.nsh file available with the FontName plugin.

!include FontRegAdv.nsh
!include FontName.nsh
 
!define FontBackup Reg\key\To\Backup\Fonts\entries\To
 
;[...]
 
Section "Fonts"
; Alternate for older versions of NSIS: pre NSIS v2.0rc1
;  push $1
;  System::Call "Shell32::SHGetSpecialFolderLocation(i $HWNDPARENT, i ${CSIDL_FONTS}|${CSIDL_FLAG_CREATE}, *i .r0)"
;  System::Call "Shell32::SHGetPathFromIDList(i r0, t .r1)"
;  System::Call 'shell32::SHGetMalloc(*i . r2)' ; IMalloc
;  System::Call '$2->5(i r0)' ; ->Free
;  System::Call '$2->2()' ; ->Release
;  StrCpy $FONT_DIR $1
;  pop $1
 
  StrCpy $FONT_DIR $FONTS
 
  !insertmacro InstallTTF 'ARLRDBD.TTF'
  !insertmacro InstallTTF 'LUCON.TTF'
  !insertmacro InstallTTF 'OCRAEXT.TTF'
  !insertmacro InstallFON 'ROMAN.FON'   'Roman (All res)'
  !insertmacro InstallFON 'SMALLE.FON'  'Small Fonts (VGA res)'
 
  SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
SectionEnd
 
Section "un.Fonts"
; Alternate for older versions of NSIS: pre NSIS v2.0rc1
;  push $1
;  System::Call "Shell32::SHGetSpecialFolderLocation(i $HWNDPARENT, i ${CSIDL_FONTS}|${CSIDL_FLAG_CREATE}, *i .r0)"
;  System::Call "Shell32::SHGetPathFromIDList(i r0, t .r1)"
;  System::Call 'shell32::SHGetMalloc(*i . r2)' ; IMalloc
;  System::Call '$2->5(i r0)' ; ->Free
;  System::Call '$2->2()' ; ->Release
;  StrCpy $FONT_DIR $1
;  pop $1
 
  StrCpy $FONT_DIR $FONTS
 
  !insertmacro RemoveTTF 'ARLRDBD.TTF'
  !insertmacro RemoveTTF 'LUCON.TTF'
  !insertmacro RemoveTTF 'OCRAEXT.TTF'
  !insertmacro RemoveFON 'ROMAN.FON'   'Roman (All res)'
  !insertmacro RemoveFON 'SMALLE.FON'  'Small Fonts (VGA res)'
 
  SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
SectionEnd

Note: Un.installation part for this plug in generate error nd wont work but other things work correct.

Version History

[Update 2016-06-18]
Attempted to fix uninstall compiler error
[Update 2010-11-23]
Added "Font path has white space" fix from Register_Fonts (Always remember to port fixes to both pages in the future)
[Update 2005-10-23]
Fixed problem with installation not working when font file was in a different folder as reported by Francois Gelinas
[Update 2005-01-18]
Fixed problem with FON installation as reported by Francois Gelinas
[Update 2004-01-29]
Updated to use FontName v0.6
[Update 2003-12-29]
Added definition of CSIDL_FLAG_CREATE flag.
[Update 2003-12-28]
Added a note about $FONTS variable first included in NSIS v2.0rc1
[Update 2003-12-23]
Fixed some typos in the documentation and notes.
[Update 2003-12-07]
Fixed problem with remove macros removing only the first font, thanks to lewellyn for bug report and solution.
[Update 2003-12-05]
Added a note about the font filenames and macro usage if fonts not in the same folder as the script.
[Update 2003-11-28]
Fixed problem with TTF font installation on Windows 9x with help from brainsucker and kichik. Added error reporting functionality for the FontName plugin.
[Update 2003-11-21]
Fixed macro name mismatch.

Vytautas

Personal tools
donate
ads