?? disphz.c
字號:
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
struct hz_struct //字庫信息的偏移量和大小
{
unsigned long shift;
unsigned int size;
}HZ_INDEX[1];
union utype //筆劃的點數(shù)
{
unsigned short size;
unsigned char str[2];
}BH;
unsigned char buf[1024]; //從字庫中得到的信息
unsigned char dotbuf[1024]; //經(jīng)翻譯后得到的點的坐標
FILE* outstream;
int num=1;
/*****************************************/
/* To draw a string of characters */
/* and get NC codes */
/*****************************************/
void hzdisp(int mode,int x,int y,int z,int w,int h,int angle1,int angle2,char string[],int zt,int color); //寫一串字并得到NC代碼。
/*****************************************/
/* To translate from hz library */
/* to x,y coordinate */
/*****************************************/
unsigned int decode(unsigned char *p,int length); //翻譯字庫信息
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"../BGI");
cleardevice(); //圖形界面初始化。
FILE* stream1;
FILE* stream2;
char string[100];
int tempmode,tempx,tempy,tempz,tempw,temph,tempangle1,tempangle2,tempzt,tempcolor;
if((stream2=fopen("format.txt","rb"))==NULL)
{
printf("open format file error");
exit(0);
}
if((stream1=fopen("content.txt","rb"))==NULL)
{
printf("open content file error");
exit(0);
}
if((outstream=fopen("output.txt","wb"))==NULL)
{
printf("open error");
exit(0);
}
while((!feof(stream1))&&(!feof(stream2)))
{
fscanf(stream1,"%s",string); //讀取content.txt文件內(nèi)容
fscanf(stream2,"%d",&tempmode); //讀取format.txt文件格式
fscanf(stream2,"%d",&tempx);
fscanf(stream2,"%d",&tempy);
fscanf(stream2,"%d",&tempz);
fscanf(stream2,"%d",&tempw);
fscanf(stream2,"%d",&temph);
fscanf(stream2,"%d",&tempangle1);
fscanf(stream2,"%d",&tempangle2);
fscanf(stream2,"%d",&tempzt);
fscanf(stream2,"%d",&tempcolor);
hzdisp(tempmode,tempx,tempy,tempz,tempw,temph,tempangle1,tempangle2,string,tempzt,tempcolor);//輸出一串漢字
}
getch();
setbkcolor(0);
fclose(outstream);
fclose(stream1);
fclose(stream2);
closegraph();
}
/*****************************************/
/* To draw a string of characters */
/* */
/*****************************************/
void hzdisp(int mode,int x,int y,int z,int w,int h,int angle1,int angle2,char cstring[],int zt,int color)
// mode:the writing direction of characters
// x:start x coordinate
// y:start y coordinate
// z:interval between two characters
// w:width of characters
// h:height of characters
// angle1: rotate angle of cstring
// angle2: inclined angle of cstring
// cstring: the content of characters
// zt: the font of characters
// color:the color of characters
{
FILE* fp; //for zt
FILE* fl; //for fh
unsigned int inter,locate,zone,bit;
unsigned f=0; //used to get the higher bit
long l;
int k,g=0;
int x0,y0,x1,y1,x4,y4,x5,y5;
float alfa,seta,x2,y2,x3,y3;
unsigned int hzsize;
if(zt==0) fp=fopen("../font/hzkslktj","rb"); //楷體
if(zt==1) fp=fopen("../font/hzkslhtj","rb"); //黑體
if(zt==2) fp=fopen("../font/hzkslstj","rb"); //宋體
if(zt==3) fp=fopen("../font/hzkslfsj","rb"); //仿宋體
if(zt==4) fp=fopen("../font/HZKSLYTJ","rb"); //姚體
if(zt==5) fp=fopen("../font/HZKSLXKJ","rb"); //行楷
if(zt==6) fp=fopen("../font/HZKSLWBJ","rb"); //魏碑
if(zt==7) fp=fopen("../font/HZKSLLSJ","rb"); //隸書
if(zt==8) fp=fopen("../font/HZKSLXBJ","rb"); //標宋
if(zt==9) fp=fopen("../font/HZKSLDHJ","rb"); //大黑
if(zt==10) fp=fopen("../font/HZKSLY1J","rb"); //細圓
if(zt==11) fp=fopen("../font/HZKSLMHJ","rb"); //梅黑
if(zt==12) fp=fopen("../font/HZKSLLBJ","rb"); //隸碑
if(zt==13) fp=fopen("../font/hzkslKTF","rb"); //繁體楷體
if(zt==14) fp=fopen("../font/hzkslHTF","rb"); //繁體黑體
if(zt==15) fp=fopen("../font/HZKSLY3J","rb"); //粗圓
if(zt==16) fp=fopen("../font/HZKSLFSF","rb"); //繁體仿宋體
if(zt==17) fp=fopen("../font/HZKSLSTF","rb"); //繁體宋體
if(fp==NULL)
{
printf("open error");
exit(0);
}
seta=angle2*3.14159/180;
alfa=-angle1*3.14159/180;
setcolor(color);
while(*cstring)//下面一個字一個字地讀信息。
{
inter=*(cstring++);//區(qū)位碼
if(inter>0xa0)//漢字。
{
if(f==0)
{
zone=(inter-0xa0)&0x7f; //get the value of zone
f=1;
}
else
{
if(zone>=16) //if is a hz.at first f=0,so zone must have a value.
{
bit=(inter-0xa0)&0x7f; //get bit
f=0;
locate=(zone-16)*94+(bit-1);
l=locate*6; //get offset:漢字索引信息的首地址。
fseek(fp,l,SEEK_SET);//開始找第一個漢字的索引信息、原始數(shù)據(jù)信息。
fread(HZ_INDEX,sizeof(struct hz_struct),1,fp);
if((fseek(fp,HZ_INDEX[0].shift,SEEK_SET))!=0)//定位到原始數(shù)據(jù)處。
{
printf("seek error");
exit(0);
}
memset(buf,0,1024);//清空函數(shù)。
fread(buf,HZ_INDEX[0].size,1,fp);//讀入原始數(shù)據(jù)。
hzsize=decode(buf,HZ_INDEX[0].size); //獲得矢量數(shù)據(jù)區(qū)數(shù)據(jù)并譯碼(一個漢字的矢量信息)
fprintf(outstream,"N%d G00 X0 Y0 M04\t\t",num);//這里考慮加一個G00代碼回原點
num++;
k=0;
for(int i=0;i<hzsize;i++)
{
BH.str[0]=dotbuf[k++];
BH.str[1]=dotbuf[k++];
if(BH.size==0) break;
if(mode==0) //橫向打字
{
x0=g*(w+z)+(dotbuf[k++]*w)/96;
y0=(dotbuf[k++]*h)/96;
x2=x0+y0*tan(seta);
y2=y0;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x0=x+x3;
y0=y+y3;
}
else if(mode==1) //豎向打字
{
x0=dotbuf[k++]*w/96;
y0=g*(h+z)+(dotbuf[k++]*h)/96;
x2=x0+y0*tan(seta);
y2=y0;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x0=x+x3;
y0=y+y3;
}
moveto(x0,y0);//moveto**********相當于G00。**************
fprintf(outstream,"N%d G00 X%d Y%d M04\t\t",num,x0,y0);//這里向output.txt輸出東西。
num++;
for(int j=0;j<BH.size-1;j++)
{
if(j==0)
{
x5=x0;
y5=y0;
}
else
{
x5=x4;
y5=y4;
}
if(mode==0) //橫向打字
{
x1=g*(w+z)+(dotbuf[k++]*w)/96;
y1=(dotbuf[k++]*h)/96;
x2=x1+y1*tan(seta);
y2=y1;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x4=x+x3;
y4=y+y3;
}
else if(mode==1) //豎向打字
{
x1=dotbuf[k++]*w/96;
y1=g*(h+z)+(dotbuf[k++]*h)/96;
x2=x1+x1*tan(seta);
y2=y1;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x4=x+x3;
y4=y+y3;
}
line(x5,y5,x4,y4);//************line相當于G01****************
fprintf(outstream,"N%d G01 X%d Y%d M03\t\t",num,x4,y4);//輸出output
num++;
}
line(x4,y4,x0,y0);//************line相當于G01****************
fprintf(outstream,"N%d G01 X%d Y%d M03\t\t",num,x0,y0);//輸出output
num++;
} //go back to orignal
g++;
}
else
{
fl=fopen("../font/HZKSLT","rb"); //符號庫
bit=(inter-0xa0)&0x7f;
f=0;
locate=(zone-1)*94+(bit-1);
l=locate*6; //get offset
fseek(fl,l,SEEK_SET);
fread(HZ_INDEX,sizeof(struct hz_struct),1,fl);
if((fseek(fl,HZ_INDEX[0].shift,SEEK_SET))!=0)
{
printf("seek error");
exit(0);
}
memset(buf,0,1024);
fread(buf,HZ_INDEX[0].size,1,fl);
hzsize=decode(buf,HZ_INDEX[0].size);
fclose(fl);
k=0;
for(int i=0;i<hzsize;i++)
{
BH.str[0]=dotbuf[k++];
BH.str[1]=dotbuf[k++];
if(BH.size==0) break;
if(mode==0) //橫向打字
{
x0=x+g*(w+z)+(dotbuf[k++]*w)/128;
y0=y+(dotbuf[k++]*h)/128;
x2=x0+y0*tan(seta);
y2=y0;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x0=x3;
y0=y3;
}
else if(mode==1) //豎向打字
{
x0=x+dotbuf[k++]*w/128;
y0=y+g*(h+z)+(dotbuf[k++]*h)/128;
x2=x0+y0*tan(seta);
y2=y0;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
x0=x3;
y0=y3;
}
moveto(x0,y0);//************moveto相當于G00****************
fprintf(outstream,"N%d G00 X%d Y%d M04\t\t",num,x0,y0);//輸出output
num++;
for(int j=0;j<BH.size-1;j++)
{
if(j==0)
{
x5=x0;
y5=y0;
}
else
{
x5=x3;
y5=y3;
}
if(mode==0)
{
x1=x+g*(w+z)+(dotbuf[k++]*w)/128;
y1=y+(dotbuf[k++]*h)/128;
x2=x1+y1*tan(seta);
y2=y1;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
}
else if(mode==1)
{
x1=x+dotbuf[k++]*w/128;
y1=y+g*(h+z)+(dotbuf[k++]*h)/128;
x2=x1+y1*tan(seta);
y2=y1;
x3=x2*cos(alfa)-y2*sin(alfa);
y3=x2*sin(alfa)+y2*cos(alfa);
}
line(x5,y5,x3,y3);//************line相當于G01****************
fprintf(outstream,"N%d G01 X%d Y%d M03\t\t",num,x3,y3);//輸出output
num++;
}
line(x3,y3,x0,y0);//************line相當于G01****************
fprintf(outstream,"N%d G01 X%d Y%d M03\t\t",num,x0,y0);//輸出output
num++;
} //go back to orignal
g++;
}
}
}
}
fclose(fp);
}
/*****************************************/
/* To translate from hz library */
/* to x,y coordinate */
/*****************************************/
unsigned int decode(unsigned char *p,int length)
// p:lines form library原始數(shù)據(jù)信息
// length:the number of points of a Vector Chinese characters
{
int k;
int i,count,lposi;
int xsum,ysum; //x,y absolute coordinate
unsigned char b76; //bit to disguise the different circumstance
char x0,dx,dy;
char dxth,dyth; //the sign of offset for the movement of x,y
k=2;
lposi=0; //to record the point number of a stroke
while((p-buf)<=length)
{
b76=*p&0xc0;
switch(b76)
{
case 0xc0: //標識符為11時
if(k!=2)
{
dotbuf[lposi]=(k-lposi-2)/2;
dotbuf[lposi+1]=0;
lposi=k++;
k++;
}
x0=(*p&0x3f)<<1;
dx=(*(p+1)>>7)&0x01;
dx=dx+x0;
p++;
dy=*p++&0x7f;
dotbuf[k++]=xsum=dx;
dotbuf[k++]=ysum=dy;
break;
case 0x80: //標識符為10時
dxth=dyth=1;
switch(*p&0x30)
{
case 0x00:
if(*p&0x08) dxth=-1;
dx=*p&0x07;
p++;
if(*p&0x80) dyth=-1;
dy=*p&0x7f;
break;
case 0x10:
if(*p&0x08) dyth=-1;
dy=*p&0x07;
p++;
if(*p&0x80) dxth=-1;
dx=*p&0x7f;
break;
case 0x20:
case 0x30:
p++;
if(*p&0x80) dxth=-1;
dx=*p&0x7f;
p++;
if(*p&0x80) dyth=-1;
dy=*p&0x7f;
break;
}
p++;
xsum+=dx*dxth;
ysum+=dy*dyth;
dotbuf[k++]=xsum;
dotbuf[k++]=ysum;
break;
case 0x40: //標識符為01時
dxth=*p&0x30;
if(dxth==0)
{
dxth=1;
dyth=1;
}
else if (dxth==0x10)
{
dxth=-1;
dyth=1;
}
else if (dxth==0x20)
{
dxth=-1;
dyth=-1;
}
else if(dxth==0x30)
{
dxth=1;
dyth=-1;
}
count=*p++&0x0f;
for(int l=0;l<count;l++)
{
dx=*p>>4;
dy=*p&0x0f;
xsum+=dxth*dx;
ysum+=dyth*dy;
dotbuf[k++]=xsum;
dotbuf[k++]=ysum;
p++;
}
break;
case 00: //標識符為00時
count=*p++&0x3f;
for(l=0;l<count;l++)
{
if(*p&0x80) dxth=-1;
else dxth=1;
if(*p&0x08) dyth=-1;
else dyth=1;
dx=(*p&0x70)>>4;
dy=(*p&0x07);
xsum+=dx*dxth;
ysum+=dy*dyth;
dotbuf[k++]=xsum;
dotbuf[k++]=ysum;
p++;
}
}
}
dotbuf[k++]=0;
dotbuf[k++]=0;
dotbuf[lposi]=(k-lposi-2-2)/2;
dotbuf[lposi+1]=0;
return k;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -