ps: use a dictionary in selectfont_cached
[neatpost.git] / dev.c
blobe1ac5895081c92e756c231161b159d8b4162807c
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "post.h"
7 static char dev_dir[PATHLEN]; /* device directory */
8 static char dev_dev[PATHLEN]; /* output device name */
9 int dev_res; /* device resolution */
10 int dev_uwid; /* device unitwidth */
11 int dev_hor; /* minimum horizontal movement */
12 int dev_ver; /* minimum vertical movement */
14 /* mounted fonts */
15 static char fn_name[NFONTS][FNLEN]; /* font names */
16 static struct font *fn_font[NFONTS]; /* font structs */
17 static int fn_n; /* number of device fonts */
19 static void skipline(FILE* filp)
21 int c;
22 do {
23 c = getc(filp);
24 } while (c != '\n' && c != EOF);
27 int dev_mnt(int pos, char *id, char *name)
29 char path[PATHLEN];
30 struct font *fn;
31 if (pos >= NFONTS)
32 return -1;
33 sprintf(path, "%s/dev%s/%s", dev_dir, dev_dev, name);
34 fn = font_open(path);
35 if (!fn)
36 return -1;
37 if (fn_font[pos])
38 font_close(fn_font[pos]);
39 if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
40 strcpy(fn_name[pos], id);
41 fn_font[pos] = fn;
42 return pos;
45 int dev_open(char *dir, char *dev)
47 char path[PATHLEN];
48 char tok[ILNLEN];
49 int i;
50 FILE *desc;
51 strcpy(dev_dir, dir);
52 strcpy(dev_dev, dev);
53 sprintf(path, "%s/dev%s/DESC", dir, dev);
54 desc = fopen(path, "r");
55 if (!desc)
56 return 1;
57 while (fscanf(desc, "%s", tok) == 1) {
58 if (tok[0] == '#') {
59 skipline(desc);
60 continue;
62 if (!strcmp("fonts", tok)) {
63 fscanf(desc, "%d", &fn_n);
64 for (i = 0; i < fn_n; i++)
65 fscanf(desc, "%s", fn_name[i + 1]);
66 fn_n++;
67 continue;
69 if (!strcmp("sizes", tok)) {
70 while (fscanf(desc, "%s", tok) == 1)
71 if (!strcmp("0", tok))
72 break;
73 continue;
75 if (!strcmp("res", tok)) {
76 fscanf(desc, "%d", &dev_res);
77 continue;
79 if (!strcmp("unitwidth", tok)) {
80 fscanf(desc, "%d", &dev_uwid);
81 continue;
83 if (!strcmp("hor", tok)) {
84 fscanf(desc, "%d", &dev_hor);
85 continue;
87 if (!strcmp("ver", tok)) {
88 fscanf(desc, "%d", &dev_ver);
89 continue;
91 if (!strcmp("charset", tok))
92 break;
93 skipline(desc);
95 fclose(desc);
96 return 0;
99 void dev_close(void)
101 int i;
102 for (i = 0; i < NFONTS; i++) {
103 if (fn_font[i])
104 font_close(fn_font[i]);
105 fn_font[i] = NULL;
109 struct glyph *dev_glyph(char *c, int fn)
111 if (!strncmp("GID=", c, 4))
112 return font_glyph(fn_font[fn], c + 4);
113 return font_find(fn_font[fn], c);
116 /* return the font struct at pos */
117 struct font *dev_font(int pos)
119 return pos >= 0 && pos < NFONTS ? fn_font[pos] : NULL;
122 int charwid(int wid, int sz)
124 /* the original troff rounds the widths up */
125 return (wid * sz + dev_uwid / 2) / dev_uwid;
128 int dev_fontid(struct font *fn)
130 int i;
131 for (i = 0; i < NFONTS; i++)
132 if (fn_font[i] == fn)
133 return i;
134 return 0;