text.c 6.9 KB

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