font.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "font.h"
  23. #include "util.h"
  24. #include "text.h"
  25. #define _FONT_C
  26. #include "fontglyph.h"
  27. #include "fontglyph_cn.h"
  28. #include "fontglyph_tw.h"
  29. #include "fontglyph_jp.h"
  30. #include "ascii.h"
  31. static int _font_height = 16;
  32. static uint8_t reverseBits(uint8_t x) {
  33. uint8_t y = 0;
  34. for(int i = 0 ; i < 8; i++){
  35. y <<= 1;
  36. y |= (x & 1);
  37. x >>= 1;
  38. }
  39. return y;
  40. }
  41. static uint16_t reverseBits16(uint16_t x) {
  42. //specified to unifont structure; not common means
  43. uint8_t l=reverseBits(x);
  44. uint8_t h=reverseBits(x>>8);
  45. return h<<8|l;
  46. }
  47. #ifdef DEBUG
  48. void dump_font(BYTE *buf,int rows, int cols)
  49. {
  50. for(int row=0;row<rows;row++)
  51. {
  52. for( int bit=0;bit<cols;bit++)
  53. if( buf[row] & (uint8_t)pow(2,bit) )
  54. printf("*");
  55. else
  56. printf(" ");
  57. printf("\n");
  58. }
  59. }
  60. void dump_font16(WORD *buf,int rows, int cols)
  61. {
  62. for(int row=0;row<rows;row++)
  63. {
  64. for( int bit=0;bit<cols;bit++)
  65. if( reverseBits16(buf[row]) & (uint16_t)pow(2,bit) )
  66. printf("*");
  67. else
  68. printf(" ");
  69. printf("\n");
  70. }
  71. }
  72. #endif
  73. static void PAL_LoadISOFont(void)
  74. {
  75. int i, j;
  76. for (i = 0; i < sizeof(iso_font) / 15; i++)
  77. {
  78. for (j = 0; j < 15; j++)
  79. {
  80. unicode_font[i][j] = reverseBits(iso_font[i * 15 + j]);
  81. }
  82. unicode_font[i][15] = 0;
  83. font_width[i] = 16;
  84. }
  85. }
  86. static void PAL_LoadCNFont(void)
  87. {
  88. int i;
  89. for (i = 0; i < sizeof(fontglyph_cn) / sizeof(fontglyph_cn[0]); i++)
  90. {
  91. wchar_t w = fontglyph_cn[i].code;
  92. w = (w >= unicode_upper_base) ? (w - unicode_upper_base + unicode_lower_top) : w;
  93. if (w < sizeof(unicode_font) / sizeof(unicode_font[0]))
  94. {
  95. memcpy(unicode_font[w], fontglyph_cn[i].data, 32);
  96. font_width[w] = 32;
  97. }
  98. }
  99. }
  100. static void PAL_LoadTWFont(void)
  101. {
  102. int i;
  103. for (i = 0; i < sizeof(fontglyph_tw) / sizeof(fontglyph_tw[0]); i++)
  104. {
  105. wchar_t w = fontglyph_tw[i].code;
  106. w = (w >= unicode_upper_base) ? (w - unicode_upper_base + unicode_lower_top) : w;
  107. if (w < sizeof(unicode_font) / sizeof(unicode_font[0]))
  108. {
  109. memcpy(unicode_font[w], fontglyph_tw[i].data, 32);
  110. font_width[w] = 32;
  111. }
  112. }
  113. _font_height = 15;
  114. }
  115. static void PAL_LoadJPFont(void)
  116. {
  117. int i;
  118. for (i = 0; i < sizeof(fontglyph_jp) / sizeof(fontglyph_jp[0]); i++)
  119. {
  120. wchar_t w = fontglyph_jp[i].code;
  121. w = (w >= unicode_upper_base) ? (w - unicode_upper_base + unicode_lower_top) : w;
  122. if (w < sizeof(unicode_font) / sizeof(unicode_font[0]))
  123. {
  124. memcpy(unicode_font[w], fontglyph_jp[i].data, 32);
  125. font_width[w] = 32;
  126. }
  127. }
  128. _font_height = 16;
  129. }
  130. static void PAL_LoadEmbeddedFont(void)
  131. {
  132. FILE *fp;
  133. char *char_buf;
  134. wchar_t *wchar_buf;
  135. size_t nBytes;
  136. int nChars, i;
  137. //
  138. // Load the wor16.asc file.
  139. //
  140. if (NULL == (fp = UTIL_OpenFile("wor16.asc")))
  141. {
  142. return;
  143. }
  144. //
  145. // Get the size of wor16.asc file.
  146. //
  147. fseek(fp, 0, SEEK_END);
  148. nBytes = ftell(fp);
  149. //
  150. // Allocate buffer & read all the character codes.
  151. //
  152. if (NULL == (char_buf = (char *)malloc(nBytes)))
  153. {
  154. fclose(fp);
  155. return;
  156. }
  157. fseek(fp, 0, SEEK_SET);
  158. if (fread(char_buf, 1, nBytes, fp) < nBytes)
  159. {
  160. fclose(fp);
  161. return;
  162. }
  163. //
  164. // Close wor16.asc file.
  165. //
  166. fclose(fp);
  167. //
  168. // Detect the codepage of 'wor16.asc' and exit if not BIG5 or probability < 99
  169. // Note: 100% probability is impossible as the function does not recognize some special
  170. // characters such as bopomofo that may be used by 'wor16.asc'.
  171. //
  172. if (PAL_DetectCodePageForString(char_buf, nBytes, CP_BIG5, &i) != CP_BIG5 || i < 99)
  173. {
  174. free(char_buf);
  175. return;
  176. }
  177. //
  178. // Convert characters into unicode
  179. // Explictly specify BIG5 here for compatibility with codepage auto-detection
  180. //
  181. nChars = PAL_MultiByteToWideCharCP(CP_BIG5, char_buf, nBytes, NULL, 0);
  182. if (NULL == (wchar_buf = (wchar_t *)malloc(nChars * sizeof(wchar_t))))
  183. {
  184. free(char_buf);
  185. return;
  186. }
  187. PAL_MultiByteToWideCharCP(CP_BIG5, char_buf, nBytes, wchar_buf, nChars);
  188. free(char_buf);
  189. //
  190. // Read bitmaps from wor16.fon file.
  191. //
  192. fp = UTIL_OpenFile("wor16.fon");
  193. //
  194. // The font glyph data begins at offset 0x682 in wor16.fon.
  195. //
  196. fseek(fp, 0x682, SEEK_SET);
  197. //
  198. // Replace the original fonts
  199. //
  200. for (i = 0; i < nChars; i++)
  201. {
  202. wchar_t w = (wchar_buf[i] >= unicode_upper_base) ? (wchar_buf[i] - unicode_upper_base + unicode_lower_top) : wchar_buf[i];
  203. if (fread(unicode_font[w], 30, 1, fp) == 1)
  204. {
  205. unicode_font[w][30] = 0;
  206. unicode_font[w][31] = 0;
  207. }
  208. font_width[w] = 32;
  209. }
  210. free(wchar_buf);
  211. fclose(fp);
  212. _font_height = 15;
  213. }
  214. INT
  215. PAL_LoadUserFont(
  216. LPCSTR pszBdfFileName
  217. )
  218. /*++
  219. Purpose:
  220. Loads a BDF bitmap font file.
  221. Parameters:
  222. [IN] pszBdfFileName - Name of BDF bitmap font file..
  223. Return value:
  224. 0 = success, -1 = failure.
  225. --*/
  226. {
  227. char buf[4096];
  228. int state = 0, fstate = 0;
  229. int codepage = -1;
  230. DWORD dwEncoding = 0;
  231. BYTE bFontGlyph[32] = {0};
  232. int iCurHeight = 0;
  233. int bbw = 0, bbh = 0, bbox, bboy;
  234. FILE *fp = UTIL_OpenFileForMode(pszBdfFileName, "r");
  235. if (fp == NULL)
  236. {
  237. return -1;
  238. }
  239. _font_height = 16;
  240. while (fgets(buf, 4096, fp) != NULL)
  241. {
  242. if (state == 0)
  243. {
  244. if (strncmp(buf, "CHARSET_REGISTRY", 16) == 0)
  245. {
  246. if (strcasestr(buf, "Big5") != NULL)
  247. {
  248. codepage = CP_BIG5;
  249. }
  250. else if (strcasestr(buf, "GBK") != NULL || strcasestr(buf, "GB2312") != NULL)
  251. {
  252. codepage = CP_GBK;
  253. }
  254. else if (strcasestr(buf, "ISO10646") != NULL)
  255. {
  256. codepage = CP_UCS;
  257. }
  258. //else if (strstr(buf, "JISX0208") != NULL)
  259. //
  260. // codepage = CP_JISX0208;
  261. //}
  262. else
  263. {
  264. TerminateOnError("Charset of %s is %s, which is not a supported yet.",pszBdfFileName,buf);
  265. }
  266. }
  267. else if (strncmp(buf, "ENCODING", 8) == 0)
  268. {
  269. dwEncoding = atoi(buf + 8);
  270. }
  271. else if (strncmp(buf, "SIZE", 3) == 0)
  272. {
  273. int bytes_consumed = 0, bytes_now;
  274. int got_size;
  275. BOOL got_expected = FALSE;
  276. char size[10];
  277. sscanf(buf + bytes_consumed, "%s%n", size, &bytes_now);
  278. bytes_consumed += bytes_now;
  279. while (sscanf(buf + bytes_consumed, "%d%n", &got_size, &bytes_now))
  280. {
  281. bytes_consumed += bytes_now;
  282. if (got_size == 16 || got_size == 15)
  283. {
  284. _font_height = got_size;
  285. got_expected = TRUE;
  286. }
  287. }
  288. if (!got_expected)
  289. TerminateOnError("%s not contains expected font size 15/16!", pszBdfFileName);
  290. }
  291. else if (strncmp(buf, "BBX", 3) == 0)
  292. {
  293. int bytes_consumed = 0, bytes_now;
  294. char bbx[10];
  295. sscanf(buf+bytes_consumed,"%s%n",bbx,&bytes_now);bytes_consumed += bytes_now;
  296. sscanf(buf+bytes_consumed,"%d%n",&bbw,&bytes_now);bytes_consumed += bytes_now;
  297. sscanf(buf+bytes_consumed,"%d%n",&bbh,&bytes_now);bytes_consumed += bytes_now;
  298. sscanf(buf+bytes_consumed,"%d%n",&bbox,&bytes_now);bytes_consumed += bytes_now;
  299. sscanf(buf+bytes_consumed,"%d%n",&bboy,&bytes_now);bytes_consumed += bytes_now;
  300. }
  301. else if (strncmp(buf, "BITMAP", 6) == 0)
  302. {
  303. state = 1;
  304. iCurHeight = 0;
  305. for(iCurHeight=0;iCurHeight<bboy;iCurHeight++){
  306. bFontGlyph[iCurHeight * 2] = 0;
  307. bFontGlyph[iCurHeight * 2 + 1] = 0;
  308. }
  309. memset(bFontGlyph, 0, sizeof(bFontGlyph));
  310. }
  311. }
  312. else if (state == 1)
  313. {
  314. if (strncmp(buf, "ENDCHAR", 7) == 0)
  315. {
  316. //
  317. // Replace the original fonts
  318. //
  319. BYTE szCp[3];
  320. szCp[0] = (dwEncoding >> 8) & 0xFF;
  321. szCp[1] = dwEncoding & 0xFF;
  322. szCp[2] = 0;
  323. if (codepage == CP_GBK && dwEncoding > 0xFF)
  324. {
  325. szCp[0] |= 0x80;
  326. szCp[1] |= 0x80;
  327. }
  328. wchar_t wc[2] = { 0 };
  329. PAL_MultiByteToWideCharCP(codepage, (LPCSTR)szCp, 2, wc, 1);
  330. if (wc[0] != 0)
  331. {
  332. wchar_t w = (wc[0] >= unicode_upper_base) ? (wc[0] - unicode_upper_base + unicode_lower_top) : wc[0];
  333. if (w < sizeof(unicode_font) / sizeof(unicode_font[0]))
  334. {
  335. memcpy(unicode_font[w], bFontGlyph, sizeof(bFontGlyph));
  336. }
  337. }
  338. state = 0;
  339. }
  340. else
  341. {
  342. if (iCurHeight < bbh )
  343. {
  344. WORD wCode = strtoul(buf, NULL, 16);
  345. if(bbw <= 8)
  346. {
  347. switch(fstate)
  348. {
  349. case 0:
  350. bFontGlyph[iCurHeight * 2] = wCode;
  351. fstate = 1;
  352. break;
  353. case 1:
  354. bFontGlyph[iCurHeight * 2+1] = wCode;
  355. fstate = 0;
  356. iCurHeight++;
  357. break;
  358. }
  359. }else{
  360. bFontGlyph[iCurHeight * 2] = (wCode >> 8);
  361. bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
  362. iCurHeight++;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. fclose(fp);
  369. return 0;
  370. }
  371. int
  372. PAL_InitFont(
  373. const CONFIGURATION* cfg
  374. )
  375. {
  376. if (!cfg->pszMsgFile)
  377. {
  378. PAL_LoadEmbeddedFont();
  379. }
  380. if (g_TextLib.fUseISOFont)
  381. {
  382. PAL_LoadISOFont();
  383. }
  384. if (cfg->pszFontFile)
  385. {
  386. PAL_LoadUserFont(cfg->pszFontFile);
  387. }
  388. else
  389. {
  390. switch (g_TextLib.iFontFlavor)
  391. {
  392. case kFontFlavorAuto:
  393. switch (PAL_GetCodePage())
  394. {
  395. case CP_GBK:
  396. PAL_LoadCNFont();
  397. break;
  398. case CP_BIG5:
  399. PAL_LoadTWFont();
  400. break;
  401. default:
  402. break;
  403. }
  404. break;
  405. case kFontFlavorSimpChin:
  406. PAL_LoadCNFont();
  407. break;
  408. case kFontFlavorTradChin:
  409. PAL_LoadTWFont();
  410. break;
  411. case kFontFlavorJapanese:
  412. PAL_LoadJPFont();
  413. break;
  414. case kFontFlavorUnifont:
  415. default:
  416. break;
  417. }
  418. }
  419. return 0;
  420. }
  421. void
  422. PAL_FreeFont(
  423. void
  424. )
  425. {
  426. }
  427. void
  428. PAL_DrawCharOnSurface(
  429. uint16_t wChar,
  430. SDL_Surface *lpSurface,
  431. PAL_POS pos,
  432. uint8_t bColor,
  433. BOOL fUse8x8Font
  434. )
  435. {
  436. int i, j;
  437. int x = PAL_X(pos), y = PAL_Y(pos);
  438. //
  439. // Check for NULL pointer & invalid char code.
  440. //
  441. if (lpSurface == NULL || (wChar >= unicode_lower_top && wChar < unicode_upper_base) ||
  442. wChar >= unicode_upper_top || (_font_height == 8 && wChar >= 0x100))
  443. {
  444. return;
  445. }
  446. //
  447. // Locate for this character in the font lib.
  448. //
  449. if (wChar >= unicode_upper_base)
  450. {
  451. wChar -= (unicode_upper_base - unicode_lower_top);
  452. }
  453. //
  454. // Draw the character to the surface.
  455. //
  456. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  457. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  458. if (fUse8x8Font)
  459. {
  460. for (i = 0; i < 8 && dest < top; i++, dest += lpSurface->pitch)
  461. {
  462. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  463. {
  464. if (iso_font_8x8[wChar][i] & (1 << j))
  465. {
  466. dest[j] = bColor;
  467. }
  468. }
  469. }
  470. }
  471. else
  472. {
  473. if (font_width[wChar] == 32)
  474. {
  475. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  476. {
  477. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  478. {
  479. if (unicode_font[wChar][i] & (1 << (7 - j)))
  480. {
  481. dest[j] = bColor;
  482. }
  483. }
  484. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  485. {
  486. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  487. {
  488. dest[j + 8] = bColor;
  489. }
  490. }
  491. }
  492. }
  493. else
  494. {
  495. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  496. {
  497. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  498. {
  499. if (unicode_font[wChar][i] & (1 << (7 - j)))
  500. {
  501. dest[j] = bColor;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. int
  509. PAL_CharWidth(
  510. uint16_t wChar
  511. )
  512. {
  513. if ((wChar >= unicode_lower_top && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  514. {
  515. return 0;
  516. }
  517. //
  518. // Locate for this character in the font lib.
  519. //
  520. if (wChar >= unicode_upper_base)
  521. {
  522. wChar -= (unicode_upper_base - unicode_lower_top);
  523. }
  524. return font_width[wChar] >> 1;
  525. }
  526. int
  527. PAL_FontHeight(
  528. void
  529. )
  530. {
  531. return _font_height;
  532. }