⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 disphz.c

📁 显示UCDOS矢量字库中的汉字。 内码为索引。
💻 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                                //笔划的点数
{
	unsigned short size;
	unsigned char str[2];
}BH;

unsigned char buf[1024];                  //从字库中得到的信息
unsigned char dotbuf[1024];               //经翻译后得到的点的坐标
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文件内容
		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++);//区位码
		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);//开始找第一个汉字的索引信息、原始数据信息。
					fread(HZ_INDEX,sizeof(struct hz_struct),1,fp);
					if((fseek(fp,HZ_INDEX[0].shift,SEEK_SET))!=0)//定位到原始数据处。
					{
						printf("seek error");
						exit(0);
					}
					memset(buf,0,1024);//清空函数。
					fread(buf,HZ_INDEX[0].size,1,fp);//读入原始数据。

					hzsize=decode(buf,HZ_INDEX[0].size); //获得矢量数据区数据并译码(一个汉字的矢量信息)




					
                                         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原始数据信息
//           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 + -