text.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "e.h"
  2. #include "y.tab.h"
  3. #include <ctype.h>
  4. #define CSSIZE 1000
  5. char cs[CSSIZE+20]; /* text string converted into this */
  6. char *csp; /* next spot in cs[] */
  7. char *psp; /* next character in input token */
  8. int lf, rf; /* temporary spots for left and right fonts */
  9. int lastft; /* last \f added */
  10. int nextft; /* next \f to be added */
  11. int pclass; /* class of previous character */
  12. int nclass; /* class of next character */
  13. int class[LAST][LAST] ={ /* guesswork, tuned to times roman postscript */
  14. /*OT OL IL DG LP RP SL PL IF IJ VB */
  15. /*OT*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0 }, /* OTHER */
  16. /*OL*/ { 1, 0, 1, 1, 1, 1, 1, 2, 2, 2, 0 }, /* OLET */
  17. /*IL*/ { 1, 1, 0, 1, 1, 1, 1, 3, 2, 1, 0 }, /* ILET */
  18. /*DG*/ { 1, 1, 1, 0, 1, 1, 1, 2, 2, 2, 0 }, /* DIG */
  19. /*LP*/ { 1, 1, 1, 1, 1, 2, 1, 2, 3, 3, 0 }, /* LPAR */
  20. /*RP*/ { 2, 2, 2, 1, 1, 1, 1, 2, 3, 3, 0 }, /* RPAR */
  21. /*SL*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 0 }, /* SLASH */
  22. /*PL*/ { 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 0 }, /* PLUS */
  23. /*IF*/ { 3, 3, 1, 2, 2, 3, 2, 3, 0, 1, 1 }, /* ILETF */
  24. /*IJ*/ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0 }, /* ILETJ */
  25. /*VB*/ { 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 1 }, /* VBAR */
  26. };
  27. extern void shim(int, int);
  28. extern void roman(int);
  29. extern void sadd(char *);
  30. extern void cadd(int);
  31. extern int trans(int, char *);
  32. int textc(void) /* read next UTF rune from psp */
  33. {
  34. wchar_t r;
  35. int w;
  36. w = mbtowc(&r, psp, 3);
  37. if(w == 0){
  38. psp++;
  39. return 0;
  40. }
  41. if(w < 0){
  42. psp += 1;
  43. return 0x80; /* Plan 9-ism */
  44. }
  45. psp += w;
  46. return r;
  47. }
  48. void text(int t, char *p1) /* convert text string p1 of type t */
  49. {
  50. int c;
  51. char *p;
  52. tbl *tp;
  53. yyval = salloc();
  54. ebase[yyval] = 0;
  55. eht[yyval] = EM(1.0, ps); /* ht in ems of orig size */
  56. lfont[yyval] = rfont[yyval] = ROM;
  57. lclass[yyval] = rclass[yyval] = OTHER;
  58. if (t == QTEXT) {
  59. for (p = p1; *p; p++) /* scan for embedded \f's */
  60. if (*p == '\\' && *(p+1) == 'f')
  61. break;
  62. if (*p) /* if found \f, leave it alone and hope */
  63. p = p1;
  64. else {
  65. sprintf(cs, "\\f%s%s\\fP", ftp->name, p1);
  66. p = cs;
  67. }
  68. } else if (t == SPACE)
  69. p = "\\ ";
  70. else if (t == THIN)
  71. p = "\\|";
  72. else if (t == TAB)
  73. p = "\\t";
  74. else if ((tp = lookup(restbl, p1)) != NULL) {
  75. p = tp->cval;
  76. } else {
  77. lf = rf = 0;
  78. lastft = 0;
  79. nclass = NONE; /* get started with no class == no pad */
  80. csp = cs;
  81. for (psp = p1; (c = textc()) != '\0'; ) {
  82. nextft = ft;
  83. pclass = nclass;
  84. rf = trans(c, p1);
  85. if (lf == 0) {
  86. lf = rf; /* left stuff is first found */
  87. lclass[yyval] = nclass;
  88. }
  89. if (csp-cs > CSSIZE)
  90. ERROR "converted token %.25s... too long", p1 FATAL ;
  91. }
  92. sadd("\\fP");
  93. *csp = '\0';
  94. p = cs;
  95. lfont[yyval] = lf;
  96. rfont[yyval] = rf;
  97. rclass[yyval] = nclass;
  98. }
  99. dprintf(".\t%dtext: S%d <- %s; b=%g,h=%g,lf=%c,rf=%c,ps=%d\n",
  100. t, yyval, p, ebase[yyval], eht[yyval], lfont[yyval], rfont[yyval], ps);
  101. printf(".ds %d \"%s\n", yyval, p);
  102. }
  103. int isalpharune(int c)
  104. {
  105. return ('a'<=c && c<='z') || ('A'<=c && c<='Z');
  106. }
  107. int isdigitrune(int c)
  108. {
  109. return ('0'<=c && c<='9');
  110. }
  111. trans(int c, char *p1)
  112. {
  113. int f;
  114. if (isalpharune(c) && ft == ITAL && c != 'f' && c != 'j') { /* italic letter */
  115. shim(pclass, nclass = ILET);
  116. cadd(c);
  117. return ITAL;
  118. }
  119. if (isalpharune(c) && ft != ITAL) { /* other letter */
  120. shim(pclass, nclass = OLET);
  121. cadd(c);
  122. return ROM;
  123. }
  124. if (isdigitrune(c)) {
  125. shim(pclass, nclass = DIG);
  126. roman(c);
  127. return ROM; /* this is the right side font of this object */
  128. }
  129. f = ROM;
  130. nclass = OTHER;
  131. switch (c) {
  132. case ':': case ';': case '!': case '%': case '?':
  133. shim(pclass, nclass);
  134. roman(c);
  135. return f;
  136. case '(': case '[':
  137. shim(pclass, nclass = LPAR);
  138. roman(c);
  139. return f;
  140. case ')': case ']':
  141. shim(pclass, nclass = RPAR);
  142. roman(c);
  143. return f;
  144. case ',':
  145. shim(pclass, nclass = OTHER);
  146. roman(c);
  147. return f;
  148. case '.':
  149. if (rf == ROM)
  150. roman(c);
  151. else
  152. cadd(c);
  153. return f;
  154. case '|': /* postscript needs help with default width! */
  155. shim(pclass, nclass = VBAR);
  156. sadd("\\v'.17m'\\z|\\v'-.17m'\\|"); /* and height */
  157. return f;
  158. case '=':
  159. shim(pclass, nclass = PLUS);
  160. sadd("\\(eq");
  161. return f;
  162. case '+':
  163. shim(pclass, nclass = PLUS);
  164. sadd("\\(pl");
  165. return f;
  166. case '>':
  167. case '<': /* >, >=, >>, <, <-, <=, << */
  168. shim(pclass, nclass = PLUS);
  169. if (*psp == '=') {
  170. sadd(c == '<' ? "\\(<=" : "\\(>=");
  171. psp++;
  172. } else if (c == '<' && *psp == '-') { /* <- only */
  173. sadd("\\(<-");
  174. psp++;
  175. } else if (*psp == c) { /* << or >> */
  176. cadd(c);
  177. cadd(c);
  178. psp++;
  179. } else {
  180. cadd(c);
  181. }
  182. return f;
  183. case '-':
  184. shim(pclass, nclass = PLUS); /* probably too big for ->'s */
  185. if (*psp == '>') {
  186. sadd("\\(->");
  187. psp++;
  188. } else {
  189. sadd("\\(mi");
  190. }
  191. return f;
  192. case '/':
  193. shim(pclass, nclass = SLASH);
  194. cadd('/');
  195. return f;
  196. case '~':
  197. case ' ':
  198. sadd("\\|\\|");
  199. return f;
  200. case '^':
  201. sadd("\\|");
  202. return f;
  203. case '\\': /* troff - pass only \(xx without comment */
  204. shim(pclass, nclass);
  205. cadd('\\');
  206. cadd(c = *psp++);
  207. if (c == '(' && *psp && *(psp+1)) {
  208. cadd(*psp++);
  209. cadd(*psp++);
  210. } else
  211. fprintf(stderr, "eqn warning: unquoted troff command \\%c, file %s:%d\n",
  212. c, curfile->fname, curfile->lineno);
  213. return f;
  214. case '\'':
  215. shim(pclass, nclass);
  216. sadd("\\(fm");
  217. return f;
  218. case 'f':
  219. if (ft == ITAL) {
  220. shim(pclass, nclass = ILETF);
  221. cadd('f');
  222. f = ITAL;
  223. } else
  224. cadd('f');
  225. return f;
  226. case 'j':
  227. if (ft == ITAL) {
  228. shim(pclass, nclass = ILETJ);
  229. cadd('j');
  230. f = ITAL;
  231. } else
  232. cadd('j');
  233. return f;
  234. default:
  235. shim(pclass, nclass);
  236. cadd(c);
  237. return ft==ITAL ? ITAL : ROM;
  238. }
  239. }
  240. char *pad(int n) /* return the padding as a string */
  241. {
  242. static char buf[20];
  243. buf[0] = 0;
  244. if (n < 0) {
  245. sprintf(buf, "\\h'-%du*\\w'\\^'u'", -n);
  246. return buf;
  247. }
  248. for ( ; n > 1; n -= 2)
  249. strcat(buf, "\\|");
  250. if (n > 0)
  251. strcat(buf, "\\^");
  252. return buf;
  253. }
  254. void shim(int lc, int rc) /* add padding space suitable to left and right classes */
  255. {
  256. sadd(pad(class[lc][rc]));
  257. }
  258. void roman(int c) /* add char c in "roman" font */
  259. {
  260. nextft = ROM;
  261. cadd(c);
  262. }
  263. void sadd(char *s) /* add string s to cs */
  264. {
  265. while (*s)
  266. cadd(*s++);
  267. }
  268. void cadd(int c) /* add character c to end of cs */
  269. {
  270. char *p;
  271. int w;
  272. if (lastft != nextft) {
  273. if (lastft != 0) {
  274. *csp++ = '\\';
  275. *csp++ = 'f';
  276. *csp++ = 'P';
  277. }
  278. *csp++ = '\\';
  279. *csp++ = 'f';
  280. if (ftp == ftstack) { /* bottom level */
  281. if (ftp->ft == ITAL) /* usual case */
  282. *csp++ = nextft;
  283. else /* gfont set, use it */
  284. for (p = ftp->name; *csp = *p++; )
  285. csp++;
  286. } else { /* inside some kind of font ... */
  287. for (p = ftp->name; *csp = *p++; )
  288. csp++;
  289. }
  290. lastft = nextft;
  291. }
  292. w = wctomb(csp, c);
  293. if(w > 0) /* ignore bad characters */
  294. csp += w;
  295. }