font.c 14 KB

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