merge.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <libg.h>
  4. #include <bio.h>
  5. static void usage(void);
  6. static void snarf(char *, int);
  7. static void choose(Fontchar *, Bitmap *, int, int, int);
  8. struct {
  9. char *name;
  10. Bitmap *bm;
  11. Subfont *sf;
  12. } ft[1024];
  13. int nf;
  14. void
  15. main(int argc, char **argv)
  16. {
  17. int i, errs;
  18. Fontchar *fc;
  19. Bitmap *b;
  20. int nc, ht, as;
  21. Subfont *f;
  22. binit(0, 0, "font merge");
  23. if(argc < 1)
  24. usage();
  25. nf = argc-1;
  26. for(i = 0; i < nf; i++)
  27. snarf(argv[i+1], i);
  28. nc = ft[0].sf->n;
  29. ht = ft[0].sf->height;
  30. as = ft[0].sf->ascent;
  31. errs = 0;
  32. for(i = 0; i < nf; i++){
  33. if(nc < ft[i].sf->n) nc = ft[i].sf->n;
  34. if(ht != ft[1].sf->height){
  35. fprint(2, "%s: %s.height=%d (!= %s.height=%d)\n", argv[0],
  36. ft[i].name, ft[i].sf->height, ft[0].name, ht);
  37. errs = 1;
  38. }
  39. if(as != ft[1].sf->ascent){
  40. fprint(2, "%s: %s.ascent=%d (!= %s.ascent=%d)\n", argv[0],
  41. ft[i].name, ft[i].sf->ascent, ft[0].name, ht);
  42. errs = 1;
  43. }
  44. }
  45. if(errs)
  46. exits("param mismatch");
  47. fc = (Fontchar *)malloc(nc*sizeof(Fontchar));
  48. b = balloc(Rect(0, 0, nc*64, ht), ft[0].bm->ldepth);
  49. if(b == 0 || fc == 0){
  50. fprint(2, "%s: couldn't malloc %d chars\n", argv0, nc);
  51. exits("out of memory");
  52. }
  53. bitblt(b, b->r.min, b, b->r, Zero);
  54. choose(fc, b, nc, ht, as);
  55. wrbitmapfile(1, b);
  56. bitblt(&screen, screen.r.min, b, b->r, S); bflush();sleep(5000);
  57. f = subfalloc(nc, ht, as, fc, b, ~0, ~0);
  58. wrsubfontfile(1, f);
  59. exits(0);
  60. }
  61. static void
  62. usage(void)
  63. {
  64. fprint(2, "Usage: %s file ...\n", argv0);
  65. exits("usage");
  66. }
  67. static void
  68. snarf(char *name, int i)
  69. {
  70. int fd;
  71. Bitmap *b;
  72. ft[i].name = name;
  73. if((fd = open(name, OREAD)) < 0){
  74. perror(name);
  75. exits("font read");
  76. }
  77. if((b = rdbitmapfile(fd)) == 0){
  78. fprint(2, "rdbitmapfile failed\n");
  79. exits("font read");
  80. }
  81. if((ft[i].bm = balloc(b->r, b->ldepth)) == 0){
  82. fprint(2, "ballocsnarf failed\n");
  83. exits("font read");
  84. }
  85. bitblt(ft[i].bm, b->r.min, b, b->r, S);
  86. if((ft[i].sf = rdsubfontfile(fd, b)) == 0){
  87. fprint(2, "rdsubfontfile failed\n");
  88. exits("font read");
  89. }
  90. close(fd);
  91. }
  92. static void
  93. choose(Fontchar *f, Bitmap *b, int nc, int ht, int as)
  94. {
  95. int j;
  96. Fontchar *info;
  97. int lastx = 0;
  98. int w, n;
  99. for(n = 0; n < nc; n++, f++){
  100. f->x = lastx;
  101. for(j = 0; j < nf; j++){
  102. if(n >= ft[j].sf->n)
  103. continue;
  104. info = ft[j].sf->info;
  105. if(info[n+1].x != info[n].x)
  106. goto found;
  107. }
  108. continue;
  109. found:
  110. f->left = info[n].left;
  111. f->top = info[n].top;
  112. f->bottom = info[n].bottom;
  113. f->width = info[n].width;
  114. w = info[n+1].x - info[n].x;
  115. bitblt(b, Pt(0, lastx), ft[j].bm, Rect(0, info[n].x, ht, info[n+1].x), S);
  116. lastx += w;
  117. }
  118. f->x = lastx;
  119. }