font.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. if (y >= lpSurface->h) return;
  251. y *= lpSurface->pitch;
  252. for (i = 0; i < 15; i++)
  253. {
  254. dx = x;
  255. for (j = 0; j < 8; j++)
  256. {
  257. if (pChar[i] & (1 << j))
  258. {
  259. if (dx < lpSurface->w)
  260. {
  261. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  262. }
  263. else
  264. {
  265. break;
  266. }
  267. }
  268. dx++;
  269. }
  270. y += lpSurface->pitch;
  271. if (y / lpSurface->pitch >= lpSurface->h)
  272. {
  273. break;
  274. }
  275. }
  276. }
  277. #else
  278. typedef struct tagFont
  279. {
  280. LPWORD lpBufChar;
  281. LPBYTE lpBufGlyph;
  282. INT nChar;
  283. } FONT, *LPFONT;
  284. static LPFONT gpFont = NULL;
  285. INT
  286. PAL_InitFont(
  287. VOID
  288. )
  289. /*++
  290. Purpose:
  291. Load the font files.
  292. Parameters:
  293. None.
  294. Return value:
  295. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  296. --*/
  297. {
  298. FILE *fp;
  299. if (gpFont != NULL)
  300. {
  301. //
  302. // Already initialized
  303. //
  304. return 0;
  305. }
  306. gpFont = (LPFONT)calloc(1, sizeof(FONT));
  307. if (gpFont == NULL)
  308. {
  309. return -1;
  310. }
  311. //
  312. // Load the wor16.asc file.
  313. //
  314. fp = UTIL_OpenRequiredFile("wor16.asc");
  315. //
  316. // Get the size of wor16.asc file.
  317. //
  318. fseek(fp, 0, SEEK_END);
  319. gpFont->nChar = ftell(fp);
  320. gpFont->nChar /= 2;
  321. //
  322. // Read all the character codes.
  323. //
  324. gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
  325. if (gpFont->lpBufChar == NULL)
  326. {
  327. free(gpFont);
  328. gpFont = NULL;
  329. return -1;
  330. }
  331. fseek(fp, 0, SEEK_SET);
  332. fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
  333. //
  334. // Close wor16.asc file.
  335. //
  336. fclose(fp);
  337. //
  338. // Read all bitmaps from wor16.fon file.
  339. //
  340. fp = UTIL_OpenRequiredFile("wor16.fon");
  341. gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
  342. if (gpFont->lpBufGlyph == NULL)
  343. {
  344. free(gpFont->lpBufChar);
  345. free(gpFont);
  346. gpFont = NULL;
  347. return -1;
  348. }
  349. //
  350. // The font glyph data begins at offset 0x682 in wor16.fon.
  351. //
  352. fseek(fp, 0x682, SEEK_SET);
  353. fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
  354. fclose(fp);
  355. return 0;
  356. }
  357. VOID
  358. PAL_FreeFont(
  359. VOID
  360. )
  361. /*++
  362. Purpose:
  363. Free the memory used for fonts.
  364. Parameters:
  365. None.
  366. Return value:
  367. None.
  368. --*/
  369. {
  370. if (gpFont != NULL)
  371. {
  372. free(gpFont->lpBufChar);
  373. free(gpFont->lpBufGlyph);
  374. free(gpFont);
  375. }
  376. gpFont = NULL;
  377. }
  378. VOID
  379. PAL_DrawCharOnSurface(
  380. WORD wChar,
  381. SDL_Surface *lpSurface,
  382. PAL_POS pos,
  383. BYTE bColor
  384. )
  385. /*++
  386. Purpose:
  387. Draw a BIG-5 Chinese character on a surface.
  388. Parameters:
  389. [IN] wChar - the character to be drawn (in BIG-5).
  390. [OUT] lpSurface - the destination surface.
  391. [IN] pos - the destination location of the surface.
  392. [IN] bColor - the color of the character.
  393. Return value:
  394. None.
  395. --*/
  396. {
  397. int i, j, dx;
  398. int x = PAL_X(pos), y = PAL_Y(pos);
  399. LPBYTE pChar;
  400. //
  401. // Check for NULL pointer.
  402. //
  403. if (lpSurface == NULL || gpFont == NULL)
  404. {
  405. return;
  406. }
  407. //
  408. // Locate for this character in the font lib.
  409. //
  410. for (i = 0; i < gpFont->nChar; i++)
  411. {
  412. if (gpFont->lpBufChar[i] == wChar)
  413. {
  414. break;
  415. }
  416. }
  417. if (i >= gpFont->nChar)
  418. {
  419. //
  420. // This character does not exist in the font lib.
  421. //
  422. return;
  423. }
  424. pChar = gpFont->lpBufGlyph + i * 30;
  425. //
  426. // Draw the character to the surface.
  427. //
  428. y *= lpSurface->pitch;
  429. for (i = 0; i < 30; i++)
  430. {
  431. dx = x + ((i & 1) << 3);
  432. for (j = 0; j < 8; j++)
  433. {
  434. if (pChar[i] & (1 << (7 - j)))
  435. {
  436. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  437. }
  438. dx++;
  439. }
  440. y += (i & 1) * lpSurface->pitch;
  441. }
  442. }
  443. VOID
  444. PAL_DrawASCIICharOnSurface(
  445. BYTE bChar,
  446. SDL_Surface *lpSurface,
  447. PAL_POS pos,
  448. BYTE bColor
  449. )
  450. /*++
  451. Purpose:
  452. Draw a ASCII character on a surface.
  453. Parameters:
  454. [IN] bChar - the character to be drawn.
  455. [OUT] lpSurface - the destination surface.
  456. [IN] pos - the destination location of the surface.
  457. [IN] bColor - the color of the character.
  458. Return value:
  459. None.
  460. --*/
  461. {
  462. int i, j, dx;
  463. int x = PAL_X(pos), y = PAL_Y(pos);
  464. LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
  465. //
  466. // Check for NULL pointer.
  467. //
  468. if (lpSurface == NULL)
  469. {
  470. return;
  471. }
  472. //
  473. // Draw the character to the surface.
  474. //
  475. y *= lpSurface->pitch;
  476. for (i = 0; i < 15; i++)
  477. {
  478. dx = x;
  479. for (j = 0; j < 8; j++)
  480. {
  481. if (pChar[i] & (1 << j))
  482. {
  483. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  484. }
  485. dx++;
  486. }
  487. y += lpSurface->pitch;
  488. }
  489. }
  490. #endif