defont.c 1.7 KB

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