font.c 11 KB

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