han2txt.c

Modified at 1999. 7.19 17:07
Printed at 1999. 7.19 17:07

  1: /*
  2:  * Han2Txt.C
  3:  *
  4:  * made by scgyong@nownuri.net
  5:  *
  6:  * you can modify and redistribute this source
  7:  * under the rule of Copy-Left, GNU
  8:  */
  9: 
 10: #include <stdio.h>
 11: #include <string.h>
 12: #include <stdlib.h>
 13: 
 14: #define MAXLINEWIDTH 8
 15: #define BUFWIDTH (MAXLINEWIDTH + 2)
 16: 
 17: #define CHAR_ON  '0'
 18: #define CHAR_OFF ' '
 19: 
 20: typedef enum { FALSE, TRUE } BOOL;
 21: typedef unsigned short WORD;
 22: typedef unsigned char BYTE;
 23: 
 24: #define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
 25: #define LOBYTE(w) ((BYTE)(w))
 26: 
 27: #define MAKEWORD(a, b) \
 28:     ((WORD) (((BYTE) (a)) | ((WORD) ((BYTE) (b))) << 8))
 29: 
 30: #define HAN_FIR(w) (((w) >> 10) & 0x1F)
 31: #define HAN_MID(w) (((w) >> 5)  & 0x1F)
 32: #define HAN_LAS(w) ( (w)        & 0x1F)
 33: 
 34: #define OFFSET_MID (20 * 8)
 35: #define OFFSET_LAS (OFFSET_MID + 22 * 4)
 36: 
 37: static char szHFileName[] = "hangul.fnt";
 38: static char szEFileName[] = "english.fnt";
 39: 
 40: static unsigned short hfont[8*20 + 4*22 + 4*27][16];
 41: static unsigned char  efont[96][16];
 42: 
 43: static char buf[16][BUFWIDTH * 8];
 44: static int  nChar;
 45: static int  chSaved;
 46: 
 47: static const int nFirTable[32] = {
 48:      0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 49:     15, 16, 17, 18, 19,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 50: };
 51: static const int nMidTable[32] = {
 52:     0,  0,  0,  1,  2,  3,  4,  5,  0,  0,  6,  7,  8,  9, 10, 11,
 53:     0,  0, 12, 13, 14, 15, 16, 17,  0,  0, 18, 19, 20, 21,  0,  0
 54: };
 55: static const int nLasTable[32] = {
 56:      0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 57:     15, 16,  0, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,  0,  0
 58: };
 59: 
 60: static const int nFirRef[22][2] = {
 61:     0, 0, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 1, 6, 3, 7,
 62:     3, 7, 3, 7, 1, 6, 2, 6, 4, 7, 4, 7, 4, 7, 2, 6, 1, 6, 3, 7, 0, 5
 63: };
 64: 
 65: static const int nMidRef[20] = {
 66:     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1
 67: };
 68: 
 69: static const int nLasRef[22] = {
 70:     0, 0, 2, 0, 2, 1, 2, 1, 2, 3, 0, 2, 1, 3, 3, 1, 2, 1, 3, 3, 1, 1
 71: };
 72: 
 73: BOOL LoadFont(char *szFileName, void *dest, int cbSize)
 74: {
 75:     FILE *pFile;
 76:     int   ret;
 77: 
 78:     pFile = fopen(szFileName, "rb");
 79:     if (!pFile) {
 80:         printf("Font File %s Open Error\n", szFileName);
 81:         return FALSE;
 82:     }
 83: 
 84:     ret = fread(dest, cbSize, 1, pFile);
 85: 
 86:     fclose(pFile);
 87: 
 88:     return ret ? TRUE : FALSE;
 89: }
 90: 
 91: BOOL InitProgram(void)
 92: {
 93:     BOOL ret;
 94: 
 95:     ret = LoadFont(szHFileName, hfont, sizeof (hfont));
 96:     if (!ret)
 97:         return FALSE;
 98: 
 99:     ret = LoadFont(szEFileName, efont, sizeof (efont));
100:     if (!ret)
101:         return FALSE;
102: 
103:     return TRUE;
104: }
105: 
106: BOOL PrintBuffer(void)
107: {
108:     int x, y;
109: 
110:     if (!nChar) {
111:         return FALSE;
112:     }
113: 
114:     for (y = 0; y < 16; y++) {
115:         for (x = 0; x < nChar * 8; x++) {
116:             putchar(buf[y][x]);
117:         }
118:         putchar('\n');
119:     }
120: 
121:     return TRUE;
122: }
123: 
124: BOOL ClearBuffer(void)
125: {
126:     int x, y;
127: 
128:     for (y = 0; y < 16; y++) {
129:         for (x = 0; x < BUFWIDTH * 8 - 1; x++) {
130:             buf[y][x] = CHAR_OFF;
131:         }
132:         buf[y][x] = '\0';
133:     }
134: 
135:     nChar = 0;
136: 
137:     return TRUE;
138: }
139: 
140: BOOL SetBufOn(int x, int y)
141: {
142:     buf[y][nChar * 8 + x] = CHAR_ON;
143: 
144:     return TRUE;
145: }
146: 
147: BOOL CheckForLineEnd(int nWidth)
148: {
149:     nChar += nWidth;
150: 
151:     if (nChar >= MAXLINEWIDTH) {
152:         PrintBuffer();
153:         ClearBuffer();
154:     }
155: 
156:     return TRUE;
157: }
158: 
159: BOOL PutSingleByte(int ch)
160: {
161:     int src, x, y;
162: 
163:     if (ch < 32) {
164:         ch = 32;
165:     }
166: 
167:     ch -= 32;
168: 
169:     for (y = 0; y < 16; y++) {
170:         src = efont[ch][y];
171:         for (x = 0; x < 8; x++) {
172:             if (src & 0x80)
173:                 SetBufOn(x, y);
174:             src <<= 1;
175:         }
176:     }
177: 
178:     CheckForLineEnd(1);
179: 
180:     return TRUE;
181: }
182: 
183: static BOOL memor(unsigned short *dest, unsigned short *src)
184: {
185:     int i = 16;
186: 
187:     while (i--) {
188:         *dest++ |= *src++;
189:     }
190: 
191:     return TRUE;
192: }
193: 
194: BOOL PutDoubleByte(int wChar)
195: {
196:     unsigned short buf[16];
197:     int            src, x, y;
198: 
199:     int     cfir, cmid, clas;
200:     int     ofir, omid, olas;
201:     int     mfir, mmid, mlas;
202: 
203:     //
204:     // counters
205:     //
206:     cfir = nFirTable[HAN_FIR(wChar)];
207:     cmid = nMidTable[HAN_MID(wChar)];
208:     clas = nLasTable[HAN_LAS(wChar)];
209: 
210:     //
211:     // index offsets
212:     //
213:     ofir = nFirRef[cmid][clas != 0];
214:     omid = nMidRef[cfir] + (clas ? 2 : 0);
215:     olas = nLasRef[cmid];
216: 
217:     //
218:     // memory offsets
219:     //
220:     mfir = ofir * 20 + cfir;
221:     mmid = omid * 22 + cmid;
222:     mlas = olas * 28 + clas;
223: 
224:     if (cfir) {
225:         memcpy(buf, hfont[mfir], 32);
226:     } else {
227:         memset(buf, 0, 32);
228:     }
229: 
230:     if (cmid) {
231:         memor(buf, hfont[mmid + OFFSET_MID]);
232:     }
233: 
234:     if (clas) {
235:         memor(buf, hfont[mlas + OFFSET_LAS]);
236:     }
237: 
238:     for (y = 0; y < 16; y++) {
239:         // change byte order
240:         src = MAKEWORD(HIBYTE(buf[y]), LOBYTE(buf[y]));
241:         for (x = 0; x < 16; x++) {
242:             if (src & 0x8000)
243:                 SetBufOn(x, y);
244:             src <<= 1;
245:         }
246:     }
247: 
248:     CheckForLineEnd(2);
249: 
250:     return TRUE;
251: }
252: 
253: BOOL PutChar(int ch)
254: {
255:     if (chSaved) {
256:         PutDoubleByte(MAKEWORD(ch, chSaved));
257:         chSaved = 0;
258:     } else if (ch & 0x80) {
259:         chSaved = ch;
260:     } else {
261:         PutSingleByte(ch);
262:     }
263: 
264:     return TRUE;
265: }
266: 
267: int main(void)
268: {
269:     int ch;
270: 
271:     if (!InitProgram()) {
272:         printf("Program Initialization Error\n");
273:         exit(0);
274:     }
275: 
276:     ClearBuffer();
277: 
278:     while ((ch = getchar()) != EOF) {
279:         PutChar(ch);
280:     }
281: 
282:     PrintBuffer();
283: 
284:     return 0;
285: }
286: 

This document has been generated using RooTMAN's Source Printer ver. 1.3.1.
han2txt.c / Confidential / BbCom / July 19, 1999