font.c 13 KB

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