font.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. for (i = 0; i < 0x80; i++)
  105. {
  106. memcpy(unicode_font[i], &iso_font[i * 15], 15);
  107. unicode_font[i][15] = 0;
  108. }
  109. _font_height = 15;
  110. return 0;
  111. }
  112. INT
  113. PAL_LoadBdfFont(
  114. LPCSTR pszBdfFileName
  115. )
  116. /*++
  117. Purpose:
  118. Loads a BDF bitmap font file.
  119. Parameters:
  120. [IN] pszBdfFileName - Name of BDF bitmap font file..
  121. Return value:
  122. 0 = success, -1 = failure.
  123. --*/
  124. {
  125. char buf[4096];
  126. int state = 0;
  127. int codepage = -1;
  128. DWORD dwEncoding = 0;
  129. BYTE bFontGlyph[32] = {0};
  130. int iCurHeight = 0;
  131. FILE *fp = UTIL_OpenFileForMode(pszBdfFileName, "r");
  132. if (fp == NULL)
  133. {
  134. return -1;
  135. }
  136. while (fgets(buf, 4096, fp) != NULL)
  137. {
  138. if (state == 0)
  139. {
  140. if (strncmp(buf, "CHARSET_REGISTRY", 16) == 0)
  141. {
  142. if (strstr(buf, "Big5") != NULL)
  143. {
  144. codepage = CP_BIG5;
  145. }
  146. else if (strstr(buf, "BIG5") != NULL)
  147. {
  148. codepage = CP_BIG5;
  149. }
  150. //else if (strstr(buf, "JISX0208") != NULL)
  151. //
  152. // codepage = CP_JISX0208;
  153. //}
  154. }
  155. else if (strncmp(buf, "ENCODING", 8) == 0)
  156. {
  157. dwEncoding = atoi(buf + 8);
  158. }
  159. else if (strncmp(buf, "BITMAP", 6) == 0)
  160. {
  161. state = 1;
  162. iCurHeight = 0;
  163. memset(bFontGlyph, 0, sizeof(bFontGlyph));
  164. }
  165. }
  166. else if (state == 1)
  167. {
  168. if (strncmp(buf, "ENDCHAR", 7) == 0)
  169. {
  170. //
  171. // Replace the original fonts
  172. //
  173. BYTE szCp[3];
  174. szCp[0] = (dwEncoding >> 8) & 0xFF;
  175. szCp[1] = dwEncoding & 0xFF;
  176. szCp[2] = 0;
  177. wchar_t wc[2] = { 0 };
  178. PAL_MultiByteToWideCharCP(codepage, (LPCSTR)szCp, 2, wc, 1);
  179. if (wc[0] != 0)
  180. {
  181. wchar_t w = (wc[0] >= unicode_upper_base) ? (wc[0] - unicode_upper_base + 0xd800) : wc[0];
  182. memcpy(unicode_font[w], bFontGlyph, sizeof(bFontGlyph));
  183. }
  184. state = 0;
  185. }
  186. else
  187. {
  188. if (iCurHeight < 16)
  189. {
  190. WORD wCode = strtoul(buf, NULL, 16);
  191. bFontGlyph[iCurHeight * 2] = (wCode >> 8);
  192. bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
  193. iCurHeight++;
  194. }
  195. }
  196. }
  197. }
  198. _font_height = 16;
  199. fclose(fp);
  200. return 0;
  201. }
  202. VOID
  203. PAL_FreeFont(
  204. VOID
  205. )
  206. /*++
  207. Purpose:
  208. None.
  209. Parameters:
  210. None.
  211. Return value:
  212. None.
  213. --*/
  214. {
  215. }
  216. VOID
  217. PAL_DrawCharOnSurface(
  218. WORD wChar,
  219. SDL_Surface *lpSurface,
  220. PAL_POS pos,
  221. BYTE bColor,
  222. BOOL fUse8x8Font
  223. )
  224. /*++
  225. Purpose:
  226. Draw a Unicode character on a surface.
  227. Parameters:
  228. [IN] wChar - the unicode character to be drawn.
  229. [OUT] lpSurface - the destination surface.
  230. [IN] pos - the destination location of the surface.
  231. [IN] bColor - the color of the character.
  232. Return value:
  233. None.
  234. --*/
  235. {
  236. int i, j;
  237. int x = PAL_X(pos), y = PAL_Y(pos);
  238. //
  239. // Check for NULL pointer & invalid char code.
  240. //
  241. if (lpSurface == NULL || (wChar >= 0xd800 && wChar < unicode_upper_base) ||
  242. wChar >= unicode_upper_top || (_font_height == 8 && wChar >= 0x100))
  243. {
  244. return;
  245. }
  246. //
  247. // Locate for this character in the font lib.
  248. //
  249. if (wChar >= unicode_upper_base)
  250. {
  251. wChar -= (unicode_upper_base - 0xd800);
  252. }
  253. //
  254. // Draw the character to the surface.
  255. //
  256. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  257. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  258. if (fUse8x8Font)
  259. {
  260. for (i = 0; i < 8 && dest < top; i++, dest += lpSurface->pitch)
  261. {
  262. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  263. {
  264. if (iso_font_8x8[wChar][i] & (1 << j))
  265. {
  266. dest[j] = bColor;
  267. }
  268. }
  269. }
  270. }
  271. else
  272. {
  273. if (font_width[wChar] == 32)
  274. {
  275. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  276. {
  277. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  278. {
  279. if (unicode_font[wChar][i] & (1 << (7 - j)))
  280. {
  281. dest[j] = bColor;
  282. }
  283. }
  284. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  285. {
  286. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  287. {
  288. dest[j + 8] = bColor;
  289. }
  290. }
  291. }
  292. }
  293. else
  294. {
  295. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  296. {
  297. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  298. {
  299. if (unicode_font[wChar][i] & (1 << (7 - j)))
  300. {
  301. dest[j] = bColor;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. INT
  309. PAL_CharWidth(
  310. WORD wChar
  311. )
  312. /*++
  313. Purpose:
  314. Get the text width of a character.
  315. Parameters:
  316. [IN] wChar - the unicode character for width calculation.
  317. Return value:
  318. The width of the character, 16 for full-width char and 8 for half-width char.
  319. --*/
  320. {
  321. if ((wChar >= 0xd800 && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  322. {
  323. return 0;
  324. }
  325. //
  326. // Locate for this character in the font lib.
  327. //
  328. if (wChar >= unicode_upper_base)
  329. {
  330. wChar -= (unicode_upper_base - 0xd800);
  331. }
  332. return font_width[wChar] >> 1;
  333. }
  334. INT
  335. PAL_FontHeight(
  336. VOID
  337. )
  338. {
  339. return _font_height;
  340. }