font.c 7.3 KB

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