Don't use .Xo/.Xc. Fix date format.
[netbsd-mini2440.git] / gnu / usr.bin / groff / xtotroff / xtotroff.c
blob72c8927b2fa68d06e04677d4617f8df3a4957057
1 /*
2 * xtotroff
4 * convert X font metrics into troff font metrics
5 */
7 # include <X11/Xlib.h>
8 # include <stdio.h>
9 # include <ctype.h>
10 # include "libXdvi/XFontName.h"
11 # include "libXdvi/DviChar.h"
13 # define charWidth(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].width)
14 # define charHeight(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].ascent)
15 # define charDepth(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].descent)
16 # define charLBearing(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].lbearing)
17 # define charRBearing(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].rbearing)
19 Display *dpy;
20 int groff_flag = 0;
22 int charExists (fi, c)
23 XFontStruct *fi;
24 int c;
26 XCharStruct *p;
28 if (c < fi->min_char_or_byte2 || c > fi->max_char_or_byte2)
29 return 0;
30 p = fi->per_char + (c - fi->min_char_or_byte2);
31 return (p->lbearing != 0 || p->rbearing != 0 || p->width != 0
32 || p->ascent != 0 || p->descent != 0 || p->attributes != 0);
35 MapFont (font_name, troff_name)
36 char *font_name;
37 char *troff_name;
39 XFontStruct *fi;
40 int count;
41 char **names;
42 FILE *out;
43 int c;
44 unsigned int attributes;
45 XFontName parsed;
46 int j, k;
47 DviCharNameMap *char_map;
48 char encoding[256];
49 char *s;
50 int wid;
52 names = XListFonts (dpy, font_name, 100000, &count);
53 if (count < 1) {
54 fprintf (stderr, "bad font name: %s\n", font_name);
55 return 0;
57 if (count > 1) {
58 fprintf (stderr, "ambiguous font name: %s\n", font_name);
59 return 0;
61 if (!XParseFontName (names[0], &parsed, &attributes)) {
62 fprintf (stderr, "not a standard name: %s\n", names[0]);
63 return 0;
65 sprintf (encoding, "%s-%s", parsed.CharSetRegistry,
66 parsed.CharSetEncoding);
67 for (s = encoding; *s; s++)
68 if (isupper (*s))
69 *s = tolower (*s);
70 char_map = DviFindMap (encoding);
71 if (!char_map) {
72 fprintf (stderr, "not a standard encoding: %s\n", encoding);
73 return 0;
76 out = fopen (troff_name, "w");
77 if (!out) {
78 perror (troff_name);
79 return 0;
81 fprintf (out, "name %s\n", troff_name);
82 if (!strcmp (char_map->encoding, "adobe-fontspecific"))
83 fprintf (out, "special\n");
84 fi = XLoadQueryFont (dpy, names[0]);
85 if (charExists (fi, ' ')) {
86 int w = charWidth (fi, ' ');
87 if (w > 0)
88 fprintf (out, "spacewidth %d\n", w);
90 fprintf (out, "charset\n");
91 for (c = fi->min_char_or_byte2; c <= fi->max_char_or_byte2; c++) {
92 char *name = DviCharName (char_map,c,0);
93 if (charExists (fi, c) && (groff_flag || name)) {
95 wid = charWidth (fi, c);
97 fprintf (out, "%s\t%d",
98 name ? name : "---",
99 wid);
100 if (groff_flag) {
101 int param[5];
102 param[0] = charHeight (fi, c);
103 param[1] = charDepth (fi, c);
104 param[2] = 0 /* charRBearing (fi, c) - wid */;
105 param[3] = 0 /* charLBearing (fi, c) */;
106 param[4] = 0; /* XXX */
107 for (j = 0; j < 5; j++)
108 if (param[j] < 0)
109 param[j] = 0;
110 for (j = 4; j >= 0; j--)
111 if (param[j] != 0)
112 break;
113 for (k = 0; k <= j; k++)
114 fprintf (out, ",%d", param[k]);
116 fprintf (out, "\t0\t0%o\n", c);
118 if (name) {
119 for (k = 1; DviCharName(char_map,c,k); k++) {
120 fprintf (out, "%s\t\"\n",
121 DviCharName (char_map,c,k));
126 XUnloadFont (dpy, fi->fid);
127 fclose (out);
128 return 1;
131 static usage(prog)
132 char *prog;
134 fprintf (stderr, "usage: %s [-g] FontMap\n", prog);
135 exit (1);
138 main (argc, argv)
139 char **argv;
141 char troff_name[1024];
142 char font_name[1024];
143 char line[1024];
144 char *a, *b, c;
145 int position;
146 FILE *map;
147 int opt;
148 extern int optind;
150 while ((opt = getopt(argc, argv, "g")) != EOF) {
151 switch (opt) {
152 case 'g':
153 groff_flag = 1;
154 break;
155 default:
156 usage();
159 if (argc - optind != 1)
160 usage(argv[0]);
162 dpy = XOpenDisplay (0);
163 if (!dpy) {
164 fprintf (stderr, "Can't connect to the X server.\n");
165 fprintf (stderr, "Make sure the DISPLAY environment variable is set correctly.\n");
166 exit (1);
168 position = 1;
170 map = fopen (argv[optind], "r");
171 if (map == NULL) {
172 perror (argv[optind]);
173 exit (1);
176 while (fgets (line, sizeof (line), map)) {
177 for (a=line,b=troff_name; *a; a++,b++) {
178 c = (*b = *a);
179 if (c == ' ' || c == '\t')
180 break;
182 *b = '\0';
183 while (*a && (*a == ' ' || *a == '\t'))
184 ++a;
185 for (b=font_name; *a; a++,b++)
186 if ((*b = *a) == '\n')
187 break;
188 *b = '\0';
189 printf ("%s -> %s\n", font_name, troff_name);
190 if (!MapFont (font_name, troff_name))
191 exit (1);
192 ++position;
194 exit (0);
198 Local Variables:
199 c-indent-level: 8
200 c-continued-statement-offset: 8
201 c-brace-offset: -8
202 c-argdecl-indent: 8
203 c-label-offset: -8
204 c-tab-always-indent: nil
205 End: