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

📄 font2bdf.c

📁 实现TTF/TTC到bdf格式的批量转换。 本来是为了依照网上的帖子把WINDOWS字库转换过来
💻 C
字号:
/* Copyright (C) 2004 Zachary Smith *//* This software is free for non-commercial use only. */
/* Re-programed by Kuang Yujun (2009-05-19) */#include <stdio.h>
#include <stdlib.h>#include <string.h>
#include <direct.h>#define FAIL 1
#define SUCC 0

#define FileListBufferBufferSize 1024
typedef struct{
	// number for BigLoop
	int number; 
	// Filenames are stored in chain as [name1 \0 name2 \0 ....]
	char FileListBuffer[FileListBufferBufferSize]; 
} sDirChain;

// Called by BigLoop() to convert one file
int font2bdf(FILE *fin, FILE *fout); 

// Called by convertCurrentDir() to convert a group of files, whose names fills the FileListBufferBufferSize
int BigLoop(sDirChain *FileListBuffer);

// Called by main() to convert all TTF/TTC file in current directory to BDF format
int convertCurrentDir();

int main(int argc, char **argv){	
	switch(argc)	{	case 1: // Process ttf and ttc files in current working directory
		{
			convertCurrentDir();			break;
		}
	default: // Process ttf and ttc files in directory given by argv[1]
		{
			char oldPWD[1024];
			if(!_getcwd(oldPWD, sizeof(oldPWD))){ // Store current PWD.
				perror("Cannot get current PWD");
				printf("Program abort.\n");
				return FAIL;
			}
			if(chdir(argv[1])){                   // Change to new PWD
				perror("Cannot change to specified dir.");
				printf("Program abort.\n");
				return FAIL;
			}
			else{
				printf("Entering [%s].\n", argv[1]);
				convertCurrentDir();              // convert TTF/TTC to BDF in new PWD
				chdir(oldPWD);                    // Go back to old PWD.
				printf("Leaving [%s] back to [%s].\n", argv[1], oldPWD);
			}
		}
		break;
	}		return SUCC;}

int convertCurrentDir()
{
	int totalFile; // In each group the filelist.number is reset, so we define this to track all the history.
	char *ptr;
	FILE *fDIR;
	sDirChain filelist;

	system("dir *.ttf *.ttc /b> font2bdf.temp"); // Simply use system command to get FileListBuffer
	fDIR = fopen("font2bdf.temp", "rt");
	
	totalFile=0;
	if(!fDIR){
		perror("dir command failure, no temp file found. Stop.\n");
	}
	else{
		
		ptr=(char*)&filelist.FileListBuffer;
		filelist.number=0;
		while(!feof(fDIR)){
			char *p;
			char buffer[300];
			p=fgets(buffer, 125, fDIR);
			// In debug version, for some size of filelist.FileListBuffer, the fgets goes wrong
			// to read nothing while EOF not reached; however, release version are definitely OK
			// The reason for this may be a bug of MSVC, who knows?!
			if(!p) break;

			// To avoid FileListBufferBuffer overflow we convert the files group by group
			// When each group converted, the ptr and filelist.number are reset to their base
			if(ptr+strlen(buffer)>(filelist.FileListBuffer+sizeof(filelist.FileListBuffer))){
				BigLoop(&filelist);
				printf("  [%d] files converted into BDF format.\n", filelist.number);
				totalFile+=filelist.number;
				ptr=(char*)&filelist.FileListBuffer;
				filelist.number=0;
			}
			strcpy(ptr, buffer);
			if(p){
				p=strchr(ptr, '\n'); if(p) *p=0;
				ptr+=strlen(ptr)+1;
				filelist.number++;
			}
			else{
				if(!filelist.number){
					printf("Temp is empty? No TTF/TTC file found.");
					return FAIL;
				}
				else break;
			}
		}

		if(filelist.number){
			BigLoop(&filelist); // Convert the last group
			printf("  [%d] files converted into BDF format.\n", filelist.number);
			totalFile+=filelist.number;
		}
		printf("[%d] font files found in total.\n", totalFile);
		fclose(fDIR); system("del font2bdf.temp/f/q"); // Close and delete the temp file
	}
	return totalFile;
}
#define STR "STARTFONT 2.1\n\
FONT %s\n\
SIZE 12 75 75\n\
FONTBOUNDINGBOX 8 16 0 0\n\
COMMENT  %s\n\
COMMENT -\n\
STARTPROPERTIES 20\n\
FOUNDRY \"misc\"\n\
FAMILY_NAME \"%s\"\n\
WEIGHT_NAME \"Medium\"\n\
SLANT \"R\"\n\
SETWIDTH_NAME \"Normal\"\n\
ADD_STYLE_NAME \"\"\n\
PIXEL_SIZE 12\n\
POINT_SIZE 120\n\
RESOLUTION_X 75\n\
RESOLUTION_Y 75\n\
SPACING \"M\"\n\
CHARSET_REGISTRY \"ISO8859\"\n\
CHARSET_ENCODING \"1\"\n\
FONT_ASCENT 13\n\
FONT_DESCENT 3\n\
FACE_NAME \"%s\"\n\
COPYRIGHT \"UESTC\"\n\
DEFAULT_CHAR 32\n\
CHARSET_COLLECTIONS \"ASCII ISO8859-1 ADOBE-STANDARD\"\n\
FULL_NAME \"%s\"\n\
ENDPROPERTIES\n\
CHARS 224\n"
int BigLoop(sDirChain *filelist)
{
	// This function only handles the files listed in sDirChain struct.
	int i;
	FILE *fin, *fout;
	char *ptr, * s2, * string;
	
	ptr=(char*)filelist->FileListBuffer;
	for(i=0; i<filelist->number; i++){
		char buffer[500];
		
		strcpy(buffer, ptr);
		string=buffer;
		
		fin = fopen (string,"rb");
		if (fin){
			s2 = strrchr (string, '.');
			if (s2){
				strcpy(s2+1, "bdf");
				fout=fopen(string, "wt");
				if(fout){
					*s2 = 0;
					printf("  Converting [%s] into [%s.bdf]...\r", ptr, string);

					// The following two line block is from Zachary Smith
					// I donot know how it works, but it DO works well.
					fprintf (fout, STR, string, string, string, string, string);
					font2bdf(fin, fout);

					fclose(fin);
					fclose(fout);
					printf("  Converting [%s] into [%s.bdf]. OK.\n", ptr, string);
				}
			}
		}
		ptr+=strlen(ptr)+1;
	}
	return filelist->number;
}

int font2bdf(FILE *fin, FILE *fout)
{
	// This part is from Zachary Smith.
	// Though it is very basic, the original version is not easy to use
	// And the idea behind it is not my concern, so comments are not added.
	unsigned char buf[4096];
	int i, j;
	
	if (4096 !=fread (buf, 1, 4096, fin))
		return FAIL;
	
	for(i=32; i<256; i++) {
		fprintf (fout, "STARTCHAR %02x\n", i);
		fprintf (fout, "ENCODING %d\n", i);
		fprintf (fout, "SWIDTH 600 0\n");
		fprintf (fout, "DWIDTH 8 0\n");
		fprintf (fout, "BBX 8 16 0 -3\n");
		fprintf (fout, "BITMAP\n");
		
		for (j=0; j<16; j++) 
			fprintf (fout, "%02x\n", (int)buf[(i*16)+j]);
		
		fprintf (fout, "ENDCHAR\n");
	}
	fprintf (fout, "ENDFONT\n");
	return SUCC;
}

⌨️ 快捷键说明

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