conv_gb.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifdef PLAN9
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <bio.h>
  5. #else
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "plan9.h"
  9. #endif
  10. #include "hdr.h"
  11. #include "conv.h"
  12. #include "gb.h"
  13. /*
  14. a state machine for interpreting gb.
  15. */
  16. void
  17. gbproc(int c, Rune **r, long input_loc)
  18. {
  19. static enum { state0, state1 } state = state0;
  20. static int lastc;
  21. long n, ch, cold = c;
  22. switch(state)
  23. {
  24. case state0: /* idle state */
  25. if(c < 0)
  26. return;
  27. if(c >= 0xA1){
  28. lastc = c;
  29. state = state1;
  30. return;
  31. }
  32. emit(c);
  33. return;
  34. case state1: /* seen a font spec */
  35. if(c >= 0xA1)
  36. n = (lastc-0xA0)*100 + (c-0xA0);
  37. else {
  38. nerrors++;
  39. if(squawk)
  40. EPR "%s: bad gb glyph %d (from 0x%x,0x%lx) near byte %ld in %s\n", argv0, c-0xA0, lastc, cold, input_loc, file);
  41. if(!clean)
  42. emit(BADMAP);
  43. state = state0;
  44. return;
  45. }
  46. ch = tabgb[n];
  47. if(ch < 0){
  48. nerrors++;
  49. if(squawk)
  50. EPR "%s: unknown gb %ld (from 0x%x,0x%lx) near byte %ld in %s\n", argv0, n, lastc, cold, input_loc, file);
  51. if(!clean)
  52. emit(BADMAP);
  53. } else
  54. emit(ch);
  55. state = state0;
  56. }
  57. }
  58. void
  59. gb_in(int fd, long *notused, struct convert *out)
  60. {
  61. Rune ob[N];
  62. Rune *r, *re;
  63. uchar ibuf[N];
  64. int n, i;
  65. long nin;
  66. USED(notused);
  67. r = ob;
  68. re = ob+N-3;
  69. nin = 0;
  70. while((n = read(fd, ibuf, sizeof ibuf)) > 0){
  71. for(i = 0; i < n; i++){
  72. gbproc(ibuf[i], &r, nin++);
  73. if(r >= re){
  74. OUT(out, ob, r-ob);
  75. r = ob;
  76. }
  77. }
  78. if(r > ob){
  79. OUT(out, ob, r-ob);
  80. r = ob;
  81. }
  82. }
  83. gbproc(-1, &r, nin);
  84. if(r > ob)
  85. OUT(out, ob, r-ob);
  86. }
  87. void
  88. gb_out(Rune *base, int n, long *notused)
  89. {
  90. char *p;
  91. int i;
  92. Rune r;
  93. static int first = 1;
  94. USED(notused);
  95. if(first){
  96. first = 0;
  97. for(i = 0; i < NRUNE; i++)
  98. tab[i] = -1;
  99. for(i = 0; i < GBMAX; i++)
  100. if(tabgb[i] != -1)
  101. tab[tabgb[i]] = i;
  102. }
  103. nrunes += n;
  104. p = obuf;
  105. for(i = 0; i < n; i++){
  106. r = base[i];
  107. if(r < 128)
  108. *p++ = r;
  109. else {
  110. if(tab[r] != -1){
  111. r = tab[r];
  112. *p++ = 0xA0 + (r/100);
  113. *p++ = 0xA0 + (r%100);
  114. continue;
  115. }
  116. if(squawk)
  117. EPR "%s: rune 0x%x not in output cs\n", argv0, r);
  118. nerrors++;
  119. if(clean)
  120. continue;
  121. *p++ = BYTEBADMAP;
  122. }
  123. }
  124. noutput += p-obuf;
  125. if(p > obuf)
  126. write(1, obuf, p-obuf);
  127. }