font.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #else
  158. typedef struct tagFont
  159. {
  160. LPWORD lpBufChar;
  161. LPBYTE lpBufGlyph;
  162. INT nChar;
  163. } FONT, *LPFONT;
  164. static LPFONT gpFont = NULL;
  165. INT
  166. PAL_InitFont(
  167. VOID
  168. )
  169. /*++
  170. Purpose:
  171. Load the font files.
  172. Parameters:
  173. None.
  174. Return value:
  175. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  176. --*/
  177. {
  178. FILE *fp;
  179. if (gpFont != NULL)
  180. {
  181. //
  182. // Already initialized
  183. //
  184. return 0;
  185. }
  186. gpFont = (LPFONT)calloc(1, sizeof(FONT));
  187. if (gpFont == NULL)
  188. {
  189. return -1;
  190. }
  191. //
  192. // Load the wor16.asc file.
  193. //
  194. fp = UTIL_OpenRequiredFile("wor16.asc");
  195. //
  196. // Get the size of wor16.asc file.
  197. //
  198. fseek(fp, 0, SEEK_END);
  199. gpFont->nChar = ftell(fp);
  200. gpFont->nChar /= 2;
  201. //
  202. // Read all the character codes.
  203. //
  204. gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
  205. if (gpFont->lpBufChar == NULL)
  206. {
  207. free(gpFont);
  208. gpFont = NULL;
  209. return -1;
  210. }
  211. fseek(fp, 0, SEEK_SET);
  212. fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
  213. //
  214. // Close wor16.asc file.
  215. //
  216. fclose(fp);
  217. //
  218. // Read all bitmaps from wor16.fon file.
  219. //
  220. fp = UTIL_OpenRequiredFile("wor16.fon");
  221. gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
  222. if (gpFont->lpBufGlyph == NULL)
  223. {
  224. free(gpFont->lpBufChar);
  225. free(gpFont);
  226. gpFont = NULL;
  227. return -1;
  228. }
  229. //
  230. // The font glyph data begins at offset 0x682 in wor16.fon.
  231. //
  232. fseek(fp, 0x682, SEEK_SET);
  233. fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
  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. if (gpFont != NULL)
  251. {
  252. free(gpFont->lpBufChar);
  253. free(gpFont->lpBufGlyph);
  254. free(gpFont);
  255. }
  256. gpFont = NULL;
  257. }
  258. VOID
  259. PAL_DrawCharOnSurface(
  260. WORD wChar,
  261. SDL_Surface *lpSurface,
  262. PAL_POS pos,
  263. BYTE bColor
  264. )
  265. /*++
  266. Purpose:
  267. Draw a BIG-5 Chinese character on a surface.
  268. Parameters:
  269. [IN] wChar - the character to be drawn (in BIG-5).
  270. [OUT] lpSurface - the destination surface.
  271. [IN] pos - the destination location of the surface.
  272. [IN] bColor - the color of the character.
  273. Return value:
  274. None.
  275. --*/
  276. {
  277. int i, j, dx;
  278. int x = PAL_X(pos), y = PAL_Y(pos);
  279. LPBYTE pChar;
  280. //
  281. // Check for NULL pointer.
  282. //
  283. if (lpSurface == NULL || gpFont == NULL)
  284. {
  285. return;
  286. }
  287. //
  288. // Locate for this character in the font lib.
  289. //
  290. for (i = 0; i < gpFont->nChar; i++)
  291. {
  292. if (gpFont->lpBufChar[i] == wChar)
  293. {
  294. break;
  295. }
  296. }
  297. if (i >= gpFont->nChar)
  298. {
  299. //
  300. // This character does not exist in the font lib.
  301. //
  302. return;
  303. }
  304. pChar = gpFont->lpBufGlyph + i * 30;
  305. //
  306. // Draw the character to the surface.
  307. //
  308. y *= lpSurface->pitch;
  309. for (i = 0; i < 30; i++)
  310. {
  311. dx = x + ((i & 1) << 3);
  312. for (j = 0; j < 8; j++)
  313. {
  314. if (pChar[i] & (1 << (7 - j)))
  315. {
  316. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  317. }
  318. dx++;
  319. }
  320. y += (i & 1) * lpSurface->pitch;
  321. }
  322. }
  323. VOID
  324. PAL_DrawASCIICharOnSurface(
  325. BYTE bChar,
  326. SDL_Surface *lpSurface,
  327. PAL_POS pos,
  328. BYTE bColor
  329. )
  330. /*++
  331. Purpose:
  332. Draw a ASCII character on a surface.
  333. Parameters:
  334. [IN] bChar - the character to be drawn.
  335. [OUT] lpSurface - the destination surface.
  336. [IN] pos - the destination location of the surface.
  337. [IN] bColor - the color of the character.
  338. Return value:
  339. None.
  340. --*/
  341. {
  342. int i, j, dx;
  343. int x = PAL_X(pos), y = PAL_Y(pos);
  344. LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
  345. //
  346. // Check for NULL pointer.
  347. //
  348. if (lpSurface == NULL)
  349. {
  350. return;
  351. }
  352. //
  353. // Draw the character to the surface.
  354. //
  355. y *= lpSurface->pitch;
  356. for (i = 0; i < 15; i++)
  357. {
  358. dx = x;
  359. for (j = 0; j < 8; j++)
  360. {
  361. if (pChar[i] & (1 << j))
  362. {
  363. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  364. }
  365. dx++;
  366. }
  367. y += lpSurface->pitch;
  368. }
  369. }
  370. #endif