If the font library is version 5.0, the following piece of code should be removed
Hope everyone can like it!!!!!!!
if(j==1){ //If it is a Chinese character, adjust the address offset value
hz.offset-=0x10000000;
}
Hope everyone can like it!!!!!!!
#include "d:\borland\user\src\graphics.h"
#include "d:\borland\user\src\graphics.c"
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <mem.h>
#include <alloc.h>
#include <dir.h>
#include <dos.h>
#include <stdlib.h>
#define baseSize 250 //The larger this value is, the smaller the font is. The original value is 168
#define portion 100 //100
FILE *fpfh,*fphz,*fpe,*fp;
int X0,Y0,xRange,yRange,curX,curY;
int newX0,newY0,newxRange,newyRange;
unsigned char *wordCont_0=NULL,*wordCont_8=NULL;
unsigned bytesNum_0,bytesNum_8;
unsigned char myread(void);
void control(int *xx,int *yy,unsigned char controlWord,int nowxRange,
int nowyRange,int nowX0,int nowY0,int call_8_0);
void showhz(char *fontfileName,unsigned long col);
void b3_4(int *x,int *y,unsigned long col);
void b3_3(int *x,int *y,unsigned long col);
int readbyte(void);
int readcount;
static unsigned char next; //Used to read the flag of the high four bits and low four bits of a byte, 1 for reading the high bits, 0 for reading the low bits
typedef struct _hz{
unsigned long offset; //4 bytes
unsigned int length; //2 bytes
}hz_;
hz_ hz;
unsigned char hzdata; //Used to read font data
void main(void)
{
unsigned char *hzstring="然字Mm骤啊H骤g9酆8G字,汉,然。!!戆";//戆您原回骤然燃";//"共有汉字个骤";原骤戆啊汉字
long color;
initgraph(VBE480_16M);
X0=Y0=1;xRange=yRange=152;//The original value is 152
showhz(hzstring,rgbcolor(255,0,0));
getch();
cleardevice();
closegraph();
}
void showhz(char *hzstring,unsigned long col)
{
unsigned long oldcolor;
unsigned long wordOffset;
unsigned char qm,wm;
int bx,by; //Used to record the coordinates of the starting point of the stroke
int xx,yy,controlWord,i,j,m=0;
char ch,cha,chx,chy,chxx,chyy;
char mark1_x,mark1_y,mark2_x,mark2_y,mark3_x,mark3_y;
char *fontFileName="HZKPSKTJ";//HZKPSXKJ";//HZKPSST.GBK";//"HZKPSHTJ";//"HZKPSFSJ";//"HZKPSXBJ";HZKPSKTJ;HZKPSY3J;ASCPS;HZKPST;HZKPSDHJ,HZKPSYTJ
oldcolor=getcolor();
setcolor(col);
if((fpfh=fopen("HZKPST","rb"))==NULL) { //Open Chinese symbol curve font library
printf("Cannot open %s file\n","HZKPST");
exit(0);
}
if((fphz=fopen(fontFileName,"rb"))==NULL) { //Open Chinese curve font library, if it is a GBK font library, then open the GBK font library
printf("Cannot open %s file\n",fontFileName);
exit(0);
}
if((fpe=fopen("ASCPS","rb"))==NULL) { //Open English curve font library
printf("Cannot open %s file\n","ASCPS");
exit(0);
}
i=0;
while(i<strlen(hzstring))
{
readcount=0;
next=0;
if((hzstring&0x80)==0x80){ //Judge whether it is a Chinese character, if it is equal, that is, the highest bit is 1, then it is a Chinese character
qm=hzstring-0xa0; //Area code
wm=hzstring-0xa0; //Bit code
j=1; //As a flag of Chinese characters, used to adjust the address offset
if(strcmp(strrchr(fontFileName, '.')+1,"GBK")==0){ //First find the position of the last '.', add 1 to make the pointer point to the first character after '.', then compare, judge whether it is a GBK font library
fp=fphz; //GBK font library still has some missing Chinese characters, such as the character "镕", which does not exist in the font library
wordOffset=(((long)qm-1)*94+(wm-1))*6; //GBK font library has included the Chinese symbol font library before 16 areas. When calculating the address, the area code should be subtracted by 1 instead of 6
}
else { //Not a GBK font library
if(qm>=16){ //Chinese characters after 16 areas
fp=fphz;
wordOffset=(((long)qm-16)*94+(wm-1))*6; //Offset of Chinese characters after 16 areas
}
else{ //Chinese symbols before 16 areas
fp=fpfh;
wordOffset=(((long)qm-1)*94+(wm-1))*6; //Offset of Chinese symbols before 16 areas
}
}
i+=2; //Chinese characters are two bytes
}
else{ //It is an English character
fp=fpe;
wordOffset=(0*94+(hzstring-32)-1)*6; //There are ten types of English fonts 0~9. Let the font number be N, and the ASCII code of the character be CC, then the offset of the English character is (N*94+(CC-32)-1)*6. The ASCII code value of the space is 32, but it does not exist in the font library. The starting character of the font library is the character '!', with a value of 33. Actually, it should be subtracted by 33. There are 94 characters in each type of font in the font library
i+=1;
}
fseek(fp,wordOffset,SEEK_SET);
fread(&hz,sizeof(hz_),1,fp);
if(j==1){ //If it is a Chinese character, adjust the address offset value
hz.offset-=0x10000000;
j=0;
}
fseek(fp,hz.offset,SEEK_SET);
fread(hzdata,1,hz.length,fp);
while(readcount<hz.length) {
ch=myread(); //Take the control code
switch(ch){
case 0: //New starting point 0000
if(readcount>0)lineto(bx,by); //Do not connect when the first starting point of the entire character, connect the last point of the stroke with the starting point, and close the stroke
xx=readbyte();
yy=readbyte();
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx+=X0;yy+=Y0;
moveto(xx,yy); //Stroke starting point
curX=xx;curY=yy;
bx=xx;by=yy; //Record the coordinates of the starting point of the stroke
break;
case 1:
xx=readbyte();
xx=xRange*xx/baseSize+0.5;
xx+=X0;
lineto(xx,curY);
curX=xx;
break;
case 2:
yy=readbyte();
yy=yRange*yy/baseSize+0.5;
yy+=Y0;
lineto(curX,yy);
curY=yy;
break;
case 3:
xx=readbyte();
yy=readbyte();
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx+=X0;yy+=Y0;
lineto(xx,yy);
curX=xx;curY=yy;
break;
case 4:
xx=curX;yy=curY;
xx=readbyte();
yy=readbyte();
xx=readbyte();
yy=readbyte();
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx+=X0;yy+=Y0;
xx+=X0;yy+=Y0;
b3_3(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 5:
xx=curX;yy=curY;
xx=readbyte();
yy=readbyte();
xx=readbyte();
yy=readbyte();
xx=readbyte();
yy=readbyte();
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
for(m=1;m<4;m++) {
xx+=X0;yy+=Y0;
}
b3_4(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 6:
xx=readbyte();
yy=readbyte();
xx=readbyte();
yy=readbyte();
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
xx=xRange*xx/baseSize+0.5;
yy=yRange*yy/baseSize+0.5;
for(m=0;m<2;m++) {
xx+=X0;yy+=Y0;
}
rectangle(xx,yy,xx,yy);
break;
case 7:
mark1_x=1;
ch=myread();
if(ch&8)mark1_x=-1;
ch&=7;
xx=curX+ch*mark1_x*xRange/baseSize+0.5;
yy=readbyte();
yy=yRange*yy/baseSize+0.5;
yy+=Y0;
lineto(xx,yy);
curX=xx;curY=yy;
break;
case 8:
mark1_y=1;
xx=readbyte();
xx=xRange*xx/baseSize+0.5;
xx+=X0;
ch=myread();
if(ch&8)mark1_y=-1;
ch&=7;
yy=curY+ch*mark1_y*yRange/baseSize+0.5;
lineto(xx,yy);
curX=xx;curY=yy;
break;
case 9:
mark1_x=1;
mark1_y=1;
ch=myread();
if(ch&8)mark1_x=-1;
ch&=7;
xx=curX+ch*mark1_x*xRange/baseSize+0.5;
ch=myread();
if(ch&8)mark1_y=-1;
ch&=7;
yy=curY+ch*mark1_y*yRange/baseSize+0.5;
lineto(xx,yy);
curX=xx;curY=yy;
break;
case 10:
mark1_x=1;
mark1_y=1;
ch=myread(); //The first four bits read are the high four bits of the six-bit data
cha=myread(); //The high two bits of the four bits read the second time are the low two bits of the six-bit data, and the low two bits are the high two bits of the second six-bit data
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark1_x=-1;
ch&=0x1f;
xx=curX+ch*mark1_x*xRange/baseSize+0.5;
ch=myread(); //The four bits read the third time are the low four bits of the second six-bit data
ch=((cha&0x03)<<4)+ch;
if(ch&0x20)mark1_y=-1;
ch&=0x1f;
yy=curY+ch*mark1_y*yRange/baseSize+0.5;
lineto(xx,yy);
curX=xx;curY=yy;
break;
case 11:
mark1_x=1;
mark1_y=1;
mark2_x=1;
mark2_y=1;
xx=curX;yy=curY;
ch=myread();
if(ch&8)mark1_x=-1;
chx=ch=(ch&7)*mark1_x;
xx=curX+ch*xRange/baseSize+0.5;
ch=myread();
if(ch&8)mark1_y=-1;
chy=ch=(ch&7)*mark1_y;
yy=curY+ch*yRange/baseSize+0.5;
ch=myread();
if(ch&8)mark2_x=-1;
ch=(ch&7)*mark2_x;
xx=curX+(chx+ch)*xRange/baseSize+0.5;
ch=myread();
if(ch&8)mark2_y=-1;
ch=(ch&7)*mark2_y;
yy=curY+(chy+ch)*yRange/baseSize+0.5;
b3_3(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 12:
mark1_x=1;
mark1_y=1;
mark2_x=1;
mark2_y=1;
xx=curX;yy=curY;
ch=myread(); //1
cha=myread(); //2
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark1_x=-1;
chx=ch=(ch&0x1f)*mark1_x;
xx=curX+ch*xRange/baseSize+0.5;
ch=myread(); //3
ch=((cha&0x03)<<4)+ch;
if(ch&0x20)mark1_y=-1;
chy=ch=(ch&0x1f)*mark1_y;
yy=curY+ch*yRange/baseSize+0.5;
ch=myread(); //1
cha=myread(); //2
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark2_x=-1;
ch=(ch&0x1f)*mark2_x;
xx=curX+(chx+ch)*xRange/baseSize+0.5;
ch=myread(); //3
ch=((cha&0x3)<<4)+ch;
if(ch&0x20)mark2_y=-1;
ch=(ch&0x1f)*mark2_y;
yy=curY+(chy+ch)*yRange/baseSize+0.5;
b3_3(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 13:
mark1_x=1;
mark1_y=1;
mark2_x=1;
mark2_y=1;
mark3_x=1;
mark3_y=1;
xx=curX;yy=curY;
ch=myread();
if(ch&8)mark1_x=-1;
chx=ch=(ch&7)*mark1_x;
xx=curX+ch*xRange/baseSize+0.5;
ch=myread();
if(ch&8)mark1_y=-1;
chy=ch=(ch&7)*mark1_y;
yy=curY+ch*yRange/baseSize+0.5;
ch=myread();
if((ch)&8)mark2_x=-1;
chxx=ch=(ch&7)*mark2_x;
xx=curX+(chx+ch)*xRange/baseSize+0.5;
ch=myread();
if((ch)&8)mark2_y=-1;
chyy=ch=(ch&7)*mark2_y;
yy=curY+(chy+ch)*yRange/baseSize+0.5;
ch=myread();
if((ch)&8)mark3_x=-1;
ch=(ch&7)*mark3_x;
xx=curX+(chx+chxx+ch)*xRange/baseSize+0.5;
ch=myread();
if((ch)&8)mark3_y=-1;
ch=(ch&7)*mark3_y;
yy=curY+(chy+chyy+ch)*yRange/baseSize+0.5;
b3_4(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 14:
mark1_x=1;
mark1_y=1;
mark2_x=1;
mark2_y=1;
mark3_x=1;
mark3_y=1;
xx=curX;yy=curY;
ch=myread(); //1
cha=myread(); //2
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark1_x=-1;
chx=ch=(ch&0x1f)*mark1_x;
xx=curX+ch*xRange/baseSize+0.5;
ch=myread(); //3
ch=((cha&0x03)<<4)+ch;
if(ch&0x20)mark1_y=-1;
chy=ch=(ch&0x1f)*mark1_y;
yy=curY+ch*yRange/baseSize+0.5;
ch=myread(); //1
cha=myread(); //2
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark2_x=-1;
chxx=ch=(ch&0x1f)*mark2_x;
xx=curX+(chx+ch)*xRange/baseSize+0.5;
ch=myread(); //3
ch=((cha&0x03)<<4)+ch;
if(ch&0x20)mark2_y=-1;
chyy=ch=(ch&0x1f)*mark2_y;
yy=curY+(chy+ch)*yRange/baseSize+0.5;
ch=myread(); //1
cha=myread(); //2
ch=(ch<<2)+(cha>>2);
if(ch&0x20)mark3_x=-1;
ch=(ch&0x1f)*mark3_x;
xx=curX+(chx+chxx+ch)*xRange/baseSize+0.5;
ch=myread(); //3
ch=((cha&0x03)<<4)+ch;
if(ch&0x20)mark3_y=-1;
ch=(ch&0x1f)*mark3_y;
yy=curY+(chy+chyy+ch)*yRange/baseSize+0.5;
b3_4(xx,yy,COLOR);
curX=xx;curY=yy;
moveto(curX,curY);
break;
case 15: //Only read two bytes, no processing
readbyte();
readbyte();
break;
}
}
if((readcount<hz.length))lineto(bx,by); //Connect the last point of the last stroke with the starting point to close the stroke
getch();
X0+=xRange*168/baseSize+0.5; //Here 168 is more appropriate, but the reason has not been found
if(getmaxx()-X0<xRange*168/baseSize){
X0=0;
Y0+=yRange*168/baseSize+0.5;
}
if(getmaxy()-Y0<yRange*168/baseSize){
Y0=0;
X0=0;
getch();
cleardevice();
}
}
setcolor(oldcolor);
fp=0;
fclose(fpe);
fclose(fpfh);
fclose(fphz);
return;
}
unsigned char myread(void) //Each time it is called, a four-bit number of a byte is read, first read the low four bits, then read the high four bits
{
unsigned char a;
if(!next){ //When next is 0, read the low four bits, and when it is 1, read the high four bits
a=hzdata&0x0f;
next=1;
}
else{
a=(hzdata&0xf0)>>4;
readcount++;
next=0;
}
return a;
}
int readbyte(void)
{
int kk=0;
unsigned char a,b;
a=myread()<<4;
b=myread();
kk=(int)(b+a);
return kk;
}
void b3_4(int *x,int *y,unsigned long col) //Cubic B-spline curve, n can specify a constant
{ int n,*bx,*by,sign=1,j=0,k,i;
double a,b,c,d,dt,xx,f0_3,f_0,f_1,f_2;
n=xx=sqrt(((double)(x-x))*((double)(x-x))+
((double)(y-y))*((double)(y-y)))+
sqrt(((double)(x-x))*((double)(x-x))+
((double)(y-y))*((double)(y-y)))+
sqrt(((double)(x-x))*((double)(x-x))+
((double)(y-y))*((double)(y-y)));
if(xx>n) n++;
n*=2;
if(n==0) n=20;
do {
bx=(int *)malloc(n*sizeof(int));
by=(int *)malloc(n*sizeof(int));
if((!bx)||(!by)) {
/*printf("Memory Alloction Error!");*/
return;
}
dt=(double)1/(double)n;
a=((-1)*x+3*x-3*x+x);
b=3*(x-2*x+x);
c=3*(x-x);
d=x;
f0_3=6*a*dt*dt*dt;
f_2=f0_3+2*b*dt*dt;
f_1=f0_3/6+b*dt*dt+c*dt;
f_0=d;
bx=f_0+0.5;
for(i=0;i<n;i++) {
f_0+=f_1;
bx=f_0+0.5;
f_1+=f_2;
f_2+=f0_3;
}
a=((-1)*y+3*y-3*y+y);
b=3*(y-2*y+y);
c=3*(y-y);
d=y;
f0_3=6*a*dt*dt*dt;
f_2=f0_3+2*b*dt*dt;
f_1=f0_3/6+b*dt*dt+c*dt;
f_0=d;
bx=f_0+0.5;
for(i=0;i<n;i++) {
f_0+=f_1;
by=f_0+0.5;
f_1+=f_2;
f_2+=f0_3;
}
sign=1;j=0;k=n/100+1;
for(i=1;i<n;i++) {
if((abs(bx-bx)>1)||(abs(by-by)>1)) j++;
if(j>k) {
free(bx); free(by);
if(n>3) n=n+n/3;
else n=6;
sign=0;
} break;
}
} while(sign==0);
for(i=1;i<n;i++)
if((bx!=bx)||(by!=by)) putpixel(bx,by,col);
free(bx); free(by);
}
void b3_3(int *x,int *y,unsigned long col) //Quadratic B-spline curve, n can specify a constant
{ int n,*bx,*by,sign=1,j=0,k,i;
double a,b,c,d,dt,xx,f0_3,f_0,f_1,f_2;
n=xx=sqrt(((double)(x-x))*((double)(x-x))+
((double)(y-y))*((double)(y-y)))+
sqrt(((double)(x-x))*((double)(x-x))+
((double)(y-y))*((double)(y-y)));
if(xx>n) n++;
n*=4;
if(n==0) n=20;
do {
bx=(int *)malloc(n*sizeof(int));
by=(int *)malloc(n*sizeof(int));
if((!bx)||(!by)) {
/*printf("Memory Alloction Error!");*/
return;
}
dt=(double)1/(double)n;
b=x-2*x+x;
c=2*(x-x);
d=x;
f_2=2*b*dt*dt;
f_1=b*dt*dt+c*dt;
f_0=d;
bx=f_0+0.5;
for(i=0;i<n;i++) {
f_0+=f_1;
bx=f_0+0.5;
f_1+=f_2;
}
b=y-2*y+y;
c=2*(y-y);
d=y;
f_2=2*b*dt*dt;
f_1=b*dt*dt+c*dt;
f_0=d;
by=f_0+0.5;
for(i=0;i<n;i++) {
f_0+=f_1;
by=f_0+0.5;
f_1+=f_2;
}
sign=1;j=0;k=n/100+1;
for(i=1;i<n;i++) {
if((abs(bx-bx)>1)||(abs(by-by)>1)) j++;
if(j>k) {
free(bx); free(by);
if(n>3) n=n+n/3;
else n=6;
sign=0;
} break;
}
} while(sign==0);
for(i=1;i<n;i++)
if((bx!=bx)||(by!=by))
{
putpixel(bx,by,col);
}
free(bx); free(by);
}
Recent Ratings for This Post
( 1 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| DOSroot | +6 | 2009-04-17 04:07 |
