treadsubfont.c - plan9port - [fork] Plan 9 from user space | |
git clone git://src.adamsgaard.dk/plan9port | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
treadsubfont.c (1264B) | |
--- | |
1 #include <u.h> | |
2 #include <libc.h> | |
3 #include <draw.h> | |
4 | |
5 Subfont* | |
6 readsubfonti(Display*d, char *name, int fd, Image *ai, int dolock) | |
7 { | |
8 char hdr[3*12+4+1]; | |
9 int n; | |
10 uchar *p; | |
11 Fontchar *fc; | |
12 Subfont *f; | |
13 Image *i; | |
14 | |
15 i = ai; | |
16 if(i == nil){ | |
17 i = readimage(d, fd, dolock); | |
18 if(i == nil) | |
19 return nil; | |
20 } | |
21 if(read(fd, hdr, 3*12) != 3*12){ | |
22 if(ai == nil) | |
23 freeimage(i); | |
24 werrstr("rdsubfonfile: header read error: %r"); | |
25 return nil; | |
26 } | |
27 n = atoi(hdr); | |
28 p = malloc(6*(n+1)); | |
29 if(p == nil) | |
30 goto Err; | |
31 if(read(fd, p, 6*(n+1)) != 6*(n+1)){ | |
32 werrstr("rdsubfonfile: fontchar read error: %r"); | |
33 Err: | |
34 if(ai == nil) | |
35 freeimage(i); | |
36 free(p); | |
37 return nil; | |
38 } | |
39 fc = malloc(sizeof(Fontchar)*(n+1)); | |
40 if(fc == nil) | |
41 goto Err; | |
42 _unpackinfo(fc, p, n); | |
43 if(dolock) | |
44 lockdisplay(d); | |
45 f = allocsubfont(name, n, atoi(hdr+12), atoi(hdr+24), fc, i); | |
46 if(dolock) | |
47 unlockdisplay(d); | |
48 if(f == nil){ | |
49 free(fc); | |
50 goto Err; | |
51 } | |
52 free(p); | |
53 return f; | |
54 } | |
55 | |
56 Subfont* | |
57 readsubfont(Display*d, char *name, int fd, int dolock) | |
58 { | |
59 return readsubfonti(d, name, fd, nil, dolock); | |
60 } | |
61 | |
62 void | |
63 _unpackinfo(Fontchar *fc, uchar *p, int n) | |
64 { | |
65 int j; | |
66 | |
67 for(j=0; j<=n; j++){ | |
68 fc->x = p[0]|(p[1]<<8); | |
69 fc->top = p[2]; | |
70 fc->bottom = p[3]; | |
71 fc->left = p[4]; | |
72 fc->width = p[5]; | |
73 fc++; | |
74 p += 6; | |
75 } | |
76 } |