Set external TTF font (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_SetExternalFont_TTF.c
GUI SetExternalFont TTF.png
File(s) required
  • GUI_SetExternalFont_TTF.c
  • Roboto.ttf
Runs in simulation Yes
Runs on target Yes
Download GUI_SetExternalFont_TTF.zip

The sample shows how to create a custom font from an external .ttf file. It supports both the usage of Windows file system and emFile.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_SetExternalFont_TTF.c
Purpose     : Sample that demonstrates how to create a custom font
              in emWin from an external .TTF file.
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - (x)
---------------------------END-OF-HEADER------------------------------
*/

#include "GUI.h"
#include <stdlib.h>
#ifdef WIN32
  #include <Windows.h>
#else
  #include "FS.h"
#endif

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
//
// Font size in pixels
//
#define FONT_SIZE 24
#ifdef WIN32
  #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Fonts\\Roboto.ttf"
#else
  #define FILE_PATH "Roboto.ttf"
#endif

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/

/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
#ifdef WIN32
  DWORD          NumBytes;
  DWORD          NumBytesRead;
  HANDLE         hFile;
#else
  FS_FILE      * pFile;
  char           acVolumeName[10];
  int            NumBytes;
#endif
  GUI_RECT       Rect;
  GUI_FONT       Font;
  GUI_TTF_CS     Cs;
  GUI_TTF_DATA   TTF;
  U8           * pData;
  
  //
  // Init GUI.
  //
  GUI_Init();
#ifdef WIN32
  //
  // Create file handle
  //
  hFile = CreateFile(FILE_PATH, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  //
  // If file is valid, read data into buffer
  //
  if (hFile != INVALID_HANDLE_VALUE) {
    NumBytes = GetFileSize(hFile, NULL);
    pData    = (U8 *)malloc(NumBytes);
    ReadFile(hFile, pData, NumBytes, &NumBytesRead, NULL);
  }
  //
  // Close file handle
  //
  CloseHandle(hFile);
#else
  //
  // Start emFile.
  //
  FS_Init();
  //
  // Enable long file name support
  //
  FS_FAT_SupportLFN();
  //
  // Mount volume
  //
  FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
  if (FS_Mount(acVolumeName) > 0) {
    //
    // Open file
    //
    pFile = FS_FOpen(FILE_PATH, "rb");
  }
  //
  // Read file
  //
  if(pFile) {
    NumBytes = FS_GetFileSize(pFile);
    pData    = (U8 *)malloc(NumBytes);
    FS_Read(pFile, pData, NumBytes);
  }
  //
  // Close file after reading operation has finished.
  //
  FS_FClose(pFile);
#endif
  //
  // Fill the TTF struct with the file data read
  //
  TTF.NumBytes = NumBytes;
  TTF.pData    = pData;
  //
  // Fill the GUI_TTF_CS struct
  //
  Cs.FaceIndex   = 0;
  Cs.pTTF        = &TTF;
  Cs.PixelHeight = FONT_SIZE;
  //
  // Create the font by pointing to a font struct and the CS struct
  //
  GUI_TTF_CreateFont(&Font, &Cs);
  //
  // Set the newly created font as the current font
  //
  GUI_SetFont(&Font);
  //
  // Display some text in the current font
  //
  Rect.x0 = 0;
  Rect.y0 = 0;
  Rect.x1 = LCD_GetXSize() - 1;
  Rect.y1 = LCD_GetYSize() - 1;
  GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of a TTF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
  //
  // Clear the TTF cache and free memory
  //
  GUI_TTF_DestroyCache();
  free(pData);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/