Unable to use XBF font on hardware with emFile file system

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Unable to use XBF font on hardware with emFile file system

      Hi,
      I am facing a problem while trying to use xbf font with external memory. We are using NAND storage and emFile file system.
      I have a demo code using the xbf font similar to the example application from emWin here: Set external XBF font (Sample) - SEGGER Knowledge Base
      To generate the xbf font, I used the Font Converter for emWin (Demo version) V6.44 tool. The simulation on VS2022 worked normally with the expected output.

      C Source Code: SimulatorCode VS2022

      1. static void checkXBFFont(void){
      2. HANDLE hFile;
      3. GUI_RECT Rect;
      4. GUI_FONT Font;
      5. GUI_XBF_DATA Data;
      6. // Set up a rectangle with the size of the screen for later use.
      7. Rect.x0 = 0;
      8. Rect.y0 = 0;
      9. Rect.x1 = LCD_GetXSize() - 1;
      10. Rect.y1 = LCD_GetYSize() - 1;
      11. //
      12. // Create file handle
      13. //
      14. hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
      15. if (hFile != INVALID_HANDLE_VALUE) {
      16. //
      17. // Create the font from the XBF file
      18. // The font and xbf data will be loaded into the two variables created above.
      19. // This data has to be valid during the time we use the font.
      20. //
      21. GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_EXT, _GetData, &hFile);
      22. GUI_SetFont(&Font);
      23. GUI_SetFont(UI_PLAIN_TEXT_FONT_H1);
      24. GUI_SetColor(GUI_RED);
      25. GUI_FillRectEx(&Rect);
      26. GUI_SetColor(GUI_BLACK);
      27. GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of an XBF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
      28. CloseHandle(hFile);
      29. GUI_XBF_DeleteFont(&Font);
      30. }
      31. while (1) {
      32. GUI_Delay(100);
      33. }
      34. }
      Display All

      The above example code naturally uses the windows file system for this purpose.


      Here is the code I am using on the hardware.

      C Source Code: emFileXBFExample.c

      1. static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer) {
      2. FS_FILE * pFile;
      3. int NumBytesRead;
      4. pFile = (FS_FILE *)pVoid;
      5. //
      6. // Set file pointer to the requested position
      7. //
      8. if(FS_SetFilePos(pFile, Off, FS_FILE_BEGIN)) {
      9. return 1; // Error
      10. }
      11. //
      12. // Read font data
      13. //
      14. NumBytesRead = FS_FRead(pBuffer, 1, NumBytes, pFile);
      15. if (NumBytesRead == 0) {
      16. return 1; // Error
      17. }
      18. if (NumBytesRead != NumBytes) {
      19. return 1; // Error
      20. }
      21. return 0; // Ok
      22. }
      23. static void checkXBFFont(void){
      24. FS_FILE * pFile = NULL;
      25. char acVolumeName[10];
      26. GUI_RECT Rect;
      27. static GUI_FONT Font;
      28. GUI_XBF_DATA Data;
      29. // Set up a rectangle with the size of the screen for later use.
      30. Rect.x0 = 0;
      31. Rect.y0 = 0;
      32. Rect.x1 = LCD_GetXSize() - 1;
      33. Rect.y1 = LCD_GetYSize() - 1;
      34. //
      35. // Start emFile
      36. //
      37. //FS_Init();
      38. //
      39. // Enable long file name support
      40. //
      41. //FS_FAT_SupportLFN();
      42. //
      43. // Mount volume
      44. //
      45. // int ret = FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
      46. // debug_console_send_message("GetVolumeName successful for volume: %s\n", acVolumeName);
      47. // if ((ret = FS_Mount(acVolumeName)) > 0) {
      48. // // Open file
      49. // debug_console_send_message("GetVolumeName and Mount successfull for volume: %s\n", acVolumeName);
      50. // pFile = FS_FOpen(FILE_PATH, "rb");
      51. // }
      52. // else{
      53. // debug_console_send_message("Mount unsuccessfull with ret code: %d\n", ret);
      54. //
      55. // debug_console_send_message("File open o peration failed - error: %d (%s)\n", ret,
      56. // FS_ErrorNo2Text(ret));
      57. // }
      58. pFile = FS_FOpen(FILE_PATH, "rb");
      59. debug_console_send_message("Opening file: %s, can it? %s\n", FILE_PATH, (pFile == NULL) ? "No" : "Yes");
      60. if(pFile != NULL){
      61. debug_console_send_message("%s File Open Success\n", FILE_PATH);
      62. int res = GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_EXT, _GetData, pFile);
      63. if(res == 1){
      64. debug_console_send_message("Font creation failed, ret=%d\n", res);
      65. }
      66. else {
      67. debug_console_send_message("Font Creation success\n");
      68. }
      69. GUI_SetFont(&Font);
      70. GUI_SetColor(GUI_RED);
      71. GUI_FillRectEx(&Rect);
      72. GUI_SetColor(GUI_BLACK);
      73. GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of an XBF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
      74. FS_FClose(pFile);
      75. //GUI_XBF_DeleteFont(&Font);
      76. }
      77. else{
      78. //debug_console_send_message("File open operation failed - error: %d (%s)\n", emfile_ret,
      79. // FS_ErrorNo2Text(emfile_ret));
      80. }
      81. while (1) {
      82. GUI_Delay(100);
      83. }
      84. }
      Display All
      Here are a few changes I made from the example code provided by emWin:

      1. I ensured some of the setup happens before so I removed the call to FS_Init and FS_Mount.
      2. Made sure FS_Close is only called on a valid file pointer.
      3. Added some logs

      Here is the output I receive:

      Shell-Script: output.log

      1. 1398|Opening file: diskpart:1:\F2S, can it? Yes
      2. 1398|diskpart:1:\F2S File Open Success
      3. 1399|Font Creation success

      I have added these checks and it seems like the Font file was successfully read and font was successfully generated. Still I only see a blank red screen with no text.

      I am using:
      1. emWin V6.38
      2. Font Converted (Demo Version) V6.44
      3. emFile V5.28


      Is there any other way to check if the XBF font is successfully loaded and ready to use?

      The post was edited 1 time, last by sarthak: Typo Using emWin V6.38 and not 28 ().