font.c 8.0 KB

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