#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iconv.h>
FILE* cclibfp;
void read_cclib( unsigned char high, unsigned char low, unsigned char *buf )
{
long p;
high = high - 0xa0; /* change high byte to domain_code */
low = low - 0xa0; /* change low byte */
/* if(high >= 15){ */
/* high -= 6; */
/* } /\* some code compress the charset at 10-15 domain *\/ */
/* p = ((long)((high - 1) * 94 + low -1)) * 32 - 16; /* get the wrong position */
p = ((long)((high - 1) * 94 + low + 16)) * 32 + 16; /* get the position */
fseek(cclibfp, p, SEEK_SET);
fread(buf, 32, 1, cclibfp);
}
void itoa(int value, char* buf, int base){
int i = strlen(buf);
for(; value || i ;){
buf[--i] = "0123456789abcdef"[value % base];
value /= base;
}
}
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
//UNICODE码转为GB2312码
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
//GB2312码转为UNICODE码
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
int chinese_magic(char* aim, char* back, char* fore)
{
char* in_utf8 = aim;
char chn[2];
u2g(in_utf8,strlen(in_utf8),chn,2);
unsigned char buf[32];
/* char string[5]; */
/* string[4] = '\0'; */
long i16;
int i, j;
if((cclibfp = fopen("font.gz","rb")) == NULL){
printf("\n 不能打开字库文件 font.ft \n");
exit(1);
}
read_cclib(chn[0],chn[1],buf);
printf("\n\n\n");
/* i = 12; */
/* printf("----------------\n"); */
/* itoa(i, string, 16); */
/* printf("%d is %s here\n", i, string); */
/* printf("----------------\n"); */
for(i = 0; i < 32; i += 2){
i16 = buf[i]<<8 | buf[i+1];
/* itoa(i16, string, 16); */
/* for(j = strlen(string); j < 4; j++){ */
/* printf("0"); */
/* } */
/* printf("%s", string); */
for(j = 0; j < 16; j++){
if(i16 & (0x8000>>j)){
printf("%s", fore);
} else {
printf("%s", back);
}
}
printf("\n");
}
fclose(cclibfp);
return 0;
}
int main()
{
return chinese_magic("好","一","李");
}
|