font.c 7.8 KB

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