font.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_InitEmbeddedFont(
  32. VOID
  33. )
  34. /*++
  35. Purpose:
  36. None.
  37. Parameters:
  38. None.
  39. Return value:
  40. Always return 0.
  41. --*/
  42. {
  43. FILE *fp;
  44. char *char_buf;
  45. wchar_t *wchar_buf;
  46. int nBytes, nChars, i;
  47. //
  48. // Load the wor16.asc file.
  49. //
  50. if (NULL == (fp = UTIL_OpenFile("wor16.asc")))
  51. {
  52. return 0;
  53. }
  54. //
  55. // Get the size of wor16.asc file.
  56. //
  57. fseek(fp, 0, SEEK_END);
  58. nBytes = ftell(fp);
  59. //
  60. // Allocate buffer & read all the character codes.
  61. //
  62. if (NULL == (char_buf = (char *)malloc(nBytes)))
  63. {
  64. fclose(fp);
  65. return 0;
  66. }
  67. fseek(fp, 0, SEEK_SET);
  68. fread(char_buf, 1, nBytes, fp);
  69. //
  70. // Close wor16.asc file.
  71. //
  72. fclose(fp);
  73. //
  74. // Convert characters into unicode
  75. //
  76. nChars = PAL_MultiByteToWideChar(char_buf, nBytes, NULL, 0);
  77. if (NULL == (wchar_buf = (wchar_t *)malloc(nChars * sizeof(wchar_t))))
  78. {
  79. free(char_buf);
  80. return 0;
  81. }
  82. PAL_MultiByteToWideChar(char_buf, nBytes, wchar_buf, nChars);
  83. free(char_buf);
  84. //
  85. // Read bitmaps from wor16.fon file.
  86. //
  87. fp = UTIL_OpenFile("wor16.fon");
  88. //
  89. // The font glyph data begins at offset 0x682 in wor16.fon.
  90. //
  91. fseek(fp, 0x682, SEEK_SET);
  92. //
  93. // Replace the original fonts
  94. //
  95. for (i = 0; i < nChars; i++)
  96. {
  97. wchar_t w = (wchar_buf[i] >= unicode_upper_base) ? (wchar_buf[i] - unicode_upper_base + 0xd800) : wchar_buf[i];
  98. fread(unicode_font[w], 30, 1, fp);
  99. unicode_font[w][30] = 0;
  100. unicode_font[w][31] = 0;
  101. }
  102. free(wchar_buf);
  103. fclose(fp);
  104. _font_height = 15;
  105. return 0;
  106. }
  107. INT
  108. PAL_LoadBdfFont(
  109. LPCSTR pszBdfFileName
  110. )
  111. /*++
  112. Purpose:
  113. Loads a BDF bitmap font file.
  114. Parameters:
  115. [IN] pszBdfFileName - Name of BDF bitmap font file..
  116. Return value:
  117. 0 = success, -1 = failure.
  118. --*/
  119. {
  120. char buf[4096];
  121. int state = 0;
  122. int codepage = -1;
  123. DWORD dwEncoding = 0;
  124. BYTE bFontGlyph[32] = {0};
  125. int iCurHeight = 0;
  126. FILE *fp = UTIL_OpenFileForMode(pszBdfFileName, "r");
  127. if (fp == NULL)
  128. {
  129. return -1;
  130. }
  131. while (fgets(buf, 4096, fp) != NULL)
  132. {
  133. if (state == 0)
  134. {
  135. if (strncmp(buf, "CHARSET_REGISTRY", 16) == 0)
  136. {
  137. if (strstr(buf, "Big5") != NULL)
  138. {
  139. codepage = CP_BIG5;
  140. }
  141. else if (strstr(buf, "BIG5") != NULL)
  142. {
  143. codepage = CP_BIG5;
  144. }
  145. //else if (strstr(buf, "JISX0208") != NULL)
  146. //
  147. // codepage = CP_JISX0208;
  148. //}
  149. }
  150. else if (strncmp(buf, "ENCODING", 8) == 0)
  151. {
  152. dwEncoding = atoi(buf + 8);
  153. }
  154. else if (strncmp(buf, "BITMAP", 6) == 0)
  155. {
  156. state = 1;
  157. iCurHeight = 0;
  158. memset(bFontGlyph, 0, sizeof(bFontGlyph));
  159. }
  160. }
  161. else if (state == 1)
  162. {
  163. if (strncmp(buf, "ENDCHAR", 7) == 0)
  164. {
  165. //
  166. // Replace the original fonts
  167. //
  168. BYTE szCp[3];
  169. szCp[0] = (dwEncoding >> 8) & 0xFF;
  170. szCp[1] = dwEncoding & 0xFF;
  171. szCp[2] = 0;
  172. wchar_t wc[2] = { 0 };
  173. PAL_MultiByteToWideCharCP(codepage, (LPCSTR)szCp, 2, wc, 1);
  174. if (wc[0] != 0)
  175. {
  176. wchar_t w = (wc[0] >= unicode_upper_base) ? (wc[0] - unicode_upper_base + 0xd800) : wc[0];
  177. memcpy(unicode_font[w], bFontGlyph, sizeof(bFontGlyph));
  178. }
  179. state = 0;
  180. }
  181. else
  182. {
  183. if (iCurHeight < 16)
  184. {
  185. WORD wCode = strtoul(buf, NULL, 16);
  186. bFontGlyph[iCurHeight * 2] = (wCode >> 8);
  187. bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
  188. iCurHeight++;
  189. }
  190. }
  191. }
  192. }
  193. _font_height = 16;
  194. fclose(fp);
  195. return 0;
  196. }
  197. VOID
  198. PAL_FreeFont(
  199. VOID
  200. )
  201. /*++
  202. Purpose:
  203. None.
  204. Parameters:
  205. None.
  206. Return value:
  207. None.
  208. --*/
  209. {
  210. }
  211. VOID
  212. PAL_DrawCharOnSurface(
  213. WORD wChar,
  214. SDL_Surface *lpSurface,
  215. PAL_POS pos,
  216. BYTE bColor,
  217. BOOL fUse8x8Font
  218. )
  219. /*++
  220. Purpose:
  221. Draw a Unicode character on a surface.
  222. Parameters:
  223. [IN] wChar - the unicode character to be drawn.
  224. [OUT] lpSurface - the destination surface.
  225. [IN] pos - the destination location of the surface.
  226. [IN] bColor - the color of the character.
  227. Return value:
  228. None.
  229. --*/
  230. {
  231. int i, j;
  232. int x = PAL_X(pos), y = PAL_Y(pos);
  233. //
  234. // Check for NULL pointer & invalid char code.
  235. //
  236. if (lpSurface == NULL || (wChar >= 0xd800 && wChar < unicode_upper_base) ||
  237. wChar >= unicode_upper_top || (_font_height == 8 && wChar >= 0x100))
  238. {
  239. return;
  240. }
  241. //
  242. // Locate for this character in the font lib.
  243. //
  244. if (wChar >= unicode_upper_base)
  245. {
  246. wChar -= (unicode_upper_base - 0xd800);
  247. }
  248. //
  249. // Draw the character to the surface.
  250. //
  251. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  252. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  253. if (fUse8x8Font)
  254. {
  255. for (i = 0; i < 8 && dest < top; i++, dest += lpSurface->pitch)
  256. {
  257. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  258. {
  259. if (iso_font_8x8[wChar][i] & (1 << j))
  260. {
  261. dest[j] = bColor;
  262. }
  263. }
  264. }
  265. }
  266. else
  267. {
  268. if (font_width[wChar] == 32)
  269. {
  270. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  271. {
  272. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  273. {
  274. if (unicode_font[wChar][i] & (1 << (7 - j)))
  275. {
  276. dest[j] = bColor;
  277. }
  278. }
  279. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  280. {
  281. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  282. {
  283. dest[j + 8] = bColor;
  284. }
  285. }
  286. }
  287. }
  288. else
  289. {
  290. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  291. {
  292. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  293. {
  294. if (unicode_font[wChar][i] & (1 << (7 - j)))
  295. {
  296. dest[j] = bColor;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. INT
  304. PAL_CharWidth(
  305. WORD wChar
  306. )
  307. /*++
  308. Purpose:
  309. Get the text width of a character.
  310. Parameters:
  311. [IN] wChar - the unicode character for width calculation.
  312. Return value:
  313. The width of the character, 16 for full-width char and 8 for half-width char.
  314. --*/
  315. {
  316. if ((wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  317. {
  318. return 0;
  319. }
  320. //
  321. // Locate for this character in the font lib.
  322. //
  323. if (wChar >= unicode_upper_base)
  324. {
  325. wChar -= (unicode_upper_base - 0xd800);
  326. }
  327. return font_width[wChar] >> 1;
  328. }
  329. INT
  330. PAL_FontHeight(
  331. VOID
  332. )
  333. {
  334. return _font_height;
  335. }