Create chinese font

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

    • Hi,

      Try the code below. The font gets created within the parent window of a button. The font data and the handle to the file needs to be valid as long as the button uses the font. Therefore the variables are declared static.

      On delete of the window (which does not occur in this example) the button gets also deleted (automatically). At this point the font is not used anymore and can be deleted, too. Although, the window never gets deleted in this example it should show how to make sure a font stays valid as long as it is used.

      C Source Code

      1. /*********************************************************************
      2. * SEGGER Microcontroller GmbH *
      3. * Solutions for real time microcontroller applications *
      4. **********************************************************************
      5. * *
      6. * (c) 1996 - 2020 SEGGER Microcontroller GmbH *
      7. * *
      8. * Internet: www.segger.com Support: support@segger.com *
      9. * *
      10. **********************************************************************
      11. ** emWin V6.10 - Graphical user interface for embedded applications **
      12. emWin is protected by international copyright laws. Knowledge of the
      13. source code may not be used to write a similar product. This file may
      14. only be used in accordance with a license and should not be re-
      15. distributed in any way. We appreciate your understanding and fairness.
      16. ----------------------------------------------------------------------
      17. File : GUI_SetExternalFont_XBF.c
      18. Purpose : Sample that demonstrates how to create a custom font
      19. from an external .XBF file
      20. This sample supports both usage on a Windows PC as well
      21. as usage on a target device using emFile.
      22. Requirements: WindowManager - ( )
      23. MemoryDevices - ( )
      24. AntiAliasing - ( )
      25. VNC-Server - ( )
      26. PNG-Library - ( )
      27. TrueTypeFonts - ( )
      28. ---------------------------END-OF-HEADER------------------------------
      29. */
      30. #include "DIALOG.h"
      31. #include <Windows.h>
      32. /*********************************************************************
      33. *
      34. * Defines
      35. *
      36. **********************************************************************
      37. */
      38. #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Fonts\\Roboto30.xbf"
      39. /*********************************************************************
      40. *
      41. * Static code
      42. *
      43. **********************************************************************
      44. */
      45. /*********************************************************************
      46. *
      47. * _GetData
      48. */
      49. static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer) {
      50. HANDLE hFile;
      51. DWORD NumBytesRead;
      52. hFile = *(HANDLE *)pVoid;
      53. //
      54. // Set file pointer to the requested position
      55. //
      56. if (SetFilePointer(hFile, Off, 0, FILE_BEGIN) == 0xFFFFFFFF) {
      57. return 1; // Error
      58. }
      59. //
      60. // Read font data
      61. //
      62. if (!ReadFile(hFile, pBuffer, NumBytes, &NumBytesRead, 0)) {
      63. return 1; // Error
      64. }
      65. if (NumBytesRead != NumBytes) {
      66. return 1; // Error
      67. }
      68. return 0; // Ok
      69. }
      70. /*********************************************************************
      71. *
      72. * _cbBk
      73. */
      74. static void _cbBk(WM_MESSAGE * pMsg) {
      75. static HANDLE hFile;
      76. static GUI_RECT Rect;
      77. static GUI_FONT Font;
      78. static GUI_XBF_DATA Data;
      79. WM_HWIN hItem;
      80. switch (pMsg->MsgId) {
      81. case WM_CREATE:
      82. //
      83. // Create file handle
      84. //
      85. hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
      86. //
      87. // Create the font from the XBF file
      88. // The font and xbf data will be loaded into the two variables created above.
      89. // This data has to be valid during the time we use the font.
      90. //
      91. GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_AA4_EXT, _GetData, &hFile);
      92. hItem = BUTTON_CreateEx(10, 10, 160, 40, pMsg->hWin, WM_CF_SHOW, 0, 0);
      93. BUTTON_SetFont(hItem, &Font);
      94. BUTTON_SetText(hItem, "A button");
      95. break;
      96. case WM_PAINT:
      97. GUI_SetBkColor(GUI_BLACK);
      98. GUI_Clear();
      99. break;
      100. case WM_DELETE:
      101. //
      102. // Close file handle
      103. //
      104. CloseHandle(hFile);
      105. //
      106. // When the font is no longer currently used, it should be deleted.
      107. //
      108. GUI_XBF_DeleteFont(&Font);
      109. break;
      110. default:
      111. WM_DefaultProc(pMsg);
      112. break;
      113. }
      114. }
      115. /*********************************************************************
      116. *
      117. * Public code
      118. *
      119. **********************************************************************
      120. */
      121. /*********************************************************************
      122. *
      123. * MainTask
      124. */
      125. void MainTask(void) {
      126. //
      127. // Init emWin.
      128. //
      129. GUI_Init();
      130. WM_MULTIBUF_Enable(1);
      131. WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbBk, 0);
      132. while (1) {
      133. GUI_Delay(100);
      134. }
      135. }
      136. /*************************** End of file ****************************/
      Display All

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hi,

      CreateFile() is a function provided by the Win32-API. It is used in the above example so it can be run in the emWin simulation on Windows.

      If you want to use an XBF font from external memory, your target needs a file system. You can then adapt the sample by replacing the Win32-functions by the equal functions provided by the target file system you are going to use.

      You can find an example in the SEGGER Wiki that demonstrates how external XBF fonts can be used in emWin, demonstrated via both the Windows and the emFile system.

      Best regards,
      Florian
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Florian,

      I have tried that example but for some reason it doesn't work on widgets.
      Is there a way to set this external Font as default font or this function has to be called every time a new screen is drawn?

      Best regards,
      BMD
    • Hi,

      since a regular GUI_FONT is created from the XBF-file, the font can be used at any point where a font can be set, including widgets.

      Though it is crucial the pointers to GUI_FONT and GUI_XBF_DATA stay valid and the file is kept open while using the font. This is because, every time the font is redrawn (like when a widget uses it), the pixel data of the font is read from the file.

      When the font is not needed anymore (e.g. when the widget is deleted), GUI_XBF_DeleteFont() can be called and the file can be closed.

      Best regards,
      Florian
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • HI Florian,

      Appreciate all your help. So I tried the following code but I don't see anything on the display whatsoever. Could you please tell me where I am possibly going wrong?
      Thank you for all your help.

      Best regards,
      BMD

      C Source Code

      1. /*********************************************************************
      2. * SEGGER Microcontroller GmbH *
      3. * Solutions for real time microcontroller applications *
      4. **********************************************************************
      5. * *
      6. * (c) 1996 - 2020 SEGGER Microcontroller GmbH *
      7. * *
      8. * Internet: www.segger.com Support: support@segger.com *
      9. * *
      10. **********************************************************************
      11. ** emWin V6.10 - Graphical user interface for embedded applications **
      12. emWin is protected by international copyright laws. Knowledge of the
      13. source code may not be used to write a similar product. This file may
      14. only be used in accordance with a license and should not be re-
      15. distributed in any way. We appreciate your understanding and fairness.
      16. ----------------------------------------------------------------------
      17. File : GUI_SetExternalFont_XBF.c
      18. Purpose : Sample that demonstrates how to create a custom font
      19. from an external .XBF file
      20. This sample supports both usage on a Windows PC as well
      21. as usage on a target device using emFile.
      22. Requirements: WindowManager - ( )
      23. MemoryDevices - ( )
      24. AntiAliasing - ( )
      25. VNC-Server - ( )
      26. PNG-Library - ( )
      27. TrueTypeFonts - ( )
      28. ---------------------------END-OF-HEADER------------------------------
      29. */
      30. #include "DIALOG.h"
      31. #include "FS.h"
      32. /*********************************************************************
      33. *
      34. * Defines
      35. *
      36. **********************************************************************
      37. */
      38. #define FILE_PATH "Font.xbf"
      39. /*********************************************************************
      40. *
      41. * Static code
      42. *
      43. **********************************************************************
      44. */
      45. /*********************************************************************
      46. *
      47. * _GetData
      48. */
      49. static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer)
      50. {
      51. FS_FILE * pFile;
      52. int NumBytesRead;
      53. pFile = (FS_FILE *)pVoid;
      54. //
      55. // Set file pointer to the requested position
      56. //
      57. if(FS_SetFilePos(pFile, Off, FS_FILE_BEGIN))
      58. {
      59. return 1; // Error
      60. }
      61. //
      62. // Read font data
      63. //
      64. NumBytesRead = FS_FRead(pBuffer, 1, NumBytes, pFile);
      65. if (NumBytesRead == 0)
      66. {
      67. return 1; // Error
      68. }
      69. if (NumBytesRead != NumBytes)
      70. {
      71. return 1; // Error
      72. }
      73. return 0; // Ok
      74. }
      75. /*********************************************************************
      76. *
      77. * _cbBk
      78. */
      79. static void _cbBk(WM_MESSAGE * pMsg) {
      80. FS_FILE * pFile;
      81. static GUI_RECT Rect;
      82. static GUI_FONT Font;
      83. static GUI_XBF_DATA Data;
      84. WM_HWIN hItem;
      85. switch (pMsg->MsgId) {
      86. case WM_CREATE:
      87. //
      88. // Create file handle
      89. //
      90. pFile = FS_FOpen(FILE_PATH, "rb");
      91. //
      92. // Create the font from the XBF file
      93. // The font and xbf data will be loaded into the two variables created above.
      94. // This data has to be valid during the time we use the font.
      95. //
      96. GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_AA4_EXT, _GetData, &pFile);
      97. hItem = BUTTON_CreateEx(10, 10, 160, 40, pMsg->hWin, WM_CF_SHOW, 0, 0);
      98. BUTTON_SetFont(hItem, &Font);
      99. BUTTON_SetText(hItem, "A button");
      100. break;
      101. case WM_PAINT:
      102. GUI_SetBkColor(GUI_RED);
      103. GUI_Clear();
      104. break;
      105. case WM_DELETE:
      106. //
      107. // Close file handle
      108. //
      109. FS_FClose(pFile);
      110. //
      111. // When the font is no longer currently used, it should be deleted.
      112. //
      113. GUI_XBF_DeleteFont(&Font);
      114. break;
      115. default:
      116. WM_DefaultProc(pMsg);
      117. break;
      118. }
      119. }
      120. /*********************************************************************
      121. *
      122. * Public code
      123. *
      124. **********************************************************************
      125. */
      126. /*********************************************************************
      127. *
      128. * MainTask
      129. */
      130. void MainTask(void) {
      131. //
      132. // Init emWin.
      133. //
      134. GUI_Init();
      135. WM_MULTIBUF_Enable(1);
      136. WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbBk, 0);
      137. // while (1) {
      138. // GUI_Delay(100);
      139. // }
      140. }
      141. /*************************** End of file ****************************/
      Display All
    • Hi,

      I noticed a few things:

      1. You must not comment out the super-loop in your main-task when using the Window Manager. This will only work when only using GUI_... functions in the MainTask. Otherwise emWin isn't kept alive and no more routines will be called after the MainTask finished.
      2. You also have to initialize emFile and mount the external volume.

      Once again, have a closer look at the XBF font sample, the file is read exactly the same way.

      Best regards,
      Florian
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Florian,

      Thank you for looking into it. Really appreciate it.

      But I am initializing emWin and emFile in another file before calling Maintask().
      I have tried the XBF font sample. That works perfectly fine. But now I am trying to incorporate it into widgets and that doesn't seem to work at all whatsoever.

      Best regards,
      BMD