summaryrefslogtreecommitdiff
path: root/src/utils/xtotroff/xtotroff.c
blob: b3d5b02e95f144a973e9a7836ebb0106af6fd418 (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
/*
 * xtotroff
 *
 * convert X font metrics into troff font metrics
 */

#include	<X11/Xlib.h>
#include	<stdio.h>
#include	<ctype.h>
#include	<unistd.h>
#include	<stdlib.h>
#include	<getopt.h>
#include	<string.h>
#include	<fcntl.h>
#include	"XFontName.h"
#include	"DviChar.h"

#ifdef X_NOT_STDC_ENV
char *malloc();
#else 
#include <stdlib.h>
#endif

#define charWidth(fi,c)	((fi)->per_char[(c) - (fi)->min_char_or_byte2].width)
#define charHeight(fi,c)	((fi)->per_char[(c) - (fi)->min_char_or_byte2].ascent)
#define charDepth(fi,c)	((fi)->per_char[(c) - (fi)->min_char_or_byte2].descent)
#define charLBearing(fi,c)	((fi)->per_char[(c) - (fi)->min_char_or_byte2].lbearing)
#define charRBearing(fi,c)	((fi)->per_char[(c) - (fi)->min_char_or_byte2].rbearing)

Display		*dpy;
int		groff_flag = 0;
unsigned	resolution = 75;
unsigned	point_size = 10;

int charExists (XFontStruct *fi, int c)
{
	XCharStruct *p;

	/* `c' is always >= 0 */
	if ((unsigned int)c < fi->min_char_or_byte2
	    || (unsigned int)c > fi->max_char_or_byte2)
		return 0;
	p = fi->per_char + (c - fi->min_char_or_byte2);
	return (p->lbearing != 0 || p->rbearing != 0 || p->width != 0
		|| p->ascent != 0 || p->descent != 0 || p->attributes != 0);
}

/* Canonicalize the font name by replacing scalable parts by *s. */

static int
CanonicalizeFontName (char *font_name, char *canon_font_name)
{
	unsigned int	attributes;
	XFontName	parsed;

	if (!XParseFontName(font_name, &parsed, &attributes)) {
		fprintf (stderr, "not a standard name: %s\n", font_name);
		return 0;
	}

	attributes &= ~(FontNamePixelSize|FontNameAverageWidth
			|FontNamePointSize
			|FontNameResolutionX|FontNameResolutionY);
	XFormatFontName(&parsed, attributes, canon_font_name);
	return 1;
}

static int
FontNamesAmbiguous(const char *font_name, char **names, int count)
{
	char name1[2048], name2[2048];
	int i;

	if (count == 1)
		return 0;

	for (i = 0; i < count; i++) {
		if (!CanonicalizeFontName(names[i], i == 0 ? name1 : name2)) {
			fprintf(stderr, "bad font name: %s\n", names[i]);
			return 1;
		}
		if (i > 0 && strcmp(name1, name2) != 0) {
			fprintf(stderr, "ambiguous font name: %s\n", font_name);
			fprintf(stderr, "  matches %s\n", names[0]);
			fprintf(stderr, "  and %s\n", names[i]);
			return 1;
		}

	}
	return 0;
}

static int
MapFont (char *font_name, const char *troff_name)
{
	XFontStruct	*fi;
	int		count;
	char		**names;
	FILE		*out;
	unsigned int	c;
	unsigned int	attributes;
	XFontName	parsed;
	int		j, k;
	DviCharNameMap	*char_map;
	char		encoding[256];
	char		*s;
	int		wid;
	char		name_string[2048];

	if (!XParseFontName(font_name, &parsed, &attributes)) {
		fprintf (stderr, "not a standard name: %s\n", font_name);
		return 0;
	}

	attributes &= ~(FontNamePixelSize|FontNameAverageWidth);
	attributes |= FontNameResolutionX;
	attributes |= FontNameResolutionY;
	attributes |= FontNamePointSize;
	parsed.ResolutionX = resolution;
	parsed.ResolutionY = resolution;
	parsed.PointSize = point_size*10;
	XFormatFontName(&parsed, attributes, name_string);

	names = XListFonts (dpy, name_string, 100000, &count);
	if (count < 1) {
		fprintf (stderr, "bad font name: %s\n", font_name);
		return 0;
	}

	if (FontNamesAmbiguous(font_name, names, count))
		return 0;

	XParseFontName(names[0], &parsed, &attributes);
	sprintf (encoding, "%s-%s", parsed.CharSetRegistry,
				    parsed.CharSetEncoding);
	for (s = encoding; *s; s++)
		if (isupper (*s))
			*s = tolower (*s);
	char_map = DviFindMap (encoding);
	if (!char_map) {
		fprintf (stderr, "not a standard encoding: %s\n", encoding);
		return 0;
	}

	fi = XLoadQueryFont (dpy, names[0]);
	if (!fi) {
		fprintf (stderr, "font does not exist: %s\n", names[0]);
		return 0;
	}
		
	printf ("%s -> %s\n", names[0], troff_name);

	{ /* Avoid race while opening file */
		int fd;
		(void) unlink (troff_name);
		fd = open (troff_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
		out = fdopen (fd, "w");
	}

	if (!out) {
		perror (troff_name);
		return 0;
	}
	fprintf (out, "name %s\n", troff_name);
	if (!strcmp (char_map->encoding, "adobe-fontspecific"))
		fprintf (out, "special\n");
	if (charExists (fi, ' ')) {
		int w = charWidth (fi, ' ');
		if (w > 0)
			fprintf (out, "spacewidth %d\n", w);
	}
	fprintf (out, "charset\n");
	for (c = fi->min_char_or_byte2; c <= fi->max_char_or_byte2; c++) {
		const char *name = DviCharName (char_map, c, 0);
		if (charExists (fi, c) && (groff_flag || name)) {

			wid = charWidth (fi, c);

			fprintf (out, "%s\t%d",
					name ? name : "---",
	 				wid);
			if (groff_flag) {
				int	param[5];
				param[0] = charHeight (fi, c);
				param[1] = charDepth (fi, c);
				param[2] = 0 /* charRBearing (fi, c) - wid */;
				param[3] = 0 /* charLBearing (fi, c) */;
				param[4] = 0; /* XXX */
				for (j = 0; j < 5; j++)
					if (param[j] < 0)
						param[j] = 0;
				for (j = 4; j >= 0; j--)
					if (param[j] != 0)
						break;
				for (k = 0; k <= j; k++)
					fprintf (out, ",%d", param[k]);
			}
			fprintf (out, "\t0\t0%o\n", c);
			
			if (name) {
				for (k = 1; DviCharName(char_map,c,k); k++) {
					fprintf (out, "%s\t\"\n",
						 DviCharName (char_map,c,k));
				}
			}
		}
	}
	XUnloadFont (dpy, fi->fid);
	fclose (out);
	return 1;
}

static void
usage(const char *prog)
{
	fprintf (stderr,
		 "usage: %s [-g] [-r resolution] [-s pointsize] FontMap\n",
		 prog);
	exit (1);
}

int
main (int argc, char **argv)
{
	char	troff_name[1024];
	char	font_name[1024];
	char	line[1024];
	char	*a, *b, c;
	int	position;
	FILE	*map;
	int	opt;

	while ((opt = getopt(argc, argv, "gr:s:")) != EOF) {
		switch (opt) {
		case 'g':
			groff_flag = 1;
			break;
		case 'r':
			sscanf(optarg, "%u", &resolution);
			break;
		case 's':
			sscanf(optarg, "%u", &point_size);
			break;
		default:
			usage(argv[0]);
		}
	}
	if (argc - optind != 1)
		usage(argv[0]);

	dpy = XOpenDisplay (0);
	if (!dpy) {
		fprintf (stderr, "Can't connect to the X server.\n");
		fprintf (stderr, "Make sure the DISPLAY environment variable is set correctly.\n");
		exit (1);
	}
        position = 1;

	map = fopen (argv[optind], "r");
	if (map == NULL) {
		perror (argv[optind]);
		exit (1);
	}

	while (fgets (line, sizeof (line), map)) {
		for (a=line,b=troff_name; *a; a++,b++) {
			c = (*b = *a);
			if (c == ' ' || c == '\t')
				break;
		}
		*b = '\0';
		while (*a && (*a == ' ' || *a == '\t'))
			++a;
		for (b=font_name; *a; a++,b++)
			if ((*b = *a) == '\n')
				break;
		*b = '\0';
		if (!MapFont (font_name, troff_name))
			exit (1);
		++position;
	}
	exit (0);
}

/*
Local Variables:
c-indent-level: 8
c-continued-statement-offset: 8
c-brace-offset: -8
c-argdecl-indent: 8
c-label-offset: -8
c-tab-always-indent: nil
End:
*/