font.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "font.h"
  22. #include "ascii.h"
  23. #include "util.h"
  24. typedef struct tagFont
  25. {
  26. LPWORD lpBufChar;
  27. LPBYTE lpBufGlyph;
  28. INT nChar;
  29. } FONT, *LPFONT;
  30. static LPFONT gpFont = NULL;
  31. INT
  32. PAL_InitFont(
  33. VOID
  34. )
  35. /*++
  36. Purpose:
  37. Load the font files.
  38. Parameters:
  39. None.
  40. Return value:
  41. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  42. --*/
  43. {
  44. FILE *fp;
  45. if (gpFont != NULL)
  46. {
  47. //
  48. // Already initialized
  49. //
  50. return 0;
  51. }
  52. gpFont = (LPFONT)calloc(1, sizeof(FONT));
  53. if (gpFont == NULL)
  54. {
  55. return -1;
  56. }
  57. //
  58. // Load the wor16.asc file.
  59. //
  60. fp = UTIL_OpenRequiredFile("wor16.asc");
  61. //
  62. // Get the size of wor16.asc file.
  63. //
  64. fseek(fp, 0, SEEK_END);
  65. gpFont->nChar = ftell(fp);
  66. gpFont->nChar /= 2;
  67. //
  68. // Read all the character codes.
  69. //
  70. gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
  71. if (gpFont->lpBufChar == NULL)
  72. {
  73. free(gpFont);
  74. gpFont = NULL;
  75. return -1;
  76. }
  77. fseek(fp, 0, SEEK_SET);
  78. fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
  79. //
  80. // Close wor16.asc file.
  81. //
  82. fclose(fp);
  83. //
  84. // Read all bitmaps from wor16.fon file.
  85. //
  86. fp = UTIL_OpenRequiredFile("wor16.fon");
  87. gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
  88. if (gpFont->lpBufGlyph == NULL)
  89. {
  90. free(gpFont->lpBufChar);
  91. free(gpFont);
  92. gpFont = NULL;
  93. return -1;
  94. }
  95. //
  96. // The font glyph data begins at offset 0x682 in wor16.fon.
  97. //
  98. fseek(fp, 0x682, SEEK_SET);
  99. fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
  100. fclose(fp);
  101. return 0;
  102. }
  103. VOID
  104. PAL_FreeFont(
  105. VOID
  106. )
  107. /*++
  108. Purpose:
  109. Free the memory used for fonts.
  110. Parameters:
  111. None.
  112. Return value:
  113. None.
  114. --*/
  115. {
  116. if (gpFont != NULL)
  117. {
  118. free(gpFont->lpBufChar);
  119. free(gpFont->lpBufGlyph);
  120. free(gpFont);
  121. }
  122. gpFont = NULL;
  123. }
  124. VOID
  125. PAL_DrawCharOnSurface(
  126. WORD wChar,
  127. SDL_Surface *lpSurface,
  128. PAL_POS pos,
  129. BYTE bColor
  130. )
  131. /*++
  132. Purpose:
  133. Draw a BIG-5 Chinese character on a surface.
  134. Parameters:
  135. [IN] wChar - the character to be drawn (in BIG-5).
  136. [OUT] lpSurface - the destination surface.
  137. [IN] pos - the destination location of the surface.
  138. [IN] bColor - the color of the character.
  139. Return value:
  140. None.
  141. --*/
  142. {
  143. int i, j, dx;
  144. int x = PAL_X(pos), y = PAL_Y(pos);
  145. LPBYTE pChar;
  146. //
  147. // Check for NULL pointer.
  148. //
  149. if (lpSurface == NULL || gpFont == NULL)
  150. {
  151. return;
  152. }
  153. //
  154. // Locate for this character in the font lib.
  155. //
  156. for (i = 0; i < gpFont->nChar; i++)
  157. {
  158. if (gpFont->lpBufChar[i] == wChar)
  159. {
  160. break;
  161. }
  162. }
  163. if (i >= gpFont->nChar)
  164. {
  165. //
  166. // This character does not exist in the font lib.
  167. //
  168. return;
  169. }
  170. pChar = gpFont->lpBufGlyph + i * 30;
  171. //
  172. // Draw the character to the surface.
  173. //
  174. y *= lpSurface->pitch;
  175. for (i = 0; i < 30; i++)
  176. {
  177. dx = x + ((i & 1) << 3);
  178. for (j = 0; j < 8; j++)
  179. {
  180. if (pChar[i] & (1 << (7 - j)))
  181. {
  182. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  183. }
  184. dx++;
  185. }
  186. y += (i & 1) * lpSurface->pitch;
  187. }
  188. }
  189. VOID
  190. PAL_DrawASCIICharOnSurface(
  191. BYTE bChar,
  192. SDL_Surface *lpSurface,
  193. PAL_POS pos,
  194. BYTE bColor
  195. )
  196. /*++
  197. Purpose:
  198. Draw a ASCII character on a surface.
  199. Parameters:
  200. [IN] bChar - the character to be drawn.
  201. [OUT] lpSurface - the destination surface.
  202. [IN] pos - the destination location of the surface.
  203. [IN] bColor - the color of the character.
  204. Return value:
  205. None.
  206. --*/
  207. {
  208. int i, j, dx;
  209. int x = PAL_X(pos), y = PAL_Y(pos);
  210. LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
  211. //
  212. // Check for NULL pointer.
  213. //
  214. if (lpSurface == NULL)
  215. {
  216. return;
  217. }
  218. //
  219. // Draw the character to the surface.
  220. //
  221. y *= lpSurface->pitch;
  222. for (i = 0; i < 15; i++)
  223. {
  224. dx = x;
  225. for (j = 0; j < 8; j++)
  226. {
  227. if (pChar[i] & (1 << j))
  228. {
  229. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  230. }
  231. dx++;
  232. }
  233. y += lpSurface->pitch;
  234. }
  235. }