font.c 14 KB

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