font.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 "ascii.h"
  28. static int _font_height = 16;
  29. static uint8_t reverseBits(uint8_t x) {
  30. uint8_t y = 0;
  31. for(int i = 0 ; i < 8; i++){
  32. y <<= 1;
  33. y |= (x & 1);
  34. x >>= 1;
  35. }
  36. return y;
  37. }
  38. static uint16_t reverseBits16(uint16_t x) {
  39. //specified to unifont structure; not common means
  40. uint8_t l=reverseBits(x);
  41. uint8_t h=reverseBits(x>>8);
  42. return h<<8|l;
  43. }
  44. #ifdef DEBUG
  45. void dump_font(BYTE *buf,int rows, int cols)
  46. {
  47. for(int row=0;row<rows;row++)
  48. {
  49. for( int bit=0;bit<cols;bit++)
  50. if( buf[row] & (uint8_t)pow(2,bit) )
  51. printf("*");
  52. else
  53. printf(" ");
  54. printf("\n");
  55. }
  56. }
  57. void dump_font16(WORD *buf,int rows, int cols)
  58. {
  59. for(int row=0;row<rows;row++)
  60. {
  61. for( int bit=0;bit<cols;bit++)
  62. if( reverseBits16(buf[row]) & (uint16_t)pow(2,bit) )
  63. printf("*");
  64. else
  65. printf(" ");
  66. printf("\n");
  67. }
  68. }
  69. #endif
  70. static void PAL_LoadISOFont(void)
  71. {
  72. int i, j;
  73. for (i = 0; i < sizeof(iso_font) / 15; i++)
  74. {
  75. for (j = 0; j < 15; j++)
  76. {
  77. unicode_font[i][j] = reverseBits(iso_font[i * 15 + j]);
  78. }
  79. unicode_font[i][15] = 0;
  80. font_width[i] = 16;
  81. }
  82. }
  83. static void PAL_LoadEmbeddedFont(void)
  84. {
  85. FILE *fp;
  86. char *char_buf;
  87. wchar_t *wchar_buf;
  88. size_t nBytes;
  89. int nChars, i;
  90. //
  91. // Load the wor16.asc file.
  92. //
  93. if (NULL == (fp = UTIL_OpenFile("wor16.asc")))
  94. {
  95. return;
  96. }
  97. //
  98. // Get the size of wor16.asc file.
  99. //
  100. fseek(fp, 0, SEEK_END);
  101. nBytes = ftell(fp);
  102. //
  103. // Allocate buffer & read all the character codes.
  104. //
  105. if (NULL == (char_buf = (char *)malloc(nBytes)))
  106. {
  107. fclose(fp);
  108. return;
  109. }
  110. fseek(fp, 0, SEEK_SET);
  111. if (fread(char_buf, 1, nBytes, fp) < nBytes)
  112. {
  113. fclose(fp);
  114. return;
  115. }
  116. //
  117. // Close wor16.asc file.
  118. //
  119. fclose(fp);
  120. //
  121. // Detect the codepage of 'wor16.asc' and exit if not BIG5 or probability < 99
  122. // Note: 100% probability is impossible as the function does not recognize some special
  123. // characters such as bopomofo that may be used by 'wor16.asc'.
  124. //
  125. if (PAL_DetectCodePageForString(char_buf, nBytes, CP_BIG5, &i) != CP_BIG5 || i < 99)
  126. {
  127. free(char_buf);
  128. return;
  129. }
  130. //
  131. // Convert characters into unicode
  132. // Explictly specify BIG5 here for compatibility with codepage auto-detection
  133. //
  134. nChars = PAL_MultiByteToWideCharCP(CP_BIG5, char_buf, nBytes, NULL, 0);
  135. if (NULL == (wchar_buf = (wchar_t *)malloc(nChars * sizeof(wchar_t))))
  136. {
  137. free(char_buf);
  138. return;
  139. }
  140. PAL_MultiByteToWideCharCP(CP_BIG5, char_buf, nBytes, wchar_buf, nChars);
  141. free(char_buf);
  142. //
  143. // Read bitmaps from wor16.fon file.
  144. //
  145. fp = UTIL_OpenFile("wor16.fon");
  146. //
  147. // The font glyph data begins at offset 0x682 in wor16.fon.
  148. //
  149. fseek(fp, 0x682, SEEK_SET);
  150. //
  151. // Replace the original fonts
  152. //
  153. for (i = 0; i < nChars; i++)
  154. {
  155. wchar_t w = (wchar_buf[i] >= unicode_upper_base) ? (wchar_buf[i] - unicode_upper_base + unicode_lower_top) : wchar_buf[i];
  156. if (fread(unicode_font[w], 30, 1, fp) == 1)
  157. {
  158. unicode_font[w][30] = 0;
  159. unicode_font[w][31] = 0;
  160. }
  161. font_width[w] = 32;
  162. }
  163. free(wchar_buf);
  164. fclose(fp);
  165. _font_height = 15;
  166. }
  167. INT
  168. PAL_LoadUserFont(
  169. LPCSTR pszBdfFileName
  170. )
  171. /*++
  172. Purpose:
  173. Loads a BDF bitmap font file.
  174. Parameters:
  175. [IN] pszBdfFileName - Name of BDF bitmap font file..
  176. Return value:
  177. 0 = success, -1 = failure.
  178. --*/
  179. {
  180. char buf[4096];
  181. int state = 0, fstate = 0;
  182. int codepage = -1;
  183. DWORD dwEncoding = 0;
  184. BYTE bFontGlyph[32] = {0};
  185. int iCurHeight = 0;
  186. int bbw = 0, bbh = 0, bbox, bboy;
  187. FILE *fp = UTIL_OpenFileForMode(pszBdfFileName, "r");
  188. if (fp == NULL)
  189. {
  190. return -1;
  191. }
  192. _font_height = 16;
  193. while (fgets(buf, 4096, fp) != NULL)
  194. {
  195. if (state == 0)
  196. {
  197. if (strncmp(buf, "CHARSET_REGISTRY", 16) == 0)
  198. {
  199. if (strcasestr(buf, "Big5") != NULL)
  200. {
  201. codepage = CP_BIG5;
  202. }
  203. else if (strcasestr(buf, "GBK") != NULL || strcasestr(buf, "GB2312") != NULL)
  204. {
  205. codepage = CP_GBK;
  206. }
  207. else if (strcasestr(buf, "ISO10646") != NULL)
  208. {
  209. codepage = CP_UCS;
  210. }
  211. //else if (strstr(buf, "JISX0208") != NULL)
  212. //
  213. // codepage = CP_JISX0208;
  214. //}
  215. else
  216. {
  217. TerminateOnError("Charset of %s is %s, which is not a supported yet.",pszBdfFileName,buf);
  218. }
  219. }
  220. else if (strncmp(buf, "ENCODING", 8) == 0)
  221. {
  222. dwEncoding = atoi(buf + 8);
  223. }
  224. else if (strncmp(buf, "SIZE", 3) == 0)
  225. {
  226. int bytes_consumed = 0, bytes_now;
  227. int got_size;
  228. BOOL got_expected = FALSE;
  229. char size[10];
  230. sscanf(buf + bytes_consumed, "%s%n", size, &bytes_now);
  231. bytes_consumed += bytes_now;
  232. while (sscanf(buf + bytes_consumed, "%d%n", &got_size, &bytes_now))
  233. {
  234. bytes_consumed += bytes_now;
  235. if (got_size == 16 || got_size == 15)
  236. {
  237. _font_height = got_size;
  238. got_expected = TRUE;
  239. }
  240. }
  241. if (!got_expected)
  242. TerminateOnError("%s not contains expected font size 15/16!", pszBdfFileName);
  243. }
  244. else if (strncmp(buf, "BBX", 3) == 0)
  245. {
  246. int bytes_consumed = 0, bytes_now;
  247. char bbx[10];
  248. sscanf(buf+bytes_consumed,"%s%n",bbx,&bytes_now);bytes_consumed += bytes_now;
  249. sscanf(buf+bytes_consumed,"%d%n",&bbw,&bytes_now);bytes_consumed += bytes_now;
  250. sscanf(buf+bytes_consumed,"%d%n",&bbh,&bytes_now);bytes_consumed += bytes_now;
  251. sscanf(buf+bytes_consumed,"%d%n",&bbox,&bytes_now);bytes_consumed += bytes_now;
  252. sscanf(buf+bytes_consumed,"%d%n",&bboy,&bytes_now);bytes_consumed += bytes_now;
  253. }
  254. else if (strncmp(buf, "BITMAP", 6) == 0)
  255. {
  256. state = 1;
  257. iCurHeight = 0;
  258. for(iCurHeight=0;iCurHeight<bboy;iCurHeight++){
  259. bFontGlyph[iCurHeight * 2] = 0;
  260. bFontGlyph[iCurHeight * 2 + 1] = 0;
  261. }
  262. memset(bFontGlyph, 0, sizeof(bFontGlyph));
  263. }
  264. }
  265. else if (state == 1)
  266. {
  267. if (strncmp(buf, "ENDCHAR", 7) == 0)
  268. {
  269. //
  270. // Replace the original fonts
  271. //
  272. BYTE szCp[3];
  273. szCp[0] = (dwEncoding >> 8) & 0xFF;
  274. szCp[1] = dwEncoding & 0xFF;
  275. szCp[2] = 0;
  276. if (codepage == CP_GBK && dwEncoding > 0xFF)
  277. {
  278. szCp[0] |= 0x80;
  279. szCp[1] |= 0x80;
  280. }
  281. wchar_t wc[2] = { 0 };
  282. PAL_MultiByteToWideCharCP(codepage, (LPCSTR)szCp, 2, wc, 1);
  283. if (wc[0] != 0)
  284. {
  285. wchar_t w = (wc[0] >= unicode_upper_base) ? (wc[0] - unicode_upper_base + unicode_lower_top) : wc[0];
  286. if (w < sizeof(unicode_font) / sizeof(unicode_font[0]))
  287. {
  288. memcpy(unicode_font[w], bFontGlyph, sizeof(bFontGlyph));
  289. }
  290. }
  291. state = 0;
  292. }
  293. else
  294. {
  295. if (iCurHeight < bbh )
  296. {
  297. WORD wCode = strtoul(buf, NULL, 16);
  298. if(bbw <= 8)
  299. {
  300. switch(fstate)
  301. {
  302. case 0:
  303. bFontGlyph[iCurHeight * 2] = wCode;
  304. fstate = 1;
  305. break;
  306. case 1:
  307. bFontGlyph[iCurHeight * 2+1] = wCode;
  308. fstate = 0;
  309. iCurHeight++;
  310. break;
  311. }
  312. }else{
  313. bFontGlyph[iCurHeight * 2] = (wCode >> 8);
  314. bFontGlyph[iCurHeight * 2 + 1] = (wCode & 0xFF);
  315. iCurHeight++;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. fclose(fp);
  322. return 0;
  323. }
  324. int
  325. PAL_InitFont(
  326. const CONFIGURATION* cfg
  327. )
  328. {
  329. if (!cfg->pszMsgFile)
  330. {
  331. PAL_LoadEmbeddedFont();
  332. }
  333. if (g_TextLib.fUseISOFont)
  334. {
  335. PAL_LoadISOFont();
  336. }
  337. if (cfg->pszFontFile)
  338. {
  339. PAL_LoadUserFont(cfg->pszFontFile);
  340. }
  341. return 0;
  342. }
  343. void
  344. PAL_FreeFont(
  345. void
  346. )
  347. {
  348. }
  349. void
  350. PAL_DrawCharOnSurface(
  351. uint16_t wChar,
  352. SDL_Surface *lpSurface,
  353. PAL_POS pos,
  354. uint8_t bColor,
  355. BOOL fUse8x8Font
  356. )
  357. {
  358. int i, j;
  359. int x = PAL_X(pos), y = PAL_Y(pos);
  360. //
  361. // Check for NULL pointer & invalid char code.
  362. //
  363. if (lpSurface == NULL || (wChar >= unicode_lower_top && wChar < unicode_upper_base) ||
  364. wChar >= unicode_upper_top || (_font_height == 8 && wChar >= 0x100))
  365. {
  366. return;
  367. }
  368. //
  369. // Locate for this character in the font lib.
  370. //
  371. if (wChar >= unicode_upper_base)
  372. {
  373. wChar -= (unicode_upper_base - unicode_lower_top);
  374. }
  375. //
  376. // Draw the character to the surface.
  377. //
  378. LPBYTE dest = (LPBYTE)lpSurface->pixels + y * lpSurface->pitch + x;
  379. LPBYTE top = (LPBYTE)lpSurface->pixels + lpSurface->h * lpSurface->pitch;
  380. if (fUse8x8Font)
  381. {
  382. for (i = 0; i < 8 && dest < top; i++, dest += lpSurface->pitch)
  383. {
  384. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  385. {
  386. if (iso_font_8x8[wChar][i] & (1 << j))
  387. {
  388. dest[j] = bColor;
  389. }
  390. }
  391. }
  392. }
  393. else
  394. {
  395. if (font_width[wChar] == 32)
  396. {
  397. for (i = 0; i < _font_height * 2 && dest < top; i += 2, dest += lpSurface->pitch)
  398. {
  399. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  400. {
  401. if (unicode_font[wChar][i] & (1 << (7 - j)))
  402. {
  403. dest[j] = bColor;
  404. }
  405. }
  406. for (j = 0; j < 8 && x + j + 8 < lpSurface->w; j++)
  407. {
  408. if (unicode_font[wChar][i + 1] & (1 << (7 - j)))
  409. {
  410. dest[j + 8] = bColor;
  411. }
  412. }
  413. }
  414. }
  415. else
  416. {
  417. for (i = 0; i < _font_height && dest < top; i++, dest += lpSurface->pitch)
  418. {
  419. for (j = 0; j < 8 && x + j < lpSurface->w; j++)
  420. {
  421. if (unicode_font[wChar][i] & (1 << (7 - j)))
  422. {
  423. dest[j] = bColor;
  424. }
  425. }
  426. }
  427. }
  428. }
  429. }
  430. int
  431. PAL_CharWidth(
  432. uint16_t wChar
  433. )
  434. {
  435. if ((wChar >= unicode_lower_top && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
  436. {
  437. return 0;
  438. }
  439. //
  440. // Locate for this character in the font lib.
  441. //
  442. if (wChar >= unicode_upper_base)
  443. {
  444. wChar -= (unicode_upper_base - unicode_lower_top);
  445. }
  446. return font_width[wChar] >> 1;
  447. }
  448. int
  449. PAL_FontHeight(
  450. void
  451. )
  452. {
  453. return _font_height;
  454. }