Blame


1 76193d7c 2003-09-30 devnull #include <u.h>
2 76193d7c 2003-09-30 devnull #include <libc.h>
3 76193d7c 2003-09-30 devnull #include <draw.h>
4 76193d7c 2003-09-30 devnull
5 161060a4 2003-10-11 devnull extern vlong _drawflength(int);
6 9b4a2324 2009-07-09 rsc int _fontpipe(char*);
7 161060a4 2003-10-11 devnull
8 213fc4f6 2015-02-17 rsc int
9 213fc4f6 2015-02-17 rsc parsefontscale(char *name, char **base)
10 213fc4f6 2015-02-17 rsc {
11 213fc4f6 2015-02-17 rsc char *p;
12 213fc4f6 2015-02-17 rsc int scale;
13 213fc4f6 2015-02-17 rsc
14 213fc4f6 2015-02-17 rsc p = name;
15 213fc4f6 2015-02-17 rsc scale = 0;
16 213fc4f6 2015-02-17 rsc while('0' <= *p && *p <= '9') {
17 213fc4f6 2015-02-17 rsc scale = scale*10 + *p - '0';
18 213fc4f6 2015-02-17 rsc p++;
19 213fc4f6 2015-02-17 rsc }
20 213fc4f6 2015-02-17 rsc if(*p == '*' && scale > 0)
21 213fc4f6 2015-02-17 rsc *base = p+1;
22 213fc4f6 2015-02-17 rsc else {
23 213fc4f6 2015-02-17 rsc *base = name;
24 213fc4f6 2015-02-17 rsc scale = 1;
25 213fc4f6 2015-02-17 rsc }
26 213fc4f6 2015-02-17 rsc return scale;
27 213fc4f6 2015-02-17 rsc }
28 213fc4f6 2015-02-17 rsc
29 76193d7c 2003-09-30 devnull Font*
30 213fc4f6 2015-02-17 rsc openfont1(Display *d, char *name)
31 76193d7c 2003-09-30 devnull {
32 76193d7c 2003-09-30 devnull Font *fnt;
33 77f23268 2015-02-17 rsc int fd, i, n, scale;
34 213fc4f6 2015-02-17 rsc char *buf, *nambuf, *fname, *freename;
35 76193d7c 2003-09-30 devnull
36 e6f4b2e3 2003-10-13 devnull nambuf = 0;
37 213fc4f6 2015-02-17 rsc freename = nil;
38 213fc4f6 2015-02-17 rsc scale = parsefontscale(name, &fname);
39 e6f4b2e3 2003-10-13 devnull
40 213fc4f6 2015-02-17 rsc fd = open(fname, OREAD);
41 213fc4f6 2015-02-17 rsc if(fd < 0 && strncmp(fname, "/lib/font/bit/", 14) == 0){
42 213fc4f6 2015-02-17 rsc nambuf = smprint("#9/font/%s", fname+14);
43 e6f4b2e3 2003-10-13 devnull if(nambuf == nil)
44 e6f4b2e3 2003-10-13 devnull return 0;
45 8ad51794 2004-03-25 devnull nambuf = unsharp(nambuf);
46 8ad51794 2004-03-25 devnull if(nambuf == nil)
47 8ad51794 2004-03-25 devnull return 0;
48 e6f4b2e3 2003-10-13 devnull if((fd = open(nambuf, OREAD)) < 0){
49 e6f4b2e3 2003-10-13 devnull free(nambuf);
50 e6f4b2e3 2003-10-13 devnull return 0;
51 e6f4b2e3 2003-10-13 devnull }
52 213fc4f6 2015-02-17 rsc fname = nambuf;
53 213fc4f6 2015-02-17 rsc if(scale > 1) {
54 213fc4f6 2015-02-17 rsc name = smprint("%d*%s", scale, fname);
55 213fc4f6 2015-02-17 rsc freename = name;
56 213fc4f6 2015-02-17 rsc } else {
57 213fc4f6 2015-02-17 rsc name = fname;
58 213fc4f6 2015-02-17 rsc }
59 e6f4b2e3 2003-10-13 devnull }
60 9b4a2324 2009-07-09 rsc if(fd >= 0)
61 9b4a2324 2009-07-09 rsc n = _drawflength(fd);
62 213fc4f6 2015-02-17 rsc if(fd < 0 && strncmp(fname, "/mnt/font/", 10) == 0) {
63 213fc4f6 2015-02-17 rsc fd = _fontpipe(fname+10);
64 775cb933 2015-08-27 rsc n = 128*1024;
65 9b4a2324 2009-07-09 rsc }
66 76193d7c 2003-09-30 devnull if(fd < 0)
67 76193d7c 2003-09-30 devnull return 0;
68 76193d7c 2003-09-30 devnull
69 76193d7c 2003-09-30 devnull buf = malloc(n+1);
70 76193d7c 2003-09-30 devnull if(buf == 0){
71 76193d7c 2003-09-30 devnull close(fd);
72 e6f4b2e3 2003-10-13 devnull free(nambuf);
73 76193d7c 2003-09-30 devnull return 0;
74 76193d7c 2003-09-30 devnull }
75 9b4a2324 2009-07-09 rsc i = readn(fd, buf, n);
76 76193d7c 2003-09-30 devnull close(fd);
77 9b4a2324 2009-07-09 rsc if(i <= 0){
78 76193d7c 2003-09-30 devnull free(buf);
79 e6f4b2e3 2003-10-13 devnull free(nambuf);
80 76193d7c 2003-09-30 devnull return 0;
81 76193d7c 2003-09-30 devnull }
82 9b4a2324 2009-07-09 rsc buf[i] = 0;
83 76193d7c 2003-09-30 devnull fnt = buildfont(d, buf, name);
84 76193d7c 2003-09-30 devnull free(buf);
85 e6f4b2e3 2003-10-13 devnull free(nambuf);
86 213fc4f6 2015-02-17 rsc free(freename);
87 77f23268 2015-02-17 rsc if(scale != 1) {
88 77f23268 2015-02-17 rsc fnt->scale = scale;
89 77f23268 2015-02-17 rsc fnt->height *= scale;
90 77f23268 2015-02-17 rsc fnt->ascent *= scale;
91 77f23268 2015-02-17 rsc fnt->width *= scale;
92 77f23268 2015-02-17 rsc }
93 76193d7c 2003-09-30 devnull return fnt;
94 76193d7c 2003-09-30 devnull }
95 9b4a2324 2009-07-09 rsc
96 213fc4f6 2015-02-17 rsc void
97 213fc4f6 2015-02-17 rsc swapfont(Font *targ, Font **oldp, Font **newp)
98 213fc4f6 2015-02-17 rsc {
99 213fc4f6 2015-02-17 rsc Font f, *old, *new;
100 213fc4f6 2015-02-17 rsc
101 213fc4f6 2015-02-17 rsc if(targ != *oldp)
102 213fc4f6 2015-02-17 rsc sysfatal("bad swapfont %p %p %p", targ, *oldp, *newp);
103 213fc4f6 2015-02-17 rsc
104 213fc4f6 2015-02-17 rsc old = *oldp;
105 213fc4f6 2015-02-17 rsc new = *newp;
106 213fc4f6 2015-02-17 rsc
107 213fc4f6 2015-02-17 rsc f.name = old->name;
108 213fc4f6 2015-02-17 rsc f.display = old->display;
109 213fc4f6 2015-02-17 rsc f.height = old->height;
110 213fc4f6 2015-02-17 rsc f.ascent = old->ascent;
111 213fc4f6 2015-02-17 rsc f.width = old->width;
112 213fc4f6 2015-02-17 rsc f.nsub = old->nsub;
113 213fc4f6 2015-02-17 rsc f.age = old->age;
114 213fc4f6 2015-02-17 rsc f.maxdepth = old->maxdepth;
115 213fc4f6 2015-02-17 rsc f.ncache = old->ncache;
116 213fc4f6 2015-02-17 rsc f.nsubf = old->nsubf;
117 213fc4f6 2015-02-17 rsc f.scale = old->scale;
118 213fc4f6 2015-02-17 rsc f.cache = old->cache;
119 213fc4f6 2015-02-17 rsc f.subf = old->subf;
120 213fc4f6 2015-02-17 rsc f.sub = old->sub;
121 213fc4f6 2015-02-17 rsc f.cacheimage = old->cacheimage;
122 213fc4f6 2015-02-17 rsc
123 213fc4f6 2015-02-17 rsc old->name = new->name;
124 213fc4f6 2015-02-17 rsc old->display = new->display;
125 213fc4f6 2015-02-17 rsc old->height = new->height;
126 213fc4f6 2015-02-17 rsc old->ascent = new->ascent;
127 213fc4f6 2015-02-17 rsc old->width = new->width;
128 213fc4f6 2015-02-17 rsc old->nsub = new->nsub;
129 213fc4f6 2015-02-17 rsc old->age = new->age;
130 213fc4f6 2015-02-17 rsc old->maxdepth = new->maxdepth;
131 213fc4f6 2015-02-17 rsc old->ncache = new->ncache;
132 213fc4f6 2015-02-17 rsc old->nsubf = new->nsubf;
133 213fc4f6 2015-02-17 rsc old->scale = new->scale;
134 213fc4f6 2015-02-17 rsc old->cache = new->cache;
135 213fc4f6 2015-02-17 rsc old->subf = new->subf;
136 213fc4f6 2015-02-17 rsc old->sub = new->sub;
137 213fc4f6 2015-02-17 rsc old->cacheimage = new->cacheimage;
138 213fc4f6 2015-02-17 rsc
139 213fc4f6 2015-02-17 rsc new->name = f.name;
140 213fc4f6 2015-02-17 rsc new->display = f.display;
141 213fc4f6 2015-02-17 rsc new->height = f.height;
142 213fc4f6 2015-02-17 rsc new->ascent = f.ascent;
143 213fc4f6 2015-02-17 rsc new->width = f.width;
144 213fc4f6 2015-02-17 rsc new->nsub = f.nsub;
145 213fc4f6 2015-02-17 rsc new->age = f.age;
146 213fc4f6 2015-02-17 rsc new->maxdepth = f.maxdepth;
147 213fc4f6 2015-02-17 rsc new->ncache = f.ncache;
148 213fc4f6 2015-02-17 rsc new->nsubf = f.nsubf;
149 213fc4f6 2015-02-17 rsc new->scale = f.scale;
150 213fc4f6 2015-02-17 rsc new->cache = f.cache;
151 213fc4f6 2015-02-17 rsc new->subf = f.subf;
152 213fc4f6 2015-02-17 rsc new->sub = f.sub;
153 213fc4f6 2015-02-17 rsc new->cacheimage = f.cacheimage;
154 213fc4f6 2015-02-17 rsc
155 213fc4f6 2015-02-17 rsc *oldp = new;
156 213fc4f6 2015-02-17 rsc *newp = old;
157 213fc4f6 2015-02-17 rsc }
158 213fc4f6 2015-02-17 rsc
159 79555a99 2015-02-17 rsc static char*
160 79555a99 2015-02-17 rsc hidpiname(Font *f)
161 79555a99 2015-02-17 rsc {
162 79555a99 2015-02-17 rsc char *p, *q;
163 79555a99 2015-02-17 rsc int size;
164 79555a99 2015-02-17 rsc
165 79555a99 2015-02-17 rsc // If font name has form x,y return y.
166 79555a99 2015-02-17 rsc p = strchr(f->namespec, ',');
167 79555a99 2015-02-17 rsc if(p != nil)
168 79555a99 2015-02-17 rsc return strdup(p+1);
169 79555a99 2015-02-17 rsc
170 79555a99 2015-02-17 rsc // If font name is /mnt/font/Name/Size/font, scale Size.
171 79555a99 2015-02-17 rsc if(strncmp(f->name, "/mnt/font/", 10) == 0) {
172 79555a99 2015-02-17 rsc p = strchr(f->name+10, '/');
173 79555a99 2015-02-17 rsc if(p == nil || *++p < '0' || *p > '9')
174 79555a99 2015-02-17 rsc goto scale;
175 79555a99 2015-02-17 rsc q = p;
176 79555a99 2015-02-17 rsc size = 0;
177 79555a99 2015-02-17 rsc while('0' <= *q && *q <= '9')
178 79555a99 2015-02-17 rsc size = size*10 + *q++ - '0';
179 79555a99 2015-02-17 rsc return smprint("%.*s%d%s", utfnlen(f->name, p-f->name), f->name, size*2, q);
180 79555a99 2015-02-17 rsc }
181 79555a99 2015-02-17 rsc
182 79555a99 2015-02-17 rsc // Otherwise use pixel doubling.
183 79555a99 2015-02-17 rsc scale:
184 79555a99 2015-02-17 rsc return smprint("%d*%s", f->scale*2, f->name);
185 79555a99 2015-02-17 rsc }
186 79555a99 2015-02-17 rsc
187 213fc4f6 2015-02-17 rsc void
188 213fc4f6 2015-02-17 rsc loadhidpi(Font *f)
189 213fc4f6 2015-02-17 rsc {
190 213fc4f6 2015-02-17 rsc char *name;
191 213fc4f6 2015-02-17 rsc Font *fnew;
192 213fc4f6 2015-02-17 rsc
193 213fc4f6 2015-02-17 rsc if(f->hidpi == f)
194 213fc4f6 2015-02-17 rsc return;
195 213fc4f6 2015-02-17 rsc if(f->hidpi != nil) {
196 213fc4f6 2015-02-17 rsc swapfont(f, &f->lodpi, &f->hidpi);
197 213fc4f6 2015-02-17 rsc return;
198 213fc4f6 2015-02-17 rsc }
199 213fc4f6 2015-02-17 rsc
200 79555a99 2015-02-17 rsc name = hidpiname(f);
201 213fc4f6 2015-02-17 rsc fnew = openfont1(f->display, name);
202 213fc4f6 2015-02-17 rsc if(fnew == nil)
203 213fc4f6 2015-02-17 rsc return;
204 213fc4f6 2015-02-17 rsc f->hidpi = fnew;
205 213fc4f6 2015-02-17 rsc free(name);
206 213fc4f6 2015-02-17 rsc
207 213fc4f6 2015-02-17 rsc swapfont(f, &f->lodpi, &f->hidpi);
208 213fc4f6 2015-02-17 rsc }
209 213fc4f6 2015-02-17 rsc
210 213fc4f6 2015-02-17 rsc Font*
211 213fc4f6 2015-02-17 rsc openfont(Display *d, char *name)
212 213fc4f6 2015-02-17 rsc {
213 213fc4f6 2015-02-17 rsc Font *f;
214 79555a99 2015-02-17 rsc char *p;
215 79555a99 2015-02-17 rsc char *namespec;
216 213fc4f6 2015-02-17 rsc
217 79555a99 2015-02-17 rsc // If font name has form x,y use x for lodpi, y for hidpi.
218 79555a99 2015-02-17 rsc name = strdup(name);
219 79555a99 2015-02-17 rsc namespec = strdup(name);
220 79555a99 2015-02-17 rsc if((p = strchr(name, ',')) != nil)
221 79555a99 2015-02-17 rsc *p = '\0';
222 79555a99 2015-02-17 rsc
223 213fc4f6 2015-02-17 rsc f = openfont1(d, name);
224 25822e8d 2015-05-15 rsc if(!f)
225 25822e8d 2015-05-15 rsc return nil;
226 213fc4f6 2015-02-17 rsc f->lodpi = f;
227 79555a99 2015-02-17 rsc f->namespec = namespec;
228 213fc4f6 2015-02-17 rsc
229 213fc4f6 2015-02-17 rsc /* add to display list for when dpi changes */
230 213fc4f6 2015-02-17 rsc /* d can be nil when invoked from mc. */
231 213fc4f6 2015-02-17 rsc if(d != nil) {
232 213fc4f6 2015-02-17 rsc f->ondisplaylist = 1;
233 213fc4f6 2015-02-17 rsc f->prev = d->lastfont;
234 213fc4f6 2015-02-17 rsc f->next = nil;
235 213fc4f6 2015-02-17 rsc if(f->prev)
236 213fc4f6 2015-02-17 rsc f->prev->next = f;
237 213fc4f6 2015-02-17 rsc else
238 213fc4f6 2015-02-17 rsc d->firstfont = f;
239 213fc4f6 2015-02-17 rsc d->lastfont = f;
240 213fc4f6 2015-02-17 rsc
241 213fc4f6 2015-02-17 rsc /* if this is a hi-dpi display, find hi-dpi version and swap */
242 213fc4f6 2015-02-17 rsc if(d->dpi >= DefaultDPI*3/2)
243 213fc4f6 2015-02-17 rsc loadhidpi(f);
244 213fc4f6 2015-02-17 rsc }
245 79555a99 2015-02-17 rsc
246 79555a99 2015-02-17 rsc free(name);
247 213fc4f6 2015-02-17 rsc
248 213fc4f6 2015-02-17 rsc return f;
249 213fc4f6 2015-02-17 rsc }
250 213fc4f6 2015-02-17 rsc
251 9b4a2324 2009-07-09 rsc int
252 9b4a2324 2009-07-09 rsc _fontpipe(char *name)
253 9b4a2324 2009-07-09 rsc {
254 9b4a2324 2009-07-09 rsc int p[2];
255 9b4a2324 2009-07-09 rsc char c;
256 9b4a2324 2009-07-09 rsc char buf[1024], *argv[10];
257 9b4a2324 2009-07-09 rsc int nbuf, pid;
258 9b4a2324 2009-07-09 rsc
259 9b4a2324 2009-07-09 rsc if(pipe(p) < 0)
260 9b4a2324 2009-07-09 rsc return -1;
261 9b4a2324 2009-07-09 rsc pid = rfork(RFNOWAIT|RFFDG|RFPROC);
262 9b4a2324 2009-07-09 rsc if(pid < 0) {
263 9b4a2324 2009-07-09 rsc close(p[0]);
264 9b4a2324 2009-07-09 rsc close(p[1]);
265 9b4a2324 2009-07-09 rsc return -1;
266 9b4a2324 2009-07-09 rsc }
267 9b4a2324 2009-07-09 rsc if(pid == 0) {
268 9b4a2324 2009-07-09 rsc close(p[0]);
269 9b4a2324 2009-07-09 rsc dup(p[1], 1);
270 9b4a2324 2009-07-09 rsc dup(p[1], 2);
271 9b4a2324 2009-07-09 rsc if(p[1] > 2)
272 9b4a2324 2009-07-09 rsc close(p[1]);
273 9b4a2324 2009-07-09 rsc argv[0] = "fontsrv";
274 9b4a2324 2009-07-09 rsc argv[1] = "-pp";
275 9b4a2324 2009-07-09 rsc argv[2] = name;
276 9b4a2324 2009-07-09 rsc argv[3] = nil;
277 9b4a2324 2009-07-09 rsc execvp("fontsrv", argv);
278 9b4a2324 2009-07-09 rsc print("exec fontsrv: %r\n");
279 9b4a2324 2009-07-09 rsc _exit(0);
280 9b4a2324 2009-07-09 rsc }
281 9b4a2324 2009-07-09 rsc close(p[1]);
282 9b4a2324 2009-07-09 rsc
283 9b4a2324 2009-07-09 rsc // success marked with leading \001.
284 9b4a2324 2009-07-09 rsc // otherwise an error happened.
285 9b4a2324 2009-07-09 rsc for(nbuf=0; nbuf<sizeof buf-1; nbuf++) {
286 9b4a2324 2009-07-09 rsc if(read(p[0], &c, 1) < 1 || c == '\n') {
287 9b4a2324 2009-07-09 rsc buf[nbuf] = '\0';
288 9b4a2324 2009-07-09 rsc werrstr(buf);
289 9b4a2324 2009-07-09 rsc close(p[0]);
290 9b4a2324 2009-07-09 rsc return -1;
291 9b4a2324 2009-07-09 rsc }
292 9b4a2324 2009-07-09 rsc if(c == '\001')
293 9b4a2324 2009-07-09 rsc break;
294 9b4a2324 2009-07-09 rsc }
295 9b4a2324 2009-07-09 rsc return p[0];
296 9b4a2324 2009-07-09 rsc }