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

📄 convbdf.c

📁 the embedded GUI for SamSung s3c2410 cpu based board.is microwindows0.90
💻 C
📖 第 1 页 / 共 2 页
字号:
		}		if (isprefix(buf, "DWIDTH ")) {			if (sscanf(buf, "DWIDTH %d", &width) != 1) {				fprintf(stderr, "Error: bad 'DWIDTH'\n");				return 0;			}			/* use font boundingbox width if DWIDTH <= 0*/			if (width <= 0)				width = pf->fbbw - pf->fbbx;			continue;		}		if (isprefix(buf, "BBX ")) {			if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {				fprintf(stderr, "Error: bad 'BBX'\n");				return 0;			}			continue;		}		if (strequal(buf, "BITMAP")) {			MWIMAGEBITS *ch_bitmap = pf->bits + ofs;			int ch_words;			if (encoding < 0)				continue;			/* set bits offset in encode map*/			if (pf->offset[encoding-pf->firstchar] != (unsigned long)-1) {				fprintf(stderr, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n",					encoding, encoding);				continue;			}			pf->offset[encoding-pf->firstchar] = ofs;			/* calc char width*/			if (bbx < 0) {				width -= bbx;				/*if (width > maxwidth)					width = maxwidth;*/				bbx = 0;			}			if (width > maxwidth)				maxwidth = width;			pf->width[encoding-pf->firstchar] = width;			/* clear bitmap*/			memset(ch_bitmap, 0, MWIMAGE_BYTES(width) * pf->height);			ch_words = MWIMAGE_WORDS(width);#define BM(row,col)	(*(ch_bitmap + ((row)*ch_words) + (col)))#define MWIMAGE_NIBBLES	(MWIMAGE_BITSPERIMAGE/4)			/* read bitmaps*/			for (i=0; ; ++i) {				int hexnibbles;				if (!bdf_getline(fp, buf, sizeof(buf))) {					fprintf(stderr, "Error: EOF reading BITMAP data\n");					return 0;				}				if (isprefix(buf, "ENDCHAR"))					break;				hexnibbles = strlen(buf);				for (k=0; k<ch_words; ++k) {					int ndx = k * MWIMAGE_NIBBLES;					int padnibbles = hexnibbles - ndx;					MWIMAGEBITS value;										if (padnibbles <= 0)						break;					if (padnibbles >= MWIMAGE_NIBBLES)						padnibbles = 0;					value = bdf_hexval((unsigned char *)buf,						ndx, ndx+MWIMAGE_NIBBLES-1-padnibbles);					value <<= padnibbles * MWIMAGE_NIBBLES;					BM(pf->height - pf->descent - bby - bbh + i, k) |=						value >> bbx;					/* handle overflow into next image word*/					if (bbx) {						BM(pf->height - pf->descent - bby - bbh + i, k+1) =							value << (MWIMAGE_BITSPERIMAGE - bbx);					}				}			}			ofs += MWIMAGE_WORDS(width) * pf->height;			continue;		}		if (strequal(buf, "ENDFONT"))			break;	}	/* set max width*/	pf->maxwidth = maxwidth;	/* change unused offset/width values to default char values*/	for (i=0; i<pf->size; ++i) {		int defchar = pf->defaultchar - pf->firstchar;		if (pf->offset[i] == (unsigned long)-1) {			pf->offset[i] = pf->offset[defchar];			pf->width[i] = pf->width[defchar];		}	}	/* determine whether font doesn't require encode table*/	l = 0;	for (i=0; i<pf->size; ++i) {		if (pf->offset[i] != l) {			encodetable = 1;			break;		}		l += MWIMAGE_WORDS(pf->width[i]) * pf->height;	}	if (!encodetable) {		free(pf->offset);		pf->offset = NULL;	}	/* determine whether font is fixed-width*/	for (i=0; i<pf->size; ++i) {		if (pf->width[i] != maxwidth) {			proportional = 1;			break;		}	}	if (!proportional) {		free(pf->width);		pf->width = NULL;	}	/* reallocate bits array to actual bits used*/	if (ofs < pf->bits_size) {		pf->bits = realloc(pf->bits, ofs * sizeof(MWIMAGEBITS));		pf->bits_size = ofs;	} else if (ofs > pf->bits_size) {		fprintf(stderr, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");		if (ofs > pf->bits_size+EXTRA) {			fprintf(stderr, "Error: Not enough bits initially allocated\n");			return 0;		}		pf->bits_size = ofs;	}	return 1;}/* read the next non-comment line, returns buf or NULL if EOF*/char *bdf_getline(FILE *fp, char *buf, int len){	int c;	char *b;	for (;;) {		b = buf;		while ((c = getc(fp)) != EOF) {			if (c == '\r')				continue;			if (c == '\n')				break;			if (b - buf >= (len - 1))				break;			*b++ = c;		}		*b = '\0';		if (c == EOF && b == buf)			return NULL;		if (b != buf && !isprefix(buf, "COMMENT"))			break;	}	return buf;}/* return hex value of portion of buffer*/MWIMAGEBITSbdf_hexval(unsigned char *buf, int ndx1, int ndx2){	MWIMAGEBITS val = 0;	int i, c;	for (i=ndx1; i<=ndx2; ++i) {		c = buf[i];		if (c >= '0' && c <= '9')			c -= '0';		else if (c >= 'A' && c <= 'F')			c = c - 'A' + 10;		else if (c >= 'a' && c <= 'f')			c = c - 'a' + 10;		else c = 0;		val = (val << 4) | c;	}	return val;}/* generate C source from in-core font*/intgen_c_source(PMWCFONT pf, char *path){	FILE *ofp;	int i;	int did_defaultchar = 0;	int did_syncmsg = 0;	time_t t = time(0);	MWIMAGEBITS *ofs = pf->bits;	char buf[256];	char obuf[256];	char hdr1[] = {		"/* Generated by convbdf on %s. */\n"		"#include \"device.h\"\n"		"\n"		"/* Font information:\n"		"   name: %s\n"		"   facename: %s\n"		"   w x h: %dx%d\n"		"   size: %d\n"		"   ascent: %d\n"		"   descent: %d\n"		"   first char: %d (0x%02x)\n"		"   last char: %d (0x%02x)\n"		"   default char: %d (0x%02x)\n"		"   proportional: %s\n"		"   %s\n"		"*/\n"		"\n"		"/* Font character bitmap data. */\n"		"static MWIMAGEBITS _%s_bits[] = {\n"	};	ofp = fopen(path, "w");	if (!ofp) {		fprintf(stderr, "Can't create %s\n", path);		return 1;	}	fprintf(stderr, "Generating %s\n", path);	strcpy(buf, ctime(&t));	buf[strlen(buf)-1] = 0;	fprintf(ofp, hdr1, buf, 		pf->name,		pf->facename? pf->facename: "",		pf->maxwidth, pf->height,		pf->size,		pf->ascent, pf->descent,		pf->firstchar, pf->firstchar,		pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,		pf->defaultchar, pf->defaultchar,		pf->width? "yes": "no",		pf->copyright? pf->copyright: "",		pf->name);	/* generate bitmaps*/	for (i=0; i<pf->size; ++i) {		int x;		int bitcount = 0; 		int width = pf->width ? pf->width[i] : pf->maxwidth;		int height = pf->height;		MWIMAGEBITS *bits = pf->bits + (pf->offset? pf->offset[i]: (height * i));		MWIMAGEBITS bitvalue;		/*		 * Generate bitmap bits only if not this index isn't		 * the default character in encode map, or the default		 * character hasn't been generated yet.		 */		if (pf->offset && (pf->offset[i] == pf->offset[pf->defaultchar-pf->firstchar])) {			if (did_defaultchar)				continue;			did_defaultchar = 1;		}		fprintf(ofp, "\n/* Character %d (0x%02x):\n   width %d",			i+pf->firstchar, i+pf->firstchar, width);		if (gen_map) {			fprintf(ofp, "\n   +");			for (x=0; x<width; ++x) fprintf(ofp, "-");			fprintf(ofp, "+\n");			x = 0;			while (height > 0) {				if (x == 0) fprintf(ofp, "   |");				if (bitcount <= 0) {					bitcount = MWIMAGE_BITSPERIMAGE;					bitvalue = *bits++;				}				fprintf(ofp, MWIMAGE_TESTBIT(bitvalue)? "*": " ");				bitvalue = MWIMAGE_SHIFTBIT(bitvalue);				--bitcount;				if (++x == width) {					fprintf(ofp, "|\n");					--height;					x = 0;					bitcount = 0;				}			}			fprintf(ofp, "   +");			for (x=0; x<width; ++x) fprintf(ofp, "-");			fprintf(ofp, "+ */\n");		} else			fprintf(ofp, " */\n");		bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));		for (x=MWIMAGE_WORDS(width)*pf->height; x>0; --x) {			fprintf(ofp, "0x%04x,\n", *bits);			if (!did_syncmsg && *bits++ != *ofs++) {				fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");				did_syncmsg = 1;			}		}		}	fprintf(ofp, 	"};\n\n");	if (pf->offset) {		/* output offset table*/		fprintf(ofp, "/* Character->glyph mapping. */\n"			"static unsigned long _%s_offset[] = {\n",			pf->name);		for (i=0; i<pf->size; ++i)			fprintf(ofp, "  %ld,\t/* (0x%02x) */\n", pf->offset[i], i+pf->firstchar);		fprintf(ofp, "};\n\n");	}	/* output width table for proportional fonts*/	if (pf->width) {		fprintf(ofp, 	"/* Character width data. */\n"			"static unsigned char _%s_width[] = {\n",			pf->name);		for (i=0; i<pf->size; ++i)			fprintf(ofp, "  %d,\t/* (0x%02x) */\n", pf->width[i], i+pf->firstchar);		fprintf(ofp, "};\n\n");	}	/* output MWCFONT struct*/	if (pf->offset)		sprintf(obuf, "_%s_offset,", pf->name);	else sprintf(obuf, "0,  /* no encode table*/");	if (pf->width)		sprintf(buf, "_%s_width,", pf->name);	else sprintf(buf, "0,  /* fixed width*/");	fprintf(ofp, 	"/* Exported structure definition. */\n"		"MWCFONT font_%s = {\n"		"  \"%s\",\n"		"  %d,\n"		"  %d,\n"		"  %d,\n"		"  %d,\n"		"  %d,\n"		"  _%s_bits,\n"		"  %s\n"		"  %s\n"		"  %d,\n"		"  sizeof(_%s_bits)/sizeof(MWIMAGEBITS),\n"		"};\n",		pf->name, pf->name,		pf->maxwidth, pf->height,		pf->ascent,		pf->firstchar,		pf->size,		pf->name,		obuf,		buf,		pf->defaultchar,		pf->name);	return 0;}static intWRITEBYTE(FILE *fp, unsigned char c){	return putc(c, fp) != EOF;}static intWRITESHORT(FILE *fp, unsigned short s){	putc(s, fp);	return putc(s>>8, fp) != EOF;}static intWRITELONG(FILE *fp, unsigned long l){	putc(l, fp);	putc(l>>8, fp);	putc(l>>16, fp);	return putc(l>>24, fp) != EOF;}static intWRITESTR(FILE *fp, char *str, int count){	return fwrite(str, 1, count, fp) == count;}static intWRITESTRPAD(FILE *fp, char *str, int totlen){	int ret;		while (str && *str && totlen > 0)		if (*str) {			ret = putc(*str++, fp);			--totlen;		}	while (--totlen >= 0)		ret = putc(' ', fp);	return ret;}/* generate .fnt format file from in-core font*/intgen_fnt_file(PMWCFONT pf, char *path){	FILE *ofp;	int i;	ofp = fopen(path, "wb");	if (!ofp) {		fprintf(stderr, "Can't create %s\n", path);		return 1;	}	fprintf(stderr, "Generating %s\n", path);	/* write magic and version #*/	WRITESTR(ofp, VERSION, 4);	/* internal font name*/	WRITESTRPAD(ofp, pf->name, 64);	/* copyright*/	WRITESTRPAD(ofp, pf->copyright, 256);	/* font info*/	WRITESHORT(ofp, pf->maxwidth);	WRITESHORT(ofp, pf->height);	WRITESHORT(ofp, pf->ascent);	WRITESHORT(ofp, 0);	WRITELONG(ofp, pf->firstchar);	WRITELONG(ofp, pf->defaultchar);	WRITELONG(ofp, pf->size);	/* variable font data sizes*/	WRITELONG(ofp, pf->bits_size);		  /* # words of MWIMAGEBITS*/	WRITELONG(ofp, pf->offset? pf->size: 0);  /* # longs of offset*/	WRITELONG(ofp, pf->width? pf->size: 0);	  /* # bytes of width*/	/* variable font data*/	for (i=0; i<pf->bits_size; ++i)		WRITESHORT(ofp, pf->bits[i]);        if (ftell(ofp) & 2)		WRITESHORT(ofp, 0);		/* pad to 32-bit boundary*/	if (pf->offset)		for (i=0; i<pf->size; ++i)			WRITELONG(ofp, pf->offset[i]);	if (pf->width)		for (i=0; i<pf->size; ++i)			WRITEBYTE(ofp, pf->width[i]);	fclose(ofp);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -