/* Bitmap is raw power !! */ /* Fundamental Font Handling Routines: Demo on mixing Guobiao bmp font CCLIB.16 & Eten Big5 bmp font STDFONTS.15 & ASCII font ASCFONT.15*/ /* Compatibility: run with Borland/Turbo C graphics library */ /* to port, change & related graphics function, the original font bmp should remain unchanged :-) */ /* the program first grabs a few char from CCLIB.16 and STDFONTS.15 display them in native format using MCGAHI graphics mode. it then waits for an integer, which is used for colour for next display in VGAHI mode. enter another integer to end note that one of the image is a mixed Guobiao & Big5 & ASCII */ /* to align mixed fonts: (height + bot + top) should be equal */ /* ASCII is 8x15 bitmap 1 column wide */ /* 16x16 bitmap is 2 column wide */ /* 24x24 bitmap is 3 column wide !! */ /* these parameters can be obtained from HBF files */ /* the font bmp assumes mono 1 plane bmp format */ /* when displayed convert to physical format */ /* note VGAHI = 4 plane, 1 bit/pixel */ /* MCGAHI = 1 plane, 1 bit/pixel == font bmp */ #include #include #include #include #include typedef struct fonts { char *path; char *name; FILE *fp; int size; char width; char height; char bot; char top; } font, *pfont; enum fontmsg { DEFAULT = 0, ERROR = -1 }; pfont *FONT; /* global variable used by font functions */ /* FONT is an array of pointers to font structure */ int initfont(char n); /* initialize n font */ int openfont(pfont fnt, char n); int setfont(pfont fnt); /* set current Font FONT[0] to *fnt */ void *readfont(long unsigned int start, long unsigned int n); /* returns bmp font read */ int *setimage(int x, int y, void *bmp); /* convert bmp to Borland bmp */ int *initimage(int x, int y); /* init a window */ int iputchar(int *img, int row, int col, char *bmp); /* put a char int Window img; bmp defines the char */ /* col unit 8 bits, row unit (top+height+bot) */ int icat(int *img1, int x, int y, int *img2); /* cat window img2 to window img1 */ int *plane4(int *img, unsigned char c); /* convert 1 plane bmp to 4 plane bmp */ /* c for colour */ main() { font GB, TW, US; char *fn; int gmode=MCGAHI, gdrv=MCGA; int *img, i, *img2, *img3; initfont(16); GB.path = "E:\\"; GB.name = "CCLIB.16"; GB.size = 32; GB.width = 2; GB.height = 16; GB.bot = 0; GB.top = 0; TW.path = "E:\\"; TW.name = "STDFONTS.15"; TW.size = 30; TW.width = 2; TW.height = 15; TW.bot = 4; TW.top = 3; US.path = "E:\\"; US.name = "ASCFONT.15"; US.size = 15; US.width = 1; US.height = 15; US.bot = 0; US.top = 0; if (openfont(&GB, 1)==ERROR) { printf("Can't open font!\n"); return -1; } openfont(&TW, 2); openfont(&US, 3); initgraph(&gdrv, &gmode, ""); line(200, 99, 400, 99); /* Draw reference lines */ line(299, 99, 299, 400); setfont(&GB); img = setimage(16, 160, readfont(1000, 10)); putimage(100, 100, img, COPY_PUT); setfont(&US); img = setimage(8, 150, readfont(0, 10)); putimage(80, 100, img, COPY_PUT); setfont(&TW); img2 = initimage(48, 66); iputchar(img2, 0, 0, readfont(11, 1)); iputchar(img2, 1, 0, readfont(12, 1)); iputchar(img2, 2, 1, readfont(1, 1)); putimage(200, 100, img2, COPY_PUT); setfont(&GB); img = initimage(160,80); iputchar(img,0,0,readfont(1110,1)); /* Chinese char is 2 column wide */ iputchar(img,0,1,readfont(1121,1)); /* this overlaps previous char */ iputchar(img,1,0,readfont(1132,1)); iputchar(img,1,5,readfont(1143,1)); iputchar(img,0,7,readfont(2232,1)); iputchar(img,1,7,readfont(2322,1)); setfont(&US); iputchar(img,0,3,readfont(65,1)); /* mixed font not aligned if */ iputchar(img,1,2,readfont(66,1)); /* (height + top + bot) unequal */ iputchar(img,0,6,readfont(67,1)); /* Inserting ASCII char */ setfont(&TW); iputchar(img,0,4,readfont(11,1)); iputchar(img,1,3,readfont(12,1)); iputchar(img,2,0,readfont(13,1)); iputchar(img,2,2,readfont(13,1)); putimage(300, 100, img, COPY_PUT); img3 = initimage(160, 160); /* cat img & img2 into img3 */ icat(img3, 0, 20, img); icat(img3, 60, 25, img2); putimage(200, 200, img3, OR_PUT); scanf("%d", &i); closegraph(); gmode = VGAHI; gdrv = VGA; initgraph(&gdrv, &gmode, ""); /* now shown in VGA 16 colour */ putimage(200, 200, plane4(img3, i), COPY_PUT); scanf("%d", &i); closegraph(); fcloseall(); } int openfont(pfont fnt, char n) { char s[255]; s[0]='\0'; strcat(s, fnt->path); strcat(s, fnt->name); fnt->fp = fopen(s, "r+b"); if (fnt->fp == NULL) return ERROR; FONT[n] = fnt; return 0; } int setfont(pfont fnt) { if (fnt==NULL) return ERROR; FONT[DEFAULT] = fnt; return 0; } int initfont(char n) { FONT = calloc(n, sizeof(pfont)); if (FONT==NULL) return ERROR; return 0; } void *readfont(long unsigned int start, long unsigned int n) /* this routine reads bmp font file */ /* can be modified to get bmp from Font Buffer instead */ /* modify it to rasterize TTF or others !! */ /* the rest of your business is BITMAP -- EASSSSYYYY !!!! */ { void *p; if (FONT[0] == NULL) return NULL; p = calloc(n, FONT[0]->size); fseek(FONT[0]->fp, (long) (start*FONT[0]->size), SEEK_SET); fread(p, FONT[0]->size, n, FONT[0]->fp); return p; } int *setimage(int x, int y, void *bmp) { int size, *img; size = x*y/8+2*sizeof(int); img = malloc(size); if (img==NULL) return NULL; img[0] = x-1; img[1] = y-1; memcpy((img+2), bmp, size); return img; } int *initimage(int x, int y) { int size, *img; size = x*y/8+2*sizeof(int); img = calloc(1, size); if (img==NULL) return NULL; img[0] = x-1; img[1] = y-1; return img; } int iputchar(int *img, int row, int col, char *bmp) { int base, offset, i; char *im; im = (char *) (img+2); if (FONT[0]==NULL) return ERROR; offset = (img[0]+1)/8; base = row*(FONT[0]->top+FONT[0]->height+FONT[0]->bot)*offset + FONT[0]->top*offset + col; for (i=0; iheight; i++) memcpy((im+base+i*offset), bmp+i*FONT[0]->width, FONT[0]->width); return 0; } int icat(int *img1, int x, int y, int *img2) { int base, offset, length, height, i; char *im1, *im2; im1 = (char *) (img1+2); im2 = (char *) (img2+2); offset = (img1[0]+1)/8; base = x/8+y*offset; height = img2[1]+1; length = (img2[0]+1)/8; for (i=0; i