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

📄 ps_util.c

📁 PSlib是一个用来生成PostScript文件的类库。提供了一个生成PostScript文件的简单方法。
💻 C
📖 第 1 页 / 共 2 页
字号:
		for(i=0; i<256; i++) {			if(strlen(enc->vec[i]) > 0)				ght_insert(hashvec, (char *) i+1, strlen(enc->vec[i])+1, enc->vec[i]);		}	}	return(hashvec);}/* }}} *//* ps_build_enc_from_font() {{{ * Builds the hash table with a font encoding from the font itself. * This will only include those glyphs which have a number > 0. */ght_hash_table_t *ps_build_enc_from_font(PSDoc *psdoc, ADOBEFONTMETRIC *metrics) {	ght_hash_table_t *hashvec;	hashvec = ght_create(512);	if(hashvec) {		ght_iterator_t iterator;		char *p_key;		ADOBEINFO *p_e;		ght_set_alloc(hashvec, ps_ght_malloc, ps_ght_free, psdoc);		for(p_e = ght_first(metrics->gadobechars, &iterator, (void **) &p_key); p_e; p_e = ght_next(metrics->gadobechars, &iterator, (void **) &p_key)) {			/* Do not add glyphs with a number <= 0 */			if(p_e->adobenum > 0) {				if(0 > ght_insert(hashvec, (char *) p_e->adobenum+1, strlen(p_e->adobename)+1, p_e->adobename))				fprintf(stderr, "Could not insert entry %d->%s into font encoding hash table.\n", p_e->adobenum, p_e->adobename);			}		}	}	return(hashvec);}/* }}} *//* ps_build_enc_vector() {{{ * Builds the encoding vector from the hash table */ENCODING *ps_build_enc_vector(PSDoc *psdoc, ght_hash_table_t *hashvec) {	if(hashvec) {		ght_iterator_t iterator;		char *glyphname;		int i;		ENCODING *enc;		enc = (ENCODING *) psdoc->malloc(psdoc, sizeof(ENCODING), _("Allocate memory for new encoding vector.")) ;		if(NULL == enc) {			ps_error(psdoc, PS_MemoryError, _("Could not allocate memory for encoding vector."));			return NULL;		}		memset(enc, 0, sizeof(ENCODING));		for(i = (int) ght_first(hashvec, &iterator, (void **) &glyphname); i; i = (int) ght_next(hashvec, &iterator, (void **) &glyphname)) {//			printf("got %s = %d\n", glyphname, i-1);			enc->vec[i-1] = ps_strdup(psdoc, glyphname);		}		return(enc);	} else {		return NULL;	}}/* }}} *//* ps_list_fontenc() {{{ *  */void ps_list_fontenc(PSDoc *psdoc, ght_hash_table_t *hashvec) {	if(hashvec) {		ght_iterator_t iterator;		char *glyphname;		int i;		fprintf(stderr, "---- Font encoding vector -----\n");		fprintf(stderr, "Has %d entries.\n", ght_size(hashvec));		for(i = (int) ght_first(hashvec, &iterator, (void **) &glyphname); i; i = (int) ght_next(hashvec, &iterator, (void **) &glyphname)) {			fprintf(stderr, "%s = %d\n", glyphname, i-1);		}	}}/* }}} *//* ps_free_enc_vector() {{{ * Frees all memory allocated for an encoding vector including the vector * itself. */void ps_free_enc_vector(PSDoc *psdoc, ENCODING *enc) {	int i;	if(!enc)		return;	if(enc->name)		psdoc->free(psdoc, enc->name);	for(i=0; i<256; i++) {		if(enc->vec[i])			psdoc->free(psdoc, enc->vec[i]);	}	psdoc->free(psdoc, enc);}/* }}} *//* ps_fontenc_code() {{{ * Returns the index in the fontencoding vector */unsigned char ps_fontenc_code(PSDoc *psdoc, ght_hash_table_t *fontenc, char *adobename) {	if(fontenc) {		int code;		code = (int) ght_get(fontenc, strlen(adobename)+1, (void *) adobename);		if(code)			return((unsigned char) code -1);		else {			ps_error(psdoc, PS_Warning, _("The font encoding vector does not contain the glyph '%s'. Using '?' instead."), adobename);			return '?';		}	} else {		return '?';	}}/* }}} *//* ps_fontenc_has_glpyh() {{{ * checks of a glyph is in the fontencoding vector */int ps_fontenc_has_glyph(PSDoc *psdoc, ght_hash_table_t *fontenc, char *adobename) {	if(fontenc) {		int code;		code = (int) ght_get(fontenc, strlen(adobename)+1, (void *) adobename);		if(code)			return ps_true;		else {			return ps_false;		}	} else {		return ps_false;	}}/* }}} *//* ps_inputenc_name() {{{ * Returns the adobe name for a char in the input encoding vector */char *ps_inputenc_name(PSDoc *psdoc, unsigned char c) {	if(psdoc->inputenc) {		return(psdoc->inputenc->vec[c]);	} else {		return NULL;	}}/* }}} *//* ps_get_bool_parameter() {{{ */int ps_get_bool_parameter(PSDoc *psdoc, const char *name, int deflt) {	const char *onoffstr;	onoffstr = PS_get_parameter(psdoc, name, 0);	if(NULL == onoffstr) {		return(deflt);	} else {		if(!strcmp(onoffstr, "true"))			return(1);		else			return(0);	}}/* }}} *//* ps_open_file_in_path() {{{ */FILE *ps_open_file_in_path(PSDoc *psdoc, const char *filename) {	FILE *fp;	PS_RESOURCE **pathres;	int count;	int i;	char buffer[255];	if(fp = fopen(filename, "rb"))		return fp;	if(NULL != (pathres = ps_get_resources(psdoc, "SearchPath", &count))) {		for(i=count-1; i>=0; i--) {#ifdef HAVE_SNPRINTF			snprintf(buffer, 255, "%s/%s", pathres[i]->value, filename);#else			sprintf(buffer, "%s/%s", pathres[i]->value, filename);#endif			fprintf(stderr, "Searching for %s\n", buffer);			if(fp = fopen(buffer, "rb")) {				fprintf(stderr, "found %s in %s\n", filename, pathres[i]->value);				break;			}		}		psdoc->free(psdoc, pathres);		if(fp)			return(fp);	}#ifdef PACKAGE_DATA_DIR#ifdef HAVE_SNPRINTF	snprintf(buffer, 255, "%s/%s", PACKAGE_DATA_DIR, filename);#else	sprintf(buffer, "%s/%s", PACKAGE_DATA_DIR, filename);#endif	if(fp = fopen(buffer, "rb"))		return fp;#endif	return NULL;}/* }}} */int pow85[5] = {1, 85, 85*85, 85*85*85, 85*85*85*85};	/* ps_ascii85_encode() {{{ */void ps_ascii85_encode(PSDoc *psdoc, char *data, size_t len) {	unsigned long buffer;	unsigned char c;	int i, count, cc;	buffer = 0;	count = 0;	cc = 0;	while(count <= len-4) {//		buffer = ((long) data[count] << 24) + ((long) data[count+1] << 16) + ((long) data[count+2] << 8) + (long) data[count+3];		buffer |= ((unsigned char) data[count] << 24);		buffer |= ((unsigned char) data[count+1] << 16);		buffer |= ((unsigned char) data[count+2] << 8);		buffer |= ((unsigned char) data[count+3]);		if(buffer == 0) {			ps_putc(psdoc, 'z');			cc++;		} else {			for(i=4; i>=0; i--) {				c = (unsigned int) buffer / pow85[i];				ps_putc(psdoc, c+'!');				buffer = (unsigned int) buffer % pow85[i];			}			cc += 4;		}		count += 4;		if(cc > 55) {			ps_putc(psdoc, '\n');			cc = 0;		}	}	if(len-count) {		buffer = 0;		for(i=0; i<len-count; i++)			buffer = (buffer << 8) + (long) data[count+i];		buffer = buffer << ((4-len+count)*8);		for(i=4; i>=4-len+count; i--) {			c = (unsigned int) buffer / pow85[i];			ps_putc(psdoc, c+'!');			buffer = (unsigned int) buffer % pow85[i];		}	}	ps_putc(psdoc, '~');	ps_putc(psdoc, '>');}/* }}} *//* ps_asciihex_encode() {{{ */void ps_asciihex_encode(PSDoc *psdoc, char *data, size_t len) {	int i, cc=0;	unsigned char *dataptr;	dataptr = data;	for(i=0; i<len; i++) {		ps_printf(psdoc, "%02x", *dataptr);		dataptr ++;		cc++;		if(cc > 35 && i < (len-1)) {			ps_printf(psdoc, "\n");			cc = 0;		}	}	ps_putc(psdoc, '\n');	ps_putc(psdoc, '>');}/* }}} *//* ps_parse_optlist() {{{ */ght_hash_table_t *ps_parse_optlist(PSDoc *psdoc, const char *optstr) {	int i, isname;	char name[100], value[100], delim;	const char *optstrptr;	ght_hash_table_t *opthash;	if(optstr == NULL || optstr[0] == '\0')		return(NULL);	opthash = ght_create(30);	if(opthash) {		ght_set_alloc(opthash, ps_ght_malloc, ps_ght_free, psdoc);		isname = 1;		optstrptr = optstr;		name[0] = '\0';		value[0] = '\0';		/* Skip leading spaces */		while(*optstrptr == ' ')			optstrptr++;		while(*optstrptr != '\0') {			if(isname) {				i = 0;				while(*optstrptr != '\0' && *optstrptr != ' ') {					name[i] = *optstrptr;					i++;					optstrptr++;				}				name[i] = '\0';				isname = 0;				optstrptr++;			} else {				if(*optstrptr == '{') {					delim = '}';					optstrptr++;				} else {					delim = ' ';				}				i = 0;				while(*optstrptr != '\0' && *optstrptr != delim) {					value[i] = *optstrptr;					i++;					optstrptr++;				}				value[i] = '\0';				isname = 1;				optstrptr++;				/* Value and Name is read, insert it into hash */				if(name[0]) {					ght_insert(opthash, (char *) ps_strdup(psdoc, value), strlen(name)+1, name);					name[0] = '\0';					value[0] = '\0';				}			}			while(*optstrptr != '\0' && *optstrptr == ' ')				optstrptr++;		}	}	return(opthash);}/* }}} *//* get_optlist_element_as_float() {{{ * Retrieves an element from the hash and converts its value into float * Modifies *value only if the conversion was successful. * Returns -1 if the parameter is not available and -2 if it is available * but the value cannot be retrieved. */int get_optlist_element_as_float(PSDoc *psdoc, ght_hash_table_t *opthash, const char *name, float *value) {	float tmp;	char *strval;	char *endval;	if(NULL == opthash) {		return(-1);	}	if(NULL == (strval = ght_get(opthash, strlen(name)+1, (void *) name))) {		return(-1);	}	tmp = (float) strtod(strval, &endval);	if(endval == strval) {		return(-2);	}	*value = tmp;	return(0);} /* }}} *//* get_optlist_element_as_int() {{{ * Retrieves an element from the hash and converts its value into integer * Modifies *value only if the conversion was successful. * Returns -1 if the parameter is not available and -2 if it is available * but the value cannot be retrieved. */int get_optlist_element_as_int(PSDoc *psdoc, ght_hash_table_t *opthash, const char *name, int *value) {	long tmp;	char *strval;	char *endval;	if(NULL == opthash) {		return(-1);	}	if(NULL == (strval = ght_get(opthash, strlen(name)+1, (void *) name))) {		return(-1);	}	tmp = strtol(strval, &endval, 10);	if(endval == strval) {		return(-2);	}	*value = tmp;	return(0);} /* }}} *//* get_optlist_element_as_bool() {{{ * Retrieves an element from the hash and converts its value into boolean * Returns -1 if the parameter is not available and -2 if it is available * but the value cannot be retrieved. */int get_optlist_element_as_bool(PSDoc *psdoc, ght_hash_table_t *opthash, const char *name, ps_bool *value) {	char *strval;	if(NULL == opthash) {		return(-1);	}	if(NULL == (strval = ght_get(opthash, strlen(name)+1, (void *) name))) {		return(-1);	}	if(strcmp(strval, "false") == 0) {		*value = ps_false;	} else if(strcmp(strval, "true") == 0) {		*value = ps_true;	} else {		return(-2);	}	return(0);} /* }}} *//* get_optlist_element_as_string() {{{ * Retrieves an element from the hash and converts its value into string * Returns -1 if the parameter is not available */int get_optlist_element_as_string(PSDoc *psdoc, ght_hash_table_t *opthash, const char *name, char **value) {	char *strval;	if(NULL == opthash) {		return(-1);	}	if(NULL == (strval = ght_get(opthash, strlen(name)+1, (void *) name))) {		return(-1);	}	*value = strval;	return(0);} /* }}} *//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=2 ts=2 fdm=marker * vim<600: sw=2 ts=2 */

⌨️ 快捷键说明

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