readsubfont.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. Subfont*
  5. readsubfonti(Display*d, char *name, int fd, Image *ai, int dolock)
  6. {
  7. char hdr[3*12+4+1];
  8. int n;
  9. uchar *p;
  10. Fontchar *fc;
  11. Subfont *f;
  12. Image *i;
  13. i = ai;
  14. if(i == nil){
  15. i = readimage(d, fd, dolock);
  16. if(i == nil)
  17. return nil;
  18. }
  19. if(read(fd, hdr, 3*12) != 3*12){
  20. if(ai == nil)
  21. freeimage(i);
  22. werrstr("rdsubfonfile: header read error: %r");
  23. return nil;
  24. }
  25. n = atoi(hdr);
  26. p = malloc(6*(n+1));
  27. if(p == nil)
  28. goto Err;
  29. if(read(fd, p, 6*(n+1)) != 6*(n+1)){
  30. werrstr("rdsubfonfile: fontchar read error: %r");
  31. Err:
  32. if(ai == nil)
  33. freeimage(i);
  34. free(p);
  35. return nil;
  36. }
  37. fc = malloc(sizeof(Fontchar)*(n+1));
  38. if(fc == nil)
  39. goto Err;
  40. _unpackinfo(fc, p, n);
  41. if(dolock)
  42. lockdisplay(d);
  43. f = allocsubfont(name, n, atoi(hdr+12), atoi(hdr+24), fc, i);
  44. if(dolock)
  45. unlockdisplay(d);
  46. if(f == nil){
  47. free(fc);
  48. goto Err;
  49. }
  50. free(p);
  51. return f;
  52. }
  53. Subfont*
  54. readsubfont(Display*d, char *name, int fd, int dolock)
  55. {
  56. return readsubfonti(d, name, fd, nil, dolock);
  57. }