Project

General

Profile

Google Fonts in QT

This wiki page describes how to incorporate Google Fonts into your QT applications.

Overview

We have started using Google fonts on several products were we cross develop a graphical user interface to run on our ARM platforms and on Microsoft Windows. Often when an application will only be target on an ARM platform we do a large part of the QT based GUI development on Microsoft Windows using Visual Studio as Visual Studio debugger can be easier for some developers to debug on than the GNU tools. The advantage of using Google fonts is that they are available on all of our platforms and generally give much a more uniform look as code is moved from one platform to another. If your QT application specifies a font, and that font is not available on the target platform, the application picks what it thinks is a close font. Results may vary and usually vary poorly.

Google fonts provide a safe license for use in your embedded applications. Google fonts can be found online here: https://fonts.google.com/

The two fonts that you we often use a basic fonts are:
  1. "Lato" for proportionally spaced text where you might have used either "Arial", "Helvetica", "MS Shell Dlg" or just the QT default font in the past. https://fonts.google.com/specimen/Lato
  2. "Source Code Pro" for mono spaced text where you might have used either "Lucida Console", "Courier" or "mono" font in the past. https://fonts.google.com/specimen/Source+Code+Pro

Converting an Application To Use Google Fonts

The following sections detail areas that we have found which our source code needs attention when switching to Google Fonts.

Default Application Font

QT has a default font. That default font is different on different platforms. On Windows it is "Arial" (a sanserif style) but on Linux it is a serifed style similar to Times Roman. If your application is going to run on different platforms you should define "Lato" as the default font.

To define "Lato" as your application's default font you want to add the following code to your main function after you create your QApplication object.

int main(int argc, char *argv[])
{
    QApplication app (argc, argv);
    app.setFont (QFont ("Lato", 9, QFont::Normal));

Style Sheets

If you are converting an existing application to use Google fonts, check your style sheet (*.qss) files. They may contain font specifications. Also do this if you are taking an existing style sheet from an older existing application.

Source Code Fonts Declarations

If you are converting an existing application to use Google fonts, grep the source code and *.ui files if any for the following keywords to find all internal font declarations:

  • "QFont"
  • "setFont"
  • "Arial"
  • "Lucida"
  • "Times"
  • "Helvetica"
  • "Courier"
  • "MS Shell Dlg"

Font Installation for ARM9 Application on ARM9 Platforms

To install the Google fonts which you use on your ARM9 target filesystem you need to copy all of the "*.ttf" files that are included in the font package into the folder "/usr/lib/fonts/" on your target.

Font Installation for Windows Applications with Nullsoft's NSIS Installer

If your application will be cross target to the Microsoft Windows environment and you are using the NullSoft installer you will need to make the following changes to your installer script to install the Google fonts onto the end users PC. To install them using NSIS, the following needs to be done:

  • Install the font library for NSIS
    • Attached to this item (nsis_FontName-0.7.zip)
    • This is not a great installer. It might give you an indication that it did not install correctly but it probably did.
  • Change the batch file you build your installer with to set the environment variable GOOGLEFONTPATH to point to the folder where your google fonts are:
    rem ---------------------------------------------------------------------------
    rem Set path to fonts, relative to the folder that holds the nsis files.
    rem ---------------------------------------------------------------------------
    set GOOGLEFONTPATH=<some place on your PC where the font TTF files are located>
    
  • Change the installer .nsi script with the following:
    • Include appropriate libraries:
      !include FontName.nsh
      !include FontRegAdv.nsh
      !include WinMessages.nsh
      
    • Include a Fonts section
      ;---------------------------------------------------------------------------
      ;--- Fonts update
      ;---------------------------------------------------------------------------
      Section "Fonts" FONTS_SECTION
      
          SectionIn RO  ; Makes it required.
      
          StrCpy $FONT_DIR $FONTS
          !echo "font_dir is $FONTS" 
      
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Black.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-BlackItalic.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Bold.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-BoldItalic.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Hairline.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-HairlineItalic.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Italic.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Light.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-LightItalic.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\Lato-Regular.ttf
      
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Black.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Bold.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-ExtraLight.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Light.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Medium.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Regular.ttf
          !insertmacro InstallTTF $%GOOGLEFONTPATH%\SourceCodePro-Semibold.ttf
      
          SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
      SectionEnd
      

Uninstalling Fonts

There is also a way to arrange for uninstalling fonts if that is needed. See http://nsis.sourceforge.net/Advanced_Font_Installation

NOTE: You should NOT UNINSTALL FONTS! Once you add a font to a PC, that font is available to all applications on that machine. Your user may use that font for some Word documents and be surprised that they look different once they uninstall your application.

Installer Error

I'm not sure why but if you have the Google fonts page open in Chrome, you will get the following error trying to install the "SourceCodePro-Regular" font. You may see this error if other programs are open that are using any of the Google fonts.

{{fnlist}}

Go to top
Add picture from clipboard (Maximum size: 600 MB)