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.
static void checkXBFFont(void){
HANDLE hFile;
GUI_RECT Rect;
GUI_FONT Font;
GUI_XBF_DATA Data;
// 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;
//
// Create file handle
//
hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE) {
//
// 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_EXT, _GetData, &hFile);
GUI_SetFont(&Font);
GUI_SetFont(UI_PLAIN_TEXT_FONT_H1);
GUI_SetColor(GUI_RED);
GUI_FillRectEx(&Rect);
GUI_SetColor(GUI_BLACK);
GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of an XBF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
CloseHandle(hFile);
GUI_XBF_DeleteFont(&Font);
}
while (1) {
GUI_Delay(100);
}
}
Display More
The above example code naturally uses the windows file system for this purpose.
Here is the code I am using on the hardware.
static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer) {
FS_FILE * pFile;
int NumBytesRead;
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
}
static void checkXBFFont(void){
FS_FILE * pFile = NULL;
char acVolumeName[10];
GUI_RECT Rect;
static GUI_FONT Font;
GUI_XBF_DATA Data;
// 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;
//
// Start emFile
//
//FS_Init();
//
// Enable long file name support
//
//FS_FAT_SupportLFN();
//
// Mount volume
//
// int ret = FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
// debug_console_send_message("GetVolumeName successful for volume: %s\n", acVolumeName);
// if ((ret = FS_Mount(acVolumeName)) > 0) {
// // Open file
// debug_console_send_message("GetVolumeName and Mount successfull for volume: %s\n", acVolumeName);
// pFile = FS_FOpen(FILE_PATH, "rb");
// }
// else{
// debug_console_send_message("Mount unsuccessfull with ret code: %d\n", ret);
//
// debug_console_send_message("File open o peration failed - error: %d (%s)\n", ret,
// FS_ErrorNo2Text(ret));
// }
pFile = FS_FOpen(FILE_PATH, "rb");
debug_console_send_message("Opening file: %s, can it? %s\n", FILE_PATH, (pFile == NULL) ? "No" : "Yes");
if(pFile != NULL){
debug_console_send_message("%s File Open Success\n", FILE_PATH);
int res = GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_EXT, _GetData, pFile);
if(res == 1){
debug_console_send_message("Font creation failed, ret=%d\n", res);
}
else {
debug_console_send_message("Font Creation success\n");
}
GUI_SetFont(&Font);
GUI_SetColor(GUI_RED);
GUI_FillRectEx(&Rect);
GUI_SetColor(GUI_BLACK);
GUI_DispStringInRectWrap("This text is displayed in a custom font loaded out of an XBF file.", &Rect, GUI_TA_LEFT, GUI_WRAPMODE_WORD);
FS_FClose(pFile);
//GUI_XBF_DeleteFont(&Font);
}
else{
//debug_console_send_message("File open operation failed - error: %d (%s)\n", emfile_ret,
// FS_ErrorNo2Text(emfile_ret));
}
while (1) {
GUI_Delay(100);
}
}
Display More
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:
1398|Opening file: diskpart:1:\F2S, can it? Yes
1398|diskpart:1:\F2S File Open Success
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?