readsubfont.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. Subfont*
  13. readsubfonti(Display*d, char *name, int fd, Image *ai, int dolock)
  14. {
  15. char hdr[3*12+4+1];
  16. int n;
  17. uint8_t *p;
  18. Fontchar *fc;
  19. Subfont *f;
  20. Image *i;
  21. i = ai;
  22. if(i == nil){
  23. i = readimage(d, fd, dolock);
  24. if(i == nil)
  25. return nil;
  26. }
  27. if(read(fd, hdr, 3*12) != 3*12){
  28. if(ai == nil)
  29. freeimage(i);
  30. werrstr("rdsubfonfile: header read error: %r");
  31. return nil;
  32. }
  33. n = atoi(hdr);
  34. p = malloc(6*(n+1));
  35. if(p == nil)
  36. goto Err;
  37. if(read(fd, p, 6*(n+1)) != 6*(n+1)){
  38. werrstr("rdsubfonfile: fontchar read error: %r");
  39. Err:
  40. if(ai == nil)
  41. freeimage(i);
  42. free(p);
  43. return nil;
  44. }
  45. fc = malloc(sizeof(Fontchar)*(n+1));
  46. if(fc == nil)
  47. goto Err;
  48. _unpackinfo(fc, p, n);
  49. if(dolock)
  50. lockdisplay(d);
  51. f = allocsubfont(name, n, atoi(hdr+12), atoi(hdr+24), fc, i);
  52. if(dolock)
  53. unlockdisplay(d);
  54. if(f == nil){
  55. free(fc);
  56. goto Err;
  57. }
  58. free(p);
  59. return f;
  60. }
  61. Subfont*
  62. readsubfont(Display *d, char *name, int fd, int dolock)
  63. {
  64. return readsubfonti(d, name, fd, nil, dolock);
  65. }