aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/misc/ibmfont.c
blob: 99d2f64c14e5a55b60b6c8a1bdbd0f38644d3263 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *
 * Program that converts IBM font files to a format that works on Unix systems.
 * Essentially all the information needed came from the Adobe paper "Supporting
 * Downloadable PostScript Fonts". To use the program type,
 *
 *	ibmfont font.ibm >font.unix
 *
 * where font.ibm is the font file, exactly as it came over from an IBM PC,
 * and font.unix is equivalent host resident font file usable on Unix systems.
 *
 */

#include <stdio.h>
#include <signal.h>

#define OFF		0
#define ON		1

#define NON_FATAL	0
#define FATAL		1

#define FALSE		0
#define TRUE		1

char	**argv;
int	argc;

char	*prog_name;

int	x_stat;
int	debug = OFF;
int	ignore = OFF;

FILE	*fp_in;
FILE	*fp_out;

/*****************************************************************************/

main(agc, agv)

    int		agc;
    char	*agv[];

{

/*
 *
 * IBM PC to Unix font converter.
 *
 */

    argc = agc;
    argv = agv;
    prog_name = argv[0];

    fp_in = stdin;
    fp_out = stdout;

    options();
    arguments();
    exit(x_stat);

}   /* End of main */

/*****************************************************************************/

options()

{

    int		ch;
    char	*names = "DI";

    extern char	*optarg;
    extern int	optind;

/*
 *
 * Command line options.
 *
 */

    while ( (ch = getopt(argc, argv, names)) != EOF ) {
	switch ( ch ) {
	    case 'D':			/* debug flag */
		    debug = ON;
		    break;

	    case 'I':			/* ignore FATAL errors */
		    ignore = ON;
		    break;

	    case '?':			/* don't understand the option */
		    error(FATAL, "");
		    break;

	    default:			/* don't know what to do for ch */
		    error(FATAL, "missing case for option %c\n", ch);
		    break;
	}   /* End switch */
    }   /* End while */

    argc -= optind;
    argv += optind;

}   /* End of options */

/*****************************************************************************/

arguments()

{

/*
 *
 * Everything esle is an input file. No arguments or '-' means stdin.
 *
 */


    if ( argc < 1 )
	conv();
    else
	while ( argc > 0 ) {
	    if ( strcmp(*argv, "-") == 0 )
		fp_in = stdin;
	    else if ( (fp_in = fopen(*argv, "r")) == NULL )
		error(FATAL, "can't open %s", *argv);
	    conv();
	    if ( fp_in != stdin )
		fclose(fp_in);
	    argc--;
	    argv++;
	}   /* End while */

}   /* End of arguments */

/*****************************************************************************/

conv()

{

    int		blocksize;
    int		blocktype;
    int		seg;
    long	ftell();

/*
 *
 * Font files on the IBM PC are stored in a compressed binary format. Individual
 * segments in the file are preceeded by a header that looks like,
 *
 *		Byte 1:		128
 *		Byte 2:		segment type (1=ASCII, 2=TOHEX, or 3=EOF)
 *		Bytes 3-6:	length of the segment
 *		Bytes 7 ...	data
 *
 */

    while ( 1 ) {
	seg = ftell(fp_in);
	if ( getc(fp_in) != 128 )
	    error(FATAL, "bad file format");
	blocktype = getc(fp_in);
	blocksize = getint(fp_in);
	if ( debug == ON ) {
	    fprintf(stderr, "blocktype = %d, blocksize = %d\n", blocktype, blocksize);
	    fprintf(stderr, "start=0%o, end=0%o\n", seg, seg+blocksize+6);
	    fprintf(stderr, "start=%d, end=%d\n", seg, seg+blocksize+6);
	}   /* End if */
	switch ( blocktype ) {
	    case 1:
		asciitext(blocksize);
		break;

	    case 2:
		hexdata(blocksize);
		break;

	    case 3:
		return;

	    default:
		error(FATAL, "unknown resource type %d", blocktype);
	}   /* End switch */
    }	/* End while */

}   /* End of conv */

/*****************************************************************************/

asciitext(count)

    int		count;			/* bytes left in the block */

{

    int		ch;
    int		i = 0;

/*
 *
 * Handles type 1 (ie. ASCII text) blocks. Changing carriage returns to newlines
 * is all I've done.
 *
 */

    for ( i = 0; i < count; i++ ) {
	if ( (ch = getc(fp_in)) == '\r' )
	    ch = '\n';
	putc(ch, fp_out);
    }	/* End for */

}   /* End of asciitext */

/*****************************************************************************/

hexdata(count)

    int		count;			/* bytes left in the block */

{

    int		i;
    int		n;

/*
 *
 * Reads the next count bytes and converts each byte to hex. Also starts a new
 * line every 80 hex characters.
 *
 */

    for ( i = 0, n = 0; i < count; i++ ) {
	fprintf(fp_out, "%.2X", getc(fp_in));
	if ( (++n % 40) == 0 )
	    putc('\n', fp_out);
    }	/* End for */

}   /* End of hexdata */

/*****************************************************************************/

getint()

{

    int		val;

/*
 *
 * Reads the next four bytes into an integer and returns the value to the caller.
 * First two bytes are probably always 0.
 *
 */

    val = getc(fp_in);
    val |= (getc(fp_in) << 8);
    val |= (getc(fp_in) << 16);
    val |= (getc(fp_in) << 24);

    return(val);

}   /* End of getint */

/*****************************************************************************/

error(kind, mesg, a1, a2, a3)

    int		kind;
    char	*mesg;
    unsigned	a1, a2, a3;

{

/*
 *
 * Print mesg and quit if kind is FATAL.
 *
 */

    if ( mesg != NULL && *mesg != '\0' ) {
	fprintf(stderr, "%s: ", prog_name);
	fprintf(stderr, mesg, a1, a2, a3);
	putc('\n', stderr);
    }	/* End if */

    if ( kind == FATAL && ignore == OFF )
	exit(x_stat | 01);

}   /* End of error */

/*****************************************************************************/