font.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "font.h"
  24. #include "util.h"
  25. #include "text.h"
  26. #define _FONT_C
  27. #include "fontglyph.h"
  28. #include "ascii.h"
  29. static int _font_height = 16;
  30. INT
  31. PAL_InitFont(
  32. BOOL fUseEmbeddedFonts
  33. )
  34. /*++
  35. Purpose:
  36. None.
  37. Parameters:
  38. None.
  39. Return value:
  40. Always return 0.
  41. --*/
  42. {
  43. if (fUseEmbeddedFonts)
  44. {
  45. FILE *fp;
  46. char *char_buf;
  47. wchar_t *wchar_buf;
  48. int nBytes, nChars, i;
  49. //
  50. // Load the wor16.asc file.
  51. //
  52. if (NULL == (fp = UTIL_OpenFile("wor16.asc")))
  53. {
  54. return 0;
  55. }
  56. //
  57. // Get the size of wor16.asc file.
  58. //
  59. fseek(fp, 0, SEEK_END);
  60. nBytes = ftell(fp);
  61. //
  62. // Allocate buffer & read all the character codes.
  63. //
  64. if (NULL == (char_buf = (char *)malloc(nBytes)))
  65. {
  66. fclose(fp);
  67. return 0;
  68. }
  69. fseek(fp, 0, SEEK_SET);
  70. fread(char_buf, 1, nBytes, fp);
  71. //
  72. // Close wor16.asc file.
  73. //
  74. fclose(fp);
  75. //
  76. // Convert characters into unicode
  77. //
  78. nChars = PAL_MultiByteToWideChar(char_buf, nBytes, NULL, 0);
  79. if (NULL == (wchar_buf = (wchar_t *)malloc(nChars * sizeof(wchar_t))))
  80. {
  81. free(char_buf);
  82. return 0;
  83. }
  84. PAL_MultiByteToWideChar(char_buf, nBytes, wchar_buf, nChars);
  85. free(char_buf);
  86. //
  87. // Read bitmaps from wor16.fon file.
  88. //
  89. fp = UTIL_OpenFile("wor16.fon");
  90. //
  91. // The font glyph data begins at offset 0x682 in wor16.fon.
  92. //
  93. fseek(fp, 0x682, SEEK_SET);
  94. //
  95. // Replace the original fonts
  96. //
  97. for (i = 0; i < nChars; i++)
  98. {
  99. fread(unicode_font[wchar_buf[i]], 30, 1, fp);
  100. unicode_font[wchar_buf[i]][30] = 0;
  101. unicode_font[wchar_buf[i]][31] = 0;
  102. }
  103. free(wchar_buf);
  104. fclose(fp);
  105. for (int i = 0; i < 0x80; i++)
  106. {
  107. memcpy(unicode_font[i], &iso_font[i * 15], 15);
  108. unicode_font[i][15] = 0;
  109. }
  110. _font_height = 15;
  111. }
  112. return 0;
  113. }
  114. VOID
  115. PAL_FreeFont(
  116. VOID
  117. )
  118. /*++
  119. Purpose:
  120. None.
  121. Parameters:
  122. None.
  123. Return value:
  124. None.
  125. --*/
  126. {
  127. }
  128. VOID
  129. PAL_DrawCharOnSurface(
  130. WORD wChar,
  131. SDL_Surface *lpSurface,
  132. PAL_POS pos,
  133. BYTE bColor
  134. )
  135. /*++
  136. Purpose:
  137. Draw a Unicode character on a surface.
  138. Parameters:
  139. [IN] wChar - the unicode character to be drawn.
  140. [OUT] lpSurface - the destination surface.
  141. [IN] pos - the destination location of the surface.
  142. [IN] bColor - the color of the character.
  143. Return value:
  144. None.
  145. --*/
  146. {
  147. int i, j;
  148. int x = PAL_X(pos), y = PAL_Y(pos);
  149. //
  150. // Check for NULL pointer & invalid char code.
  151. //
  152. if (lpSurface == NULL || (wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  153. {
  154. return;
  155. }
  156. //
  157. // Locate for this character in the font lib.
  158. //
  159. if (wChar >= unicode_upper_base)
  160. {
  161. wChar -= (unicode_upper_base - 0xd800);
  162. }
  163. //
  164. // Draw the character to the surface.
  165. //
  166. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  167. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  168. if (font_width[wChar] == 32)
  169. {
  170. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  171. {
  172. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  173. {
  174. if (unicode_font[wChar][i] & (1 << (7 - j)))
  175. {
  176. dest[j] = bColor;
  177. }
  178. }
  179. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  180. {
  181. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  182. {
  183. dest[j + 8] = bColor;
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  191. {
  192. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  193. {
  194. if (unicode_font[wChar][i] & (1 << (7 - j)))
  195. {
  196. dest[j] = bColor;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. INT
  203. PAL_CharWidth(
  204. WORD wChar
  205. )
  206. /*++
  207. Purpose:
  208. Get the text width of a character.
  209. Parameters:
  210. [IN] wChar - the unicode character for width calculation.
  211. Return value:
  212. The width of the character, 16 for full-width char and 8 for half-width char.
  213. --*/
  214. {
  215. if ((wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  216. {
  217. return 0;
  218. }
  219. //
  220. // Locate for this character in the font lib.
  221. //
  222. if (wChar >= unicode_upper_base)
  223. {
  224. wChar -= (unicode_upper_base - 0xd800);
  225. }
  226. return font_width[wChar] >> 1;
  227. }