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