defont.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "lib9.h"
  2. #include "draw.h"
  3. #include "memdraw.h"
  4. Memsubfont*
  5. getmemdefont(void)
  6. {
  7. char *hdr, *p;
  8. int n;
  9. Fontchar *fc;
  10. Memsubfont *f;
  11. int ld;
  12. Rectangle r;
  13. Memdata *md;
  14. Memimage *i;
  15. /*
  16. * make sure data is word-aligned. this is true with Plan 9 compilers
  17. * but not in general. the byte order is right because the data is
  18. * declared as char*, not ulong*.
  19. */
  20. p = (char*)defontdata;
  21. n = (ulong)p & 3;
  22. if(n != 0){
  23. memmove(p+(4-n), p, sizeofdefont-n);
  24. p += 4-n;
  25. }
  26. ld = atoi(p+0*12);
  27. r.min.x = atoi(p+1*12);
  28. r.min.y = atoi(p+2*12);
  29. r.max.x = atoi(p+3*12);
  30. r.max.y = atoi(p+4*12);
  31. md = mallocz(sizeof(Memdata), 1);
  32. if(md == nil)
  33. return nil;
  34. p += 5*12;
  35. md->base = nil; /* so freememimage doesn't free p */
  36. md->bdata = (uchar*)p; /* ick */
  37. md->ref = 1;
  38. md->allocd = 1; /* so freememimage does free md */
  39. i = allocmemimaged(r, drawld2chan[ld], md);
  40. if(i == nil){
  41. free(md);
  42. return nil;
  43. }
  44. hdr = p+Dy(r)*i->width*sizeof(ulong);
  45. n = atoi(hdr);
  46. p = hdr+3*12;
  47. fc = malloc(sizeof(Fontchar)*(n+1));
  48. if(fc == 0){
  49. freememimage(i);
  50. return 0;
  51. }
  52. _unpackinfo(fc, (uchar*)p, n);
  53. f = allocmemsubfont("*default*", n, atoi(hdr+12), atoi(hdr+24), fc, i);
  54. if(f == 0){
  55. freememimage(i);
  56. free(fc);
  57. return 0;
  58. }
  59. return f;
  60. }