defont.c 1.3 KB

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