Set external XBF font (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_SetExternalFont_XBF.c
GUI SetExternalFont XBF.png
File(s) required
  • GUI_SetExternalFont_XBF.c
  • Roboto30.xbf
Runs in simulation Yes
Runs on target Yes
Download GUI_SetExternalFont_XBF.zip

This sample shows how to create a custom font from an external .xbf 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_XBF.c
Purpose     : Sample that demonstrates how to create a custom font
              from an external .XBF file
              This sample supports both usage on a Windows PC as well
              as usage on a target device using emFile.
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#ifdef WIN32
  #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Fonts\\Roboto30.xbf"
#else
  #define FILE_PATH "Roboto30.xbf"
#endif

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _GetData
*/
static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer) {
#ifdef WIN32
  HANDLE    hFile;
  DWORD     NumBytesRead;
#else
  FS_FILE * pFile;
  int       NumBytesRead;
#endif

#ifdef WIN32
  hFile = *(HANDLE *)pVoid;

  //
  // Set file pointer to the requested position
  //
  if (SetFilePointer(hFile, Off, 0, FILE_BEGIN) == 0xFFFFFFFF) {
    return 1; // Error
  }
  //
  // Read font data
  //
  if (!ReadFile(hFile, pBuffer, NumBytes, &NumBytesRead, 0)) {
    return 1; // Error
  }
  if (NumBytesRead != NumBytes) {
    return 1; // Error
  }
  return 0;   // Ok
#else
  pFile = (FS_FILE *)pVoid;
  //
  // Set file pointer to the requested position
  //
  if(FS_SetFilePos(pFile, Off, FS_FILE_BEGIN)) {
    return 1; // Error
  }
  //
  // Read font data
  //
  NumBytesRead = FS_FRead(pBuffer, 1, NumBytes, pFile);

  if (NumBytesRead == 0) {
    return 1; // Error
  }
  if (NumBytesRead != NumBytes) {
    return 1; // Error
  }
  return 0;   // Ok
#endif
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
#ifdef WIN32
  HANDLE       hFile;
#else
  FS_FILE *    pFile;
  char         acVolumeName[10];
#endif
  GUI_RECT     Rect;
  GUI_FONT     Font;
  GUI_XBF_DATA Data;

  //
  // Init emWin.
  //
  GUI_Init();
  //
  // Set up a rectangle with the size of the screen for later use.
  //
  Rect.x0 = 0;
  Rect.y0 = 0;
  Rect.x1 = LCD_GetXSize() - 1;
  Rect.y1 = LCD_GetYSize() - 1;
#ifdef WIN32
  //
  // Create file handle
  //
  hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  //
  // Create the font from the XBF file
  // The font and xbf data will be loaded into the two variables created above.
  // This data has to be valid during the time we use the font.
  //
  GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_AA4_EXT, _GetData, &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");
  }
  //
  // Create the font from the XBF file
  // The font and xbf data will be loaded into the two variables created above.
  // This data has to be valid during the time we use the font.
  //
  GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_AA4_EXT, _GetData, pFile);
#endif
  //
  // Set the newly created font as the current font
  //
  GUI_SetFont(&Font);
  //
  // Display some text in the current font
  //
  GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of an XBF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
#ifdef WIN32
  //
  // Close file handle
  //
  CloseHandle(hFile);
#else
  //
  // Close file handle
  //
  FS_FClose(pFile);
#endif
  //
  // When the font is no longer currently used, it should be deleted.
  //
  GUI_XBF_DeleteFont(&Font);

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

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