ps: put the findfont call near scalefont and setfont calls
[neatpost.git] / dev.c
blob02cce40c7c9ae357ca2eef9d71e1bd6e5a13862a
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "post.h"
7 char dev_dir[PATHLEN]; /* device directory */
8 int dev_res; /* device resolution */
9 int dev_uwid; /* device unitwidth */
10 int dev_hor; /* minimum horizontal movement */
11 int dev_ver; /* minimum vertical movement */
13 /* mounted fonts */
14 static char fn_name[NFONTS][FNLEN]; /* font names */
15 static struct font *fn_font[NFONTS]; /* font structs */
16 static int fn_n; /* number of mounted fonts */
18 static void skipline(FILE* filp)
20 int c;
21 do {
22 c = getc(filp);
23 } while (c != '\n' && c != EOF);
26 int dev_mnt(int pos, char *id, char *name)
28 char path[PATHLEN];
29 struct font *fn;
30 sprintf(path, "%s/%s", dev_dir, name);
31 fn = font_open(path);
32 if (!fn)
33 return -1;
34 if (fn_font[pos])
35 font_close(fn_font[pos]);
36 if (fn_name[pos] != name) /* ignore if fn_name[pos] is passed */
37 strcpy(fn_name[pos], id);
38 fn_font[pos] = fn;
39 return pos;
42 int dev_open(char *dir)
44 char path[PATHLEN];
45 char tok[ILNLEN];
46 int i;
47 FILE *desc;
48 strcpy(dev_dir, dir);
49 sprintf(path, "%s/DESC", dir);
50 desc = fopen(path, "r");
51 while (fscanf(desc, "%s", tok) == 1) {
52 if (tok[0] == '#') {
53 skipline(desc);
54 continue;
56 if (!strcmp("fonts", tok)) {
57 fscanf(desc, "%d", &fn_n);
58 for (i = 0; i < fn_n; i++)
59 fscanf(desc, "%s", fn_name[i + 1]);
60 fn_n++;
61 continue;
63 if (!strcmp("sizes", tok)) {
64 while (fscanf(desc, "%s", tok) == 1)
65 if (!strcmp("0", tok))
66 break;
67 continue;
69 if (!strcmp("res", tok)) {
70 fscanf(desc, "%d", &dev_res);
71 continue;
73 if (!strcmp("unitwidth", tok)) {
74 fscanf(desc, "%d", &dev_uwid);
75 continue;
77 if (!strcmp("hor", tok)) {
78 fscanf(desc, "%d", &dev_hor);
79 continue;
81 if (!strcmp("ver", tok)) {
82 fscanf(desc, "%d", &dev_ver);
83 continue;
85 if (!strcmp("charset", tok)) {
86 break;
89 fclose(desc);
90 return 0;
93 void dev_close(void)
95 int i;
96 for (i = 0; i < fn_n; i++) {
97 if (fn_font[i])
98 font_close(fn_font[i]);
99 fn_font[i] = NULL;
103 struct glyph *dev_glyph(char *c, int fn)
105 struct glyph *g;
106 int i;
107 g = font_find(fn_font[fn], c);
108 if (g)
109 return g;
110 for (i = 0; i < fn_n; i++)
111 if (fn_font[i] && fn_font[i]->special)
112 if ((g = font_find(fn_font[i], c)))
113 return g;
114 return NULL;
117 struct glyph *dev_glyph_byid(char *id, int fn)
119 return font_glyph(fn_font[fn], id);
122 int dev_kernpair(char *c1, char *c2)
124 return 0;
127 /* return the font struct at pos */
128 struct font *dev_font(int pos)
130 return pos >= 0 && pos < fn_n ? fn_font[pos] : NULL;
133 int charwid(int wid, int sz)
135 /* the original troff rounds the widths up */
136 return (wid * sz + dev_uwid / 2) / dev_uwid;
139 int dev_fontid(struct font *fn)
141 int i;
142 for (i = 0; i < fn_n; i++)
143 if (fn_font[i] == fn)
144 return i;
145 return 0;