getdefont.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. Subfont*
  5. getdefont(Display *d)
  6. {
  7. char *hdr, *p;
  8. int n;
  9. Fontchar *fc;
  10. Subfont *f;
  11. int ld;
  12. Rectangle r;
  13. Image *i;
  14. /*
  15. * make sure data is word-aligned. this is true with Plan 9 compilers
  16. * but not in general. the byte order is right because the data is
  17. * declared as char*, not ulong*.
  18. */
  19. p = (char*)defontdata;
  20. n = (ulong)p & 3;
  21. if(n != 0){
  22. memmove(p+(4-n), p, sizeofdefont-n);
  23. p += 4-n;
  24. }
  25. ld = atoi(p+0*12);
  26. r.min.x = atoi(p+1*12);
  27. r.min.y = atoi(p+2*12);
  28. r.max.x = atoi(p+3*12);
  29. r.max.y = atoi(p+4*12);
  30. i = allocimage(d, r, drawld2chan[ld], 0, 0);
  31. if(i == 0)
  32. return 0;
  33. p += 5*12;
  34. n = loadimage(i, r, (uchar*)p, (defontdata+sizeofdefont)-(uchar*)p);
  35. if(n < 0){
  36. freeimage(i);
  37. return 0;
  38. }
  39. hdr = p+n;
  40. n = atoi(hdr);
  41. p = hdr+3*12;
  42. fc = malloc(sizeof(Fontchar)*(n+1));
  43. if(fc == 0){
  44. freeimage(i);
  45. return 0;
  46. }
  47. _unpackinfo(fc, (uchar*)p, n);
  48. f = allocsubfont("*default*", n, atoi(hdr+12), atoi(hdr+24), fc, i);
  49. if(f == 0){
  50. freeimage(i);
  51. free(fc);
  52. return 0;
  53. }
  54. return f;
  55. }