font.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  3. // All rights reserved.
  4. //
  5. // This file is part of SDLPAL.
  6. //
  7. // SDLPAL is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. #include "font.h"
  21. #include "ascii.h"
  22. #include "util.h"
  23. typedef struct tagFont
  24. {
  25. LPWORD lpBufChar;
  26. LPBYTE lpBufGlyph;
  27. INT nChar;
  28. } FONT, *LPFONT;
  29. static LPFONT gpFont = NULL;
  30. INT
  31. PAL_InitFont(
  32. VOID
  33. )
  34. /*++
  35. Purpose:
  36. Load the font files.
  37. Parameters:
  38. None.
  39. Return value:
  40. 0 if succeed, -1 if cannot allocate memory, -2 if cannot load files.
  41. --*/
  42. {
  43. FILE *fp;
  44. if (gpFont != NULL)
  45. {
  46. //
  47. // Already initialized
  48. //
  49. return 0;
  50. }
  51. gpFont = (LPFONT)calloc(1, sizeof(FONT));
  52. if (gpFont == NULL)
  53. {
  54. return -1;
  55. }
  56. //
  57. // Load the wor16.asc file.
  58. //
  59. fp = UTIL_OpenRequiredFile("wor16.asc");
  60. //
  61. // Get the size of wor16.asc file.
  62. //
  63. fseek(fp, 0, SEEK_END);
  64. gpFont->nChar = ftell(fp);
  65. gpFont->nChar /= 2;
  66. //
  67. // Read all the character codes.
  68. //
  69. gpFont->lpBufChar = (LPWORD)calloc(gpFont->nChar, sizeof(WORD));
  70. if (gpFont->lpBufChar == NULL)
  71. {
  72. free(gpFont);
  73. gpFont = NULL;
  74. return -1;
  75. }
  76. fseek(fp, 0, SEEK_SET);
  77. fread(gpFont->lpBufChar, sizeof(WORD), gpFont->nChar, fp);
  78. //
  79. // Close wor16.asc file.
  80. //
  81. fclose(fp);
  82. //
  83. // Read all bitmaps from wor16.fon file.
  84. //
  85. fp = UTIL_OpenRequiredFile("wor16.fon");
  86. gpFont->lpBufGlyph = (LPBYTE)calloc(gpFont->nChar, 30);
  87. if (gpFont->lpBufGlyph == NULL)
  88. {
  89. free(gpFont->lpBufChar);
  90. free(gpFont);
  91. gpFont = NULL;
  92. return -1;
  93. }
  94. //
  95. // The font glyph data begins at offset 0x682 in wor16.fon.
  96. //
  97. fseek(fp, 0x682, SEEK_SET);
  98. fread(gpFont->lpBufGlyph, 30, gpFont->nChar, fp);
  99. fclose(fp);
  100. return 0;
  101. }
  102. VOID
  103. PAL_FreeFont(
  104. VOID
  105. )
  106. /*++
  107. Purpose:
  108. Free the memory used for fonts.
  109. Parameters:
  110. None.
  111. Return value:
  112. None.
  113. --*/
  114. {
  115. if (gpFont != NULL)
  116. {
  117. free(gpFont->lpBufChar);
  118. free(gpFont->lpBufGlyph);
  119. free(gpFont);
  120. }
  121. gpFont = NULL;
  122. }
  123. VOID
  124. PAL_DrawCharOnSurface(
  125. WORD wChar,
  126. SDL_Surface *lpSurface,
  127. PAL_POS pos,
  128. BYTE bColor
  129. )
  130. /*++
  131. Purpose:
  132. Draw a BIG-5 Chinese character on a surface.
  133. Parameters:
  134. [IN] wChar - the character to be drawn (in BIG-5).
  135. [OUT] lpSurface - the destination surface.
  136. [IN] pos - the destination location of the surface.
  137. [IN] bColor - the color of the character.
  138. Return value:
  139. None.
  140. --*/
  141. {
  142. int i, j, dx;
  143. int x = PAL_X(pos), y = PAL_Y(pos);
  144. LPBYTE pChar;
  145. //
  146. // Check for NULL pointer.
  147. //
  148. if (lpSurface == NULL || gpFont == NULL)
  149. {
  150. return;
  151. }
  152. //
  153. // Locate for this character in the font lib.
  154. //
  155. for (i = 0; i < gpFont->nChar; i++)
  156. {
  157. if (gpFont->lpBufChar[i] == wChar)
  158. {
  159. break;
  160. }
  161. }
  162. if (i >= gpFont->nChar)
  163. {
  164. //
  165. // This character does not exist in the font lib.
  166. //
  167. return;
  168. }
  169. pChar = gpFont->lpBufGlyph + i * 30;
  170. //
  171. // Draw the character to the surface.
  172. //
  173. y *= lpSurface->pitch;
  174. for (i = 0; i < 30; i++)
  175. {
  176. dx = x + ((i & 1) << 3);
  177. for (j = 0; j < 8; j++)
  178. {
  179. if (pChar[i] & (1 << (7 - j)))
  180. {
  181. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  182. }
  183. dx++;
  184. }
  185. y += (i & 1) * lpSurface->pitch;
  186. }
  187. }
  188. VOID
  189. PAL_DrawASCIICharOnSurface(
  190. BYTE bChar,
  191. SDL_Surface *lpSurface,
  192. PAL_POS pos,
  193. BYTE bColor
  194. )
  195. /*++
  196. Purpose:
  197. Draw a ASCII character on a surface.
  198. Parameters:
  199. [IN] bChar - the character to be drawn.
  200. [OUT] lpSurface - the destination surface.
  201. [IN] pos - the destination location of the surface.
  202. [IN] bColor - the color of the character.
  203. Return value:
  204. None.
  205. --*/
  206. {
  207. int i, j, dx;
  208. int x = PAL_X(pos), y = PAL_Y(pos);
  209. LPBYTE pChar = &iso_font[(int)(bChar & ~128) * 15];
  210. //
  211. // Check for NULL pointer.
  212. //
  213. if (lpSurface == NULL)
  214. {
  215. return;
  216. }
  217. //
  218. // Draw the character to the surface.
  219. //
  220. y *= lpSurface->pitch;
  221. for (i = 0; i < 15; i++)
  222. {
  223. dx = x;
  224. for (j = 0; j < 8; j++)
  225. {
  226. if (pChar[i] & (1 << j))
  227. {
  228. ((LPBYTE)(lpSurface->pixels))[y + dx] = bColor;
  229. }
  230. dx++;
  231. }
  232. y += lpSurface->pitch;
  233. }
  234. }