getdefont.c 1.0 KB

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