out: support internal links
[neatpost.git] / dev.c
blob034623980775684dd1a6ce4ca156ed412d4459bd
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 if (strchr(name, '/'))
34 strcpy(path, name);
35 else
36 sprintf(path, "%s/dev%s/%s", dev_dir, dev_dev, name);
37 fn = font_open(path);
38 if (!fn)
39 return -1;
40 if (fn_font[pos])
41 font_close(fn_font[pos]);
42 if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
43 snprintf(fn_name[pos], sizeof(fn_name[pos]), "%s", id);
44 fn_font[pos] = fn;
45 return pos;
48 int dev_open(char *dir, char *dev)
50 char path[PATHLEN];
51 char tok[ILNLEN];
52 int i;
53 FILE *desc;
54 strcpy(dev_dir, dir);
55 strcpy(dev_dev, dev);
56 sprintf(path, "%s/dev%s/DESC", dir, dev);
57 desc = fopen(path, "r");
58 if (!desc)
59 return 1;
60 while (fscanf(desc, "%s", tok) == 1) {
61 if (tok[0] == '#') {
62 skipline(desc);
63 continue;
65 if (!strcmp("fonts", tok)) {
66 fscanf(desc, "%d", &fn_n);
67 for (i = 0; i < fn_n; i++)
68 fscanf(desc, "%s", fn_name[i + 1]);
69 fn_n++;
70 continue;
72 if (!strcmp("sizes", tok)) {
73 while (fscanf(desc, "%s", tok) == 1)
74 if (!strcmp("0", tok))
75 break;
76 continue;
78 if (!strcmp("res", tok)) {
79 fscanf(desc, "%d", &dev_res);
80 continue;
82 if (!strcmp("unitwidth", tok)) {
83 fscanf(desc, "%d", &dev_uwid);
84 continue;
86 if (!strcmp("hor", tok)) {
87 fscanf(desc, "%d", &dev_hor);
88 continue;
90 if (!strcmp("ver", tok)) {
91 fscanf(desc, "%d", &dev_ver);
92 continue;
94 if (!strcmp("charset", tok))
95 break;
96 skipline(desc);
98 fclose(desc);
99 return 0;
102 void dev_close(void)
104 int i;
105 for (i = 0; i < NFONTS; i++) {
106 if (fn_font[i])
107 font_close(fn_font[i]);
108 fn_font[i] = NULL;
112 struct glyph *dev_glyph(char *c, int fn)
114 if (!strncmp("GID=", c, 4))
115 return font_glyph(fn_font[fn], c + 4);
116 return font_find(fn_font[fn], c);
119 /* return the font struct at pos */
120 struct font *dev_font(int pos)
122 return pos >= 0 && pos < NFONTS ? fn_font[pos] : NULL;
125 int dev_fontid(struct font *fn)
127 int i;
128 for (i = 0; i < NFONTS; i++)
129 if (fn_font[i] == fn)
130 return i;
131 return 0;