openfont.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. Font*
  13. openfont(Display *d, char *name)
  14. {
  15. Font *fnt;
  16. int fd, i, n;
  17. char *buf;
  18. Dir *dir;
  19. fd = open(name, OREAD);
  20. if(fd < 0)
  21. return 0;
  22. dir = dirfstat(fd);
  23. if(dir == nil){
  24. Err0:
  25. close(fd);
  26. return 0;
  27. }
  28. n = dir->length;
  29. free(dir);
  30. buf = malloc(n+1);
  31. if(buf == 0)
  32. goto Err0;
  33. buf[n] = 0;
  34. i = read(fd, buf, n);
  35. close(fd);
  36. if(i != n){
  37. free(buf);
  38. return 0;
  39. }
  40. fnt = buildfont(d, buf, name);
  41. free(buf);
  42. return fnt;
  43. }
  44. Font*
  45. opendefaultfont(Display *d)
  46. {
  47. Font *fnt;
  48. fnt = openfont(d, getenv("font"));
  49. if(fnt == nil)
  50. openfont(d, "/lib/font/bit/pelm/latin1.8.font");
  51. return fnt;
  52. }