font.c 14 KB

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