font.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. #include "font.h"
  22. #include "ascii.h"
  23. #include "util.h"
  24. #ifdef PAL_WIN95
  25. /*
  26. * Portions based on:
  27. *
  28. * YH - Console Chinese Environment -
  29. * Copyright (C) 1999 Red Flag Linux (office@sonata.iscas.ac.cn)
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. * 1. Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * 2. Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY RED FLAG LINUX ``AS IS'' AND ANY
  41. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. */
  53. /*
  54. * Portions based on:
  55. *
  56. * KON2 - Kanji ON Console -
  57. * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
  58. *
  59. * CCE - Console Chinese Environment -
  60. * Copyright (C) 1998-1999 Rui He (herui@cs.duke.edu)
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions
  64. * are met:
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in the
  69. * documentation and/or other materials provided with the distribution.
  70. *
  71. * THIS SOFTWARE IS PROVIDED BY TAKASHI MANABE ``AS IS'' AND ANY
  72. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74. * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
  75. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  76. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  77. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  78. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  79. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  80. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  81. * SUCH DAMAGE.
  82. *
  83. */
  84. #include "gbfont.h"
  85. #include "big5font.h"
  86. BOOL fIsBig5 = FALSE;
  87. INT
  88. PAL_InitFont(
  89. VOID
  90. )
  91. /*++
  92. Purpose:
  93. Load the font files.
  94. Parameters:
  95. None.
  96. Return value:
  97. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  98. --*/
  99. {
  100. FILE *fp;
  101. fp = fopen(PAL_PREFIX "word.dat", "rb");
  102. if (!fp)
  103. {
  104. return 0;
  105. }
  106. fseek(fp, 0x1E, SEEK_SET);
  107. if (fgetc(fp) == 0xAA)
  108. {
  109. fIsBig5 = TRUE;
  110. }
  111. fclose(fp);
  112. return 0;
  113. }
  114. VOID
  115. PAL_FreeFont(
  116. VOID
  117. )
  118. /*++
  119. Purpose:
  120. Free the memory used for fonts.
  121. Parameters:
  122. None.
  123. Return value:
  124. None.
  125. --*/
  126. {
  127. }
  128. static BOOL is_gb(unsigned char b1, unsigned char b2)
  129. {
  130. if (b1 < 0xa1 || b1 > 0xfe)
  131. return FALSE;
  132. if (b2 < 0xa1 || b2 > 0xfe)
  133. return FALSE;
  134. return TRUE;
  135. }
  136. VOID
  137. PAL_DrawCharOnSurface(
  138. WORD wChar,
  139. SDL_Surface *lpSurface,
  140. PAL_POS pos,
  141. BYTE bColor
  142. )
  143. /*++
  144. Purpose:
  145. Draw a BIG-5 Chinese character on a surface.
  146. Parameters:
  147. [IN] wChar - the character to be drawn (in GB2312/BIG5).
  148. [OUT] lpSurface - the destination surface.
  149. [IN] pos - the destination location of the surface.
  150. [IN] bColor - the color of the character.
  151. Return value:
  152. None.
  153. --*/
  154. {
  155. int i, j, dx;
  156. int x = PAL_X(pos), y = PAL_Y(pos);
  157. LPBYTE pChar;
  158. BYTE ch1, ch2;
  159. //
  160. // Check for NULL pointer.
  161. //
  162. if (lpSurface == NULL)
  163. {
  164. return;
  165. }
  166. //
  167. // Locate for this character in the font lib.
  168. //
  169. ch1 = wChar & 0xff;
  170. ch2 = wChar >> 8;
  171. if (fIsBig5)
  172. {
  173. if (ch2 < 0xa1)
  174. pChar = &big5font[((ch1 - 0xA1) * 157 + ch2 - 0x40) << 5] + 8;
  175. else
  176. pChar = &big5font[((ch1 - 0xA1) * 157 + 63 + ch2 - 0xA1) << 5] + 8;
  177. }
  178. else
  179. {
  180. if (!is_gb(ch1, ch2))
  181. {
  182. return;
  183. }
  184. pChar = &gbfont[((ch1 - 0xa1) * 94 + (ch2 - 0xa1)) * 32];
  185. }
  186. if (pChar == NULL) return;
  187. //
  188. // Draw the character to the surface.
  189. //
  190. if (y >= lpSurface->h) return;
  191. y *= lpSurface->pitch;
  192. for (i = 0; i < 32; i++)
  193. {
  194. dx = x + ((i & 1) << 3);
  195. for (j = 0; j < 8; j++)
  196. {
  197. if (pChar[i] & (1 << (7 - j)))
  198. {
  199. if (dx < lpSurface->w)
  200. {
  201. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  202. }
  203. else
  204. {
  205. break;
  206. }
  207. }
  208. dx++;
  209. }
  210. y += (i & 1) * lpSurface->pitch;
  211. if (y / lpSurface->pitch >= lpSurface->h)
  212. {
  213. break;
  214. }
  215. }
  216. }
  217. VOID
  218. PAL_DrawASCIICharOnSurface(
  219. BYTE bChar,
  220. SDL_Surface *lpSurface,
  221. PAL_POS pos,
  222. BYTE bColor
  223. )
  224. /*++
  225. Purpose:
  226. Draw a ASCII character on a surface.
  227. Parameters:
  228. [IN] bChar - the character to be drawn.
  229. [OUT] lpSurface - the destination surface.
  230. [IN] pos - the destination location of the surface.
  231. [IN] bColor - the color of the character.
  232. Return value:
  233. None.
  234. --*/
  235. {
  236. int i, j, dx;
  237. int x = PAL_X(pos), y = PAL_Y(pos);
  238. LPBYTE pChar;
  239. pChar = &iso_font[(int)(bChar & ~128) * 15];
  240. //
  241. // Check for NULL pointer.
  242. //
  243. if (lpSurface == NULL)
  244. {
  245. return;
  246. }
  247. //
  248. // Draw the character to the surface.
  249. //
  250. y *= lpSurface->pitch;
  251. for (i = 0; i < 15; i++)
  252. {
  253. dx = x;
  254. for (j = 0; j < 8; j++)
  255. {
  256. if (pChar[i] & (1 << j))
  257. {
  258. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  259. }
  260. dx++;
  261. }
  262. y += lpSurface->pitch;
  263. }
  264. }
  265. #else
  266. typedef struct tagFont
  267. {
  268. LPWORD lpBufChar;
  269. LPBYTE lpBufGlyph;
  270. INT nChar;
  271. } FONT, *LPFONT;
  272. static LPFONT gpFont = NULL;
  273. INT
  274. PAL_InitFont(
  275. VOID
  276. )
  277. /*++
  278. Purpose:
  279. Load the font files.
  280. Parameters:
  281. None.
  282. Return value:
  283. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  284. --*/
  285. {
  286. FILE *fp;
  287. if (gpFont != NULL)
  288. {
  289. //
  290. // Already initialized
  291. //
  292. return 0;
  293. }
  294. gpFont = (LPFONT)calloc(1, sizeof(FONT));
  295. if (gpFont == NULL)
  296. {
  297. return -1;
  298. }
  299. //
  300. // Load the wor16.asc file.
  301. //
  302. fp = UTIL_OpenRequiredFile("wor16.asc");
  303. //
  304. // Get the size of wor16.asc file.
  305. //
  306. fseek(fp, 0, SEEK_END);
  307. gpFont->nChar = ftell(fp);
  308. gpFont->nChar /= 2;
  309. //
  310. // Read all the character codes.
  311. //
  312. gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
  313. if (gpFont->lpBufChar == NULL)
  314. {
  315. free(gpFont);
  316. gpFont = NULL;
  317. return -1;
  318. }
  319. fseek(fp, 0, SEEK_SET);
  320. fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
  321. //
  322. // Close wor16.asc file.
  323. //
  324. fclose(fp);
  325. //
  326. // Read all bitmaps from wor16.fon file.
  327. //
  328. fp = UTIL_OpenRequiredFile("wor16.fon");
  329. gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
  330. if (gpFont->lpBufGlyph == NULL)
  331. {
  332. free(gpFont->lpBufChar);
  333. free(gpFont);
  334. gpFont = NULL;
  335. return -1;
  336. }
  337. //
  338. // The font glyph data begins at offset 0x682 in wor16.fon.
  339. //
  340. fseek(fp, 0x682, SEEK_SET);
  341. fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
  342. fclose(fp);
  343. return 0;
  344. }
  345. VOID
  346. PAL_FreeFont(
  347. VOID
  348. )
  349. /*++
  350. Purpose:
  351. Free the memory used for fonts.
  352. Parameters:
  353. None.
  354. Return value:
  355. None.
  356. --*/
  357. {
  358. if (gpFont != NULL)
  359. {
  360. free(gpFont->lpBufChar);
  361. free(gpFont->lpBufGlyph);
  362. free(gpFont);
  363. }
  364. gpFont = NULL;
  365. }
  366. VOID
  367. PAL_DrawCharOnSurface(
  368. WORD wChar,
  369. SDL_Surface *lpSurface,
  370. PAL_POS pos,
  371. BYTE bColor
  372. )
  373. /*++
  374. Purpose:
  375. Draw a BIG-5 Chinese character on a surface.
  376. Parameters:
  377. [IN] wChar - the character to be drawn (in BIG-5).
  378. [OUT] lpSurface - the destination surface.
  379. [IN] pos - the destination location of the surface.
  380. [IN] bColor - the color of the character.
  381. Return value:
  382. None.
  383. --*/
  384. {
  385. int i, j, dx;
  386. int x = PAL_X(pos), y = PAL_Y(pos);
  387. LPBYTE pChar;
  388. //
  389. // Check for NULL pointer.
  390. //
  391. if (lpSurface == NULL || gpFont == NULL)
  392. {
  393. return;
  394. }
  395. //
  396. // Locate for this character in the font lib.
  397. //
  398. for (i = 0; i < gpFont->nChar; i++)
  399. {
  400. if (gpFont->lpBufChar[i] == wChar)
  401. {
  402. break;
  403. }
  404. }
  405. if (i >= gpFont->nChar)
  406. {
  407. //
  408. // This character does not exist in the font lib.
  409. //
  410. return;
  411. }
  412. pChar = gpFont->lpBufGlyph + i * 30;
  413. //
  414. // Draw the character to the surface.
  415. //
  416. y *= lpSurface->pitch;
  417. for (i = 0; i < 30; i++)
  418. {
  419. dx = x + ((i & 1) << 3);
  420. for (j = 0; j < 8; j++)
  421. {
  422. if (pChar[i] & (1 << (7 - j)))
  423. {
  424. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  425. }
  426. dx++;
  427. }
  428. y += (i & 1) * lpSurface->pitch;
  429. }
  430. }
  431. VOID
  432. PAL_DrawASCIICharOnSurface(
  433. BYTE bChar,
  434. SDL_Surface *lpSurface,
  435. PAL_POS pos,
  436. BYTE bColor
  437. )
  438. /*++
  439. Purpose:
  440. Draw a ASCII character on a surface.
  441. Parameters:
  442. [IN] bChar - the character to be drawn.
  443. [OUT] lpSurface - the destination surface.
  444. [IN] pos - the destination location of the surface.
  445. [IN] bColor - the color of the character.
  446. Return value:
  447. None.
  448. --*/
  449. {
  450. int i, j, dx;
  451. int x = PAL_X(pos), y = PAL_Y(pos);
  452. LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
  453. //
  454. // Check for NULL pointer.
  455. //
  456. if (lpSurface == NULL)
  457. {
  458. return;
  459. }
  460. //
  461. // Draw the character to the surface.
  462. //
  463. y *= lpSurface->pitch;
  464. for (i = 0; i < 15; i++)
  465. {
  466. dx = x;
  467. for (j = 0; j < 8; j++)
  468. {
  469. if (pChar[i] & (1 << j))
  470. {
  471. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  472. }
  473. dx++;
  474. }
  475. y += lpSurface->pitch;
  476. }
  477. }
  478. #endif