font.c 12 KB

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