openmemsubfont.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include <memdraw.h>
  13. Memsubfont*
  14. openmemsubfont(char *name)
  15. {
  16. Memsubfont *sf;
  17. Memimage *i;
  18. Fontchar *fc;
  19. int fd, n;
  20. char hdr[3*12+4+1];
  21. uint8_t *p;
  22. fd = open(name, OREAD);
  23. if(fd < 0)
  24. return nil;
  25. p = nil;
  26. i = readmemimage(fd);
  27. if(i == nil)
  28. goto Err;
  29. if(read(fd, hdr, 3*12) != 3*12){
  30. werrstr("openmemsubfont: header read error: %r");
  31. goto Err;
  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("openmemsubfont: fontchar read error: %r");
  39. goto Err;
  40. }
  41. fc = malloc(sizeof(Fontchar)*(n+1));
  42. if(fc == nil)
  43. goto Err;
  44. _unpackinfo(fc, p, n);
  45. sf = allocmemsubfont(name, n, atoi(hdr+12), atoi(hdr+24), fc, i);
  46. if(sf == nil){
  47. free(fc);
  48. goto Err;
  49. }
  50. free(p);
  51. return sf;
  52. Err:
  53. close(fd);
  54. if (i != nil)
  55. freememimage(i);
  56. if (p != nil)
  57. free(p);
  58. return nil;
  59. }