File: fonts.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (165 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: fonts.c,v 2.1 1997/01/05 18:41:39 papowell Exp $
 */

#include "portable.h"
#include "common.h"
#include "hp4.h"


/*
 * This function decides which font to download by reading the
 * MACHINESFONTS file.
 */
void selectfont()
{
	char *fntname, *t;
	int len;
	char fontpath[MAXPATHLEN];

	/* find font for current job */
	fntname = 0;
	if(zopts != NULL){
        if ((fntname=strstr(zopts,"font="))){
			log(1,"-Zfont option detected");
			if( (fntname=strchr(fntname,'=')) ){
				static char *dups;
				if( dups ) free(dups);
				dups = 0;

				fntname++;
				/* use a duplicate of the string */
				fntname = strdup(fntname);
				if( (t = strpbrk(fntname, " \t" )) ) *t = 0;
			}
		}
	}

	if( fntname == 0 || *fntname == 0 ) fntname = fnt_scan(host);
	if( fntname == 0 || *fntname == 0 ) fntname = default_font;
	if( fntname && *fntname == 0 ) fntname = 0;
		
	/* we will reset between jobs, so we just return */
	if( fntname == 0 ){
		log(4,"selectfont: NO default font, sending '%s'", PGRESET );
		len = strlen(PGRESET);
		if (writecn(STDOUT, PGRESET, len) != len){
			logerr(3,"Cannot send PGRESET string");
		}
		return;
	}
	strncpy( fontpath, FONTPATH, sizeof(fontpath)-2 );
	fontpath[sizeof(fontpath)-2] = 0;
	fontpath[sizeof(fontpath)-1] = 0;
	len = strlen( fontpath );
	if( len > 0 && fontpath[len-1] != '/' ){
		strcpy( fontpath+len, "/" );
		++len;
	}
	if( fntname[0] == '/' ){
		strncpy( fontpath, fntname, sizeof(fontpath)-2 );
	} else {
		strncpy( fontpath+len, fntname, sizeof(fontpath)-len-2 );
	}

	log(4,"selectfont: font '%s'", fontpath );
	sendfont(fontpath);

	/* select the font either downloaded or from previous job */
	t = SELFONT;
	len = strlen( t );
	if (writecn(STDOUT, t, len ) != len ) {
		log(2,"Cannot send SELFONT string '%s'", SELFONT );
	} else {
		log(2,"Font selected successfully");
	}
	log(4,"selectfont: done");
}

char *fnt_scan( mchn )
char *mchn;
{
	FILE *fontfp;
	char *s, *name, *dir, *file;
	static char *end_value;
	int len;
	char fontpath[MAXPATHLEN];
	char fileline[MAXPATHLEN];

	if( mchn == 0 || *mchn == 0 ) return( (char *) 0 );

	strncpy( fontpath, FONTPATH, sizeof(fontpath)-2 );
	fontpath[sizeof(fontpath)-2] = 0;
	fontpath[sizeof(fontpath)-1] = 0;
	len = strlen( fontpath );
	if( len > 0 && fontpath[len-1] != '/' ){
		strcpy( fontpath+len, "/" );
		++len;
	}
	s = MACHINESFONTS;
	if( s[0] == '/' ){
		strncpy( fontpath, s, sizeof(fontpath)-2 );
	} else {
		strncpy( fontpath+len, s, sizeof(fontpath)-len-2 );
	}
	log(4, "Opening host font file '%s'", fontpath);
	if( (fontfp = fopen( fontpath, "r" )) == NULL ){
		log(2, "fnt_scan: missing host font file '%s'", fontpath );
		return( (char *)0 );
	}

	while( fgets( fileline, sizeof(fileline)-1, fontfp ) ) {
		log(4, "line '%s'", fileline);
		name = fileline;
		if( (s = strchr( name, '\n' ) ) ) *s = 0;
		while( isspace(*name) ) ++name;
		if ( *name == '#' )	continue;
		dir = strpbrk(name, " \t" );
		if( dir == 0 ) continue;
		while( isspace( *dir ) ) *dir++ = 0;
		if( *dir == 0 ) continue;
		log(4,"fnt_scan: checking '%s' = '%s', dir '%s'", mchn, name, dir );
		if( strcasecmp( mchn, name ) ) continue;
		file = strpbrk(dir, " \t" );
		if( file ){
			*file++ = 0;
			while( isspace(*file) ) ++file;
			s = strpbrk(file, " \t" );
			if( s ) *s = 0;
		}
		log(4,"fnt_scan: dir '%s' file '%s'", dir, file );
		strncpy( fontpath, FONTPATH, sizeof(fontpath)-2 );
		fontpath[sizeof(fontpath)-2] = 0;
		fontpath[sizeof(fontpath)-1] = 0;
		len = strlen( fontpath );
		if( len > 0 && fontpath[len-1] != '/' ){
			strcpy( fontpath+len, "/" );
			++len;
		}
		if( dir[0] == '/' ){
			strncpy( fontpath, dir, sizeof(fontpath)-2 );
		} else {
			strncpy( fontpath+len, dir, sizeof(fontpath)-len-2 );
		}
		if( file && *file ){
			len = strlen( fontpath );
			if( len > 0 && fontpath[len-1] != '/' ){
				strcpy( fontpath+len, "/" );
				++len;
			}
			strncpy( fontpath+len, file, sizeof(fontpath)-len -2);
		}
		break;
	}

	fclose( fontfp );
	if( end_value ) free( end_value );
	end_value = strdup( fontpath );
	return( end_value );
}