4 * convert X font metrics into troff font metrics
13 #include "XFontName.h"
22 #define charWidth(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].width)
23 #define charHeight(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].ascent)
24 #define charDepth(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].descent)
25 #define charLBearing(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].lbearing)
26 #define charRBearing(fi,c) ((fi)->per_char[(c) - (fi)->min_char_or_byte2].rbearing)
30 unsigned resolution = 75;
31 unsigned point_size = 10;
33 int charExists (fi, c)
39 if (c < fi->min_char_or_byte2 || c > fi->max_char_or_byte2)
41 p = fi->per_char + (c - fi->min_char_or_byte2);
42 return (p->lbearing != 0 || p->rbearing != 0 || p->width != 0
43 || p->ascent != 0 || p->descent != 0 || p->attributes != 0);
46 /* Canonicalize the font name by replacing scalable parts by *s. */
48 CanonicalizeFontName (font_name, canon_font_name)
49 char *font_name, *canon_font_name;
51 unsigned int attributes;
54 if (!XParseFontName(font_name, &parsed, &attributes)) {
55 fprintf (stderr, "not a standard name: %s\n", font_name);
59 attributes &= ~(FontNamePixelSize|FontNameAverageWidth
61 |FontNameResolutionX|FontNameResolutionY);
62 XFormatFontName(&parsed, attributes, canon_font_name);
66 int FontNamesAmbiguous(font_name, names, count)
71 char name1[2048], name2[2048];
77 for (i = 0; i < count; i++) {
78 if (!CanonicalizeFontName(names[i], i == 0 ? name1 : name2)) {
79 fprintf(stderr, "bad font name: %s\n", names[i]);
82 if (i > 0 && strcmp(name1, name2) != 0) {
83 fprintf(stderr, "ambiguous font name: %s\n", font_name);
84 fprintf(stderr, " matches %s\n", names[0]);
85 fprintf(stderr, " and %s\n", names[i]);
93 MapFont (font_name, troff_name)
102 unsigned int attributes;
105 DviCharNameMap *char_map;
109 char name_string[2048];
111 if (!XParseFontName(font_name, &parsed, &attributes)) {
112 fprintf (stderr, "not a standard name: %s\n", font_name);
116 attributes &= ~(FontNamePixelSize|FontNameAverageWidth);
117 attributes |= FontNameResolutionX;
118 attributes |= FontNameResolutionY;
119 attributes |= FontNamePointSize;
120 parsed.ResolutionX = resolution;
121 parsed.ResolutionY = resolution;
122 parsed.PointSize = point_size*10;
123 XFormatFontName(&parsed, attributes, name_string);
125 names = XListFonts (dpy, name_string, 100000, &count);
127 fprintf (stderr, "bad font name: %s\n", font_name);
131 if (FontNamesAmbiguous(font_name, names, count))
134 XParseFontName(names[0], &parsed, &attributes);
135 sprintf (encoding, "%s-%s", parsed.CharSetRegistry,
136 parsed.CharSetEncoding);
137 for (s = encoding; *s; s++)
140 char_map = DviFindMap (encoding);
142 fprintf (stderr, "not a standard encoding: %s\n", encoding);
146 fi = XLoadQueryFont (dpy, names[0]);
148 fprintf (stderr, "font does not exist: %s\n", names[0]);
152 printf ("%s -> %s\n", names[0], troff_name);
154 { /* Avoid race while opening file */
156 (void) unlink (troff_name);
157 fd = open (troff_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
158 out = fdopen (fd, "w");
165 fprintf (out, "name %s\n", troff_name);
166 if (!strcmp (char_map->encoding, "adobe-fontspecific"))
167 fprintf (out, "special\n");
168 if (charExists (fi, ' ')) {
169 int w = charWidth (fi, ' ');
171 fprintf (out, "spacewidth %d\n", w);
173 fprintf (out, "charset\n");
174 for (c = fi->min_char_or_byte2; c <= fi->max_char_or_byte2; c++) {
175 char *name = DviCharName (char_map,c,0);
176 if (charExists (fi, c) && (groff_flag || name)) {
178 wid = charWidth (fi, c);
180 fprintf (out, "%s\t%d",
185 param[0] = charHeight (fi, c);
186 param[1] = charDepth (fi, c);
187 param[2] = 0 /* charRBearing (fi, c) - wid */;
188 param[3] = 0 /* charLBearing (fi, c) */;
189 param[4] = 0; /* XXX */
190 for (j = 0; j < 5; j++)
193 for (j = 4; j >= 0; j--)
196 for (k = 0; k <= j; k++)
197 fprintf (out, ",%d", param[k]);
199 fprintf (out, "\t0\t0%o\n", c);
202 for (k = 1; DviCharName(char_map,c,k); k++) {
203 fprintf (out, "%s\t\"\n",
204 DviCharName (char_map,c,k));
209 XUnloadFont (dpy, fi->fid);
218 "usage: %s [-g] [-r resolution] [-s pointsize] FontMap\n",
224 /* For use by DviChar.c */
231 fprintf(stderr, "Out of memory\n");
240 char troff_name[1024];
241 char font_name[1024];
250 while ((opt = getopt(argc, argv, "gr:s:")) != EOF) {
256 sscanf(optarg, "%u", &resolution);
259 sscanf(optarg, "%u", &point_size);
265 if (argc - optind != 1)
268 dpy = XOpenDisplay (0);
270 fprintf (stderr, "Can't connect to the X server.\n");
271 fprintf (stderr, "Make sure the DISPLAY environment variable is set correctly.\n");
276 map = fopen (argv[optind], "r");
278 perror (argv[optind]);
282 while (fgets (line, sizeof (line), map)) {
283 for (a=line,b=troff_name; *a; a++,b++) {
285 if (c == ' ' || c == '\t')
289 while (*a && (*a == ' ' || *a == '\t'))
291 for (b=font_name; *a; a++,b++)
292 if ((*b = *a) == '\n')
295 if (!MapFont (font_name, troff_name))
305 c-continued-statement-offset: 8
309 c-tab-always-indent: nil