font.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. wchar_t w = (wchar_buf[i] >= unicode_upper_base) ? (wchar_buf[i] - unicode_upper_base + 0xd800) : wchar_buf[i];
  100. fread(unicode_font[w], 30, 1, fp);
  101. unicode_font[w][30] = 0;
  102. unicode_font[w][31] = 0;
  103. }
  104. free(wchar_buf);
  105. fclose(fp);
  106. for (i = 0; i < 0x80; i++)
  107. {
  108. memcpy(unicode_font[i], &iso_font[i * 15], 15);
  109. unicode_font[i][15] = 0;
  110. }
  111. _font_height = 15;
  112. }
  113. return 0;
  114. }
  115. VOID
  116. PAL_FreeFont(
  117. VOID
  118. )
  119. /*++
  120. Purpose:
  121. None.
  122. Parameters:
  123. None.
  124. Return value:
  125. None.
  126. --*/
  127. {
  128. }
  129. VOID
  130. PAL_DrawCharOnSurface(
  131. WORD wChar,
  132. SDL_Surface *lpSurface,
  133. PAL_POS pos,
  134. BYTE bColor,
  135. BOOL fUse8x8Font
  136. )
  137. /*++
  138. Purpose:
  139. Draw a Unicode character on a surface.
  140. Parameters:
  141. [IN] wChar - the unicode character to be drawn.
  142. [OUT] lpSurface - the destination surface.
  143. [IN] pos - the destination location of the surface.
  144. [IN] bColor - the color of the character.
  145. Return value:
  146. None.
  147. --*/
  148. {
  149. int i, j;
  150. int x = PAL_X(pos), y = PAL_Y(pos);
  151. //
  152. // Check for NULL pointer & invalid char code.
  153. //
  154. if (lpSurface == NULL || (wChar >= 0xd800 && wChar < unicode_upper_base) ||
  155. wChar >= unicode_upper_top || (_font_height == 8 && wChar >= 0x100))
  156. {
  157. return;
  158. }
  159. //
  160. // Locate for this character in the font lib.
  161. //
  162. if (wChar >= unicode_upper_base)
  163. {
  164. wChar -= (unicode_upper_base - 0xd800);
  165. }
  166. //
  167. // Draw the character to the surface.
  168. //
  169. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  170. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  171. if (fUse8x8Font)
  172. {
  173. for (i = 0; i < 8 && dest < top; i++, dest += lpSurface->pitch)
  174. {
  175. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  176. {
  177. if (iso_font_8x8[wChar][i] & (1 << j))
  178. {
  179. dest[j] = bColor;
  180. }
  181. }
  182. }
  183. }
  184. else
  185. {
  186. if (font_width[wChar] == 32)
  187. {
  188. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  189. {
  190. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  191. {
  192. if (unicode_font[wChar][i] & (1 << (7 - j)))
  193. {
  194. dest[j] = bColor;
  195. }
  196. }
  197. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  198. {
  199. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  200. {
  201. dest[j + 8] = bColor;
  202. }
  203. }
  204. }
  205. }
  206. else
  207. {
  208. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  209. {
  210. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  211. {
  212. if (unicode_font[wChar][i] & (1 << (7 - j)))
  213. {
  214. dest[j] = bColor;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. INT
  222. PAL_CharWidth(
  223. WORD wChar
  224. )
  225. /*++
  226. Purpose:
  227. Get the text width of a character.
  228. Parameters:
  229. [IN] wChar - the unicode character for width calculation.
  230. Return value:
  231. The width of the character, 16 for full-width char and 8 for half-width char.
  232. --*/
  233. {
  234. if ((wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  235. {
  236. return 0;
  237. }
  238. //
  239. // Locate for this character in the font lib.
  240. //
  241. if (wChar >= unicode_upper_base)
  242. {
  243. wChar -= (unicode_upper_base - 0xd800);
  244. }
  245. return font_width[wChar] >> 1;
  246. }
  247. INT
  248. PAL_FontHeight(
  249. VOID
  250. )
  251. {
  252. return _font_height;
  253. }