font.c 7.4 KB

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