#include #include #include #include #include #include "bmplib.h" Font::Font(string stfn) { const char *fn=stfn.c_str(); char fn1[100], fn2[100]; strcpy(fn1, "/usr/local/fonts/"); strcat(fn1, fn); for (int i=0; 1; i+=1) { char c=fn1[i]; if (c==0) break; if (c>='A' && c<='Z') fn1[i]=c+32; } strcpy(fn2, fn1); strcat(fn1, ".fom"); strcat(fn2, ".fon"); ok=0; baseline=0; int fom=open(fn1, O_RDONLY); int fon=open(fn2, O_RDONLY); for (int i=0; i<128; i+=1) font[i]=NULL; if (fom<0 || fon<0) { fprintf(stderr, "Can't find font '%s'\n", fn); return; } byte bb[20]; while (1) { int n=read(fom, bb, 10); if (n!=10) break; charinfo * ci= new charinfo; font[bb[0]]=ci; ci->width=bb[1]; ci->height=bb[2]; ci->ascent=bb[3]; baseline=bb[3]; ci->blanks=bb[4]; ci->rows=bb[5]; ci->bytesperrow=bb[6]; ci->offset=bb[7] | (bb[8]<<8) | (bb[9]<<16); int size=ci->rows*ci->bytesperrow; byte * pp = new byte[size]; if (size>0) { lseek(fon, ci->offset, SEEK_SET); read(fon, pp, size); } ci->pattern=pp; } close(fom); close(fon); ok=1; } Font::~Font(void) { for (int i=0; i<128; i+=1) { if (font[i]!=NULL) { delete[] font[i]->pattern; delete font[i]; } } } void Font::MeasureString(string st, int & w, int & h) { h=0; w=0; const char * s=st.c_str(); for (int i=0; 1; i+=1) { int c=s[i]; if (c==0) break; if (c<0 || c>127) continue; if (font[c]==NULL) continue; h=font[c]->height; w+=font[c]->width; } } Picture::Picture(int w, int h, int bgcol=0xFFFFFF) { int size=w*h; thefont=NULL; paper=new unsigned int[size]; if (paper==NULL) { paperw=0; paperh=0; paper=dummypaper; return; } for (int i=0; i=paperw || y>=paperh) return 0; return paper[y*paperw+x]; } void Picture::SetPixel(int x, int y, int c) { if (x<0 || y<0 || x>=paperw || y>=paperh) return; paper[y*paperw+x]=c; } void Picture::SetPixel(int x, int y) { if (x<0 || y<0 || x>=paperw || y>=paperh) return; paper[y*paperw+x]=curcol; } void Picture::FillRectangle(int x, int y, int w, int h, int c) { if (x<0) { w+=x; x=0; } if (y<0) { h+=y; y=0; } int maxx=x+w, maxy=y+h; if (maxx>paperw) maxx=paperw; if (maxy>paperh) maxy=paperh; for (int i=y; ipaperw) maxx=paperw; if (maxy>paperh) maxy=paperh; w=maxx-x; h=maxy-y; if (w<0) w=0; if (h<0) h=0; int p0=y*paperw; for (int j=x; j127) return 0; charinfo * ci=thefont->font[c]; if (ci==NULL) return 0; int rr=ci->rows, cc=ci->bytesperrow; byte * pp=ci->pattern; for (int r=0; rblanks; for (int c=0; cwidth; } int Picture::DrawString(int x, int y, string st) { const char *s=st.c_str(); int len=0; for (int i=0; 1; i+=1) { char c=s[i]; if (c==0) break; len+=DrawChar(x+len, y, c); } return len; } struct BMPHeader { short int rubbish; char letterB; char letterM; int filesize; int reserved; int dataoffset; }; struct BMPInfoHeader { int forty; int width; int height; short int planes; short int bitsperpixel; int compression; int imagesize; int xpixelspermetre; int ypixelspermetre; int numcolours; int numimportantcolours; }; int Picture::SaveAsBMP(string fname) { FILE *f=fopen(fname.c_str(), "w"); if (f==NULL) return 0; int x=0, y=0, w=paperw, h=paperh; BMPHeader h1; h1.letterB='B'; h1.letterM='M'; int bytesperline=3*w; int extraperline=(4-(bytesperline&3))&3; bytesperline+=extraperline; int filesize=54+bytesperline*h; int extrafile=(4-(filesize&3))&3; h1.filesize=filesize+extrafile; h1.reserved=0; h1.dataoffset=54; fwrite(&h1.letterB, 14, 1, f); BMPInfoHeader h2; h2.forty=40; h2.width=w; h2.height=h; h2.planes=1; h2.bitsperpixel=24; h2.compression=0; h2.imagesize=0; h2.xpixelspermetre=3936; h2.ypixelspermetre=3936; h2.numcolours=0; h2.numimportantcolours=0; fwrite(&h2, 40, 1, f); for (int yc=y+h-1; yc>=y; yc-=1) { for (int xc=x; xc>16)|(c&0xFF00)|((c&0xFF)<<16); fwrite(&v, 3, 1, f); } if (extraperline>0) { int n=0; fwrite(&n, extraperline, 1, f); } } if (extrafile>0) { int n=0; fwrite(&n, extrafile, 1, f); } fclose(f); return 1; }